2003-09-18 22:48:46 -04:00
# Author:: Nathaniel Talbott.
# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
# License:: Ruby license.
2003-10-02 19:03:13 -04:00
require 'test/unit'
2003-09-18 22:48:46 -04:00
module Test
module Unit
class TC_Assertions < TestCase
def check ( value , message = " " )
add_assertion
if ( ! value )
raise AssertionFailedError . new ( message )
end
end
def check_assertions ( expect_fail , expected_message = " " , return_value_expected = false )
@actual_assertion_count = 0
failed = true
actual_message = nil
@catch_assertions = true
return_value = nil
begin
return_value = yield
failed = false
rescue AssertionFailedError = > error
actual_message = error . message
end
@catch_assertions = false
check ( expect_fail == failed , ( expect_fail ? " Should have failed, but didn't " : " Should not have failed, but did with message \n < #{ actual_message } > " ) )
check ( 1 == @actual_assertion_count , " Should have made one assertion but made < #{ @actual_assertion_count } > " )
if ( expect_fail )
case expected_message
when String
check ( actual_message == expected_message , " Should have the correct message. \n < #{ expected_message . inspect } > expected but was \n < #{ actual_message . inspect } > " )
when Regexp
check ( actual_message =~ expected_message , " The message should match correctly. \n </ #{ expected_message . source } /> expected to match \n < #{ actual_message . inspect } > " )
else
check ( false , " Incorrect expected message type in assert_nothing_failed " )
end
else
if ( ! return_value_expected )
check ( return_value . nil? , " Should not return a value but returned < #{ return_value } > " )
else
check ( ! return_value . nil? , " Should return a value " )
end
end
return return_value
end
def check_nothing_fails ( return_value_expected = false , & proc )
check_assertions ( false , " " , return_value_expected , & proc )
end
def check_fails ( expected_message = " " , & proc )
check_assertions ( true , expected_message , & proc )
end
def test_assert_block
check_nothing_fails {
assert_block { true }
}
check_nothing_fails {
assert_block ( " successful assert_block " ) { true }
}
check_nothing_fails {
assert_block ( " successful assert_block " ) { true }
}
2003-10-04 17:40:17 -04:00
check_fails ( " assert_block failed. " ) {
2003-09-18 22:48:46 -04:00
assert_block { false }
}
check_fails ( " failed assert_block " ) {
assert_block ( " failed assert_block " ) { false }
}
end
def test_assert
2003-10-04 17:40:17 -04:00
check_nothing_fails { assert ( " a " ) }
check_nothing_fails { assert ( true ) }
check_nothing_fails { assert ( true , " successful assert " ) }
check_fails ( " <nil> is not true. " ) { assert ( nil ) }
check_fails ( " <false> is not true. " ) { assert ( false ) }
check_fails ( " failed assert. \n <false> is not true. " ) { assert ( false , " failed assert " ) }
2003-09-18 22:48:46 -04:00
end
def test_assert_equal
check_nothing_fails {
assert_equal ( " string1 " , " string1 " )
}
check_nothing_fails {
assert_equal ( " string1 " , " string1 " , " successful assert_equal " )
}
check_nothing_fails {
assert_equal ( " string1 " , " string1 " , " successful assert_equal " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <"string1"> expected but was \n <"string2">. } ) {
2003-09-18 22:48:46 -04:00
assert_equal ( " string1 " , " string2 " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ failed assert_equal. \n <"string1"> expected but was \n <"string2">. } ) {
2003-09-18 22:48:46 -04:00
assert_equal ( " string1 " , " string2 " , " failed assert_equal " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <"1"> expected but was \n <1>. } ) do
2003-10-04 13:18:27 -04:00
assert_equal ( " 1 " , 1 )
end
2003-09-18 22:48:46 -04:00
end
def test_assert_raises
return_value = nil
check_nothing_fails ( true ) {
return_value = assert_raises ( RuntimeError ) {
raise " Error "
}
}
check ( return_value . kind_of? ( Exception ) , " Should have returned the exception from a successful assert_raises " )
check ( return_value . message == " Error " , " Should have returned the correct exception from a successful assert_raises " )
check_nothing_fails ( true ) {
assert_raises ( ArgumentError , " successful assert_raises " ) {
raise ArgumentError . new ( " Error " )
}
}
check_nothing_fails ( true ) {
assert_raises ( RuntimeError ) {
raise " Error "
}
}
check_nothing_fails ( true ) {
assert_raises ( RuntimeError , " successful assert_raises " ) {
raise " Error "
}
}
2003-10-04 17:40:17 -04:00
check_fails ( " <RuntimeError> exception expected but none was thrown. " ) {
2003-09-18 22:48:46 -04:00
assert_raises ( RuntimeError ) {
1 + 1
}
}
2003-10-04 20:59:02 -04:00
check_fails ( %r{ \ Afailed assert_raises. \ n<ArgumentError> exception expected but was \ nClass: <RuntimeError> \ nMessage: <"Error"> \ n---Backtrace--- \ n.+ \ n--------------- \ Z }m ) {
2003-09-18 22:48:46 -04:00
assert_raises ( ArgumentError , " failed assert_raises " ) {
raise " Error "
}
}
end
def test_assert_instance_of
check_nothing_fails {
assert_instance_of ( String , " string " )
}
check_nothing_fails {
assert_instance_of ( String , " string " , " successful assert_instance_of " )
}
check_nothing_fails {
assert_instance_of ( String , " string " , " successful assert_instance_of " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <"string"> expected to be an instance of \n <Hash> but was \n <String>. } ) {
2003-09-18 22:48:46 -04:00
assert_instance_of ( Hash , " string " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ failed assert_instance_of. \n <"string"> expected to be an instance of \n <Hash> but was \n <String>. } ) {
2003-09-18 22:48:46 -04:00
assert_instance_of ( Hash , " string " , " failed assert_instance_of " )
}
end
def test_assert_nil
check_nothing_fails {
assert_nil ( nil )
}
check_nothing_fails {
assert_nil ( nil , " successful assert_nil " )
}
check_nothing_fails {
assert_nil ( nil , " successful assert_nil " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <nil> expected but was \n <"string">. } ) {
2003-09-18 22:48:46 -04:00
assert_nil ( " string " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ failed assert_nil. \n <nil> expected but was \n <"string">. } ) {
2003-09-18 22:48:46 -04:00
assert_nil ( " string " , " failed assert_nil " )
}
end
def test_assert_kind_of
check_nothing_fails {
assert_kind_of ( Module , Array )
}
check_nothing_fails {
assert_kind_of ( Object , " string " , " successful assert_kind_of " )
}
check_nothing_fails {
assert_kind_of ( Object , " string " , " successful assert_kind_of " )
}
check_nothing_fails {
assert_kind_of ( Comparable , 1 )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <"string"> \n expected to be kind_of?<Class>. } ) {
2003-09-18 22:48:46 -04:00
assert_kind_of ( Class , " string " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ failed assert_kind_of. \n <"string"> \n expected to be kind_of?<Class>. } ) {
2003-09-18 22:48:46 -04:00
assert_kind_of ( Class , " string " , " failed assert_kind_of " )
}
end
def test_assert_match
check_nothing_fails {
assert_match ( / strin. / , " string " )
}
check_nothing_fails {
assert_match ( " strin " , " string " )
}
check_nothing_fails {
assert_match ( / strin. / , " string " , " successful assert_match " )
}
check_nothing_fails {
assert_match ( / strin. / , " string " , " successful assert_match " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <"string"> expected to be =~ \n </slin./>. } ) {
2003-09-18 22:48:46 -04:00
assert_match ( / slin. / , " string " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <"string"> expected to be =~ \n </strin \\ ./>. } ) {
2003-10-04 13:18:27 -04:00
assert_match ( " strin. " , " string " )
2003-09-18 22:48:46 -04:00
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ failed assert_match. \n <"string"> expected to be =~ \n </slin./>. } ) {
2003-09-18 22:48:46 -04:00
assert_match ( / slin. / , " string " , " failed assert_match " )
}
end
def test_assert_same
thing = " thing "
check_nothing_fails {
assert_same ( thing , thing )
}
check_nothing_fails {
assert_same ( thing , thing , " successful assert_same " )
}
check_nothing_fails {
assert_same ( thing , thing , " successful assert_same " )
}
thing2 = " thing "
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <"thing"> \n with id < #{ thing . __id__ } > expected to be equal? to \n <"thing"> \n with id < #{ thing2 . __id__ } >. } ) {
2003-09-18 22:48:46 -04:00
assert_same ( thing , thing2 )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ failed assert_same. \n <"thing"> \n with id < #{ thing . __id__ } > expected to be equal? to \n <"thing"> \n with id < #{ thing2 . __id__ } >. } ) {
2003-09-18 22:48:46 -04:00
assert_same ( thing , thing2 , " failed assert_same " )
}
end
def test_assert_nothing_raised
check_nothing_fails {
assert_nothing_raised {
1 + 1
}
}
check_nothing_fails {
assert_nothing_raised ( " successful assert_nothing_raised " ) {
1 + 1
}
}
check_nothing_fails {
assert_nothing_raised ( " successful assert_nothing_raised " ) {
1 + 1
}
}
check_nothing_fails {
begin
assert_nothing_raised ( RuntimeError , StandardError , " successful assert_nothing_raised " ) {
raise ZeroDivisionError . new ( " ArgumentError " )
}
rescue ZeroDivisionError
end
}
2003-10-04 20:59:02 -04:00
check_fails ( %r{ \ AException raised: \ nClass: <RuntimeError> \ nMessage: <"Error"> \ n---Backtrace--- \ n.+ \ n--------------- \ Z }m ) {
2003-09-18 22:48:46 -04:00
assert_nothing_raised {
raise " Error "
}
}
2003-10-04 20:59:02 -04:00
check_fails ( %r{ \ Afailed assert_nothing_raised \ . \ nException raised: \ nClass: <RuntimeError> \ nMessage: <"Error"> \ n---Backtrace--- \ n.+ \ n--------------- \ Z }m ) {
2003-09-18 22:48:46 -04:00
assert_nothing_raised ( " failed assert_nothing_raised " ) {
raise " Error "
}
}
2003-10-04 20:59:02 -04:00
check_fails ( %r{ \ AException raised: \ nClass: <RuntimeError> \ nMessage: <"Error"> \ n---Backtrace--- \ n.+ \ n--------------- \ Z }m ) {
2003-09-18 22:48:46 -04:00
assert_nothing_raised ( StandardError , RuntimeError ) {
raise " Error "
}
}
2003-10-04 17:40:17 -04:00
check_fails ( " Failure. " ) do
2003-10-01 22:20:42 -04:00
assert_nothing_raised do
flunk ( " Failure " )
end
end
2003-09-18 22:48:46 -04:00
end
def test_flunk
2003-10-04 17:40:17 -04:00
check_fails ( " Flunked. " ) {
2003-09-18 22:48:46 -04:00
flunk
}
2003-10-04 17:40:17 -04:00
check_fails ( " flunk message. " ) {
2003-09-18 22:48:46 -04:00
flunk ( " flunk message " )
}
end
def test_assert_not_same
thing = " thing "
thing2 = " thing "
check_nothing_fails {
assert_not_same ( thing , thing2 )
}
check_nothing_fails {
assert_not_same ( thing , thing2 , " message " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <"thing"> \n with id < #{ thing . __id__ } > expected to not be equal? to \n <"thing"> \n with id < #{ thing . __id__ } >. } ) {
2003-09-18 22:48:46 -04:00
assert_not_same ( thing , thing )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ message. \n <"thing"> \n with id < #{ thing . __id__ } > expected to not be equal? to \n <"thing"> \n with id < #{ thing . __id__ } >. } ) {
2003-09-18 22:48:46 -04:00
assert_not_same ( thing , thing , " message " )
}
end
def test_assert_not_equal
check_nothing_fails {
assert_not_equal ( " string1 " , " string2 " )
}
check_nothing_fails {
assert_not_equal ( " string1 " , " string2 " , " message " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ <"string"> expected to be != to \n <"string">. } ) {
2003-09-18 22:48:46 -04:00
assert_not_equal ( " string " , " string " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ message. \n <"string"> expected to be != to \n <"string">. } ) {
2003-09-18 22:48:46 -04:00
assert_not_equal ( " string " , " string " , " message " )
}
end
def test_assert_no_match
check_nothing_fails {
assert_no_match ( / sling / , " string " )
}
check_nothing_fails {
assert_no_match ( / sling / , " string " , " message " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ </string/> expected to not match \n <"string">. } ) {
2003-09-18 22:48:46 -04:00
assert_no_match ( / string / , " string " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ message. \n </string/> expected to not match \n <"string">. } ) {
2003-09-18 22:48:46 -04:00
assert_no_match ( / string / , " string " , " message " )
}
end
def test_assert_throws
check_nothing_fails {
assert_throws ( :thing , " message " ) {
throw :thing
}
}
2003-10-04 17:40:17 -04:00
check_fails ( " message. \n <:thing> expected to be thrown but \n <:thing2> was thrown. " ) {
2003-09-18 22:48:46 -04:00
assert_throws ( :thing , " message " ) {
throw :thing2
}
}
2003-10-04 17:40:17 -04:00
check_fails ( " message. \n <:thing> should have been thrown. " ) {
2003-09-18 22:48:46 -04:00
assert_throws ( :thing , " message " ) {
1 + 1
}
}
end
def test_assert_nothing_thrown
check_nothing_fails {
assert_nothing_thrown ( " message " ) {
1 + 1
}
}
2003-10-04 17:40:17 -04:00
check_fails ( " message. \n <:thing> was thrown when nothing was expected. " ) {
2003-09-18 22:48:46 -04:00
assert_nothing_thrown ( " message " ) {
throw :thing
}
}
end
def test_assert_operator
check_nothing_fails {
assert_operator ( " thing " , :== , " thing " , " message " )
}
2003-10-04 13:18:27 -04:00
check_fails ( %Q{ <0.15> \n given as the operator for # assert_operator must be a Symbol or # respond_to?(:to_str). } ) do
assert_operator ( " thing " , 0 . 15 , " thing " )
end
2003-10-04 17:40:17 -04:00
check_fails ( %Q{ message. \n <"thing1"> expected to be \n == \n <"thing2">. } ) {
2003-09-18 22:48:46 -04:00
assert_operator ( " thing1 " , :== , " thing2 " , " message " )
}
end
def test_assert_respond_to
check_nothing_fails {
assert_respond_to ( " thing " , :to_s , " message " )
}
check_nothing_fails {
assert_respond_to ( " thing " , " to_s " , " message " )
}
2003-10-04 13:18:27 -04:00
check_fails ( " <0.15> \n given as the method name argument to # assert_respond_to must be a Symbol or # respond_to?(:to_str). " ) {
2003-09-18 22:48:46 -04:00
assert_respond_to ( " thing " , 0 . 15 )
}
2003-10-04 17:40:17 -04:00
check_fails ( " message. \n <:symbol> \n of type <Symbol> \n expected to respond_to?<:non_existent>. " ) {
2003-09-18 22:48:46 -04:00
assert_respond_to ( :symbol , :non_existent , " message " )
}
end
def test_assert_in_delta
check_nothing_fails {
assert_in_delta ( 1 . 4 , 1 . 4 , 0 )
}
check_nothing_fails {
assert_in_delta ( 0 . 5 , 0 . 4 , 0 . 1 , " message " )
}
check_nothing_fails {
float_thing = Object . new
def float_thing . to_f
0 . 2
end
assert_in_delta ( 0 . 1 , float_thing , 0 . 1 )
}
2003-10-04 17:40:17 -04:00
check_fails ( " message. \n <0.5> and \n <0.4> expected to be within \n <0.05> of each other. " ) {
2003-09-18 22:48:46 -04:00
assert_in_delta ( 0 . 5 , 0 . 4 , 0 . 05 , " message " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %r{ The arguments must respond to to_f; the first float did not \ . \ n<.+> \ nof type <Object> \ nexpected to respond_to \ ?<:to_f>. } ) {
2003-09-18 22:48:46 -04:00
assert_in_delta ( Object . new , 0 . 4 , 0 . 1 )
}
2003-10-04 17:40:17 -04:00
check_fails ( " The delta should not be negative. \n <-0.1> expected to be \n >= \n <0.0>. " ) {
2003-09-18 22:48:46 -04:00
assert_in_delta ( 0 . 5 , 0 . 4 , - 0 . 1 , " message " )
}
end
def test_assert_send
object = Object . new
class << object
private
def return_argument ( argument , bogus )
return argument
end
end
check_nothing_fails {
assert_send ( [ object , :return_argument , true , " bogus " ] , " message " )
}
2003-10-04 17:40:17 -04:00
check_fails ( %r{ \ Amessage \ . \ n<.+> expected to respond to \ n<return_argument \ ( \ [false, "bogus" \ ] \ )> with a true value. \ Z } ) {
2003-09-18 22:48:46 -04:00
assert_send ( [ object , :return_argument , false , " bogus " ] , " message " )
}
end
def test_condition_invariant
object = Object . new
def object . inspect
@changed = true
end
def object . == ( other )
@changed || = false
return ( ! @changed )
end
check_nothing_fails {
assert_equal ( object , object , " message " )
}
end
def add_failure ( message , location = caller )
if ( ! @catch_assertions )
super
end
end
def add_assertion
if ( ! @catch_assertions )
super
else
@actual_assertion_count += 1
end
end
end
end
end