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> Sun Oct 5 04:10:00 2003 Nathaniel Talbott <ntalbott@ruby-lang.org>
* lib/test/unit/assertions.rb: refactored message building. * 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 # The assertion upon which all other assertions are
# based. Passes if the block yields true. # based. Passes if the block yields true.
public public
def assert_block(message="assert_block failed") # :yields: def assert_block(message="assert_block failed.") # :yields:
_wrap_assertion do _wrap_assertion do
if (! yield) if (! yield)
raise AssertionFailedError.new(message.to_s) raise AssertionFailedError.new(message.to_s)
@ -36,10 +36,10 @@ module Test # :nodoc:
# Passes if boolean is true. # Passes if boolean is true.
public public
def assert(boolean, message="assert failed") def assert(boolean, message=nil)
_wrap_assertion do _wrap_assertion do
assert_block("assert should not be called with a block.") { !block_given? } 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
end end
@ -51,7 +51,7 @@ module Test # :nodoc:
def assert_equal(expected, actual, message=nil) def assert_equal(expected, actual, message=nil)
full_message = build_message(message, <<EOT, expected, actual) full_message = build_message(message, <<EOT, expected, actual)
<?> expected but was <?> expected but was
<?> <?>.
EOT EOT
assert_block(full_message) { expected == actual } assert_block(full_message) { expected == actual }
end end
@ -62,7 +62,7 @@ EOT
_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, "<?> 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 assert_block(full_message) do
thrown = false thrown = false
begin begin
@ -87,9 +87,9 @@ EOT
full_message = build_message(message, <<EOT, object, klass, object.class) full_message = build_message(message, <<EOT, object, klass, object.class)
<?> expected to be an instance of <?> expected to be an instance of
<?> but was <?> but was
<?> <?>.
EOT EOT
assert_block(full_message) { klass == object.class } assert_block(full_message){object.instance_of?(klass)}
end end
end end
@ -104,8 +104,8 @@ EOT
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, "<?>\nexpected to be kind_of\\?<?>", object, klass) full_message = build_message(message, "<?>\nexpected to be kind_of\\?<?>.", object, klass)
assert_block(full_message) { object.kind_of?(klass) } assert_block(full_message){object.kind_of?(klass)}
end end
end end
@ -115,11 +115,13 @@ EOT
_wrap_assertion do _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) 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) full_message = build_message(message, <<EOT, object, object.class, method)
<?> <?>
of type <?> of type <?>
expected to respond_to\\?<?> expected to respond_to\\?<?>.
EOT EOT
assert_block(full_message) { object.respond_to?(method) } assert_block(full_message) { object.respond_to?(method) }
end end
@ -135,7 +137,7 @@ EOT
else else
pattern pattern
end 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 } assert_block(full_message) { string =~ pattern }
end end
end end
@ -148,7 +150,7 @@ EOT
<?> <?>
with id <?> expected to be equal\\? to with id <?> expected to be equal\\? to
<?> <?>
with id <?> with id <?>.
EOT EOT
assert_block(full_message) { actual.equal?(expected) } assert_block(full_message) { actual.equal?(expected) }
end end
@ -160,11 +162,11 @@ EOT
def assert_operator(object1, operator, object2, message="") def assert_operator(object1, operator, object2, message="")
_wrap_assertion do _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) 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) full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
<?> expected to be <?> expected to be
? ?
<?> <?>.
EOT EOT
assert_block(full_message) { object1.send(operator, object2) } assert_block(full_message) { object1.send(operator, object2) }
end end
@ -182,8 +184,7 @@ EOT
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, "Exception raised:\n?", e) assert_block(build_message(message, "Exception raised:\n?", e)){false}
flunk(full_message)
else else
raise e.class, e.message, e.backtrace raise e.class, e.message, e.backtrace
end end
@ -194,8 +195,8 @@ EOT
# Always fails. # Always fails.
public public
def flunk(message="Assertion flunked") def flunk(message="Flunked")
assert(false, message) assert_block(build_message(message)){false}
end end
# Passes if !actual.equal?(expected). # Passes if !actual.equal?(expected).
@ -205,7 +206,7 @@ EOT
<?> <?>
with id <?> expected to not be equal\\? to with id <?> expected to not be equal\\? to
<?> <?>
with id <?> with id <?>.
EOT EOT
assert_block(full_message) { !actual.equal?(expected) } assert_block(full_message) { !actual.equal?(expected) }
end end
@ -213,15 +214,16 @@ EOT
# 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 to be != to\n<?>", expected, actual) full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
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, "<?> expected to not be nil", object) warn("Assertions#assert_not_nil is deprecated and will be removed after 1.8.1. Use #assert.")
assert_block(full_message) { !object.nil? } full_message = build_message(message, "<?> expected to not be nil.", object)
assert_block(full_message){!object.nil?}
end end
# Passes if string !~ regularExpression. # Passes if string !~ regularExpression.
@ -229,7 +231,7 @@ EOT
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, "<?> 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 } assert_block(full_message) { regexp !~ string }
end end
end end
@ -239,20 +241,20 @@ EOT
def assert_throws(expected_symbol, message="", &proc) def assert_throws(expected_symbol, message="", &proc)
_wrap_assertion do _wrap_assertion do
assert_instance_of(Symbol, expected_symbol, "assert_throws expects the symbol that should be thrown for its first argument") 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 caught = true
begin begin
catch(expected_symbol) do catch(expected_symbol) do
proc.call proc.call
caught = false caught = false
end end
full_message = build_message(message, "<?> should have been thrown", expected_symbol) full_message = build_message(message, "<?> should have been thrown.", expected_symbol)
assert(caught, full_message) assert_block(full_message){caught}
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 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) flunk(full_message)
end end
end end
@ -288,7 +290,7 @@ EOT
full_message = build_message(message, <<EOT, expected_float, actual_float, delta) full_message = build_message(message, <<EOT, expected_float, actual_float, delta)
<?> and <?> and
<?> expected to be within <?> expected to be within
<?> of each other <?> of each other.
EOT 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
@ -302,16 +304,16 @@ EOT
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, <<EOT, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1]) full_message = build_message(message, <<EOT, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1])
<?> expected to respond to <?> expected to respond to
<?(?)> with a true value <?(?)> with a true value.
EOT 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(head, template, *arguments) # :nodoc: def build_message(head, template=nil, *arguments) # :nodoc:
raise "No block should be given to build_message" if(block_given?) template &&= template.chomp
return AssertionMessage.new(head, template.chomp("\n"), arguments) return AssertionMessage.new(head, template, arguments)
end end
private private
@ -348,7 +350,8 @@ EOT
class Template class Template
def self.create(string) def self.create(string)
self.new(string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m)) parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
self.new(parts)
end end
attr_reader :count attr_reader :count
@ -396,17 +399,21 @@ EOM
@template ||= Template.create(@template_string) @template ||= Template.create(@template_string)
end end
def add_period(string)
(string =~ /\.\Z/ ? string : string + '.')
end
def to_s def to_s
message_parts = [] message_parts = []
if (@head) if (@head)
head = @head.to_s head = @head.to_s
unless(head.empty?) unless(head.empty?)
head << "." unless(head =~ /\.\Z/) message_parts << add_period(head)
message_parts << head
end end
end end
message_parts << template.result(@parameters.collect{|e| convert(e)}) tail = template.result(@parameters.collect{|e| convert(e)})
return message_parts.join("\n") message_parts << tail unless(tail.empty?)
message_parts.join("\n")
end end
end end
end end

