Class | RR::Double |
In: |
lib/rr/double.rb
|
Parent: | Object |
RR::Double is the use case for a method call. It has the ArgumentEqualityExpectation, TimesCalledExpectation, and the implementation.
definition | [R] | |
double_injection | [R] | |
times_called | [R] | |
times_called_expectation | [R] |
# File lib/rr/double.rb, line 22 22: def initialize(double_injection, definition) 23: @double_injection = double_injection 24: @definition = definition 25: @times_called = 0 26: @times_called_expectation = Expectations::TimesCalledExpectation.new(self) 27: definition.double = self 28: verify_method_signature if definition.verify_method_signature? 29: double_injection.register_double self 30: end
Double#attempt? returns true when the TimesCalledExpectation is satisfied.
# File lib/rr/double.rb, line 46 46: def attempt? 47: verify_times_matcher_is_set 48: times_called_expectation.attempt? 49: end
Double#exact_match? returns true when the passed in arguments exactly match the ArgumentEqualityExpectation arguments.
# File lib/rr/double.rb, line 34 34: def exact_match?(*arguments) 35: definition.exact_match?(*arguments) 36: end
# File lib/rr/double.rb, line 81 81: def formatted_name 82: self.class.formatted_name(method_name, expected_arguments) 83: end
# File lib/rr/double.rb, line 7 7: def formatted_name(method_name, args) 8: formatted_errors = args.collect {|arg| arg.inspect}.join(', ') 9: "#{method_name}(#{formatted_errors})" 10: end
# File lib/rr/double.rb, line 93 93: def implementation_is_original_method? 94: definition.implementation_is_original_method? 95: end
# File lib/rr/double.rb, line 12 12: def list_message_part(doubles) 13: doubles.collect do |double| 14: "- #{formatted_name(double.method_name, double.expected_arguments)}" 15: end.join("\n") 16: end
# File lib/rr/double.rb, line 85 85: def method_call(args) 86: if verbose? 87: puts Double.formatted_name(method_name, args) 88: end 89: times_called_expectation.attempt if definition.times_matcher 90: space.verify_ordered_double(self) if ordered? 91: end
# File lib/rr/double.rb, line 60 60: def terminal? 61: verify_times_matcher_is_set 62: times_called_expectation.terminal? 63: end
The TimesCalledMatcher for the TimesCalledExpectation
# File lib/rr/double.rb, line 77 77: def times_matcher 78: definition.times_matcher 79: end
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 54 54: def verify 55: verify_times_matcher_is_set 56: times_called_expectation.verify! 57: true 58: end
Double#wildcard_match? returns true when the passed in arguments wildcard match the ArgumentEqualityExpectation arguments.
# File lib/rr/double.rb, line 40 40: def wildcard_match?(*arguments) 41: definition.wildcard_match?(*arguments) 42: end
# File lib/rr/double.rb, line 144 144: def args 145: definition.argument_expectation.expected_arguments 146: end
# File lib/rr/double.rb, line 148 148: def argument_expectation 149: definition.argument_expectation 150: end
# File lib/rr/double.rb, line 135 135: def arity_matches? 136: return true if subject_accepts_only_varargs? 137: if subject_accepts_varargs? 138: return ((subject_arity * -1) - 1) <= args.size 139: else 140: return subject_arity == args.size 141: end 142: end
# File lib/rr/double.rb, line 127 127: def subject_accepts_only_varargs? 128: subject_arity == -1 129: end
# File lib/rr/double.rb, line 123 123: def subject_arity 124: definition.subject.method(double_injection.send(:original_method_alias_name)).arity 125: end
# File lib/rr/double.rb, line 112 112: def verify_argument_expectation_is_set 113: unless definition.argument_expectation 114: raise RR::Errors::DoubleDefinitionError, "#definition.argument_expectation is not set" 115: end 116: end
# File lib/rr/double.rb, line 118 118: def verify_method_signature 119: raise RR::Errors::SubjectDoesNotImplementMethodError unless definition.subject.respond_to?(double_injection.send(:original_method_alias_name)) 120: raise RR::Errors::SubjectHasDifferentArityError unless arity_matches? 121: end