rr 0.3.11 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. data/CHANGES +8 -3
  2. data/README +130 -39
  3. data/Rakefile +6 -5
  4. data/lib/rr.rb +8 -8
  5. data/lib/rr/adapters/rr_methods.rb +228 -0
  6. data/lib/rr/adapters/rspec.rb +1 -1
  7. data/lib/rr/adapters/test_unit.rb +1 -1
  8. data/lib/rr/double.rb +294 -89
  9. data/lib/rr/{scenario_creator.rb → double_creator.rb} +65 -66
  10. data/lib/rr/{scenario_definition.rb → double_definition.rb} +36 -36
  11. data/lib/rr/{scenario_definition_builder.rb → double_definition_builder.rb} +3 -3
  12. data/lib/rr/double_insertion.rb +132 -0
  13. data/lib/rr/{scenario_matches.rb → double_matches.rb} +2 -2
  14. data/lib/rr/{scenario_method_proxy.rb → double_method_proxy.rb} +2 -2
  15. data/lib/rr/errors/argument_equality_error.rb +3 -3
  16. data/lib/rr/errors/rr_error.rb +13 -13
  17. data/lib/rr/errors/scenario_definition_error.rb +3 -3
  18. data/lib/rr/errors/scenario_not_found_error.rb +3 -3
  19. data/lib/rr/errors/scenario_order_error.rb +3 -3
  20. data/lib/rr/errors/times_called_error.rb +3 -3
  21. data/lib/rr/expectations/any_argument_expectation.rb +1 -1
  22. data/lib/rr/expectations/argument_equality_expectation.rb +1 -1
  23. data/lib/rr/expectations/times_called_expectation.rb +1 -1
  24. data/lib/rr/hash_with_object_id_key.rb +1 -1
  25. data/lib/rr/space.rb +40 -40
  26. data/lib/rr/times_called_matchers/any_times_matcher.rb +13 -13
  27. data/lib/rr/times_called_matchers/at_least_matcher.rb +11 -11
  28. data/lib/rr/times_called_matchers/at_most_matcher.rb +16 -16
  29. data/lib/rr/times_called_matchers/integer_matcher.rb +13 -13
  30. data/lib/rr/times_called_matchers/non_terminal.rb +21 -21
  31. data/lib/rr/times_called_matchers/proc_matcher.rb +7 -7
  32. data/lib/rr/times_called_matchers/range_matcher.rb +14 -14
  33. data/lib/rr/times_called_matchers/terminal.rb +16 -16
  34. data/lib/rr/times_called_matchers/times_called_matcher.rb +32 -32
  35. data/spec/core_spec_suite.rb +18 -0
  36. data/{examples → spec}/environment_fixture_setup.rb +0 -1
  37. data/{examples/high_level_example.rb → spec/high_level_spec.rb} +11 -11
  38. data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +67 -0
  39. data/spec/rr/adapters/rr_methods_creator_spec.rb +365 -0
  40. data/spec/rr/adapters/rr_methods_space_spec.rb +134 -0
  41. data/spec/rr/adapters/rr_methods_spec_helper.rb +11 -0
  42. data/{examples/rr/extensions/instance_methods_times_matcher_example.rb → spec/rr/adapters/rr_methods_times_matcher_spec.rb} +4 -4
  43. data/spec/rr/double/double_insertion_bind_spec.rb +78 -0
  44. data/spec/rr/double/double_insertion_dispatching_spec.rb +221 -0
  45. data/spec/rr/double/double_insertion_has_original_method_spec.rb +56 -0
  46. data/spec/rr/double/double_insertion_register_scenario_spec.rb +24 -0
  47. data/spec/rr/double/double_insertion_reset_spec.rb +89 -0
  48. data/spec/rr/double/double_insertion_spec.rb +66 -0
  49. data/spec/rr/double/double_insertion_verify_spec.rb +23 -0
  50. data/spec/rr/double_creator_spec.rb +454 -0
  51. data/{examples/rr/scenario_definition_example.rb → spec/rr/double_definition_spec.rb} +143 -143
  52. data/spec/rr/double_method_proxy_spec.rb +71 -0
  53. data/spec/rr/double_spec.rb +654 -0
  54. data/spec/rr/errors/rr_error_spec.rb +65 -0
  55. data/spec/rr/expectations/any_argument_expectation_spec.rb +47 -0
  56. data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +38 -0
  57. data/spec/rr/expectations/argument_equality_expectation_spec.rb +58 -0
  58. data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +53 -0
  59. data/spec/rr/expectations/duck_type_argument_equality_expectation_spec.rb +71 -0
  60. data/spec/rr/expectations/is_a_argument_equality_expectation_spec.rb +51 -0
  61. data/spec/rr/expectations/numeric_argument_equality_expectation_spec.rb +47 -0
  62. data/spec/rr/expectations/range_argument_equality_expectation_spec.rb +59 -0
  63. data/spec/rr/expectations/regexp_argument_equality_expectation_spec.rb +72 -0
  64. data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +43 -0
  65. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +67 -0
  66. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +71 -0
  67. data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +21 -0
  68. data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +103 -0
  69. data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +79 -0
  70. data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +81 -0
  71. data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +40 -0
  72. data/spec/rr/rspec/rspec_adapter_spec.rb +65 -0
  73. data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +52 -0
  74. data/spec/rr/rspec/rspec_usage_spec.rb +67 -0
  75. data/spec/rr/space/hash_with_object_id_key_spec.rb +88 -0
  76. data/spec/rr/space/space_create_spec.rb +278 -0
  77. data/spec/rr/space/space_helper.rb +7 -0
  78. data/spec/rr/space/space_register_spec.rb +32 -0
  79. data/spec/rr/space/space_reset_spec.rb +131 -0
  80. data/spec/rr/space/space_spec.rb +32 -0
  81. data/spec/rr/space/space_verify_spec.rb +181 -0
  82. data/{examples → spec}/rr/test_unit/test_helper.rb +1 -1
  83. data/{examples → spec}/rr/test_unit/test_unit_backtrace_test.rb +0 -0
  84. data/{examples → spec}/rr/test_unit/test_unit_integration_test.rb +4 -4
  85. data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +47 -0
  86. data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +55 -0
  87. data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +70 -0
  88. data/spec/rr/times_called_matchers/integer_matcher_spec.rb +70 -0
  89. data/spec/rr/times_called_matchers/proc_matcher_spec.rb +55 -0
  90. data/spec/rr/times_called_matchers/range_matcher_spec.rb +76 -0
  91. data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +118 -0
  92. data/spec/rspec_spec_suite.rb +16 -0
  93. data/spec/spec_helper.rb +9 -0
  94. data/{examples/example_suite.rb → spec/spec_suite.rb} +3 -3
  95. data/{examples/test_unit_example_suite.rb → spec/test_unit_spec_suite.rb} +0 -0
  96. metadata +93 -93
  97. data/examples/core_example_suite.rb +0 -31
  98. data/examples/example_helper.rb +0 -9
  99. data/examples/rr/double/double_bind_example.rb +0 -70
  100. data/examples/rr/double/double_dispatching_example.rb +0 -236
  101. data/examples/rr/double/double_example.rb +0 -66
  102. data/examples/rr/double/double_has_original_method_example.rb +0 -56
  103. data/examples/rr/double/double_register_scenario_example.rb +0 -24
  104. data/examples/rr/double/double_reset_example.rb +0 -89
  105. data/examples/rr/double/double_verify_example.rb +0 -23
  106. data/examples/rr/errors/rr_error_example.rb +0 -65
  107. data/examples/rr/expectations/any_argument_expectation_example.rb +0 -52
  108. data/examples/rr/expectations/anything_argument_equality_expectation_example.rb +0 -38
  109. data/examples/rr/expectations/argument_equality_expectation_example.rb +0 -62
  110. data/examples/rr/expectations/boolean_argument_equality_expectation_example.rb +0 -48
  111. data/examples/rr/expectations/duck_type_argument_equality_expectation_example.rb +0 -67
  112. data/examples/rr/expectations/is_a_argument_equality_expectation_example.rb +0 -50
  113. data/examples/rr/expectations/numeric_argument_equality_expectation_example.rb +0 -46
  114. data/examples/rr/expectations/range_argument_equality_expectation_example.rb +0 -59
  115. data/examples/rr/expectations/regexp_argument_equality_expectation_example.rb +0 -67
  116. data/examples/rr/expectations/times_called_expectation/times_called_expectation_any_times_example.rb +0 -50
  117. data/examples/rr/expectations/times_called_expectation/times_called_expectation_at_least_example.rb +0 -73
  118. data/examples/rr/expectations/times_called_expectation/times_called_expectation_at_most_example.rb +0 -77
  119. data/examples/rr/expectations/times_called_expectation/times_called_expectation_example.rb +0 -42
  120. data/examples/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +0 -20
  121. data/examples/rr/expectations/times_called_expectation/times_called_expectation_integer_example.rb +0 -111
  122. data/examples/rr/expectations/times_called_expectation/times_called_expectation_proc_example.rb +0 -88
  123. data/examples/rr/expectations/times_called_expectation/times_called_expectation_range_example.rb +0 -90
  124. data/examples/rr/extensions/instance_methods_argument_matcher_example.rb +0 -65
  125. data/examples/rr/extensions/instance_methods_creator_example.rb +0 -363
  126. data/examples/rr/extensions/instance_methods_example_helper.rb +0 -11
  127. data/examples/rr/extensions/instance_methods_space_example.rb +0 -122
  128. data/examples/rr/rspec/rspec_adapter_example.rb +0 -63
  129. data/examples/rr/rspec/rspec_backtrace_tweaking_example.rb +0 -36
  130. data/examples/rr/rspec/rspec_usage_example.rb +0 -65
  131. data/examples/rr/scenario_creator_example.rb +0 -459
  132. data/examples/rr/scenario_example.rb +0 -701
  133. data/examples/rr/scenario_method_proxy_example.rb +0 -71
  134. data/examples/rr/space/hash_with_object_id_key_example.rb +0 -86
  135. data/examples/rr/space/space_create_example.rb +0 -278
  136. data/examples/rr/space/space_example.rb +0 -29
  137. data/examples/rr/space/space_helper.rb +0 -7
  138. data/examples/rr/space/space_register_example.rb +0 -32
  139. data/examples/rr/space/space_reset_example.rb +0 -120
  140. data/examples/rr/space/space_verify_example.rb +0 -169
  141. data/examples/rr/times_called_matchers/any_times_matcher_example.rb +0 -63
  142. data/examples/rr/times_called_matchers/at_least_matcher_example.rb +0 -70
  143. data/examples/rr/times_called_matchers/at_most_matcher_example.rb +0 -85
  144. data/examples/rr/times_called_matchers/integer_matcher_example.rb +0 -91
  145. data/examples/rr/times_called_matchers/proc_matcher_example.rb +0 -77
  146. data/examples/rr/times_called_matchers/range_matcher_example.rb +0 -97
  147. data/examples/rr/times_called_matchers/times_called_matcher_example.rb +0 -53
  148. data/examples/rspec_example_suite.rb +0 -25
  149. data/lib/rr/extensions/instance_methods.rb +0 -228
  150. data/lib/rr/scenario.rb +0 -337
