bourne 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,526 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'method_definer'
3
+ require 'bourne/expectation'
4
+ require 'mocha/sequence'
5
+ require 'bourne/mockery'
6
+ require 'execution_point'
7
+ require 'simple_counter'
8
+
9
+ class ExpectationTest < Test::Unit::TestCase
10
+
11
+ include Mocha
12
+
13
+ class FakeMockery
14
+ attr_reader :invocations
15
+
16
+ def initialize
17
+ @invocations = []
18
+ end
19
+
20
+ def invocation(mock, method_name, args)
21
+ @invocations << { :mock => mock, :method_name => method_name, :args => args }
22
+ end
23
+ end
24
+
25
+ def setup
26
+ Mockery.instance_variable_set('@instance', FakeMockery.new)
27
+ end
28
+
29
+ def teardown
30
+ Mockery.reset_instance
31
+ end
32
+
33
+ def new_expectation
34
+ Expectation.new(nil, :expected_method)
35
+ end
36
+
37
+ def test_should_match_calls_to_same_method_with_any_parameters
38
+ assert new_expectation.match?(:expected_method, 1, 2, 3)
39
+ end
40
+
41
+ def test_should_match_calls_to_same_method_with_exactly_zero_parameters
42
+ expectation = new_expectation.with()
43
+ assert expectation.match?(:expected_method)
44
+ end
45
+
46
+ def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
47
+ expectation = new_expectation.with()
48
+ assert !expectation.match?(:expected_method, 1, 2, 3)
49
+ end
50
+
51
+ def test_should_match_calls_to_same_method_with_expected_parameter_values
52
+ expectation = new_expectation.with(1, 2, 3)
53
+ assert expectation.match?(:expected_method, 1, 2, 3)
54
+ end
55
+
56
+ def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
57
+ expectation = new_expectation.with() {|x, y, z| x + y == z}
58
+ assert expectation.match?(:expected_method, 1, 2, 3)
59
+ end
60
+
61
+ def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
62
+ expectation = new_expectation.with() {|x, y, z| x + y == z}
63
+ assert !expectation.match?(:different_method, 1, 2, 3)
64
+ end
65
+
66
+ def test_should_not_match_calls_to_different_methods_with_no_parameters
67
+ assert !new_expectation.match?(:unexpected_method)
68
+ end
69
+
70
+ def test_should_not_match_calls_to_same_method_with_too_few_parameters
71
+ expectation = new_expectation.with(1, 2, 3)
72
+ assert !expectation.match?(:unexpected_method, 1, 2)
73
+ end
74
+
75
+ def test_should_not_match_calls_to_same_method_with_too_many_parameters
76
+ expectation = new_expectation.with(1, 2)
77
+ assert !expectation.match?(:unexpected_method, 1, 2, 3)
78
+ end
79
+
80
+ def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
81
+ expectation = new_expectation.with(1, 2, 3)
82
+ assert !expectation.match?(:unexpected_method, 1, 0, 3)
83
+ end
84
+
85
+ def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
86
+ expectation = new_expectation.with() {|x, y, z| x + y == z}
87
+ assert !expectation.match?(:expected_method, 1, 0, 3)
88
+ end
89
+
90
+ def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
91
+ expectation = new_expectation.times(1)
92
+ assert expectation.invocations_allowed?
93
+ expectation.invoke([])
94
+ assert !expectation.invocations_allowed?
95
+ end
96
+
97
+ def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
98
+ expectation = new_expectation.times(2)
99
+ assert expectation.invocations_allowed?
100
+ expectation.invoke([])
101
+ assert expectation.invocations_allowed?
102
+ expectation.invoke([])
103
+ assert !expectation.invocations_allowed?
104
+ end
105
+
106
+ def test_should_allow_invocations_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
107
+ expectation = new_expectation.times(2..3)
108
+ assert expectation.invocations_allowed?
109
+ expectation.invoke([])
110
+ assert expectation.invocations_allowed?
111
+ expectation.invoke([])
112
+ assert expectation.invocations_allowed?
113
+ expectation.invoke([])
114
+ assert !expectation.invocations_allowed?
115
+ end
116
+
117
+ def test_should_store_provided_backtrace
118
+ backtrace = Object.new
119
+ expectation = Expectation.new(nil, :expected_method, backtrace)
120
+ assert_equal backtrace, expectation.backtrace
121
+ end
122
+
123
+ def test_should_default_backtrace_to_caller
124
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
125
+ assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
126
+ end
127
+
128
+ def test_should_not_yield
129
+ yielded = false
130
+ new_expectation.invoke([]) { yielded = true }
131
+ assert_equal false, yielded
132
+ end
133
+
134
+ def test_should_yield_no_parameters
135
+ expectation = new_expectation().yields()
136
+ yielded_parameters = nil
137
+ expectation.invoke([]) { |*parameters| yielded_parameters = parameters }
138
+ assert_equal Array.new, yielded_parameters
139
+ end
140
+
141
+ def test_should_yield_with_specified_parameters
142
+ expectation = new_expectation().yields(1, 2, 3)
143
+ yielded_parameters = nil
144
+ expectation.invoke([]) { |*parameters| yielded_parameters = parameters }
145
+ assert_equal [1, 2, 3], yielded_parameters
146
+ end
147
+
148
+ def test_should_yield_different_parameters_on_consecutive_invocations
149
+ expectation = new_expectation().yields(1, 2, 3).yields(4, 5)
150
+ yielded_parameters = []
151
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
152
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
153
+ assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
154
+ end
155
+
156
+ def test_should_yield_multiple_times_for_single_invocation
157
+ expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5])
158
+ yielded_parameters = []
159
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
160
+ assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
161
+ end
162
+
163
+ def test_should_yield_multiple_times_for_first_invocation_and_once_for_second_invocation
164
+ expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5]).then.yields(6, 7)
165
+ yielded_parameters = []
166
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
167
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
168
+ assert_equal [[1, 2, 3], [4, 5], [6, 7]], yielded_parameters
169
+ end
170
+
171
+ def test_should_return_specified_value
172
+ expectation = new_expectation.returns(99)
173
+ assert_equal 99, expectation.invoke([])
174
+ end
175
+
176
+ def test_should_return_same_specified_value_multiple_times
177
+ expectation = new_expectation.returns(99)
178
+ assert_equal 99, expectation.invoke([])
179
+ assert_equal 99, expectation.invoke([])
180
+ end
181
+
182
+ def test_should_return_specified_values_on_consecutive_calls
183
+ expectation = new_expectation.returns(99, 100, 101)
184
+ assert_equal 99, expectation.invoke([])
185
+ assert_equal 100, expectation.invoke([])
186
+ assert_equal 101, expectation.invoke([])
187
+ end
188
+
189
+ def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
190
+ values = [99, 100, 101]
191
+ expectation = new_expectation.returns(*values)
192
+ values.shift
193
+ assert_equal 99, expectation.invoke([])
194
+ assert_equal 100, expectation.invoke([])
195
+ assert_equal 101, expectation.invoke([])
196
+ end
197
+
198
+ def test_should_return_nil_by_default
199
+ assert_nil new_expectation.invoke([])
200
+ end
201
+
202
+ def test_should_return_nil_if_no_value_specified
203
+ expectation = new_expectation.returns()
204
+ assert_nil expectation.invoke([])
205
+ end
206
+
207
+ def test_should_raise_runtime_exception
208
+ expectation = new_expectation.raises
209
+ assert_raise(RuntimeError) { expectation.invoke([]) }
210
+ end
211
+
212
+ def test_should_raise_custom_exception
213
+ exception = Class.new(Exception)
214
+ expectation = new_expectation.raises(exception)
215
+ assert_raise(exception) { expectation.invoke([]) }
216
+ end
217
+
218
+ def test_should_raise_same_instance_of_custom_exception
219
+ exception_klass = Class.new(StandardError)
220
+ expected_exception = exception_klass.new
221
+ expectation = new_expectation.raises(expected_exception)
222
+ actual_exception = assert_raise(exception_klass) { expectation.invoke([]) }
223
+ assert_same expected_exception, actual_exception
224
+ end
225
+
226
+ def test_should_use_the_default_exception_message
227
+ expectation = new_expectation.raises(Exception)
228
+ exception = assert_raise(Exception) { expectation.invoke([]) }
229
+ assert_equal Exception.new.message, exception.message
230
+ end
231
+
232
+ def test_should_raise_custom_exception_with_message
233
+ exception_msg = "exception message"
234
+ expectation = new_expectation.raises(Exception, exception_msg)
235
+ exception = assert_raise(Exception) { expectation.invoke([]) }
236
+ assert_equal exception_msg, exception.message
237
+ end
238
+
239
+ def test_should_return_values_then_raise_exception
240
+ expectation = new_expectation.returns(1, 2).then.raises()
241
+ assert_equal 1, expectation.invoke([])
242
+ assert_equal 2, expectation.invoke([])
243
+ assert_raise(RuntimeError) { expectation.invoke([]) }
244
+ end
245
+
246
+ def test_should_raise_exception_then_return_values
247
+ expectation = new_expectation.raises().then.returns(1, 2)
248
+ assert_raise(RuntimeError) { expectation.invoke([]) }
249
+ assert_equal 1, expectation.invoke([])
250
+ assert_equal 2, expectation.invoke([])
251
+ end
252
+
253
+ def test_should_verify_successfully_if_expected_call_was_made
254
+ expectation = new_expectation
255
+ expectation.invoke([])
256
+ assert expectation.verified?
257
+ end
258
+
259
+ def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
260
+ expectation = new_expectation.once
261
+ expectation.invoke([])
262
+ expectation.invoke([])
263
+ assert !expectation.verified?
264
+ end
265
+
266
+ def test_should_not_verify_successfully_if_call_expected_once_but_not_invoked
267
+ expectation = new_expectation.once
268
+ assert !expectation.verified?
269
+ end
270
+
271
+ def test_should_verify_successfully_if_call_expected_once_and_invoked_once
272
+ expectation = new_expectation.once
273
+ expectation.invoke([])
274
+ assert expectation.verified?
275
+ end
276
+
277
+ def test_should_not_verify_successfully_if_call_expected_twice_and_invoked_three_times
278
+ expectation = new_expectation.twice
279
+ expectation.invoke([])
280
+ expectation.invoke([])
281
+ expectation.invoke([])
282
+ assert !expectation.verified?
283
+ end
284
+
285
+ def test_should_not_verify_successfully_if_call_expected_twice_but_invoked_once
286
+ expectation = new_expectation.twice
287
+ expectation.invoke([])
288
+ assert !expectation.verified?
289
+ end
290
+
291
+ def test_should_verify_successfully_if_call_expected_twice_and_invoked_twice
292
+ expectation = new_expectation.twice
293
+ expectation.invoke([])
294
+ expectation.invoke([])
295
+ assert expectation.verified?
296
+ end
297
+
298
+ def test_should_verify_successfully_if_expected_call_was_made_at_least_once
299
+ expectation = new_expectation.at_least_once
300
+ 3.times {expectation.invoke([])}
301
+ assert expectation.verified?
302
+ end
303
+
304
+ def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
305
+ expectation = new_expectation.with(1, 2, 3).at_least_once
306
+ assert !expectation.verified?
307
+ assert_match(/expected at least once, not yet invoked/i, expectation.mocha_inspect)
308
+ end
309
+
310
+ def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
311
+ expectation = new_expectation.times(2)
312
+ 2.times {expectation.invoke([])}
313
+ assert expectation.verified?
314
+ end
315
+
316
+ def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
317
+ expectation = new_expectation.times(2)
318
+ 1.times {expectation.invoke([])}
319
+ assert !expectation.verified?
320
+ assert_match(/expected exactly twice, already invoked once/i, expectation.mocha_inspect)
321
+ end
322
+
323
+ def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
324
+ expectation = new_expectation.times(2)
325
+ 3.times {expectation.invoke([])}
326
+ assert !expectation.verified?
327
+ end
328
+
329
+ def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
330
+ expectation = new_expectation
331
+ expectation.invoke([])
332
+ assertion_counter = SimpleCounter.new
333
+ expectation.verified?(assertion_counter)
334
+ assert_equal 1, assertion_counter.count
335
+ end
336
+
337
+ def test_should_not_increment_assertion_counter_for_stub_because_it_does_not_need_verifying
338
+ stub = Expectation.new(nil, :expected_method).at_least(0)
339
+ assertion_counter = SimpleCounter.new
340
+ stub.verified?(assertion_counter)
341
+ assert_equal 0, assertion_counter.count
342
+ end
343
+
344
+ def test_should_store_backtrace_from_point_where_expectation_was_created
345
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
346
+ assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
347
+ end
348
+
349
+ class FakeMock
350
+
351
+ def initialize(name)
352
+ @name = name
353
+ end
354
+
355
+ def mocha_inspect
356
+ @name
357
+ end
358
+
359
+ end
360
+
361
+ def test_should_raise_error_with_message_indicating_which_method_was_expected_to_be_called_on_which_mock_object_with_which_parameters_and_in_what_sequences
362
+ mock = FakeMock.new('mock')
363
+ sequence_one = Sequence.new('one')
364
+ sequence_two = Sequence.new('two')
365
+ expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
366
+ assert !expectation.verified?
367
+ assert_match "mock.expected_method(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", expectation.mocha_inspect
368
+ end
369
+
370
+ class FakeConstraint
371
+
372
+ def initialize(allows_invocation_now)
373
+ @allows_invocation_now = allows_invocation_now
374
+ end
375
+
376
+ def allows_invocation_now?
377
+ @allows_invocation_now
378
+ end
379
+
380
+ end
381
+
382
+ def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation_now
383
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
384
+ constraint_two = FakeConstraint.new(allows_invocation_now = true)
385
+ expectation = Expectation.new(nil, :method_one)
386
+ expectation.add_ordering_constraint(constraint_one)
387
+ expectation.add_ordering_constraint(constraint_two)
388
+ assert expectation.in_correct_order?
389
+ end
390
+
391
+ def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allow_invocation_now
392
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
393
+ constraint_two = FakeConstraint.new(allows_invocation_now = false)
394
+ expectation = Expectation.new(nil, :method_one)
395
+ expectation.add_ordering_constraint(constraint_one)
396
+ expectation.add_ordering_constraint(constraint_two)
397
+ assert !expectation.in_correct_order?
398
+ end
399
+
400
+ def test_should_match_if_all_ordering_constraints_allow_invocation_now
401
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
402
+ constraint_two = FakeConstraint.new(allows_invocation_now = true)
403
+ expectation = Expectation.new(nil, :method_one)
404
+ expectation.add_ordering_constraint(constraint_one)
405
+ expectation.add_ordering_constraint(constraint_two)
406
+ assert expectation.match?(:method_one)
407
+ end
408
+
409
+ def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
410
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
411
+ constraint_two = FakeConstraint.new(allows_invocation_now = false)
412
+ expectation = Expectation.new(nil, :method_one)
413
+ expectation.add_ordering_constraint(constraint_one)
414
+ expectation.add_ordering_constraint(constraint_two)
415
+ assert !expectation.match?(:method_one)
416
+ end
417
+
418
+ def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
419
+ expectation = Expectation.new(nil, :method_one).times(1)
420
+ assert !expectation.satisfied?
421
+ end
422
+
423
+ def test_should_be_satisfied_when_required_invocation_has_been_made
424
+ expectation = Expectation.new(nil, :method_one).times(1)
425
+ expectation.invoke([])
426
+ assert expectation.satisfied?
427
+ end
428
+
429
+ def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
430
+ expectation = Expectation.new(nil, :method_one).at_least(2)
431
+ expectation.invoke([])
432
+ assert !expectation.satisfied?
433
+ end
434
+
435
+ def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
436
+ expectation = Expectation.new(nil, :method_one).at_least(2)
437
+ 2.times { expectation.invoke([]) }
438
+ assert expectation.satisfied?
439
+ end
440
+
441
+ class FakeSequence
442
+
443
+ attr_reader :expectations
444
+
445
+ def initialize
446
+ @expectations = []
447
+ end
448
+
449
+ def constrain_as_next_in_sequence(expectation)
450
+ @expectations << expectation
451
+ end
452
+
453
+ end
454
+
455
+ def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
456
+ sequence_one = FakeSequence.new
457
+ sequence_two = FakeSequence.new
458
+ expectation = Expectation.new(nil, :method_one)
459
+ assert_equal expectation, expectation.in_sequence(sequence_one, sequence_two)
460
+ assert_equal [expectation], sequence_one.expectations
461
+ assert_equal [expectation], sequence_two.expectations
462
+ end
463
+
464
+ class FakeState
465
+
466
+ def initialize
467
+ @active = false
468
+ end
469
+
470
+ def activate
471
+ @active = true
472
+ end
473
+
474
+ def active?
475
+ @active
476
+ end
477
+
478
+ end
479
+
480
+ def test_should_change_state_when_expectation_is_invoked
481
+ state = FakeState.new
482
+ expectation = Expectation.new(nil, :method_one)
483
+
484
+ expectation.then(state)
485
+
486
+ expectation.invoke([])
487
+ assert state.active?
488
+ end
489
+
490
+ def test_should_match_when_state_is_active
491
+ state = FakeState.new
492
+ expectation = Expectation.new(nil, :method_one)
493
+
494
+ expectation.when(state)
495
+ assert !expectation.match?(:method_one)
496
+
497
+ state.activate
498
+ assert expectation.match?(:method_one)
499
+ end
500
+
501
+ def test_should_record_invocation
502
+ Mockery.instance.invocations.clear
503
+
504
+ mock = 'a mock'
505
+ method = :call_me
506
+ args = [1, 2, 3]
507
+ expectation = Expectation.new(mock, method)
508
+
509
+ expectation.invoke(args)
510
+ assert_equal 1, Mockery.instance.invocations.size
511
+ invocation = Mockery.instance.invocations.first
512
+ assert_equal method, invocation[:method_name], "Got: #{invocation.inspect}"
513
+ assert_equal args, invocation[:args], "Got: #{invocation.inspect}"
514
+ assert_equal mock, invocation[:mock], "Got: #{invocation.inspect}"
515
+ end
516
+
517
+ def test_should_expose_invocation_count
518
+ expectation = Expectation.new(nil, :a_method)
519
+ assert_equal 0, expectation.invocation_count
520
+ expectation.invoke(1)
521
+ assert_equal 1, expectation.invocation_count
522
+ expectation.invocation_count = 3
523
+ assert_equal 3, expectation.invocation_count
524
+ end
525
+
526
+ end