Class: RR::Space

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

Overview

RR::Space.instance is the global state subject 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
29
30
31
32
33
# File 'lib/rr/space.rb', line 24

def initialize
  @double_injections = HashWithObjectIdKey.new do |hash, subject_object|
    hash.set_with_object_id(subject_object, {})
  end
  @method_missing_injections = HashWithObjectIdKey.new
  @singleton_method_added_injections = HashWithObjectIdKey.new
  @ordered_doubles = []
  @trim_backtrace = false
  @recorded_calls = RR::RecordedCalls.new
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

#method_missing_injectionsObject (readonly)

Returns the value of attribute method_missing_injections.



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

def method_missing_injections
  @method_missing_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

#recorded_callsObject (readonly)

Returns the value of attribute recorded_calls.



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

def recorded_calls
  @recorded_calls
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(subject, method_name) ⇒ Object

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



39
40
41
42
43
# File 'lib/rr/space.rb', line 39

def double_injection(subject, method_name)
  @double_injections[subject][method_name.to_sym] ||= begin
    Injections::DoubleInjection.new(subject, method_name.to_sym, (class << subject; self; end)).bind
  end
end

#double_injection_exists?(subject, method_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def double_injection_exists?(subject, method_name)
  @double_injections.include?(subject) && @double_injections[subject].include?(method_name.to_sym)
end

#method_missing_injection(subject) ⇒ Object



49
50
51
52
53
# File 'lib/rr/space.rb', line 49

def method_missing_injection(subject)
  @method_missing_injections[subject] ||= begin
    Injections::MethodMissingInjection.new(subject).bind
  end
end

#method_missing_injection_exists?(subject) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rr/space.rb', line 55

def method_missing_injection_exists?(subject)
  @method_missing_injections.include?(subject)
end

#record_call(subject, method_name, arguments, block) ⇒ Object



126
127
128
# File 'lib/rr/space.rb', line 126

def record_call(subject, method_name, arguments, block)
  @recorded_calls << [subject, method_name, arguments, block]
end

#register_ordered_double(double) ⇒ Object

Registers the ordered Double to be verified.



70
71
72
# File 'lib/rr/space.rb', line 70

def register_ordered_double(double)
  @ordered_doubles << double unless ordered_doubles.include?(double)
end

#resetObject

Resets the registered Doubles and ordered Doubles



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

def reset
  reset_ordered_doubles
  reset_double_injections
  reset_method_missing_injections
  reset_singleton_method_added_injections
  reset_recorded_calls
end

#reset_double(subject, method_name) ⇒ Object

Resets the DoubleInjection for the passed in subject and method_name.



120
121
122
123
124
# File 'lib/rr/space.rb', line 120

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

#singleton_method_added_injection(subject) ⇒ Object



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

def singleton_method_added_injection(subject)
  @singleton_method_added_injections[subject] ||= begin
    Injections::SingletonMethodAddedInjection.new(subject).bind
  end
end

#singleton_method_added_injection_exists?(subject) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rr/space.rb', line 65

def singleton_method_added_injection_exists?(subject)
  @singleton_method_added_injections.include?(subject)
end

#verify_double(subject, method_name) ⇒ Object

Verifies the DoubleInjection for the passed in subject and method_name.



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

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

#verify_doubles(*objects) ⇒ Object Also known as: verify

Verifies all the DoubleInjection objects have met their TimesCalledExpectations.



93
94
95
96
97
98
99
100
# File 'lib/rr/space.rb', line 93

def verify_doubles(*objects)
  objects = @double_injections.keys if objects.empty?
  objects.each do |subject|
    @double_injections[subject].keys.each do |method_name|
      verify_double(subject, method_name)
    end
  end
end

#verify_ordered_double(double) ⇒ Object

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rr/space.rb', line 76

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