1 #ifndef __cxxtest__TestTracker_cpp__
2 #define __cxxtest__TestTracker_cpp__
4 #include <cxxtest/TestTracker.h>
6 namespace CxxTest
7 {
8 bool TestTracker::_created = false;
10 TestTracker::TestTracker()
11 {
12 if ( !_created ) {
13 initialize();
14 _created = true;
15 }
16 }
18 TestTracker::~TestTracker()
19 {
20 }
22 TestTracker & TestTracker::tracker()
23 {
24 static TestTracker theTracker;
25 return theTracker;
26 }
28 void TestTracker::initialize()
29 {
30 _warnings = 0;
31 _failedTests = 0;
32 _testFailedAsserts = 0;
33 _suiteFailedTests = 0;
34 _failedSuites = 0;
35 setListener( 0 );
36 _world = 0;
37 _suite = 0;
38 _test = 0;
39 }
41 const TestDescription *TestTracker::fixTest( const TestDescription *d ) const
42 {
43 return d ? d : &dummyTest();
44 }
46 const SuiteDescription *TestTracker::fixSuite( const SuiteDescription *d ) const
47 {
48 return d ? d : &dummySuite();
49 }
51 const WorldDescription *TestTracker::fixWorld( const WorldDescription *d ) const
52 {
53 return d ? d : &dummyWorld();
54 }
56 const TestDescription &TestTracker::dummyTest() const
57 {
58 return dummySuite().testDescription(0);
59 }
61 const SuiteDescription &TestTracker::dummySuite() const
62 {
63 return dummyWorld().suiteDescription(0);
64 }
66 const WorldDescription &TestTracker::dummyWorld() const
67 {
68 return _dummyWorld;
69 }
71 void TestTracker::setListener( TestListener *l )
72 {
73 _l = l ? l : &_dummyListener;
74 }
76 void TestTracker::enterWorld( const WorldDescription &wd )
77 {
78 setWorld( &wd );
79 _warnings = _failedTests = _testFailedAsserts = _suiteFailedTests = _failedSuites = 0;
80 _l->enterWorld( wd );
81 }
83 void TestTracker::enterSuite( const SuiteDescription &sd )
84 {
85 setSuite( &sd );
86 _testFailedAsserts = _suiteFailedTests = 0;
87 _l->enterSuite(sd);
88 }
90 void TestTracker::enterTest( const TestDescription &td )
91 {
92 setTest( &td );
93 _testFailedAsserts = false;
94 _l->enterTest(td);
95 }
97 void TestTracker::leaveTest( const TestDescription &td )
98 {
99 _l->leaveTest( td );
100 setTest( 0 );
101 }
103 void TestTracker::leaveSuite( const SuiteDescription &sd )
104 {
105 _l->leaveSuite( sd );
106 setSuite( 0 );
107 }
109 void TestTracker::leaveWorld( const WorldDescription &wd )
110 {
111 _l->leaveWorld( wd );
112 setWorld( 0 );
113 }
115 void TestTracker::trace( const char *file, unsigned line, const char *expression )
116 {
117 _l->trace( file, line, expression );
118 }
120 void TestTracker::warning( const char *file, unsigned line, const char *expression )
121 {
122 countWarning();
123 _l->warning( file, line, expression );
124 }
126 void TestTracker::failedTest( const char *file, unsigned line, const char *expression )
127 {
128 countFailure();
129 _l->failedTest( file, line, expression );
130 }
132 void TestTracker::failedAssert( const char *file, unsigned line, const char *expression )
133 {
134 countFailure();
135 _l->failedAssert( file, line, expression );
136 }
138 void TestTracker::failedAssertEquals( const char *file, unsigned line,
139 const char *xStr, const char *yStr,
140 const char *x, const char *y )
141 {
142 countFailure();
143 _l->failedAssertEquals( file, line, xStr, yStr, x, y );
144 }
146 void TestTracker::failedAssertSameData( const char *file, unsigned line,
147 const char *xStr, const char *yStr,
148 const char *sizeStr, const void *x,
149 const void *y, unsigned size )
150 {
151 countFailure();
152 _l->failedAssertSameData( file, line, xStr, yStr, sizeStr, x, y, size );
153 }
155 void TestTracker::failedAssertDelta( const char *file, unsigned line,
156 const char *xStr, const char *yStr, const char *dStr,
157 const char *x, const char *y, const char *d )
158 {
159 countFailure();
160 _l->failedAssertDelta( file, line, xStr, yStr, dStr, x, y, d );
161 }
163 void TestTracker::failedAssertDiffers( const char *file, unsigned line,
164 const char *xStr, const char *yStr,
165 const char *value )
166 {
167 countFailure();
168 _l->failedAssertDiffers( file, line, xStr, yStr, value );
169 }
171 void TestTracker::failedAssertLessThan( const char *file, unsigned line,
172 const char *xStr, const char *yStr,
173 const char *x, const char *y )
174 {
175 countFailure();
176 _l->failedAssertLessThan( file, line, xStr, yStr, x, y );
177 }
179 void TestTracker::failedAssertLessThanEquals( const char *file, unsigned line,
180 const char *xStr, const char *yStr,
181 const char *x, const char *y )
182 {
183 countFailure();
184 _l->failedAssertLessThanEquals( file, line, xStr, yStr, x, y );
185 }
187 void TestTracker::failedAssertPredicate( const char *file, unsigned line,
188 const char *predicate, const char *xStr, const char *x )
189 {
190 countFailure();
191 _l->failedAssertPredicate( file, line, predicate, xStr, x );
192 }
194 void TestTracker::failedAssertRelation( const char *file, unsigned line,
195 const char *relation, const char *xStr, const char *yStr,
196 const char *x, const char *y )
197 {
198 countFailure();
199 _l->failedAssertRelation( file, line, relation, xStr, yStr, x, y );
200 }
202 void TestTracker::failedAssertThrows( const char *file, unsigned line,
203 const char *expression, const char *type,
204 bool otherThrown )
205 {
206 countFailure();
207 _l->failedAssertThrows( file, line, expression, type, otherThrown );
208 }
210 void TestTracker::failedAssertThrowsNot( const char *file, unsigned line, const char *expression )
211 {
212 countFailure();
213 _l->failedAssertThrowsNot( file, line, expression );
214 }
216 void TestTracker::setWorld( const WorldDescription *w )
217 {
218 _world = fixWorld( w );
219 setSuite( 0 );
220 }
222 void TestTracker::setSuite( const SuiteDescription *s )
223 {
224 _suite = fixSuite( s );
225 setTest( 0 );
226 }
228 void TestTracker::setTest( const TestDescription *t )
229 {
230 _test = fixTest( t );
231 }
233 void TestTracker::countWarning()
234 {
235 ++ _warnings;
236 }
238 void TestTracker::countFailure()
239 {
240 if ( ++ _testFailedAsserts == 1 ) {
241 ++ _failedTests;
242 if ( ++ _suiteFailedTests == 1 )
243 ++ _failedSuites;
244 }
245 }
246 };
248 #endif // __cxxtest__TestTracker_cpp__