~ubuntu-branches/ubuntu/karmic/libcairo-ruby/karmic

« back to all changes in this revision

Viewing changes to test-unit/test/test_assertions.rb

  • Committer: Bazaar Package Importer
  • Author(s): Paul van Tilburg, Gunnar Wolf, Paul van Tilburg
  • Date: 2009-05-05 12:14:31 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090505121431-n803uyjz51je38l0
Tags: 1.8.0-1
[ Gunnar Wolf ]
* Changed section to Ruby as per ftp-masters' request

[ Paul van Tilburg ]
* New upstream release.
* debian/patches:
  - Dropped patch 01_fix-st.h-ruby1.9-paths: fixed by upstream. 
* debian/control:
  - Bumped standards version to 3.8.1; no changes required.
  - Added ${misc:Depends} to the depends of libcairo-ruby (binary).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Author:: Nathaniel Talbott.
 
2
# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
 
3
# License:: Ruby license.
 
4
 
 
5
require 'test/unit'
 
6
 
 
7
module Test
 
8
  module Unit
 
9
    class TC_Assertions < TestCase
 
10
      def check(value, message="")
 
11
        add_assertion
 
12
        raise AssertionFailedError.new(message) unless value
 
13
      end
 
14
 
 
15
      def check_assertions(expect_fail, expected_message="",
 
16
                           return_value_expected=false)
 
17
        @actual_assertion_count = 0
 
18
        failed = true
 
19
        actual_message = nil
 
20
        @catch_assertions = true
 
21
        return_value = nil
 
22
        begin
 
23
          return_value = yield
 
24
          failed = false
 
25
        rescue AssertionFailedError => error
 
26
          actual_message = error.message
 
27
        end
 
28
        @catch_assertions = false
 
29
 
 
30
        if expect_fail
 
31
          message = "Should have failed, but didn't"
 
32
        else
 
33
          message = "Should not have failed, but did with message\n" +
 
34
            "<#{actual_message}>"
 
35
        end
 
36
        check(expect_fail == failed, message)
 
37
 
 
38
        message = "Should have made one assertion but made\n" +
 
39
          "<#{@actual_assertion_count}>"
 
40
        check(1 == @actual_assertion_count, message)
 
41
 
 
42
        if expect_fail
 
43
          case expected_message
 
44
          when String
 
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}>")
 
49
          when Regexp
 
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}>")
 
54
          else
 
55
            check(false,
 
56
                  "Incorrect expected message type in assert_nothing_failed")
 
57
          end
 
58
        else
 
59
          if return_value_expected
 
60
            check(!return_value.nil?, "Should return a value")
 
61
          else
 
62
            check(return_value.nil?,
 
63
                  "Should not return a value but returned <#{return_value}>")
 
64
          end
 
65
        end
 
66
 
 
67
        return_value
 
68
      end
 
69
 
 
70
      def check_nothing_fails(return_value_expected=false, &proc)
 
71
        check_assertions(false, "", return_value_expected, &proc)
 
72
      end
 
73
 
 
74
      def check_fails(expected_message="", &proc)
 
75
        check_assertions(true, expected_message, &proc)
 
76
      end
 
77
 
 
78
      def inspect_tag(tag)
 
79
        begin
 
80
          throw tag
 
81
        rescue NameError
 
82
          tag.to_s.inspect
 
83
        rescue ArgumentError
 
84
          tag.inspect
 
85
        end
 
86
      end
 
87
 
 
88
      def test_assert_block
 
89
        check_nothing_fails {
 
90
          assert_block {true}
 
91
        }
 
92
        check_nothing_fails {
 
93
          assert_block("successful assert_block") {true}
 
94
        }
 
95
        check_nothing_fails {
 
96
          assert_block("successful assert_block") {true}
 
97
        }
 
98
        check_fails("assert_block failed.") {
 
99
          assert_block {false}
 
100
        }
 
101
        check_fails("failed assert_block") {
 
102
          assert_block("failed assert_block") {false}
 
103
        }
 
104
      end
 
105
      
 
106
      def test_assert
 
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")}
 
113
      end
 
114
      
 
115
      def test_assert_equal
 
116
        check_nothing_fails {
 
117
          assert_equal("string1", "string1")
 
118
        }
 
