ruby-changes:31133
From: nobu <ko1@a...>
Date: Wed, 9 Oct 2013 17:43:19 +0900 (JST)
Subject: [ruby-changes:31133] nobu:r43212 (trunk): test/unit/assertions.rb: return exception
nobu 2013-10-09 17:43:12 +0900 (Wed, 09 Oct 2013) New Revision: 43212 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43212 Log: test/unit/assertions.rb: return exception * lib/test/unit/assertions.rb (assert_raise_with_message): return raised exception same as assert_raise. * test/ruby, test/-ext-: use assert_raise_with_message. Modified files: trunk/lib/test/unit/assertions.rb trunk/test/-ext-/marshal/test_usrmarshal.rb trunk/test/-ext-/typeddata/test_typeddata.rb trunk/test/ruby/test_beginendblock.rb trunk/test/ruby/test_bignum.rb trunk/test/ruby/test_exception.rb trunk/test/ruby/test_file.rb trunk/test/ruby/test_io.rb trunk/test/ruby/test_io_m17n.rb trunk/test/ruby/test_iseq.rb trunk/test/ruby/test_module.rb trunk/test/ruby/test_object.rb trunk/test/ruby/test_regexp.rb trunk/test/ruby/test_require.rb trunk/test/ruby/test_signal.rb trunk/test/ruby/test_sprintf.rb trunk/test/ruby/test_super.rb Index: lib/test/unit/assertions.rb =================================================================== --- lib/test/unit/assertions.rb (revision 43211) +++ lib/test/unit/assertions.rb (revision 43212) @@ -99,6 +99,7 @@ module Test https://github.com/ruby/ruby/blob/trunk/lib/test/unit/assertions.rb#L99 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) + ex end # :call-seq: Index: test/ruby/test_module.rb =================================================================== --- test/ruby/test_module.rb (revision 43211) +++ test/ruby/test_module.rb (revision 43212) @@ -248,10 +248,11 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L248 ":", ["String::", "[Bug #7573]"], ].each do |name, msg| - e = assert_raise(NameError, "#{msg}#{': ' if msg}wrong constant name #{name.dump}") { + expected = "wrong constant name %s" % name + msg = "#{msg}#{': ' if msg}wrong constant name #{name.dump}" + assert_raise_with_message(NameError, expected, msg) { Object.const_get name } - assert_equal("wrong constant name %s" % name, e.message) end end @@ -624,8 +625,9 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L625 bug5084 = '[ruby-dev:44200]' assert_raise(TypeError, bug5084) { c1.const_get(1) } bug7574 = '[ruby-dev:46749]' - e = assert_raise(NameError) { Object.const_get("String\0") } - assert_equal("wrong constant name \"String\\u0000\"", e.message, bug7574) + assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) { + Object.const_get("String\0") + } end def test_const_defined_invalid_name @@ -634,8 +636,9 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L636 bug5084 = '[ruby-dev:44200]' assert_raise(TypeError, bug5084) { c1.const_defined?(1) } bug7574 = '[ruby-dev:46749]' - e = assert_raise(NameError) { Object.const_defined?("String\0") } - assert_equal("wrong constant name \"String\\u0000\"", e.message, bug7574) + assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) { + Object.const_defined?("String\0") + } end def test_const_get_no_inherited @@ -1656,7 +1659,7 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L1659 @@foo $foo ].each do |name| - assert_raise(NameError) do + assert_raise_with_message(NameError, /#{Regexp.quote(name)}/) do Module.new { attr_accessor name.to_sym } end end Index: test/ruby/test_io_m17n.rb =================================================================== --- test/ruby/test_io_m17n.rb (revision 43211) +++ test/ruby/test_io_m17n.rb (revision 43212) @@ -763,10 +763,12 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_m17n.rb#L763 assert_equal(eucjp, r.read) end) - e = assert_raise(ArgumentError) {with_pipe("UTF-8", "UTF-8".encode("UTF-32BE")) {}} - assert_match(/invalid name encoding/, e.message) - e = assert_raise(ArgumentError) {with_pipe("UTF-8".encode("UTF-32BE")) {}} - assert_match(/invalid name encoding/, e.message) + assert_raise_with_message(ArgumentError, /invalid name encoding/) do + with_pipe("UTF-8", "UTF-8".encode("UTF-32BE")) {} + end + assert_raise_with_message(ArgumentError, /invalid name encoding/) do + with_pipe("UTF-8".encode("UTF-32BE")) {} + end ENCS.each {|enc| pipe(enc, Index: test/ruby/test_require.rb =================================================================== --- test/ruby/test_require.rb (revision 43211) +++ test/ruby/test_require.rb (revision 43212) @@ -50,8 +50,7 @@ class TestRequire < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L50 def test_require_nonascii bug3758 = '[ruby-core:31915]' ["\u{221e}", "\x82\xa0".force_encoding("cp932")].each do |path| - e = assert_raise(LoadError, bug3758) {require path} - assert_match(/#{path}\z/, e.message, bug3758) + assert_raise_with_message(LoadError, /#{path}\z/, bug3758) {require path} end end Index: test/ruby/test_bignum.rb =================================================================== --- test/ruby/test_bignum.rb (revision 43211) +++ test/ruby/test_bignum.rb (revision 43212) @@ -634,8 +634,7 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_bignum.rb#L634 if (big = 2**31-1).is_a?(Fixnum) return end - e = assert_raise(RangeError) {(1 << big).to_s} - assert_match(/too big to convert/, e.message) + assert_raise_with_message(RangeError, /too big to convert/) {(1 << big).to_s} end def test_fix_fdiv Index: test/ruby/test_sprintf.rb =================================================================== --- test/ruby/test_sprintf.rb (revision 43211) +++ test/ruby/test_sprintf.rb (revision 43212) @@ -325,70 +325,52 @@ class TestSprintf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_sprintf.rb#L325 def test_named_untyped assert_equal("value", sprintf("%<key>s", :key => "value")) - e = assert_raise(ArgumentError) {sprintf("%1$<key2>s", :key => "value")} - assert_equal("named<key2> after numbered", e.message) - e = assert_raise(ArgumentError) {sprintf("%s%s%<key2>s", "foo", "bar", :key => "value")} - assert_equal("named<key2> after unnumbered(2)", e.message) - e = assert_raise(ArgumentError) {sprintf("%<key><key2>s", :key => "value")} - assert_equal("named<key2> after <key>", e.message) - e = assert_raise(KeyError) {sprintf("%<key>s", {})} - assert_equal("key<key> not found", e.message) + assert_raise_with_message(ArgumentError, "named<key2> after numbered") {sprintf("%1$<key2>s", :key => "value")} + assert_raise_with_message(ArgumentError, "named<key2> after unnumbered(2)") {sprintf("%s%s%<key2>s", "foo", "bar", :key => "value")} + assert_raise_with_message(ArgumentError, "named<key2> after <key>") {sprintf("%<key><key2>s", :key => "value")} + assert_raise_with_message(KeyError, "key<key> not found") {sprintf("%<key>s", {})} end def test_named_untyped_enc key = "\u{3012}" [Encoding::UTF_8, Encoding::EUC_JP].each do |enc| k = key.encode(enc) - e = assert_raise(ArgumentError) {sprintf("%1$<#{k}>s", key: "value")} + e = assert_raise_with_message(ArgumentError, "named<#{k}> after numbered") {sprintf("%1$<#{k}>s", key: "value")} assert_equal(enc, e.message.encoding) - assert_equal("named<#{k}> after numbered", e.message) - e = assert_raise(ArgumentError) {sprintf("%s%s%<#{k}>s", "foo", "bar", key: "value")} + e = assert_raise_with_message(ArgumentError, "named<#{k}> after unnumbered(2)") {sprintf("%s%s%<#{k}>s", "foo", "bar", key: "value")} assert_equal(enc, e.message.encoding) - assert_equal("named<#{k}> after unnumbered(2)", e.message) - e = assert_raise(ArgumentError) {sprintf("%<key><#{k}>s", key: "value")} + e = assert_raise_with_message(ArgumentError, "named<#{k}> after <key>") {sprintf("%<key><#{k}>s", key: "value")} assert_equal(enc, e.message.encoding) - assert_equal("named<#{k}> after <key>", e.message) - e = assert_raise(ArgumentError) {sprintf("%<#{k}><key>s", k.to_sym => "value")} + e = assert_raise_with_message(ArgumentError, "named<key> after <#{k}>") {sprintf("%<#{k}><key>s", k.to_sym => "value")} assert_equal(enc, e.message.encoding) - assert_equal("named<key> after <#{k}>", e.message) - e = assert_raise(KeyError) {sprintf("%<#{k}>s", {})} + e = assert_raise_with_message(KeyError, "key<#{k}> not found") {sprintf("%<#{k}>s", {})} assert_equal(enc, e.message.encoding) - assert_equal("key<#{k}> not found", e.message) end end def test_named_typed assert_equal("value", sprintf("%{key}", :key => "value")) - e = assert_raise(ArgumentError) {sprintf("%1${key2}", :key => "value")} - assert_equal("named{key2} after numbered", e.message) - e = assert_raise(ArgumentError) {sprintf("%s%s%{key2}", "foo", "bar", :key => "value")} - assert_equal("named{key2} after unnumbered(2)", e.message) - e = assert_raise(ArgumentError) {sprintf("%<key>{key2}", :key => "value")} - assert_equal("named{key2} after <key>", e.message) + assert_raise_with_message(ArgumentError, "named{key2} after numbered") {sprintf("%1${key2}", :key => "value")} + assert_raise_with_message(ArgumentError, "named{key2} after unnumbered(2)") {sprintf("%s%s%{key2}", "foo", "bar", :key => "value")} + assert_raise_with_message(ArgumentError, "named{key2} after <key>") {sprintf("%<key>{key2}", :key => "value")} assert_equal("value{key2}", sprintf("%{key}{key2}", :key => "value")) - e = assert_raise(KeyError) {sprintf("%{key}", {})} - assert_equal("key{key} not found", e.message) + assert_raise_with_message(KeyError, "key{key} not found") {sprintf("%{key}", {})} end def test_named_typed_enc key = "\u{3012}" [Encoding::UTF_8, Encoding::EUC_JP].each do |enc| k = key.encode(enc) - e = assert_raise(ArgumentError) {sprintf("%1${#{k}}s", key: "value")} + e = assert_raise_with_message(ArgumentError, "named{#{k}} after numbered") {sprintf("%1${#{k}}s", key: "value")} assert_equal(enc, e.message.encoding) - assert_equal("named{#{k}} after numbered", e.message) - e = assert_raise(ArgumentError) {sprintf("%s%s%{#{k}}s", "foo", "bar", key: "value")} + e = assert_raise_with_message(ArgumentError, "named{#{k}} after unnumbered(2)") {sprintf("%s%s%{#{k}}s", "foo", "bar", key: "value")} assert_equal(enc, e.message.encoding) - assert_equal("named{#{k}} after unnumbered(2)", e.message) - e = assert_raise(ArgumentError) {sprintf("%<key>{#{k}}s", key: "value")} + e = assert_raise_with_message(ArgumentError, "named{#{k}} after <key>") {sprintf("%<key>{#{k}}s", key: "value")} assert_equal(enc, e.message.encoding) - assert_equal("named{#{k}} after <key>", e.message) - e = assert_raise(ArgumentError) {sprintf("%<#{k}>{key}s", k.to_sym => "value")} + e = assert_raise_with_message(ArgumentError, "named{key} after <#{k}>") {sprintf("%<#{k}>{key}s", k.to_sym => "value")} assert_equal(enc, e.message.encoding) - assert_equal("named{key} after <#{k}>", e.message) - e = assert_raise(KeyError) {sprintf("%{#{k}}", {})} + e = assert_raise_with_message(KeyError, "key{#{k}} not found") {sprintf("%{#{k}}", {})} assert_equal(enc, e.message.encoding) - assert_equal("key{#{k}} not found", e.message) end end end Index: test/ruby/test_beginendblock.rb =================================================================== --- test/ruby/test_beginendblock.rb (revision 43211) +++ test/ruby/test_beginendblock.rb (revision 43212) @@ -32,22 +32,19 @@ class TestBeginEndBlock < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/ruby/test_beginendblock.rb#L32 end def test_begininmethod - e = assert_raise(SyntaxError) do + assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do eval("def foo; BEGIN {}; end") end - assert_match(/BEGIN is permitted only at toplevel/, e.message) - e = assert_raise(SyntaxError) do + assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do eval('eval("def foo; BEGIN {}; end")') end - assert_match(/BEGIN is permitted only at toplevel/, e.message) end def test_begininclass - e = assert_raise(SyntaxError) do + assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do eval("class TestBeginEndBlock; BEGIN {}; end") end - assert_match(/BEGIN is permitted only at toplevel/, e.message) end def test_endblockwarn Index: test/ruby/test_regexp.rb =================================================================== --- test/ruby/test_regexp.rb (revision 43211) +++ test/ruby/test_regexp.rb (revision 43212) @@ -946,8 +946,9 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L946 def test_error_message_on_failed_conversion bug7539 = '[ruby-core:50733]' assert_equal false, /x/=== 42 - err = assert_raise(TypeError){ Regexp.quote(42) } - assert_equal 'no implicit conversion of Fixnum into String', err.message, bug7539 + assert_raise_with_message(TypeError, 'no implicit conversion of Fixnum into String', bug7539) { + Regexp.quote(42) + } end def test_conditional_expression Index: test/ruby/test_object.rb =================================================================== --- test/ruby/test_object.rb (revision 43211) +++ test/ruby/test_object.rb (revision 43212) @@ -488,11 +488,10 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L488 end end - e = assert_raise(ArgumentError, '[bug:6000]') do + msg = 'respond_to? must accept 1 or 2 arguments (requires 3)' + assert_raise_with_message(ArgumentError, msg, '[bug:6000]') do [[p]].flatten end - - assert_equal('respond_to? must accept 1 or 2 arguments (requires 3)', e.message) end def test_method_missing_passed_block @@ -803,9 +802,7 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L802 def test_type_error_message issue = "Bug #7539" - err = assert_raise(TypeError){ Integer([42]) } - assert_equal "can't convert Array into Integer", err.message, issue - err = assert_raise(TypeError){ [].first([42]) } - assert_equal 'no implicit conversion of Array into Integer', err.message, issue + assert_raise_with_message(TypeError, "can't convert Array into Integer") {Integer([42])} + assert_raise_with_message(TypeError, 'no implicit conversion of Array into Integer') {[].first([42])} end end Index: test/ruby/test_signal.rb =================================================================== --- test/ruby/test_signal.rb (revision 43211) +++ test/ruby/test_signal.rb (revision 43212) @@ -16,12 +16,10 @@ class TestSignal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_signal.rb#L16 assert_equal 2, x Signal.trap(:INT) { raise "Interrupt" } - ex = assert_raise(RuntimeError) { + assert_raise_with_message(RuntimeError, /Interrupt/) { Process.kill :INT, Process.pid sleep 0.1 } - assert_kind_of Exception, ex - assert_match(/Interrupt/, ex.message) ensure Signal.trap :INT, oldtrap if oldtrap end Index: test/ruby/test_file.rb =================================================================== --- test/ruby/test_file.rb (revision 43211) +++ test/ruby/test_file.rb (revision 43212) @@ -339,8 +339,9 @@ class TestFile < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file.rb#L339 end def test_file_open_double_mode - e = assert_raise(ArgumentError) { File.open("a", 'w', :mode => 'rw+') } - assert_equal 'mode specified twice', e.message + assert_raise_with_message(ArgumentError, 'mode specified twice') { + File.open("a", 'w', :mode => 'rw+') + } end def test_conflicting_encodings Index: test/ruby/test_super.rb =================================================================== --- test/ruby/test_super.rb (revision 43211) +++ test/ruby/test_super.rb (revision 43212) @@ -152,14 +152,13 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_super.rb#L152 a = A.new a.uu(12) assert_equal("A#tt", a.tt(12), "[ruby-core:3856]") - e = assert_raise(RuntimeError, "[ruby-core:24244]") { + assert_raise_with_message(RuntimeError, /implicit argument passing of super from method defined by define_method/, "[ruby-core:24244]") { lambda { Class.new { define_method(:a) {super} }.new.a }.call } - assert_match(/implicit argument passing of super from method defined by define_method/, e.message) end class SubSeq Index: test/ruby/test_iseq.rb =================================================================== --- test/ruby/test_iseq.rb (revision 43211) +++ test/ruby/test_iseq.rb (revision 43212) @@ -49,8 +49,7 @@ class TestISeq < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_iseq.rb#L49 def test_unsupport_type ary = RubyVM::InstructionSequence.compile("p").to_a ary[9] = :foobar - e = assert_raise(TypeError) {RubyVM::InstructionSequence.load(ary)} - assert_match(/:foobar/, e.message) + assert_raise_with_message(TypeError, /:foobar/) {RubyVM::InstructionSequence.load(ary)} end if defined?(RubyVM::InstructionSequence.load) def test_disasm_encoding Index: test/ruby/test_io.rb =================================================================== --- test/ruby/test_io.rb (revision 43211) +++ test/ruby/test_io.rb (revision 43212) @@ -2098,8 +2098,9 @@ End https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L2098 assert_equal(["foo\n", "bar\n", "baz\n"], a, bug) bug6054 = '[ruby-dev:45267]' - e = assert_raise(IOError, bug6054) {IO.foreach(t.path, mode:"w").next} - assert_match(/not opened for reading/, e.message, bug6054) + assert_raise_with_message(IOError, /not opened for reading/, bug6054) do + IO.foreach(t.path, mode:"w").next + end } end Index: test/ruby/test_exception.rb =================================================================== --- test/ruby/test_exception.rb (revision 43211) +++ test/ruby/test_exception.rb (revision 43212) @@ -28,7 +28,7 @@ class TestException < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L28 def test_exception_in_rescue string = "this must be handled no.3" - e = assert_raise(RuntimeError) do + assert_raise_with_message(RuntimeError, string) do begin raise "exception in rescue clause" rescue @@ -36,12 +36,11 @@ class TestException < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L36 end assert(false) end - assert_equal(string, e.message) end def test_exception_in_ensure string = "exception in ensure clause" - e = assert_raise(RuntimeError) do + assert_raise_with_message(RuntimeError, string) do begin raise "this must be handled no.4" ensure @@ -51,7 +50,6 @@ class TestException < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L50 end assert(false) end - assert_equal(string, e.message) end def test_exception_ensure @@ -333,8 +331,10 @@ end.join https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L331 bug3237 = '[ruby-core:29948]' str = "\u2600" id = :"\u2604" - e = assert_raise(NoMethodError) {str.__send__(id)} - assert_equal("undefined method `#{id}' for #{str.inspect}:String", e.message, bug3237) + msg = "undefined method `#{id}' for #{str.inspect}:String" + assert_raise_with_message(NoMethodError, msg, bug3237) do + str.__send__(id) + end end def test_errno Index: test/-ext-/typeddata/test_typeddata.rb =================================================================== --- test/-ext-/typeddata/test_typeddata.rb (revision 43211) +++ test/-ext-/typeddata/test_typeddata.rb (revision 43212) @@ -3,19 +3,14 @@ require "-test-/typ (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/