1 | #--
|
---|
2 | #
|
---|
3 | # Author:: Nathaniel Talbott.
|
---|
4 | # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
|
---|
5 | # License:: Ruby license.
|
---|
6 |
|
---|
7 | module Test
|
---|
8 | module Unit
|
---|
9 |
|
---|
10 | # A collection of tests which can be #run.
|
---|
11 | #
|
---|
12 | # Note: It is easy to confuse a TestSuite instance with
|
---|
13 | # something that has a static suite method; I know because _I_
|
---|
14 | # have trouble keeping them straight. Think of something that
|
---|
15 | # has a suite method as simply providing a way to get a
|
---|
16 | # meaningful TestSuite instance.
|
---|
17 | class TestSuite
|
---|
18 | attr_reader :name, :tests
|
---|
19 |
|
---|
20 | STARTED = name + "::STARTED"
|
---|
21 | FINISHED = name + "::FINISHED"
|
---|
22 |
|
---|
23 | # Creates a new TestSuite with the given name.
|
---|
24 | def initialize(name="Unnamed TestSuite")
|
---|
25 | @name = name
|
---|
26 | @tests = []
|
---|
27 | end
|
---|
28 |
|
---|
29 | # Runs the tests and/or suites contained in this
|
---|
30 | # TestSuite.
|
---|
31 | def run(result, &progress_block)
|
---|
32 | yield(STARTED, name)
|
---|
33 | @tests.each do |test|
|
---|
34 | test.run(result, &progress_block)
|
---|
35 | end
|
---|
36 | yield(FINISHED, name)
|
---|
37 | end
|
---|
38 |
|
---|
39 | # Adds the test to the suite.
|
---|
40 | def <<(test)
|
---|
41 | @tests << test
|
---|
42 | self
|
---|
43 | end
|
---|
44 |
|
---|
45 | def delete(test)
|
---|
46 | @tests.delete(test)
|
---|
47 | end
|
---|
48 |
|
---|
49 | # Retuns the rolled up number of tests in this suite;
|
---|
50 | # i.e. if the suite contains other suites, it counts the
|
---|
51 | # tests within those suites, not the suites themselves.
|
---|
52 | def size
|
---|
53 | total_size = 0
|
---|
54 | @tests.each { |test| total_size += test.size }
|
---|
55 | total_size
|
---|
56 | end
|
---|
57 |
|
---|
58 | def empty?
|
---|
59 | tests.empty?
|
---|
60 | end
|
---|
61 |
|
---|
62 | # Overridden to return the name given the suite at
|
---|
63 | # creation.
|
---|
64 | def to_s
|
---|
65 | @name
|
---|
66 | end
|
---|
67 |
|
---|
68 | # It's handy to be able to compare TestSuite instances.
|
---|
69 | def ==(other)
|
---|
70 | return false unless(other.kind_of?(self.class))
|
---|
71 | return false unless(@name == other.name)
|
---|
72 | @tests == other.tests
|
---|
73 | end
|
---|
74 | end
|
---|
75 | end
|
---|
76 | end
|
---|