mcmire-rr 1.0.5.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. data/CHANGES +269 -0
  2. data/Gemfile +16 -0
  3. data/Gemfile.lock +47 -0
  4. data/LICENSE +22 -0
  5. data/README.rdoc +390 -0
  6. data/Rakefile +49 -0
  7. data/VERSION +1 -0
  8. data/lib/rr.rb +101 -0
  9. data/lib/rr/adapters/minitest.rb +31 -0
  10. data/lib/rr/adapters/rr_methods.rb +146 -0
  11. data/lib/rr/adapters/rspec.rb +61 -0
  12. data/lib/rr/adapters/rspec2.rb +22 -0
  13. data/lib/rr/adapters/test_unit.rb +31 -0
  14. data/lib/rr/blank_slate.rb +17 -0
  15. data/lib/rr/class_instance_method_defined.rb +9 -0
  16. data/lib/rr/double.rb +154 -0
  17. data/lib/rr/double_definitions/child_double_definition_create.rb +27 -0
  18. data/lib/rr/double_definitions/double_definition.rb +365 -0
  19. data/lib/rr/double_definitions/double_definition_create.rb +139 -0
  20. data/lib/rr/double_definitions/double_definition_create_blank_slate.rb +26 -0
  21. data/lib/rr/double_definitions/double_injections/any_instance_of.rb +28 -0
  22. data/lib/rr/double_definitions/double_injections/instance.rb +16 -0
  23. data/lib/rr/double_definitions/strategies/double_injection/any_instance_of.rb +30 -0
  24. data/lib/rr/double_definitions/strategies/double_injection/double_injection_strategy.rb +10 -0
  25. data/lib/rr/double_definitions/strategies/double_injection/instance.rb +17 -0
  26. data/lib/rr/double_definitions/strategies/implementation/implementation_strategy.rb +10 -0
  27. data/lib/rr/double_definitions/strategies/implementation/proxy.rb +60 -0
  28. data/lib/rr/double_definitions/strategies/implementation/reimplementation.rb +14 -0
  29. data/lib/rr/double_definitions/strategies/implementation/strongly_typed_reimplementation.rb +15 -0
  30. data/lib/rr/double_definitions/strategies/strategy.rb +43 -0
  31. data/lib/rr/double_definitions/strategies/strategy_methods.rb +53 -0
  32. data/lib/rr/double_definitions/strategies/verification/dont_allow.rb +31 -0
  33. data/lib/rr/double_definitions/strategies/verification/mock.rb +42 -0
  34. data/lib/rr/double_definitions/strategies/verification/stub.rb +43 -0
  35. data/lib/rr/double_definitions/strategies/verification/verification_strategy.rb +10 -0
  36. data/lib/rr/double_matches.rb +42 -0
  37. data/lib/rr/errors/argument_equality_error.rb +6 -0
  38. data/lib/rr/errors/double_definition_error.rb +6 -0
  39. data/lib/rr/errors/double_not_found_error.rb +6 -0
  40. data/lib/rr/errors/double_order_error.rb +6 -0
  41. data/lib/rr/errors/rr_error.rb +20 -0
  42. data/lib/rr/errors/spy_verification_errors/double_injection_not_found_error.rb +8 -0
  43. data/lib/rr/errors/spy_verification_errors/invocation_count_error.rb +8 -0
  44. data/lib/rr/errors/spy_verification_errors/spy_verification_error.rb +8 -0
  45. data/lib/rr/errors/subject_does_not_implement_method_error.rb +6 -0
  46. data/lib/rr/errors/subject_has_different_arity_error.rb +6 -0
  47. data/lib/rr/errors/times_called_error.rb +6 -0
  48. data/lib/rr/expectations/any_argument_expectation.rb +21 -0
  49. data/lib/rr/expectations/argument_equality_expectation.rb +41 -0
  50. data/lib/rr/expectations/times_called_expectation.rb +57 -0
  51. data/lib/rr/hash_with_object_id_key.rb +46 -0
  52. data/lib/rr/injections/double_injection.rb +220 -0
  53. data/lib/rr/injections/injection.rb +33 -0
  54. data/lib/rr/injections/method_missing_injection.rb +73 -0
  55. data/lib/rr/injections/singleton_method_added_injection.rb +72 -0
  56. data/lib/rr/method_dispatches/base_method_dispatch.rb +84 -0
  57. data/lib/rr/method_dispatches/method_dispatch.rb +59 -0
  58. data/lib/rr/method_dispatches/method_missing_dispatch.rb +61 -0
  59. data/lib/rr/proc_from_block.rb +7 -0
  60. data/lib/rr/recorded_calls.rb +103 -0
  61. data/lib/rr/space.rb +119 -0
  62. data/lib/rr/spy_verification.rb +48 -0
  63. data/lib/rr/spy_verification_proxy.rb +13 -0
  64. data/lib/rr/times_called_matchers/any_times_matcher.rb +18 -0
  65. data/lib/rr/times_called_matchers/at_least_matcher.rb +15 -0
  66. data/lib/rr/times_called_matchers/at_most_matcher.rb +23 -0
  67. data/lib/rr/times_called_matchers/integer_matcher.rb +19 -0
  68. data/lib/rr/times_called_matchers/never_matcher.rb +23 -0
  69. data/lib/rr/times_called_matchers/non_terminal.rb +27 -0
  70. data/lib/rr/times_called_matchers/proc_matcher.rb +11 -0
  71. data/lib/rr/times_called_matchers/range_matcher.rb +21 -0
  72. data/lib/rr/times_called_matchers/terminal.rb +20 -0
  73. data/lib/rr/times_called_matchers/times_called_matcher.rb +44 -0
  74. data/lib/rr/wildcard_matchers.rb +158 -0
  75. data/lib/rr/wildcard_matchers/anything.rb +18 -0
  76. data/lib/rr/wildcard_matchers/boolean.rb +23 -0
  77. data/lib/rr/wildcard_matchers/duck_type.rb +32 -0
  78. data/lib/rr/wildcard_matchers/hash_including.rb +29 -0
  79. data/lib/rr/wildcard_matchers/is_a.rb +25 -0
  80. data/lib/rr/wildcard_matchers/numeric.rb +13 -0
  81. data/lib/rr/wildcard_matchers/range.rb +7 -0
  82. data/lib/rr/wildcard_matchers/regexp.rb +7 -0
  83. data/lib/rr/wildcard_matchers/satisfy.rb +26 -0
  84. data/spec/api/any_instance_of/all_instances_of_spec.rb +12 -0
  85. data/spec/api/any_instance_of/any_instance_of_spec.rb +47 -0
  86. data/spec/api/any_instance_of/instance_of_spec.rb +12 -0
  87. data/spec/api/dont_allow/dont_allow_after_stub_spec.rb +14 -0
  88. data/spec/api/mock/mock_spec.rb +193 -0
  89. data/spec/api/proxy/proxy_spec.rb +86 -0
  90. data/spec/api/spy/spy_spec.rb +49 -0
  91. data/spec/api/strong/strong_spec.rb +87 -0
  92. data/spec/api/stub/stub_spec.rb +152 -0
  93. data/spec/core_spec_suite.rb +18 -0
  94. data/spec/environment_fixture_setup.rb +7 -0
  95. data/spec/minitest_spec_suite.rb +21 -0
  96. data/spec/proc_from_block_spec.rb +14 -0
  97. data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +67 -0
  98. data/spec/rr/adapters/rr_methods_creator_spec.rb +137 -0
  99. data/spec/rr/adapters/rr_methods_space_spec.rb +98 -0
  100. data/spec/rr/adapters/rr_methods_spec_helper.rb +7 -0
  101. data/spec/rr/adapters/rr_methods_times_matcher_spec.rb +13 -0
  102. data/spec/rr/double_definitions/child_double_definition_creator_spec.rb +112 -0
  103. data/spec/rr/double_definitions/double_definition_create_blank_slate_spec.rb +91 -0
  104. data/spec/rr/double_definitions/double_definition_create_spec.rb +443 -0
  105. data/spec/rr/double_injection/double_injection_spec.rb +546 -0
  106. data/spec/rr/double_injection/double_injection_verify_spec.rb +29 -0
  107. data/spec/rr/errors/rr_error_spec.rb +67 -0
  108. data/spec/rr/expectations/any_argument_expectation_spec.rb +47 -0
  109. data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +14 -0
  110. data/spec/rr/expectations/argument_equality_expectation_spec.rb +135 -0
  111. data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +34 -0
  112. data/spec/rr/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
  113. data/spec/rr/expectations/hash_including_spec.rb +17 -0
  114. data/spec/rr/expectations/satisfy_argument_equality_expectation_spec.rb +59 -0
  115. data/spec/rr/expectations/satisfy_spec.rb +14 -0
  116. data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +22 -0
  117. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +37 -0
  118. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +43 -0
  119. data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +11 -0
  120. data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +58 -0
  121. data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +35 -0
  122. data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +39 -0
  123. data/spec/rr/minitest/minitest_integration_test.rb +59 -0
  124. data/spec/rr/minitest/test_helper.rb +7 -0
  125. data/spec/rr/rspec/invocation_matcher_spec.rb +279 -0
  126. data/spec/rr/rspec/rspec_adapter_spec.rb +63 -0
  127. data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +21 -0
  128. data/spec/rr/rspec/rspec_backtrace_tweaking_spec_fixture.rb +11 -0
  129. data/spec/rr/rspec/rspec_usage_spec.rb +86 -0
  130. data/spec/rr/space/hash_with_object_id_key_spec.rb +88 -0
  131. data/spec/rr/space/space_spec.rb +596 -0
  132. data/spec/rr/test_unit/test_helper.rb +7 -0
  133. data/spec/rr/test_unit/test_unit_backtrace_test.rb +36 -0
  134. data/spec/rr/test_unit/test_unit_integration_test.rb +59 -0
  135. data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +47 -0
  136. data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +55 -0
  137. data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +70 -0
  138. data/spec/rr/times_called_matchers/integer_matcher_spec.rb +70 -0
  139. data/spec/rr/times_called_matchers/proc_matcher_spec.rb +55 -0
  140. data/spec/rr/times_called_matchers/range_matcher_spec.rb +76 -0
  141. data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +118 -0
  142. data/spec/rr/wildcard_matchers/anything_spec.rb +24 -0
  143. data/spec/rr/wildcard_matchers/boolean_spec.rb +36 -0
  144. data/spec/rr/wildcard_matchers/duck_type_spec.rb +52 -0
  145. data/spec/rr/wildcard_matchers/is_a_spec.rb +32 -0
  146. data/spec/rr/wildcard_matchers/numeric_spec.rb +32 -0
  147. data/spec/rr/wildcard_matchers/range_spec.rb +35 -0
  148. data/spec/rr/wildcard_matchers/regexp_spec.rb +43 -0
  149. data/spec/rr_spec.rb +28 -0
  150. data/spec/rspec_spec_suite.rb +16 -0
  151. data/spec/spec_helper.rb +40 -0
  152. data/spec/spec_suite.rb +50 -0
  153. data/spec/spy_verification_spec.rb +129 -0
  154. data/spec/test_unit_spec_suite.rb +20 -0
  155. metadata +220 -0
