| 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 146
146: def args
147: definition.argument_expectation.expected_arguments
148: end
# File lib/rr/double.rb, line 150
150: def argument_expectation
151: definition.argument_expectation
152: end
# File lib/rr/double.rb, line 137
137: def arity_matches?
138: return true if subject_accepts_only_varargs?
139: if subject_accepts_varargs?
140: return ((subject_arity * -1) - 1) <= args.size
141: else
142: return subject_arity == args.size
143: end
144: end
# File lib/rr/double.rb, line 129
129: def subject_accepts_only_varargs?
130: subject_arity == -1
131: end
# File lib/rr/double.rb, line 125
125: def subject_arity
126: double_injection.original_method.arity
127: 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: unless double_injection.subject_has_original_method?
120: raise RR::Errors::SubjectDoesNotImplementMethodError
121: end
122: raise RR::Errors::SubjectHasDifferentArityError unless arity_matches?
123: end