Add `Test::Unit::CoreAssertions#assert_raise_kind_of`

Similar to `Test::Unit::assert_raise`, but allows sub classes too.
This commit is contained in:
Nobuyoshi Nakada 2025-01-24 15:51:28 +09:00
Родитель ae94fca788
Коммит c51668d249
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 3582D74E1FEE4465
2 изменённых файлов: 49 добавлений и 0 удалений

Просмотреть файл

@ -514,6 +514,43 @@ eom
ex
end
# :call-seq:
# assert_raise_kind_of(*args, &block)
#
#Tests if the given block raises one of the given exceptions or
#sub exceptions of the given exceptions. If the last argument
#is a String, it will be used as the error message.
#
# assert_raise do #Fails, no Exceptions are raised
# end
#
# assert_raise SystemCallErr do
# Dir.chdir(__FILE__) #Raises Errno::ENOTDIR, so assertion succeeds
# end
def assert_raise_kind_of(*exp, &b)
case exp.last
when String, Proc
msg = exp.pop
end
begin
yield
rescue Test::Unit::PendedError => e
raise e unless exp.include? Test::Unit::PendedError
rescue *exp => e
pass
rescue Exception => e
flunk(message(msg) {"#{mu_pp(exp)} family exception expected, not #{mu_pp(e)}"})
ensure
unless e
exp = exp.first if exp.size == 1
flunk(message(msg) {"#{mu_pp(exp)} family expected but nothing was raised"})
end
end
e
end
TEST_DIR = File.join(__dir__, "test/unit") #:nodoc:
# :call-seq:

Просмотреть файл

@ -47,6 +47,18 @@ class TestAssertion < Test::Unit::TestCase
end
end
def test_assert_raise_kind_of
my_error = Class.new(StandardError)
assert_raise_kind_of(my_error) do
raise my_error
end
assert_raise_kind_of(StandardError) do
raise my_error
end
end
def test_assert_pattern_list
assert_pattern_list([/foo?/], "foo")
assert_not_pattern_list([/foo?/], "afoo")