test/test_mini_test.rb in minitest-1.3.0 vs test/test_mini_test.rb in minitest-1.3.1

- old
+ new

@@ -1,12 +1,15 @@ require 'stringio' +require 'pathname' require 'minitest/unit' MiniTest::Unit.autorun -class TestMiniTest < MiniTest::Unit::TestCase +module M; end +class E < StandardError; include M; end +class TestMiniTest < MiniTest::Unit::TestCase def setup srand 42 MiniTest::Unit::TestCase.reset @tu = MiniTest::Unit.new @output = StringIO.new("") @@ -17,26 +20,30 @@ def teardown MiniTest::Unit.output = $stdout Object.send :remove_const, :ATestCase if defined? ATestCase end - BT_MIDDLE = ["./lib/mini/test.rb:165:in `run_test_suites'", - "./lib/mini/test.rb:161:in `each'", - "./lib/mini/test.rb:161:in `run_test_suites'", - "./lib/mini/test.rb:158:in `each'", - "./lib/mini/test.rb:158:in `run_test_suites'", - "./lib/mini/test.rb:139:in `run'", - "./lib/mini/test.rb:106:in `run'"] + pwd = Pathname.new(File.expand_path(Dir.pwd)) + basedir = Pathname.new(File.expand_path(MiniTest::MINI_DIR)) + 'mini' + basedir = basedir.relative_path_from(pwd).to_s + MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}" + BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:165:in `run_test_suites'", + "#{MINITEST_BASE_DIR}/test.rb:161:in `each'", + "#{MINITEST_BASE_DIR}/test.rb:161:in `run_test_suites'", + "#{MINITEST_BASE_DIR}/test.rb:158:in `each'", + "#{MINITEST_BASE_DIR}/test.rb:158:in `run_test_suites'", + "#{MINITEST_BASE_DIR}/test.rb:139:in `run'", + "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"] def test_filter_backtrace # this is a semi-lame mix of relative paths. # I cheated by making the autotest parts not have ./ bt = (["lib/autotest.rb:571:in `add_exception'", "test/test_autotest.rb:62:in `test_add_exception'", - "./lib/mini/test.rb:165:in `__send__'"] + + "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] + BT_MIDDLE + - ["./lib/mini/test.rb:29", + ["#{MINITEST_BASE_DIR}/test.rb:29", "test/test_autotest.rb:422"]) bt = util_expand_bt bt ex = ["lib/autotest.rb:571:in `add_exception'", "test/test_autotest.rb:62:in `test_add_exception'"] @@ -54,22 +61,22 @@ bt end end def test_filter_backtrace_all_unit - bt = (["./lib/mini/test.rb:165:in `__send__'"] + + bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] + BT_MIDDLE + - ["./lib/mini/test.rb:29"]) + ["#{MINITEST_BASE_DIR}/test.rb:29"]) ex = bt.clone fu = MiniTest::filter_backtrace(bt) assert_equal ex, fu end def test_filter_backtrace_unit_starts - bt = (["./lib/mini/test.rb:165:in `__send__'"] + + bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] + BT_MIDDLE + - ["./lib/mini/test.rb:29", + ["#{MINITEST_BASE_DIR}/mini/test.rb:29", "-e:1"]) bt = util_expand_bt bt ex = ["-e:1"] @@ -81,10 +88,11 @@ exception = MiniTest::Assertion.new "Oh no!" exception.set_backtrace ["unhappy"] assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception) assert_equal 1, @tu.failures assert_match(/^Failure.*Oh no!/m, @tu.report.first) + assert_match("method_name(SomeClass) [unhappy]", @tu.report.first) end def test_class_puke_with_failure_and_flunk_in_backtrace exception = begin MiniTest::Unit::TestCase.new('fake tc').flunk @@ -93,10 +101,76 @@ end assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception) refute @tu.report.any?{|line| line =~ /in .flunk/} end + def test_class_puke_with_assertion_failed_and_long_backtrace + bt = (["test/test_some_class.rb:615:in `method_name'", + "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'", + "test/test_some_class.rb:615:in `each'", + "test/test_some_class.rb:614:in `test_method_name'", + "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] + + BT_MIDDLE + + ["#{MINITEST_BASE_DIR}/test.rb:29"]) + bt = util_expand_bt bt + + ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first + + exception = MiniTest::Assertion.new "Oh no!" + exception.set_backtrace bt + assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception) + assert_equal 1, @tu.failures + assert_match(/^Failure.*Oh no!/m, @tu.report.first) + assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first) + end + + def test_class_puke_with_assertion_failed_and_user_defined_assertions + bt = (["lib/test/my/util.rb:16:in `another_method_name'", + "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'", + "lib/test/my/util.rb:15:in `block in assert_something'", + "lib/test/my/util.rb:14:in `each'", + "lib/test/my/util.rb:14:in `assert_something'", + "test/test_some_class.rb:615:in `each'", + "test/test_some_class.rb:614:in `test_method_name'", + "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] + + BT_MIDDLE + + ["#{MINITEST_BASE_DIR}/test.rb:29"]) + bt = util_expand_bt bt + + ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first + + exception = MiniTest::Assertion.new "Oh no!" + exception.set_backtrace bt + assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception) + assert_equal 1, @tu.failures + assert_match(/^Failure.*Oh no!/m, @tu.report.first) + assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first) + end + + def test_class_puke_with_flunk_and_user_defined_assertions + bt = (["lib/test/my/util.rb:16:in `flunk'", + "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'", + "lib/test/my/util.rb:15:in `block in assert_something'", + "lib/test/my/util.rb:14:in `each'", + "lib/test/my/util.rb:14:in `assert_something'", + "test/test_some_class.rb:615:in `each'", + "test/test_some_class.rb:614:in `test_method_name'", + "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] + + BT_MIDDLE + + ["#{MINITEST_BASE_DIR}/test.rb:29"]) + bt = util_expand_bt bt + + ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first + + exception = MiniTest::Assertion.new "Oh no!" + exception.set_backtrace bt + assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception) + assert_equal 1, @tu.failures + assert_match(/^Failure.*Oh no!/m, @tu.report.first) + assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first) + end + def test_class_puke_with_non_failure_exception exception = Exception.new("Oh no again!") assert_equal 'E', @tu.puke('SomeClass', 'method_name', exception) assert_equal 1, @tu.errors assert_match(/^Exception.*Oh no again!/m, @tu.report.first) @@ -240,14 +314,14 @@ 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips " output = @output.string.sub(/Finished in .*/, "Finished in 0.00") output.sub!(/Loaded suite .*/, 'Loaded suite blah') - output.sub!(/[\w\/\.]+:\d+/, 'FILE:LINE') + output.sub!(/^(\s+)(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+:/o, '\1FILE:LINE:') + output.sub!(/\[(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+\]/o, '[FILE:LINE]') assert_equal(expected, output) - end - + end def test_run_failing_filtered tc = Class.new(MiniTest::Unit::TestCase) do def test_something assert true end @@ -418,11 +492,11 @@ @tc.assert_includes [true], true end def test_assert_includes_triggered - @assertion_count = 4 + @assertion_count = 3 e = @tc.assert_raises MiniTest::Assertion do @tc.assert_includes [true], false end @@ -450,17 +524,38 @@ end end def test_assert_match @assertion_count = 2 - @tc.assert_match "blah blah blah", /\w+/ + @tc.assert_match(/\w+/, "blah blah blah") end + def test_assert_match_object + @assertion_count = 2 + + pattern = Object.new + def pattern.=~(other) true end + + @tc.assert_match pattern, 5 + end + + def test_assert_match_object_triggered + @assertion_count = 2 + + pattern = Object.new + def pattern.=~(other) false end + def pattern.inspect; "<<Object>>" end + + util_assert_triggered 'Expected <<Object>> to match 5.' do + @tc.assert_match pattern, 5 + end + end + def test_assert_match_triggered @assertion_count = 2 util_assert_triggered 'Expected /\d+/ to match "blah blah blah".' do - @tc.assert_match "blah blah blah", /\d+/ + @tc.assert_match(/\d+/, "blah blah blah") end end def test_assert_nil @tc.assert_nil nil @@ -481,35 +576,39 @@ @tc.assert_operator 2, :<, 1 end end def test_assert_raises - @assertion_count = 2 - @tc.assert_raises RuntimeError do raise "blah" end end - def test_assert_raises_triggered_different - @assertion_count = 2 + def test_assert_raises_module + @tc.assert_raises M do + raise E + end + end + def test_assert_raises_triggered_different e = assert_raises MiniTest::Assertion do @tc.assert_raises RuntimeError do raise SyntaxError, "icky" end end - expected = "<[RuntimeError]> exception expected, not + expected = "[RuntimeError] exception expected, not Class: <SyntaxError> Message: <\"icky\"> ---Backtrace--- FILE:LINE:in `test_assert_raises_triggered_different' ----------------. -Expected [RuntimeError] to include SyntaxError." +---------------" - assert_equal expected, expected.gsub(/[\w\/\.]+:\d+/, 'FILE:LINE') + actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE') + actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION =~ /^1\.9/ + + assert_equal expected, actual end def test_assert_raises_triggered_none e = assert_raises MiniTest::Assertion do @tc.assert_raises MiniTest::Assertion do @@ -520,10 +619,30 @@ expected = "MiniTest::Assertion expected but nothing was raised." assert_equal expected, e.message end + def test_assert_raises_triggered_subclass + e = assert_raises MiniTest::Assertion do + @tc.assert_raises StandardError do + raise E + end + end + + expected = "[StandardError] exception expected, not +Class: <E> +Message: <\"E\"> +---Backtrace--- +FILE:LINE:in `test_assert_raises_triggered_subclass' +---------------" + + actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE') + actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION =~ /^1\.9/ + + assert_equal expected, actual + end + def test_assert_respond_to @tc.assert_respond_to "blah", :empty? end def test_assert_respond_to_triggered @@ -725,11 +844,11 @@ @tc.refute_includes [true], false end def test_refute_includes_triggered - @assertion_count = 4 + @assertion_count = 3 e = @tc.assert_raises MiniTest::Assertion do @tc.refute_includes [true], true end @@ -756,15 +875,46 @@ @tc.refute_kind_of String, "blah" end end def test_refute_match - @tc.refute_match "blah blah blah", /\d+/ + @assertion_count = 2 + @tc.refute_match(/\d+/, "blah blah blah") end + def test_refute_match_object + @assertion_count = 2 + @tc.refute_match Object.new, 5 # default #=~ returns false + end + + def test_assert_object_triggered + @assertion_count = 2 + + pattern = Object.new + def pattern.=~(other) false end + def pattern.inspect; "<<Object>>" end + + util_assert_triggered 'Expected <<Object>> to match 5.' do + @tc.assert_match pattern, 5 + end + end + + def test_refute_match_object_triggered + @assertion_count = 2 + + pattern = Object.new + def pattern.=~(other) true end + def pattern.inspect; "<<Object>>" end + + util_assert_triggered 'Expected <<Object>> to not match 5.' do + @tc.refute_match pattern, 5 + end + end + def test_refute_match_triggered + @assertion_count = 2 util_assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do - @tc.refute_match "blah blah blah", /\w+/ + @tc.refute_match(/\w+/, "blah blah blah") end end def test_refute_nil @tc.refute_nil 42