ruby-changes:32093
From: nobu <ko1@a...>
Date: Fri, 13 Dec 2013 18:18:02 +0900 (JST)
Subject: [ruby-changes:32093] nobu:r44172 (trunk): assertions.rb: last match in assert_raise_with_message
nobu 2013-12-13 18:17:55 +0900 (Fri, 13 Dec 2013) New Revision: 44172 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44172 Log: assertions.rb: last match in assert_raise_with_message * lib/test/unit/assertions.rb (assert_raise_with_message): set caller's last match if expected is a Regexp. * test/ruby/test_arity.rb (err_mess): use assert_raise_with_message. Modified files: trunk/lib/test/unit/assertions.rb trunk/test/ruby/test_arity.rb Index: lib/test/unit/assertions.rb =================================================================== --- lib/test/unit/assertions.rb (revision 44171) +++ lib/test/unit/assertions.rb (revision 44172) @@ -88,7 +88,7 @@ module Test https://github.com/ruby/ruby/blob/trunk/lib/test/unit/assertions.rb#L88 # 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) + def assert_raise_with_message(exception, expected, msg = nil, &block) case expected when String assert = :assert_equal @@ -100,7 +100,14 @@ module Test https://github.com/ruby/ruby/blob/trunk/lib/test/unit/assertions.rb#L100 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) + + if assert == :assert_equal + assert_equal(expected, ex.message, msg) + else + msg = message(msg) { "Expected #{mu_pp expected} to match #{mu_pp ex.message}" } + assert expected =~ ex.message, msg + block.binding.eval("proc{|_|$~=_}").call($~) + end ex end Index: test/ruby/test_arity.rb =================================================================== --- test/ruby/test_arity.rb (revision 44171) +++ test/ruby/test_arity.rb (revision 44172) @@ -3,7 +3,7 @@ require 'test/unit' https://github.com/ruby/ruby/blob/trunk/test/ruby/test_arity.rb#L3 class TestArity < Test::Unit::TestCase def err_mess(method_proc = nil, argc = 0) args = (1..argc).to_a - err = assert_raise(ArgumentError) do + assert_raise_with_message(ArgumentError, /wrong number of arguments \((.*)\)/) do case method_proc when nil yield @@ -13,8 +13,6 @@ class TestArity < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_arity.rb#L13 method_proc.call(*args) end end - s = err.to_s - assert s =~ /wrong number of arguments \((.*)\)/, "Unexpected ArgumentError's message: #{s}" $1 end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/