@@ -0,0 +1,220 @@
1
+ module RR
2
+ module Injections
3
+ # RR::DoubleInjection is the binding of an subject and a method.
4
+ # A double_injection has 0 to many Double objects. Each Double
5
+ # has Argument Expectations and Times called Expectations.
6
+ class DoubleInjection < Injection
7
+ extend(Module.new do
8
+ def find_or_create(subject_class, method_name)
9
+ instances[subject_class][method_name.to_sym] ||= begin
10
+ new(subject_class, method_name.to_sym).bind
11
+ end
12
+ end
13
+
14
+ def find_or_create_by_subject(subject, method_name)
15
+ find_or_create(class << subject; self; end, method_name)
16
+ end
17
+
18
+ def find(subject_class, method_name)
19
+ instances[subject_class] && instances[subject_class][method_name.to_sym]
20
+ end
21
+
22
+ def find_by_subject(subject, method_name)
23
+ find(class << subject; self; end, method_name)
24
+ end
25
+
26
+ def exists?(subject_class, method_name)
27
+ !!find(subject_class, method_name)
28
+ end
29
+
30
+ def exists_by_subject?(subject, method_name)
31
+ exists?((class << subject; self; end), method_name)
32
+ end
33
+
34
+ def dispatch_method(subject, subject_class, method_name, arguments, block)
35
+ subject_eigenclass = (class << subject; self; end)
36
+ if (
37
+ exists?(subject_class, method_name) &&
38
+ (subject_class == subject_eigenclass) || !subject.is_a?(Class)
39
+ )
40
+ find(subject_class, method_name.to_sym).dispatch_method(subject, arguments, block)
41
+ else
42
+ new(subject_class, method_name.to_sym).dispatch_original_method(subject, arguments, block)
43
+ end
44
+ end
45
+
46
+ def reset
47
+ instances.each do |subject_class, method_double_map|
48
+ SingletonMethodAddedInjection.find(subject_class) && SingletonMethodAddedInjection.find(subject_class).reset
49
+ method_double_map.keys.each do |method_name|
50
+ reset_double(subject_class, method_name)
51
+ end
52
+ Injections::DoubleInjection.instances.delete(subject_class) if Injections::DoubleInjection.instances.has_key?(subject_class)
53
+ end
54
+ end
55
+
56
+ def verify(*subjects)
57
+ subject_classes = subjects.empty? ?
58
+ Injections::DoubleInjection.instances.keys :
59
+ subjects.map {|subject| class << subject; self; end}
60
+ subject_classes.each do |subject_class|
61
+ instances.include?(subject_class) &&
62
+ instances[subject_class].keys.each do |method_name|
63
+ verify_double(subject_class, method_name)
64
+ end &&
65
+ instances.delete(subject_class)
66
+ end
67
+ end
68
+
69
+ # Verifies the DoubleInjection for the passed in subject and method_name.
70
+ def verify_double(subject_class, method_name)
71
+ Injections::DoubleInjection.find(subject_class, method_name).verify
72
+ ensure
73
+ reset_double subject_class, method_name
74
+ end
75
+
76
+ # Resets the DoubleInjection for the passed in subject and method_name.
77
+ def reset_double(subject_class, method_name)
78
+ double_injection = Injections::DoubleInjection.instances[subject_class].delete(method_name)
79
+ double_injection.reset
80
+ Injections::DoubleInjection.instances.delete(subject_class) if Injections::DoubleInjection.instances[subject_class].empty?
81
+ end
82
+
83
+ def instances
84
+ @instances ||= HashWithObjectIdKey.new do |hash, subject_class|
85
+ hash.set_with_object_id(subject_class, {})
86
+ end
87
+ end
88
+ end)
89
+ include ClassInstanceMethodDefined
90
+
91
+ attr_reader :subject_class, :method_name, :doubles
92
+
93
+ MethodArguments = Struct.new(:arguments, :block)
94
+
95
+ def initialize(subject_class, method_name)
96
+ @subject_class = subject_class
97
+ @method_name = method_name.to_sym
98
+ @doubles = []
99
+ @dispatch_method_delegates_to_dispatch_original_method = nil
100
+ end
101
+
102
+ # RR::DoubleInjection#register_double adds the passed in Double
103
+ # into this DoubleInjection's list of Double objects.
104
+ def register_double(double)
105
+ @doubles << double
106
+ end
107
+
108
+ # RR::DoubleInjection#bind injects a method that acts as a dispatcher
109
+ # that dispatches to the matching Double when the method
110
+ # is called.
111
+ def bind
112
+ if subject_has_method_defined?(method_name)
113
+ bind_method_with_alias
114
+ else
115
+ Injections::MethodMissingInjection.find_or_create(subject_class)
116
+ Injections::SingletonMethodAddedInjection.find_or_create(subject_class)
117
+ bind_method_that_self_destructs_and_delegates_to_method_missing
118
+ end
119
+ self
120
+ end
121
+
122
+ BoundObjects = {}
123
+
124
+ def bind_method_that_self_destructs_and_delegates_to_method_missing
125
+ id = BoundObjects.size
126
+ BoundObjects[id] = subject_class
127
+
128
+ subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
129
+ def #{method_name}(*args, &block)
130
+ ::RR::Injections::DoubleInjection::BoundObjects[#{id}].class_eval do
131
+ remove_method(:#{method_name})
132
+ end
133
+ method_missing(:#{method_name}, *args, &block)
134
+ end
135
+ RUBY
136
+ self
137
+ end
138
+
139
+ def bind_method
140
+ id = BoundObjects.size
141
+ BoundObjects[id] = subject_class
142
+
143
+ subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
144
+ def #{method_name}(*args, &block)
145
+ arguments = MethodArguments.new(args, block)
146
+ obj = ::RR::Injections::DoubleInjection::BoundObjects[#{id}]
147
+ RR::Injections::DoubleInjection.dispatch_method(self, obj, :#{method_name}, arguments.arguments, arguments.block)
148
+ end
149
+ RUBY
150
+ self
151
+ end
152
+
153
+ # RR::DoubleInjection#verify verifies each Double
154
+ # TimesCalledExpectation are met.
155
+ def verify
156
+ @doubles.each do |double|
157
+ double.verify
158
+ end
159
+ end
160
+
161
+ # RR::DoubleInjection#reset removes the injected dispatcher method.
162
+
163
+ # It binds the original method implementation on the subject
164
+ # if one exists.
165
+ def reset
166
+ if subject_has_original_method?
167
+ subject_class.__send__(:remove_method, method_name)
168
+ subject_class.__send__(:alias_method, method_name, original_method_alias_name)
169
+ subject_class.__send__(:remove_method, original_method_alias_name)
170
+ else
171
+ if subject_has_method_defined?(method_name)
172
+ subject_class.__send__(:remove_method, method_name)
173
+ end
174
+ end
175
+ end
176
+
177
+ def dispatch_method(subject, args, block)
178
+ if @dispatch_method_delegates_to_dispatch_original_method
179
+ dispatch_original_method(subject, args, block)
180
+ else
181
+ dispatch = MethodDispatches::MethodDispatch.new(self, subject, args, block)
182
+ dispatch.call
183
+ end
184
+ end
185
+
186
+ def dispatch_original_method(subject, args, block)
187
+ dispatch = MethodDispatches::MethodDispatch.new(self, subject, args, block)
188
+ dispatch.call_original_method
189
+ end
190
+
191
+ def subject_has_original_method_missing?
192
+ class_instance_method_defined(subject_class, MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name)
193
+ end
194
+
195
+ def original_method_alias_name
196
+ "__rr__original_#{@method_name}"
197
+ end
198
+
199
+ def dispatch_method_delegates_to_dispatch_original_method
200
+ @dispatch_method_delegates_to_dispatch_original_method = true
201
+ yield
202
+ ensure
203
+ @dispatch_method_delegates_to_dispatch_original_method = nil
204
+ end
205
+
206
+ protected
207
+ def deferred_bind_method
208
+ unless subject_has_method_defined?(original_method_alias_name)
209
+ bind_method_with_alias
210
+ end
211
+ @performed_deferred_bind = true
212
+ end
213
+
214
+ def bind_method_with_alias
215
+ subject_class.__send__(:alias_method, original_method_alias_name, method_name)
216
+ bind_method
217
+ end
218
+ end
219
+ end
220
+ end
@@ -0,0 +1,33 @@
1
+ module RR
2
+ module Injections
3
+ class Injection
4
+ extend(Module.new do
5
+ def instances
6
+ @instances ||= HashWithObjectIdKey.new
7
+ end
8
+ end)
9
+
10
+ include Space::Reader
11
+ include ClassInstanceMethodDefined
12
+
13
+ def subject_has_method_defined?(method_name_in_question)
14
+ class_instance_method_defined(subject_class, method_name_in_question)
15
+ end
16
+
17
+ def subject_has_original_method?
18
+ subject_has_method_defined?(original_method_alias_name)
19
+ end
20
+
21
+ def original_method
22
+ subject_class.instance_method(original_method_alias_name)
23
+ end
24
+
25
+ protected
26
+ def subject_respond_to_method?(subject, method_name)
27
+ subject_has_method_defined?(method_name) ||
28
+ class_instance_method_defined(subject_class, :respond_to?) &&
29
+ subject.respond_to?(method_name)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,73 @@
1
+ module RR
2
+ module Injections
3
+ class MethodMissingInjection < Injection
4
+ extend(Module.new do
5
+ def find_or_create(subject_class)
6
+ instances[subject_class] ||= begin
7
+ new(subject_class).bind
8
+ end
9
+ end
10
+
11
+ def exists?(subject)
12
+ instances.include?(subject)
13
+ end
14
+ end)
15
+ include ClassInstanceMethodDefined
16
+
17
+ attr_reader :subject_class
18
+ def initialize(subject_class)
19
+ @subject_class = subject_class
20
+ @placeholder_method_defined = false
21
+ end
22
+
23
+ def bind
24
+ unless class_instance_method_defined(subject_class, original_method_alias_name)
25
+ unless class_instance_method_defined(subject_class, :method_missing)
26
+ @placeholder_method_defined = true
27
+ subject_class.class_eval do
28
+ def method_missing(method_name, *args, &block)
29
+ super
30
+ end
31
+ end
32
+ end
33
+ subject_class.__send__(:alias_method, original_method_alias_name, :method_missing)
34
+ bind_method
35
+ end
36
+ self
37
+ end
38
+
39
+ def reset
40
+ if subject_has_method_defined?(original_method_alias_name)
41
+ memoized_original_method_alias_name = original_method_alias_name
42
+ placeholder_method_defined = @placeholder_method_defined
43
+ subject_class.class_eval do
44
+ remove_method :method_missing
45
+ unless placeholder_method_defined
46
+ alias_method :method_missing, memoized_original_method_alias_name
47
+ end
48
+ remove_method memoized_original_method_alias_name
49
+ end
50
+ end
51
+ end
52
+
53
+ protected
54
+ BoundObjects = {}
55
+
56
+ def bind_method
57
+ id = BoundObjects.size
58
+ BoundObjects[id] = subject_class
59
+
60
+ subject_class.class_eval((<<-METHOD), __FILE__, __LINE__ + 1)
61
+ def method_missing(method_name, *args, &block)
62
+ obj = ::RR::Injections::MethodMissingInjection::BoundObjects[#{id}]
63
+ MethodDispatches::MethodMissingDispatch.new(self, obj, method_name, args, block).call
64
+ end
65
+ METHOD
66
+ end
67
+
68
+ def original_method_alias_name
69
+ MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,72 @@
1
+ module RR
2
+ module Injections
3
+ class SingletonMethodAddedInjection < Injection
4
+ extend(Module.new do
5
+ def find_or_create(subject_class)
6
+ instances[subject_class] ||= begin
7
+ new(subject_class).bind
8
+ end
9
+ end
10
+
11
+ def find(subject)
12
+ instances[subject]
13
+ end
14
+
15
+ def exists?(subject)
16
+ instances.include?(subject)
17
+ end
18
+ end)
19
+ include ClassInstanceMethodDefined
20
+
21
+ attr_reader :subject_class
22
+ def initialize(subject_class)
23
+ @subject_class = subject_class
24
+ @placeholder_method_defined = false
25
+ end
26
+
27
+ def bind
28
+ unless class_instance_method_defined(subject_class, original_method_alias_name, false)
29
+ unless class_instance_method_defined(subject_class, :singleton_method_added, false)
30
+ @placeholder_method_defined = true
31
+ subject_class.class_eval do
32
+ #def singleton_method_added(method_name)
33
+ # super
34
+ #end
35
+ end
36
+ end
37
+
38
+ memoized_original_method_alias_name = original_method_alias_name
39
+ subject_class.__send__(:alias_method, original_method_alias_name, :singleton_method_added)
40
+ memoized_subject_class = subject_class
41
+ memoized_original_method_alias_name = original_method_alias_name
42
+ subject_class.__send__(:define_method, :singleton_method_added) do |method_name_arg|
43
+ if Injections::DoubleInjection.exists?(memoized_subject_class, method_name_arg)
44
+ Injections::DoubleInjection.find_or_create(memoized_subject_class, method_name_arg).send(:deferred_bind_method)
45
+ end
46
+ __send__(memoized_original_method_alias_name, method_name_arg)
47
+ end
48
+ end
49
+ self
50
+ end
51
+
52
+ def reset
53
+ if subject_has_method_defined?(original_method_alias_name)
54
+ memoized_original_method_alias_name = original_method_alias_name
55
+ placeholder_method_defined = @placeholder_method_defined
56
+ subject_class.class_eval do
57
+ remove_method :singleton_method_added
58
+ unless placeholder_method_defined
59
+ alias_method :singleton_method_added, memoized_original_method_alias_name
60
+ end
61
+ remove_method memoized_original_method_alias_name
62
+ end
63
+ end
64
+ end
65
+
66
+ protected
67
+ def original_method_alias_name
68
+ "__rr__original_singleton_method_added"
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,84 @@
1
+ module RR
2
+ module MethodDispatches
3
+ class BaseMethodDispatch
4
+ extend Forwardable
5
+ include Space::Reader
6
+
7
+ attr_reader :args, :block, :double
8
+
9
+ def call
10
+ raise NotImplementedError
11
+ end
12
+
13
+ protected
14
+ def find_double_to_attempt
15
+ matches = DoubleMatches.new(doubles).find_all_matches(args)
16
+
17
+ unless matches.exact_terminal_doubles_to_attempt.empty?
18
+ return matches.exact_terminal_doubles_to_attempt.first
19
+ end
20
+
21
+ unless matches.exact_non_terminal_doubles_to_attempt.empty?
22
+ return matches.exact_non_terminal_doubles_to_attempt.last
23
+ end
24
+
25
+ unless matches.wildcard_terminal_doubles_to_attempt.empty?
26
+ return matches.wildcard_terminal_doubles_to_attempt.first
27
+ end
28
+
29
+ unless matches.wildcard_non_terminal_doubles_to_attempt.empty?
30
+ return matches.wildcard_non_terminal_doubles_to_attempt.last
31
+ end
32
+
33
+ unless matches.matching_doubles.empty?
34
+ return matches.matching_doubles.first # This will raise a TimesCalledError
35
+ end
36
+
37
+ return nil
38
+ end
39
+
40
+ def call_yields
41
+ if definition.yields_value
42
+ if block
43
+ block.call(*definition.yields_value)
44
+ else
45
+ raise ArgumentError, "A Block must be passed into the method call when using yields"
46
+ end
47
+ end
48
+ end
49
+
50
+ def call_original_method_missing
51
+ subject.__send__(MethodMissingDispatch.original_method_missing_alias_name, method_name, *args, &block)
52
+ end
53
+
54
+ def implementation_is_original_method?
55
+ double.implementation_is_original_method?
56
+ end
57
+
58
+ def extract_subject_from_return_value(return_value)
59
+ case return_value
60
+ when DoubleDefinitions::DoubleDefinition
61
+ return_value.root_subject
62
+ when DoubleDefinitions::DoubleDefinitionCreateBlankSlate
63
+ return_value.__double_definition_create__.root_subject
64
+ else
65
+ return_value
66
+ end
67
+ end
68
+
69
+ def double_not_found_error
70
+ message =
71
+ "On subject #{subject},\n" <<
72
+ "unexpected method invocation:\n" <<
73
+ " #{Double.formatted_name(method_name, args)}\n" <<
74
+ "expected invocations:\n" <<
75
+ Double.list_message_part(doubles)
76
+ raise Errors::DoubleNotFoundError, message
77
+ end
78
+
79
+ def_delegators :definition, :after_call_proc
80
+ def_delegators :double, :definition
81
+ def_delegators :double_injection, :doubles
82
+ end
83
+ end
84
+ end