diff options
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | lib/test/unit/assertions.rb | 36 | ||||
-rw-r--r-- | test/fileutils/test_fileutils.rb | 30 |
3 files changed, 48 insertions, 24 deletions
@@ -1,3 +1,9 @@ +Fri Jul 5 11:29:47 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * lib/test/unit/assertions.rb (assert_raise_with_message): move from + test/fileutils/test_fileutils.rb. this is still experimental and + the interface may be changed. + Fri Jul 5 11:08:00 2013 NAKAMURA Usaku <usa@ruby-lang.org> * win32/win32.c (w32_spawn): r41710 made that if the command starts with diff --git a/lib/test/unit/assertions.rb b/lib/test/unit/assertions.rb index b532146051..bc8530e3f6 100644 --- a/lib/test/unit/assertions.rb +++ b/lib/test/unit/assertions.rb @@ -66,6 +66,42 @@ module Test end # :call-seq: + # assert_raise_with_message(exception, expected, msg = nil, &block) + # + #Tests if the given block raises an exception with the expected + #message. + # + # assert_raise_with_message(RuntimeError, "foo") do + # nil #Fails, no Exceptions are raised + # end + # + # assert_raise_with_message(RuntimeError, "foo") do + # raise ArgumentError, "foo" #Fails, different Exception is raised + # end + # + # assert_raise_with_message(RuntimeError, "foo") do + # raise "bar" #Fails, RuntimeError is raised but the message differs + # end + # + # assert_raise_with_message(RuntimeError, "foo") do + # raise "foo" #Raises RuntimeError with the message, so assertion succeeds + # end + def assert_raise_with_message(exception, expected, msg = nil) + case expected + when String + assert = :assert_equal + when Regexp + assert = :assert_match + else + raise TypeError, "Expected #{expected.inspect} to be a kind of String or Regexp, not #{expected.class}" + end + + ex = assert_raise(exception, msg) {yield} + msg = message(msg, "") {"Expected Exception(#{exception}) was raised, but the message doesn't match"} + __send__(assert, expected, ex.message, msg) + end + + # :call-seq: # assert_nothing_raised( *args, &block ) # #If any exceptions are given as arguments, the assertion will diff --git a/test/fileutils/test_fileutils.rb b/test/fileutils/test_fileutils.rb index ea9c38bfe9..abcecb9472 100644 --- a/test/fileutils/test_fileutils.rb +++ b/test/fileutils/test_fileutils.rb @@ -962,23 +962,23 @@ class TestFileUtils assert_equal 0500, File.stat('tmp/a').mode & 07777 end - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod "a", 'tmp/a' } - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod "x+a", 'tmp/a' } - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod "u+z", 'tmp/a' } - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod ",+x", 'tmp/a' } - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod "755", 'tmp/a' } @@ -1187,29 +1187,11 @@ class TestFileUtils uptodate? Pathname.new('tmp/a'), [Pathname.new('tmp/b'), Pathname.new('tmp/c')] } # [Bug #6708] [ruby-core:46256] - assert_raises_with_message(ArgumentError, "wrong number of arguments (3 for 2)") { + assert_raise_with_message(ArgumentError, "wrong number of arguments (3 for 2)") { uptodate?('new',['old', 'oldest'], {}) } end - def assert_raises_with_message(klass, message) - begin - yield - flunk("Expected Exception #{klass} didn't raise") - rescue klass => ex - if message.kind_of? String - flag = !!(ex.message == message) - assert(flag, "Expected Exception(#{klass}) was raised, but the message doesn't match") - elsif message.kind_of? Regexp - flag = !!(ex.message =~ message) - assert(flag, "Expected Exception(#{klass}) was raised, but the message doesn't match") - else - raise - end - end - end - private :assert_raises_with_message - def test_cd check_singleton :cd end |