Refactor `Test::Unit::CoreAssertions#assert_nothing_raised`

* Separate exception classes to be rescued or reraised
* Use the filtered backtrace in the failure message
* Raise a new `AssertionFailedError` with the original backtrace
This commit is contained in:
Nobuyoshi Nakada 2021-10-09 17:44:01 +09:00
Родитель fc913ad21d
Коммит c423cc932e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7CD2805BFA3770C6
1 изменённых файлов: 8 добавлений и 20 удалений

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

@ -172,27 +172,15 @@ module Test
msg = args.pop
end
begin
line = __LINE__; yield
rescue Test::Unit::PendedError
yield
rescue Test::Unit::PendedError, *(Test::Unit::AssertionFailedError if args.empty?)
raise
rescue Exception => e
bt = e.backtrace
as = e.instance_of?(Test::Unit::AssertionFailedError)
if as
ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
bt.reject! {|ln| ans =~ ln}
end
if ((args.empty? && !as) ||
args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
msg = message(msg) {
"Exception raised:\n<#{mu_pp(e)}>\n" +
"Backtrace:\n" +
e.backtrace.map{|frame| " #{frame}"}.join("\n")
}
raise Test::Unit::AssertionFailedError, msg.call, bt
else
raise
end
rescue *(args.empty? ? Exception : args) => e
msg = message(msg) {
"Exception raised:\n<#{mu_pp(e)}>\n""Backtrace:\n" <<
Test.filter_backtrace(e.backtrace).map{|frame| " #{frame}"}.join("\n")
}
raise Test::Unit::AssertionFailedError, msg.call, e.backtrace
end
end