1 |
|
2 |
#ifndef CPPUNIT_TESTRESULT_H |
3 |
#define CPPUNIT_TESTRESULT_H |
4 |
|
5 |
#include <vector> |
6 |
|
7 |
#include "CppUnitTest/Guards.h" |
8 |
#include "CppUnitTest/TestFailure.h" |
9 |
|
10 |
#include "CppUnitTest/CppUnitTestNamespace.h" |
11 |
BEGIN_NAMESPACE_CPPUNITTEST |
12 |
|
13 |
class CppUnitException; |
14 |
class Test; |
15 |
|
16 |
|
17 |
/* |
18 |
* A TestResult collects the results of executing a test case. It is an |
19 |
* instance of the Collecting Parameter pattern. |
20 |
* |
21 |
* The test framework distinguishes between failures and errors. |
22 |
* A failure is anticipated and checked for with assertions. Errors are |
23 |
* unanticipated problems signified by exceptions that are not generated |
24 |
* by the framework. |
25 |
* |
26 |
* TestResult supplies a template method 'setSynchronizationObject ()' |
27 |
* so that subclasses can provide mutual exclusion in the face of multiple |
28 |
* threads. This can be useful when tests execute in one thread and |
29 |
* they fill a subclass of TestResult which effects change in another |
30 |
* thread. To have mutual exclusion, override setSynchronizationObject () |
31 |
* and make sure that you create an instance of ExclusiveZone at the |
32 |
* beginning of each method. |
33 |
* |
34 |
* see Test |
35 |
*/ |
36 |
|
37 |
class TestResult |
38 |
{ |
39 |
REFERENCEOBJECT (TestResult) |
40 |
|
41 |
public: |
42 |
TestResult (); |
43 |
virtual ~TestResult (); |
44 |
|
45 |
virtual void addError (Test *test, CppUnitException *e); |
46 |
virtual void addFailure (Test *test, CppUnitException *e); |
47 |
virtual void startTest (Test *test); |
48 |
virtual void endTest (Test *test); |
49 |
virtual int runTests (); |
50 |
virtual int testErrors (); |
51 |
virtual int testFailures (); |
52 |
virtual bool wasSuccessful (); |
53 |
virtual bool shouldStop (); |
54 |
virtual void stop (); |
55 |
|
56 |
virtual std::vector<TestFailure *>& errors (); |
57 |
virtual std::vector<TestFailure *>& failures (); |
58 |
|
59 |
|
60 |
class SynchronizationObject |
61 |
{ |
62 |
public: |
63 |
SynchronizationObject () {} |
64 |
virtual ~SynchronizationObject () {} |
65 |
|
66 |
virtual void lock () {} |
67 |
virtual void unlock () {} |
68 |
}; |
69 |
|
70 |
class ExclusiveZone |
71 |
{ |
72 |
SynchronizationObject *m_syncObject; |
73 |
|
74 |
public: |
75 |
ExclusiveZone (SynchronizationObject *syncObject) |
76 |
: m_syncObject (syncObject) |
77 |
{ m_syncObject->lock (); } |
78 |
|
79 |
~ExclusiveZone () |
80 |
{ m_syncObject->unlock (); } |
81 |
}; |
82 |
|
83 |
protected: |
84 |
virtual void setSynchronizationObject (SynchronizationObject *syncObject); |
85 |
|
86 |
std::vector<TestFailure *> m_errors; |
87 |
std::vector<TestFailure *> m_failures; |
88 |
int m_runTests; |
89 |
bool m_stop; |
90 |
SynchronizationObject *m_syncObject; |
91 |
|
92 |
}; |
93 |
|
94 |
|
95 |
|
96 |
// Construct a TestResult |
97 |
inline TestResult::TestResult () |
98 |
: m_syncObject (new SynchronizationObject ()) |
99 |
{ m_runTests = 0; m_stop = false; } |
100 |
|
101 |
|
102 |
// Adds an error to the list of errors. The passed in exception |
103 |
// caused the error |
104 |
inline void TestResult::addError (Test *test, CppUnitException *e) |
105 |
{ ExclusiveZone zone (m_syncObject); m_errors.push_back (new TestFailure (test, e)); } |
106 |
|
107 |
|
108 |
// Adds a failure to the list of failures. The passed in exception |
109 |
// caused the failure. |
110 |
inline void TestResult::addFailure (Test *test, CppUnitException *e) |
111 |
{ ExclusiveZone zone (m_syncObject); m_failures.push_back (new TestFailure (test, e)); } |
112 |
|
113 |
|
114 |
// Informs the result that a test will be started. |
115 |
inline void TestResult::startTest (Test *test) |
116 |
{ ExclusiveZone zone (m_syncObject); m_runTests++; } |
117 |
|
118 |
|
119 |
// Informs the result that a test was completed. |
120 |
inline void TestResult::endTest (Test *test) |
121 |
{ ExclusiveZone zone (m_syncObject); } |
122 |
|
123 |
|
124 |
// Gets the number of run tests. |
125 |
inline int TestResult::runTests () |
126 |
{ ExclusiveZone zone (m_syncObject); return m_runTests; } |
127 |
|
128 |
|
129 |
// Gets the number of detected errors. |
130 |
inline int TestResult::testErrors () |
131 |
{ ExclusiveZone zone (m_syncObject); return m_errors.size (); } |
132 |
|
133 |
|
134 |
// Gets the number of detected failures. |
135 |
inline int TestResult::testFailures () |
136 |
{ ExclusiveZone zone (m_syncObject); return m_failures.size (); } |
137 |
|
138 |
|
139 |
// Returns whether the entire test was successful or not. |
140 |
inline bool TestResult::wasSuccessful () |
141 |
{ ExclusiveZone zone (m_syncObject); return m_failures.size () == 0 && m_errors.size () == 0; } |
142 |
|
143 |
|
144 |
// Returns a vector of the errors. |
145 |
inline std::vector<TestFailure *>& TestResult::errors () |
146 |
{ ExclusiveZone zone (m_syncObject); return m_errors; } |
147 |
|
148 |
|
149 |
// Returns a vector of the failures. |
150 |
inline std::vector<TestFailure *>& TestResult::failures () |
151 |
{ ExclusiveZone zone (m_syncObject); return m_failures; } |
152 |
|
153 |
|
154 |
// Returns whether testing should be stopped |
155 |
inline bool TestResult::shouldStop () |
156 |
{ ExclusiveZone zone (m_syncObject); return m_stop; } |
157 |
|
158 |
|
159 |
// Stop testing |
160 |
inline void TestResult::stop () |
161 |
{ ExclusiveZone zone (m_syncObject); m_stop = true; } |
162 |
|
163 |
|
164 |
// Accept a new synchronization object for protection of this instance |
165 |
// TestResult assumes ownership of the object |
166 |
inline void TestResult::setSynchronizationObject (SynchronizationObject *syncObject) |
167 |
{ delete m_syncObject; m_syncObject = syncObject; } |
168 |
|
169 |
END_NAMESPACE_CPPUNITTEST |
170 |
|
171 |
#endif |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|