class RR::Double

RR::Double is the use case for a method call. It has the ArgumentEqualityExpectation, TimesCalledExpectation, and the implementation.

Attributes

definition[R]
double_injection[R]
times_called[R]
times_called_expectation[R]

Public Class Methods

new(double_injection, definition) click to toggle source
# File lib/rr/double.rb, line 29
def initialize(double_injection, definition)
  @double_injection = double_injection
  @definition = definition
  @times_called = 0
  @times_called_expectation = Expectations::TimesCalledExpectation.new(self)
  definition.double = self
  verify_method_signature if definition.verify_method_signature?
  double_injection.register_double self
end

Public Instance Methods

attempt?() click to toggle source

Double#attempt? returns true when the TimesCalledExpectation is satisfied.

# File lib/rr/double.rb, line 53
def attempt?
  verify_times_matcher_is_set
  times_called_expectation.attempt?
end
exact_match?(arguments, keyword_arguments) click to toggle source

Double#exact_match? returns true when the passed in arguments exactly match the ArgumentEqualityExpectation arguments.

# File lib/rr/double.rb, line 41
def exact_match?(arguments, keyword_arguments)
  definition.exact_match?(arguments, keyword_arguments)
end
expected_arguments() click to toggle source

The Arguments that this Double expects

# File lib/rr/double.rb, line 78
def expected_arguments
  verify_argument_expectation_is_set
  argument_expectation.expected_arguments
end
expected_keyword_arguments() click to toggle source

The keyword arguments that this Double expects

# File lib/rr/double.rb, line 84
def expected_keyword_arguments
  verify_argument_expectation_is_set
  argument_expectation.expected_keyword_arguments
end
formatted_name() click to toggle source
# File lib/rr/double.rb, line 94
def formatted_name
  self.class.formatted_name(method_name,
                            expected_arguments,
                            expected_keyword_arguments)
end
implementation_is_original_method?() click to toggle source
# File lib/rr/double.rb, line 108
def implementation_is_original_method?
  definition.implementation_is_original_method?
end
method_call(args, kwargs) click to toggle source
# File lib/rr/double.rb, line 100
def method_call(args, kwargs)
  if verbose?
    puts Double.formatted_name(method_name, args, kwargs)
  end
  times_called_expectation.attempt if definition.times_matcher
  space.verify_ordered_double(self) if ordered?
end
method_name() click to toggle source

The method name that this Double is attatched to

# File lib/rr/double.rb, line 73
def method_name
  double_injection.method_name
end
terminal?() click to toggle source
# File lib/rr/double.rb, line 67
def terminal?
  verify_times_matcher_is_set
  times_called_expectation.terminal?
end
times_matcher() click to toggle source

The TimesCalledMatcher for the TimesCalledExpectation

# File lib/rr/double.rb, line 90
def times_matcher
  definition.times_matcher
end
verify() click to toggle source

Double#verify verifies the the TimesCalledExpectation is satisfied for this double. A TimesCalledError is raised if the TimesCalledExpectation is not met.

# File lib/rr/double.rb, line 61
def verify
  verify_times_matcher_is_set
  times_called_expectation.verify!
  true
end
wildcard_match?(arguments, keyword_arguments) click to toggle source

Double#wildcard_match? returns true when the passed in arguments wildcard match the ArgumentEqualityExpectation arguments.

# File lib/rr/double.rb, line 47
def wildcard_match?(arguments, keyword_arguments)
  definition.wildcard_match?(arguments, keyword_arguments)
end

Protected Instance Methods

args() click to toggle source
# File lib/rr/double.rb, line 161
def args
  definition.argument_expectation.expected_arguments
end
argument_expectation() click to toggle source
# File lib/rr/double.rb, line 169
def argument_expectation
  definition.argument_expectation
end
arity_matches?() click to toggle source
# File lib/rr/double.rb, line 152
def arity_matches?
  return true if subject_accepts_only_varargs?
  if subject_accepts_varargs?
    return ((subject_arity * -1) - 1) <= args.size
  else
    return subject_arity == args.size
  end
end
kwargs() click to toggle source
# File lib/rr/double.rb, line 165
def kwargs
  definition.argument_expectation.expected_keyword_arguments
end
ordered?() click to toggle source
# File lib/rr/double.rb, line 113
def ordered?
  definition.ordered?
end
subject_accepts_only_varargs?() click to toggle source
# File lib/rr/double.rb, line 144
def subject_accepts_only_varargs?
  subject_arity == -1
end
subject_accepts_varargs?() click to toggle source
# File lib/rr/double.rb, line 148
def subject_accepts_varargs?
  subject_arity < 0
end
subject_arity() click to toggle source
# File lib/rr/double.rb, line 140
def subject_arity
  double_injection.original_method.arity
end
verbose?() click to toggle source
# File lib/rr/double.rb, line 117
def verbose?
  definition.verbose?
end
verify_argument_expectation_is_set() click to toggle source
# File lib/rr/double.rb, line 127
def verify_argument_expectation_is_set
  unless definition.argument_expectation
    raise RR::Errors.build_error(:DoubleDefinitionError, "#definition.argument_expectation is not set")
  end
end
verify_method_signature() click to toggle source
# File lib/rr/double.rb, line 133
def verify_method_signature
  unless double_injection.subject_has_original_method?
    raise RR::Errors.build_error(:SubjectDoesNotImplementMethodError)
  end
  raise RR::Errors.build_error(:SubjectHasDifferentArityError) unless arity_matches?
end
verify_times_matcher_is_set() click to toggle source
# File lib/rr/double.rb, line 121
def verify_times_matcher_is_set
  unless definition.times_matcher
    raise RR::Errors.build_error(:DoubleDefinitionError, "#definition.times_matcher is not set")
  end
end