Module | Test::Unit::Assertions |
In: |
lib/test/unit/assertions.rb
|
Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is included in Test::Unit::TestCase.
To include it in your own code and use its functionality, you simply need to rescue Test::Unit::AssertionFailedError. Additionally you may override add_assertion to get notified whenever an assertion is made.
Notes:
def deny(boolean, message = nil) message = build_message message, '<?> is not false or nil.', boolean assert_block message do not boolean end end
UncaughtThrow | = | {NameError => /^uncaught throw \`(.+)\'$/, ThreadError => /^uncaught throw \`(.+)\' in thread /} |
Select whether or not to use the pretty-printer. If this option is set to false before any assertions are made, pp.rb will not be required.
# File lib/test/unit/assertions.rb, line 525 525: def self.use_pp=(value) 526: AssertionMessage.use_pp = value 527: end
Asserts that boolean is not false or nil.
Example:
assert [1, 2].include?(5)
# File lib/test/unit/assertions.rb, line 60 60: def assert(boolean, message=nil) 61: _wrap_assertion do 62: assert_block("assert should not be called with a block.") { !block_given? } 63: assert_block(build_message(message, "<?> is not true.", boolean)) { boolean } 64: end 65: end
The assertion upon which all other assertions are based. Passes if the block yields true.
Example:
assert_block "Couldn't do the thing" do do_the_thing end
# File lib/test/unit/assertions.rb, line 45 45: def assert_block(message="assert_block failed.") # :yields: 46: _wrap_assertion do 47: if (! yield) 48: raise AssertionFailedError.new(message.to_s) 49: end 50: end 51: end
Passes if expected == +actual.
Note that the ordering of arguments is important, since a helpful error message is generated when this one fails that tells you the values of expected and actual.
Example:
assert_equal 'MY STRING', 'my string'.upcase
# File lib/test/unit/assertions.rb, line 78 78: def assert_equal(expected, actual, message=nil) 79: full_message = build_message(message, "<?> expected but was\n<?>.\n", expected, actual) 80: assert_block(full_message) { expected == actual } 81: end
Passes if expected_float and actual_float are equal within delta tolerance.
Example:
assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
# File lib/test/unit/assertions.rb, line 445 445: def assert_in_delta(expected_float, actual_float, delta, message="") 446: _wrap_assertion do 447: {expected_float => "first float", actual_float => "second float", delta => "delta"}.each do |float, name| 448: assert_respond_to(float, :to_f, "The arguments must respond to to_f; the #{name} did not") 449: end 450: assert_operator(delta, :>=, 0.0, "The delta should not be negative") 451: full_message = build_message(message, "<?> and\n<?> expected to be within\n<?> of each other.\n", expected_float, actual_float, delta) 452: assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f } 453: end 454: end
Passes if object .instance_of? klass
Example:
assert_instance_of String, 'foo'
# File lib/test/unit/assertions.rb, line 153 153: def assert_instance_of(klass, object, message="") 154: _wrap_assertion do 155: assert_equal(Class, klass.class, "assert_instance_of takes a Class as its first argument") 156: full_message = build_message(message, "<?> expected to be an instance of\n<?> but was\n<?>.\n", object, klass, object.class) 157: assert_block(full_message){object.instance_of?(klass)} 158: end 159: end
Passes if object .kind_of? klass
Example:
assert_kind_of Object, 'foo'
# File lib/test/unit/assertions.rb, line 184 184: def assert_kind_of(klass, object, message="") 185: _wrap_assertion do 186: assert(klass.kind_of?(Module), "The first parameter to assert_kind_of should be a kind_of Module.") 187: full_message = build_message(message, "<?>\nexpected to be kind_of\\?\n<?> but was\n<?>.", object, klass, object.class) 188: assert_block(full_message){object.kind_of?(klass)} 189: end 190: end
Passes if string =~ pattern.
Example:
assert_match(/\d+/, 'five, 6, seven')
# File lib/test/unit/assertions.rb, line 223 223: def assert_match(pattern, string, message="") 224: _wrap_assertion do 225: pattern = case(pattern) 226: when String 227: Regexp.new(Regexp.escape(pattern)) 228: else 229: pattern 230: end 231: full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern) 232: assert_block(full_message) { string =~ pattern } 233: end 234: end
Passes if object is nil.
Example:
assert_nil [1, 2].uniq!
# File lib/test/unit/assertions.rb, line 173 173: def assert_nil(object, message="") 174: assert_equal(nil, object, message) 175: end
Passes if regexp !~ string
Example:
assert_no_match(/two/, 'one 2 three')
# File lib/test/unit/assertions.rb, line 370 370: def assert_no_match(regexp, string, message="") 371: _wrap_assertion do 372: assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.") 373: full_message = build_message(message, "<?> expected to not match\n<?>.", regexp, string) 374: assert_block(full_message) { regexp !~ string } 375: end 376: end
Passes if expected != actual
Example:
assert_not_equal 'some string', 5
# File lib/test/unit/assertions.rb, line 346 346: def assert_not_equal(expected, actual, message="") 347: full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual) 348: assert_block(full_message) { expected != actual } 349: end
Passes if ! object .nil?
Example:
assert_not_nil '1 two 3'.sub!(/two/, '2')
# File lib/test/unit/assertions.rb, line 358 358: def assert_not_nil(object, message="") 359: full_message = build_message(message, "<?> expected to not be nil.", object) 360: assert_block(full_message){!object.nil?} 361: end
Passes if ! actual .equal? expected
Example:
assert_not_same Object.new, Object.new
# File lib/test/unit/assertions.rb, line 328 328: def assert_not_same(expected, actual, message="") 329: full_message = build_message(message, "<?>\nwith id <?> expected to not be equal\\\\? to\n<?>\nwith id <?>.\n", expected, expected.__id__, actual, actual.__id__) 330: assert_block(full_message) { !actual.equal?(expected) } 331: end
Passes if block does not raise an exception.
Example:
assert_nothing_raised do [1, 2].uniq end
# File lib/test/unit/assertions.rb, line 288 288: def assert_nothing_raised(*args) 289: _wrap_assertion do 290: if Module === args.last 291: message = "" 292: else 293: message = args.pop 294: end 295: exceptions, modules = _check_exception_class(args) 296: begin 297: yield 298: rescue Exception => e 299: if ((args.empty? && !e.instance_of?(AssertionFailedError)) || 300: _expected_exception?(e, exceptions, modules)) 301: assert_block(build_message(message, "Exception raised:\n?", e)){false} 302: else 303: raise 304: end 305: end 306: nil 307: end 308: end
Passes if block does not throw anything.
Example:
assert_nothing_thrown do [1, 2].uniq end
# File lib/test/unit/assertions.rb, line 421 421: def assert_nothing_thrown(message="", &proc) 422: _wrap_assertion do 423: assert(block_given?, "Should have passed a block to assert_nothing_thrown") 424: begin 425: proc.call 426: rescue NameError, ThreadError => error 427: if UncaughtThrow[error.class] !~ error.message 428: raise error 429: end 430: full_message = build_message(message, "<?> was thrown when nothing was expected", $1.intern) 431: flunk(full_message) 432: end 433: assert(true, "Expected nothing to be thrown") 434: end 435: end
Compares the +object1+ with +object2+ using operator.
Passes if object1.send(operator, object2) is true.
Example:
assert_operator 5, :>=, 4
# File lib/test/unit/assertions.rb, line 265 265: def assert_operator(object1, operator, object2, message="") 266: _wrap_assertion do 267: full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator) 268: assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)} 269: full_message = build_message(message, "<?> expected to be\n?\n<?>.\n", object1, AssertionMessage.literal(operator), object2) 270: assert_block(full_message) { object1.__send__(operator, object2) } 271: end 272: end
Passes if the block raises one of the given exceptions.
Example:
assert_raise RuntimeError, LoadError do raise 'Boom!!!' end
# File lib/test/unit/assertions.rb, line 111 111: def assert_raise(*args) 112: _wrap_assertion do 113: if Module === args.last 114: message = "" 115: else 116: message = args.pop 117: end 118: exceptions, modules = _check_exception_class(args) 119: expected = args.size == 1 ? args.first : args 120: actual_exception = nil 121: full_message = build_message(message, "<?> exception expected but none was thrown.", expected) 122: assert_block(full_message) do 123: begin 124: yield 125: rescue Exception => actual_exception 126: break 127: end 128: false 129: end 130: full_message = build_message(message, "<?> exception expected but was\n?", expected, actual_exception) 131: assert_block(full_message) {_expected_exception?(actual_exception, exceptions, modules)} 132: actual_exception 133: end 134: end
Alias of assert_raise.
Will be deprecated in 1.9, and removed in 2.0.
# File lib/test/unit/assertions.rb, line 142 142: def assert_raises(*args, &block) 143: assert_raise(*args, &block) 144: end
Passes if object .respond_to? method
Example:
assert_respond_to 'bugbear', :slice
# File lib/test/unit/assertions.rb, line 199 199: def assert_respond_to(object, method, message="") 200: _wrap_assertion do 201: 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) 202: 203: assert_block(full_message) do 204: method.kind_of?(Symbol) || method.respond_to?(:to_str) 205: end 206: full_message = build_message(message, "<?>\nof type <?>\nexpected to respond_to\\\\?<?>.\n", object, object.class, method) 207: assert_block(full_message) { object.respond_to?(method) } 208: end 209: end
Passes if actual .equal? expected (i.e. they are the same instance).
Example:
o = Object.new assert_same o, o
# File lib/test/unit/assertions.rb, line 245 245: def assert_same(expected, actual, message="") 246: full_message = build_message(message, "<?>\nwith id <?> expected to be equal\\\\? to\n<?>\nwith id <?>.\n", expected, expected.__id__, actual, actual.__id__) 247: assert_block(full_message) { actual.equal?(expected) } 248: end
Passes if the method send returns a true value.
send_array is composed of:
Example:
assert_send [[1, 2], :include?, 4]
# File lib/test/unit/assertions.rb, line 473 473: def assert_send(send_array, message="") 474: _wrap_assertion do 475: assert_instance_of(Array, send_array, "assert_send requires an array of send information") 476: assert(send_array.size >= 2, "assert_send requires at least a receiver and a message name") 477: full_message = build_message(message, "<?> expected to respond to\n<?(?)> with a true value.\n", send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1]) 478: assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) } 479: end 480: end
Passes if the block throws expected_symbol
Example:
assert_throws :done do throw :done end
# File lib/test/unit/assertions.rb, line 390 390: def assert_throws(expected_symbol, message="", &proc) 391: _wrap_assertion do 392: assert_instance_of(Symbol, expected_symbol, "assert_throws expects the symbol that should be thrown for its first argument") 393: assert_block("Should have passed a block to assert_throws."){block_given?} 394: caught = true 395: begin 396: catch(expected_symbol) do 397: proc.call 398: caught = false 399: end 400: full_message = build_message(message, "<?> should have been thrown.", expected_symbol) 401: assert_block(full_message){caught} 402: rescue NameError, ThreadError => error 403: if UncaughtThrow[error.class] !~ error.message 404: raise error 405: end 406: full_message = build_message(message, "<?> expected to be thrown but\n<?> was thrown.", expected_symbol, $1.intern) 407: flunk(full_message) 408: end 409: end 410: end
Builds a failure message. head is added before the template and arguments replaces the ’?’s positionally in the template.
# File lib/test/unit/assertions.rb, line 491 491: def build_message(head, template=nil, *arguments) 492: template &&= template.chomp 493: return AssertionMessage.new(head, template, arguments) 494: end
Flunk always fails.
Example:
flunk 'Not done testing yet.'
# File lib/test/unit/assertions.rb, line 317 317: def flunk(message="Flunked") 318: assert_block(build_message(message)){false} 319: end
# File lib/test/unit/assertions.rb, line 497 497: def _wrap_assertion 498: @_assertion_wrapped ||= false 499: unless (@_assertion_wrapped) 500: @_assertion_wrapped = true 501: begin 502: add_assertion 503: return yield 504: ensure 505: @_assertion_wrapped = false 506: end 507: else 508: return yield 509: end 510: end
Called whenever an assertion is made. Define this in classes that include Test::Unit::Assertions to record assertion counts.
# File lib/test/unit/assertions.rb, line 517 517: def add_assertion 518: end