diff options
Diffstat (limited to 'test/minitest/test_minitest_unit.rb')
-rw-r--r-- | test/minitest/test_minitest_unit.rb | 398 |
1 files changed, 280 insertions, 118 deletions
diff --git a/test/minitest/test_minitest_unit.rb b/test/minitest/test_minitest_unit.rb index 65b912e986..199fa0347f 100644 --- a/test/minitest/test_minitest_unit.rb +++ b/test/minitest/test_minitest_unit.rb @@ -6,7 +6,7 @@ ###################################################################### require 'pathname' -require_relative 'metametameta' +require 'minitest/metametameta' module MyModule; end class AnError < StandardError; include MyModule; end @@ -24,30 +24,13 @@ class TestMiniTestUnit < MetaMetaMetaTestCase "#{MINITEST_BASE_DIR}/test.rb:139:in `run'", "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"] - def test_wtf - $hook_value = nil - - capture_io do # don't care about deprecation - MiniTest::Unit::TestCase.add_setup_hook do - $hook_value = 42 - end - end - - run_setup_hooks - - assert_equal 42, $hook_value - assert_equal [Proc], MiniTest::Unit::TestCase.setup_hooks.map(&:class) - MiniTest::Unit::TestCase.reset_setup_teardown_hooks - assert_equal [], MiniTest::Unit::TestCase.setup_hooks.map(&:class) - end - def test_class_puke_with_assertion_failed 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) + assert_match("SomeClass#method_name [unhappy]", @tu.report.first) end def test_class_puke_with_assertion_failed_and_long_backtrace @@ -67,7 +50,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase 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) + assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first) end def test_class_puke_with_assertion_failed_and_user_defined_assertions @@ -90,7 +73,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase 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) + assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first) end def test_class_puke_with_failure_and_flunk_in_backtrace @@ -123,7 +106,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase 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) + assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first) end def test_class_puke_with_non_failure_exception @@ -179,42 +162,22 @@ class TestMiniTestUnit < MetaMetaMetaTestCase assert_instance_of MiniTest::Unit, MiniTest::Unit.runner end - def with_overridden_include - Class.class_eval do - def inherited_with_hacks klass - throw :inherited_hook - end - - alias inherited_without_hacks inherited - alias inherited inherited_with_hacks - alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros - end - - yield - ensure - Class.class_eval do - alias inherited inherited_without_hacks - undef_method :inherited_with_hacks - undef_method :inherited_without_hacks + def test_passed_eh_teardown_good + test_class = Class.new MiniTest::Unit::TestCase do + def teardown; assert true; end + def test_omg; assert true; end end - refute_respond_to Class, :inherited_with_hacks - refute_respond_to Class, :inherited_without_hacks - end - - def test_inherited_hook_plays_nice_with_others - with_overridden_include do - assert_throws :inherited_hook do - Class.new MiniTest::Unit::TestCase - end - end + test = test_class.new :test_omg + test.run @tu + assert test.passed? end - def test_passed_eh_teardown_good + def test_passed_eh_teardown_skipped test_class = Class.new MiniTest::Unit::TestCase do def teardown; assert true; end - def test_omg; assert true; end + def test_omg; skip "bork"; end end test = test_class.new :test_omg @@ -242,6 +205,40 @@ class TestMiniTestUnit < MetaMetaMetaTestCase end end +class TestMiniTestUnitInherited < MetaMetaMetaTestCase + def with_overridden_include + Class.class_eval do + def inherited_with_hacks klass + throw :inherited_hook + end + + alias inherited_without_hacks inherited + alias inherited inherited_with_hacks + alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros + end + + yield + ensure + Class.class_eval do + alias inherited inherited_without_hacks + + undef_method :inherited_with_hacks + undef_method :inherited_without_hacks + end + + refute_respond_to Class, :inherited_with_hacks + refute_respond_to Class, :inherited_without_hacks + end + + def test_inherited_hook_plays_nice_with_others + with_overridden_include do + assert_throws :inherited_hook do + Class.new MiniTest::Unit::TestCase + end + end + end +end + class TestMiniTestRunner < MetaMetaMetaTestCase # do not parallelize this suite... it just can't handle it. @@ -297,9 +294,9 @@ class TestMiniTestRunner < MetaMetaMetaTestCase Finished tests in 0.00 1) Error: - test_error(#<Class:0xXXX>): + #<Class:0xXXX>#test_error: RuntimeError: unhandled exception - FILE:LINE:in `test_error' + FILE:LINE:in \`test_error\' 2 tests, 1 assertions, 0 failures, 1 errors, 0 skips EOM @@ -324,9 +321,9 @@ class TestMiniTestRunner < MetaMetaMetaTestCase Finished tests in 0.00 1) Error: - test_something(#<Class:0xXXX>): + #<Class:0xXXX>#test_something: RuntimeError: unhandled exception - FILE:LINE:in `teardown' + FILE:LINE:in \`teardown\' 1 tests, 1 assertions, 0 failures, 1 errors, 0 skips EOM @@ -351,7 +348,7 @@ class TestMiniTestRunner < MetaMetaMetaTestCase Finished tests in 0.00 1) Failure: - test_failure(#<Class:0xXXX>) [FILE:LINE]: + #<Class:0xXXX>#test_failure [FILE:LINE]: Failed assertion, no message given. 2 tests, 2 assertions, 1 failures, 0 errors, 0 skips @@ -382,6 +379,65 @@ class TestMiniTestRunner < MetaMetaMetaTestCase assert_report expected, %w[--name /some|thing/ --seed 42] end + def assert_filtering name, expected, a = false + args = %W[--name #{name} --seed 42] + + alpha = Class.new MiniTest::Unit::TestCase do + define_method :test_something do + assert a + end + end + Object.const_set(:Alpha, alpha) + + beta = Class.new MiniTest::Unit::TestCase do + define_method :test_something do + assert true + end + end + Object.const_set(:Beta, beta) + + assert_report expected, args + ensure + Object.send :remove_const, :Alpha + Object.send :remove_const, :Beta + end + + def test_run_filtered_including_suite_name + expected = clean <<-EOM + . + + Finished tests in 0.00 + + 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips + EOM + + assert_filtering "/Beta#test_something/", expected + end + + def test_run_filtered_including_suite_name_string + expected = clean <<-EOM + . + + Finished tests in 0.00 + + 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips + EOM + + assert_filtering "Beta#test_something", expected + end + + def test_run_filtered_string_method_only + expected = clean <<-EOM + .. + + Finished tests in 0.00 + + 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips + EOM + + assert_filtering "test_something", expected, :pass + end + def test_run_passing Class.new MiniTest::Unit::TestCase do def test_something @@ -441,7 +497,7 @@ class TestMiniTestRunner < MetaMetaMetaTestCase Finished tests in 0.00 1) Skipped: - test_skip(#<Class:0xXXX>) [FILE:LINE]: + #<Class:0xXXX>#test_skip [FILE:LINE]: not yet 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips @@ -508,7 +564,13 @@ class TestMiniTestRunner < MetaMetaMetaTestCase end end + def test_parallel_each_size + assert_equal 0, ParallelEach.new([]).size + end + def test_run_parallel + skip "I don't have ParallelEach debugged yet" if maglev? + test_count = 2 test_latch = Latch.new test_count main_latch = Latch.new @@ -637,12 +699,12 @@ class TestMiniTestUnitOrder < MetaMetaMetaTestCase def test_setup_and_teardown_survive_inheritance call_order = [] - parent = Class.new MiniTest::Spec do - before do + parent = Class.new MiniTest::Unit::TestCase do + define_method :setup do call_order << :setup_method end - after do + define_method :teardown do call_order << :teardown_method end @@ -665,7 +727,9 @@ class TestMiniTestUnitOrder < MetaMetaMetaTestCase end class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase - parallelize_me! if ENV["PARALLEL"] + # 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 @@ -681,7 +745,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase def teardown assert_equal(@assertion_count, @tc._assertions, - "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc._assertions + "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc.passed? end def non_verbose @@ -711,30 +775,6 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end end - def test_assert_block - exp = ["NOTE: MiniTest::Unit::TestCase#assert_block is deprecated,", - "use assert. It will be removed on 2013-01-01."].join " " - - out, err = capture_io do - @tc.assert_block do - true - end - end - - assert_equal "", out - assert_match exp, err - end - - def test_assert_block_triggered - assert_output do - util_assert_triggered "blah.\nExpected block to return true value." do - @tc.assert_block "blah" do - false - end - end - end - end - def test_assert_empty @assertion_count = 2 @@ -778,6 +818,8 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_assert_equal_different_diff_deactivated + skip "https://github.com/MagLev/maglev/issues/209" if maglev? + without_diff do util_assert_triggered util_msg("haha" * 10, "blah" * 10) do o1 = "haha" * 10 @@ -890,8 +932,17 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1 end + def test_delta_consistency + @tc.assert_in_delta 0, 1, 1 + + util_assert_triggered "Expected |0 - 1| (1) to not be <= 1." do + @tc.refute_in_delta 0, 1, 1 + end + end + def test_assert_in_delta_triggered - util_assert_triggered 'Expected |0.0 - 0.001| (0.001) to be < 1.0e-06.' do + x = maglev? ? "9.999999xxxe-07" : "1.0e-06" + util_assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001 end end @@ -913,15 +964,25 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_in_epsilon(-10000, -9991) end + def test_epsilon_consistency + @tc.assert_in_epsilon 1.0, 1.001 + + msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001." + util_assert_triggered msg do + @tc.refute_in_epsilon 1.0, 1.001 + end + end + def test_assert_in_epsilon_triggered - util_assert_triggered 'Expected |10000 - 9990| (10) to be < 9.99.' do + util_assert_triggered 'Expected |10000 - 9990| (10) to be <= 9.99.' do @tc.assert_in_epsilon 10000, 9990 end end def test_assert_in_epsilon_triggered_negative_case - x = RUBY18 ? "0.1" : "0.10000000000000009" - util_assert_triggered "Expected |-1.1 - -1| (#{x}) to be < 0.1." do + x = (RUBY18 and not maglev?) ? "0.1" : "0.100000xxx" + y = maglev? ? "0.100000xxx" : "0.1" + util_assert_triggered "Expected |-1.1 - -1| (#{x}) to be <= #{y}." do @tc.assert_in_epsilon(-1.1, -1, 0.1) end end @@ -1149,7 +1210,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase Class: <SyntaxError> Message: <\"icky\"> ---Backtrace--- - FILE:LINE:in `test_assert_raises_triggered_different' + FILE:LINE:in \`test_assert_raises_triggered_different\' --------------- EOM @@ -1172,7 +1233,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase Class: <SyntaxError> Message: <\"icky\"> ---Backtrace--- - FILE:LINE:in `test_assert_raises_triggered_different_msg' + FILE:LINE:in \`test_assert_raises_triggered_different_msg\' --------------- EOM @@ -1218,7 +1279,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase Class: <AnError> Message: <\"AnError\"> ---Backtrace--- - FILE:LINE:in `test_assert_raises_triggered_subclass' + FILE:LINE:in \`test_assert_raises_triggered_subclass\' --------------- EOM @@ -1326,7 +1387,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase non_verbose do out, err = capture_io do puts 'hi' - warn 'bye!' + $stderr.puts 'bye!' end assert_equal "hi\n", out @@ -1357,7 +1418,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase # These don't have corresponding refutes _on purpose_. They're # useless and will never be added, so don't bother. - ignores = %w[assert_block assert_output assert_raises assert_send + ignores = %w[assert_output assert_raises assert_send assert_silent assert_throws] # These are test/unit methods. I'm not actually sure why they're still here @@ -1372,24 +1433,6 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase assert_empty asserts.map { |n| n.sub(/^assert/, 'refute') } - refutes end - def test_expectation - @assertion_count = 2 - - @tc.assert_equal true, 1.must_equal(1) - end - - def test_expectation_triggered - util_assert_triggered "Expected: 2\n Actual: 1" do - 1.must_equal 2 - end - end - - def test_expectation_with_a_message - util_assert_triggered "Expected: 2\n Actual: 1" do - 1.must_equal 2, '' - end - end - def test_flunk util_assert_triggered 'Epic Fail!' do @tc.flunk @@ -1409,6 +1452,36 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase assert_equal "blah2.", @tc.message("") { "blah2" }.call assert_equal "blah1.\nblah2.", @tc.message(:blah1) { "blah2" }.call assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call + + message = proc { "blah1" } + assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call + + message = @tc.message { "blah1" } + assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call + end + + def test_message_message + util_assert_triggered "whoops.\nExpected: 1\n Actual: 2" do + @tc.assert_equal 1, 2, message { "whoops" } + end + end + + def test_message_lambda + util_assert_triggered "whoops.\nExpected: 1\n Actual: 2" do + @tc.assert_equal 1, 2, lambda { "whoops" } + end + end + + def test_message_deferred + @assertion_count, var = 0, nil + + msg = message { var = "blah" } + + assert_nil var + + msg.call + + assert_equal "blah", var end def test_pass @@ -1455,18 +1528,19 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_refute_in_delta_triggered - util_assert_triggered 'Expected |0.0 - 0.001| (0.001) to not be < 0.1.' do + x = maglev? ? "0.100000xxx" : "0.1" + util_assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1 end end def test_refute_in_epsilon - @tc.refute_in_epsilon 10000, 9990 + @tc.refute_in_epsilon 10000, 9990-1 end def test_refute_in_epsilon_triggered - util_assert_triggered 'Expected |10000 - 9991| (9) to not be < 10.0.' do - @tc.refute_in_epsilon 10000, 9991 + util_assert_triggered 'Expected |10000 - 9990| (10) to not be <= 10.0.' do + @tc.refute_in_epsilon 10000, 9990 fail end end @@ -1613,7 +1687,12 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end srand 42 - expected = %w(test_test2 test_test1 test_test3) + expected = case + when maglev? then + %w(test_test2 test_test3 test_test1) + else + %w(test_test2 test_test1 test_test3) + end assert_equal expected, sample_test_case.test_methods end @@ -1660,6 +1739,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase msg = e.message.sub(/(---Backtrace---).*/m, '\1') msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)') + msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform assert_equal expected, msg end @@ -1703,3 +1783,85 @@ class TestMiniTestGuard < MiniTest::Unit::TestCase assert self.windows? "mswin" end end + +class TestMiniTestUnitRecording < MetaMetaMetaTestCase + # do not parallelize this suite... it just can't handle it. + + def assert_run_record(*expected, &block) + def @tu.record suite, method, assertions, time, error + recording[method] << error + end + + def @tu.recording + @recording ||= Hash.new { |h,k| h[k] = [] } + end + + MiniTest::Unit.runner = @tu + + Class.new MiniTest::Unit::TestCase, &block + + with_output do + @tu.run + end + + recorded = @tu.recording.fetch("test_method").map(&:class) + + assert_equal expected, recorded + end + + def test_record_passing + assert_run_record NilClass do + def test_method + assert true + end + end + end + + def test_record_failing + assert_run_record MiniTest::Assertion do + def test_method + assert false + end + end + end + + def test_record_error + assert_run_record RuntimeError do + def test_method + raise "unhandled exception" + end + end + end + + def test_record_error_teardown + assert_run_record NilClass, RuntimeError do + def test_method + assert true + end + + def teardown + raise "unhandled exception" + end + end + end + + def test_record_error_in_test_and_teardown + assert_run_record AnError, RuntimeError do + def test_method + raise AnError + end + + def teardown + raise "unhandled exception" + end + end + end + + def test_record_skip + assert_run_record MiniTest::Skip do + def test_method + skip "not yet" + end + end + end +end |