1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/test/unit/assertions.rb: made small improvements to assertion

messages. Deprecated Assertions#assert_not_nil; use #assert instead.

	* test/testunit/test_assertions.rb: ditto.

	* test/testunit/util/test_procwrapper.rb: use #assert instead of
	  #assert_not_nil.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4686 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ntalbott 2003-10-04 21:40:17 +00:00
parent 960f29a956
commit 82f064a524
4 changed files with 100 additions and 104 deletions

View file

@ -1,3 +1,13 @@
Sun Oct 5 :00 2003 Nathaniel Talbott <ntalbott@ruby-lang.org>
* lib/test/unit/assertions.rb: made small improvements to assertion
messages. Deprecated Assertions#assert_not_nil; use #assert instead.
* test/testunit/test_assertions.rb: ditto.
* test/testunit/util/test_procwrapper.rb: use #assert instead of
#assert_not_nil.
Sun Oct 5 04:10:00 2003 Nathaniel Talbott <ntalbott@ruby-lang.org>
* lib/test/unit/assertions.rb: refactored message building.

View file

@ -26,7 +26,7 @@ module Test # :nodoc:
# The assertion upon which all other assertions are
# based. Passes if the block yields true.
public
def assert_block(message="assert_block failed") # :yields:
def assert_block(message="assert_block failed.") # :yields:
_wrap_assertion do
if (! yield)
raise AssertionFailedError.new(message.to_s)
@ -36,10 +36,10 @@ module Test # :nodoc:
# Passes if boolean is true.
public
def assert(boolean, message="assert failed")
def assert(boolean, message=nil)
_wrap_assertion do
assert_block("assert should not be called with a block.") { !block_given? }
assert_block(message) { boolean }
assert_block(build_message(message, "<?> is not true.", boolean)) { boolean }
end
end
@ -51,7 +51,7 @@ module Test # :nodoc:
def assert_equal(expected, actual, message=nil)
full_message = build_message(message, <<EOT, expected, actual)
<?> expected but was
<?>
<?>.
EOT
assert_block(full_message) { expected == actual }
end
@ -62,7 +62,7 @@ EOT
_wrap_assertion do
assert_instance_of(Class, expected_exception_klass, "Should expect a class of exception")
actual_exception = nil
full_message = build_message(message, "<?> exception expected but none was thrown", expected_exception_klass)
full_message = build_message(message, "<?> exception expected but none was thrown.", expected_exception_klass)
assert_block(full_message) do
thrown = false
begin
@ -87,9 +87,9 @@ EOT
full_message = build_message(message, <<EOT, object, klass, object.class)
<?> expected to be an instance of
<?> but was
<?>
<?>.
EOT
assert_block(full_message) { klass == object.class }
assert_block(full_message){object.instance_of?(klass)}
end
end
@ -104,8 +104,8 @@ EOT
def assert_kind_of(klass, object, message="")
_wrap_assertion do
assert(klass.kind_of?(Module), "The first parameter to assert_kind_of should be a kind_of Module.")
full_message = build_message(message, "<?>\nexpected to be kind_of\\?<?>", object, klass)
assert_block(full_message) { object.kind_of?(klass) }
full_message = build_message(message, "<?>\nexpected to be kind_of\\?<?>.", object, klass)
assert_block(full_message){object.kind_of?(klass)}
end
end
@ -115,11 +115,13 @@ EOT
_wrap_assertion do
full_message = build_message(nil, "<?>\ngiven as the method name argument to #assert_respond_to must be a Symbol or #respond_to\\?(:to_str).", method)
assert(method.kind_of?(Symbol) || method.respond_to?(:to_str), full_message)
assert_block(full_message) do
method.kind_of?(Symbol) || method.respond_to?(:to_str)
end
full_message = build_message(message, <<EOT, object, object.class, method)
<?>
of type <?>
expected to respond_to\\?<?>
expected to respond_to\\?<?>.
EOT
assert_block(full_message) { object.respond_to?(method) }
end
@ -135,7 +137,7 @@ EOT
else
pattern
end
full_message = build_message(message, "<?> expected to be =~\n<?>", string, pattern)
full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
assert_block(full_message) { string =~ pattern }
end
end
@ -148,7 +150,7 @@ EOT
<?>
with id <?> expected to be equal\\? to
<?>
with id <?>
with id <?>.
EOT
assert_block(full_message) { actual.equal?(expected) }
end
@ -160,11 +162,11 @@ EOT
def assert_operator(object1, operator, object2, message="")
_wrap_assertion do
full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
assert(operator.kind_of?(Symbol) || operator.respond_to?(:to_str), full_message)
assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
<?> expected to be
?
<?>
<?>.
EOT
assert_block(full_message) { object1.send(operator, object2) }
end
@ -182,8 +184,7 @@ EOT
yield
rescue Exception => e
if ((args.empty? && !e.instance_of?(AssertionFailedError)) || args.include?(e.class))
full_message = build_message(message, "Exception raised:\n?", e)
flunk(full_message)
assert_block(build_message(message, "Exception raised:\n?", e)){false}
else
raise e.class, e.message, e.backtrace
end
@ -194,8 +195,8 @@ EOT
# Always fails.
public
def flunk(message="Assertion flunked")
assert(false, message)
def flunk(message="Flunked")
assert_block(build_message(message)){false}
end
# Passes if !actual.equal?(expected).
@ -205,7 +206,7 @@ EOT
<?>
with id <?> expected to not be equal\\? to
<?>
with id <?>
with id <?>.
EOT
assert_block(full_message) { !actual.equal?(expected) }
end
@ -213,15 +214,16 @@ EOT
# Passes if expected != actual.
public
def assert_not_equal(expected, actual, message="")
full_message = build_message(message, "<?> expected to be != to\n<?>", expected, actual)
full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
assert_block(full_message) { expected != actual }
end
# Passes if !object.nil?.
public
def assert_not_nil(object, message="")
full_message = build_message(message, "<?> expected to not be nil", object)
assert_block(full_message) { !object.nil? }
warn("Assertions#assert_not_nil is deprecated and will be removed after 1.8.1. Use #assert.")
full_message = build_message(message, "<?> expected to not be nil.", object)
assert_block(full_message){!object.nil?}
end
# Passes if string !~ regularExpression.
@ -229,7 +231,7 @@ EOT
def assert_no_match(regexp, string, message="")
_wrap_assertion do
assert_instance_of(Regexp, regexp, "The first argument to assert_does_not_match should be a Regexp.")
full_message = build_message(message, "<?> expected to not match\n<?>", regexp, string)
full_message = build_message(message, "<?> expected to not match\n<?>.", regexp, string)
assert_block(full_message) { regexp !~ string }
end
end
@ -239,20 +241,20 @@ EOT
def assert_throws(expected_symbol, message="", &proc)
_wrap_assertion do
assert_instance_of(Symbol, expected_symbol, "assert_throws expects the symbol that should be thrown for its first argument")
assert(block_given?, "Should have passed a block to assert_throws")
assert_block("Should have passed a block to assert_throws."){block_given?}
caught = true
begin
catch(expected_symbol) do
proc.call
caught = false
end
full_message = build_message(message, "<?> should have been thrown", expected_symbol)
assert(caught, full_message)
full_message = build_message(message, "<?> should have been thrown.", expected_symbol)
assert_block(full_message){caught}
rescue NameError => name_error
if ( name_error.message !~ /^uncaught throw `(.+)'$/ ) #`
raise name_error
end
full_message = build_message(message, "<?> expected to be thrown but\n<?> was thrown", expected_symbol, $1.intern)
full_message = build_message(message, "<?> expected to be thrown but\n<?> was thrown.", expected_symbol, $1.intern)
flunk(full_message)
end
end
@ -288,7 +290,7 @@ EOT
full_message = build_message(message, <<EOT, expected_float, actual_float, delta)
<?> and
<?> expected to be within
<?> of each other
<?> of each other.
EOT
assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f }
end
@ -302,16 +304,16 @@ EOT
assert(send_array.size >= 2, "assert_send requires at least a receiver and a message name")
full_message = build_message(message, <<EOT, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1])
<?> expected to respond to
<?(?)> with a true value
<?(?)> with a true value.
EOT
assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
end
end
public
def build_message(head, template, *arguments) # :nodoc:
raise "No block should be given to build_message" if(block_given?)
return AssertionMessage.new(head, template.chomp("\n"), arguments)
def build_message(head, template=nil, *arguments) # :nodoc:
template &&= template.chomp
return AssertionMessage.new(head, template, arguments)
end
private
@ -348,7 +350,8 @@ EOT
class Template
def self.create(string)
self.new(string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m))
parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
self.new(parts)
end
attr_reader :count
@ -396,17 +399,21 @@ EOM
@template ||= Template.create(@template_string)
end
def add_period(string)
(string =~ /\.\Z/ ? string : string + '.')
end
def to_s
message_parts = []
if (@head)
head = @head.to_s
unless(head.empty?)
head << "." unless(head =~ /\.\Z/)
message_parts << head
message_parts << add_period(head)
end
end
message_parts << template.result(@parameters.collect{|e| convert(e)})
return message_parts.join("\n")
tail = template.result(@parameters.collect{|e| convert(e)})
message_parts << tail unless(tail.empty?)
message_parts.join("\n")
end
end
end

