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.

Methods

Included Modules

Space::Reader

Attributes

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

Public Class methods

[Source]

    # 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

Public Instance methods

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

[Source]

    # 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.

[Source]

    # File lib/rr/double.rb, line 34
34:     def exact_match?(*arguments)
35:       definition.exact_match?(*arguments)
36:     end

The Arguments that this Double expects

[Source]

    # File lib/rr/double.rb, line 71
71:     def expected_arguments
72:       verify_argument_expectation_is_set
73:       argument_expectation.expected_arguments
74:     end

[Source]

    # File lib/rr/double.rb, line 81
81:     def formatted_name
82:       self.class.formatted_name(method_name, expected_arguments)
83:     end

[Source]

    # 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

[Source]

    # File lib/rr/double.rb, line 93
93:     def implementation_is_original_method?
94:       definition.implementation_is_original_method?
95:     end

[Source]

    # 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

[Source]

    # 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

The method name that this Double is attatched to

[Source]

    # File lib/rr/double.rb, line 66
66:     def method_name
67:       double_injection.method_name
68:     end

[Source]

    # 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

[Source]

    # 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.

[Source]

    # 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.

[Source]

    # File lib/rr/double.rb, line 40
40:     def wildcard_match?(*arguments)
41:       definition.wildcard_match?(*arguments)
42:     end

Protected Instance methods

[Source]

     # File lib/rr/double.rb, line 146
146:     def args
147:       definition.argument_expectation.expected_arguments
148:     end

[Source]

     # File lib/rr/double.rb, line 150
150:     def argument_expectation
151:       definition.argument_expectation
152:     end

[Source]

     # 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

[Source]

     # File lib/rr/double.rb, line 98
 98:     def ordered?
 99:       definition.ordered?
100:     end

[Source]

     # File lib/rr/double.rb, line 129
129:     def subject_accepts_only_varargs?
130:       subject_arity == -1
131:     end

[Source]

     # File lib/rr/double.rb, line 133
133:     def subject_accepts_varargs?
134:       subject_arity < 0
135:     end

[Source]

     # File lib/rr/double.rb, line 125
125:     def subject_arity
126:       double_injection.original_method.arity
127:     end

[Source]

     # File lib/rr/double.rb, line 102
102:     def verbose?
103:       definition.verbose?
104:     end

[Source]

     # 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

[Source]

     # 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

[Source]

     # File lib/rr/double.rb, line 106
106:     def verify_times_matcher_is_set
107:       unless definition.times_matcher
108:         raise RR::Errors::DoubleDefinitionError, "#definition.times_matcher is not set"
109:       end
110:     end

[Validate]