1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Replace Test::Assertion and Test::Skip to Test::Unit::AssertionFailedError and Test::Unit::PendedError

This commit is contained in:
Hiroshi SHIBATA 2021-09-10 20:56:07 +09:00
parent 7cec81e073
commit d05383812a
Notes: git 2021-09-11 08:48:28 +09:00
8 changed files with 54 additions and 58 deletions

View file

@ -103,7 +103,7 @@ module Test
pend 'assert_no_memory_leak may consider MJIT memory usage as leak' if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? pend 'assert_no_memory_leak may consider MJIT memory usage as leak' if defined?(RubyVM::JIT) && RubyVM::JIT.enabled?
require_relative 'memory_status' require_relative 'memory_status'
raise Test::Skip, "unsupported platform" unless defined?(Memory::Status) raise Test::Unit::PendedError, "unsupported platform" unless defined?(Memory::Status)
token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m" token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m"
token_dump = token.dump token_dump = token.dump
@ -168,11 +168,11 @@ module Test
end end
begin begin
line = __LINE__; yield line = __LINE__; yield
rescue Test::Skip rescue Test::Unit::PendedError
raise raise
rescue Exception => e rescue Exception => e
bt = e.backtrace bt = e.backtrace
as = e.instance_of?(Test::Assertion) as = e.instance_of?(Test::Unit::AssertionFailedError)
if as if as
ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
bt.reject! {|ln| ans =~ ln} bt.reject! {|ln| ans =~ ln}
@ -184,7 +184,7 @@ module Test
"Backtrace:\n" + "Backtrace:\n" +
e.backtrace.map{|frame| " #{frame}"}.join("\n") e.backtrace.map{|frame| " #{frame}"}.join("\n")
} }
raise Test::Assertion, msg.call, bt raise Test::Unit::AssertionFailedError, msg.call, bt
else else
raise raise
end end
@ -387,8 +387,8 @@ eom
begin begin
yield yield
rescue Test::Skip => e rescue Test::Unit::PendedError => e
return e if exp.include? Test::Skip return e if exp.include? Test::Unit::PendedError
raise e raise e
rescue Exception => e rescue Exception => e
expected = exp.any? { |ex| expected = exp.any? { |ex|
@ -699,7 +699,7 @@ eom
if message if message
msg = "#{message}\n#{msg}" msg = "#{message}\n#{msg}"
end end
raise Test::Assertion, msg raise Test::Unit::AssertionFailedError, msg
end end
end end

View file

@ -9,16 +9,6 @@ require "leakchecker"
# See Test::Unit # See Test::Unit
module Test module Test
##
# Assertion base class
class Assertion < Exception; end
##
# Assertion raised when skipping a test
class Skip < Assertion; end
class << self class << self
## ##
# Filter object for backtraces. # Filter object for backtraces.
@ -62,9 +52,15 @@ module Test
# Test::Unit has been left in the standard library to support legacy test # Test::Unit has been left in the standard library to support legacy test
# suites. # suites.
module Unit module Unit
# Compatibility hack for assert_raise ##
AssertionFailedError = Test::Assertion # Assertion base class
PendedError = Test::Skip
class AssertionFailedError < Exception; end
##
# Assertion raised when skipping a test
class PendedError < AssertionFailedError; end
module RunCount # :nodoc: all module RunCount # :nodoc: all
@@run_count = 0 @@run_count = 0
@ -630,7 +626,7 @@ module Test
unless @interrupt || !@options[:retry] || @need_quit unless @interrupt || !@options[:retry] || @need_quit
parallel = @options[:parallel] parallel = @options[:parallel]
@options[:parallel] = false @options[:parallel] = false
suites, rep = rep.partition {|r| r[:testcase] && r[:file] && r[:report].any? {|e| !e[2].is_a?(Test::Skip)}} suites, rep = rep.partition {|r| r[:testcase] && r[:file] && r[:report].any? {|e| !e[2].is_a?(Test::Unit::PendedError)}}
suites.map {|r| File.realpath(r[:file])}.uniq.each {|file| require file} suites.map {|r| File.realpath(r[:file])}.uniq.each {|file| require file}
suites.map! {|r| eval("::"+r[:testcase])} suites.map! {|r| eval("::"+r[:testcase])}
del_status_line or puts del_status_line or puts
@ -1535,11 +1531,11 @@ module Test
# hidden when not verbose (-v), note this is temporally. # hidden when not verbose (-v), note this is temporally.
n = report.size n = report.size
e = case e e = case e
when Test::Skip then when Test::Unit::PendedError then
@skips += 1 @skips += 1
return "S" unless @verbose return "S" unless @verbose
"Skipped:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n" "Skipped:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"
when Test::Assertion then when Test::Unit::AssertionFailedError then
@failures += 1 @failures += 1
"Failure:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n" "Failure:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"
else else
@ -1549,7 +1545,7 @@ module Test
end end
@report << e @report << e
rep = e[0, 1] rep = e[0, 1]
if Test::Skip === e and /no message given\z/ =~ e.message if Test::Unit::PendedError === e and /no message given\z/ =~ e.message
report.slice!(n..-1) report.slice!(n..-1)
rep = "." rep = "."
end end

View file

@ -117,7 +117,7 @@ module Test
self._assertions += 1 self._assertions += 1
unless test then unless test then
msg = msg.call if Proc === msg msg = msg.call if Proc === msg
raise Test::Assertion, msg raise Test::Unit::AssertionFailedError, msg
end end
true true
end end
@ -578,7 +578,7 @@ module Test
def skip msg = nil, bt = caller def skip msg = nil, bt = caller
msg ||= "Skipped, no message given" msg ||= "Skipped, no message given"
@skip = true @skip = true
raise Test::Skip, msg, bt raise Test::Unit::PendedError, msg, bt
end end
alias omit skip alias omit skip

View file

@ -160,21 +160,21 @@ module Test
end end
def puke(klass, meth, e) # :nodoc: def puke(klass, meth, e) # :nodoc:
if e.is_a?(Test::Skip) if e.is_a?(Test::Unit::PendedError)
new_e = Test::Skip.new(e.message) new_e = Test::Unit::PendedError.new(e.message)
new_e.set_backtrace(e.backtrace) new_e.set_backtrace(e.backtrace)
e = new_e e = new_e
end end
@partial_report << [klass.name, meth, e.is_a?(Test::Assertion) ? e : ProxyError.new(e)] @partial_report << [klass.name, meth, e.is_a?(Test::Unit::AssertionFailedError) ? e : ProxyError.new(e)]
super super
end end
def record(suite, method, assertions, time, error) # :nodoc: def record(suite, method, assertions, time, error) # :nodoc:
case error case error
when nil when nil
when Test::Assertion, Test::Skip when Test::Unit::AssertionFailedError, Test::Unit::PendedError
case error.cause case error.cause
when nil, Test::Assertion, Test::Skip when nil, Test::Unit::AssertionFailedError, Test::Unit::PendedError
else else
bt = error.backtrace bt = error.backtrace
error = error.class.new(error.message) error = error.class.new(error.message)

View file

@ -141,7 +141,7 @@ module Test
# Subclass TestCase to create your own tests. Typically you'll want a # Subclass TestCase to create your own tests. Typically you'll want a
# TestCase subclass per implementation class. # TestCase subclass per implementation class.
# #
# See MiniTest::Assertions # See MiniTest::Unit::AssertionFailedErrors
class TestCase class TestCase
include Assertions include Assertions
@ -195,7 +195,7 @@ module Test
rescue *PASSTHROUGH_EXCEPTIONS rescue *PASSTHROUGH_EXCEPTIONS
raise raise
rescue Exception => e rescue Exception => e
@passed = Test::Skip === e @passed = Test::Unit::PendedError === e
time = Time.now - start_time time = Time.now - start_time
runner.record self.class, self.__name__, self._assertions, time, e runner.record self.class, self.__name__, self._assertions, time, e
result = runner.puke self.class, self.__name__, e result = runner.puke self.class, self.__name__, e

View file

@ -22,7 +22,7 @@ class TestAssertion < Test::Unit::TestCase
end end
def test_assert_raise def test_assert_raise
assert_raise(Test::Assertion) do assert_raise(Test::Unit::AssertionFailedError) do
return_in_assert_raise return_in_assert_raise
end end
end end

View file

@ -19,7 +19,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase
"#{MINITEST_BASE_DIR}/test.rb:106:in `run'"] "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
def test_class_puke_with_assertion_failed def test_class_puke_with_assertion_failed
exception = Test::Assertion.new "Oh no!" exception = Test::Unit::AssertionFailedError.new "Oh no!"
exception.set_backtrace ["unhappy"] exception.set_backtrace ["unhappy"]
assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception) assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
assert_equal 1, @tu.failures assert_equal 1, @tu.failures
@ -39,7 +39,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase
ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
exception = Test::Assertion.new "Oh no!" exception = Test::Unit::AssertionFailedError.new "Oh no!"
exception.set_backtrace bt exception.set_backtrace bt
assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception) assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
assert_equal 1, @tu.failures assert_equal 1, @tu.failures
@ -62,7 +62,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase
ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
exception = Test::Assertion.new "Oh no!" exception = Test::Unit::AssertionFailedError.new "Oh no!"
exception.set_backtrace bt exception.set_backtrace bt
assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception) assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
assert_equal 1, @tu.failures assert_equal 1, @tu.failures
@ -73,7 +73,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase
def test_class_puke_with_failure_and_flunk_in_backtrace def test_class_puke_with_failure_and_flunk_in_backtrace
exception = begin exception = begin
Test::Unit::TestCase.new('fake tc').flunk Test::Unit::TestCase.new('fake tc').flunk
rescue Test::Assertion => failure rescue Test::Unit::AssertionFailedError => failure
failure failure
end end
assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception) assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
@ -95,7 +95,7 @@ class TestMiniTestUnit < MetaMetaMetaTestCase
ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
exception = Test::Assertion.new "Oh no!" exception = Test::Unit::AssertionFailedError.new "Oh no!"
exception.set_backtrace bt exception.set_backtrace bt
assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception) assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
assert_equal 1, @tu.failures assert_equal 1, @tu.failures
@ -704,7 +704,7 @@ class TestMiniTestUnitTestCase < Test::Unit::TestCase
def test_assert_includes_triggered def test_assert_includes_triggered
@assertion_count = 3 @assertion_count = 3
e = @tc.assert_raise Test::Assertion do e = @tc.assert_raise Test::Unit::AssertionFailedError do
@tc.assert_includes [true], false @tc.assert_includes [true], false
end end
@ -895,7 +895,7 @@ class TestMiniTestUnitTestCase < Test::Unit::TestCase
def test_assert_raise_skip def test_assert_raise_skip
@assertion_count = 0 @assertion_count = 0
util_assert_triggered "skipped", Test::Skip do util_assert_triggered "skipped", Test::Unit::PendedError do
@tc.assert_raise ArgumentError do @tc.assert_raise ArgumentError do
begin begin
raise "blah" raise "blah"
@ -907,7 +907,7 @@ class TestMiniTestUnitTestCase < Test::Unit::TestCase
end end
def test_assert_raise_triggered_different def test_assert_raise_triggered_different
e = assert_raise Test::Assertion do e = assert_raise Test::Unit::AssertionFailedError do
@tc.assert_raise RuntimeError do @tc.assert_raise RuntimeError do
raise SyntaxError, "icky" raise SyntaxError, "icky"
end end
@ -924,7 +924,7 @@ class TestMiniTestUnitTestCase < Test::Unit::TestCase
end end
def test_assert_raise_triggered_different_msg def test_assert_raise_triggered_different_msg
e = assert_raise Test::Assertion do e = assert_raise Test::Unit::AssertionFailedError do
@tc.assert_raise RuntimeError, "XXX" do @tc.assert_raise RuntimeError, "XXX" do
raise SyntaxError, "icky" raise SyntaxError, "icky"
end end
@ -942,31 +942,31 @@ class TestMiniTestUnitTestCase < Test::Unit::TestCase
end end
def test_assert_raise_triggered_none def test_assert_raise_triggered_none
e = assert_raise Test::Assertion do e = assert_raise Test::Unit::AssertionFailedError do
@tc.assert_raise Test::Assertion do @tc.assert_raise Test::Unit::AssertionFailedError do
# do nothing # do nothing
end end
end end
expected = "Test::Assertion expected but nothing was raised." expected = "Test::Unit::AssertionFailedError expected but nothing was raised."
assert_equal expected, e.message assert_equal expected, e.message
end end
def test_assert_raise_triggered_none_msg def test_assert_raise_triggered_none_msg
e = assert_raise Test::Assertion do e = assert_raise Test::Unit::AssertionFailedError do
@tc.assert_raise Test::Assertion, "XXX" do @tc.assert_raise Test::Unit::AssertionFailedError, "XXX" do
# do nothing # do nothing
end end
end end
expected = "XXX.\nTest::Assertion expected but nothing was raised." expected = "XXX.\nTest::Unit::AssertionFailedError expected but nothing was raised."
assert_equal expected, e.message assert_equal expected, e.message
end end
def test_assert_raise_triggered_subclass def test_assert_raise_triggered_subclass
e = assert_raise Test::Assertion do e = assert_raise Test::Unit::AssertionFailedError do
@tc.assert_raise StandardError do @tc.assert_raise StandardError do
raise AnError raise AnError
end end
@ -1223,7 +1223,7 @@ class TestMiniTestUnitTestCase < Test::Unit::TestCase
def test_refute_includes_triggered def test_refute_includes_triggered
@assertion_count = 3 @assertion_count = 3
e = @tc.assert_raise Test::Assertion do e = @tc.assert_raise Test::Unit::AssertionFailedError do
@tc.refute_includes [true], true @tc.refute_includes [true], true
end end
@ -1344,7 +1344,7 @@ class TestMiniTestUnitTestCase < Test::Unit::TestCase
def test_skip def test_skip
@assertion_count = 0 @assertion_count = 0
util_assert_triggered "haha!", Test::Skip do util_assert_triggered "haha!", Test::Unit::PendedError do
@tc.skip "haha!" @tc.skip "haha!"
end end
end end
@ -1379,7 +1379,7 @@ class TestMiniTestUnitTestCase < Test::Unit::TestCase
assert_equal expected, sample_test_case.test_methods assert_equal expected, sample_test_case.test_methods
end end
def assert_triggered expected, klass = Test::Assertion def assert_triggered expected, klass = Test::Unit::AssertionFailedError
e = assert_raise klass do e = assert_raise klass do
yield yield
end end
@ -1465,7 +1465,7 @@ class TestMiniTestUnitRecording < MetaMetaMetaTestCase
end end
def test_record_failing def test_record_failing
assert_run_record Test::Assertion do assert_run_record Test::Unit::AssertionFailedError do
def test_method def test_method
assert false assert false
end end
@ -1505,7 +1505,7 @@ class TestMiniTestUnitRecording < MetaMetaMetaTestCase
end end
def test_record_skip def test_record_skip
assert_run_record Test::Skip do assert_run_record Test::Unit::PendedError do
def test_method def test_method
skip "not yet" skip "not yet"
end end

View file

@ -120,9 +120,9 @@ module TestParallel
assert_kind_of(Array,result[3]) assert_kind_of(Array,result[3])
assert_kind_of(Array,result[4]) assert_kind_of(Array,result[4])
assert_kind_of(Array,result[2][1]) assert_kind_of(Array,result[2][1])
assert_kind_of(Test::Assertion,result[2][0][2]) assert_kind_of(Test::Unit::AssertionFailedError,result[2][0][2])
assert_kind_of(Test::Skip,result[2][1][2]) assert_kind_of(Test::Unit::PendedError,result[2][1][2])
assert_kind_of(Test::Skip,result[2][2][2]) assert_kind_of(Test::Unit::PendedError,result[2][2][2])
assert_kind_of(Exception, result[2][3][2]) assert_kind_of(Exception, result[2][3][2])
assert_equal(result[5], "TestE") assert_equal(result[5], "TestE")
end end