minitest 5.10.3 → 5.18.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.
@@ -1,8 +1,14 @@
1
1
  require "minitest/autorun"
2
2
 
3
- class TestMinitestMock < Minitest::Test
4
- parallelize_me!
3
+ def with_kwargs_env
4
+ ENV["MT_KWARGS_HAC\K"] = "1"
5
+
6
+ yield
7
+ ensure
8
+ ENV.delete "MT_KWARGS_HAC\K"
9
+ end
5
10
 
11
+ class TestMinitestMock < Minitest::Test
6
12
  def setup
7
13
  @mock = Minitest::Mock.new.expect(:foo, nil)
8
14
  @mock.expect(:meaning_of_life, 42)
@@ -51,7 +57,7 @@ class TestMinitestMock < Minitest::Test
51
57
  @mock.sum
52
58
  end
53
59
 
54
- assert_equal "mocked method :sum expects 2 arguments, got 0", e.message
60
+ assert_equal "mocked method :sum expects 2 arguments, got []", e.message
55
61
  end
56
62
 
57
63
  def test_return_mock_does_not_raise
@@ -64,8 +70,6 @@ class TestMinitestMock < Minitest::Test
64
70
  end
65
71
 
66
72
  def test_mock_args_does_not_raise
67
- skip "non-opaque use of ==" if maglev?
68
-
69
73
  arg = Minitest::Mock.new
70
74
  mock = Minitest::Mock.new
71
75
  mock.expect(:foo, nil, [arg])
@@ -135,7 +139,7 @@ class TestMinitestMock < Minitest::Test
135
139
  @mock.expect :blah, 3, false
136
140
  end
137
141
 
138
- assert_equal "args must be an array", e.message
142
+ assert_match "args must be an array", e.message
139
143
  end
140
144
 
141
145
  def test_respond_appropriately
@@ -152,7 +156,7 @@ class TestMinitestMock < Minitest::Test
152
156
 
153
157
  expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
154
158
 
155
- assert_equal expected, e.message
159
+ assert_match expected, e.message
156
160
  end
157
161
 
158
162
  def test_assign_per_mock_return_values
@@ -212,7 +216,7 @@ class TestMinitestMock < Minitest::Test
212
216
  mock.a
213
217
  end
214
218
 
215
- assert_equal "No more expects available for :a: []", e.message
219
+ assert_equal "No more expects available for :a: [] {}", e.message
216
220
  end
217
221
 
218
222
  def test_same_method_expects_are_verified_when_all_called
@@ -254,6 +258,30 @@ class TestMinitestMock < Minitest::Test
254
258
  assert_equal exp, e.message
255
259
  end
256
260
 
261
+ def test_delegator_calls_are_propagated
262
+ delegator = Object.new
263
+ mock = Minitest::Mock.new delegator
264
+
265
+ refute delegator.nil?
266
+ refute mock.nil?
267
+ assert_mock mock
268
+ end
269
+
270
+ def test_handles_kwargs_in_error_message
271
+ mock = Minitest::Mock.new
272
+
273
+ mock.expect :foo, nil, [], kw: true
274
+ mock.expect :foo, nil, [], kw: false
275
+
276
+ mock.foo kw: true
277
+
278
+ e = assert_raises(MockExpectationError) { mock.verify }
279
+
280
+ exp = "expected foo(:kw=>false) => nil, got [foo(:kw=>true) => nil]"
281
+
282
+ assert_equal exp, e.message
283
+ end
284
+
257
285
  def test_verify_passes_when_mock_block_returns_true
258
286
  mock = Minitest::Mock.new
259
287
  mock.expect :foo, nil do
@@ -272,11 +300,188 @@ class TestMinitestMock < Minitest::Test
272
300
  a1 == arg1 && a2 == arg2 && a3 == arg3
273
301
  end
274
302
 