@@ -1,337 +0,0 @@
1
- module RR
2
- # RR::Scenario is the use case for a method call.
3
- # It has the ArgumentEqualityExpectation, TimesCalledExpectation,
4
- # and the implementation.
5
- class Scenario
6
- class << self
7
- def formatted_name(method_name, args)
8
- formatted_errors = args.collect {|arg| arg.inspect}.join(', ')
9
- "#{method_name}(#{formatted_errors})"
10
- end
11
-
12
- def list_message_part(scenarios)
13
- scenarios.collect do |scenario|
14
- "- #{formatted_name(scenario.method_name, scenario.expected_arguments)}"
15
- end.join("\n")
16
- end
17
- end
18
-
19
- attr_reader :times_called, :double, :definition
20
-
21
- def initialize(space, double, definition)
22
- @space = space
23
- @double = double
24
- @definition = definition
25
- @times_called = 0
26
- @times_called_expectation = Expectations::TimesCalledExpectation.new(self)
27
- end
28
-
29
- # Scenario#with sets the expectation that the Scenario will receive
30
- # the passed in arguments.
31
- #
32
- # Passing in a block sets the return value.
33
- #
34
- # mock(subject).method_name.with(1, 2) {:return_value}
35
- def with(*args, &returns)
36
- definition.with(*args, &returns)
37
- end
38
-
39
- # Scenario#with_any_args sets the expectation that the Scenario can receive
40
- # any arguments.
41
- #
42
- # Passing in a block sets the return value.
43
- #
44
- # mock(subject).method_name.with_any_args {:return_value}
45
- def with_any_args(&returns)
46
- definition.with_any_args(&returns)
47
- end
48
-
49
- # Scenario#with_no_args sets the expectation that the Scenario will receive
50
- # no arguments.
51
- #
52
- # Passing in a block sets the return value.
53
- #
54
- # mock(subject).method_name.with_no_args {:return_value}
55
- def with_no_args(&returns)
56
- definition.with_no_args(&returns)
57
- end
58
-
59
- # Scenario#never sets the expectation that the Scenario will never be
60
- # called.
61
- #
62
- # This method does not accept a block because it will never be called.
63
- #
64
- # mock(subject).method_name.never
65
- def never
66
- definition.never
67
- end
68
-
69
- # Scenario#once sets the expectation that the Scenario will be called
70
- # 1 time.
71
- #
72
- # Passing in a block sets the return value.
73
- #
74
- # mock(subject).method_name.once {:return_value}
75
- def once(&returns)
76
- definition.once(&returns)
77
- end
78
-
79
- # Scenario#twice sets the expectation that the Scenario will be called
80
- # 2 times.
81
- #
82
- # Passing in a block sets the return value.
83
- #
84
- # mock(subject).method_name.twice {:return_value}
85
- def twice(&returns)
86
- definition.twice(&returns)
87
- end
88
-
89
- # Scenario#at_least sets the expectation that the Scenario
90
- # will be called at least n times.
91
- # It works by creating a TimesCalledExpectation.
92
- #
93
- # Passing in a block sets the return value.
94
- #
95
- # mock(subject).method_name.at_least(4) {:return_value}
96
- def at_least(number, &returns)
97
- definition.at_least(number, &returns)
98
- end
99
-
100
- # Scenario#at_most allows sets the expectation that the Scenario
101
- # will be called at most n times.
102
- # It works by creating a TimesCalledExpectation.
103
- #
104
- # Passing in a block sets the return value.
105
- #
106
- # mock(subject).method_name.at_most(4) {:return_value}
107
- def at_most(number, &returns)
108
- definition.at_most(number, &returns)
109
- end
110
-
111
- # Scenario#any_number_of_times sets an that the Scenario will be called
112
- # any number of times. This effectively removes the times called expectation
113
- # from the Scenarion
114
- #
115
- # Passing in a block sets the return value.
116
- #
117
- # mock(subject).method_name.any_number_of_times
118
- def any_number_of_times(&returns)
119
- definition.any_number_of_times(&returns)
120
- end
121
-
122
- # Scenario#times creates an TimesCalledExpectation of the passed
123
- # in number.
124
- #
125
- # Passing in a block sets the return value.
126
- #
127
- # mock(subject).method_name.times(4) {:return_value}
128
- def times(number, &returns)
129
- definition.times(number, &returns)
130
- end
131
-
132
- # Scenario#ordered sets the Scenario to have an ordered
133
- # expectation.
134
- #
135
- # Passing in a block sets the return value.
136
- #
137
- # mock(subject).method_name.ordered {return_value}
138
- def ordered(&returns)
139
- definition.ordered(&returns)
140
- end
141
-
142
- # Scenario#ordered? returns true when the Scenario is ordered.
143
- #
144
- # mock(subject).method_name.ordered?
145
- def ordered?
146
- definition.ordered?
147
- end
148
-
149
- # Scenario#yields sets the Scenario to invoke a passed in block when
150
- # the Scenario is called.
151
- # An Expection will be raised if no block is passed in when the
152
- # Scenario is called.
153
- #
154
- # Passing in a block sets the return value.
155
- #
156
- # mock(subject).method_name.yields(yield_arg1, yield_arg2) {return_value}
157
- # subject.method_name {|yield_arg1, yield_arg2|}
158
- def yields(*args, &returns)
159
- definition.yields(*args, &returns)
160
- end
161
-
162
- # Scenario#after_call creates a callback that occurs after call
163
- # is called. The passed in block receives the return value of
164
- # the Scenario being called.
165
- # An Expection will be raised if no block is passed in.
166
- #
167
- # mock(subject).method_name {return_value}.after_call {|return_value|}
168
- # subject.method_name # return_value
169
- #
170
- # This feature is built into probes.
171
- # probe(User).find('1') {|user| mock(user).valid? {false}}
172
- def after_call(&block)
173
- definition.after_call &block
174
- end
175
-
176
- # Scenario#returns accepts an argument value or a block.
177
- # It will raise an ArgumentError if both are passed in.
178
- #
179
- # Passing in a block causes Scenario to return the return value of
180
- # the passed in block.
181
- #
182
- # Passing in an argument causes Scenario to return the argument.
183
- def returns(value=nil, &implementation)
184
- definition.returns(value, &implementation)
185
- end
186
-
187
- # Scenario#implemented_by sets the implementation of the Scenario.
188
- # This method takes a Proc or a Method. Passing in a Method allows
189
- # the Scenario to accept blocks.
190
- #
191
- # obj = Object.new
192
- # def obj.foobar
193
- # yield(1)
194
- # end
195
- # mock(obj).method_name.implemented_by(obj.method(:foobar))
196
- def implemented_by(implementation)
197
- definition.implemented_by implementation
198
- end
199
-
200
- # Scenario#implemented_by_original_method sets the implementation
201
- # of the Scenario to be the original method.
202
- # This is primarily used with probes.
203
- #
204
- # obj = Object.new
205
- # def obj.foobar
206
- # yield(1)
207
- # end
208
- # mock(obj).method_name.implemented_by_original_method
209
- # obj.foobar {|arg| puts arg} # puts 1
210
- def implemented_by_original_method
211
- definition.implemented_by_original_method
212
- end
213
-
214
- # Scenario#call calls the Scenario's implementation. The return
215
- # value of the implementation is returned.
216
- #
217
- # A TimesCalledError is raised when the times called
218
- # exceeds the expected TimesCalledExpectation.
219
- def call(double, *args, &block)
220
- self.times_called_expectation.attempt! if definition.times_matcher
221
- @space.verify_ordered_scenario(self) if ordered?
222
- yields!(block)
223
- return_value = call_implementation(double, *args, &block)
224
- return return_value unless definition.after_call_value
225
- definition.after_call_value.call(return_value)
226
- end
227
-
228
- def yields!(block)
229
- if definition.yields_value
230
- unless block
231
- raise ArgumentError, "A Block must be passed into the method call when using yields"
232
- end
233
- block.call(*definition.yields_value)
234
- end
235
- end
236
- protected :yields!
237
-
238
- def call_implementation(double, *args, &block)
239
- return nil unless implementation
240
-
241
- if implementation === ScenarioDefinition::ORIGINAL_METHOD
242
- if double.object_has_original_method?
243
- return double.call_original_method(*args, &block)
244
- else
245
- return double.object.__send__(
246
- :method_missing,
247
- method_name,
248
- *args,
249
- &block
250
- )
251
- end
252
- end
253
-
254
- if implementation.is_a?(Method)
255
- return implementation.call(*args, &block)
256
- else
257
- args << block if block
258
- return implementation.call(*args)
259
- end
260
- end
261
- protected :call_implementation
262
-
263
- # Scenario#exact_match? returns true when the passed in arguments
264
- # exactly match the ArgumentEqualityExpectation arguments.
265
- def exact_match?(*arguments)
266
- definition.exact_match?(*arguments)
267
- end
268
-
269
- # Scenario#wildcard_match? returns true when the passed in arguments
270
- # wildcard match the ArgumentEqualityExpectation arguments.
271
- def wildcard_match?(*arguments)
272
- definition.wildcard_match?(*arguments)
273
- end
274
-
275
- # Scenario#attempt? returns true when the
276
- # TimesCalledExpectation is satisfied.
277
- def attempt?
278
- return true unless definition.times_matcher
279
- times_called_expectation.attempt?
280
- end
281
-
282
- # Scenario#verify verifies the the TimesCalledExpectation
283
- # is satisfied for this scenario. A TimesCalledError
284
- # is raised if the TimesCalledExpectation is not met.
285
- def verify
286
- return true unless definition.times_matcher
287
- times_called_expectation.verify!
288
- true
289
- end
290
-
291
- def terminal?
292
- return false unless definition.times_matcher
293
- times_called_expectation.terminal?
294
- end
295
-
296
- # The method name that this Scenario is attatched to
297
- def method_name
298
- double.method_name
299
- end
300
-
301
- # The Arguments that this Scenario expects
302
- def expected_arguments
303
- return [] unless argument_expectation
304
- argument_expectation.expected_arguments
305
- end
306
-
307
- # The TimesCalledMatcher for the TimesCalledExpectation
308
- def times_matcher
309
- times_called_expectation.matcher
310
- end
311
-
312
- def times_called_expectation
313
- @times_called_expectation.matcher = definition.times_matcher
314
- @times_called_expectation
315
- end
316
-
317
- def implementation
318
- definition.implementation
319
- end
320
- def implementation=(value)
321
- definition.implementation = value
322
- end
323
- protected :implementation=
324
-
325
- def argument_expectation
326
- definition.argument_expectation
327
- end
328
- def argument_expectation=(value)
329
- definition.argument_expectation = value
330
- end
331
- protected :argument_expectation=
332
-
333
- def formatted_name
334
- self.class.formatted_name(method_name, expected_arguments)
335
- end
336
- end
337
- end