Class: RR::Injections::DoubleInjection

Inherits:
Injection
  • Object
show all
Defined in:
lib/rr/injections/double_injection.rb

Overview

RR::DoubleInjection is the binding of an subject and a method. A double_injection has 0 to many Double objects. Each Double has Argument Expectations and Times called Expectations.

Defined Under Namespace

Classes: MethodArguments

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Injection

#subject_has_method_defined?, #subject_has_original_method?

Methods included from Space::Reader

#space

Constructor Details

#initialize(subject_class, method_name) ⇒ DoubleInjection

Returns a new instance of DoubleInjection.



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

def initialize(subject_class, method_name)
  @subject_class = subject_class
  @method_name = method_name.to_sym
  @doubles = []
  @dispatch_method_delegates_to_dispatch_original_method = nil
end

Instance Attribute Details

#doublesObject (readonly)

Returns the value of attribute doubles.



87
88
89
# File 'lib/rr/injections/double_injection.rb', line 87

def doubles
  @doubles
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



87
88
89
# File 'lib/rr/injections/double_injection.rb', line 87

def method_name
  @method_name
end

#subject_classObject (readonly)

Returns the value of attribute subject_class.



87
88
89
# File 'lib/rr/injections/double_injection.rb', line 87

def subject_class
  @subject_class
end

Instance Method Details

#bindObject

RR::DoubleInjection#bind injects a method that acts as a dispatcher that dispatches to the matching Double when the method is called.



107
108
109
110
111
112
113
114
115
116
# File 'lib/rr/injections/double_injection.rb', line 107

def bind
  if subject_has_method_defined?(method_name)
    bind_method_with_alias
  else
    Injections::MethodMissingInjection.find_or_create(subject_class)
    Injections::SingletonMethodAddedInjection.find_or_create(subject_class)
    bind_method_that_self_destructs_and_delegates_to_method_missing
  end
  self
end

#bind_methodObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/rr/injections/double_injection.rb', line 131

def bind_method
  subject_class_object_id = subject_class.object_id
  subject_class.class_eval(<<-METHOD, __FILE__, __LINE__ + 1)
  def #{method_name}(*args, &block)
    arguments = MethodArguments.new(args, block)
    RR::Injections::DoubleInjection.dispatch_method(self, ObjectSpace._id2ref(#{subject_class_object_id}), :#{method_name}, arguments.arguments, arguments.block)
  end
  METHOD
  self
end

#bind_method_that_self_destructs_and_delegates_to_method_missingObject



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rr/injections/double_injection.rb', line 118

def bind_method_that_self_destructs_and_delegates_to_method_missing
  subject_class_object_id = subject_class.object_id
  subject_class.class_eval(<<-METHOD, __FILE__, __LINE__ + 1)
  def #{method_name}(*args, &block)
    ObjectSpace._id2ref(#{subject_class_object_id}).class_eval do
      remove_method(:#{method_name})
    end
    method_missing(:#{method_name}, *args, &block)
  end
  METHOD
  self
end

#dispatch_method(subject, args, block) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/rr/injections/double_injection.rb', line 166

def dispatch_method(subject, args, block)
  if @dispatch_method_delegates_to_dispatch_original_method
    dispatch_original_method(subject, args, block)
  else
    dispatch = MethodDispatches::MethodDispatch.new(self, subject, args, block)
    dispatch.call
  end
end

#dispatch_method_delegates_to_dispatch_original_methodObject



188
189
190
191
192
193
# File 'lib/rr/injections/double_injection.rb', line 188

def dispatch_method_delegates_to_dispatch_original_method
  @dispatch_method_delegates_to_dispatch_original_method = true
  yield
ensure
  @dispatch_method_delegates_to_dispatch_original_method = nil
end

#dispatch_original_method(subject, args, block) ⇒ Object



175
176
177
178
# File 'lib/rr/injections/double_injection.rb', line 175

def dispatch_original_method(subject, args, block)
  dispatch = MethodDispatches::MethodDispatch.new(self, subject, args, block)
  dispatch.call_original_method
end

#original_method_alias_nameObject



184
185
186
# File 'lib/rr/injections/double_injection.rb', line 184

def original_method_alias_name
  "__rr__original_#{@method_name}"
end

#register_double(double) ⇒ Object

RR::DoubleInjection#register_double adds the passed in Double into this DoubleInjection’s list of Double objects.



100
101
102
# File 'lib/rr/injections/double_injection.rb', line 100

def register_double(double)
  @doubles << double
end

#resetObject

It binds the original method implementation on the subject if one exists.



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rr/injections/double_injection.rb', line 154

def reset
  if subject_has_original_method?
    subject_class.__send__(:remove_method, method_name)
    subject_class.__send__(:alias_method, method_name, original_method_alias_name)
    subject_class.__send__(:remove_method, original_method_alias_name)
  else
    if subject_has_method_defined?(method_name)
      subject_class.__send__(:remove_method, method_name)
    end
  end
end

#subject_has_original_method_missing?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/rr/injections/double_injection.rb', line 180

def subject_has_original_method_missing?
  ClassInstanceMethodDefined.call(subject_class, MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name)
end

#verifyObject

RR::DoubleInjection#verify verifies each Double TimesCalledExpectation are met.



144
145
146
147
148
# File 'lib/rr/injections/double_injection.rb', line 144

def verify
  @doubles.each do |double|
    double.verify
  end
end