View file

@ -66,7 +66,7 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_block("successful assert_block") {true} assert_block("successful assert_block") {true}
} }
check_fails("assert_block failed") { check_fails("assert_block failed.") {
assert_block {false} assert_block {false}
} }
check_fails("failed assert_block") { check_fails("failed assert_block") {
@ -75,18 +75,12 @@ module Test
end end
def test_assert def test_assert
check_nothing_fails { check_nothing_fails{assert("a")}
assert(true) check_nothing_fails{assert(true)}
} check_nothing_fails{assert(true, "successful assert")}
check_nothing_fails { check_fails("<nil> is not true."){assert(nil)}
assert(true, "successful assert") check_fails("<false> is not true."){assert(false)}
} check_fails("failed assert.\n<false> is not true."){assert(false, "failed assert")}
check_fails("assert failed") {
assert(false)
}
check_fails("failed assert") {
assert(false, "failed assert")
}
end end
def test_assert_equal def test_assert_equal
@ -99,13 +93,13 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_equal("string1", "string1", "successful assert_equal") 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") 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") 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) assert_equal("1", 1)
end end
end end
@ -134,12 +128,12 @@ module Test
raise "Error" raise "Error"
} }
} }
check_fails("<RuntimeError> exception expected but none was thrown") { check_fails("<RuntimeError> exception expected but none was thrown.") {
assert_raises(RuntimeError) { assert_raises(RuntimeError) {
1 + 1 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") { assert_raises(ArgumentError, "failed assert_raises") {
raise "Error" raise "Error"
} }
@ -156,10 +150,10 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_instance_of(String, "string", "successful assert_instance_of") 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") 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") assert_instance_of(Hash, "string", "failed assert_instance_of")
} }
end end
@ -174,10 +168,10 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_nil(nil, "successful assert_nil") 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") 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") assert_nil("string", "failed assert_nil")
} }
end end
@ -195,10 +189,10 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_kind_of(Comparable, 1) 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") 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") assert_kind_of(Class, "string", "failed assert_kind_of")
} }
end end
@ -216,13 +210,13 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_match(/strin./, "string", "successful assert_match") 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") 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") 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") assert_match(/slin./, "string", "failed assert_match")
} }
end end
@ -239,10 +233,10 @@ module Test
assert_same(thing, thing, "successful assert_same") assert_same(thing, thing, "successful assert_same")
} }
thing2 = "thing" 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) 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") assert_same(thing, thing2, "failed assert_same")
} }
end end
@ -271,22 +265,22 @@ module Test
rescue ZeroDivisionError rescue ZeroDivisionError
end 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 { assert_nothing_raised {
raise "Error" 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") { assert_nothing_raised("failed assert_nothing_raised") {
raise "Error" 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) { assert_nothing_raised(StandardError, RuntimeError) {
raise "Error" raise "Error"
} }
} }
check_fails("Failure") do check_fails("Failure.") do
assert_nothing_raised do assert_nothing_raised do
flunk("Failure") flunk("Failure")
end end
@ -294,10 +288,10 @@ module Test
end end
def test_flunk def test_flunk
check_fails("Assertion flunked") { check_fails("Flunked.") {
flunk flunk
} }
check_fails("flunk message") { check_fails("flunk message.") {
flunk("flunk message") flunk("flunk message")
} }
end end
@ -311,10 +305,10 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_not_same(thing, thing2, "message") 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) 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") assert_not_same(thing, thing, "message")
} }
end end
@ -326,29 +320,14 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_not_equal("string1", "string2", "message") 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") 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") assert_not_equal("string", "string", "message")
} }
end 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 def test_assert_no_match
check_nothing_fails { check_nothing_fails {
assert_no_match(/sling/, "string") assert_no_match(/sling/, "string")
@ -356,10 +335,10 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_no_match(/sling/, "string", "message") 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") 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") assert_no_match(/string/, "string", "message")
} }
end end
@ -370,12 +349,12 @@ module Test
throw :thing 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") { assert_throws(:thing, "message") {
throw :thing2 throw :thing2
} }
} }
check_fails("message.\n<:thing> should have been thrown") { check_fails("message.\n<:thing> should have been thrown.") {
assert_throws(:thing, "message") { assert_throws(:thing, "message") {
1 + 1 1 + 1
} }
@ -388,7 +367,7 @@ module Test
1 + 1 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") { assert_nothing_thrown("message") {
throw :thing 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 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") assert_operator("thing", 0.15, "thing")
end 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") assert_operator("thing1", :==, "thing2", "message")
} }
end 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).") { 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) 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") assert_respond_to(:symbol, :non_existent, "message")
} }
end end
@ -436,13 +415,13 @@ module Test
end end
assert_in_delta(0.1, float_thing, 0.1) 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") 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) 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") assert_in_delta(0.5, 0.4, -0.1, "message")
} }
end end
@ -458,7 +437,7 @@ module Test
check_nothing_fails { check_nothing_fails {
assert_send([object, :return_argument, true, "bogus"], "message") 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") assert_send([object, :return_argument, false, "bogus"], "message")
} }
end end

View file

@ -28,7 +28,7 @@ module Test
assert_equal(@wrapped_original, @wrapped_munged, "The wrappers should be equivalent") assert_equal(@wrapped_original, @wrapped_munged, "The wrappers should be equivalent")
a_hash = {@wrapped_original => @original} 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 end
end end