root / lib / test / unit / testsuite.rb @ 16a276fdc77847a92cba21d060b9880eb1fe75bb

View | Annotate | Download (2.8 KB)

1
#--
2
#
3
# Author:: Nathaniel Talbott.
4
# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
5
# License:: Ruby license.
6
7
require 'test/unit/error'
8
9
module Test
10
  module Unit
11
12
    # A collection of tests which can be #run.
13
    #
14
    # Note: It is easy to confuse a TestSuite instance with
15
    # something that has a static suite method; I know because _I_
16
    # have trouble keeping them straight. Think of something that
17
    # has a suite method as simply providing a way to get a
18
    # meaningful TestSuite instance.
19
    class TestSuite
20
      attr_reader :name, :tests
21
      
22
      STARTED = name + "::STARTED"
23
      FINISHED = name + "::FINISHED"
24
25
      # Creates a new TestSuite with the given name.
26
      def initialize(name="Unnamed TestSuite", test_case=nil)
27
        @name = name
28
        @tests = []
29
        @test_case = test_case
30
      end
31
32
      # Runs the tests and/or suites contained in this
33
      # TestSuite.
34
      def run(result, &progress_block)
35
        yield(STARTED, name)
36
        run_startup(result)
37
        @tests.each do |test|
38
          test.run(result, &progress_block)
39
        end
40
        run_shutdown(result)
41
        yield(FINISHED, name)
42
      end
43
44
      # Adds the test to the suite.
45
      def <<(test)
46
        @tests << test
47
        self
48
      end
49
50
      def delete(test)
51
        @tests.delete(test)
52
      end
53
54
      # Retuns the rolled up number of tests in this suite;
55
      # i.e. if the suite contains other suites, it counts the
56
      # tests within those suites, not the suites themselves.
57
      def size
58
        total_size = 0
59
        @tests.each { |test| total_size += test.size }
60
        total_size
61
      end
62
      
63
      def empty?
64
        tests.empty?
65
      end
66
67
      # Overridden to return the name given the suite at
68
      # creation.
69
      def to_s
70
        @name
71
      end
72
      
73
      # It's handy to be able to compare TestSuite instances.
74
      def ==(other)
75
        return false unless(other.kind_of?(self.class))
76
        return false unless(@name == other.name)
77
        @tests == other.tests
78
      end
79
80
      private
81
      def run_startup(result)
82
        return if @test_case.nil? or !@test_case.respond_to?(:startup)
83
        begin
84
          @test_case.startup
85
        rescue Exception
86
          raise unless handle_exception($!, result)
87
        end
88
      end
89
90
      def run_shutdown(result)
91
        return if @test_case.nil? or !@test_case.respond_to?(:shutdown)
92
        begin
93
          @test_case.shutdown
94
        rescue Exception
95
          raise unless handle_exception($!, result)
96
        end
97
      end
98
99
      def handle_exception(exception, result)
100
        case exception
101
        when *ErrorHandler::PASS_THROUGH_EXCEPTIONS
102
          false
103
        else
104
          result.add_error(Error.new(@test_case.name, exception))
105
          true
106
        end
107
      end
108
    end
109
  end
110
end