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