rr 0.4.10 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,815 +0,0 @@
1
- require "spec/spec_helper"
2
-
3
- module RR
4
- describe DoubleDefinition, " with returns block_callback_strategy", :shared => true do
5
- before do
6
- @definition.returns_block_callback_strategy
7
- create_definition
8
- end
9
- end
10
-
11
- describe DoubleDefinition, " with after_call block_callback_strategy", :shared => true do
12
- before do
13
- @definition.proxy
14
- @definition.after_call_block_callback_strategy
15
- create_definition
16
- end
17
- end
18
-
19
- describe DoubleDefinition do
20
- it_should_behave_like "Swapped Space"
21
-
22
- before do
23
- @object = Object.new
24
- add_original_method
25
- @double_injection = Space.instance.double_injection(@object, :foobar)
26
- @double = Double.new(@double_injection)
27
- @definition = @double.definition
28
- end
29
-
30
- def add_original_method
31
- def @object.foobar(a, b)
32
- :original_return_value
33
- end
34
- end
35
-
36
- describe "#with" do
37
- class << self
38
- define_method "#with" do
39
- it "returns DoubleDefinition" do
40
- @definition.with(1).should === @definition
41
- end
42
-
43
- it "sets an ArgumentEqualityExpectation" do
44
- @definition.should be_exact_match(1, 2)
45
- @definition.should_not be_exact_match(2)
46
- end
47
- end
48
- end
49
-
50
- def create_definition
51
- actual_args = nil
52
- @definition.with(1, 2) do |*args|
53
- actual_args = args
54
- :new_return_value
55
- end
56
- @object.foobar(1, 2)
57
- @return_value = @object.foobar(1, 2)
58
- @args = actual_args
59
- end
60
-
61
- context "with returns block_callback_strategy" do
62
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
63
- send "#with"
64
-
65
- it "sets return value when block passed in" do
66
- @return_value.should == :new_return_value
67
- @args.should == [1, 2]
68
- end
69
- end
70
-
71
- context "with after_call block_callback_strategy" do
72
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
73
- send "#with"
74
-
75
- it "sets return value when block passed in" do
76
- @return_value.should == :new_return_value
77
- @args.should == [:original_return_value]
78
- end
79
- end
80
- end
81
-
82
- describe "#with_any_args" do
83
- class << self
84
- define_method "#with_any_args" do
85
- it "returns DoubleDefinition" do
86
- @definition.with_no_args.should === @definition
87
- end
88
-
89
- it "sets an AnyArgumentExpectation" do
90
- @definition.should_not be_exact_match(1)
91
- @definition.should be_wildcard_match(1)
92
- end
93
- end
94
- end
95
-
96
- def create_definition
97
- actual_args = nil
98
- @definition.with_any_args do |*args|
99
- actual_args = args
100
- :new_return_value
101
- end
102
- @return_value = @object.foobar(1, 2)
103
- @args = actual_args
104
- end
105
-
106
- describe "with returns block_callback_strategy" do
107
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
108
- send "#with_any_args"
109
-
110
- it "sets return value when block passed in" do
111
- @return_value.should == :new_return_value
112
- @args.should == [1, 2]
113
- end
114
- end
115
-
116
- describe "with after_call block_callback_strategy" do
117
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
118
- send "#with_any_args"
119
-
120
- it "sets return value when block passed in" do
121
- @return_value.should == :new_return_value
122
- @args.should == [:original_return_value]
123
- end
124
- end
125
- end
126
-
127
- describe "#with_no_args" do
128
- class << self
129
- define_method "#with_no_args" do
130
- it "returns DoubleDefinition" do
131
- @definition.with_no_args.should === @definition
132
- end
133
-
134
- it "sets an ArgumentEqualityExpectation with no arguments" do
135
- @definition.argument_expectation.should == Expectations::ArgumentEqualityExpectation.new()
136
- end
137
- end
138
- end
139
-
140
- def add_original_method
141
- def @object.foobar()
142
- :original_return_value
143
- end
144
- end
145
-
146
- def create_definition
147
- actual_args = nil
148
- @definition.with_no_args do |*args|
149
- actual_args = args
150
- :new_return_value
151
- end
152
- @return_value = @object.foobar
153
- @args = actual_args
154
- end
155
-
156
- context "with returns block_callback_strategy" do
157
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
158
- send "#with_no_args"
159
-
160
- it "sets return value when block passed in" do
161
- @return_value.should == :new_return_value
162
- @args.should == []
163
- end
164
- end
165
-
166
- context "with after_call block_callback_strategy" do
167
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
168
- send "#with_no_args"
169
-
170
- it "sets return value when block passed in" do
171
- @return_value.should == :new_return_value
172
- @args.should == [:original_return_value]
173
- end
174
- end
175
- end
176
-
177
- describe "#never" do
178
- it "returns DoubleDefinition" do
179
- @definition.never.should === @definition
180
- end
181
-
182
- it "sets up a Times Called Expectation with 0" do
183
- @definition.with_any_args
184
- @definition.never
185
- lambda {@object.foobar}.should raise_error(Errors::TimesCalledError)
186
- end
187
-
188
- it "sets return value when block passed in" do
189
- @definition.with_any_args.never
190
- lambda {@object.foobar}.should raise_error(Errors::TimesCalledError)
191
- end
192
- end
193
-
194
- describe "#once" do
195
- class << self
196
- define_method "#once" do
197
- it "returns DoubleDefinition" do
198
- @definition.once.should === @definition
199
- end
200
-
201
- it "sets up a Times Called Expectation with 1" do
202
- lambda {@object.foobar}.should raise_error(Errors::TimesCalledError)
203
- end
204
- end
205
- end
206
-
207
- def create_definition
208
- actual_args = nil
209
- @definition.with_any_args.once do |*args|
210
- actual_args = args
211
- :new_return_value
212
- end
213
- @return_value = @object.foobar(1, 2)
214
- @args = actual_args
215
- end
216
-
217
- context "with returns block_callback_strategy" do
218
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
219
- send "#once"
220
-
221
- it "sets return value when block passed in" do
222
- @return_value.should == :new_return_value
223
- @args.should == [1, 2]
224
- end
225
- end
226
-
227
- context "with after_call block_callback_strategy" do
228
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
229
- send "#once"
230
-
231
- it "sets return value when block passed in" do
232
- @return_value.should == :new_return_value
233
- @args.should == [:original_return_value]
234
- end
235
- end
236
- end
237
-
238
- describe "#twice" do
239
- class << self
240
- define_method "#twice" do
241
- it "returns DoubleDefinition" do
242
- @definition.twice.should === @definition
243
- end
244
-
245
- it "sets up a Times Called Expectation with 2" do
246
- @definition.twice.with_any_args
247
- lambda {@object.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
248
- end
249
- end
250
- end
251
-
252
- def create_definition
253
- actual_args = nil
254
- @definition.with_any_args.twice do |*args|
255
- actual_args = args
256
- :new_return_value
257
- end
258
- @object.foobar(1, 2)
259
- @return_value = @object.foobar(1, 2)
260
- @args = actual_args
261
- end
262
-
263
- context "with returns block_callback_strategy" do
264
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
265
- send "#twice"
266
-
267
- it "sets return value when block passed in" do
268
- @return_value.should == :new_return_value
269
- @args.should == [1, 2]
270
- end
271
- end
272
-
273
- context "with after_call block_callback_strategy" do
274
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
275
- send "#twice"
276
-
277
- it "sets return value when block passed in" do
278
- @return_value.should == :new_return_value
279
- @args.should == [:original_return_value]
280
- end
281
- end
282
- end
283
-
284
- describe "#at_least" do
285
- class << self
286
- define_method "#at_least" do
287
- it "returns DoubleDefinition" do
288
- @definition.with_any_args.at_least(2).should === @definition
289
- end
290
-
291
- it "sets up a Times Called Expectation with 1" do
292
- @definition.times_matcher.should == TimesCalledMatchers::AtLeastMatcher.new(2)
293
- end
294
- end
295
- end
296
-
297
- def create_definition
298
- actual_args = nil
299
- @definition.with_any_args.at_least(2) do |*args|
300
- actual_args = args
301
- :new_return_value
302
- end
303
- @object.foobar(1, 2)
304
- @return_value = @object.foobar(1, 2)
305
- @args = actual_args
306
- end
307
-
308
- context "with returns block_callback_strategy" do
309
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
310
- send "#at_least"
311
-
312
- it "sets return value when block passed in" do
313
- @return_value.should == :new_return_value
314
- @args.should == [1, 2]
315
- end
316
- end
317
-
318
- context "with after_call block_callback_strategy" do
319
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
320
- send "#at_least"
321
-
322
- it "sets return value when block passed in" do
323
- @return_value.should == :new_return_value
324
- @args.should == [:original_return_value]
325
- end
326
- end
327
- end
328
-
329
- describe "#at_most" do
330
- class << self
331
- define_method "#at_most" do
332
- it "returns DoubleDefinition" do
333
- @definition.with_any_args.at_most(2).should === @definition
334
- end
335
-
336
- it "sets up a Times Called Expectation with 1" do
337
- lambda do
338
- @object.foobar
339
- end.should raise_error(
340
- Errors::TimesCalledError,
341
- "foobar()\nCalled 3 times.\nExpected at most 2 times."
342
- )
343
- end
344
- end
345
- end
346
-
347
- def create_definition
348
- actual_args = nil
349
- @definition.with_any_args.at_most(2) do |*args|
350
- actual_args = args
351
- :new_return_value
352
- end
353
- @object.foobar(1, 2)
354
- @return_value = @object.foobar(1, 2)
355
- @args = actual_args
356
- end
357
-
358
- context "with returns block_callback_strategy" do
359
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
360
- send "#at_most"
361
-
362
- it "sets return value when block passed in" do
363
- @return_value.should == :new_return_value
364
- @args.should == [1, 2]
365
- end
366
- end
367
-
368
- context "with after_call block_callback_strategy" do
369
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
370
- send "#at_most"
371
-
372
- it "sets return value when block passed in" do
373
- @return_value.should == :new_return_value
374
- @args.should == [:original_return_value]
375
- end
376
- end
377
- end
378
-
379
- describe "#times" do
380
- class << self
381
- define_method "#times" do
382
- it "returns DoubleDefinition" do
383
- @definition.times(3).should === @definition
384
- end
385
-
386
- it "sets up a Times Called Expectation with passed in times" do
387
- lambda {@object.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
388
- end
389
- end
390
- end
391
-
392
- def create_definition
393
- actual_args = nil
394
- @definition.with(1, 2).times(3) do |*args|
395
- actual_args = args
396
- :new_return_value
397
- end
398
- @object.foobar(1, 2)
399
- @object.foobar(1, 2)
400
- @return_value = @object.foobar(1, 2)
401
- @args = actual_args
402
- end
403
-
404
- context "with returns block_callback_strategy" do
405
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
406
- send "#times"
407
-
408
- it "sets return value when block passed in" do
409
- @return_value.should == :new_return_value
410
- @args.should == [1, 2]
411
- end
412
- end
413
-
414
- context "with after_call block_callback_strategy" do
415
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
416
- send "#times"
417
-
418
- it "sets return value when block passed in" do
419
- @return_value.should == :new_return_value
420
- @args.should == [:original_return_value]
421
- end
422
- end
423
- end
424
-
425
- describe "#any_number_of_times" do
426
- class << self
427
- define_method "#any_number_of_times" do
428
- it "returns DoubleDefinition" do
429
- @definition.any_number_of_times.should === @definition
430
- end
431
-
432
- it "sets up a Times Called Expectation with AnyTimes matcher" do
433
- @definition.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
434
- end
435
- end
436
- end
437
-
438
- def create_definition
439
- actual_args = nil
440
- @definition.with(1, 2).any_number_of_times do |*args|
441
- actual_args = args
442
- :new_return_value
443
- end
444
- @object.foobar(1, 2)
445
- @return_value = @object.foobar(1, 2)
446
- @args = actual_args
447
- end
448
-
449
- context "with returns block_callback_strategy" do
450
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
451
- send "#any_number_of_times"
452
-
453
- it "sets return value when block passed in" do
454
- @return_value.should == :new_return_value
455
- @args.should == [1, 2]
456
- end
457
- end
458
-
459
- context "with after_call block_callback_strategy" do
460
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
461
- send "#any_number_of_times"
462
-
463
- it "sets return value when block passed in" do
464
- @return_value.should == :new_return_value
465
- @args.should == [:original_return_value]
466
- end
467
- end
468
- end
469
-
470
- describe "#ordered" do
471
- class << self
472
- define_method "#ordered" do
473
- it "adds itself to the ordered doubles list" do
474
- @definition.ordered
475
- Space.instance.ordered_doubles.should include(@double)
476
- end
477
-
478
- it "does not double_injection add itself" do
479
- @definition.ordered
480
- Space.instance.ordered_doubles.should == [@double]
481
- end
482
-
483
- it "sets ordered? to true" do
484
- @definition.should be_ordered
485
- end
486
-
487
- it "raises error when there is no Double" do
488
- @definition.double = nil
489
- lambda do
490
- @definition.ordered
491
- end.should raise_error(
492
- Errors::DoubleDefinitionError,
493
- "Double Definitions must have a dedicated Double to be ordered. " <<
494
- "For example, using instance_of does not allow ordered to be used. " <<
495
- "proxy the class's #new method instead."
496
- )
497
- end
498
- end
499
- end
500
-
501
- def create_definition
502
- actual_args = nil
503
- @definition.with(1, 2).once.ordered do |*args|
504
- actual_args = args
505
- :new_return_value
506
- end
507
- @return_value = @object.foobar(1, 2)
508
- @args = actual_args
509
- end
510
-
511
- context "with returns block_callback_strategy" do
512
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
513
- send "#ordered"
514
-
515
- it "sets return value when block passed in" do
516
- @return_value.should == :new_return_value
517
- @args.should == [1, 2]
518
- end
519
- end
520
-
521
- context "with after_call block_callback_strategy" do
522
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
523
- send "#ordered"
524
-
525
- it "sets return value when block passed in" do
526
- @return_value.should == :new_return_value
527
- @args.should == [:original_return_value]
528
- end
529
- end
530
- end
531
-
532
- describe "#ordered?" do
533
- it "defaults to false" do
534
- @definition.should_not be_ordered
535
- end
536
- end
537
-
538
- describe "#yields" do
539
- class << self
540
- define_method "#yields" do
541
- it "returns DoubleDefinition" do
542
- @definition.yields(:baz).should === @definition
543
- end
544
-
545
- it "yields the passed in argument to the call block when there is a no returns value set" do
546
- @passed_in_block_arg.should == :baz
547
- end
548
- end
549
- end
550
-
551
- def create_definition
552
- actual_args = nil
553
- @definition.with(1, 2).once.yields(:baz) do |*args|
554
- actual_args = args
555
- :new_return_value
556
- end
557
- passed_in_block_arg = nil
558
- @return_value = @object.foobar(1, 2) do |arg|
559
- passed_in_block_arg = arg
560
- end
561
- @passed_in_block_arg = passed_in_block_arg
562
-
563
- @args = actual_args
564
- end
565
-
566
- context "with returns block_callback_strategy" do
567
- it_should_behave_like "RR::DoubleDefinition with returns block_callback_strategy"
568
- send "#yields"
569
-
570
- it "sets return value when block passed in" do
571
- @return_value.should == :new_return_value
572
- @args.length.should == 3
573
- @args[0..1].should == [1, 2]
574
- @args[2].should be_instance_of(Proc)
575
- end
576
- end
577
-
578
- context "with after_call block_callback_strategy" do
579
- it_should_behave_like "RR::DoubleDefinition with after_call block_callback_strategy"
580
- send "#yields"
581
-
582
- it "sets return value when block passed in" do
583
- @return_value.should == :new_return_value
584
- @args.should == [:original_return_value]
585
- end
586
- end
587
- end
588
-
589
- describe "#after_call" do
590
- it "returns DoubleDefinition" do
591
- @definition.after_call {}.should === @definition
592
- end
593
-
594
- it "sends return value of Double implementation to after_call" do
595
- return_value = {}
596
- @definition.with_any_args.returns(return_value).after_call do |value|
597
- value[:foo] = :bar
598
- value
599
- end
600
-
601
- actual_value = @object.foobar
602
- actual_value.should === return_value
603
- actual_value.should == {:foo => :bar}
604
- end
605
-
606
- it "receives the return value in the after_call callback" do
607
- return_value = :returns_value
608
- @definition.with_any_args.returns(return_value).after_call do |value|
609
- :after_call_value
610
- end
611
-
612
- actual_value = @object.foobar
613
- actual_value.should == :after_call_value
614
- end
615
-
616
- it "allows after_call to mock the return value" do
617
- return_value = Object.new
618
- @definition.with_any_args.returns(return_value).after_call do |value|
619
- mock(value).inner_method(1) {:baz}
620
- value
621
- end
622
-
623
- @object.foobar.inner_method(1).should == :baz
624
- end
625
-
626
- it "raises an error when not passed a block" do
627
- lambda do
628
- @definition.after_call
629
- end.should raise_error(ArgumentError, "after_call expects a block")
630
- end
631
- end
632
-
633
- describe "#returns" do
634
- it "returns DoubleDefinition" do
635
- @definition.returns {:baz}.should === @definition
636
- @definition.returns(:baz).should === @definition
637
- end
638
-
639
- it "sets the value of the method when passed a block" do
640
- @definition.with_any_args.returns {:baz}
641
- @object.foobar.should == :baz
642
- end
643
-
644
- it "sets the value of the method when passed an argument" do
645
- @definition.returns(:baz).with_no_args
646
- @object.foobar.should == :baz
647
- end
648
-
649
- it "returns false when passed false" do
650
- @definition.returns(false).with_any_args
651
- @object.foobar.should == false
652
- end
653
-
654
- it "raises an error when both argument and block is passed in" do
655
- lambda do
656
- @definition.returns(:baz) {:another}
657
- end.should raise_error(ArgumentError, "returns cannot accept both an argument and a block")
658
- end
659
- end
660
-
661
- describe "#implemented_by" do
662
- it "returns the DoubleDefinition" do
663
- @definition.implemented_by(lambda{:baz}).should === @definition
664
- end
665
-
666
- it "sets the implementation to the passed in lambda" do
667
- @definition.implemented_by(lambda{:baz}).with_no_args
668
- @object.foobar.should == :baz
669
- end
670
-
671
- it "sets the implementation to the passed in method" do
672
- def @object.foobar(a, b)
673
- [b, a]
674
- end
675
- @definition.implemented_by(@object.method(:foobar))
676
- @object.foobar(1, 2).should == [2, 1]
677
- end
678
- end
679
-
680
- describe "#proxy" do
681
- it "returns the DoubleDefinition object" do
682
- @definition.proxy.should === @definition
683
- end
684
-
685
- it "sets the implementation to the original method" do
686
- @definition.proxy.with_any_args
687
- @object.foobar(1, 2).should == :original_return_value
688
- end
689
-
690
- it "calls method_missing when original_method does not exist" do
691
- class << @object
692
- def method_missing(method_name, *args, &block)
693
- "method_missing for #{method_name}(#{args.inspect})"
694
- end
695
- end
696
- double_injection = Space.instance.double_injection(@object, :does_not_exist)
697
- double = Double.new(double_injection)
698
- double.with_any_args
699
- double.proxy
700
-
701
- return_value = @object.does_not_exist(1, 2)
702
- return_value.should == "method_missing for does_not_exist([1, 2])"
703
- end
704
- end
705
-
706
- describe "#exact_match?" do
707
- it "returns false when no expectation set" do
708
- @definition.should_not be_exact_match()
709
- @definition.should_not be_exact_match(nil)
710
- @definition.should_not be_exact_match(Object.new)
711
- @definition.should_not be_exact_match(1, 2, 3)
712
- end
713
-
714
- it "returns false when arguments are not an exact match" do
715
- @definition.with(1, 2, 3)
716
- @definition.should_not be_exact_match(1, 2)
717
- @definition.should_not be_exact_match(1)
718
- @definition.should_not be_exact_match()
719
- @definition.should_not be_exact_match("does not match")
720
- end
721
-
722
- it "returns true when arguments are an exact match" do
723
- @definition.with(1, 2, 3)
724
- @definition.should be_exact_match(1, 2, 3)
725
- end
726
- end
727
-
728
- describe "#wildcard_match?" do
729
- it "returns false when no expectation set" do
730
- @definition.should_not be_wildcard_match()
731
- @definition.should_not be_wildcard_match(nil)
732
- @definition.should_not be_wildcard_match(Object.new)
733
- @definition.should_not be_wildcard_match(1, 2, 3)
734
- end
735
-
736
- it "returns true when arguments are an exact match" do
737
- @definition.with(1, 2, 3)
738
- @definition.should be_wildcard_match(1, 2, 3)
739
- @definition.should_not be_wildcard_match(1, 2)
740
- @definition.should_not be_wildcard_match(1)
741
- @definition.should_not be_wildcard_match()
742
- @definition.should_not be_wildcard_match("does not match")
743
- end
744
-
745
- it "returns true when with_any_args" do
746
- @definition.with_any_args
747
-
748
- @definition.should be_wildcard_match(1, 2, 3)
749
- @definition.should be_wildcard_match(1, 2)
750
- @definition.should be_wildcard_match(1)
751
- @definition.should be_wildcard_match()
752
- @definition.should be_wildcard_match("does not match")
753
- end
754
- end
755
-
756
- describe "#terminal?" do
757
- it "returns true when times_matcher's terminal? is true" do
758
- @definition.once
759
- @definition.times_matcher.should be_terminal
760
- @definition.should be_terminal
761
- end
762
-
763
- it "returns false when times_matcher's terminal? is false" do
764
- @definition.any_number_of_times
765
- @definition.times_matcher.should_not be_terminal
766
- @definition.should_not be_terminal
767
- end
768
-
769
- it "returns false when there is not times_matcher" do
770
- @definition.times_matcher.should be_nil
771
- @definition.should_not be_terminal
772
- end
773
- end
774
-
775
- describe "#expected_arguments" do
776
- it "returns argument expectation's expected_arguments when there is a argument expectation" do
777
- @definition.with(1, 2)
778
- @definition.expected_arguments.should == [1, 2]
779
- end
780
-
781
- it "returns an empty array when there is no argument expectation" do
782
- @definition.argument_expectation.should be_nil
783
- @definition.expected_arguments.should == []
784
- end
785
- end
786
-
787
- describe "#block_callback_strategy" do
788
- it "defaults to :returns" do
789
- @definition.block_callback_strategy.should == :returns
790
- end
791
- end
792
-
793
- describe "#returns_block_callback_strategy!" do
794
- it "sets the block_callback_strategy to :returns" do
795
- @definition.returns_block_callback_strategy
796
- @definition.block_callback_strategy.should == :returns
797
- end
798
- end
799
-
800
- describe "#after_call_block_callback_strategy!" do
801
- it "sets the block_callback_strategy to :after_call" do
802
- @definition.after_call_block_callback_strategy
803
- @definition.block_callback_strategy.should == :after_call
804
- end
805
- end
806
-
807
- describe "#verbose" do
808
- it "sets the verbose? to true" do
809
- @definition.should_not be_verbose
810
- @definition.verbose
811
- @definition.should be_verbose
812
- end
813
- end
814
- end
815
- end