View file

@ -66,7 +66,7 @@ module Test
check_nothing_fails {
assert_block("successful assert_block") {true}
}
check_fails("assert_block failed") {
check_fails("assert_block failed.") {
assert_block {false}
}
check_fails("failed assert_block") {
@ -75,18 +75,12 @@ module Test
end
def test_assert
check_nothing_fails {
assert(true)
}
check_nothing_fails {
assert(true, "successful assert")
}
check_fails("assert failed") {
assert(false)
}
check_fails("failed assert") {
assert(false, "failed assert")
}
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")}
end
def test_assert_equal
@ -99,13 +93,13 @@ module Test
check_nothing_fails {
assert_equal("string1", "string1", "successful assert_equal")
}
check_fails(%Q{<"string1"> expected but was\n<"string2">}) {
check_fails(%Q{<"string1"> expected but was\n<"string2">.}) {
assert_equal("string1", "string2")
}
check_fails(%Q{failed assert_equal.\n<"string1"> expected but was\n<"string2">}) {
check_fails(%Q{failed assert_equal.\n<"string1"> expected but was\n<"string2">.}) {
assert_equal("string1", "string2", "failed assert_equal")
}
check_fails(%Q{<"1"> expected but was\n<1>}) do
check_fails(%Q{<"1"> expected but was\n<1>.}) do
assert_equal("1", 1)
end
end
@ -134,12 +128,12 @@ module Test
raise "Error"
}
}
check_fails("<RuntimeError> exception expected but none was thrown") {
check_fails("<RuntimeError> exception expected but none was thrown.") {
assert_raises(RuntimeError) {
1 + 1
}
}
check_fails(%r{^failed assert_raises.\n<ArgumentError> exception expected but was\nClass: <RuntimeError>\nMessage: <Error>\n---Backtrace---\n.+\n---------------$}m) {
check_fails(%r{\Afailed assert_raises.\n<ArgumentError> exception expected but was\nClass: <RuntimeError>\nMessage: <Error>\n---Backtrace---\n.+\n---------------\Z}m) {
assert_raises(ArgumentError, "failed assert_raises") {
raise "Error"
}
@ -156,10 +150,10 @@ module Test
check_nothing_fails {
assert_instance_of(String, "string", "successful assert_instance_of")
}
check_fails(%Q{<"string"> expected to be an instance of\n<Hash> but was\n<String>}) {
check_fails(%Q{<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
assert_instance_of(Hash, "string")
}
check_fails(%Q{failed assert_instance_of.\n<"string"> expected to be an instance of\n<Hash> but was\n<String>}) {
check_fails(%Q{failed assert_instance_of.\n<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
assert_instance_of(Hash, "string", "failed assert_instance_of")
}
end
@ -174,10 +168,10 @@ module Test
check_nothing_fails {
assert_nil(nil, "successful assert_nil")
}
check_fails(%Q{<nil> expected but was\n<"string">}) {
check_fails(%Q{<nil> expected but was\n<"string">.}) {
assert_nil("string")
}
check_fails(%Q{failed assert_nil.\n<nil> expected but was\n<"string">}) {
check_fails(%Q{failed assert_nil.\n<nil> expected but was\n<"string">.}) {
assert_nil("string", "failed assert_nil")
}
end
@ -195,10 +189,10 @@ module Test
check_nothing_fails {
assert_kind_of(Comparable, 1)
}
check_fails(%Q{<"string">\nexpected to be kind_of?<Class>}) {
check_fails(%Q{<"string">\nexpected to be kind_of?<Class>.}) {
assert_kind_of(Class, "string")
}
check_fails(%Q{failed assert_kind_of.\n<"string">\nexpected to be kind_of?<Class>}) {
check_fails(%Q{failed assert_kind_of.\n<"string">\nexpected to be kind_of?<Class>.}) {
assert_kind_of(Class, "string", "failed assert_kind_of")
}
end
@ -216,13 +210,13 @@ module Test
check_nothing_fails {
assert_match(/strin./, "string", "successful assert_match")
}
check_fails(%Q{<"string"> expected to be =~\n</slin./>}) {
check_fails(%Q{<"string"> expected to be =~\n</slin./>.}) {
assert_match(/slin./, "string")
}
check_fails(%Q{<"string"> expected to be =~\n</strin\\./>}) {
check_fails(%Q{<"string"> expected to be =~\n</strin\\./>.}) {
assert_match("strin.", "string")
}
check_fails(%Q{failed assert_match.\n<"string"> expected to be =~\n</slin./>}) {
check_fails(%Q{failed assert_match.\n<"string"> expected to be =~\n</slin./>.}) {
assert_match(/slin./, "string", "failed assert_match")
}
end
@ -239,10 +233,10 @@ module Test
assert_same(thing, thing, "successful assert_same")
}
thing2 = "thing"
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>}) {
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
assert_same(thing, thing2)
}
check_fails(%Q{failed assert_same.\n<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>}) {
check_fails(%Q{failed assert_same.\n<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
assert_same(thing, thing2, "failed assert_same")
}
end
@ -271,22 +265,22 @@ module Test
rescue ZeroDivisionError
end
}
check_fails(%r{^Exception raised:\nClass: <RuntimeError>\nMessage: <Error>\n---Backtrace---\n.+\n---------------$}m) {
check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <Error>\n---Backtrace---\n.+\n---------------\Z}m) {
assert_nothing_raised {
raise "Error"
}
}
check_fails(%r{^failed assert_nothing_raised\.\nException raised:\nClass: <RuntimeError>\nMessage: <Error>\n---Backtrace---\n.+\n---------------$}m) {
check_fails(%r{\Afailed assert_nothing_raised\.\nException raised:\nClass: <RuntimeError>\nMessage: <Error>\n---Backtrace---\n.+\n---------------\Z}m) {
assert_nothing_raised("failed assert_nothing_raised") {
raise "Error"
}
}
check_fails(%r{^Exception raised:\nClass: <RuntimeError>\nMessage: <Error>\n---Backtrace---\n.+\n---------------$}m) {
check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <Error>\n---Backtrace---\n.+\n---------------\Z}m) {
assert_nothing_raised(StandardError, RuntimeError) {
raise "Error"
}
}
check_fails("Failure") do
check_fails("Failure.") do
assert_nothing_raised do
flunk("Failure")
end
@ -294,10 +288,10 @@ module Test
end
def test_flunk
check_fails("Assertion flunked") {
check_fails("Flunked.") {
flunk
}
check_fails("flunk message") {
check_fails("flunk message.") {
flunk("flunk message")
}
end
@ -311,10 +305,10 @@ module Test
check_nothing_fails {
assert_not_same(thing, thing2, "message")
}
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>}) {
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
assert_not_same(thing, thing)
}
check_fails(%Q{message.\n<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>}) {
check_fails(%Q{message.\n<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
assert_not_same(thing, thing, "message")
}
end
@ -326,29 +320,14 @@ module Test
check_nothing_fails {
assert_not_equal("string1", "string2", "message")
}
check_fails(%Q{<"string"> expected to be != to\n<"string">}) {
check_fails(%Q{<"string"> expected to be != to\n<"string">.}) {
assert_not_equal("string", "string")
}
check_fails(%Q{message.\n<"string"> expected to be != to\n<"string">}) {
check_fails(%Q{message.\n<"string"> expected to be != to\n<"string">.}) {
assert_not_equal("string", "string", "message")
}
end
def test_assert_not_nil
check_nothing_fails {
assert_not_nil("string")
}
check_nothing_fails {
assert_not_nil("string", "message")
}
check_fails("<nil> expected to not be nil") {
assert_not_nil(nil)
}
check_fails("message.\n<nil> expected to not be nil") {
assert_not_nil(nil, "message")
}
end
def test_assert_no_match
check_nothing_fails {
assert_no_match(/sling/, "string")
@ -356,10 +335,10 @@ module Test
check_nothing_fails {
assert_no_match(/sling/, "string", "message")
}
check_fails(%Q{</string/> expected to not match\n<"string">}) {
check_fails(%Q{</string/> expected to not match\n<"string">.}) {
assert_no_match(/string/, "string")
}
check_fails(%Q{message.\n</string/> expected to not match\n<"string">}) {
check_fails(%Q{message.\n</string/> expected to not match\n<"string">.}) {
assert_no_match(/string/, "string", "message")
}
end
@ -370,12 +349,12 @@ module Test
throw :thing
}
}
check_fails("message.\n<:thing> expected to be thrown but\n<:thing2> was thrown") {
check_fails("message.\n<:thing> expected to be thrown but\n<:thing2> was thrown.") {
assert_throws(:thing, "message") {
throw :thing2
}
}
check_fails("message.\n<:thing> should have been thrown") {
check_fails("message.\n<:thing> should have been thrown.") {
assert_throws(:thing, "message") {
1 + 1
}
@ -388,7 +367,7 @@ module Test
1 + 1
}
}
check_fails("message.\n<:thing> was thrown when nothing was expected") {
check_fails("message.\n<:thing> was thrown when nothing was expected.") {
assert_nothing_thrown("message") {
throw :thing
}
@ -402,7 +381,7 @@ module Test
check_fails(%Q{<0.15>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to?(:to_str).}) do
assert_operator("thing", 0.15, "thing")
end
check_fails(%Q{message.\n<"thing1"> expected to be\n==\n<"thing2">}) {
check_fails(%Q{message.\n<"thing1"> expected to be\n==\n<"thing2">.}) {
assert_operator("thing1", :==, "thing2", "message")
}
end
@ -417,7 +396,7 @@ module Test
check_fails("<0.15>\ngiven as the method name argument to #assert_respond_to must be a Symbol or #respond_to?(:to_str).") {
assert_respond_to("thing", 0.15)
}
check_fails("message.\n<:symbol>\nof type <Symbol>\nexpected to respond_to?<:non_existent>") {
check_fails("message.\n<:symbol>\nof type <Symbol>\nexpected to respond_to?<:non_existent>.") {
assert_respond_to(:symbol, :non_existent, "message")
}
end
@ -436,13 +415,13 @@ module Test
end
assert_in_delta(0.1, float_thing, 0.1)
}
check_fails("message.\n<0.5> and\n<0.4> expected to be within\n<0.05> of each other") {
check_fails("message.\n<0.5> and\n<0.4> expected to be within\n<0.05> of each other.") {
assert_in_delta(0.5, 0.4, 0.05, "message")
}
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>}) {
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>.}) {
assert_in_delta(Object.new, 0.4, 0.1)
}
check_fails("The delta should not be negative.\n<-0.1> expected to be\n>=\n<0.0>") {
check_fails("The delta should not be negative.\n<-0.1> expected to be\n>=\n<0.0>.") {
assert_in_delta(0.5, 0.4, -0.1, "message")
}
end
@ -458,7 +437,7 @@ module Test
check_nothing_fails {
assert_send([object, :return_argument, true, "bogus"], "message")
}
check_fails(%r{message\.\n<.+> expected to respond to\n<return_argument\(\[false, "bogus"\]\)> with a true value}) {
check_fails(%r{\Amessage\.\n<.+> expected to respond to\n<return_argument\(\[false, "bogus"\]\)> with a true value.\Z}) {
assert_send([object, :return_argument, false, "bogus"], "message")
}
end

View file

@ -28,7 +28,7 @@ module Test
assert_equal(@wrapped_original, @wrapped_munged, "The wrappers should be equivalent")
a_hash = {@wrapped_original => @original}
assert_not_nil(a_hash[@wrapped_original], "Should be able to access the wrapper in the hash")
assert(a_hash[@wrapped_original], "Should be able to access the wrapper in the hash")
end
end
end