mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44172 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									3ba2006aa4
								
							
						
					
					
						commit
						287d2adab0
					
				
					 2 changed files with 10 additions and 5 deletions
				
			
		| 
						 | 
					@ -88,7 +88,7 @@ module Test
 | 
				
			||||||
      #    assert_raise_with_message(RuntimeError, "foo") do
 | 
					      #    assert_raise_with_message(RuntimeError, "foo") do
 | 
				
			||||||
      #      raise "foo" #Raises RuntimeError with the message, so assertion succeeds
 | 
					      #      raise "foo" #Raises RuntimeError with the message, so assertion succeeds
 | 
				
			||||||
      #    end
 | 
					      #    end
 | 
				
			||||||
      def assert_raise_with_message(exception, expected, msg = nil)
 | 
					      def assert_raise_with_message(exception, expected, msg = nil, &block)
 | 
				
			||||||
        case expected
 | 
					        case expected
 | 
				
			||||||
        when String
 | 
					        when String
 | 
				
			||||||
          assert = :assert_equal
 | 
					          assert = :assert_equal
 | 
				
			||||||
| 
						 | 
					@ -100,7 +100,14 @@ module Test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ex = assert_raise(exception, *msg) {yield}
 | 
					        ex = assert_raise(exception, *msg) {yield}
 | 
				
			||||||
        msg = message(msg, "") {"Expected Exception(#{exception}) was raised, but the message doesn't match"}
 | 
					        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
 | 
					        ex
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,7 +3,7 @@ require 'test/unit'
 | 
				
			||||||
class TestArity < Test::Unit::TestCase
 | 
					class TestArity < Test::Unit::TestCase
 | 
				
			||||||
  def err_mess(method_proc = nil, argc = 0)
 | 
					  def err_mess(method_proc = nil, argc = 0)
 | 
				
			||||||
    args = (1..argc).to_a
 | 
					    args = (1..argc).to_a
 | 
				
			||||||
    err = assert_raise(ArgumentError) do
 | 
					    assert_raise_with_message(ArgumentError, /wrong number of arguments \((.*)\)/) do
 | 
				
			||||||
      case method_proc
 | 
					      case method_proc
 | 
				
			||||||
      when nil
 | 
					      when nil
 | 
				
			||||||
        yield
 | 
					        yield
 | 
				
			||||||
| 
						 | 
					@ -13,8 +13,6 @@ class TestArity < Test::Unit::TestCase
 | 
				
			||||||
        method_proc.call(*args)
 | 
					        method_proc.call(*args)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    s = err.to_s
 | 
					 | 
				
			||||||
    assert s =~ /wrong number of arguments \((.*)\)/, "Unexpected ArgumentError's message: #{s}"
 | 
					 | 
				
			||||||
    $1
 | 
					    $1
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue