1
# Author:: Nathaniel Talbott.
2
# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
3
# License:: Ruby license.
9
class TC_Assertions < TestCase
10
def check(value, message="")
12
raise AssertionFailedError.new(message) unless value
15
def check_assertions(expect_fail, expected_message="",
16
return_value_expected=false)
17
@actual_assertion_count = 0
20
@catch_assertions = true
25
rescue AssertionFailedError => error
26
actual_message = error.message
28
@catch_assertions = false
31
message = "Should have failed, but didn't"
33
message = "Should not have failed, but did with message\n" +
36
check(expect_fail == failed, message)
38
message = "Should have made one assertion but made\n" +
39
"<#{@actual_assertion_count}>"
40
check(1 == @actual_assertion_count, message)
45
check(actual_message == expected_message,
46
"Should have the correct message.\n" +
47
"<#{expected_message.inspect}> expected but was\n" +
48
"<#{actual_message.inspect}>")
50
check(actual_message =~ expected_message,
51
"The message should match correctly.\n" +
52
"</#{expected_message.source}/> expected to match\n" +
53
"<#{actual_message.inspect}>")
56
"Incorrect expected message type in assert_nothing_failed")
59
if return_value_expected
60
check(!return_value.nil?, "Should return a value")
62
check(return_value.nil?,
63
"Should not return a value but returned <#{return_value}>")
70
def check_nothing_fails(return_value_expected=false, &proc)
71
check_assertions(false, "", return_value_expected, &proc)
74
def check_fails(expected_message="", &proc)
75
check_assertions(true, expected_message, &proc)
93
assert_block("successful assert_block") {true}
96
assert_block("successful assert_block") {true}
98
check_fails("assert_block failed.") {
101
check_fails("failed assert_block") {
102
assert_block("failed assert_block") {false}
107
check_nothing_fails{assert("a")}
108
check_nothing_fails{assert(true)}
109
check_nothing_fails{assert(true, "successful assert")}
110
check_fails("<nil> is not true."){assert(nil)}
111
check_fails("<false> is not true."){assert(false)}
112
check_fails("failed assert.\n<false> is not true."){assert(false, "failed assert")}
115
def test_assert_equal
116
check_nothing_fails {
117
assert_equal("string1", "string1")
119
check_nothing_fails {
120
assert_equal( "string1", "string1", "successful assert_equal")
122
check_nothing_fails {
123
assert_equal("string1", "string1", "successful assert_equal")
126
message = <<-EOM.chomp
127
<"string1"> expected but was
136
check_fails(message) {
137
assert_equal("string1", "string2")
140
message = <<-EOM.chomp
142
<"string1"> expected but was
151
check_fails(message) {
152
assert_equal("string1", "string2", "failed assert_equal")
155
message = <<-EOM.chomp
156
<"111111"> expected but was
164
check_fails(message) do
165
assert_equal("111111", 111111)
169
def test_assert_equal_for_too_small_difference
170
message = <<-EOM.chomp
174
check_fails(message) do
179
def test_assert_equal_for_same_inspected_objects
181
now_without_usec = Time.at(now.to_i)
182
message = <<-EOM.chomp
183
<#{now.inspect}> expected but was
186
check_fails(message) do
187
assert_equal(now, now_without_usec)
191
def test_assert_equal_with_multi_lines_result
192
message = <<-EOM.chomp
193
<#{"a\nb".inspect}> expected but was
201
check_fails(message) do
202
assert_equal("a\nb", "x")
206
def test_assert_raise
208
check_nothing_fails(true) {
209
return_value = assert_raise(RuntimeError) {
213
check(return_value.kind_of?(Exception), "Should have returned the exception from a successful assert_raise")
214
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
215
check_nothing_fails(true) {
216
assert_raise(ArgumentError, "successful assert_raise") {
217
raise ArgumentError.new("Error")
220
check_nothing_fails(true) {
221
assert_raise(RuntimeError) {
225
check_nothing_fails(true) {
226
assert_raise(RuntimeError, "successful assert_raise") {
230
check_fails("<RuntimeError> exception expected but none was thrown.") {
231
assert_raise(RuntimeError) {
235
check_fails(%r{\Afailed assert_raise.\n<ArgumentError> exception expected but was\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
236
assert_raise(ArgumentError, "failed assert_raise") {
240
check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
241
assert_nothing_raised(Object) {
246
exceptions = [ArgumentError, TypeError]
247
modules = [Math, Comparable]
248
rescues = exceptions + modules
249
exceptions.each do |exc|
250
check_nothing_fails(true) {
251
return_value = assert_raise(*rescues) {
255
check(return_value.instance_of?(exc), "Should have returned #{exc} but was #{return_value.class}")
256
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
258
modules.each do |mod|
259
check_nothing_fails(true) {
260
return_value = assert_raise(*rescues) {
261
raise Exception.new("Error").extend(mod)
264
check(mod === return_value, "Should have returned #{mod}")
265
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
267
check_fails("<[ArgumentError, TypeError, Math, Comparable]> exception expected but none was thrown.") {
268
assert_raise(*rescues) {
272
check_fails(%r{\Afailed assert_raise.
273
<\[ArgumentError, TypeError\]> exception expected but was
274
Class: <RuntimeError>
278
---------------\Z}m) {
279
assert_raise(ArgumentError, TypeError, "failed assert_raise") {
285
def test_assert_instance_of
286
check_nothing_fails {
287
assert_instance_of(String, "string")
289
check_nothing_fails {
290
assert_instance_of(String, "string", "successful assert_instance_of")
292
check_nothing_fails {
293
assert_instance_of(String, "string", "successful assert_instance_of")
295
check_fails(%Q{<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
296
assert_instance_of(Hash, "string")
298
check_fails(%Q{failed assert_instance_of.\n<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
299
assert_instance_of(Hash, "string", "failed assert_instance_of")
304
check_nothing_fails {
307
check_nothing_fails {
308
assert_nil(nil, "successful assert_nil")
310
check_nothing_fails {
311
assert_nil(nil, "successful assert_nil")
313
check_fails(%Q{<"string"> expected to be nil.}) {
316
check_fails(%Q{failed assert_nil.\n<"string"> expected to be nil.}) {
317
assert_nil("string", "failed assert_nil")
321
def test_assert_not_nil
322
check_nothing_fails{assert_not_nil(false)}
323
check_nothing_fails{assert_not_nil(false, "message")}
324
check_fails("<nil> expected to not be nil."){assert_not_nil(nil)}
325
check_fails("message.\n<nil> expected to not be nil.") {assert_not_nil(nil, "message")}
328
def test_assert_kind_of
329
check_nothing_fails {
330
assert_kind_of(Module, Array)
332
check_nothing_fails {
333
assert_kind_of(Object, "string", "successful assert_kind_of")
335
check_nothing_fails {
336
assert_kind_of(Object, "string", "successful assert_kind_of")
338
check_nothing_fails {
339
assert_kind_of(Comparable, 1)
341
check_fails(%Q{<"string">\nexpected to be kind_of?\n<Class> but was\n<String>.}) {
342
assert_kind_of(Class, "string")
344
check_fails(%Q{failed assert_kind_of.\n<"string">\nexpected to be kind_of?\n<Class> but was\n<String>.}) {
345
assert_kind_of(Class, "string", "failed assert_kind_of")
349
def test_assert_match
350
check_nothing_fails {
351
assert_match(/strin./, "string")
353
check_nothing_fails {
354
assert_match("strin", "string")
356
check_nothing_fails {
357
assert_match(/strin./, "string", "successful assert_match")
359
check_nothing_fails {
360
assert_match(/strin./, "string", "successful assert_match")
362
check_fails(%Q{<"string"> expected to be =~\n</slin./>.}) {
363
assert_match(/slin./, "string")
365
check_fails(%Q{<"string"> expected to be =~\n</strin\\./>.}) {
366
assert_match("strin.", "string")
368
check_fails(%Q{failed assert_match.\n<"string"> expected to be =~\n</slin./>.}) {
369
assert_match(/slin./, "string", "failed assert_match")
375
check_nothing_fails {
376
assert_same(thing, thing)
378
check_nothing_fails {
379
assert_same(thing, thing, "successful assert_same")
381
check_nothing_fails {
382
assert_same(thing, thing, "successful assert_same")
385
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
386
assert_same(thing, thing2)
388
check_fails(%Q{failed assert_same.\n<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
389
assert_same(thing, thing2, "failed assert_same")
393
def test_assert_nothing_raised
394
check_nothing_fails {
395
assert_nothing_raised {
399
check_nothing_fails {
400
assert_nothing_raised("successful assert_nothing_raised") {
404
check_nothing_fails {
405
assert_nothing_raised("successful assert_nothing_raised") {
409
check_nothing_fails {
411
assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") {
412
raise ZeroDivisionError.new("ArgumentError")
414
rescue ZeroDivisionError
417
check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
418
assert_nothing_raised(Object) {
422
check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
423
assert_nothing_raised {
427
check_fails(%r{\Afailed assert_nothing_raised\.\nException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
428
assert_nothing_raised("failed assert_nothing_raised") {
432
check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
433
assert_nothing_raised(StandardError, RuntimeError) {
437
check_fails("Failure.") do
438
assert_nothing_raised do
445
check_fails("Flunked.") {
448
check_fails("flunk message.") {
449
flunk("flunk message")
453
def test_assert_not_same
456
check_nothing_fails {
457
assert_not_same(thing, thing2)
459
check_nothing_fails {
460
assert_not_same(thing, thing2, "message")
462
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
463
assert_not_same(thing, thing)
465
check_fails(%Q{message.\n<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
466
assert_not_same(thing, thing, "message")
470
def test_assert_not_equal
471
check_nothing_fails {
472
assert_not_equal("string1", "string2")
474
check_nothing_fails {
475
assert_not_equal("string1", "string2", "message")
477
check_fails(%Q{<"string"> expected to be != to\n<"string">.}) {
478
assert_not_equal("string", "string")
480
check_fails(%Q{message.\n<"string"> expected to be != to\n<"string">.}) {
481
assert_not_equal("string", "string", "message")
485
def test_assert_no_match
486
check_nothing_fails{assert_no_match(/sling/, "string")}
487
check_nothing_fails{assert_no_match(/sling/, "string", "message")}
488
check_fails(%Q{The first argument to assert_no_match should be a Regexp.\n<"asdf"> expected to be an instance of\n<Regexp> but was\n<String>.}) do
489
assert_no_match("asdf", "asdf")
491
check_fails(%Q{</string/> expected to not match\n<"string">.}) do
492
assert_no_match(/string/, "string")
494
check_fails(%Q{message.\n</string/> expected to not match\n<"string">.}) do
495
assert_no_match(/string/, "string", "message")
499
def test_assert_throws
500
check_nothing_fails do
501
assert_throws(:thing, "message") do
507
check_fails("message.\n" +
508
"<:thing> expected to be thrown but\n" +
509
"<#{inspect_tag(tag)}> was thrown.") do
510
assert_throws(:thing, "message") do
514
check_fails("message.\n" +
515
"<:thing> should have been thrown.") do
516
assert_throws(:thing, "message") do
522
def test_assert_nothing_thrown
523
check_nothing_fails do
524
assert_nothing_thrown("message") do
530
inspected = inspect_tag(tag)
531
check_fails("message.\n" +
532
"<#{inspected}> was thrown when nothing was expected.") do
533
assert_nothing_thrown("message") do
539
def test_assert_operator
540
check_nothing_fails {
541
assert_operator("thing", :==, "thing", "message")
543
check_fails(%Q{<0.15>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to?(:to_str).}) do
544
assert_operator("thing", 0.15, "thing")
546
check_fails(%Q{message.\n<"thing1"> expected to be\n==\n<"thing2">.}) {
547
assert_operator("thing1", :==, "thing2", "message")
551
def test_assert_respond_to
552
check_nothing_fails {
553
assert_respond_to("thing", :to_s, "message")
555
check_nothing_fails {
556
assert_respond_to("thing", "to_s", "message")
558
check_fails("<0.15>\ngiven as the method name argument to #assert_respond_to must be a Symbol or #respond_to?(:to_str).") {
559
assert_respond_to("thing", 0.15)
561
check_fails("message.\n<:symbol>\nof type <Symbol>\nexpected to respond_to?<:non_existent>.") {
562
assert_respond_to(:symbol, :non_existent, "message")
566
def test_assert_in_delta
567
check_nothing_fails {
568
assert_in_delta(1.4, 1.4, 0)
570
check_nothing_fails {
571
assert_in_delta(0.5, 0.4, 0.1, "message")
573
check_nothing_fails {
574
float_thing = Object.new
578
assert_in_delta(0.1, float_thing, 0.1)
580
check_fails("message.\n<0.5> and\n<0.4> expected to be within\n<0.05> of each other.") {
581
assert_in_delta(0.5, 0.4, 0.05, "message")
583
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>.}) {
584
assert_in_delta(Object.new, 0.4, 0.1)
586
check_fails("The delta should not be negative.\n<-0.1> expected to be\n>=\n<0.0>.") {
587
assert_in_delta(0.5, 0.4, -0.1, "message")
595
def return_argument(argument, bogus)
599
check_nothing_fails {
600
assert_send([object, :return_argument, true, "bogus"], "message")
602
check_fails(%r{\Amessage\.\n<.+> expected to respond to\n<return_argument\(\[false, "bogus"\]\)> with a true value.\Z}) {
603
assert_send([object, :return_argument, false, "bogus"], "message")
607
def test_condition_invariant
616
check_nothing_fails do
617
assert_equal(object, object, "message")
621
def test_assert_boolean
622
check_nothing_fails do
625
check_nothing_fails do
626
assert_boolean(false)
629
check_fails("<true> or <false> expected but was\n<1>") do
633
check_fails("<true> or <false> expected but was\n<nil>") do
637
check_fails("message.\n<true> or <false> expected but was\n<\"XXX\">") do
638
assert_boolean("XXX", "message")
643
check_nothing_fails do
647
check_fails("<true> expected but was\n<false>") do
651
check_fails("<true> expected but was\n<1>") do
655
check_fails("message.\n<true> expected but was\n<nil>") do
656
assert_true(nil, "message")
660
def test_assert_false
661
check_nothing_fails do
665
check_fails("<false> expected but was\n<true>") do
669
check_fails("<false> expected but was\n<nil>") do
673
check_fails("message.\n<false> expected but was\n<:false>") do
674
assert_false(:false, "message")
678
def add_failure(message, location=caller)
679
unless @catch_assertions
686
@actual_assertion_count += 1