275
- mock.foo arg1, arg2, arg3
303
+ assert_silent do
304
+ if RUBY_VERSION > "3" then
305
+ mock.foo arg1, arg2, arg3
306
+ else
307
+ mock.foo arg1, arg2, **arg3 # oddity just for ruby 2.7
308
+ end
309
+ end
310
+
311
+ assert_mock mock
312
+ end
313
+
314
+ def test_mock_block_is_passed_keyword_args__block
315
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
316
+ mock = Minitest::Mock.new
317
+ mock.expect :foo, nil do |k1:, k2:, k3:|
318
+ k1 == arg1 && k2 == arg2 && k3 == arg3
319
+ end
320
+
321
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
322
+
323
+ assert_mock mock
324
+ end
325
+
326
+ def test_mock_block_is_passed_keyword_args__block_bad_missing
327
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
328
+ mock = Minitest::Mock.new
329
+ mock.expect :foo, nil do |k1:, k2:, k3:|
330
+ k1 == arg1 && k2 == arg2 && k3 == arg3
331
+ end
332
+
333
+ e = assert_raises ArgumentError do
334
+ mock.foo(k1: arg1, k2: arg2)
335
+ end
336
+
337
+ # basically testing ruby ... need ? for ruby < 2.7 :(
338
+ assert_match(/missing keyword: :?k3/, e.message)
339
+ end
340
+
341
+ def test_mock_block_is_passed_keyword_args__block_bad_extra
342
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
343
+ mock = Minitest::Mock.new
344
+ mock.expect :foo, nil do |k1:, k2:|
345
+ k1 == arg1 && k2 == arg2 && k3 == arg3
346
+ end
347
+
348
+ e = assert_raises ArgumentError do
349
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
350
+ end
351
+
352
+ # basically testing ruby ... need ? for ruby < 2.7 :(
353
+ assert_match(/unknown keyword: :?k3/, e.message)
354
+ end
355
+
356
+ def test_mock_block_is_passed_keyword_args__block_bad_value
357
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
358
+ mock = Minitest::Mock.new
359
+ mock.expect :foo, nil do |k1:, k2:, k3:|
360
+ k1 == arg1 && k2 == arg2 && k3 == arg3
361
+ end
362
+
363
+ e = assert_raises MockExpectationError do
364
+ mock.foo(k1: arg1, k2: arg2, k3: :BAD!)
365
+ end
366
+
367
+ exp = "mocked method :foo failed block w/ [] {:k1=>:bar, :k2=>[1, 2, 3], :k3=>:BAD!}"
368
+ assert_equal exp, e.message
369
+ end
370
+
371
+ def test_mock_block_is_passed_keyword_args__args
372
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
373
+ mock = Minitest::Mock.new
374
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
375
+
376
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
276
377
 
277
378
  assert_mock mock
278
379
  end
279
380
 
