forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestResult.h
More file actions
231 lines (176 loc) · 4.68 KB
/
TestResult.h
File metadata and controls
231 lines (176 loc) · 4.68 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// TestResult.h
//
// $Id: //poco/1.4/CppUnit/include/CppUnit/TestResult.h#1 $
//
#ifndef CppUnit_TestResult_INCLUDED
#define CppUnit_TestResult_INCLUDED
#include "CppUnit/CppUnit.h"
#include "CppUnit/Guards.h"
#include "CppUnit/TestFailure.h"
#include <vector>
namespace CppUnit {
class CppUnitException;
class Test;
/*
* A TestResult collects the results of executing a test case. It is an
* instance of the Collecting Parameter pattern.
*
* The test framework distinguishes between failures and errors.
* A failure is anticipated and checked for with assertions. Errors are
* unanticipated problems signified by exceptions that are not generated
* by the framework.
*
* TestResult supplies a template method 'setSynchronizationObject ()'
* so that subclasses can provide mutual exclusion in the face of multiple
* threads. This can be useful when tests execute in one thread and
* they fill a subclass of TestResult which effects change in another
* thread. To have mutual exclusion, override setSynchronizationObject ()
* and make sure that you create an instance of ExclusiveZone at the
* beginning of each method.
*
* see Test
*/
class CppUnit_API TestResult
{
REFERENCEOBJECT (TestResult)
public:
TestResult();
virtual ~TestResult();
virtual void addError(Test* test, CppUnitException* e);
virtual void addFailure(Test* test, CppUnitException* e);
virtual void startTest(Test* test);
virtual void endTest(Test* test);
virtual int runTests();
virtual int testErrors();
virtual int testFailures();
virtual bool wasSuccessful();
virtual bool shouldStop();
virtual void stop();
virtual std::vector<TestFailure*>& errors();
virtual std::vector<TestFailure*>& failures();
class SynchronizationObject
{
public:
SynchronizationObject()
{
}
virtual ~SynchronizationObject()
{
}
virtual void lock()
{
}
virtual void unlock()
{
}
};
class ExclusiveZone
{
SynchronizationObject* m_syncObject;
public:
ExclusiveZone(SynchronizationObject* syncObject): m_syncObject(syncObject)
{
m_syncObject->lock();
}
~ExclusiveZone()
{
m_syncObject->unlock();
}
};
protected:
virtual void setSynchronizationObject(SynchronizationObject* syncObject);
std::vector<TestFailure*> _errors;
std::vector<TestFailure*> _failures;
int _runTests;
bool _stop;
SynchronizationObject* _syncObject;
};
// Construct a TestResult
inline TestResult::TestResult(): _syncObject(new SynchronizationObject())
{
_runTests = 0;
_stop = false;
}
// Adds an error to the list of errors. The passed in exception
// caused the error
inline void TestResult::addError(Test* test, CppUnitException* e)
{
ExclusiveZone zone(_syncObject);
_errors.push_back(new TestFailure(test, e));
}
// Adds a failure to the list of failures. The passed in exception
// caused the failure.
inline void TestResult::addFailure(Test* test, CppUnitException* e)
{
ExclusiveZone zone(_syncObject);
_failures.push_back(new TestFailure(test, e));
}
// Informs the result that a test will be started.
inline void TestResult::startTest(Test* test)
{
ExclusiveZone zone(_syncObject);
_runTests++;
}
// Informs the result that a test was completed.
inline void TestResult::endTest(Test* test)
{
ExclusiveZone zone(_syncObject);
}
// Gets the number of run tests.
inline int TestResult::runTests()
{
ExclusiveZone zone(_syncObject);
return _runTests;
}
// Gets the number of detected errors.
inline int TestResult::testErrors()
{
ExclusiveZone zone(_syncObject);
return (int) _errors.size();
}
// Gets the number of detected failures.
inline int TestResult::testFailures()
{
ExclusiveZone zone(_syncObject);
return (int) _failures.size();
}
// Returns whether the entire test was successful or not.
inline bool TestResult::wasSuccessful()
{
ExclusiveZone zone(_syncObject);
return _failures.size() == 0 && _errors.size () == 0;
}
// Returns a std::vector of the errors.
inline std::vector<TestFailure*>& TestResult::errors()
{
ExclusiveZone zone(_syncObject);
return _errors;
}
// Returns a std::vector of the failures.
inline std::vector<TestFailure*>& TestResult::failures()
{
ExclusiveZone zone(_syncObject);
return _failures;
}
// Returns whether testing should be stopped
inline bool TestResult::shouldStop()
{
ExclusiveZone zone(_syncObject);
return _stop;
}
// Stop testing
inline void TestResult::stop()
{
ExclusiveZone zone(_syncObject);
_stop = true;
}
// Accept a new synchronization object for protection of this instance
// TestResult assumes ownership of the object
inline void TestResult::setSynchronizationObject(SynchronizationObject* syncObject)
{
delete _syncObject;
_syncObject = syncObject;
}
} // namespace CppUnit
#endif // CppUnit_TestResult_INCLUDED