119
        check_nothing_fails {
 
120
          assert_equal( "string1", "string1", "successful assert_equal")
 
121
        }
 
122
        check_nothing_fails {
 
123
          assert_equal("string1", "string1", "successful assert_equal")
 
124
        }
 
125
 
 
126
        message = <<-EOM.chomp
 
127
<"string1"> expected but was
 
128
<"string2">.
 
129
 
 
130
diff:
 
131
- string1
 
132
?       ^
 
133
+ string2
 
134
?       ^
 
135
EOM
 
136
        check_fails(message) {
 
137
          assert_equal("string1", "string2")
 
138
        }
 
139
 
 
140
        message = <<-EOM.chomp
 
141
failed assert_equal.
 
142
<"string1"> expected but was
 
143
<"string2">.
 
144
 
 
145
diff:
 
146
- string1
 
147
?       ^
 
148
+ string2
 
149
?       ^
 
150
EOM
 
151
        check_fails(message) {
 
152
          assert_equal("string1", "string2", "failed assert_equal")
 
153
        }
 
154
 
 
155
        message = <<-EOM.chomp
 
156
<"111111"> expected but was
 
157
<111111>.
 
158
 
 
159
diff:
 
160
- "111111"
 
161
? -      -
 
162
+ 111111
 
163
EOM
 
164
        check_fails(message) do
 
165
          assert_equal("111111", 111111)
 
166
        end
 
167
      end
 
168
 
 
169
      def test_assert_equal_for_too_small_difference
 
170
        message = <<-EOM.chomp
 
171
<1> expected but was
 
172
<2>.
 
173
EOM
 
174
        check_fails(message) do
 
175
          assert_equal(1, 2)
 
176
        end
 
177
      end
 
178
 
 
179
      def test_assert_equal_for_same_inspected_objects
 
180
        now = Time.now
 
181
        now_without_usec = Time.at(now.to_i)
 
182
        message = <<-EOM.chomp
 
183
<#{now.inspect}> expected but was
 
184
<#{now.inspect}>.
 
185
EOM
 
186
        check_fails(message) do
 
187
          assert_equal(now, now_without_usec)
 
188
        end
 
189
      end
 
190
 
 
191
      def test_assert_equal_with_multi_lines_result
 
192
        message = <<-EOM.chomp
 
193
<#{"a\nb".inspect}> expected but was
 
194
<#{"x".inspect}>.
 
195
 
 
196
diff:
 
197
+ x
 
198
- a
 
199
- b
 
200
EOM
 
201
        check_fails(message) do
 
202
          assert_equal("a\nb", "x")
 
203
        end
 
204
      end
 
205
 
 
206
      def test_assert_raise
 
207
        return_value = nil
 
208
        check_nothing_fails(true) {
 
209
          return_value = assert_raise(RuntimeError) {
 
210
            raise "Error"
 
211
          }
 
212
        }
 
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")
 
218
          }
 
219
        }
 
220
        check_nothing_fails(true) {
 
221
          assert_raise(RuntimeError) {
 
222
            raise "Error"
 
223
          }
 
224
        }
 
225
        check_nothing_fails(true) {
 
226
          assert_raise(RuntimeError, "successful assert_raise") {
 
227
            raise "Error"
 
228
          }
 
229
        }
 
230
        check_fails("<RuntimeError> exception expected but none was thrown.") {
 
231
          assert_raise(RuntimeError) {
 
232
            1 + 1
 
233
          }
 
234
        }
 
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") {
 
237
            raise "Error"
 
238
          }
 
239
        }
 
240
        check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
 
241
          assert_nothing_raised(Object) {
 
242
            1 + 1
 
243
          }
 
244
        }
 
245
 
 
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) {
 
252
              raise exc, "Error"
 
253
            }
 
254
          }
 
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")
 
257
        end
 
258
        modules.each do |mod|
 
259
          check_nothing_fails(true) {
 
260
            return_value = assert_raise(*rescues) {
 
261
              raise Exception.new("Error").extend(mod)
 
262
            }
 
263
          }
 
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")
 
266
        end
 
267
        check_fails("<[ArgumentError, TypeError, Math, Comparable]> exception expected but none was thrown.") {
 
268
          assert_raise(*rescues) {
 
269
            1 + 1
 
270
          }
 
271
        }
 
