module RR

Constants

BoundObjects
MethodArguments
ProcFromBlock
VERSION

Attributes

debug[RW]
debug?[RW]
overridden_error_class[RW]
doubles[R]
method_name[R]
subject_class[R]
times[R]

Public Class Methods

adapters() click to toggle source
# File lib/rr/integrations.rb, line 15
def self.adapters
  @adapters ||= []
end
adapters_by_name() click to toggle source
# File lib/rr/integrations.rb, line 19
def self.adapters_by_name
  @adapters_by_name ||= {}
end
applicable_adapters() click to toggle source
# File lib/rr/integrations.rb, line 29
def self.applicable_adapters
  adapters.select { |adapter| adapter.applies? }
end
autohook() click to toggle source
# File lib/rr/autohook.rb, line 3
def autohook
  applicable_adapters.each { |adapter| adapter.load }
  if applicable_adapters.empty?
    puts "No adapters matched!" if RR.debug?
  end
end
find_applicable_adapter(name) click to toggle source
# File lib/rr/integrations.rb, line 33
def self.find_applicable_adapter(name)
  adapters.
    select { |adapter| name === adapter.name.to_s }.
    find   { |adapter| adapter.applies? }
end
loaded_adapter_names() click to toggle source
# File lib/rr/integrations.rb, line 23
def self.loaded_adapter_names
  adapters.
    select { |adapter| adapter.loaded? }.
    map { |adapter| adapter.name }
end
module_shim_for_adapter(adapter) click to toggle source
# File lib/rr/integrations.rb, line 39
def self.module_shim_for_adapter(adapter)
  mod = Module.new
  (class << mod; self; end).class_eval do
    define_method(:included) do |base|
      # Note: This assumes that the thing that is including this module
      # is the same that the adapter detected and will hook into.
      adapter.load
    end
  end
  mod
end
new(subject_class, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 96
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
register_adapter(klass) click to toggle source
# File lib/rr/integrations.rb, line 7
def self.register_adapter(klass)
  adapter = Integrations::Decorator.new(klass.new)
  unless adapters_by_name.key?(adapter.name)
    adapters << adapter
    adapters_by_name[adapter.name] = adapter
  end
end
ruby_18?() click to toggle source
# File lib/rr/without_autohook.rb, line 121
def ruby_18?
  RUBY_VERSION =~ /^1\.8/
end
version() click to toggle source
# File lib/rr/version.rb, line 3
def self.version; VERSION; end

Public Instance Methods

==(other) click to toggle source
# File lib/rr/times_called_matchers/times_called_matcher.rb, line 30
def ==(other)
  self.class == other.class && self.times == other.times
end
attempt?(times_called) click to toggle source
# File lib/rr/times_called_matchers/times_called_matcher.rb, line 23
def attempt?(times_called)
end
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 112
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 140
      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 125
      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, args, block) click to toggle source
# File lib/rr/injections/double_injection.rb, line 178
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_method() { || ... } click to toggle source
# File lib/rr/injections/double_injection.rb, line 200
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 187
def dispatch_original_method(subject, args, block)
  dispatch = MethodDispatches::MethodDispatch.new(self, subject, args, block)
  dispatch.call_original_method
end
error_message(times_called) click to toggle source
# File lib/rr/times_called_matchers/times_called_matcher.rb, line 26
def error_message(times_called)
  "Called #{times_called.inspect} #{pluralized_time(times_called)}.\nExpected #{expected_times_message}."
end
expected_times_message() click to toggle source
# File lib/rr/times_called_matchers/times_called_matcher.rb, line 34
def expected_times_message
  "#{@times.inspect} times"
end
list_message_part(doubles) click to toggle source
# File lib/rr/double.rb, line 12
def list_message_part(doubles)
  doubles.collect do |double|
    "- #{formatted_name(double.method_name, double.expected_arguments)}"
  end.join("\n")
end
matches?(times_called) click to toggle source
# File lib/rr/times_called_matchers/times_called_matcher.rb, line 20
def matches?(times_called)
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/rr/injections/method_missing_injection.rb, line 32
def method_missing(method_name, *args, &block)
  super
end
original_method_alias_name() click to toggle source
# File lib/rr/injections/double_injection.rb, line 196
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 105
def register_double(double)
  @doubles << double
end
reset() click to toggle source

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

# File lib/rr/injections/double_injection.rb, line 166
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
set_default_double_injection_strategy(strategy_lambda) { || ... } click to toggle source
# File lib/rr/double_definitions/double_definition_create.rb, line 11
def set_default_double_injection_strategy(strategy_lambda)
  original_strategy_lambda = default_double_injection_strategy
  begin
    @default_double_injection_strategy = strategy_lambda
    yield
  ensure
    @default_double_injection_strategy = original_strategy_lambda
  end
end
subject_has_original_method_missing?() click to toggle source
# File lib/rr/injections/double_injection.rb, line 192
def subject_has_original_method_missing?
  class_instance_method_defined(subject_class, MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name)
end
to_ary() click to toggle source
# File lib/rr/injections/method_missing_injection.rb, line 46
def to_ary; nil; end
verify() click to toggle source

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

# File lib/rr/injections/double_injection.rb, line 156
def verify
  @doubles.each do |double|
    double.verify
  end
end

Protected Instance Methods

bind_method_with_alias() click to toggle source
# File lib/rr/injections/double_injection.rb, line 215
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 208
def deferred_bind_method
  unless subject_has_method_defined?(original_method_alias_name)
    bind_method_with_alias
  end
  @performed_deferred_bind = true
end
pluralized_time(times_called) click to toggle source
# File lib/rr/times_called_matchers/times_called_matcher.rb, line 39
def pluralized_time(times_called)
  (times_called == 1) ? "time" : "times"
end