test/minitest/test_minitest_assertions.rb in minitest-5.24.1 vs test/minitest/test_minitest_assertions.rb in minitest-5.25.0

- old
+ new

@@ -1,20 +1,16 @@ -# encoding: UTF-8 - require "minitest/autorun" require_relative "metametameta" -if defined? Encoding then - e = Encoding.default_external - if e != Encoding::UTF_8 then - warn "" - warn "" - warn "NOTE: External encoding #{e} is not UTF-8. Tests WILL fail." - warn " Run tests with `RUBYOPT=-Eutf-8 rake` to avoid errors." - warn "" - warn "" - end +e = Encoding.default_external +if e != Encoding::UTF_8 then + warn "" + warn "" + warn "NOTE: External encoding #{e} is not UTF-8. Tests WILL fail." + warn " Run tests with `RUBYOPT=-Eutf-8 rake` to avoid errors." + warn "" + warn "" end SomeError = Class.new Exception unless defined? MyModule then @@ -25,12 +21,10 @@ class TestMinitestAssertions < Minitest::Test # do not call parallelize_me! - teardown accesses @tc._assertions # which is not threadsafe. Nearly every method in here is an # assertion test so it isn't worth splitting it out further. - RUBY18 = !defined? Encoding - # not included in JRuby RE_LEVELS = /\(\d+ levels\) / class DummyTest include Minitest::Assertions @@ -77,14 +71,10 @@ assert_triggered expected, Minitest::UnexpectedError do yield end end - def clean s - s.gsub(/^ {6,10}/, "") - end - def non_verbose orig_verbose = $VERBOSE $VERBOSE = false yield @@ -133,40 +123,47 @@ def test_assert_equal @tc.assert_equal 1, 1 end def test_assert_equal_different_collection_array_hex_invisible - object1 = Object.new - object2 = Object.new - msg = "No visible difference in the Array#inspect output. + exp = Object.new + act = Object.new + msg = <<~EOM.chomp + No visible difference in the Array#inspect output. You should look at the implementation of #== on Array or its members. - [#<Object:0xXXXXXX>]".gsub(/^ +/, "") + [#<Object:0xXXXXXX>] + EOM assert_triggered msg do - @tc.assert_equal [object1], [object2] + @tc.assert_equal [exp], [act] end end def test_assert_equal_different_collection_hash_hex_invisible - h1, h2 = {}, {} - h1[1] = Object.new - h2[1] = Object.new - msg = "No visible difference in the Hash#inspect output. + exp, act = {}, {} + exp[1] = Object.new + act[1] = Object.new + act_obj = act[1] + # TODO: switch to endless when 2.7 is dropped + act_obj.define_singleton_method(:inspect) { "#<Object:0xXXXXXX>" } + msg = <<~EOM.chomp % [act] + No visible difference in the Hash#inspect output. You should look at the implementation of #== on Hash or its members. - {1=>#<Object:0xXXXXXX>}".gsub(/^ +/, "") + %p + EOM assert_triggered msg do - @tc.assert_equal h1, h2 + @tc.assert_equal exp, act end end def test_assert_equal_different_diff_deactivated without_diff do assert_triggered util_msg("haha" * 10, "blah" * 10) do - o1 = "haha" * 10 - o2 = "blah" * 10 + exp = "haha" * 10 + act = "blah" * 10 - @tc.assert_equal o1, o2 + @tc.assert_equal exp, act end end end def test_assert_equal_different_message @@ -184,82 +181,88 @@ def test_assert_equal_different_hex c = Class.new do def initialize s; @name = s; end end - o1 = c.new "a" - o2 = c.new "b" - msg = clean <<-EOS + exp = c.new "a" + act = c.new "b" + msg = <<~EOS --- expected +++ actual @@ -1 +1 @@ - -#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"a\"> - +#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"b\"> + -#<#<Class:0xXXXXXX>:0xXXXXXX @name="a"> + +#<#<Class:0xXXXXXX>:0xXXXXXX @name="b"> EOS assert_triggered msg do - @tc.assert_equal o1, o2 + @tc.assert_equal exp, act end end def test_assert_equal_different_hex_invisible - o1 = Object.new - o2 = Object.new + exp = Object.new + act = Object.new - msg = "No visible difference in the Object#inspect output. + msg = <<~EOM.chomp + No visible difference in the Object#inspect output. You should look at the implementation of #== on Object or its members. - #<Object:0xXXXXXX>".gsub(/^ +/, "") + #<Object:0xXXXXXX> + EOM assert_triggered msg do - @tc.assert_equal o1, o2 + @tc.assert_equal exp, act end end def test_assert_equal_different_long - msg = "--- expected + msg = <<~EOM + --- expected +++ actual @@ -1 +1 @@ - -\"hahahahahahahahahahahahahahahahahahahaha\" - +\"blahblahblahblahblahblahblahblahblahblah\" - ".gsub(/^ +/, "") + -"hahahahahahahahahahahahahahahahahahahaha" + +"blahblahblahblahblahblahblahblahblahblah" + EOM assert_triggered msg do - o1 = "haha" * 10 - o2 = "blah" * 10 + exp = "haha" * 10 + act = "blah" * 10 - @tc.assert_equal o1, o2 + @tc.assert_equal exp, act end end def test_assert_equal_different_long_invisible - msg = "No visible difference in the String#inspect output. + msg = <<~EOM.chomp + No visible difference in the String#inspect output. You should look at the implementation of #== on String or its members. - \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "") + "blahblahblahblahblahblahblahblahblahblah" + EOM assert_triggered msg do - o1 = "blah" * 10 - o2 = "blah" * 10 - def o1.== _ + exp = "blah" * 10 + act = "blah" * 10 + def exp.== _ false end - @tc.assert_equal o1, o2 + @tc.assert_equal exp, act end end def test_assert_equal_different_long_msg - msg = "message. + msg = <<~EOM + message. --- expected +++ actual @@ -1 +1 @@ - -\"hahahahahahahahahahahahahahahahahahahaha\" - +\"blahblahblahblahblahblahblahblahblahblah\" - ".gsub(/^ +/, "") + -"hahahahahahahahahahahahahahahahahahahaha" + +"blahblahblahblahblahblahblahblahblahblah" + EOM assert_triggered msg do - o1 = "haha" * 10 - o2 = "blah" * 10 - @tc.assert_equal o1, o2, "message" + exp = "haha" * 10 + act = "blah" * 10 + @tc.assert_equal exp, act, "message" end end def test_assert_equal_different_short assert_triggered util_msg(1, 2) do @@ -279,11 +282,11 @@ @tc.assert_equal "a\nb", "a\nc" end end def test_assert_equal_does_not_allow_lhs_nil - if Minitest::VERSION =~ /^6/ then + if Minitest::VERSION >= "6" then warn "Time to strip the MT5 test" @assertion_count += 1 assert_triggered(/Use assert_nil if expecting nil/) do @tc.assert_equal nil, nil @@ -303,33 +306,27 @@ @tc.assert_equal nil, false end end def test_assert_equal_string_bug791 - exp = <<-'EOF'.gsub(/^ {10}/, "") # note single quotes - --- expected - +++ actual - @@ -1,2 +1 @@ - -"\\n - -" - +"\\\" - EOF - - exp = "Expected: \"\\\\n\"\n Actual: \"\\\\\"" + exp = <<~EOM.chomp + Expected: "\\\\n" + Actual: "\\\\" + EOM assert_triggered exp do @tc.assert_equal "\\n", "\\" end end def test_assert_equal_string_both_escaped_unescaped_newlines - msg = <<-EOM.gsub(/^ {10}/, "") + msg = <<~EOM --- expected +++ actual @@ -1,2 +1 @@ - -\"A\\n - -B\" - +\"A\\n\\\\nB\" + -"A\\n + -B" + +"A\\n\\\\nB" EOM assert_triggered msg do exp = "A\\nB" act = "A\n\\nB" @@ -337,11 +334,11 @@ @tc.assert_equal exp, act end end def test_assert_equal_string_encodings - msg = <<-EOM.gsub(/^ {10}/, "") + msg = <<~EOM --- expected +++ actual @@ -1,3 +1,3 @@ -# encoding: UTF-8 -# valid: false @@ -349,18 +346,18 @@ +# valid: true "bad-utf8-\\xF1.txt" EOM assert_triggered msg do - x = "bad-utf8-\xF1.txt" - y = x.dup.force_encoding "binary" # TODO: switch to .b when 1.9 dropped - @tc.assert_equal x, y + exp = "bad-utf8-\xF1.txt" + act = exp.dup.b + @tc.assert_equal exp, act end - end unless RUBY18 + end def test_assert_equal_string_encodings_both_different - msg = <<-EOM.gsub(/^ {10}/, "") + msg = <<~EOM --- expected +++ actual @@ -1,3 +1,3 @@ -# encoding: US-ASCII -# valid: false @@ -368,18 +365,18 @@ +# valid: true "bad-utf8-\\xF1.txt" EOM assert_triggered msg do - x = "bad-utf8-\xF1.txt".dup.force_encoding Encoding::ASCII - y = x.dup.force_encoding "binary" # TODO: switch to .b when 1.9 dropped - @tc.assert_equal x, y + exp = "bad-utf8-\xF1.txt".dup.force_encoding Encoding::ASCII + act = exp.dup.b + @tc.assert_equal exp, act end - end unless RUBY18 + end def test_assert_equal_unescape_newlines - msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc + msg = <<~'EOM' # NOTE single quotes on heredoc --- expected +++ actual @@ -1,2 +1,2 @@ -"hello +"hello\n @@ -427,11 +424,11 @@ @tc.assert_in_epsilon 10_000, 9990 end end def test_assert_in_epsilon_triggered_negative_case - x = (RUBY18 and not maglev?) ? "0.1" : "0.100000xxx" + x = "0.100000xxx" y = "0.1" assert_triggered "Expected |-1.1 - -1| (#{x}) to be <= #{y}." do @tc.assert_in_epsilon(-1.1, -1, 0.1) end end @@ -717,10 +714,11 @@ puts "not_blah" throw :boom! end end end + def test_assert_predicate @tc.assert_predicate "", :empty? end def test_assert_predicate_triggered @@ -746,21 +744,21 @@ @tc.assert_raises do raise SomeError, "blah" end end - expected = clean <<-EOM.chomp + expected = <<~EOM.chomp [StandardError] exception expected, not Class: <SomeError> - Message: <\"blah\"> + Message: <"blah"> ---Backtrace--- - FILE:LINE:in \'block in test_assert_raises_default_triggered\' + FILE:LINE:in 'block in test_assert_raises_default_triggered' --------------- EOM actual = e.message.gsub(/^.+:\d+/, "FILE:LINE") - actual.gsub!(RE_LEVELS, "") unless jruby? + actual.gsub! RE_LEVELS, "" unless jruby? actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ") assert_equal expected, actual end @@ -826,21 +824,21 @@ @tc.assert_raises SomeError do raise AnError, "some message" end end - expected = clean <<-EOM + expected = <<~EOM [SomeError] exception expected, not Class: <AnError> - Message: <\"some message\"> + Message: <"some message"> ---Backtrace--- - FILE:LINE:in \'block in test_assert_raises_subclass_triggered\' + FILE:LINE:in 'block in test_assert_raises_subclass_triggered' --------------- EOM actual = e.message.gsub(/^.+:\d+/, "FILE:LINE") - actual.gsub!(RE_LEVELS, "") unless jruby? + actual.gsub! RE_LEVELS, "" unless jruby? actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ") assert_equal expected.chomp, actual end @@ -849,21 +847,21 @@ @tc.assert_raises RuntimeError do raise SyntaxError, "icky" end end - expected = clean <<-EOM.chomp + expected = <<~EOM.chomp [RuntimeError] exception expected, not Class: <SyntaxError> - Message: <\"icky\"> + Message: <"icky"> ---Backtrace--- - FILE:LINE:in \'block in test_assert_raises_triggered_different\' + FILE:LINE:in 'block in test_assert_raises_triggered_different' --------------- EOM actual = e.message.gsub(/^.+:\d+/, "FILE:LINE") - actual.gsub!(RE_LEVELS, "") unless jruby? + actual.gsub! RE_LEVELS, "" unless jruby? actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ") assert_equal expected, actual end @@ -872,22 +870,22 @@ @tc.assert_raises RuntimeError, "XXX" do raise SyntaxError, "icky" end end - expected = clean <<-EOM + expected = <<~EOM XXX. [RuntimeError] exception expected, not Class: <SyntaxError> - Message: <\"icky\"> + Message: <"icky"> ---Backtrace--- - FILE:LINE:in \'block in test_assert_raises_triggered_different_msg\' + FILE:LINE:in 'block in test_assert_raises_triggered_different_msg' --------------- EOM actual = e.message.gsub(/^.+:\d+/, "FILE:LINE") - actual.gsub!(RE_LEVELS, "") unless jruby? + actual.gsub! RE_LEVELS, "" unless jruby? actual.gsub!(/[`']block in (?:TestMinitestAssertions#)?/, "'block in ") assert_equal expected.chomp, actual end @@ -1152,12 +1150,12 @@ def test_capture_subprocess_io @assertion_count = 0 non_verbose do out, err = capture_subprocess_io do - system("echo hi") - system("echo bye! 1>&2") + system "echo hi" + system "echo bye! 1>&2" end assert_equal "hi\n", out assert_equal "bye!", err.strip end @@ -1546,13 +1544,15 @@ assert_equal exp, act end end def test_diff_equal - msg = "No visible difference in the String#inspect output. + msg = <<~EOM.chomp + No visible difference in the String#inspect output. You should look at the implementation of #== on String or its members. - \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "") + "blahblahblahblahblahblahblahblahblahblah" + EOM o1 = "blah" * 10 o2 = "blah" * 10 def o1.== _ false @@ -1560,11 +1560,11 @@ assert_equal msg, diff(o1, o2) end def test_diff_str_mixed - msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc + msg = <<~'EOM' # NOTE single quotes on heredoc --- expected +++ actual @@ -1 +1 @@ -"A\\n\nB" +"A\n\\nB" @@ -1575,11 +1575,11 @@ assert_equal msg, diff(exp, act) end def test_diff_str_multiline - msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc + msg = <<~EOM --- expected +++ actual @@ -1,2 +1,2 @@ "A -B" @@ -1591,10 +1591,10 @@ assert_equal msg, diff(exp, act) end def test_diff_str_simple - msg = <<-'EOM'.gsub(/^ {10}/, "").chomp # NOTE single quotes on heredoc + msg = <<~EOM.chomp Expected: "A" Actual: "B" EOM exp = "A"