381
+ def test_mock_allow_all_kwargs__old_style_env
382
+ with_kwargs_env do
383
+ mock = Minitest::Mock.new
384
+ mock.expect :foo, true, [Hash]
385
+ assert_equal true, mock.foo(bar: 42)
386
+ end
387
+ end
388
+
389
+ def test_mock_allow_all_kwargs__old_style_env__rewrite
390
+ with_kwargs_env do
391
+ mock = Minitest::Mock.new
392
+ mock.expect :foo, true, [], bar: Integer
393
+ assert_equal true, mock.foo(bar: 42)
394
+ end
395
+ end
396
+
397
+ def test_mock_block_is_passed_keyword_args__args__old_style_bad
398
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
399
+ mock = Minitest::Mock.new
400
+ mock.expect :foo, nil, [{k1: arg1, k2: arg2, k3: arg3}]
401
+
402
+ e = assert_raises ArgumentError do
403
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
404
+ end
405
+
406
+ assert_equal "mocked method :foo expects 1 arguments, got []", e.message
407
+ end
408
+
409
+ def test_mock_block_is_passed_keyword_args__args__old_style_env
410
+ with_kwargs_env do
411
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
412
+ mock = Minitest::Mock.new
413
+ mock.expect :foo, nil, [{k1: arg1, k2: arg2, k3: arg3}]
414
+
415
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
416
+
417
+ assert_mock mock
418
+ end
419
+ end
420
+
421
+ def test_mock_block_is_passed_keyword_args__args__old_style_both
422
+ with_kwargs_env do
423
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
424
+ mock = Minitest::Mock.new
425
+
426
+ assert_output nil, /Using MT_KWARGS_HAC. yet passing kwargs/ do
427
+ mock.expect :foo, nil, [{}], k1: arg1, k2: arg2, k3: arg3
428
+ end
429
+
430
+ mock.foo({}, k1: arg1, k2: arg2, k3: arg3)
431
+
432
+ assert_mock mock
433
+ end
434
+ end
435
+
436
+ def test_mock_block_is_passed_keyword_args__args_bad_missing
437
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
438
+ mock = Minitest::Mock.new
439
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
440
+
441
+ e = assert_raises ArgumentError do
442
+ mock.foo(k1: arg1, k2: arg2)
443
+ end
444
+
445
+ assert_equal "mocked method :foo expects 3 keyword arguments, got %p" % {k1: arg1, k2: arg2}, e.message
446
+ end
447
+
448
+ def test_mock_block_is_passed_keyword_args__args_bad_extra
449
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
450
+ mock = Minitest::Mock.new
451
+ mock.expect :foo, nil, k1: arg1, k2: arg2
452
+
453
+ e = assert_raises ArgumentError do
454
+ mock.foo(k1: arg1, k2: arg2, k3: arg3)
455
+ end
456
+
457
+ assert_equal "mocked method :foo expects 2 keyword arguments, got %p" % {k1: arg1, k2: arg2, k3: arg3}, e.message
458
+ end
459
+
460
+ def test_mock_block_is_passed_keyword_args__args_bad_key
461
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
462
+ mock = Minitest::Mock.new
463
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
464
+
465
+ e = assert_raises MockExpectationError do
466
+ mock.foo(k1: arg1, k2: arg2, BAD: arg3)
467
+ end
468
+
469
+ assert_includes e.message, "unexpected keywords [:k1, :k2, :k3]"
470
+ assert_includes e.message, "vs [:k1, :k2, :BAD]"
471
+ end
472
+
473
+ def test_mock_block_is_passed_keyword_args__args_bad_val
474
+ arg1, arg2, arg3 = :bar, [1, 2, 3], { :a => "a" }
475
+ mock = Minitest::Mock.new
476
+ mock.expect :foo, nil, k1: arg1, k2: arg2, k3: arg3
477
+
478
+ e = assert_raises MockExpectationError do
479
+ mock.foo(k1: arg1, k2: :BAD!, k3: arg3)
480
+ end
481
+
482
+ assert_match(/unexpected keyword arguments.* vs .*:k2=>:BAD!/, e.message)
483
+ end
484
+
280
485
  def test_mock_block_is_passed_function_block
281
486
  mock = Minitest::Mock.new
282
487
  block = proc { "bar" }
@@ -288,6 +493,13 @@ class TestMinitestMock < Minitest::Test
288
493
  assert_mock mock
289
494
  end
290
495
 
496
+ def test_mock_forward_keyword_arguments
497
+ mock = Minitest::Mock.new
498
+ mock.expect(:foo, nil) { |bar:| bar == 'bar' }
499
+ mock.foo(bar: 'bar')
500
+ assert_mock mock
501
+ end
502
+
291
503
  def test_verify_fails_when_mock_block_returns_false
292
504
  mock = Minitest::Mock.new
293
505
  mock.expect :foo, nil do
@@ -295,12 +507,12 @@ class TestMinitestMock < Minitest::Test
295
507
  end
296
508
 
297
509
  e = assert_raises(MockExpectationError) { mock.foo }
298
- exp = "mocked method :foo failed block w/ []"
510
+ exp = "mocked method :foo failed block w/ [] {}"
299
511
 
300
512
  assert_equal exp, e.message
301
513
  end
302
514
 
303
- def test_mock_block_throws_if_args_passed
515
+ def test_mock_block_raises_if_args_passed
304
516
  mock = Minitest::Mock.new
