Hi,

Exceptions caused by system call often varies depending on
platforms.  So I think that it would be nice if assert_raises
accept multiple exceptions.


Index: lib/test/unit/assertions.rb =================================================================== RCS file: /cvs/ruby/src/ruby/lib/test/unit/assertions.rb,v retrieving revision 1.12 diff -u -2 -p -d -r1.12 assertions.rb --- lib/test/unit/assertions.rb 1 Dec 2003 19:25:51 -0000 1.12 +++ lib/test/unit/assertions.rb 4 Dec 2003 05:32:23 -0000 @@ -59,21 +59,27 @@ EOT # Passes if block raises exception. public - def assert_raises(expected_exception_klass, message="") + def assert_raises(*args) _wrap_assertion do - assert_instance_of(Class, expected_exception_klass, "Should expect a class of exception") + if Class === args.last and Exception >= args.last + message = "" + else + message = args.pop + end + args.each do |klass| + assert(Exception >= klass, "Should expect a class of exception") + end + expected = args.size == 1 ? args.first : args actual_exception = nil - full_message = build_message(message, "<?> exception expected but none was thrown.", expected_exception_klass) + full_message = build_message(message, "<?> exception expected but none was thrown.", expected) assert_block(full_message) do - thrown = false begin yield - rescue Exception => thrown_exception - actual_exception = thrown_exception - thrown = true + rescue Exception => actual_exception + break end - thrown + false end - full_message = build_message(message, "<?> exception expected but was\n?", expected_exception_klass, actual_exception) - assert_block(full_message) { expected_exception_klass == actual_exception.class } + full_message = build_message(message, "<?> exception expected but was\n?", expected, actual_exception) + assert_block(full_message) { args.include?(actual_exception.class) } actual_exception end @@ -177,7 +183,11 @@ EOT def assert_nothing_raised(*args) _wrap_assertion do - message = "" - if (!args[-1].instance_of?(Class)) + if Class === args.last and Exception >= args.last + message = "" + else message = args.pop + end + args.each do |klass| + assert(Exception >= klass, "Should expect a class of exception") end begin
-- Nobu Nakada