@@ -77,6 +77,14 @@ def assert_triggered expected, klass = Minitest::Assertion
77
77
self . send assert_msg , expected , msg
78
78
end
79
79
80
+ def assert_unexpected expected
81
+ expected = Regexp . new expected if String === expected
82
+
83
+ assert_triggered expected , Minitest ::UnexpectedError do
84
+ yield
85
+ end
86
+ end
87
+
80
88
def clean s
81
89
s . gsub ( /^ {6,10}/ , "" )
82
90
end
@@ -603,12 +611,117 @@ def test_assert_output_triggered_out
603
611
end
604
612
end
605
613
606
- def test_assert_output_without_block
614
+ def test_assert_output_no_block
607
615
assert_triggered "assert_output requires a block to capture output." do
608
616
@tc . assert_output "blah"
609
617
end
610
618
end
611
619
620
+ def test_assert_output_nested_assert_uncaught
621
+ @assertion_count = 1
622
+
623
+ assert_triggered "Epic Fail!" do
624
+ @tc . assert_output "blah\n " do
625
+ puts "blah"
626
+ @tc . flunk
627
+ end
628
+ end
629
+ end
630
+
631
+ def test_assert_output_nested_raise
632
+ @assertion_count = 2
633
+
634
+ @tc . assert_output "blah\n " do
635
+ @tc . assert_raises RuntimeError do
636
+ puts "blah"
637
+ raise "boom!"
638
+ end
639
+ end
640
+ end
641
+
642
+ def test_assert_output_nested_raise_bad
643
+ @assertion_count = 0
644
+
645
+ assert_unexpected "boom!" do
646
+ @tc . assert_raises do # 2) bypassed via UnexpectedError
647
+ @tc . assert_output "blah\n " do # 1) captures and raises UnexpectedError
648
+ puts "not_blah"
649
+ raise "boom!"
650
+ end
651
+ end
652
+ end
653
+ end
654
+
655
+ def test_assert_output_nested_raise_mismatch
656
+ # this test is redundant, but illustrative
657
+ @assertion_count = 0
658
+
659
+ assert_unexpected "boom!" do
660
+ @tc . assert_raises RuntimeError do # 2) bypassed via UnexpectedError
661
+ @tc . assert_output "blah\n " do # 1) captures and raises UnexpectedError
662
+ puts "not_blah"
663
+ raise ArgumentError , "boom!"
664
+ end
665
+ end
666
+ end
667
+ end
668
+
669
+ def test_assert_output_nested_throw_caught
670
+ @assertion_count = 2
671
+
672
+ @tc . assert_output "blah\n " do
673
+ @tc . assert_throws :boom! do
674
+ puts "blah"
675
+ throw :boom!
676
+ end
677
+ end
678
+ end
679
+
680
+ def test_assert_output_nested_throw_caught_bad
681
+ @assertion_count = 1 # want 0; can't prevent throw from escaping :(
682
+
683
+ @tc . assert_throws :boom! do # 2) captured via catch
684
+ @tc . assert_output "blah\n " do # 1) bypassed via throw
685
+ puts "not_blah"
686
+ throw :boom!
687
+ end
688
+ end
689
+ end
690
+
691
+ def test_assert_output_nested_throw_mismatch
692
+ @assertion_count = 0
693
+
694
+ assert_unexpected "uncaught throw :boom!" do
695
+ @tc . assert_throws :not_boom! do # 2) captured via assert_throws+rescue
696
+ @tc . assert_output "blah\n " do # 1) bypassed via throw
697
+ puts "not_blah"
698
+ throw :boom!
699
+ end
700
+ end
701
+ end
702
+ end
703
+
704
+ def test_assert_output_uncaught_raise
705
+ @assertion_count = 0
706
+
707
+ assert_unexpected "RuntimeError: boom!" do
708
+ @tc . assert_output "blah\n " do
709
+ puts "not_blah"
710
+ raise "boom!"
711
+ end
712
+ end
713
+ end
714
+
715
+ def test_assert_output_uncaught_throw
716
+ @assertion_count = 0
717
+
718
+ assert_unexpected "uncaught throw :boom!" do
719
+ @tc . assert_output "blah\n " do
720
+ puts "not_blah"
721
+ throw :boom!
722
+ end
723
+ end
724
+ end
612
725
def test_assert_predicate
613
726
@tc . assert_predicate "" , :empty?
614
727
end
@@ -671,6 +784,19 @@ def test_assert_raises_signals
671
784
end
672
785
end
673
786
787
+ def test_assert_raises_throw_nested_bad
788
+ @assertion_count = 0
789
+
790
+ assert_unexpected "RuntimeError: boom!" do
791
+ @tc . assert_raises do
792
+ @tc . assert_throws :blah do
793
+ raise "boom!"
794
+ throw :not_blah
795
+ end
796
+ end
797
+ end
798
+ end
799
+
674
800
##
675
801
# *sigh* This is quite an odd scenario, but it is from real (albeit
676
802
# ugly) test code in ruby-core:
@@ -875,7 +1001,9 @@ def test_assert_throws
875
1001
end
876
1002
877
1003
def test_assert_throws_argument_exception
878
- @tc . assert_raises ArgumentError do
1004
+ @assertion_count = 0
1005
+
1006
+ assert_unexpected "ArgumentError" do
879
1007
@tc . assert_throws :blah do
880
1008
raise ArgumentError
881
1009
end
@@ -891,7 +1019,9 @@ def test_assert_throws_different
891
1019
end
892
1020
893
1021
def test_assert_throws_name_error
894
- @tc . assert_raises NameError do
1022
+ @assertion_count = 0
1023
+
1024
+ assert_unexpected "NameError" do
895
1025
@tc . assert_throws :blah do
896
1026
raise NameError
897
1027
end
0 commit comments