summaryrefslogtreecommitdiff
path: root/test/testunit/test_testresult.rb
blob: 95d631a082d56ff9167fe89ec35a0f57b3f53df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Author:: Nathaniel Talbott.
# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
# License:: Ruby license.

require 'test/unit/testcase'
require 'test/unit/testresult'

module Test
  module Unit
    class TC_TestResult < TestCase
      def setup
        @my_result = TestResult.new
        @my_result.add_assertion()
        @my_result.add_failure("")
        @my_result.add_error("")
      end
      def test_result_changed_notification
        called1 = false
        @my_result.add_listener( TestResult::CHANGED) {
          |result|
          assert_block("The result should be correct") { result == @my_result }
          called1 = true
        }
        @my_result.add_assertion
        assert_block("Should have been notified when the assertion happened") { called1 }
        
        called1, called2 = false, false
        @my_result.add_listener( TestResult::CHANGED) {
          |result|
          assert_block("The result should be correct") { result == @my_result }
          called2 = true
        }
        @my_result.add_assertion
        assert_block("Both listeners should have been notified for a success") { called1 && called2 }
  
        called1, called2 = false, false
        @my_result.add_failure("")
        assert_block("Both listeners should have been notified for a failure") { called1 && called2 }
  
        called1, called2 = false, false    
        @my_result.add_error("")
        assert_block("Both listeners should have been notified for an error") { called1 && called2 }
  
        called1, called2 = false, false    
        @my_result.add_run
        assert_block("Both listeners should have been notified for a run") { called1 && called2 }
      end
      def test_fault_notification
        called1 = false
        fault = "fault"
        @my_result.add_listener(TestResult::FAULT) {
          | passed_fault |
          assert_block("The fault should be correct") { passed_fault == fault }
          called1 = true
        }
  
        @my_result.add_assertion
        assert_block("Should not have been notified when the assertion happened") { !called1 }
        
        @my_result.add_failure(fault)
        assert_block("Should have been notified when the failure happened") { called1 }
        
        called1, called2 = false, false
        @my_result.add_listener(TestResult::FAULT) {
          | passed_fault |
          assert_block("The fault should be correct") { passed_fault == fault }
          called2 = true
        }
  
        @my_result.add_assertion
        assert_block("Neither listener should have been notified for a success") { !(called1 || called2) }
  
        called1, called2 = false, false
        @my_result.add_failure(fault)
        assert_block("Both listeners should have been notified for a failure") { called1 && called2 }
  
        called1, called2 = false, false    
        @my_result.add_error(fault)
        assert_block("Both listeners should have been notified for an error") { called1 && called2 }
  
        called1, called2 = false, false
        @my_result.add_run
        assert_block("Neither listener should have been notified for a run") { !(called1 || called2) }
      end
      def test_passed?
        result = TestResult.new
        assert(result.passed?, "An empty result should have passed")
  
        result.add_assertion
        assert(result.passed?, "Adding an assertion should not cause the result to not pass")
  
        result.add_run
        assert(result.passed?, "Adding a run should not cause the result to not pass")
  
        result.add_failure("")
        assert(!result.passed?, "Adding a failed assertion should cause the result to not pass")
  
        result = TestResult.new
        result.add_error("")
        assert(!result.passed?, "Adding an error should cause the result to not pass")
      end
    end
  end
end