mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/test/unit/assertions.rb: changed assertion messages to rely more
heavily on #inspect. Added backtrace filtering for exceptions in assertion messages. * test/testunit/test_assertions.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4679 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
22c0d83eb1
commit
28af5f6cf5
3 changed files with 107 additions and 65 deletions
|
@ -1,3 +1,11 @@
|
|||
Sun Oct 5 02:12:00 2003 Nathaniel Talbott <ntalbott@ruby-lang.org>
|
||||
|
||||
* lib/test/unit/assertions.rb: changed assertion messages to rely more
|
||||
heavily on #inspect. Added backtrace filtering for exceptions in
|
||||
assertion messages.
|
||||
|
||||
* test/testunit/test_assertions.rb: ditto.
|
||||
|
||||
Sun Oct 5 02:12:00 2003 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp>
|
||||
|
||||
* lib/drb/acl.rb, lib/drb/ssl.rb: added.
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# License:: Ruby license.
|
||||
|
||||
require 'test/unit/assertionfailederror'
|
||||
require 'test/unit/util/backtracefilter'
|
||||
|
||||
module Test # :nodoc:
|
||||
module Unit # :nodoc:
|
||||
|
@ -120,7 +121,11 @@ module Test # :nodoc:
|
|||
public
|
||||
def assert_respond_to(object, method, message="")
|
||||
_wrap_assertion do
|
||||
assert(method.kind_of?(Symbol) || method.kind_of?(String), "The method argument to #assert_respond_to should be specified as a Symbol or a String.")
|
||||
full_message = build_message('', method) do |arg|
|
||||
"<#{arg}>\n" +
|
||||
"given as the method name argument to #assert_respond_to must be a Symbol or #respond_to?(:to_str)."
|
||||
end
|
||||
assert(method.kind_of?(Symbol) || method.respond_to?(:to_str), full_message)
|
||||
full_message = build_message(message, object, object.class, method) do |arg1, arg2, arg3|
|
||||
"<#{arg1}>\n" +
|
||||
"of type <#{arg2}>\n" +
|
||||
|
@ -134,16 +139,16 @@ module Test # :nodoc:
|
|||
public
|
||||
def assert_match(pattern, string, message="")
|
||||
_wrap_assertion do
|
||||
pattern = case(pattern)
|
||||
when String
|
||||
Regexp.new(Regexp.escape(pattern))
|
||||
else
|
||||
pattern
|
||||
end
|
||||
full_message = build_message(message, string, pattern) do |arg1, arg2|
|
||||
"<#{arg1}> expected to be =~\n" +
|
||||
"<#{arg2}>"
|
||||
end
|
||||
pattern = case(pattern)
|
||||
when String
|
||||
Regexp.new(pattern)
|
||||
else
|
||||
pattern
|
||||
end
|
||||
assert_block(full_message) { string =~ pattern }
|
||||
end
|
||||
end
|
||||
|
@ -153,8 +158,10 @@ module Test # :nodoc:
|
|||
public
|
||||
def assert_same(expected, actual, message="")
|
||||
full_message = build_message(message, expected, expected.__id__, actual, actual.__id__) do |arg1, arg2, arg3, arg4|
|
||||
"<#{arg1}:#{arg2}> expected to be equal? to\n" +
|
||||
"<#{arg3}:#{arg4}>"
|
||||
"<#{arg1}>\n" +
|
||||
"with id <#{arg2}> expected to be equal? to\n" +
|
||||
"<#{arg3}>\n" +
|
||||
"with id <#{arg4}>"
|
||||
end
|
||||
assert_block(full_message) { actual.equal?(expected) }
|
||||
end
|
||||
|
@ -164,12 +171,19 @@ module Test # :nodoc:
|
|||
# true.
|
||||
public
|
||||
def assert_operator(object1, operator, object2, message="")
|
||||
full_message = build_message(message, object1, operator, object2) do |arg1, arg2, arg3|
|
||||
"<#{arg1}> expected to be\n" +
|
||||
"#{arg2}\n" +
|
||||
"<#{arg3}>"
|
||||
_wrap_assertion do
|
||||
full_message = build_message('', operator) do |arg|
|
||||
"<#{arg}>\n" +
|
||||
"given as the operator for #assert_operator must be a Symbol or #respond_to?(:to_str)."
|
||||
end
|
||||
assert(operator.kind_of?(Symbol) || operator.respond_to?(:to_str), full_message)
|
||||
full_message = build_message(message, object1, AssertionMessage.literal(operator), object2) do |arg1, arg2, arg3|
|
||||
"<#{arg1}> expected to be\n" +
|
||||
"#{arg2}\n" +
|
||||
"<#{arg3}>"
|
||||
end
|
||||
assert_block(full_message) { object1.send(operator, object2) }
|
||||
end
|
||||
assert_block(full_message) { object1.send(operator, object2) }
|
||||
end
|
||||
|
||||
# Passes if block does not raise an exception.
|
||||
|
@ -207,8 +221,10 @@ module Test # :nodoc:
|
|||
public
|
||||
def assert_not_same(expected, actual, message="")
|
||||
full_message = build_message(message, expected, expected.__id__, actual, actual.__id__) do |arg1, arg2, arg3, arg4|
|
||||
"<#{arg1}:#{arg2}> expected to not be equal? to\n" +
|
||||
"<#{arg3}:#{arg4}>"
|
||||
"<#{arg1}>\n" +
|
||||
"with id <#{arg2}> expected to not be equal? to\n" +
|
||||
"<#{arg3}>\n" +
|
||||
"with id <#{arg4}>"
|
||||
end
|
||||
assert_block(full_message) { !actual.equal?(expected) }
|
||||
end
|
||||
|
@ -237,9 +253,9 @@ module Test # :nodoc:
|
|||
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, regexp.source, string) do |arg1, arg2|
|
||||
"</#{arg1}/> expected to not match\n" +
|
||||
" <#{arg2}>"
|
||||
full_message = build_message(message, regexp, string) do |arg1, arg2|
|
||||
"<#{arg1}> expected to not match\n" +
|
||||
"<#{arg2}>"
|
||||
end
|
||||
assert_block(full_message) { regexp !~ string }
|
||||
end
|
||||
|
@ -258,16 +274,16 @@ module Test # :nodoc:
|
|||
caught = false
|
||||
end
|
||||
full_message = build_message(message, expected_symbol) do |arg|
|
||||
"<:#{arg}> should have been thrown"
|
||||
"<#{arg}> should have been thrown"
|
||||
end
|
||||
assert(caught, full_message)
|
||||
rescue NameError => name_error
|
||||
if ( name_error.message !~ /^uncaught throw `(.+)'$/ ) #`
|
||||
raise name_error
|
||||
end
|
||||
full_message = build_message(message, expected_symbol, $1) do |arg1, arg2|
|
||||
"<:#{arg1}> expected to be thrown but\n" +
|
||||
"<:#{arg2}> was thrown"
|
||||
full_message = build_message(message, expected_symbol, $1.intern) do |arg1, arg2|
|
||||
"<#{arg1}> expected to be thrown but\n" +
|
||||
"<#{arg2}> was thrown"
|
||||
end
|
||||
flunk(full_message)
|
||||
end
|
||||
|
@ -285,8 +301,8 @@ module Test # :nodoc:
|
|||
if (name_error.message !~ /^uncaught throw `(.+)'$/ ) #`
|
||||
raise name_error
|
||||
end
|
||||
full_message = build_message(message, $1) do |arg|
|
||||
"<:#{arg}> was thrown when nothing was expected"
|
||||
full_message = build_message(message, $1.intern) do |arg|
|
||||
"<#{arg}> was thrown when nothing was expected"
|
||||
end
|
||||
flunk(full_message)
|
||||
end
|
||||
|
@ -319,9 +335,9 @@ module Test # :nodoc:
|
|||
_wrap_assertion do
|
||||
assert_instance_of(Array, send_array, "assert_send requires an array of send information")
|
||||
assert(send_array.size >= 2, "assert_send requires at least a receiver and a message name")
|
||||
full_message = build_message(message, send_array[0], send_array[1], send_array[2..-1]) do |arg1, arg2, arg3|
|
||||
full_message = build_message(message, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1]) do |arg1, arg2, arg3|
|
||||
"<#{arg1}> expected to respond to\n" +
|
||||
"<#{arg2}(#{arg3})> with true"
|
||||
"<#{arg2}(#{arg3})> with a true value"
|
||||
end
|
||||
assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
|
||||
end
|
||||
|
@ -354,20 +370,32 @@ module Test # :nodoc:
|
|||
end
|
||||
|
||||
class AssertionMessage # :nodoc: all
|
||||
def self.convert(object)
|
||||
class Literal
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
|
||||
def inspect
|
||||
@value
|
||||
end
|
||||
end
|
||||
|
||||
def self.literal(value)
|
||||
Literal.new(value)
|
||||
end
|
||||
|
||||
include Util::BacktraceFilter
|
||||
|
||||
def convert(object)
|
||||
case object
|
||||
when String
|
||||
return object
|
||||
when Symbol
|
||||
return object.to_s
|
||||
when Regexp
|
||||
return "/#{object.source}/"
|
||||
when Exception
|
||||
return "Class: <#{object.class}>\n" +
|
||||
"Message: <#{object.message}>\n" +
|
||||
"---Backtrace---\n" +
|
||||
object.backtrace.join("\n") + "\n" +
|
||||
"---------------"
|
||||
return <<EOM.strip
|
||||
Class: <#{object.class}>
|
||||
Message: <#{object.message}>
|
||||
---Backtrace---
|
||||
#{filter_backtrace(object.backtrace).join("\n")}
|
||||
---------------
|
||||
EOM
|
||||
else
|
||||
return object.inspect
|
||||
end
|
||||
|
@ -389,7 +417,7 @@ module Test # :nodoc:
|
|||
end
|
||||
@parameters = @parameters.collect {
|
||||
| parameter |
|
||||
self.class.convert(parameter)
|
||||
convert(parameter)
|
||||
}
|
||||
message_parts << @block.call(*@parameters)
|
||||
return message_parts.join("\n")
|
||||
|
|
|
@ -99,12 +99,15 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_equal("string1", "string1", "successful assert_equal")
|
||||
}
|
||||
check_fails("<string1> expected but was\n<string2>") {
|
||||
check_fails(%Q{<"string1"> expected but was\n<"string2">}) {
|
||||
assert_equal("string1", "string2")
|
||||
}
|
||||
check_fails("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
|
||||
assert_equal("1", 1)
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_raises
|
||||
|
@ -153,10 +156,10 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_instance_of(String, "string", "successful assert_instance_of")
|
||||
}
|
||||
check_fails("<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("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
|
||||
|
@ -171,10 +174,10 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_nil(nil, "successful assert_nil")
|
||||
}
|
||||
check_fails("<nil> expected but was\n<string>") {
|
||||
check_fails(%Q{<nil> expected but was\n<"string">}) {
|
||||
assert_nil("string")
|
||||
}
|
||||
check_fails("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
|
||||
|
@ -192,10 +195,10 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_kind_of(Comparable, 1)
|
||||
}
|
||||
check_fails("<string>\nexpected to be kind_of?<Class>") {
|
||||
check_fails(%Q{<"string">\nexpected to be kind_of?<Class>}) {
|
||||
assert_kind_of(Class, "string")
|
||||
}
|
||||
check_fails("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
|
||||
|
@ -213,13 +216,13 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_match(/strin./, "string", "successful assert_match")
|
||||
}
|
||||
check_fails("<string> expected to be =~\n</slin./>") {
|
||||
check_fails(%Q{<"string"> expected to be =~\n</slin./>}) {
|
||||
assert_match(/slin./, "string")
|
||||
}
|
||||
check_fails("<string> expected to be =~\n<slin>") {
|
||||
assert_match("slin", "string")
|
||||
check_fails(%Q{<"string"> expected to be =~\n</strin\\./>}) {
|
||||
assert_match("strin.", "string")
|
||||
}
|
||||
check_fails("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
|
||||
|
@ -236,10 +239,10 @@ module Test
|
|||
assert_same(thing, thing, "successful assert_same")
|
||||
}
|
||||
thing2 = "thing"
|
||||
check_fails("<#{thing}:#{thing.__id__}> expected to be equal? to\n<#{thing2}:#{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("failed assert_same.\n<#{thing}:#{thing.__id__}> expected to be equal? to\n<#{thing2}:#{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
|
||||
|
@ -308,10 +311,10 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_not_same(thing, thing2, "message")
|
||||
}
|
||||
check_fails("<#{thing}:#{thing.__id__}> expected to not be equal? to\n<#{thing}:#{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("message.\n<#{thing}:#{thing.__id__}> expected to not be equal? to\n<#{thing}:#{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
|
||||
|
@ -323,10 +326,10 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_not_equal("string1", "string2", "message")
|
||||
}
|
||||
check_fails("<string> expected to be != to\n<string>") {
|
||||
check_fails(%Q{<"string"> expected to be != to\n<"string">}) {
|
||||
assert_not_equal("string", "string")
|
||||
}
|
||||
check_fails("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
|
||||
|
@ -353,10 +356,10 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_no_match(/sling/, "string", "message")
|
||||
}
|
||||
check_fails("</string/> expected to not match\n <string>") {
|
||||
check_fails(%Q{</string/> expected to not match\n<"string">}) {
|
||||
assert_no_match(/string/, "string")
|
||||
}
|
||||
check_fails("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
|
||||
|
@ -396,7 +399,10 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_operator("thing", :==, "thing", "message")
|
||||
}
|
||||
check_fails("message.\n<thing1> expected to be\n==\n<thing2>") {
|
||||
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">}) {
|
||||
assert_operator("thing1", :==, "thing2", "message")
|
||||
}
|
||||
end
|
||||
|
@ -408,10 +414,10 @@ module Test
|
|||
check_nothing_fails {
|
||||
assert_respond_to("thing", "to_s", "message")
|
||||
}
|
||||
check_fails("The method argument to #assert_respond_to should be specified as a Symbol or a String.") {
|
||||
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
|
||||
|
@ -433,7 +439,7 @@ module Test
|
|||
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>") {
|
||||
|
@ -452,7 +458,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 true}) {
|
||||
check_fails(%r{message\.\n<.+> expected to respond to\n<return_argument\(\[false, "bogus"\]\)> with a true value}) {
|
||||
assert_send([object, :return_argument, false, "bogus"], "message")
|
||||
}
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue