summaryrefslogtreecommitdiff
path: root/tool/lib/core_assertions.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2025-01-24 15:51:28 +0900
committerNobuyoshi Nakada <[email protected]>2025-01-24 15:51:28 +0900
commitc51668d24962602b781a7348451de807f74b05be (patch)
tree199604be5822902951382264af0fda86e4056a0a /tool/lib/core_assertions.rb
parentae94fca7887f2dbab9dc6fa6ae90fcaffde3d6b5 (diff)
Add `Test::Unit::CoreAssertions#assert_raise_kind_of`
Similar to `Test::Unit::assert_raise`, but allows sub classes too.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/12625
Diffstat (limited to 'tool/lib/core_assertions.rb')
-rw-r--r--tool/lib/core_assertions.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/tool/lib/core_assertions.rb b/tool/lib/core_assertions.rb
index 62ada2bdca..66822b4e3b 100644
--- a/tool/lib/core_assertions.rb
+++ b/tool/lib/core_assertions.rb
@@ -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: