minitest 5.11.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,874 @@
1
+ require "minitest/autorun"
2
+
3
+ class TestMinitestMock < Minitest::Test
4
+ parallelize_me!
5
+
6
+ def setup
7
+ @mock = Minitest::Mock.new.expect(:foo, nil)
8
+ @mock.expect(:meaning_of_life, 42)
9
+ end
10
+
11
+ def test_create_stub_method
12
+ assert_nil @mock.foo
13
+ end
14
+
15
+ def test_allow_return_value_specification
16
+ assert_equal 42, @mock.meaning_of_life
17
+ end
18
+
19
+ def test_blow_up_if_not_called
20
+ @mock.foo
21
+
22
+ util_verify_bad "expected meaning_of_life() => 42"
23
+ end
24
+
25
+ def test_not_blow_up_if_everything_called
26
+ @mock.foo
27
+ @mock.meaning_of_life
28
+
29
+ assert_mock @mock
30
+ end
31
+
32
+ def test_allow_expectations_to_be_added_after_creation
33
+ @mock.expect(:bar, true)
34
+ assert @mock.bar
35
+ end
36
+
37
+ def test_not_verify_if_new_expected_method_is_not_called
38
+ @mock.foo
39
+ @mock.meaning_of_life
40
+ @mock.expect(:bar, true)
41
+
42
+ util_verify_bad "expected bar() => true"
43
+ end
44
+
45
+ def test_blow_up_on_wrong_number_of_arguments
46
+ @mock.foo
47
+ @mock.meaning_of_life
48
+ @mock.expect(:sum, 3, [1, 2])
49
+
50
+ e = assert_raises ArgumentError do
51
+ @mock.sum
52
+ end
53
+
54
+ assert_equal "mocked method :sum expects 2 arguments, got 0", e.message
55
+ end
56
+
57
+ def test_return_mock_does_not_raise
58
+ retval = Minitest::Mock.new
59
+ mock = Minitest::Mock.new
60
+ mock.expect(:foo, retval)
61
+ mock.foo
62
+
63
+ assert_mock mock
64
+ end
65
+
66
+ def test_mock_args_does_not_raise
67
+ skip "non-opaque use of ==" if maglev?
68
+
69
+ arg = Minitest::Mock.new
70
+ mock = Minitest::Mock.new
71
+ mock.expect(:foo, nil, [arg])
72
+ mock.foo(arg)
73
+
74
+ assert_mock mock
75
+ end
76
+
77
+ def test_set_expectation_on_special_methods
78
+ mock = Minitest::Mock.new
79
+
80
+ mock.expect :object_id, "received object_id"
81
+ assert_equal "received object_id", mock.object_id
82
+
83
+ mock.expect :respond_to_missing?, "received respond_to_missing?"
84
+ assert_equal "received respond_to_missing?", mock.respond_to_missing?
85
+
86
+ mock.expect :===, "received ==="
87
+ assert_equal "received ===", mock.===
88
+
89
+ mock.expect :inspect, "received inspect"
90
+ assert_equal "received inspect", mock.inspect
91
+
92
+ mock.expect :to_s, "received to_s"
93
+ assert_equal "received to_s", mock.to_s
94
+
95
+ mock.expect :public_send, "received public_send"
96
+ assert_equal "received public_send", mock.public_send
97
+
98
+ mock.expect :send, "received send"
99
+ assert_equal "received send", mock.send
100
+
101
+ assert_mock mock
102
+ end
103
+
104
+ def test_expectations_can_be_satisfied_via_send
105
+ @mock.send :foo
106
+ @mock.send :meaning_of_life
107
+
108
+ assert_mock @mock
109
+ end
110
+
111
+ def test_expectations_can_be_satisfied_via_public_send
112
+ skip "Doesn't run on 1.8" if RUBY_VERSION < "1.9"
113
+
114
+ @mock.public_send :foo
115
+ @mock.public_send :meaning_of_life
116
+
117
+ assert_mock @mock
118
+ end
119
+
120
+ def test_blow_up_on_wrong_arguments
121
+ @mock.foo
122
+ @mock.meaning_of_life
123
+ @mock.expect(:sum, 3, [1, 2])
124
+
125
+ e = assert_raises MockExpectationError do
126
+ @mock.sum(2, 4)
127
+ end
128
+
129
+ exp = "mocked method :sum called with unexpected arguments [2, 4]"
130
+ assert_equal exp, e.message
131
+ end
132
+
133
+ def test_expect_with_non_array_args
134
+ e = assert_raises ArgumentError do
135
+ @mock.expect :blah, 3, false
136
+ end
137
+
138
+ assert_equal "args must be an array", e.message
139
+ end
140
+
141
+ def test_respond_appropriately
142
+ assert @mock.respond_to?(:foo)
143
+ assert @mock.respond_to?(:foo, true)
144
+ assert @mock.respond_to?("foo")
145
+ assert !@mock.respond_to?(:bar)
146
+ end
147
+
148
+ def test_no_method_error_on_unexpected_methods
149
+ e = assert_raises NoMethodError do
150
+ @mock.bar
151
+ end
152
+
153
+ expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
154
+
155
+ assert_equal expected, e.message
156
+ end
157
+
158
+ def test_assign_per_mock_return_values
159
+ a = Minitest::Mock.new
160
+ b = Minitest::Mock.new
161
+
162
+ a.expect(:foo, :a)
163
+ b.expect(:foo, :b)
164
+
165
+ assert_equal :a, a.foo
166
+ assert_equal :b, b.foo
167
+ end
168
+
169
+ def test_do_not_create_stub_method_on_new_mocks
170
+ a = Minitest::Mock.new
171
+ a.expect(:foo, :a)
172
+
173
+ assert !Minitest::Mock.new.respond_to?(:foo)
174
+ end
175
+
176
+ def test_mock_is_a_blank_slate
177
+ @mock.expect :kind_of?, true, [String]
178
+ @mock.expect :==, true, [1]
179
+
180
+ assert @mock.kind_of?(String), "didn't mock :kind_of\?"
181
+ assert @mock == 1, "didn't mock :=="
182
+ end
183
+
184
+ def test_verify_allows_called_args_to_be_loosely_specified
185
+ mock = Minitest::Mock.new
186
+ mock.expect :loose_expectation, true, [Integer]
187
+ mock.loose_expectation 1
188
+
189
+ assert_mock mock
190
+ end
191
+
192
+ def test_verify_raises_with_strict_args
193
+ mock = Minitest::Mock.new
194
+ mock.expect :strict_expectation, true, [2]
195
+
196
+ e = assert_raises MockExpectationError do
197
+ mock.strict_expectation 1
198
+ end
199
+
200
+ exp = "mocked method :strict_expectation called with unexpected arguments [1]"
201
+ assert_equal exp, e.message
202
+ end
203
+
204
+ def test_method_missing_empty
205
+ mock = Minitest::Mock.new
206
+
207
+ mock.expect :a, nil
208
+
209
+ mock.a
210
+
211
+ e = assert_raises MockExpectationError do
212
+ mock.a
213
+ end
214
+
215
+ assert_equal "No more expects available for :a: []", e.message
216
+ end
217
+
218
+ def test_same_method_expects_are_verified_when_all_called
219
+ mock = Minitest::Mock.new
220
+ mock.expect :foo, nil, [:bar]
221
+ mock.expect :foo, nil, [:baz]
222
+
223
+ mock.foo :bar
224
+ mock.foo :baz
225
+
226
+ assert_mock mock
227
+ end
228
+
229
+ def test_same_method_expects_blow_up_when_not_all_called
230
+ mock = Minitest::Mock.new
231
+ mock.expect :foo, nil, [:bar]
232
+ mock.expect :foo, nil, [:baz]
233
+
234
+ mock.foo :bar
235
+
236
+ e = assert_raises(MockExpectationError) { mock.verify }
237
+
238
+ exp = "expected foo(:baz) => nil, got [foo(:bar) => nil]"
239
+
240
+ assert_equal exp, e.message
241
+ end
242
+
243
+ def test_same_method_expects_with_same_args_blow_up_when_not_all_called
244
+ mock = Minitest::Mock.new
245
+ mock.expect :foo, nil, [:bar]
246
+ mock.expect :foo, nil, [:bar]
247
+
248
+ mock.foo :bar
249
+
250
+ e = assert_raises(MockExpectationError) { mock.verify }
251
+
252
+ exp = "expected foo(:bar) => nil, got [foo(:bar) => nil]"
253
+
254
+ assert_equal exp, e.message
255
+ end
256
+
257
+ def test_verify_passes_when_mock_block_returns_true
258
+ mock = Minitest::Mock.new
259
+ mock.expect :foo, nil do
260
+ true
261
+ end
262
+
263
+ mock.foo
264
+
265
+ assert_mock mock
266
+ end
267
+
268
+ def test_mock_block_is_passed_function_params
269
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
270
+ mock = Minitest::Mock.new
271
+ mock.expect :foo, nil do |a1, a2, a3|
272
+ a1 == arg1 && a2 == arg2 && a3 == arg3
273
+ end
274
+
275
+ mock.foo arg1, arg2, arg3
276
+
277
+ assert_mock mock
278
+ end
279
+
280
+ def test_mock_block_is_passed_function_block
281
+ mock = Minitest::Mock.new
282
+ block = proc { "bar" }
283
+ mock.expect :foo, nil do |arg, &blk|
284
+ arg == "foo" &&
285
+ blk == block
286
+ end
287
+ mock.foo "foo", &block
288
+ assert_mock mock
289
+ end
290
+
291
+ def test_verify_fails_when_mock_block_returns_false
292
+ mock = Minitest::Mock.new
293
+ mock.expect :foo, nil do
294
+ false
295
+ end
296
+
297
+ e = assert_raises(MockExpectationError) { mock.foo }
298
+ exp = "mocked method :foo failed block w/ []"
299
+
300
+ assert_equal exp, e.message
301
+ end
302
+
303
+ def test_mock_block_throws_if_args_passed
304
+ mock = Minitest::Mock.new
305
+
306
+ e = assert_raises(ArgumentError) do
307
+ mock.expect :foo, nil, [:a, :b, :c] do
308
+ true
309
+ end
310
+ end
311
+
312
+ exp = "args ignored when block given"
313
+
314
+ assert_equal exp, e.message
315
+ end
316
+
317
+ def test_mock_returns_retval_when_called_with_block
318
+ mock = Minitest::Mock.new
319
+ mock.expect(:foo, 32) do
320
+ true
321
+ end
322
+
323
+ rs = mock.foo
324
+
325
+ assert_equal rs, 32
326
+ end
327
+
328
+ def util_verify_bad exp
329
+ e = assert_raises MockExpectationError do
330
+ @mock.verify
331
+ end
332
+
333
+ assert_equal exp, e.message
334
+ end
335
+
336
+ def test_mock_called_via_send
337
+ mock = Minitest::Mock.new
338
+ mock.expect(:foo, true)
339
+
340
+ mock.send :foo
341
+ assert_mock mock
342
+ end
343
+
344
+ def test_mock_called_via___send__
345
+ mock = Minitest::Mock.new
346
+ mock.expect(:foo, true)
347
+
348
+ mock.__send__ :foo
349
+ assert_mock mock
350
+ end
351
+
352
+ def test_mock_called_via_send_with_args
353
+ mock = Minitest::Mock.new
354
+ mock.expect(:foo, true, [1, 2, 3])
355
+
356
+ mock.send(:foo, 1, 2, 3)
357
+ assert_mock mock
358
+ end
359
+
360
+ end
361
+
362
+ require "minitest/metametameta"
363
+
364
+ class TestMinitestStub < Minitest::Test
365
+ parallelize_me!
366
+
367
+ def setup
368
+ super
369
+ Minitest::Test.reset
370
+
371
+ @tc = Minitest::Test.new "fake tc"
372
+ @assertion_count = 1
373
+ end
374
+
375
+ def teardown
376
+ super
377
+ assert_equal @assertion_count, @tc.assertions
378
+ end
379
+
380
+ class Time
381
+ def self.now
382
+ 24
383
+ end
384
+ end
385
+
386
+ def assert_stub val_or_callable
387
+ @assertion_count += 1
388
+
389
+ t = Time.now.to_i
390
+
391
+ Time.stub :now, val_or_callable do
392
+ @tc.assert_equal 42, Time.now
393
+ end
394
+
395
+ @tc.assert_operator Time.now.to_i, :>=, t
396
+ end
397
+
398
+ def test_stub_private_module_method
399
+ @assertion_count += 1
400
+
401
+ t0 = Time.now
402
+
403
+ self.stub :sleep, nil do
404
+ @tc.assert_nil sleep(10)
405
+ end
406
+
407
+ @tc.assert_operator Time.now - t0, :<=, 1
408
+ end
409
+
410
+ def test_stub_private_module_method_indirect
411
+ @assertion_count += 1
412
+
413
+ fail_clapper = Class.new do
414
+ def fail_clap
415
+ raise
416
+ :clap
417
+ end
418
+ end.new
419
+
420
+ fail_clapper.stub :raise, nil do |safe_clapper|
421
+ @tc.assert_equal :clap, safe_clapper.fail_clap # either form works
422
+ @tc.assert_equal :clap, fail_clapper.fail_clap # yay closures
423
+ end
424
+ end
425
+
426
+ def test_stub_public_module_method
427
+ Math.stub :log10, :stubbed do
428
+ @tc.assert_equal :stubbed, Math.log10(1000)
429
+ end
430
+ end
431
+
432
+ def test_stub_value
433
+ assert_stub 42
434
+ end
435
+
436
+ def test_stub_block
437
+ assert_stub lambda { 42 }
438
+ end
439
+
440
+ def test_stub_block_args
441
+ @assertion_count += 1
442
+
443
+ t = Time.now.to_i
444
+
445
+ Time.stub :now, lambda { |n| n * 2 } do
446
+ @tc.assert_equal 42, Time.now(21)
447
+ end
448
+
449
+ @tc.assert_operator Time.now.to_i, :>=, t
450
+ end
451
+
452
+ def test_stub_callable
453
+ obj = Object.new
454
+
455
+ def obj.call
456
+ 42
457
+ end
458
+
459
+ assert_stub obj
460
+ end
461
+
462
+ def test_stub_yield_self
463
+ obj = "foo"
464
+
465
+ val = obj.stub :to_s, "bar" do |s|
466
+ s.to_s
467
+ end
468
+
469
+ @tc.assert_equal "bar", val
470
+ end
471
+
472
+ def test_dynamic_method
473
+ @assertion_count = 2
474
+
475
+ dynamic = Class.new do
476
+ def self.respond_to? meth
477
+ meth == :found
478
+ end
479
+
480
+ def self.method_missing meth, *args, &block
481
+ if meth == :found
482
+ false
483
+ else
484
+ super
485
+ end
486
+ end
487
+ end
488
+
489
+ val = dynamic.stub(:found, true) do |s|
490
+ s.found
491
+ end
492
+
493
+ @tc.assert_equal true, val
494
+ @tc.assert_equal false, dynamic.found
495
+ end
496
+
497
+ def test_stub_NameError
498
+ e = @tc.assert_raises NameError do
499
+ Time.stub :nope_nope_nope, 42 do
500
+ # do nothing
501
+ end
502
+ end
503
+
504
+ exp = /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
505
+ assert_match exp, e.message
506
+ end
507
+
508
+ def test_mock_with_yield
509
+ mock = Minitest::Mock.new
510
+ mock.expect(:write, true) do
511
+ true
512
+ end
513
+ rs = nil
514
+
515
+ File.stub :open, true, mock do
516
+ File.open "foo.txt", "r" do |f|
517
+ rs = f.write
518
+ end
519
+ end
520
+ @tc.assert_equal true, rs
521
+ end
522
+
523
+ alias test_stub_value__old test_stub_value # TODO: remove/rename
524
+
525
+ ## Permutation Sets:
526
+
527
+ # [:value, :lambda]
528
+ # [:*, :block, :block_call]
529
+ # [:**, :block_args]
530
+ #
531
+ # Where:
532
+ #
533
+ # :value = a normal value
534
+ # :lambda = callable or lambda
535
+ # :* = no block
536
+ # :block = normal block
537
+ # :block_call = :lambda invokes the block (N/A for :value)
538
+ # :** = no args
539
+ # :args = args passed to stub
540
+
541
+ ## Permutations
542
+
543
+ # [:call, :*, :**] =>5 callable+block FIX: CALL BOTH (bug)
544
+ # [:call, :*, :**] =>6 callable
545
+
546
+ # [:lambda, :*, :**] => lambda result
547
+
548
+ # [:lambda, :*, :args] => lambda result NO ARGS
549
+
550
+ # [:lambda, :block, :**] =>5 lambda result FIX: CALL BOTH (bug)
551
+ # [:lambda, :block, :**] =>6 lambda result
552
+
553
+ # [:lambda, :block, :args] =>5 lambda result FIX: CALL BOTH (bug)
554
+ # [:lambda, :block, :args] =>6 lambda result
555
+ # [:lambda, :block, :args] =>7 raise ArgumentError
556
+
557
+ # [:lambda, :block_call, :**] =>5 lambda FIX: BUG!-not passed block to lambda
558
+ # [:lambda, :block_call, :**] =>6 lambda+block result
559
+
560
+ # [:lambda, :block_call, :args] =>5 lambda FIX: BUG!-not passed block to lambda
561
+ # [:lambda, :block_call, :args] =>6 lambda+block result
562
+
563
+ # [:value, :*, :**] => value
564
+
565
+ # [:value, :*, :args] => value, ignore args
566
+
567
+ # [:value, :block, :**] =>5 value, call block
568
+ # [:value, :block, :**] =>6 value
569
+
570
+ # [:value, :block, :args] =>5 value, call block w/ args
571
+ # [:value, :block, :args] =>6 value, call block w/ args, deprecated
572
+ # [:value, :block, :args] =>7 raise ArgumentError
573
+
574
+ # [:value, :block_call, :**] => N/A
575
+
576
+ # [:value, :block_call, :args] => N/A
577
+
578
+ class Bar
579
+ def call
580
+ puts "hi"
581
+ end
582
+ end
583
+
584
+ class Foo
585
+ def self.blocking
586
+ yield
587
+ end
588
+ end
589
+
590
+ class Thingy
591
+ def self.identity arg
592
+ arg
593
+ end
594
+ end
595
+
596
+ def test_stub_callable_block_5 # from tenderlove
597
+ @assertion_count += 1
598
+ Foo.stub5 :blocking, Bar.new do
599
+ @tc.assert_output "hi\n", "" do
600
+ Foo.blocking do
601
+ @tc.flunk "shouldn't ever hit this"
602
+ end
603
+ end
604
+ end
605
+ end
606
+
607
+ def test_stub_callable_block_6 # from tenderlove
608
+ skip_stub6
609
+
610
+ @assertion_count += 1
611
+ Foo.stub6 :blocking, Bar.new do
612
+ @tc.assert_output "hi\n", "" do
613
+ Foo.blocking do
614
+ @tc.flunk "shouldn't ever hit this"
615
+ end
616
+ end
617
+ end
618
+ end
619
+
620
+ def test_stub_lambda
621
+ Thread.stub :new, lambda { 21+21 } do
622
+ @tc.assert_equal 42, Thread.new
623
+ end
624
+ end
625
+
626
+ def test_stub_lambda_args
627
+ Thread.stub :new, lambda { 21+21 }, :wtf do
628
+ @tc.assert_equal 42, Thread.new
629
+ end
630
+ end
631
+
632
+ def test_stub_lambda_block_5
633
+ Thread.stub5 :new, lambda { 21+21 } do
634
+ result = Thread.new do
635
+ @tc.flunk "shouldn't ever hit this"
636
+ end
637
+ @tc.assert_equal 42, result
638
+ end
639
+ end
640
+
641
+ def test_stub_lambda_block_6
642
+ skip_stub6
643
+
644
+ Thread.stub6 :new, lambda { 21+21 } do
645
+ result = Thread.new do
646
+ @tc.flunk "shouldn't ever hit this"
647
+ end
648
+ @tc.assert_equal 42, result
649
+ end
650
+ end
651
+
652
+ def test_stub_lambda_block_args_5
653
+ @assertion_count += 1
654
+ Thingy.stub5 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
655
+ result = Thingy.identity :nope do |x|
656
+ @tc.flunk "shouldn't reach this"
657
+ end
658
+ @tc.assert_equal 42, result
659
+ end
660
+ end
661
+
662
+ def test_stub_lambda_block_args_6
663
+ skip_stub6
664
+
665
+ @assertion_count += 1
666
+ Thingy.stub6 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
667
+ result = Thingy.identity :nope do |x|
668
+ @tc.flunk "shouldn't reach this"
669
+ end
670
+ @tc.assert_equal 42, result
671
+ end
672
+ end
673
+
674
+ def test_stub_lambda_block_args_6_2
675
+ skip_stub6
676
+
677
+ @tc.assert_raises ArgumentError do
678
+ Thingy.stub6_2 :identity, lambda { |y| :__not_run__ }, :WTF? do
679
+ # doesn't matter
680
+ end
681
+ end
682
+ end
683
+
684
+ def test_stub_lambda_block_call_5
685
+ @assertion_count += 1
686
+ rs = nil
687
+ io = StringIO.new "", "w"
688
+ File.stub5 :open, lambda { |p, m, &blk| blk and blk.call io } do
689
+ File.open "foo.txt", "r" do |f|
690
+ rs = f && f.write("woot")
691
+ end
692
+ end
693
+ @tc.assert_equal 4, rs
694
+ @tc.assert_equal "woot", io.string
695
+ end
696
+
697
+ def test_stub_lambda_block_call_6
698
+ skip_stub6
699
+
700
+ @assertion_count += 1
701
+ rs = nil
702
+ io = StringIO.new "", "w"
703
+ File.stub6 :open, lambda { |p, m, &blk| blk.call io } do
704
+ File.open "foo.txt", "r" do |f|
705
+ rs = f.write("woot")
706
+ end
707
+ end
708
+ @tc.assert_equal 4, rs
709
+ @tc.assert_equal "woot", io.string
710
+ end
711
+
712
+ def test_stub_lambda_block_call_args_5
713
+ @assertion_count += 1
714
+ rs = nil
715
+ io = StringIO.new "", "w"
716
+ File.stub5(:open, lambda { |p, m, &blk| blk and blk.call io }, :WTF?) do
717
+ File.open "foo.txt", "r" do |f|
718
+ rs = f.write("woot")
719
+ end
720
+ end
721
+ @tc.assert_equal 4, rs
722
+ @tc.assert_equal "woot", io.string
723
+ end
724
+
725
+ def test_stub_lambda_block_call_args_6
726
+ skip_stub6
727
+
728
+ @assertion_count += 1
729
+ rs = nil
730
+ io = StringIO.new "", "w"
731
+ File.stub6(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
732
+ File.open "foo.txt", "r" do |f|
733
+ rs = f.write("woot")
734
+ end
735
+ end
736
+ @tc.assert_equal 4, rs
737
+ @tc.assert_equal "woot", io.string
738
+ end
739
+
740
+ def test_stub_lambda_block_call_args_6_2
741
+ skip_stub6
742
+
743
+ @assertion_count += 2
744
+ rs = nil
745
+ io = StringIO.new "", "w"
746
+ @tc.assert_raises ArgumentError do
747
+ File.stub6_2(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
748
+ File.open "foo.txt", "r" do |f|
749
+ rs = f.write("woot")
750
+ end
751
+ end
752
+ end
753
+ @tc.assert_nil rs
754
+ @tc.assert_equal "", io.string
755
+ end
756
+
757
+ def test_stub_value
758
+ Thread.stub :new, 42 do
759
+ result = Thread.new
760
+ @tc.assert_equal 42, result
761
+ end
762
+ end
763
+
764
+ def test_stub_value_args
765
+ Thread.stub :new, 42, :WTF? do
766
+ result = Thread.new
767
+ @tc.assert_equal 42, result
768
+ end
769
+ end
770
+
771
+ def test_stub_value_block_5
772
+ @assertion_count += 1
773
+ Thread.stub5 :new, 42 do
774
+ result = Thread.new do
775
+ @tc.assert true
776
+ end
777
+ @tc.assert_equal 42, result
778
+ end
779
+ end
780
+
781
+ def test_stub_value_block_6
782
+ skip_stub6
783
+
784
+ Thread.stub6 :new, 42 do
785
+ result = Thread.new do
786
+ @tc.flunk "shouldn't hit this"
787
+ end
788
+ @tc.assert_equal 42, result
789
+ end
790
+ end
791
+
792
+ def test_stub_value_block_args_5
793
+ @assertion_count += 2
794
+ rs = nil
795
+ io = StringIO.new "", "w"
796
+ File.stub5 :open, :value, io do
797
+ result = File.open "foo.txt", "r" do |f|
798
+ rs = f.write("woot")
799
+ end
800
+ @tc.assert_equal :value, result
801
+ end
802
+ @tc.assert_equal 4, rs
803
+ @tc.assert_equal "woot", io.string
804
+ end
805
+
806
+ def test_stub_value_block_args_5__break_if_not_passed
807
+ e = @tc.assert_raises NoMethodError do
808
+ File.stub5 :open, :return_value do # intentionally bad setup w/ no args
809
+ File.open "foo.txt", "r" do |f|
810
+ f.write "woot"
811
+ end
812
+ end
813
+ end
814
+ exp = "undefined method `write' for nil:NilClass"
815
+ assert_equal exp, e.message
816
+ end
817
+
818
+ def test_stub_value_block_args_6
819
+ skip_stub6
820
+
821
+ @assertion_count += 2
822
+ rs = nil
823
+ io = StringIO.new "", "w"
824
+ assert_deprecated do
825
+ File.stub6 :open, :value, io do
826
+ result = File.open "foo.txt", "r" do |f|
827
+ rs = f.write("woot")
828
+ end
829
+ @tc.assert_equal :value, result
830
+ end
831
+ end
832
+ @tc.assert_equal 4, rs
833
+ @tc.assert_equal "woot", io.string
834
+ end
835
+
836
+ def test_stub_value_block_args_6_2
837
+ skip_stub6
838
+
839
+ @assertion_count += 2
840
+ rs = nil
841
+ io = StringIO.new "", "w"
842
+ @tc.assert_raises ArgumentError do
843
+ File.stub6_2 :open, :value, io do
844
+ result = File.open "foo.txt", "r" do |f|
845
+ @tc.flunk "shouldn't hit this"
846
+ end
847
+ @tc.assert_equal :value, result
848
+ end
849
+ end
850
+ @tc.assert_nil rs
851
+ @tc.assert_equal "", io.string
852
+ end
853
+
854
+ def assert_deprecated re = /deprecated/
855
+ assert_output "", re do
856
+ yield
857
+ end
858
+ end
859
+
860
+ def skip_stub6
861
+ skip "not yet" unless STUB6
862
+ end
863
+ end
864
+
865
+ STUB6 = ENV["STUB6"]
866
+
867
+ if STUB6 then
868
+ require "minitest/mock6" if STUB6
869
+ else
870
+ class Object
871
+ alias stub5 stub
872
+ alias stub6 stub
873
+ end
874
+ end