305
517
 
306
518
  e = assert_raises(ArgumentError) do
@@ -311,7 +523,21 @@ class TestMinitestMock < Minitest::Test
311
523
 
312
524
  exp = "args ignored when block given"
313
525
 
314
- assert_equal exp, e.message
526
+ assert_match exp, e.message
527
+ end
528
+
529
+ def test_mock_block_raises_if_kwargs_passed
530
+ mock = Minitest::Mock.new
531
+
532
+ e = assert_raises(ArgumentError) do
533
+ mock.expect :foo, nil, kwargs:1 do
534
+ true
535
+ end
536
+ end
537
+
538
+ exp = "kwargs ignored when block given"
539
+
540
+ assert_match exp, e.message
315
541
  end
316
542
 
317
543
  def test_mock_returns_retval_when_called_with_block
@@ -362,7 +588,7 @@ end
362
588
  require "minitest/metametameta"
363
589
 
364
590
  class TestMinitestStub < Minitest::Test
365
- parallelize_me!
591
+ # Do not parallelize since we're calling stub on class methods
366
592
 
367
593
  def setup
368
594
  super
@@ -374,7 +600,7 @@ class TestMinitestStub < Minitest::Test
374
600
 
375
601
  def teardown
376
602
  super
377
- assert_equal @assertion_count, @tc.assertions
603
+ assert_equal @assertion_count, @tc.assertions if self.passed?
378
604
  end
379
605
 
380
606
  class Time
@@ -494,6 +720,18 @@ class TestMinitestStub < Minitest::Test
494
720
  @tc.assert_equal false, dynamic.found
495
721
  end
496
722
 
723
+ def test_stub_NameError
724
+ e = @tc.assert_raises NameError do
725
+ Time.stub :nope_nope_nope, 42 do
726
+ # do nothing
727
+ end
728
+ end
729
+
730
+ exp = jruby? ? /Undefined method nope_nope_nope for '#{self.class}::Time'/ :
731
+ /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
732
+ assert_match exp, e.message
733
+ end
734
+
497
735
  def test_mock_with_yield
498
736
  mock = Minitest::Mock.new
499
737
  mock.expect(:write, true) do
@@ -509,4 +747,400 @@ class TestMinitestStub < Minitest::Test
509
747
  @tc.assert_equal true, rs
510
748
  end
511
749
 
