diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-08-05 17:13:13 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-08-07 17:16:00 +0900 |
commit | 8b094404f29bef087794f365078cb7423c37172e (patch) | |
tree | 9f1b02731f45f8aeb19810016e6bedb8d1934ef3 /test/envutil.rb | |
parent | 7c7469c34d65781c1575ebf3d1a7f9e032b35aad (diff) | |
download | ruby-openssl-8b094404f29bef087794f365078cb7423c37172e.tar.gz |
test/envutil: port assert_raise_with_message from Ruby tree
Ruby's assert_raise doesn't allow the expected exception to be an
instance of an exception.
Diffstat (limited to 'test/envutil.rb')
-rw-r--r-- | test/envutil.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test/envutil.rb b/test/envutil.rb index a4964c2c..5cd8b511 100644 --- a/test/envutil.rb +++ b/test/envutil.rb @@ -576,6 +576,56 @@ eom values 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, &block) + 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 = m = nil + EnvUtil.with_default_internal(expected.encoding) do + ex = assert_raise(exception, msg || "Exception(#{exception}) with message matches to #{expected.inspect}") do + yield + end + m = ex.message + end + msg = message(msg, "") {"Expected Exception(#{exception}) was raised, but the message doesn't match"} + + if assert == :assert_equal + assert_equal(expected, m, msg) + else + msg = message(msg) { "Expected #{mu_pp expected} to match #{mu_pp m}" } + assert expected =~ m, msg + block.binding.eval("proc{|_|$~=_}").call($~) + end + ex + end + class << (AssertFile = Struct.new(:failure_message).new) include Assertions def assert_file_predicate(predicate, *args) |