diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-05 02:29:49 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-05 02:29:49 +0000 |
commit | 9d93d4df6f22fba2c2d48cbdf594fcad3682fed3 (patch) | |
tree | 368d5cdfa788a1c0fba7c9744ccfd4cd161ab2c6 /lib/test/unit | |
parent | 5716c5ef2c96efaaa30662cf3508a80b7a92d522 (diff) | |
download | ruby-9d93d4df6f22fba2c2d48cbdf594fcad3682fed3.tar.gz |
test/unit: assert_raise_with_message
* 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.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41790 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/test/unit')
-rw-r--r-- | lib/test/unit/assertions.rb | 36 |
1 files changed, 36 insertions, 0 deletions
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 |