class RR::Injections::DoubleInjection

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.

Constants

BoundObjects
MethodArguments

Attributes

doubles[R]
method_name[R]
subject_class[R]

Public Class Methods

new(subject_class, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 95
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

Public Instance Methods

bind() click to toggle source

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

# File lib/rr/injections/double_injection.rb, line 111
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_method() click to toggle source
# File lib/rr/injections/double_injection.rb, line 139
      def bind_method
        id = BoundObjects.size
        BoundObjects[id] = subject_class

        subject_class.class_eval("        def #{method_name}(*args, &block)
          arguments = MethodArguments.new(args, block)
          obj = ::RR::Injections::DoubleInjection::BoundObjects[#{id}]
          RR::Injections::DoubleInjection.dispatch_method(self, obj, :#{method_name}, arguments.arguments, arguments.block)
        end
", __FILE__, __LINE__ + 1)
        self
      end
bind_method_that_self_destructs_and_delegates_to_method_missing() click to toggle source
# File lib/rr/injections/double_injection.rb, line 124
      def bind_method_that_self_destructs_and_delegates_to_method_missing
        id = BoundObjects.size
        BoundObjects[id] = subject_class

        subject_class.class_eval("        def #{method_name}(*args, &block)
          ::RR::Injections::DoubleInjection::BoundObjects[#{id}].class_eval do
            remove_method(:#{method_name})
          end
          method_missing(:#{method_name}, *args, &block)
        end
", __FILE__, __LINE__ + 1)
        self
      end
dispatch_method(subject, subject_class, method_name, arguments, block) click to toggle source
# File lib/rr/injections/double_injection.rb, line 34
def dispatch_method(subject, subject_class, method_name, arguments, block)
  subject_eigenclass = (class << subject; self; end)
  if (
    exists?(subject_class, method_name) &&
    (subject_class == subject_eigenclass) || !subject.is_a?(Class)
  )
    find(subject_class, method_name.to_sym).dispatch_method(subject, arguments, block)
  else
    new(subject_class, method_name.to_sym).dispatch_original_method(subject, arguments, block)
  end
end
dispatch_method_delegates_to_dispatch_original_method() { || ... } click to toggle source
# File lib/rr/injections/double_injection.rb, line 199
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) click to toggle source
# File lib/rr/injections/double_injection.rb, line 186
def dispatch_original_method(subject, args, block)
  dispatch = MethodDispatches::MethodDispatch.new(self, subject, args, block)
  dispatch.call_original_method
end
exists?(subject_class, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 26
def exists?(subject_class, method_name)
  !!find(subject_class, method_name)
end
exists_by_subject?(subject, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 30
def exists_by_subject?(subject, method_name)
  exists?((class << subject; self; end), method_name)
end
find(subject_class, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 18
def find(subject_class, method_name)
  instances[subject_class] && instances[subject_class][method_name.to_sym]
end
find_by_subject(subject, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 22
def find_by_subject(subject, method_name)
  find(class << subject; self; end, method_name)
end
find_or_create(subject_class, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 8
def find_or_create(subject_class, method_name)
  instances[subject_class][method_name.to_sym] ||= begin
    new(subject_class, method_name.to_sym).bind
  end
end
find_or_create_by_subject(subject, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 14
def find_or_create_by_subject(subject, method_name)
  find_or_create(class << subject; self; end, method_name)
end
instances() click to toggle source
# File lib/rr/injections/double_injection.rb, line 83
def instances
  @instances ||= HashWithObjectIdKey.new do |hash, subject_class|
    hash.set_with_object_id(subject_class, {})
  end
end
original_method_alias_name() click to toggle source
# File lib/rr/injections/double_injection.rb, line 195
def original_method_alias_name
  "__rr__original_#{@method_name}"
end
register_double(double) click to toggle source

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

# File lib/rr/injections/double_injection.rb, line 104
def register_double(double)
  @doubles << double
end
reset() click to toggle source
# File lib/rr/injections/double_injection.rb, line 46
def reset
  instances.each do |subject_class, method_double_map|
    SingletonMethodAddedInjection.find(subject_class) && SingletonMethodAddedInjection.find(subject_class).reset
    method_double_map.keys.each do |method_name|
      reset_double(subject_class, method_name)
    end
    Injections::DoubleInjection.instances.delete(subject_class) if Injections::DoubleInjection.instances.has_key?(subject_class)
  end
end
reset_double(subject_class, method_name) click to toggle source

Resets the DoubleInjection for the passed in subject and method_name.

# File lib/rr/injections/double_injection.rb, line 77
def reset_double(subject_class, method_name)
  double_injection = Injections::DoubleInjection.instances[subject_class].delete(method_name)
  double_injection.reset
  Injections::DoubleInjection.instances.delete(subject_class) if Injections::DoubleInjection.instances[subject_class].empty?
end
subject_has_original_method_missing?() click to toggle source
# File lib/rr/injections/double_injection.rb, line 191
def subject_has_original_method_missing?
  class_instance_method_defined(subject_class, MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name)
end
verify(*subjects) click to toggle source
# File lib/rr/injections/double_injection.rb, line 56
def verify(*subjects)
  subject_classes = subjects.empty? ?
    Injections::DoubleInjection.instances.keys :
    subjects.map {|subject| class << subject; self; end}
  subject_classes.each do |subject_class|
    instances.include?(subject_class) &&
      instances[subject_class].keys.each do |method_name|
        verify_double(subject_class, method_name)
      end &&
      instances.delete(subject_class)
  end
end
verify_double(subject_class, method_name) click to toggle source

Verifies the DoubleInjection for the passed in subject and method_name.

# File lib/rr/injections/double_injection.rb, line 70
def verify_double(subject_class, method_name)
  Injections::DoubleInjection.find(subject_class, method_name).verify
ensure
  reset_double subject_class, method_name
end

Protected Instance Methods

bind_method_with_alias() click to toggle source
# File lib/rr/injections/double_injection.rb, line 214
def bind_method_with_alias
  subject_class.__send__(:alias_method, original_method_alias_name, method_name)
  bind_method
end
deferred_bind_method() click to toggle source
# File lib/rr/injections/double_injection.rb, line 207
def deferred_bind_method
  unless subject_has_method_defined?(original_method_alias_name)
    bind_method_with_alias
  end
  @performed_deferred_bind = true
end