750
+ def test_mock_with_yield_kwargs
751
+ mock = Minitest::Mock.new
752
+ rs = nil
753
+
754
+ File.stub :open, true, mock, kw:42 do
755
+ File.open "foo.txt", "r" do |f, kw:|
756
+ rs = kw
757
+ end
758
+ end
759
+
760
+ @tc.assert_equal 42, rs
761
+ end
762
+
763
+ alias test_stub_value__old test_stub_value # TODO: remove/rename
764
+
765
+ ## Permutation Sets:
766
+
767
+ # [:value, :lambda]
768
+ # [:*, :block, :block_call]
769
+ # [:**, :block_args]
770
+ #
771
+ # Where:
772
+ #
773
+ # :value = a normal value
774
+ # :lambda = callable or lambda
775
+ # :* = no block
776
+ # :block = normal block
777
+ # :block_call = :lambda invokes the block (N/A for :value)
778
+ # :** = no args
779
+ # :args = args passed to stub
780
+
781
+ ## Permutations
782
+
783
+ # [:call, :*, :**] =>5 callable+block FIX: CALL BOTH (bug)
784
+ # [:call, :*, :**] =>6 callable
785
+
786
+ # [:lambda, :*, :**] => lambda result
787
+
788
+ # [:lambda, :*, :args] => lambda result NO ARGS
789
+
790
+ # [:lambda, :block, :**] =>5 lambda result FIX: CALL BOTH (bug)
791
+ # [:lambda, :block, :**] =>6 lambda result
792
+
793
+ # [:lambda, :block, :args] =>5 lambda result FIX: CALL BOTH (bug)
794
+ # [:lambda, :block, :args] =>6 lambda result
795
+ # [:lambda, :block, :args] =>7 raise ArgumentError
796
+
797
+ # [:lambda, :block_call, :**] =>5 lambda FIX: BUG!-not passed block to lambda
798
+ # [:lambda, :block_call, :**] =>6 lambda+block result
799
+
800
+ # [:lambda, :block_call, :args] =>5 lambda FIX: BUG!-not passed block to lambda
801
+ # [:lambda, :block_call, :args] =>6 lambda+block result
802
+
803
+ # [:value, :*, :**] => value
804
+
805
+ # [:value, :*, :args] => value, ignore args
806
+
807
+ # [:value, :block, :**] =>5 value, call block
808
+ # [:value, :block, :**] =>6 value
809
+
810
+ # [:value, :block, :args] =>5 value, call block w/ args
811
+ # [:value, :block, :args] =>6 value, call block w/ args, deprecated
812
+ # [:value, :block, :args] =>7 raise ArgumentError
813
+
814
+ # [:value, :block_call, :**] => N/A
815
+
816
+ # [:value, :block_call, :args] => N/A
817
+
818
+ class Bar
819
+ def call
820
+ puts "hi"
821
+ end
822
+ end
823
+
824
+ class Foo
825
+ def self.blocking
826
+ yield
827
+ end
828
+ end
829
+
830
+ class Thingy
831
+ def self.identity arg
832
+ arg
833
+ end
834
+ end
835
+
836
+ class Keywords
837
+ def self.args req, kw1:, kw2:24
838
+ [req, kw1, kw2]
839
+ end
840
+ end
841
+
842
+ def test_stub_callable_keyword_args
843
+ Keywords.stub :args, ->(*args, **kws) { [args, kws] } do
844
+ @tc.assert_equal [["woot"], { kw1: 42 }], Keywords.args("woot", kw1: 42)
845
+ end
846
+ end
847
+
848
+ def test_stub__hash_as_last_real_arg
849
+ with_kwargs_env do
850
+ token = Object.new
851
+ def token.create_with_retry u, p; raise "shouldn't see this"; end
852
+
853
+ controller = Object.new
854
+ controller.define_singleton_method :create do |u, p|
855
+ token.create_with_retry u, p
856
+ end
857
+
858
+ params = Object.new
859
+ def params.to_hash; raise "nah"; end
860
+
861
+ token.stub(:create_with_retry, ->(u, p) { 42 }) do
862
+ act = controller.create :u, params
863
+ @tc.assert_equal 42, act
864
+ end
865
+ end
866
+ end
867
+
868
+ def test_stub_callable_block_5 # from tenderlove
869
+ @assertion_count += 1
870
+ Foo.stub5 :blocking, Bar.new do
871
+ @tc.assert_output "hi\n", "" do
872
+ Foo.blocking do
873
+ @tc.flunk "shouldn't ever hit this"
874
+ end
875
+ end
876
+ end
877
+ end
878
+
879
+ def test_stub_callable_block_6 # from tenderlove
880
+ skip_stub6
881
+
882
+ @assertion_count += 1
883
+ Foo.stub6 :blocking, Bar.new do
884
+ @tc.assert_output "hi\n", "" do
885
+ Foo.blocking do
886
+ @tc.flunk "shouldn't ever hit this"
887
+ end
888
+ end
889
+ end
890
+ end
891
+
892
+ def test_stub_lambda
893
+ Thread.stub :new, lambda { 21+21 } do
894
+ @tc.assert_equal 42, Thread.new
895
+ end
896
+ end
897
+
898
+ def test_stub_lambda_args
899
+ Thread.stub :new, lambda { 21+21 }, :wtf do
900
+ @tc.assert_equal 42, Thread.new
901
+ end
902
+ end
903
+
904
+ def test_stub_lambda_block_5
905
+ Thread.stub5 :new, lambda { 21+21 } do
906
+ result = Thread.new do
907
+ @tc.flunk "shouldn't ever hit this"
908
+ end
909
+ @tc.assert_equal 42, result
910
+ end
911
+ end
912
+
913
+ def test_stub_lambda_block_6
914
+ skip_stub6
915
+
916
+ Thread.stub6 :new, lambda { 21+21 } do
917
+ result = Thread.new do
918
+ @tc.flunk "shouldn't ever hit this"
919
+ end
920
+ @tc.assert_equal 42, result
921
+ end
922
+ end
923
+
924
+ def test_stub_lambda_block_args_5
925
+ @assertion_count += 1
926
+ Thingy.stub5 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
927
+ result = Thingy.identity :nope do |x|
928
+ @tc.flunk "shouldn't reach this"
929
+ end
930
+ @tc.assert_equal 42, result
931
+ end
932
+ end
933
+
934
+ def test_stub_lambda_block_args_6
935
+ skip_stub6
936
+
937
+ @assertion_count += 1
938
+ Thingy.stub6 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
939
+ result = Thingy.identity :nope do |x|
940
+ @tc.flunk "shouldn't reach this"
941
+ end
942
+ @tc.assert_equal 42, result
943
+ end
944
+ end
945
+
946
+ def test_stub_lambda_block_args_6_2
947
+ skip_stub6
948
+
949
+ @tc.assert_raises ArgumentError do
950
+ Thingy.stub6_2 :identity, lambda { |y| :__not_run__ }, :WTF? do
951
+ # doesn't matter
952
+ end
953
+ end
954
+ end
955
+
956
+ def test_stub_lambda_block_call_5
957
+ @assertion_count += 1
958
+ rs = nil
959
+ io = StringIO.new "", "w"
960
+ File.stub5 :open, lambda { |p, m, &blk| blk and blk.call io } do
961
+ File.open "foo.txt", "r" do |f|
962
+ rs = f && f.write("woot")
963
+ end
964
+ end
965
+ @tc.assert_equal 4, rs
966
+ @tc.assert_equal "woot", io.string
967
+ end
968
+
969
+ def test_stub_lambda_block_call_6
970
+ skip_stub6
971
+
972
+ @assertion_count += 1
973
+ rs = nil
974
+ io = StringIO.new "", "w"
975
+ File.stub6 :open, lambda { |p, m, &blk| blk.call io } do
976
+ File.open "foo.txt", "r" do |f|
977
+ rs = f.write("woot")
978
+ end
979
+ end
980
+ @tc.assert_equal 4, rs
981
+ @tc.assert_equal "woot", io.string
982
+ end
983
+
984
+ def test_stub_lambda_block_call_args_5
985
+ @assertion_count += 1
986
+ rs = nil
987
+ io = StringIO.new "", "w"
988
+ File.stub5(:open, lambda { |p, m, &blk| blk and blk.call io }, :WTF?) do
989
+ File.open "foo.txt", "r" do |f|
990
+ rs = f.write("woot")
991
+ end
992
+ end
993
+ @tc.assert_equal 4, rs
994
+ @tc.assert_equal "woot", io.string
995
+ end
996
+
997
+ def test_stub_lambda_block_call_args_6
998
+ skip_stub6
999
+
1000
+ @assertion_count += 1
1001
+ rs = nil
1002
+ io = StringIO.new "", "w"
1003
+ File.stub6(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
1004
+ File.open "foo.txt", "r" do |f|
1005
+ rs = f.write("woot")
1006
+ end
1007
+ end
1008
+ @tc.assert_equal 4, rs
1009
+ @tc.assert_equal "woot", io.string
1010
+ end
1011
+
1012
+ def test_stub_lambda_block_call_args_6_2
1013
+ skip_stub6
1014
+
1015
+ @assertion_count += 2
1016
+ rs = nil
1017
+ io = StringIO.new "", "w"
1018
+ @tc.assert_raises ArgumentError do
1019
+ File.stub6_2(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
1020
+ File.open "foo.txt", "r" do |f|
1021
+ rs = f.write("woot")
1022
+ end
1023
+ end
1024
+ end
1025
+ @tc.assert_nil rs
1026
+ @tc.assert_equal "", io.string
1027
+ end
1028
+
1029
+ def test_stub_value
1030
+ Thread.stub :new, 42 do
1031
+ result = Thread.new
1032
+ @tc.assert_equal 42, result
1033
+ end
1034
+ end
1035
+
1036
+ def test_stub_value_args
1037
+ Thread.stub :new, 42, :WTF? do
1038
+ result = Thread.new
1039
+ @tc.assert_equal 42, result
1040
+ end
1041
+ end
1042
+
1043
+ def test_stub_value_block_5
1044
+ @assertion_count += 1
1045
+ Thread.stub5 :new, 42 do
1046
+ result = Thread.new do
1047
+ @tc.assert true
1048
+ end
1049
+ @tc.assert_equal 42, result
1050
+ end
1051
+ end
1052
+
1053
+ def test_stub_value_block_6
1054
+ skip_stub6
1055
+
1056
+ Thread.stub6 :new, 42 do
1057
+ result = Thread.new do
1058
+ @tc.flunk "shouldn't hit this"
1059
+ end
1060
+ @tc.assert_equal 42, result
1061
+ end
1062
+ end
1063
+
1064
+ def test_stub_value_block_args_5
1065
+ @assertion_count += 2
1066
+ rs = nil
1067
+ io = StringIO.new "", "w"
1068
+ File.stub5 :open, :value, io do
1069
+ result = File.open "foo.txt", "r" do |f|
1070
+ rs = f.write("woot")
1071
+ end
1072
+ @tc.assert_equal :value, result
1073
+ end
1074
+ @tc.assert_equal 4, rs
1075
+ @tc.assert_equal "woot", io.string
1076
+ end
1077
+
1078
+ def test_stub_value_block_args_5__break_if_not_passed
1079
+ e = @tc.assert_raises NoMethodError do
1080
+ File.stub5 :open, :return_value do # intentionally bad setup w/ no args
1081
+ File.open "foo.txt", "r" do |f|
1082
+ f.write "woot"
1083
+ end
1084
+ end
1085
+ end
1086
+ exp = /undefined method `write' for nil/
1087
+ assert_match exp, e.message
1088
+ end
1089
+
1090
+ def test_stub_value_block_args_6
1091
+ skip_stub6
1092
+
1093
+ @assertion_count += 2
1094
+ rs = nil
1095
+ io = StringIO.new "", "w"
1096
+ assert_deprecated do
1097
+ File.stub6 :open, :value, io do
1098
+ result = File.open "foo.txt", "r" do |f|
1099
+ rs = f.write("woot")
1100
+ end
1101
+ @tc.assert_equal :value, result
1102
+ end
1103
+ end
1104
+ @tc.assert_equal 4, rs
1105
+ @tc.assert_equal "woot", io.string
1106
+ end
1107
+
1108
+ def test_stub_value_block_args_6_2
1109
+ skip_stub6
1110
+
1111
+ @assertion_count += 2
1112
+ rs = nil
1113
+ io = StringIO.new "", "w"
1114
+ @tc.assert_raises ArgumentError do
1115
+ File.stub6_2 :open, :value, io do
1116
+ result = File.open "foo.txt", "r" do |f|
1117
+ @tc.flunk "shouldn't hit this"
1118
+ end
1119
+ @tc.assert_equal :value, result
1120
+ end
1121
+ end
1122
+ @tc.assert_nil rs
1123
+ @tc.assert_equal "", io.string
1124
+ end
1125
+
1126
+ def assert_deprecated re = /deprecated/
1127
+ assert_output "", re do
1128
+ yield
1129
+ end
1130
+ end
1131
+
1132
+ def skip_stub6
1133
+ skip "not yet" unless STUB6
1134
+ end
1135
+ end
1136
+
1137
+ STUB6 = ENV["STUB6"]
1138
+
1139
+ if STUB6 then
1140
+ require "minitest/mock6" if STUB6
1141
+ else
1142
+ class Object
1143
+ alias stub5 stub
1144
+ alias stub6 stub
1145
+ end
512
1146
  end