1 /*
2 -------------------------------------------------------------------------
3 CxxTest: A lightweight C++ unit testing library.
4 Copyright (c) 2008 Sandia Corporation.
5 This software is distributed under the LGPL License v3
6 For more information, see the COPYING file in the top CxxTest directory.
7 Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 the U.S. Government retains certain rights in this software.
9 -------------------------------------------------------------------------
10 */
11
12 #ifndef __cxxtest__TestTracker_cpp__
13 #define __cxxtest__TestTracker_cpp__
14
15 #include <cxxtest/TestTracker.h>
16
17 namespace CxxTest
18 {
19 bool TestTracker::_created = false;
20 bool TestTracker::print_tracing = false;
21
TestTracker()22 TestTracker::TestTracker()
23 {
24 if (!_created)
25 {
26 initialize();
27 setListener(0);
28 _created = true;
29 }
30 }
31
~TestTracker()32 TestTracker::~TestTracker()
33 {
34 }
35
tracker()36 TestTracker & TestTracker::tracker()
37 {
38 static TestTracker theTracker;
39 return theTracker;
40 }
41
initialize()42 void TestTracker::initialize()
43 {
44 _warnings = 0;
45 _skippedTests = 0;
46 _testSkipped = false;
47 _failedTests = 0;
48 _testFailedAsserts = 0;
49 _suiteFailedTests = 0;
50 _failedSuites = 0;
51 _world = 0;
52 _suite = 0;
53 _test = 0;
54 }
55
fixTest(const TestDescription * d) const56 const TestDescription *TestTracker::fixTest(const TestDescription *d) const
57 {
58 return d ? d : &dummyTest();
59 }
60
fixSuite(const SuiteDescription * d) const61 const SuiteDescription *TestTracker::fixSuite(const SuiteDescription *d) const
62 {
63 return d ? d : &dummySuite();
64 }
65
fixWorld(const WorldDescription * d) const66 const WorldDescription *TestTracker::fixWorld(const WorldDescription *d) const
67 {
68 return d ? d : &dummyWorld();
69 }
70
dummyTest() const71 const TestDescription &TestTracker::dummyTest() const
72 {
73 return dummySuite().testDescription(0);
74 }
75
dummySuite() const76 const SuiteDescription &TestTracker::dummySuite() const
77 {
78 return dummyWorld().suiteDescription(0);
79 }
80
dummyWorld() const81 const WorldDescription &TestTracker::dummyWorld() const
82 {
83 return _dummyWorld;
84 }
85
setListener(TestListener * l)86 void TestTracker::setListener(TestListener *l)
87 {
88 _l = l ? l : &_dummyListener;
89 }
90
enterWorld(const WorldDescription & wd)91 void TestTracker::enterWorld(const WorldDescription &wd)
92 {
93 setWorld(&wd);
94 _warnings = _skippedTests = _failedTests = _testFailedAsserts = _suiteFailedTests = _failedSuites = 0;
95 _testSkipped = false;
96 _l->enterWorld(wd);
97 }
98
enterSuite(const SuiteDescription & sd)99 void TestTracker::enterSuite(const SuiteDescription &sd)
100 {
101 setSuite(&sd);
102 _testFailedAsserts = _suiteFailedTests = 0;
103 _testSkipped = false;
104 _l->enterSuite(sd);
105 }
106
enterTest(const TestDescription & td)107 void TestTracker::enterTest(const TestDescription &td)
108 {
109 setTest(&td);
110 _testFailedAsserts = false;
111 _testSkipped = false;
112 _l->enterTest(td);
113 }
114
leaveTest(const TestDescription & td)115 void TestTracker::leaveTest(const TestDescription &td)
116 {
117 _l->leaveTest(td);
118 setTest(0);
119 }
120
leaveSuite(const SuiteDescription & sd)121 void TestTracker::leaveSuite(const SuiteDescription &sd)
122 {
123 _l->leaveSuite(sd);
124 setSuite(0);
125 }
126
leaveWorld(const WorldDescription & wd)127 void TestTracker::leaveWorld(const WorldDescription &wd)
128 {
129 _l->leaveWorld(wd);
130 setWorld(0);
131 }
132
trace(const char * file,int line,const char * expression)133 void TestTracker::trace(const char *file, int line, const char *expression)
134 {
135 _l->trace(file, line, expression);
136 }
137
warning(const char * file,int line,const char * expression)138 void TestTracker::warning(const char *file, int line, const char *expression)
139 {
140 countWarning();
141 _l->warning(file, line, expression);
142 }
143
skippedTest(const char * file,int line,const char * expression)144 void TestTracker::skippedTest(const char *file, int line, const char *expression)
145 {
146 countSkipped();
147 _testSkipped = true;
148 _l->skippedTest(file, line, expression);
149 }
150
failedTest(const char * file,int line,const char * expression)151 void TestTracker::failedTest(const char *file, int line, const char *expression)
152 {
153 countFailure();
154 _l->failedTest(file, line, expression);
155 }
156
failedAssert(const char * file,int line,const char * expression)157 void TestTracker::failedAssert(const char *file, int line, const char *expression)
158 {
159 countFailure();
160 _l->failedAssert(file, line, expression);
161 }
162
failedAssertEquals(const char * file,int line,const char * xStr,const char * yStr,const char * x,const char * y)163 void TestTracker::failedAssertEquals(const char *file, int line,
164 const char *xStr, const char *yStr,
165 const char *x, const char *y)
166 {
167 countFailure();
168 _l->failedAssertEquals(file, line, xStr, yStr, x, y);
169 }
170
failedAssertSameData(const char * file,int line,const char * xStr,const char * yStr,const char * sizeStr,const void * x,const void * y,unsigned size)171 void TestTracker::failedAssertSameData(const char *file, int line,
172 const char *xStr, const char *yStr,
173 const char *sizeStr, const void *x,
174 const void *y, unsigned size)
175 {
176 countFailure();
177 _l->failedAssertSameData(file, line, xStr, yStr, sizeStr, x, y, size);
178 }
179
failedAssertDelta(const char * file,int line,const char * xStr,const char * yStr,const char * dStr,const char * x,const char * y,const char * d)180 void TestTracker::failedAssertDelta(const char *file, int line,
181 const char *xStr, const char *yStr, const char *dStr,
182 const char *x, const char *y, const char *d)
183 {
184 countFailure();
185 _l->failedAssertDelta(file, line, xStr, yStr, dStr, x, y, d);
186 }
187
failedAssertDiffers(const char * file,int line,const char * xStr,const char * yStr,const char * value)188 void TestTracker::failedAssertDiffers(const char *file, int line,
189 const char *xStr, const char *yStr,
190 const char *value)
191 {
192 countFailure();
193 _l->failedAssertDiffers(file, line, xStr, yStr, value);
194 }
195
failedAssertLessThan(const char * file,int line,const char * xStr,const char * yStr,const char * x,const char * y)196 void TestTracker::failedAssertLessThan(const char *file, int line,
197 const char *xStr, const char *yStr,
198 const char *x, const char *y)
199 {
200 countFailure();
201 _l->failedAssertLessThan(file, line, xStr, yStr, x, y);
202 }
203
failedAssertLessThanEquals(const char * file,int line,const char * xStr,const char * yStr,const char * x,const char * y)204 void TestTracker::failedAssertLessThanEquals(const char *file, int line,
205 const char *xStr, const char *yStr,
206 const char *x, const char *y)
207 {
208 countFailure();
209 _l->failedAssertLessThanEquals(file, line, xStr, yStr, x, y);
210 }
211
failedAssertPredicate(const char * file,int line,const char * predicate,const char * xStr,const char * x)212 void TestTracker::failedAssertPredicate(const char *file, int line,
213 const char *predicate, const char *xStr, const char *x)
214 {
215 countFailure();
216 _l->failedAssertPredicate(file, line, predicate, xStr, x);
217 }
218
failedAssertRelation(const char * file,int line,const char * relation,const char * xStr,const char * yStr,const char * x,const char * y)219 void TestTracker::failedAssertRelation(const char *file, int line,
220 const char *relation, const char *xStr, const char *yStr,
221 const char *x, const char *y)
222 {
223 countFailure();
224 _l->failedAssertRelation(file, line, relation, xStr, yStr, x, y);
225 }
226
failedAssertThrows(const char * file,int line,const char * expression,const char * type,bool otherThrown)227 void TestTracker::failedAssertThrows(const char *file, int line,
228 const char *expression, const char *type,
229 bool otherThrown)
230 {
231 countFailure();
232 _l->failedAssertThrows(file, line, expression, type, otherThrown);
233 }
234
failedAssertThrowsNot(const char * file,int line,const char * expression)235 void TestTracker::failedAssertThrowsNot(const char *file, int line, const char *expression)
236 {
237 countFailure();
238 _l->failedAssertThrowsNot(file, line, expression);
239 }
240
failedAssertSameFiles(const char * file,int line,const char * file1,const char * file2,const char * explanation)241 void TestTracker::failedAssertSameFiles(const char *file, int line, const char *file1, const char* file2, const char* explanation)
242 {
243 countFailure();
244 _l->failedAssertSameFiles(file, line, file1, file2, explanation);
245 }
246
setWorld(const WorldDescription * w)247 void TestTracker::setWorld(const WorldDescription *w)
248 {
249 _world = fixWorld(w);
250 setSuite(0);
251 }
252
setSuite(const SuiteDescription * s)253 void TestTracker::setSuite(const SuiteDescription *s)
254 {
255 _suite = fixSuite(s);
256 setTest(0);
257 }
258
setTest(const TestDescription * t)259 void TestTracker::setTest(const TestDescription *t)
260 {
261 _test = fixTest(t);
262 }
263
countWarning()264 void TestTracker::countWarning()
265 {
266 ++ _warnings;
267 }
268
countSkipped()269 void TestTracker::countSkipped()
270 {
271 ++ _skippedTests;
272 }
273
countFailure()274 void TestTracker::countFailure()
275 {
276 if (++ _testFailedAsserts == 1)
277 {
278 ++ _failedTests;
279 if (++ _suiteFailedTests == 1)
280 {
281 ++ _failedSuites;
282 }
283 }
284 }
285 }
286
287 #endif // __cxxtest__TestTracker_cpp__
288
289