ruby-changes:29738
From: nobu <ko1@a...>
Date: Fri, 5 Jul 2013 11:30:08 +0900 (JST)
Subject: [ruby-changes:29738] nobu:r41790 (trunk): test/unit: assert_raise_with_message
nobu 2013-07-05 11:29:49 +0900 (Fri, 05 Jul 2013) New Revision: 41790 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41790 Log: 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. Modified files: trunk/ChangeLog trunk/lib/test/unit/assertions.rb trunk/test/fileutils/test_fileutils.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 41789) +++ ChangeLog (revision 41790) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Jul 5 11:29:47 2013 Nobuyoshi Nakada <nobu@r...> + + * 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. + Fri Jul 5 11:08:00 2013 NAKAMURA Usaku <usa@r...> * win32/win32.c (w32_spawn): r41710 made that if the command starts with Index: lib/test/unit/assertions.rb =================================================================== --- lib/test/unit/assertions.rb (revision 41789) +++ lib/test/unit/assertions.rb (revision 41790) @@ -66,6 +66,42 @@ module Test https://github.com/ruby/ruby/blob/trunk/lib/test/unit/assertions.rb#L66 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 Index: test/fileutils/test_fileutils.rb =================================================================== --- test/fileutils/test_fileutils.rb (revision 41789) +++ test/fileutils/test_fileutils.rb (revision 41790) @@ -962,23 +962,23 @@ class TestFileUtils https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L962 assert_equal 0500, File.stat('tmp/a').mode & 07777 end - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod "a", 'tmp/a' } - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod "x+a", 'tmp/a' } - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod "u+z", 'tmp/a' } - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod ",+x", 'tmp/a' } - assert_raises_with_message(ArgumentError, /invalid\b.*\bfile mode/) { + assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) { chmod "755", 'tmp/a' } @@ -1187,29 +1187,11 @@ class TestFileUtils https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L1187 uptodate? Pathname.new('tmp/a'), [Pathname.new('tmp/b'), Pathname.new('tmp/c')] } # [Bug #6708] [ruby-core:46256] - assert_raises_with_message(ArgumentError, "wrong number of arguments (3 for 2)") { + assert_raise_with_message(ArgumentError, "wrong number of arguments (3 for 2)") { uptodate?('new',['old', 'oldest'], {}) } end - def assert_raises_with_message(klass, message) - begin - yield - flunk("Expected Exception #{klass} didn't raise") - rescue klass => ex - if message.kind_of? String - flag = !!(ex.message == message) - assert(flag, "Expected Exception(#{klass}) was raised, but the message doesn't match") - elsif message.kind_of? Regexp - flag = !!(ex.message =~ message) - assert(flag, "Expected Exception(#{klass}) was raised, but the message doesn't match") - else - raise - end - end - end - private :assert_raises_with_message - def test_cd check_singleton :cd end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/