272
        check_fails(%r{\Afailed assert_raise.
 
273
<\[ArgumentError, TypeError\]> exception expected but was
 
274
Class: <RuntimeError>
 
275
Message: <"Error">
 
276
---Backtrace---
 
277
.+
 
278
---------------\Z}m) {
 
279
          assert_raise(ArgumentError, TypeError, "failed assert_raise") {
 
280
            raise "Error"
 
281
          }
 
282
        }
 
283
      end
 
284
      
 
285
      def test_assert_instance_of
 
286
        check_nothing_fails {
 
287
          assert_instance_of(String, "string")
 
288
        }
 
289
        check_nothing_fails {
 
290
          assert_instance_of(String, "string", "successful assert_instance_of")
 
291
        }
 
292
        check_nothing_fails {
 
293
          assert_instance_of(String, "string", "successful assert_instance_of")
 
294
        }
 
295
        check_fails(%Q{<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
 
296
          assert_instance_of(Hash, "string")
 
297
        }
 
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")
 
300
        }
 
301
      end
 
302
      
 
303
      def test_assert_nil
 
304
        check_nothing_fails {
 
305
          assert_nil(nil)
 
306
        }
 
307
        check_nothing_fails {
 
308
          assert_nil(nil, "successful assert_nil")
 
309
        }
 
310
        check_nothing_fails {
 
311
          assert_nil(nil, "successful assert_nil")
 
312
        }
 
313
        check_fails(%Q{<"string"> expected to be nil.}) {
 
314
          assert_nil("string")
 
315
        }
 
316
        check_fails(%Q{failed assert_nil.\n<"string"> expected to be nil.}) {
 
317
          assert_nil("string", "failed assert_nil")
 
318
        }
 
319
      end
 
320
      
 
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")}
 
326
      end
 
327
        
 
328
      def test_assert_kind_of
 
329
        check_nothing_fails {
 
330
          assert_kind_of(Module, Array)
 
331
        }
 
332
        check_nothing_fails {
 
333
          assert_kind_of(Object, "string", "successful assert_kind_of")
 
334
        }
 
335
        check_nothing_fails {
 
336
          assert_kind_of(Object, "string", "successful assert_kind_of")
 
337
        }
 
338
        check_nothing_fails {
 
339
          assert_kind_of(Comparable, 1)
 
340
        }
 
341
        check_fails(%Q{<"string">\nexpected to be kind_of?\n<Class> but was\n<String>.}) {
 
342
          assert_kind_of(Class, "string")
 
343
        }
 
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")
 
346
        }
 
347
      end
 
348
      
 
349
      def test_assert_match
 
350
        check_nothing_fails {
 
351
          assert_match(/strin./, "string")
 
352
        }
 
353
        check_nothing_fails {
 
354
          assert_match("strin", "string")
 
355
        }
 
356
        check_nothing_fails {
 
357
          assert_match(/strin./, "string", "successful assert_match")
 
358
        }
 
359
        check_nothing_fails {
 
360
          assert_match(/strin./, "string", "successful assert_match")
 
361
        }
 
362
        check_fails(%Q{<"string"> expected to be =~\n</slin./>.}) {
 
363
          assert_match(/slin./, "string")
 
364
        }
 
365
        check_fails(%Q{<"string"> expected to be =~\n</strin\\./>.}) {
 
366
          assert_match("strin.", "string")
 
367
        }
 
368
        check_fails(%Q{failed assert_match.\n<"string"> expected to be =~\n</slin./>.}) {
 
369
          assert_match(/slin./, "string", "failed assert_match")
 
370
        }
 
371
      end
 
372
      
 
373
      def test_assert_same
 
374
        thing = "thing"
 
375
        check_nothing_fails {
 
376
          assert_same(thing, thing)
 
377
        }
 
378
        check_nothing_fails {
 
379
          assert_same(thing, thing, "successful assert_same")
 
380
        }
 
381
        check_nothing_fails {
 
382
          assert_same(thing, thing, "successful assert_same")
 
383
        }
 
384
        thing2 = "thing"
 
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)
 
387
        }
 
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")
 
390
        }
 
391
      end
 
392
      
 
393
      def test_assert_nothing_raised
 
394
        check_nothing_fails {
 
395
          assert_nothing_raised {
 
396
            1 + 1
 
397
          }
 
398
        }
 
