Class: RR::Space

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

Overview

RR::Space.instance is the global state object for the RR framework.

Defined Under Namespace

Modules: Reader

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpace

Returns a new instance of Space.



24
25
26
27
28
# File 'lib/rr/space.rb', line 24

def initialize
  @double_injections = HashWithObjectIdKey.new
  @ordered_doubles = []
  @trim_backtrace = false
end

Class Attribute Details

.instanceObject



11
12
13
# File 'lib/rr/space.rb', line 11

def instance
  @instance ||= new
end

Instance Attribute Details

#double_injectionsObject (readonly)

Returns the value of attribute double_injections.



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

def double_injections
  @double_injections
end

#ordered_doublesObject (readonly)

Returns the value of attribute ordered_doubles.



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

def ordered_doubles
  @ordered_doubles
end

#trim_backtraceObject

Returns the value of attribute trim_backtrace.



23
24
25
# File 'lib/rr/space.rb', line 23

def trim_backtrace
  @trim_backtrace
end

Instance Method Details

#double_injection(object, method_name) ⇒ Object

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



34
35
36
37
38
39
40
41
42
# File 'lib/rr/space.rb', line 34

def double_injection(object, method_name)
  double_injection = @double_injections[object][method_name.to_sym]
  return double_injection if double_injection

  double_injection = DoubleInjection.new(object, method_name.to_sym)
  @double_injections[object][method_name.to_sym] = double_injection
  double_injection.bind
  double_injection
end

#register_ordered_double(double) ⇒ Object

Registers the ordered Double to be verified.



45
46
47
# File 'lib/rr/space.rb', line 45

def register_ordered_double(double)
  @ordered_doubles << double
end

#resetObject

Resets the registered Doubles and ordered Doubles



78
79
80
81
# File 'lib/rr/space.rb', line 78

def reset
  reset_ordered_doubles
  reset_double_injections
end

#reset_double(object, method_name) ⇒ Object

Resets the DoubleInjection for the passed in object and method_name.



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

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

#verify_double(object, method_name) ⇒ Object

Verifies the DoubleInjection for the passed in object and method_name.



84
85
86
87
88
# File 'lib/rr/space.rb', line 84

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

#verify_doublesObject Also known as: verify

Verifies all the DoubleInjection objects have met their TimesCalledExpectations.



68
69
70
71
72
73
74
# File 'lib/rr/space.rb', line 68

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

#verify_ordered_double(double) ⇒ Object

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



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

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