minitest 5.10.3 → 5.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -362,7 +362,7 @@ end
362
362
  require "minitest/metametameta"
363
363
 
364
364
  class TestMinitestStub < Minitest::Test
365
- parallelize_me!
365
+ # Do not parallelize since we're calling stub on class methods
366
366
 
367
367
  def setup
368
368
  super
@@ -374,7 +374,7 @@ class TestMinitestStub < Minitest::Test
374
374
 
375
375
  def teardown
376
376
  super
377
- assert_equal @assertion_count, @tc.assertions
377
+ assert_equal @assertion_count, @tc.assertions if self.passed?
378
378
  end
379
379
 
380
380
  class Time
@@ -494,6 +494,17 @@ class TestMinitestStub < Minitest::Test
494
494
  @tc.assert_equal false, dynamic.found
495
495
  end
496
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
+
497
508
  def test_mock_with_yield
498
509
  mock = Minitest::Mock.new
499
510
  mock.expect(:write, true) do
@@ -509,4 +520,355 @@ class TestMinitestStub < Minitest::Test
509
520
  @tc.assert_equal true, rs
510
521
  end
511
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
512
874
  end
@@ -1,6 +1,12 @@
1
1
  require "minitest/autorun"
2
2
  require "minitest/metametameta"
3
3
 
4
+ class Runnable
5
+ def woot
6
+ assert true
7
+ end
8
+ end
9
+
4
10
  class TestMinitestReporter < MetaMetaMetaTestCase
5
11
 
6
12
  attr_accessor :r, :io
@@ -42,6 +48,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
42
48
  rescue => e
43
49
  e
44
50
  end)
51
+ @et = Minitest::Result.from @et
45
52
  end
46
53
  @et
47
54
  end
@@ -54,12 +61,13 @@ class TestMinitestReporter < MetaMetaMetaTestCase
54
61
  rescue Minitest::Assertion => e
55
62
  e
56
63
  end
64
+ @ft = Minitest::Result.from @ft
57
65
  end
58
66
  @ft
59
67
  end
60
68
 
61
69
  def passing_test
62
- @pt ||= Minitest::Test.new(:woot)
70
+ @pt ||= Minitest::Result.from Minitest::Test.new(:woot)
63
71
  end
64
72
 
65
73
  def skip_test
@@ -70,6 +78,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
70
78
  rescue Minitest::Assertion => e
71
79
  e
72
80
  end
81
+ @st = Minitest::Result.from @st
73
82
  end
74
83
  @st
75
84
  end
@@ -81,13 +90,13 @@ class TestMinitestReporter < MetaMetaMetaTestCase
81
90
  end
82
91
 
83
92
  def test_passed_eh_empty
84
- assert r.passed?
93
+ assert_predicate r, :passed?
85
94
  end
86
95
 
87
96
  def test_passed_eh_failure
88
97
  r.results << fail_test
89
98
 
90
- refute r.passed?
99
+ refute_predicate r, :passed?
91
100
  end
92
101
 
93
102
  SKIP_MSG = "\n\nYou have skipped tests. Run with --verbose for details."
@@ -97,7 +106,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
97
106
 
98
107
  r.results << error_test
99
108
 
100
- refute r.passed?
109
+ refute_predicate r, :passed?
101
110
 
102
111
  r.report
103
112
 
@@ -145,6 +154,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
145
154
  end
146
155
 
147
156
  def test_record_fail
157
+ fail_test = self.fail_test
148
158
  r.record fail_test
149
159
 
150
160
  assert_equal "F", io.string
@@ -154,6 +164,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
154
164
  end
155
165
 
156
166
  def test_record_error
167
+ error_test = self.error_test
157
168
  r.record error_test
158
169
 
159
170
  assert_equal "E", io.string
@@ -163,6 +174,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
163
174
  end
164
175
 
165
176
  def test_record_skip
177
+ skip_test = self.skip_test
166
178
  r.record skip_test
167
179
 
168
180
  assert_equal "S", io.string