Class: RR::Space

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/space.rb

Overview

RR::Space is a Dependency Injection en.wikipedia.org/wiki/Dependency_injection and global state object for the RR framework. The RR::Space.instance is a singleton that holds the state.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpace

Returns a new instance of Space.



20
21
22
23
24
# File 'lib/rr/space.rb', line 20

def initialize
  @double_insertions = HashWithObjectIdKey.new
  @ordered_scenarios = []
  @trim_backtrace = false
end

Class Attribute Details

.instanceObject



7
8
9
# File 'lib/rr/space.rb', line 7

def instance
  @instance ||= new
end

Instance Attribute Details

#double_insertionsObject (readonly)

Returns the value of attribute double_insertions.



18
19
20
# File 'lib/rr/space.rb', line 18

def double_insertions
  @double_insertions
end

#ordered_scenariosObject (readonly)

Returns the value of attribute ordered_scenarios.



18
19
20
# File 'lib/rr/space.rb', line 18

def ordered_scenarios
  @ordered_scenarios
end

#trim_backtraceObject

Returns the value of attribute trim_backtrace.



19
20
21
# File 'lib/rr/space.rb', line 19

def trim_backtrace
  @trim_backtrace
end

Instance Method Details

#double_insertion(object, method_name) ⇒ Object

Reuses or creates, if none exists, a DoubleInsertion for the passed in object and method_name. When a DoubleInsertion is created, it binds the dispatcher to the object.



56
57
58
59
60
61
62
63
64
# File 'lib/rr/space.rb', line 56

def double_insertion(object, method_name)
  double_insertion = @double_insertions[object][method_name.to_sym]
  return double_insertion if double_insertion

  double_insertion = DoubleInsertion.new(self, object, method_name.to_sym)
  @double_insertions[object][method_name.to_sym] = double_insertion
  double_insertion.bind
  double_insertion
end

#register_ordered_scenario(scenario) ⇒ Object

Registers the ordered Double to be verified.



67
68
69
# File 'lib/rr/space.rb', line 67

def register_ordered_scenario(scenario)
  @ordered_scenarios << scenario
end

#resetObject

Resets the registered Doubles and ordered Doubles



99
100
101
102
# File 'lib/rr/space.rb', line 99

def reset
  reset_ordered_scenarios
  reset_double_insertions
end

#reset_double(object, method_name) ⇒ Object

Resets the DoubleInsertion for the passed in object and method_name.



112
113
114
115
116
# File 'lib/rr/space.rb', line 112

def reset_double(object, method_name)
  double_insertion = @double_insertions[object].delete(method_name)
  @double_insertions.delete(object) if @double_insertions[object].empty?
  double_insertion.reset
end

#scenario(double_insertion, definition = scenario_definition) ⇒ Object

Creates and registers a Double to be verified.



41
42
43
44
45
46
# File 'lib/rr/space.rb', line 41

def scenario(double_insertion, definition = scenario_definition)
  scenario = Double.new(self, double_insertion, definition)
  scenario.definition.scenario = scenario
  double_insertion.register_scenario scenario
  scenario
end

#scenario_creatorObject

Creates a DoubleCreator.



36
37
38
# File 'lib/rr/space.rb', line 36

def scenario_creator
  DoubleCreator.new(self)
end

#scenario_definitionObject



48
49
50
# File 'lib/rr/space.rb', line 48

def scenario_definition
  DoubleDefinition.new(self)
end

#scenario_method_proxy(creator, object, method_name = nil, &definition) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/rr/space.rb', line 26

def scenario_method_proxy(creator, object, method_name=nil, &definition)
  if method_name && definition
    raise ArgumentError, "Cannot pass in a method name and a block"
  end
  proxy = DoubleMethodProxy.new(self, creator, object, &definition)
  return proxy unless method_name
  proxy.__send__(method_name)
end

#verify_double(object, method_name) ⇒ Object

Verifies the DoubleInsertion for the passed in object and method_name.



105
106
107
108
109
# File 'lib/rr/space.rb', line 105

def verify_double(object, method_name)
  @double_insertions[object][method_name].verify
ensure
  reset_double object, method_name
end

#verify_doublesObject

Verifies all the DoubleInsertion objects have met their TimesCalledExpectations.



90
91
92
93
94
95
96
# File 'lib/rr/space.rb', line 90

def verify_doubles
  @double_insertions.each do |object, method_double_map|
    method_double_map.keys.each do |method_name|
      verify_double(object, method_name)
    end
  end
end

#verify_ordered_scenario(scenario) ⇒ Object

Verifies that the passed in ordered Double is being called in the correct position.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rr/space.rb', line 73

def verify_ordered_scenario(scenario)
  unless scenario.terminal?
    raise Errors::DoubleOrderError,
          "Ordered Doubles cannot have a NonTerminal TimesCalledExpectation"
  end
  unless @ordered_scenarios.first == scenario
    message = Double.formatted_name(scenario.method_name, scenario.expected_arguments)
    message << " called out of order in list\n"
    message << Double.list_message_part(@ordered_scenarios)
    raise Errors::DoubleOrderError, message
  end
  @ordered_scenarios.shift unless scenario.attempt?
  scenario
end