mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/test/unit/assertions.rb: refactored message building.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4685 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0afc6e3f4f
commit
960f29a956
2 changed files with 98 additions and 108 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Sun Oct 5 04:10:00 2003 Nathaniel Talbott <ntalbott@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/test/unit/assertions.rb: refactored message building.
|
||||||
|
|
||||||
Sun Oct 5 03:40:22 2003 GOTOU Yuuzou <gotoyuzo@notwork.org>
|
Sun Oct 5 03:40:22 2003 GOTOU Yuuzou <gotoyuzo@notwork.org>
|
||||||
|
|
||||||
* ext/openssl/ossl_asn1.h: global symbols should be declared
|
* ext/openssl/ossl_asn1.h: global symbols should be declared
|
||||||
|
|
|
@ -49,10 +49,10 @@ module Test # :nodoc:
|
||||||
# of expected and actual.
|
# of expected and actual.
|
||||||
public
|
public
|
||||||
def assert_equal(expected, actual, message=nil)
|
def assert_equal(expected, actual, message=nil)
|
||||||
full_message = build_message(message, expected, actual) do |arg1, arg2|
|
full_message = build_message(message, <<EOT, expected, actual)
|
||||||
"<#{arg1}> expected but was\n" +
|
<?> expected but was
|
||||||
"<#{arg2}>"
|
<?>
|
||||||
end
|
EOT
|
||||||
assert_block(full_message) { expected == actual }
|
assert_block(full_message) { expected == actual }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -62,9 +62,7 @@ module Test # :nodoc:
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
assert_instance_of(Class, expected_exception_klass, "Should expect a class of exception")
|
assert_instance_of(Class, expected_exception_klass, "Should expect a class of exception")
|
||||||
actual_exception = nil
|
actual_exception = nil
|
||||||
full_message = build_message(message, expected_exception_klass) do |arg|
|
full_message = build_message(message, "<?> exception expected but none was thrown", expected_exception_klass)
|
||||||
"<#{arg}> exception expected but none was thrown"
|
|
||||||
end
|
|
||||||
assert_block(full_message) do
|
assert_block(full_message) do
|
||||||
thrown = false
|
thrown = false
|
||||||
begin
|
begin
|
||||||
|
@ -75,10 +73,7 @@ module Test # :nodoc:
|
||||||
end
|
end
|
||||||
thrown
|
thrown
|
||||||
end
|
end
|
||||||
full_message = build_message(message, expected_exception_klass, actual_exception) do |arg1, arg2|
|
full_message = build_message(message, "<?> exception expected but was\n?", expected_exception_klass, actual_exception)
|
||||||
"<#{arg1}> exception expected but was\n" +
|
|
||||||
arg2
|
|
||||||
end
|
|
||||||
assert_block(full_message) { expected_exception_klass == actual_exception.class }
|
assert_block(full_message) { expected_exception_klass == actual_exception.class }
|
||||||
actual_exception
|
actual_exception
|
||||||
end
|
end
|
||||||
|
@ -89,11 +84,11 @@ module Test # :nodoc:
|
||||||
def assert_instance_of(klass, object, message="")
|
def assert_instance_of(klass, object, message="")
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
assert_equal(Class, klass.class, "assert_instance_of takes a Class as its first argument")
|
assert_equal(Class, klass.class, "assert_instance_of takes a Class as its first argument")
|
||||||
full_message = build_message(message, object, klass, object.class) do |arg1, arg2, arg3|
|
full_message = build_message(message, <<EOT, object, klass, object.class)
|
||||||
"<#{arg1}> expected to be an instance of\n" +
|
<?> expected to be an instance of
|
||||||
"<#{arg2}> but was\n" +
|
<?> but was
|
||||||
"<#{arg3}>"
|
<?>
|
||||||
end
|
EOT
|
||||||
assert_block(full_message) { klass == object.class }
|
assert_block(full_message) { klass == object.class }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -109,10 +104,7 @@ module Test # :nodoc:
|
||||||
def assert_kind_of(klass, object, message="")
|
def assert_kind_of(klass, object, message="")
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
assert(klass.kind_of?(Module), "The first parameter to assert_kind_of should be a kind_of Module.")
|
assert(klass.kind_of?(Module), "The first parameter to assert_kind_of should be a kind_of Module.")
|
||||||
full_message = build_message(message, object, klass) do |arg1, arg2|
|
full_message = build_message(message, "<?>\nexpected to be kind_of\\?<?>", object, klass)
|
||||||
"<#{arg1}>\n" +
|
|
||||||
"expected to be kind_of?<#{arg2}>"
|
|
||||||
end
|
|
||||||
assert_block(full_message) { object.kind_of?(klass) }
|
assert_block(full_message) { object.kind_of?(klass) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -121,16 +113,14 @@ module Test # :nodoc:
|
||||||
public
|
public
|
||||||
def assert_respond_to(object, method, message="")
|
def assert_respond_to(object, method, message="")
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
full_message = build_message('', method) do |arg|
|
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)
|
||||||
"<#{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)
|
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|
|
full_message = build_message(message, <<EOT, object, object.class, method)
|
||||||
"<#{arg1}>\n" +
|
<?>
|
||||||
"of type <#{arg2}>\n" +
|
of type <?>
|
||||||
"expected to respond_to?<#{arg3}>"
|
expected to respond_to\\?<?>
|
||||||
end
|
EOT
|
||||||
assert_block(full_message) { object.respond_to?(method) }
|
assert_block(full_message) { object.respond_to?(method) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -145,10 +135,7 @@ module Test # :nodoc:
|
||||||
else
|
else
|
||||||
pattern
|
pattern
|
||||||
end
|
end
|
||||||
full_message = build_message(message, string, pattern) do |arg1, arg2|
|
full_message = build_message(message, "<?> expected to be =~\n<?>", string, pattern)
|
||||||
"<#{arg1}> expected to be =~\n" +
|
|
||||||
"<#{arg2}>"
|
|
||||||
end
|
|
||||||
assert_block(full_message) { string =~ pattern }
|
assert_block(full_message) { string =~ pattern }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -157,12 +144,12 @@ module Test # :nodoc:
|
||||||
# same instance).
|
# same instance).
|
||||||
public
|
public
|
||||||
def assert_same(expected, actual, message="")
|
def assert_same(expected, actual, message="")
|
||||||
full_message = build_message(message, expected, expected.__id__, actual, actual.__id__) do |arg1, arg2, arg3, arg4|
|
full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
|
||||||
"<#{arg1}>\n" +
|
<?>
|
||||||
"with id <#{arg2}> expected to be equal? to\n" +
|
with id <?> expected to be equal\\? to
|
||||||
"<#{arg3}>\n" +
|
<?>
|
||||||
"with id <#{arg4}>"
|
with id <?>
|
||||||
end
|
EOT
|
||||||
assert_block(full_message) { actual.equal?(expected) }
|
assert_block(full_message) { actual.equal?(expected) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -172,16 +159,13 @@ module Test # :nodoc:
|
||||||
public
|
public
|
||||||
def assert_operator(object1, operator, object2, message="")
|
def assert_operator(object1, operator, object2, message="")
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
full_message = build_message('', operator) do |arg|
|
full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
|
||||||
"<#{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)
|
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|
|
full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
|
||||||
"<#{arg1}> expected to be\n" +
|
<?> expected to be
|
||||||
"#{arg2}\n" +
|
?
|
||||||
"<#{arg3}>"
|
<?>
|
||||||
end
|
EOT
|
||||||
assert_block(full_message) { object1.send(operator, object2) }
|
assert_block(full_message) { object1.send(operator, object2) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -198,10 +182,7 @@ module Test # :nodoc:
|
||||||
yield
|
yield
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
if ((args.empty? && !e.instance_of?(AssertionFailedError)) || args.include?(e.class))
|
if ((args.empty? && !e.instance_of?(AssertionFailedError)) || args.include?(e.class))
|
||||||
full_message = build_message(message, e) do |arg1|
|
full_message = build_message(message, "Exception raised:\n?", e)
|
||||||
"Exception raised:\n" +
|
|
||||||
arg1
|
|
||||||
end
|
|
||||||
flunk(full_message)
|
flunk(full_message)
|
||||||
else
|
else
|
||||||
raise e.class, e.message, e.backtrace
|
raise e.class, e.message, e.backtrace
|
||||||
|
@ -220,31 +201,26 @@ module Test # :nodoc:
|
||||||
# Passes if !actual.equal?(expected).
|
# Passes if !actual.equal?(expected).
|
||||||
public
|
public
|
||||||
def assert_not_same(expected, actual, message="")
|
def assert_not_same(expected, actual, message="")
|
||||||
full_message = build_message(message, expected, expected.__id__, actual, actual.__id__) do |arg1, arg2, arg3, arg4|
|
full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
|
||||||
"<#{arg1}>\n" +
|
<?>
|
||||||
"with id <#{arg2}> expected to not be equal? to\n" +
|
with id <?> expected to not be equal\\? to
|
||||||
"<#{arg3}>\n" +
|
<?>
|
||||||
"with id <#{arg4}>"
|
with id <?>
|
||||||
end
|
EOT
|
||||||
assert_block(full_message) { !actual.equal?(expected) }
|
assert_block(full_message) { !actual.equal?(expected) }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Passes if expected != actual.
|
# Passes if expected != actual.
|
||||||
public
|
public
|
||||||
def assert_not_equal(expected, actual, message="")
|
def assert_not_equal(expected, actual, message="")
|
||||||
full_message = build_message(message, expected, actual) do |arg1, arg2|
|
full_message = build_message(message, "<?> expected to be != to\n<?>", expected, actual)
|
||||||
"<#{arg1}> expected to be != to\n" +
|
|
||||||
"<#{arg2}>"
|
|
||||||
end
|
|
||||||
assert_block(full_message) { expected != actual }
|
assert_block(full_message) { expected != actual }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Passes if !object.nil?.
|
# Passes if !object.nil?.
|
||||||
public
|
public
|
||||||
def assert_not_nil(object, message="")
|
def assert_not_nil(object, message="")
|
||||||
full_message = build_message(message, object) do |arg|
|
full_message = build_message(message, "<?> expected to not be nil", object)
|
||||||
"<#{arg}> expected to not be nil"
|
|
||||||
end
|
|
||||||
assert_block(full_message) { !object.nil? }
|
assert_block(full_message) { !object.nil? }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -253,10 +229,7 @@ module Test # :nodoc:
|
||||||
def assert_no_match(regexp, string, message="")
|
def assert_no_match(regexp, string, message="")
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
assert_instance_of(Regexp, regexp, "The first argument to assert_does_not_match should be a Regexp.")
|
assert_instance_of(Regexp, regexp, "The first argument to assert_does_not_match should be a Regexp.")
|
||||||
full_message = build_message(message, regexp, string) do |arg1, arg2|
|
full_message = build_message(message, "<?> expected to not match\n<?>", regexp, string)
|
||||||
"<#{arg1}> expected to not match\n" +
|
|
||||||
"<#{arg2}>"
|
|
||||||
end
|
|
||||||
assert_block(full_message) { regexp !~ string }
|
assert_block(full_message) { regexp !~ string }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -273,18 +246,13 @@ module Test # :nodoc:
|
||||||
proc.call
|
proc.call
|
||||||
caught = false
|
caught = false
|
||||||
end
|
end
|
||||||
full_message = build_message(message, expected_symbol) do |arg|
|
full_message = build_message(message, "<?> should have been thrown", expected_symbol)
|
||||||
"<#{arg}> should have been thrown"
|
|
||||||
end
|
|
||||||
assert(caught, full_message)
|
assert(caught, full_message)
|
||||||
rescue NameError => name_error
|
rescue NameError => name_error
|
||||||
if ( name_error.message !~ /^uncaught throw `(.+)'$/ ) #`
|
if ( name_error.message !~ /^uncaught throw `(.+)'$/ ) #`
|
||||||
raise name_error
|
raise name_error
|
||||||
end
|
end
|
||||||
full_message = build_message(message, expected_symbol, $1.intern) do |arg1, arg2|
|
full_message = build_message(message, "<?> expected to be thrown but\n<?> was thrown", expected_symbol, $1.intern)
|
||||||
"<#{arg1}> expected to be thrown but\n" +
|
|
||||||
"<#{arg2}> was thrown"
|
|
||||||
end
|
|
||||||
flunk(full_message)
|
flunk(full_message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -301,13 +269,10 @@ module Test # :nodoc:
|
||||||
if (name_error.message !~ /^uncaught throw `(.+)'$/ ) #`
|
if (name_error.message !~ /^uncaught throw `(.+)'$/ ) #`
|
||||||
raise name_error
|
raise name_error
|
||||||
end
|
end
|
||||||
full_message = build_message(message, $1.intern) do |arg|
|
full_message = build_message(message, "<?> was thrown when nothing was expected", $1.intern)
|
||||||
"<#{arg}> was thrown when nothing was expected"
|
|
||||||
end
|
|
||||||
flunk(full_message)
|
flunk(full_message)
|
||||||
end
|
end
|
||||||
full_message = build_message(message) { || "Expected nothing to be thrown" }
|
assert(true, "Expected nothing to be thrown")
|
||||||
assert(true, full_message)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -320,11 +285,11 @@ module Test # :nodoc:
|
||||||
assert_respond_to(float, :to_f, "The arguments must respond to to_f; the #{name} did not")
|
assert_respond_to(float, :to_f, "The arguments must respond to to_f; the #{name} did not")
|
||||||
end
|
end
|
||||||
assert_operator(delta, :>=, 0.0, "The delta should not be negative")
|
assert_operator(delta, :>=, 0.0, "The delta should not be negative")
|
||||||
full_message = build_message(message, expected_float, actual_float, delta) do |arg1, arg2, arg3|
|
full_message = build_message(message, <<EOT, expected_float, actual_float, delta)
|
||||||
"<#{arg1}> and\n" +
|
<?> and
|
||||||
"<#{arg2}> expected to be within\n" +
|
<?> expected to be within
|
||||||
"<#{arg3}> of each other"
|
<?> of each other
|
||||||
end
|
EOT
|
||||||
assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f }
|
assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -335,17 +300,18 @@ module Test # :nodoc:
|
||||||
_wrap_assertion do
|
_wrap_assertion do
|
||||||
assert_instance_of(Array, send_array, "assert_send requires an array of send information")
|
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")
|
assert(send_array.size >= 2, "assert_send requires at least a receiver and a message name")
|
||||||
full_message = build_message(message, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1]) do |arg1, arg2, arg3|
|
full_message = build_message(message, <<EOT, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1])
|
||||||
"<#{arg1}> expected to respond to\n" +
|
<?> expected to respond to
|
||||||
"<#{arg2}(#{arg3})> with a true value"
|
<?(?)> with a true value
|
||||||
end
|
EOT
|
||||||
assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
|
assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
public
|
public
|
||||||
def build_message(message, *arguments, &block) # :nodoc:
|
def build_message(head, template, *arguments) # :nodoc:
|
||||||
return AssertionMessage.new(message.to_s, arguments, block)
|
raise "No block should be given to build_message" if(block_given?)
|
||||||
|
return AssertionMessage.new(head, template.chomp("\n"), arguments)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -380,16 +346,41 @@ module Test # :nodoc:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Template
|
||||||
|
def self.create(string)
|
||||||
|
self.new(string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m))
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_reader :count
|
||||||
|
|
||||||
|
def initialize(parts)
|
||||||
|
@parts = parts
|
||||||
|
@count = parts.find_all{|e| e == '?'}.size
|
||||||
|
end
|
||||||
|
|
||||||
|
def result(parameters)
|
||||||
|
raise "The number of parameters does not match the number of substitutions." if(parameters.size != count)
|
||||||
|
params = parameters.dup
|
||||||
|
@parts.collect{|e| e == '?' ? params.shift : e.gsub(/\\\?/m, '?')}.join('')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.literal(value)
|
def self.literal(value)
|
||||||
Literal.new(value)
|
Literal.new(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
include Util::BacktraceFilter
|
include Util::BacktraceFilter
|
||||||
|
|
||||||
|
def initialize(head, template_string, parameters)
|
||||||
|
@head = head
|
||||||
|
@template_string = template_string
|
||||||
|
@parameters = parameters
|
||||||
|
end
|
||||||
|
|
||||||
def convert(object)
|
def convert(object)
|
||||||
case object
|
case object
|
||||||
when Exception
|
when Exception
|
||||||
return <<EOM.strip
|
return <<EOM.chop
|
||||||
Class: <#{object.class}>
|
Class: <#{object.class}>
|
||||||
Message: <#{object.message}>
|
Message: <#{object.message}>
|
||||||
---Backtrace---
|
---Backtrace---
|
||||||
|
@ -401,25 +392,20 @@ EOM
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(message, parameters, block)
|
def template
|
||||||
@message = message
|
@template ||= Template.create(@template_string)
|
||||||
@parameters = parameters
|
|
||||||
@block = block
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
message_parts = []
|
message_parts = []
|
||||||
if (@message != nil && @message != "")
|
if (@head)
|
||||||
if (@message !~ /\.$/)
|
head = @head.to_s
|
||||||
@message << "."
|
unless(head.empty?)
|
||||||
|
head << "." unless(head =~ /\.\Z/)
|
||||||
|
message_parts << head
|
||||||
end
|
end
|
||||||
message_parts << @message
|
|
||||||
end
|
end
|
||||||
@parameters = @parameters.collect {
|
message_parts << template.result(@parameters.collect{|e| convert(e)})
|
||||||
| parameter |
|
|
||||||
convert(parameter)
|
|
||||||
}
|
|
||||||
message_parts << @block.call(*@parameters)
|
|
||||||
return message_parts.join("\n")
|
return message_parts.join("\n")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue