redinger-rr 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. data/CHANGES +221 -0
  2. data/README.rdoc +343 -0
  3. data/Rakefile +88 -0
  4. data/VERSION.yml +4 -0
  5. data/lib/rr.rb +88 -0
  6. data/lib/rr/adapters/rr_methods.rb +122 -0
  7. data/lib/rr/adapters/rspec.rb +59 -0
  8. data/lib/rr/adapters/test_unit.rb +29 -0
  9. data/lib/rr/double.rb +152 -0
  10. data/lib/rr/double_definitions/child_double_definition_creator.rb +27 -0
  11. data/lib/rr/double_definitions/double_definition.rb +348 -0
  12. data/lib/rr/double_definitions/double_definition_creator.rb +167 -0
  13. data/lib/rr/double_definitions/double_definition_creator_proxy.rb +37 -0
  14. data/lib/rr/double_definitions/strategies/implementation/implementation_strategy.rb +15 -0
  15. data/lib/rr/double_definitions/strategies/implementation/proxy.rb +62 -0
  16. data/lib/rr/double_definitions/strategies/implementation/reimplementation.rb +14 -0
  17. data/lib/rr/double_definitions/strategies/implementation/strongly_typed_reimplementation.rb +17 -0
  18. data/lib/rr/double_definitions/strategies/scope/instance.rb +15 -0
  19. data/lib/rr/double_definitions/strategies/scope/instance_of_class.rb +50 -0
  20. data/lib/rr/double_definitions/strategies/scope/scope_strategy.rb +15 -0
  21. data/lib/rr/double_definitions/strategies/strategy.rb +70 -0
  22. data/lib/rr/double_definitions/strategies/verification/dont_allow.rb +34 -0
  23. data/lib/rr/double_definitions/strategies/verification/mock.rb +44 -0
  24. data/lib/rr/double_definitions/strategies/verification/stub.rb +45 -0
  25. data/lib/rr/double_definitions/strategies/verification/verification_strategy.rb +15 -0
  26. data/lib/rr/double_injection.rb +180 -0
  27. data/lib/rr/double_matches.rb +51 -0
  28. data/lib/rr/errors/argument_equality_error.rb +6 -0
  29. data/lib/rr/errors/double_definition_error.rb +6 -0
  30. data/lib/rr/errors/double_not_found_error.rb +6 -0
  31. data/lib/rr/errors/double_order_error.rb +6 -0
  32. data/lib/rr/errors/rr_error.rb +20 -0
  33. data/lib/rr/errors/spy_verification_errors/double_injection_not_found_error.rb +8 -0
  34. data/lib/rr/errors/spy_verification_errors/invocation_count_error.rb +8 -0
  35. data/lib/rr/errors/spy_verification_errors/spy_verification_error.rb +8 -0
  36. data/lib/rr/errors/subject_does_not_implement_method_error.rb +6 -0
  37. data/lib/rr/errors/subject_has_different_arity_error.rb +6 -0
  38. data/lib/rr/errors/times_called_error.rb +6 -0
  39. data/lib/rr/expectations/any_argument_expectation.rb +21 -0
  40. data/lib/rr/expectations/argument_equality_expectation.rb +41 -0
  41. data/lib/rr/expectations/times_called_expectation.rb +57 -0
  42. data/lib/rr/hash_with_object_id_key.rb +44 -0
  43. data/lib/rr/method_dispatches/base_method_dispatch.rb +108 -0
  44. data/lib/rr/method_dispatches/method_dispatch.rb +61 -0
  45. data/lib/rr/method_dispatches/method_missing_dispatch.rb +49 -0
  46. data/lib/rr/proc_from_block.rb +7 -0
  47. data/lib/rr/recorded_calls.rb +103 -0
  48. data/lib/rr/space.rb +123 -0
  49. data/lib/rr/spy_verification.rb +48 -0
  50. data/lib/rr/spy_verification_proxy.rb +18 -0
  51. data/lib/rr/times_called_matchers/any_times_matcher.rb +18 -0
  52. data/lib/rr/times_called_matchers/at_least_matcher.rb +15 -0
  53. data/lib/rr/times_called_matchers/at_most_matcher.rb +23 -0
  54. data/lib/rr/times_called_matchers/integer_matcher.rb +19 -0
  55. data/lib/rr/times_called_matchers/non_terminal.rb +27 -0
  56. data/lib/rr/times_called_matchers/proc_matcher.rb +11 -0
  57. data/lib/rr/times_called_matchers/range_matcher.rb +21 -0
  58. data/lib/rr/times_called_matchers/terminal.rb +20 -0
  59. data/lib/rr/times_called_matchers/times_called_matcher.rb +44 -0
  60. data/lib/rr/wildcard_matchers.rb +158 -0
  61. data/lib/rr/wildcard_matchers/anything.rb +18 -0
  62. data/lib/rr/wildcard_matchers/boolean.rb +23 -0
  63. data/lib/rr/wildcard_matchers/duck_type.rb +32 -0
  64. data/lib/rr/wildcard_matchers/hash_including.rb +29 -0
  65. data/lib/rr/wildcard_matchers/is_a.rb +25 -0
  66. data/lib/rr/wildcard_matchers/numeric.rb +13 -0
  67. data/lib/rr/wildcard_matchers/range.rb +7 -0
  68. data/lib/rr/wildcard_matchers/regexp.rb +7 -0
  69. data/lib/rr/wildcard_matchers/satisfy.rb +26 -0
  70. data/spec/core_spec_suite.rb +19 -0
  71. data/spec/environment_fixture_setup.rb +7 -0
  72. data/spec/high_level_spec.rb +398 -0
  73. data/spec/proc_from_block_spec.rb +14 -0
  74. data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +67 -0
  75. data/spec/rr/adapters/rr_methods_creator_spec.rb +149 -0
  76. data/spec/rr/adapters/rr_methods_space_spec.rb +115 -0
  77. data/spec/rr/adapters/rr_methods_spec_helper.rb +11 -0
  78. data/spec/rr/adapters/rr_methods_times_matcher_spec.rb +17 -0
  79. data/spec/rr/double_definitions/child_double_definition_creator_spec.rb +112 -0
  80. data/spec/rr/double_definitions/double_definition_creator_proxy_spec.rb +155 -0
  81. data/spec/rr/double_definitions/double_definition_creator_spec.rb +502 -0
  82. data/spec/rr/double_definitions/double_definition_spec.rb +1165 -0
  83. data/spec/rr/double_injection/double_injection_spec.rb +339 -0
  84. data/spec/rr/double_injection/double_injection_verify_spec.rb +29 -0
  85. data/spec/rr/double_spec.rb +352 -0
  86. data/spec/rr/errors/rr_error_spec.rb +67 -0
  87. data/spec/rr/expectations/any_argument_expectation_spec.rb +47 -0
  88. data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +14 -0
  89. data/spec/rr/expectations/argument_equality_expectation_spec.rb +135 -0
  90. data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +34 -0
  91. data/spec/rr/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
  92. data/spec/rr/expectations/hash_including_spec.rb +17 -0
  93. data/spec/rr/expectations/satisfy_argument_equality_expectation_spec.rb +59 -0
  94. data/spec/rr/expectations/satisfy_spec.rb +14 -0
  95. data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +46 -0
  96. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +69 -0
  97. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +71 -0
  98. data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +23 -0
  99. data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +104 -0
  100. data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +81 -0
  101. data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +83 -0
  102. data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +38 -0
  103. data/spec/rr/rspec/invocation_matcher_spec.rb +279 -0
  104. data/spec/rr/rspec/rspec_adapter_spec.rb +66 -0
  105. data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +31 -0
  106. data/spec/rr/rspec/rspec_backtrace_tweaking_spec_fixture.rb +11 -0
  107. data/spec/rr/rspec/rspec_usage_spec.rb +86 -0
  108. data/spec/rr/space/hash_with_object_id_key_spec.rb +88 -0
  109. data/spec/rr/space/space_spec.rb +550 -0
  110. data/spec/rr/test_unit/test_helper.rb +7 -0
  111. data/spec/rr/test_unit/test_unit_backtrace_test.rb +36 -0
  112. data/spec/rr/test_unit/test_unit_integration_test.rb +57 -0
  113. data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +47 -0
  114. data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +55 -0
  115. data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +70 -0
  116. data/spec/rr/times_called_matchers/integer_matcher_spec.rb +70 -0
  117. data/spec/rr/times_called_matchers/proc_matcher_spec.rb +55 -0
  118. data/spec/rr/times_called_matchers/range_matcher_spec.rb +76 -0
  119. data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +118 -0
  120. data/spec/rr/wildcard_matchers/anything_spec.rb +24 -0
  121. data/spec/rr/wildcard_matchers/boolean_spec.rb +36 -0
  122. data/spec/rr/wildcard_matchers/duck_type_spec.rb +52 -0
  123. data/spec/rr/wildcard_matchers/is_a_spec.rb +32 -0
  124. data/spec/rr/wildcard_matchers/numeric_spec.rb +32 -0
  125. data/spec/rr/wildcard_matchers/range_spec.rb +35 -0
  126. data/spec/rr/wildcard_matchers/regexp_spec.rb +43 -0
  127. data/spec/rr_spec.rb +28 -0
  128. data/spec/rspec_spec_suite.rb +17 -0
  129. data/spec/spec_helper.rb +109 -0
  130. data/spec/spec_suite.rb +31 -0
  131. data/spec/spy_verification_spec.rb +129 -0
  132. data/spec/test_unit_spec_suite.rb +21 -0
  133. metadata +193 -0
@@ -0,0 +1,1165 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
2
+
3
+ module RR
4
+ module DoubleDefinitions
5
+ describe DoubleDefinition do
6
+ attr_reader :subject, :double_definition_creator, :double, :definition
7
+
8
+ it_should_behave_like "Swapped Space"
9
+
10
+ before do
11
+ @subject = Object.new
12
+ add_original_method
13
+ @double_definition_creator = DoubleDefinitionCreator.new
14
+ @definition = double_definition_creator.stub(subject).foobar
15
+ @double = definition.double
16
+ end
17
+
18
+ def add_original_method
19
+ def subject.foobar(a, b)
20
+ :original_return_value
21
+ end
22
+ end
23
+
24
+ describe "#root_subject" do
25
+ it "returns #double_definition_creator.root_subject" do
26
+ definition.root_subject.should == definition.double_definition_creator.root_subject
27
+ definition.root_subject.should == subject
28
+ end
29
+ end
30
+
31
+ describe "DefinitionConstructionMethods" do
32
+ macro("DoubleDefinition where #double_definition_creator is a Reimplementation") do
33
+ before do
34
+ definition.double_definition_creator.implementation_strategy.class.should == Strategies::Implementation::Reimplementation
35
+ call_double_injection
36
+ end
37
+ end
38
+
39
+ macro("DoubleDefinition where #double_definition_creator is a Proxy") do
40
+ before do
41
+ definition.double_definition_creator.proxy
42
+ definition.double_definition_creator.implementation_strategy.class.should == Strategies::Implementation::Proxy
43
+ call_double_injection
44
+ end
45
+ end
46
+
47
+ describe "#with" do
48
+ macro("#with") do
49
+ it "returns DoubleDefinition" do
50
+ definition.with(1).should === definition
51
+ end
52
+
53
+ it "sets an ArgumentEqualityExpectation" do
54
+ definition.should be_exact_match(1, 2)
55
+ definition.should_not be_exact_match(2)
56
+ end
57
+ end
58
+
59
+ context "when not passed a block" do
60
+ before do
61
+ definition.with(1, 2)
62
+ subject.foobar(1, 2)
63
+ end
64
+
65
+ send "#with"
66
+ end
67
+
68
+ context "when passed a block" do
69
+ def call_double_injection
70
+ actual_args = nil
71
+ definition.with(1, 2) do |*args|
72
+ actual_args = args
73
+ :new_return_value
74
+ end
75
+ subject.foobar(1, 2)
76
+ @return_value = subject.foobar(1, 2)
77
+ @args = actual_args
78
+ end
79
+
80
+ context "when #double_definition_creator.implementation_strategy is a Reimplementation" do
81
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
82
+ send "#with"
83
+
84
+ describe "#subject.method_name being called" do
85
+ it "returns the return value of the block" do
86
+ @return_value.should == :new_return_value
87
+ @args.should == [1, 2]
88
+ end
89
+ end
90
+ end
91
+
92
+ context "when #double_definition_creator.implementation_strategy is a Proxy" do
93
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
94
+ send "#with"
95
+
96
+ describe "#subject.method_name being called" do
97
+ it "returns the return value of the block" do
98
+ @return_value.should == :new_return_value
99
+ @args.should == [:original_return_value]
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "#with_any_args" do
107
+ macro "#with_any_args" do
108
+ it "returns DoubleDefinition" do
109
+ definition.with_no_args.should === definition
110
+ end
111
+
112
+ it "sets an AnyArgumentExpectation" do
113
+ definition.should_not be_exact_match(1)
114
+ definition.should be_wildcard_match(1)
115
+ end
116
+ end
117
+
118
+ context "when not passed a block" do
119
+ before do
120
+ definition.with_any_args
121
+ end
122
+
123
+ send "#with_any_args"
124
+ end
125
+
126
+ context "when passed a block" do
127
+ def call_double_injection
128
+ actual_args = nil
129
+ definition.with_any_args do |*args|
130
+ actual_args = args
131
+ :new_return_value
132
+ end
133
+ @return_value = subject.foobar(1, 2)
134
+ @args = actual_args
135
+ end
136
+
137
+ context "when #double_definition_creator.implementation_strategy is a Reimplementation" do
138
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
139
+ send "#with_any_args"
140
+
141
+ describe "#subject.method_name being called" do
142
+ it "returns the return value of the block" do
143
+ @return_value.should == :new_return_value
144
+ @args.should == [1, 2]
145
+ end
146
+ end
147
+ end
148
+
149
+ context "when #double_definition_creator.implementation_strategy is a Proxy" do
150
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
151
+ send "#with_any_args"
152
+
153
+ describe "#subject.method_name being called" do
154
+ it "returns the return value of the block" do
155
+ @return_value.should == :new_return_value
156
+ @args.should == [:original_return_value]
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ describe "#with_no_args" do
164
+ macro "#with_no_args" do
165
+ it "returns DoubleDefinition" do
166
+ definition.with_no_args.should === definition
167
+ end
168
+
169
+ it "sets an ArgumentEqualityExpectation with no arguments" do
170
+ definition.argument_expectation.should == Expectations::ArgumentEqualityExpectation.new()
171
+ end
172
+ end
173
+
174
+ def add_original_method
175
+ def subject.foobar()
176
+ :original_return_value
177
+ end
178
+ end
179
+
180
+ context "when not passed a block" do
181
+ before do
182
+ definition.with_no_args
183
+ end
184
+
185
+ send "#with_no_args"
186
+ end
187
+
188
+ context "when passed a block" do
189
+ def call_double_injection
190
+ actual_args = nil
191
+ definition.with_no_args do |*args|
192
+ actual_args = args
193
+ :new_return_value
194
+ end
195
+ @return_value = subject.foobar
196
+ @args = actual_args
197
+ end
198
+
199
+ context "when #double_definition_creator.implementation_strategy is a Reimplementation" do
200
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
201
+ send "#with_no_args"
202
+
203
+ describe "#subject.method_name being called" do
204
+ it "returns the return value of the block" do
205
+ @return_value.should == :new_return_value
206
+ @args.should == []
207
+ end
208
+ end
209
+ end
210
+
211
+ context "when #double_definition_creator.implementation_strategy is a Proxy" do
212
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
213
+ send "#with_no_args"
214
+
215
+ describe "#subject.method_name being called" do
216
+ it "returns the return value of the block" do
217
+ @return_value.should == :new_return_value
218
+ @args.should == [:original_return_value]
219
+ end
220
+ end
221
+ end
222
+ end
223
+ end
224
+
225
+ describe "#never" do
226
+ it "returns DoubleDefinition" do
227
+ definition.never.should === definition
228
+ end
229
+
230
+ it "sets up a Times Called Expectation with 0" do
231
+ definition.with_any_args
232
+ definition.never
233
+ lambda {subject.foobar}.should raise_error(Errors::TimesCalledError)
234
+ end
235
+
236
+ describe "#subject.method_name being called" do
237
+ it "raises a TimesCalledError" do
238
+ definition.with_any_args.never
239
+ lambda {subject.foobar}.should raise_error(Errors::TimesCalledError)
240
+ end
241
+ end
242
+ end
243
+
244
+ describe "#once" do
245
+ macro "#once" do
246
+ it "returns DoubleDefinition" do
247
+ definition.once.should === definition
248
+ end
249
+
250
+ it "sets up a Times Called Expectation with 1" do
251
+ lambda {subject.foobar}.should raise_error(Errors::TimesCalledError)
252
+ end
253
+ end
254
+
255
+ context "when not passed a block" do
256
+ before do
257
+ definition.with_any_args.once
258
+ subject.foobar(1, 2)
259
+ end
260
+
261
+ send "#once"
262
+ end
263
+
264
+ context "when passed a block" do
265
+ def call_double_injection
266
+ actual_args = nil
267
+ definition.with_any_args.once do |*args|
268
+ actual_args = args
269
+ :new_return_value
270
+ end
271
+ @return_value = subject.foobar(1, 2)
272
+ @args = actual_args
273
+ end
274
+
275
+ context "with returns block_callback_strategy" do
276
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
277
+ send "#once"
278
+
279
+ describe "#subject.method_name being called with any arguments" do
280
+ it "returns the return value of the block" do
281
+ @return_value.should == :new_return_value
282
+ @args.should == [1, 2]
283
+ end
284
+ end
285
+ end
286
+
287
+ context "with after_call block_callback_strategy" do
288
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
289
+ send "#once"
290
+
291
+ describe "#subject.method_name being called with any arguments" do
292
+ it "returns the return value of the block" do
293
+ @return_value.should == :new_return_value
294
+ @args.should == [:original_return_value]
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end
300
+
301
+ describe "#twice" do
302
+ macro "#twice" do
303
+ it "returns DoubleDefinition" do
304
+ definition.twice.should === definition
305
+ end
306
+
307
+ it "sets up a Times Called Expectation with 2" do
308
+ definition.twice.with_any_args
309
+ lambda {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
310
+ end
311
+ end
312
+
313
+ context "when not passed a block" do
314
+ before do
315
+ definition.with_any_args.twice
316
+ subject.foobar(1, 2)
317
+ subject.foobar(1, 2)
318
+ end
319
+
320
+ send "#twice"
321
+ end
322
+
323
+ context "when passed a block" do
324
+ def call_double_injection
325
+ actual_args = nil
326
+ definition.with_any_args.twice do |*args|
327
+ actual_args = args
328
+ :new_return_value
329
+ end
330
+ subject.foobar(1, 2)
331
+ @return_value = subject.foobar(1, 2)
332
+ @args = actual_args
333
+ end
334
+
335
+ context "with returns block_callback_strategy" do
336
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
337
+ send "#twice"
338
+
339
+ describe "#subject.method_name being called" do
340
+ it "returns the return value of the block" do
341
+ @return_value.should == :new_return_value
342
+ @args.should == [1, 2]
343
+ end
344
+ end
345
+ end
346
+
347
+ context "with after_call block_callback_strategy" do
348
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
349
+ send "#twice"
350
+
351
+ describe "#subject.method_name being called" do
352
+ it "returns the return value of the block" do
353
+ @return_value.should == :new_return_value
354
+ @args.should == [:original_return_value]
355
+ end
356
+ end
357
+ end
358
+ end
359
+ end
360
+
361
+ describe "#at_least" do
362
+ macro "#at_least" do
363
+ it "returns DoubleDefinition" do
364
+ definition.with_any_args.at_least(2).should === definition
365
+ end
366
+
367
+ it "sets up a Times Called Expectation with 1" do
368
+ definition.times_matcher.should == TimesCalledMatchers::AtLeastMatcher.new(2)
369
+ end
370
+ end
371
+
372
+ context "when not passed a block" do
373
+ before do
374
+ definition.with_any_args.at_least(2)
375
+ subject.foobar(1, 2)
376
+ end
377
+
378
+ send "#at_least"
379
+ end
380
+
381
+ context "when passed a block" do
382
+ def call_double_injection
383
+ actual_args = nil
384
+ definition.with_any_args.at_least(2) do |*args|
385
+ actual_args = args
386
+ :new_return_value
387
+ end
388
+ subject.foobar(1, 2)
389
+ @return_value = subject.foobar(1, 2)
390
+ @args = actual_args
391
+ end
392
+
393
+ context "with returns block_callback_strategy" do
394
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
395
+ send "#at_least"
396
+
397
+ describe "#subject.method_name being called" do
398
+ it "returns the return value of the block" do
399
+ @return_value.should == :new_return_value
400
+ @args.should == [1, 2]
401
+ end
402
+ end
403
+ end
404
+
405
+ context "with after_call block_callback_strategy" do
406
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
407
+ send "#at_least"
408
+
409
+ describe "#subject.method_name being called" do
410
+ it "returns the return value of the block" do
411
+ @return_value.should == :new_return_value
412
+ @args.should == [:original_return_value]
413
+ end
414
+ end
415
+ end
416
+ end
417
+ end
418
+
419
+ describe "#at_most" do
420
+ macro "#at_most" do
421
+ it "returns DoubleDefinition" do
422
+ definition.with_any_args.at_most(2).should === definition
423
+ end
424
+
425
+ it "sets up a Times Called Expectation with 1" do
426
+ lambda do
427
+ subject.foobar
428
+ end.should raise_error(Errors::TimesCalledError, "foobar()\nCalled 3 times.\nExpected at most 2 times.")
429
+ end
430
+ end
431
+
432
+ context "when not passed a block" do
433
+ before do
434
+ definition.with_any_args.at_most(2)
435
+ subject.foobar(1, 2)
436
+ subject.foobar(1, 2)
437
+ end
438
+
439
+ send "#at_most"
440
+ end
441
+
442
+ context "when passed a block" do
443
+ def call_double_injection
444
+ actual_args = nil
445
+ definition.with_any_args.at_most(2) do |*args|
446
+ actual_args = args
447
+ :new_return_value
448
+ end
449
+ subject.foobar(1, 2)
450
+ @return_value = subject.foobar(1, 2)
451
+ @args = actual_args
452
+ end
453
+
454
+ context "with returns block_callback_strategy" do
455
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
456
+ send "#at_most"
457
+
458
+ describe "#subject.method_name being called" do
459
+ it "returns the return value of the block" do
460
+ @return_value.should == :new_return_value
461
+ @args.should == [1, 2]
462
+ end
463
+ end
464
+ end
465
+
466
+ context "with after_call block_callback_strategy" do
467
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
468
+ send "#at_most"
469
+
470
+ describe "#subject.method_name being called" do
471
+ it "returns the return value of the block" do
472
+ @return_value.should == :new_return_value
473
+ @args.should == [:original_return_value]
474
+ end
475
+ end
476
+ end
477
+ end
478
+ end
479
+
480
+ describe "#times" do
481
+ macro "#times" do
482
+ it "returns DoubleDefinition" do
483
+ definition.times(3).should === definition
484
+ end
485
+
486
+ it "sets up a Times Called Expectation with passed in times" do
487
+ lambda {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
488
+ end
489
+ end
490
+
491
+ context "when not passed a block" do
492
+ before do
493
+ definition.with(1, 2).times(3)
494
+ subject.foobar(1, 2)
495
+ subject.foobar(1, 2)
496
+ subject.foobar(1, 2)
497
+ end
498
+
499
+ send "#times"
500
+ end
501
+
502
+ context "when passed a block" do
503
+ def call_double_injection
504
+ actual_args = nil
505
+ definition.with(1, 2).times(3) do |*args|
506
+ actual_args = args
507
+ :new_return_value
508
+ end
509
+ subject.foobar(1, 2)
510
+ subject.foobar(1, 2)
511
+ @return_value = subject.foobar(1, 2)
512
+ @args = actual_args
513
+ end
514
+
515
+ context "with returns block_callback_strategy" do
516
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
517
+ send "#times"
518
+
519
+ describe "#subject.method_name being called" do
520
+ it "returns the return value of the block" do
521
+ @return_value.should == :new_return_value
522
+ @args.should == [1, 2]
523
+ end
524
+ end
525
+ end
526
+
527
+ context "with after_call block_callback_strategy" do
528
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
529
+ send "#times"
530
+
531
+ describe "#subject.method_name being called" do
532
+ it "returns the return value of the block" do
533
+ @return_value.should == :new_return_value
534
+ @args.should == [:original_return_value]
535
+ end
536
+ end
537
+ end
538
+ end
539
+ end
540
+
541
+ describe "#any_number_of_times" do
542
+ macro "#any_number_of_times" do
543
+ it "returns DoubleDefinition" do
544
+ definition.any_number_of_times.should === definition
545
+ end
546
+
547
+ it "sets up a Times Called Expectation with AnyTimes matcher" do
548
+ definition.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
549
+ end
550
+ end
551
+
552
+ context "when not passed a block" do
553
+ before do
554
+ definition.with(1, 2).any_number_of_times
555
+ subject.foobar(1, 2)
556
+ end
557
+
558
+ send "#any_number_of_times"
559
+ end
560
+
561
+ context "when passed a block" do
562
+ def call_double_injection
563
+ actual_args = nil
564
+ definition.with(1, 2).any_number_of_times do |*args|
565
+ actual_args = args
566
+ :new_return_value
567
+ end
568
+ subject.foobar(1, 2)
569
+ @return_value = subject.foobar(1, 2)
570
+ @args = actual_args
571
+ end
572
+
573
+ context "with returns block_callback_strategy" do
574
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
575
+ send "#any_number_of_times"
576
+
577
+ describe "#subject.method_name being called" do
578
+ it "returns the return value of the block" do
579
+ @return_value.should == :new_return_value
580
+ @args.should == [1, 2]
581
+ end
582
+ end
583
+ end
584
+
585
+ context "with after_call block_callback_strategy" do
586
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
587
+ send "#any_number_of_times"
588
+
589
+ describe "#subject.method_name being called" do
590
+ it "returns the return value of the block" do
591
+ @return_value.should == :new_return_value
592
+ @args.should == [:original_return_value]
593
+ end
594
+ end
595
+ end
596
+ end
597
+ end
598
+
599
+ describe "#ordered" do
600
+ macro "#ordered" do
601
+ it "adds itself to the ordered doubles list" do
602
+ definition.ordered
603
+ Space.instance.ordered_doubles.should include(double)
604
+ end
605
+
606
+ it "does not double_injection add itself" do
607
+ definition.ordered
608
+ Space.instance.ordered_doubles.should == [double]
609
+ end
610
+
611
+ it "sets ordered? to true" do
612
+ definition.should be_ordered
613
+ end
614
+
615
+ context "when there is no Double" do
616
+ it "raises a DoubleDefinitionError" do
617
+ definition.double = nil
618
+ lambda do
619
+ definition.ordered
620
+ end.should raise_error(
621
+ Errors::DoubleDefinitionError,
622
+ "Double Definitions must have a dedicated Double to be ordered. " <<
623
+ "For example, using instance_of does not allow ordered to be used. " <<
624
+ "proxy the class's #new method instead."
625
+ )
626
+ end
627
+ end
628
+ end
629
+
630
+ context "when not passed a block" do
631
+ before do
632
+ definition.with(1, 2).once.ordered
633
+ subject.foobar(1, 2)
634
+ end
635
+
636
+ send "#ordered"
637
+ end
638
+
639
+ context "when passed a block" do
640
+ def call_double_injection
641
+ actual_args = nil
642
+ definition.with(1, 2).once.ordered do |*args|
643
+ actual_args = args
644
+ :new_return_value
645
+ end
646
+ @return_value = subject.foobar(1, 2)
647
+ @args = actual_args
648
+ end
649
+
650
+ context "with returns block_callback_strategy" do
651
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
652
+ send "#ordered"
653
+
654
+ describe "#subject.method_name being called" do
655
+ it "returns the return value of the block" do
656
+ @return_value.should == :new_return_value
657
+ @args.should == [1, 2]
658
+ end
659
+ end
660
+ end
661
+
662
+ context "with after_call block_callback_strategy" do
663
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
664
+ send "#ordered"
665
+
666
+ describe "#subject.method_name being called" do
667
+ it "returns the return value of the block" do
668
+ @return_value.should == :new_return_value
669
+ @args.should == [:original_return_value]
670
+ end
671
+ end
672
+ end
673
+ end
674
+ end
675
+
676
+ describe "#yields" do
677
+ macro "#yields" do
678
+ it "returns DoubleDefinition" do
679
+ definition.yields(:baz).should === definition
680
+ end
681
+
682
+ context "when there is a no returns value set" do
683
+ it "yields the passed in argument to the call block" do
684
+ @passed_in_block_arg.should == :baz
685
+ end
686
+ end
687
+ end
688
+
689
+ context "when not passed a block" do
690
+ before do
691
+ definition.with(1, 2).once.yields(:baz)
692
+ passed_in_block_arg = nil
693
+ subject.foobar(1, 2) do |arg|
694
+ passed_in_block_arg = arg
695
+ end
696
+ @passed_in_block_arg = passed_in_block_arg
697
+ end
698
+
699
+ send "#yields"
700
+ end
701
+
702
+ context "when passed a block" do
703
+ def call_double_injection
704
+ actual_args = nil
705
+ definition.with(1, 2).once.yields(:baz) do |*args|
706
+ actual_args = args
707
+ :new_return_value
708
+ end
709
+ passed_in_block_arg = nil
710
+ @block = lambda do |arg|
711
+ passed_in_block_arg = arg
712
+ end
713
+ @return_value = subject.foobar(1, 2, &@block)
714
+ @passed_in_block_arg = passed_in_block_arg
715
+
716
+ @args = actual_args
717
+ end
718
+
719
+ context "with returns block_callback_strategy" do
720
+ send "DoubleDefinition where #double_definition_creator is a Reimplementation"
721
+ send "#yields"
722
+
723
+ describe "#subject.method_name being called" do
724
+ it "returns the return value of the block" do
725
+ @return_value.should == :new_return_value
726
+ end
727
+
728
+ it "passes an array of the args with the block appended as a ProcFromBlock around the original block" do
729
+ @args.length.should == 3
730
+ @args[0..1].should == [1, 2]
731
+ @args[2].should be_instance_of(ProcFromBlock)
732
+ @block.should == Proc.new(&@block)
733
+ @args[2].should == @block
734
+ end
735
+ end
736
+ end
737
+
738
+ context "with after_call block_callback_strategy" do
739
+ send "DoubleDefinition where #double_definition_creator is a Proxy"
740
+ send "#yields"
741
+
742
+ describe "#subject.method_name being called" do
743
+ it "returns the return value of the block" do
744
+ @return_value.should == :new_return_value
745
+ @args.should == [:original_return_value]
746
+ end
747
+ end
748
+ end
749
+ end
750
+ end
751
+
752
+ describe "#after_call" do
753
+ context "when passed a block" do
754
+ it "returns DoubleDefinition" do
755
+ definition.after_call {}.should === definition
756
+ end
757
+
758
+ describe "#subject.method_name being called" do
759
+ it "calls the block with the return value of the implementation" do
760
+ return_value = {:original => :value}
761
+ definition.with_any_args.returns(return_value).after_call do |value|
762
+ value[:foo] = :bar
763
+ value
764
+ end
765
+
766
+ actual_value = subject.foobar
767
+ actual_value.should === return_value
768
+ actual_value.should == {:original => :value, :foo => :bar}
769
+ end
770
+
771
+ context "when the return value of the #after_call_proc is a DoubleDefinition" do
772
+ it "returns the #subject of the DoubleDefinition" do
773
+ return_value = Object.new
774
+ inner_double_definition = nil
775
+ definition.with_any_args.returns(return_value).after_call do |value|
776
+ inner_double_definition = mock(value).inner_method(1) {:baz}
777
+ end
778
+
779
+ foobar_return_value = subject.foobar
780
+ foobar_return_value.should == inner_double_definition.subject
781
+ foobar_return_value.inner_method(1).should == :baz
782
+ end
783
+ end
784
+
785
+ context "when the return value of the #after_call_proc is a DoubleDefinitionCreatorProxy" do
786
+ it "returns the #__subject__ of the DoubleDefinitionCreatorProxy" do
787
+ return_value = Object.new
788
+ inner_double_proxy = nil
789
+ definition.with_any_args.returns(return_value).after_call do |value|
790
+ inner_double_proxy = mock(value)
791
+ end
792
+
793
+ foobar_return_value = subject.foobar
794
+ foobar_return_value.should == inner_double_proxy.__creator__.subject
795
+ end
796
+ end
797
+
798
+ context "when the return value of the #after_call_proc is an Object" do
799
+ it "returns the return value of the #after_call_proc" do
800
+ return_value = :returns_value
801
+ definition.with_any_args.returns(return_value).after_call do |value|
802
+ :after_call_proc
803
+ end
804
+
805
+ actual_value = subject.foobar
806
+ actual_value.should == :after_call_proc
807
+ end
808
+ end
809
+ end
810
+ end
811
+
812
+ context "when not passed a block" do
813
+ it "raises an ArgumentError" do
814
+ lambda do
815
+ definition.after_call
816
+ end.should raise_error(ArgumentError, "after_call expects a block")
817
+ end
818
+ end
819
+ end
820
+
821
+ describe "#returns" do
822
+ it "returns DoubleDefinition" do
823
+ definition.returns {:baz}.should === definition
824
+ definition.returns(:baz).should === definition
825
+ end
826
+
827
+ context "when passed neither an argument nor a block" do
828
+ describe "#subject.method_name being called" do
829
+ it "returns nil" do
830
+ definition.with_any_args.returns
831
+ subject.foobar.should be_nil
832
+ end
833
+ end
834
+ end
835
+
836
+ context "when passed a block" do
837
+ describe "#subject.method_name being called" do
838
+ it "returns the return value of the block" do
839
+ definition.with_any_args.returns {:baz}
840
+ subject.foobar.should == :baz
841
+ end
842
+ end
843
+ end
844
+
845
+ context "when passed an argument" do
846
+ describe "#subject.method_name being called" do
847
+ it "returns the passed-in argument" do
848
+ definition.returns(:baz).with_no_args
849
+ subject.foobar.should == :baz
850
+ end
851
+ end
852
+
853
+ context "when the argument is false" do
854
+ describe "#subject.method_name being called" do
855
+ it "returns false" do
856
+ definition.returns(false).with_any_args
857
+ subject.foobar.should == false
858
+ end
859
+ end
860
+ end
861
+ end
862
+
863
+ context "when passed both an argument and a block" do
864
+ it "raises an error" do
865
+ lambda do
866
+ definition.returns(:baz) {:another}
867
+ end.should raise_error(ArgumentError, "returns cannot accept both an argument and a block")
868
+ end
869
+ end
870
+ end
871
+
872
+ describe "#implemented_by" do
873
+ it "returns the DoubleDefinition" do
874
+ definition.implemented_by(lambda{:baz}).should === definition
875
+ end
876
+
877
+ context "when passed a Proc" do
878
+ describe "#subject.method_name being called" do
879
+ it "returns the return value of the passed-in Proc" do
880
+ definition.implemented_by(lambda{:baz}).with_no_args
881
+ subject.foobar.should == :baz
882
+ end
883
+ end
884
+ end
885
+
886
+ context "when passed a Method" do
887
+ it "sets the implementation to the passed in method" do
888
+ def subject.foobar(a, b)
889
+ [b, a]
890
+ end
891
+ definition.implemented_by(subject.method(:foobar))
892
+ subject.foobar(1, 2).should == [2, 1]
893
+ end
894
+ end
895
+ end
896
+
897
+ describe "#verbose" do
898
+ it "sets the verbose? to true" do
899
+ definition.should_not be_verbose
900
+ definition.verbose
901
+ definition.should be_verbose
902
+ end
903
+ end
904
+
905
+ describe "#verify_method_signature" do
906
+ it "sets #verify_method_signature? to true" do
907
+ definition.verify_method_signature?.should be_false
908
+ definition.verify_method_signature
909
+ definition.verify_method_signature?.should be_true
910
+ end
911
+
912
+ it "returns self" do
913
+ definition.verify_method_signature.should == definition
914
+ end
915
+ end
916
+ end
917
+
918
+ describe "NestedDoubleCreationMethods" do
919
+ attr_reader :child_double_definition_creator
920
+ after do
921
+ RR.verify(ChildDoubleDefinitionCreator)
922
+ RR.verify(child_double_definition_creator)
923
+ end
924
+
925
+ describe "#mock" do
926
+ it "instantiates a ChildDoubleDefinitionCreator with self and delegates to its #mock method with the passed-in arguments and block" do
927
+ child_subject = Object.new
928
+ child_double_definition_creator = nil
929
+ mock.proxy(ChildDoubleDefinitionCreator).new(definition) do |child_double_definition_creator|
930
+ mock.proxy(child_double_definition_creator).mock(child_subject, :baz)
931
+ child_double_definition_creator = child_double_definition_creator
932
+ end
933
+
934
+ definition.mock(child_subject, :baz)
935
+ @child_double_definition_creator = child_double_definition_creator
936
+ end
937
+ end
938
+
939
+ describe "#mock!" do
940
+ it "instantiates a ChildDoubleDefinitionCreator with self and delegates to its #mock! method with the passed-in arguments and block" do
941
+ child_double_definition_creator = nil
942
+ mock.proxy(ChildDoubleDefinitionCreator).new(definition) do |child_double_definition_creator|
943
+ mock.proxy(child_double_definition_creator).mock!(:baz)
944
+ child_double_definition_creator = child_double_definition_creator
945
+ end
946
+
947
+ definition.mock!(:baz)
948
+ @child_double_definition_creator = child_double_definition_creator
949
+ end
950
+ end
951
+
952
+ describe "#stub" do
953
+ it "instantiates a ChildDoubleDefinitionCreator with self and delegates to its #stub method with the passed-in arguments and block" do
954
+ child_subject = Object.new
955
+ child_double_definition_creator = nil
956
+ mock.proxy(ChildDoubleDefinitionCreator).new(definition) do |child_double_definition_creator|
957
+ mock.proxy(child_double_definition_creator).stub(child_subject, :baz)
958
+ child_double_definition_creator = child_double_definition_creator
959
+ end
960
+
961
+ definition.stub(child_subject, :baz)
962
+ @child_double_definition_creator = child_double_definition_creator
963
+ end
964
+ end
965
+
966
+ describe "#stub!" do
967
+ it "instantiates a ChildDoubleDefinitionCreator with self and delegates to its #stub! method with the passed-in arguments and block" do
968
+ child_double_definition_creator = nil
969
+ mock.proxy(ChildDoubleDefinitionCreator).new(definition) do |child_double_definition_creator|
970
+ mock.proxy(child_double_definition_creator).stub!(:baz)
971
+ child_double_definition_creator = child_double_definition_creator
972
+ end
973
+
974
+ definition.stub!(:baz)
975
+ @child_double_definition_creator = child_double_definition_creator
976
+ end
977
+ end
978
+
979
+ describe "#dont_allow" do
980
+ it "instantiates a ChildDoubleDefinitionCreator with self and delegates to its #dont_allow method with the passed-in arguments and block" do
981
+ child_subject = Object.new
982
+ child_double_definition_creator = nil
983
+ mock.proxy(ChildDoubleDefinitionCreator).new(definition) do |child_double_definition_creator|
984
+ mock.proxy(child_double_definition_creator).dont_allow(child_subject, :baz)
985
+ child_double_definition_creator = child_double_definition_creator
986
+ end
987
+
988
+ definition.dont_allow(child_subject, :baz)
989
+ @child_double_definition_creator = child_double_definition_creator
990
+ end
991
+ end
992
+
993
+ describe "#dont_allow!" do
994
+ it "instantiates a ChildDoubleDefinitionCreator with self and delegates to its #dont_allow! method with the passed-in arguments and block" do
995
+ child_double_definition_creator = nil
996
+ mock.proxy(ChildDoubleDefinitionCreator).new(definition) do |child_double_definition_creator|
997
+ mock.proxy(child_double_definition_creator).dont_allow!(:baz)
998
+ child_double_definition_creator = child_double_definition_creator
999
+ end
1000
+
1001
+ definition.dont_allow!(:baz)
1002
+ @child_double_definition_creator = child_double_definition_creator
1003
+ end
1004
+ end
1005
+
1006
+ describe "#proxy" do
1007
+ it "instantiates a ChildDoubleDefinitionCreator with self and delegates to its #proxy method with the passed-in arguments and block" do
1008
+ child_subject = Object.new
1009
+ child_double_definition_creator = nil
1010
+ mock.proxy(ChildDoubleDefinitionCreator).new(definition) do |child_double_definition_creator|
1011
+ mock.proxy(child_double_definition_creator).proxy(DoubleDefinitionCreator::NO_SUBJECT, nil)
1012
+ child_double_definition_creator = child_double_definition_creator
1013
+ end
1014
+
1015
+ definition.proxy.mock(child_subject)
1016
+ @child_double_definition_creator = child_double_definition_creator
1017
+ end
1018
+ end
1019
+
1020
+ describe "#proxy!" do
1021
+ it "raises a DoubleDefinitionError" do
1022
+ lambda do
1023
+ definition.proxy!(:baz)
1024
+ end.should raise_error(Errors::DoubleDefinitionError)
1025
+ end
1026
+ end
1027
+
1028
+ describe "#strong" do
1029
+ it "instantiates a ChildDoubleDefinitionCreator with self and delegates to its #strong method with the passed-in arguments and block" do
1030
+ child_subject = Object.new
1031
+ child_double_definition_creator = nil
1032
+ mock.proxy(ChildDoubleDefinitionCreator).new(definition) do |child_double_definition_creator|
1033
+ mock.proxy(child_double_definition_creator).strong(DoubleDefinitionCreator::NO_SUBJECT, nil)
1034
+ child_double_definition_creator = child_double_definition_creator
1035
+ end
1036
+
1037
+ definition.strong.mock(child_subject)
1038
+ @child_double_definition_creator = child_double_definition_creator
1039
+ end
1040
+ end
1041
+
1042
+ describe "#strong!" do
1043
+ it "raises a DoubleDefinitionError" do
1044
+ lambda do
1045
+ definition.strong!(:baz)
1046
+ end.should raise_error(Errors::DoubleDefinitionError)
1047
+ end
1048
+ end
1049
+ end
1050
+
1051
+ describe "StateQueryMethods" do
1052
+ describe "#ordered?" do
1053
+ it "defaults to false" do
1054
+ definition.should_not be_ordered
1055
+ end
1056
+ end
1057
+
1058
+ describe "#exact_match?" do
1059
+ context "when no argument_expectation set" do
1060
+ it "raises a DoubleDefinitionError" do
1061
+ definition.argument_expectation = nil
1062
+ lambda do
1063
+ definition.exact_match?
1064
+ end.should raise_error(Errors::DoubleDefinitionError)
1065
+ end
1066
+ end
1067
+
1068
+ context "when arguments are not an exact match" do
1069
+ it "returns false" do
1070
+ definition.with(1, 2, 3)
1071
+ definition.should_not be_exact_match(1, 2)
1072
+ definition.should_not be_exact_match(1)
1073
+ definition.should_not be_exact_match()
1074
+ definition.should_not be_exact_match("does not match")
1075
+ end
1076
+ end
1077
+
1078
+ context "when arguments are an exact match" do
1079
+ it "returns true" do
1080
+ definition.with(1, 2, 3)
1081
+ definition.should be_exact_match(1, 2, 3)
1082
+ end
1083
+ end
1084
+ end
1085
+
1086
+ describe "#wildcard_match?" do
1087
+ context "when no #argument_expectation is set" do
1088
+ it "raises a DoubleDefinitionError" do
1089
+ definition.argument_expectation = nil
1090
+ lambda do
1091
+ definition.wildcard_match?
1092
+ end.should raise_error(Errors::DoubleDefinitionError)
1093
+ end
1094
+ end
1095
+
1096
+ context "when arguments are an exact match" do
1097
+ it "returns true" do
1098
+ definition.with(1, 2, 3)
1099
+ definition.should be_wildcard_match(1, 2, 3)
1100
+ definition.should_not be_wildcard_match(1, 2)
1101
+ definition.should_not be_wildcard_match(1)
1102
+ definition.should_not be_wildcard_match()
1103
+ definition.should_not be_wildcard_match("does not match")
1104
+ end
1105
+ end
1106
+
1107
+ context "when with_any_args" do
1108
+ it "returns true" do
1109
+ definition.with_any_args
1110
+
1111
+ definition.should be_wildcard_match(1, 2, 3)
1112
+ definition.should be_wildcard_match(1, 2)
1113
+ definition.should be_wildcard_match(1)
1114
+ definition.should be_wildcard_match()
1115
+ definition.should be_wildcard_match("does not match")
1116
+ end
1117
+ end
1118
+ end
1119
+
1120
+ describe "#terminal?" do
1121
+ context "when times_matcher's terminal? is true" do
1122
+ it "returns true" do
1123
+ definition.once
1124
+ definition.times_matcher.should be_terminal
1125
+ definition.should be_terminal
1126
+ end
1127
+ end
1128
+
1129
+ context "when times_matcher's terminal? is false" do
1130
+ it "returns false" do
1131
+ definition.any_number_of_times
1132
+ definition.times_matcher.should_not be_terminal
1133
+ definition.should_not be_terminal
1134
+ end
1135
+ end
1136
+
1137
+ context "when there is not times_matcher" do
1138
+ it "raises a DoubleDefinitionError" do
1139
+ definition.times_matcher = nil
1140
+ lambda do
1141
+ definition.terminal?
1142
+ end.should raise_error(Errors::DoubleDefinitionError)
1143
+ end
1144
+ end
1145
+ end
1146
+
1147
+ describe "#expected_arguments" do
1148
+ context "when there is a argument expectation" do
1149
+ it "returns argument expectation's expected_arguments" do
1150
+ definition.with(1, 2)
1151
+ definition.expected_arguments.should == [1, 2]
1152
+ end
1153
+ end
1154
+
1155
+ context "when there is no argument expectation" do
1156
+ it "returns an empty array" do
1157
+ definition.argument_expectation = nil
1158
+ definition.expected_arguments.should == []
1159
+ end
1160
+ end
1161
+ end
1162
+ end
1163
+ end
1164
+ end
1165
+ end