399
        check_nothing_fails {
 
400
          assert_nothing_raised("successful assert_nothing_raised") {
 
401
            1 + 1
 
402
          }
 
403
        }
 
404
        check_nothing_fails {
 
405
          assert_nothing_raised("successful assert_nothing_raised") {
 
406
            1 + 1
 
407
          }
 
408
        }
 
409
        check_nothing_fails {
 
410
          begin
 
411
            assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") {
 
412
              raise ZeroDivisionError.new("ArgumentError")
 
413
            }
 
414
          rescue ZeroDivisionError
 
415
          end
 
416
        }
 
417
        check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
 
418
          assert_nothing_raised(Object) {
 
419
            1 + 1
 
420
          }
 
421
        }
 
422
        check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
 
423
          assert_nothing_raised {
 
424
            raise "Error"
 
425
          }
 
426
        }
 
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") {
 
429
            raise "Error"
 
430
          }
 
431
        }
 
432
        check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
 
433
          assert_nothing_raised(StandardError, RuntimeError) {
 
434
            raise "Error"
 
435
          }
 
436
        }
 
437
        check_fails("Failure.") do
 
438
          assert_nothing_raised do
 
439
            flunk("Failure")
 
440
          end
 
441
        end
 
442
      end
 
443
      
 
444
      def test_flunk
 
445
        check_fails("Flunked.") {
 
446
          flunk
 
447
        }
 
448
        check_fails("flunk message.") {
 
449
          flunk("flunk message")
 
450
        }
 
451
      end
 
452
      
 
453
      def test_assert_not_same
 
454
        thing = "thing"
 
455
        thing2 = "thing"
 
456
        check_nothing_fails {
 
457
          assert_not_same(thing, thing2)
 
458
        }
 
459
        check_nothing_fails {
 
460
          assert_not_same(thing, thing2, "message")
 
461
        }
 
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)
 
464
        }
 
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")
 
467
        }
 
468
      end
 
469
      
 
470
      def test_assert_not_equal
 
471
        check_nothing_fails {
 
472
          assert_not_equal("string1", "string2")
 
473
        }
 
474
        check_nothing_fails {
 
475
          assert_not_equal("string1", "string2", "message")
 
476
        }
 
477
        check_fails(%Q{<"string"> expected to be != to\n<"string">.}) {
 
478
          assert_not_equal("string", "string")
 
479
        }
 
480
        check_fails(%Q{message.\n<"string"> expected to be != to\n<"string">.}) {
 
481
          assert_not_equal("string", "string", "message")
 
482
        }
 
483
      end
 
484
      
 
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")
 
490
        end
 
491
        check_fails(%Q{</string/> expected to not match\n<"string">.}) do
 
492
          assert_no_match(/string/, "string")
 
493
        end
 
494
        check_fails(%Q{message.\n</string/> expected to not match\n<"string">.}) do
 
495
          assert_no_match(/string/, "string", "message")
 
496
        end
 
497
      end
 
498
      
 
499
      def test_assert_throws
 
500
        check_nothing_fails do
 
501
          assert_throws(:thing, "message") do
 
502
            throw :thing
 
503
          end
 
504
        end
 
505
 
 
506
        tag = :thing2
 
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
 
511
            throw :thing2
 
512
          end
 
513
        end
 
514
        check_fails("message.\n" +
 
515
                    "<:thing> should have been thrown.") do
 
516
          assert_throws(:thing, "message") do
 
517
            1 + 1
 
518
          end
 
519
        end
 
520
      end
 
521
      
 
522
      def test_assert_nothing_thrown
 
523
        check_nothing_fails do
 
524
          assert_nothing_thrown("message") do
 
525
            1 + 1
 
526
          end
 
527
        end
 
528
 
 
529
        tag = :thing
 
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
 
534
            throw tag
 
535
          end
 
536
        end
 
537
      end
 
538
      
 
539
      def test_assert_operator
 
540
        check_nothing_fails {
 
541
          assert_operator("thing", :==, "thing", "message")
 
542
        }
 
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")
 
545
        end
 
546
        check_fails(%Q{message.\n<"thing1"> expected to be\n==\n<"thing2">.}) {
 
547
          assert_operator("thing1", :==, "thing2", "message")
 
548
        }
 
549
      end
 
550
      
 
551
      def test_assert_respond_to
 
552
        check_nothing_fails {
 
553
          assert_respond_to("thing", :to_s, "message")
 
554
        }
 
555
        check_nothing_fails {
 
556
          assert_respond_to("thing", "to_s", "message")
 
557
        }
 
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)
 
560
        }
 
561
        check_fails("message.\n<:symbol>\nof type <Symbol>\nexpected to respond_to?<:non_existent>.") {
 
562
          assert_respond_to(:symbol, :non_existent, "message")
 
563
        }
 
564
      end
 
565
      
 
566
      def test_assert_in_delta
 
567
        check_nothing_fails {
 
568
          assert_in_delta(1.4, 1.4, 0)
 
569
        }
 
570
        check_nothing_fails {
 
571
          assert_in_delta(0.5, 0.4, 0.1, "message")
 
572
        }
 
573
        check_nothing_fails {
 
574
          float_thing = Object.new
 
575
          def float_thing.to_f
 
576
            0.2
 
577
          end
 
578
          assert_in_delta(0.1, float_thing, 0.1)
 
579
        }
 
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")
 
582
        }
 
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)
 
585
        }
 
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")
 
588
        }
 
589
      end
 
590
      
 
591
      def test_assert_send
 
592
        object = Object.new
 
593
        class << object
 
594
          private
 
595
          def return_argument(argument, bogus)
 
596
            return argument
 
597
          end
 
598
        end
 
599
        check_nothing_fails {
 
600
          assert_send([object, :return_argument, true, "bogus"], "message")
 
601
        }
 
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")
 
604
        }
 
605
      end
 
606
      
 
607
      def test_condition_invariant
 
608
        object = Object.new
 
609
        def object.inspect
 
610
          @changed = true
 
611
        end
 
612
        def object.==(other)
 
613
          @changed ||= false
 
614
          return (!@changed)
 
615
        end
 
616
        check_nothing_fails do
 
617
          assert_equal(object, object, "message")
 
618
        end
 
619
      end
 
620
 
 
621
      def test_assert_boolean
 
622
        check_nothing_fails do
 
623
          assert_boolean(true)
 
624
        end
 
625
        check_nothing_fails do
 
626
          assert_boolean(false)
 
627
        end
 
628
 
 
629
        check_fails("<true> or <false> expected but was\n<1>") do
 
630
          assert_boolean(1)
 
631
        end
 
632
 
 
633
        check_fails("<true> or <false> expected but was\n<nil>") do
 
634
          assert_boolean(nil)
 
635
        end
 
636
 
 
637
        check_fails("message.\n<true> or <false> expected but was\n<\"XXX\">") do
 
638
          assert_boolean("XXX", "message")
 
639
        end
 
640
      end
 
641
 
 
642
      def test_assert_true
 
643
        check_nothing_fails do
 
644
          assert_true(true)
 
645
        end
 
646
 
 
647
        check_fails("<true> expected but was\n<false>") do
 
648
          assert_true(false)
 
649
        end
 
650
 
 
651
        check_fails("<true> expected but was\n<1>") do
 
652
          assert_true(1)
 
653
        end
 
654
 
 
655
        check_fails("message.\n<true> expected but was\n<nil>") do
 
656
          assert_true(nil, "message")
 
657
        end
 
658
      end
 
659
 
 
660
      def test_assert_false
 
661
        check_nothing_fails do
 
662
          assert_false(false)
 
663
        end
 
664
 
 
665
        check_fails("<false> expected but was\n<true>") do
 
666
          assert_false(true)
 
667
        end
 
668
 
 
669
        check_fails("<false> expected but was\n<nil>") do
 
670
          assert_false(nil)
 
671
        end
 
672
 
 
673
        check_fails("message.\n<false> expected but was\n<:false>") do
 
674
          assert_false(:false, "message")
 
675
        end
 
676
      end
 
677
 
 
678
      def add_failure(message, location=caller)
 
679
        unless @catch_assertions
 
680
          super
 
681
        end
 
682
      end
 
683
 
 
684
      def add_assertion
 
685
        if @catch_assertions
 
686
          @actual_assertion_count += 1
 
687
        else
 
688
          super
 
689
        end
 
690
      end
 
691
    end
 
692
  end
 
693
end