From 9d93d4df6f22fba2c2d48cbdf594fcad3682fed3 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 5 Jul 2013 02:29:49 +0000 Subject: 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 --- lib/test/unit/assertions.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib') 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 @@ -65,6 +65,42 @@ module Test assert_raises(*args, &b) 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 ) # -- cgit v1.2.3