class RR::RecordedCalls

Attributes

ordered_index[RW]
recorded_calls[R]

Public Class Methods

new(recorded_calls=[]) click to toggle source
# File lib/rr/recorded_calls.rb, line 5
def initialize(recorded_calls=[])
  @recorded_calls = recorded_calls
  @ordered_index = 0
end

Public Instance Methods

==(other) click to toggle source
# File lib/rr/recorded_calls.rb, line 29
def ==(other)
  recorded_calls == other.recorded_calls
end
[](index) click to toggle source
# File lib/rr/recorded_calls.rb, line 12
def [](index)
  @recorded_calls[index]
end
add(subject, method_name, arguments, block) click to toggle source
# File lib/rr/recorded_calls.rb, line 21
def add(subject, method_name, arguments, block)
  recorded_calls << RecordedCall.new(subject, method_name, arguments, block)
end
any?(&block) click to toggle source
# File lib/rr/recorded_calls.rb, line 25
def any?(&block)
  recorded_calls.any?(&block)
end
clear() click to toggle source
# File lib/rr/recorded_calls.rb, line 16
def clear
  self.ordered_index = 0
  recorded_calls.clear
end
match_error(spy_verification) click to toggle source
# File lib/rr/recorded_calls.rb, line 33
def match_error(spy_verification)
  double_injection_exists_error(spy_verification) || begin
    if spy_verification.ordered?
      ordered_match_error(spy_verification)
    else
      unordered_match_error(spy_verification)
    end
  end
end

Protected Instance Methods

double_injection_exists_error(spy_verification) click to toggle source
# File lib/rr/recorded_calls.rb, line 46
def double_injection_exists_error(spy_verification)
  unless Injections::DoubleInjection.exists_by_subject?(spy_verification.subject, spy_verification.method_name)
    RR::Errors.build_error(RR::Errors::SpyVerificationErrors::DoubleInjectionNotFoundError,
      "A Double Injection for the subject and method call:\n" <<
      "#{spy_verification.subject_inspect}\n" <<
      "#{spy_verification.method_name}\ndoes not exist in:\n" <<
      "\t#{recorded_calls.map {|call| call.inspect }.join("\n\t")}"
    )
  end
end
invocation_count_error(spy_verification, matching_recorded_calls) click to toggle source
# File lib/rr/recorded_calls.rb, line 96
def invocation_count_error(spy_verification, matching_recorded_calls)
  RR::Errors.build_error(RR::Errors::SpyVerificationErrors::InvocationCountError,
    "On subject #{spy_verification.subject.inspect}\n" <<
    "Expected #{Double.formatted_name(spy_verification.method_name, spy_verification.argument_expectation.expected_arguments)}\n" <<
    "to be called #{spy_verification.times_matcher.expected_times_message},\n" <<
    "but was called #{matching_recorded_calls.size} times.\n" <<
    "All of the method calls related to Doubles are:\n" <<
    "\t#{recorded_calls.map {|call| call.inspect}.join("\n\t")}"
  )
end
match_argument_expectation(spy_verification) click to toggle source
# File lib/rr/recorded_calls.rb, line 89
def match_argument_expectation(spy_verification)
  lambda do |recorded_call|
    spy_verification.argument_expectation.exact_match?(*recorded_call[2]) ||
    spy_verification.argument_expectation.wildcard_match?(*recorded_call[2])
  end
end
match_double_injection(spy_verification) click to toggle source
# File lib/rr/recorded_calls.rb, line 82
def match_double_injection(spy_verification)
  lambda do |recorded_call|
    recorded_call[0] == spy_verification.subject &&
    recorded_call[1] == spy_verification.method_name
  end
end
matching_recorded_calls(spy_verification) click to toggle source
# File lib/rr/recorded_calls.rb, line 76
def matching_recorded_calls(spy_verification)
  recorded_calls[ordered_index..-1].
    select(&match_double_injection(spy_verification)).
    select(&match_argument_expectation(spy_verification))
end
ordered_match_error(spy_verification) click to toggle source
# File lib/rr/recorded_calls.rb, line 57
def ordered_match_error(spy_verification)
  memoized_matching_recorded_calls = matching_recorded_calls(spy_verification)

  if memoized_matching_recorded_calls.last
    self.ordered_index = recorded_calls.index(memoized_matching_recorded_calls.last)
  end
  (0..memoized_matching_recorded_calls.size).to_a.any? do |i|
    spy_verification.times_matcher.matches?(i)
  end ? nil : invocation_count_error(spy_verification, memoized_matching_recorded_calls)
end
unordered_match_error(spy_verification) click to toggle source
# File lib/rr/recorded_calls.rb, line 68
def unordered_match_error(spy_verification)
  memoized_matching_recorded_calls = matching_recorded_calls(spy_verification)

  spy_verification.times_matcher.matches?(
    memoized_matching_recorded_calls.size
  ) ? nil : invocation_count_error(spy_verification, memoized_matching_recorded_calls)
end