1 #ifndef __cxxtest__RealDescriptions_cpp__
2 #define __cxxtest__RealDescriptions_cpp__
4 //
5 // NOTE: If an error occur during world construction/deletion, CxxTest cannot
6 // know where the error originated.
7 //
9 #include <cxxtest/RealDescriptions.h>
11 namespace CxxTest
12 {
13 RealTestDescription::RealTestDescription()
14 {
15 }
17 RealTestDescription::RealTestDescription( List &argList,
18 SuiteDescription &argSuite,
19 unsigned argLine,
20 const char *argTestName )
21 {
22 initialize( argList, argSuite, argLine, argTestName );
23 }
25 void RealTestDescription::initialize( List &argList,
26 SuiteDescription &argSuite,
27 unsigned argLine,
28 const char *argTestName )
29 {
30 _suite = &argSuite;
31 _line = argLine;
32 _testName = argTestName;
33 attach( argList );
34 }
36 bool RealTestDescription::setUp()
37 {
38 if ( !suite() )
39 return false;
41 for ( GlobalFixture *gf = GlobalFixture::firstGlobalFixture(); gf != 0; gf = gf->nextGlobalFixture() ) {
42 bool ok;
43 _TS_TRY { ok = gf->setUp(); }
44 _TS_LAST_CATCH( { ok = false; } );
46 if ( !ok ) {
47 doFailTest( file(), line(), "Error in GlobalFixture::setUp()" );
48 return false;
49 }
50 }
52 _TS_TRY {
53 _TSM_ASSERT_THROWS_NOTHING( file(), line(), "Exception thrown from setUp()", suite()->setUp() );
54 }
55 _TS_CATCH_ABORT( { return false; } );
57 return true;
58 }
60 bool RealTestDescription::tearDown()
61 {
62 if ( !suite() )
63 return false;
65 _TS_TRY {
66 _TSM_ASSERT_THROWS_NOTHING( file(), line(), "Exception thrown from tearDown()", suite()->tearDown() );
67 }
68 _TS_CATCH_ABORT( { return false; } );
70 for ( GlobalFixture *gf = GlobalFixture::lastGlobalFixture(); gf != 0; gf = gf->prevGlobalFixture() ) {
71 bool ok;
72 _TS_TRY { ok = gf->tearDown(); }
73 _TS_LAST_CATCH( { ok = false; } );
75 if ( !ok ) {
76 doFailTest( file(), line(), "Error in GlobalFixture::tearDown()" );
77 return false;
78 }
79 }
81 return true;
82 }
84 const char *RealTestDescription::file() const { return _suite->file(); }
85 unsigned RealTestDescription::line() const { return _line; }
86 const char *RealTestDescription::testName() const { return _testName; }
87 const char *RealTestDescription::suiteName() const { return _suite->suiteName(); }
89 TestDescription *RealTestDescription::next() { return (RealTestDescription *)Link::next(); }
90 const TestDescription *RealTestDescription::next() const { return (const RealTestDescription *)Link::next(); }
92 TestSuite *RealTestDescription::suite() const { return _suite->suite(); }
94 void RealTestDescription::run()
95 {
96 _TS_TRY { runTest(); }
97 _TS_CATCH_ABORT( {} )
98 ___TSM_CATCH( file(), line(), "Exception thrown from test" );
99 }
101 RealSuiteDescription::RealSuiteDescription() {}
102 RealSuiteDescription::RealSuiteDescription( const char *argFile,
103 unsigned argLine,
104 const char *argSuiteName,
105 List &argTests )
106 {
107 initialize( argFile, argLine, argSuiteName, argTests );
108 }
110 void RealSuiteDescription::initialize( const char *argFile,
111 unsigned argLine,
112 const char *argSuiteName,
113 List &argTests )
114 {
115 _file = argFile;
116 _line = argLine;
117 _suiteName = argSuiteName;
118 _tests = &argTests;
120 attach( _suites );
121 }
123 const char *RealSuiteDescription::file() const { return _file; }
124 unsigned RealSuiteDescription::line() const { return _line; }
125 const char *RealSuiteDescription::suiteName() const { return _suiteName; }
127 TestDescription *RealSuiteDescription::firstTest() { return (RealTestDescription *)_tests->head(); }
128 const TestDescription *RealSuiteDescription::firstTest() const { return (const RealTestDescription *)_tests->head(); }
129 SuiteDescription *RealSuiteDescription::next() { return (RealSuiteDescription *)Link::next(); }
130 const SuiteDescription *RealSuiteDescription::next() const { return (const RealSuiteDescription *)Link::next(); }
132 unsigned RealSuiteDescription::numTests() const { return _tests->size(); }
134 const TestDescription &RealSuiteDescription::testDescription( unsigned i ) const
135 {
136 return *(RealTestDescription *)_tests->nth( i );
137 }
139 void RealSuiteDescription::activateAllTests()
140 {
141 _tests->activateAll();
142 }
144 bool RealSuiteDescription::leaveOnly( const char *testName )
145 {
146 for ( TestDescription *td = firstTest(); td != 0; td = td->next() ) {
147 if ( stringsEqual( td->testName(), testName ) ) {
148 _tests->leaveOnly( *td );
149 return true;
150 }
151 }
152 return false;
153 }
155 StaticSuiteDescription::StaticSuiteDescription() {}
156 StaticSuiteDescription::StaticSuiteDescription( const char *argFile, unsigned argLine,
157 const char *argSuiteName, TestSuite &argSuite,
158 List &argTests ) :
159 RealSuiteDescription( argFile, argLine, argSuiteName, argTests )
160 {
161 doInitialize( argSuite );
162 }
164 void StaticSuiteDescription::initialize( const char *argFile, unsigned argLine,
165 const char *argSuiteName, TestSuite &argSuite,
166 List &argTests )
167 {
168 RealSuiteDescription::initialize( argFile, argLine, argSuiteName, argTests );
169 doInitialize( argSuite );
170 }
172 void StaticSuiteDescription::doInitialize( TestSuite &argSuite )
173 {
174 _suite = &argSuite;
175 }
177 TestSuite *StaticSuiteDescription::suite() const
178 {
179 return _suite;
180 }
182 bool StaticSuiteDescription::setUp() { return true; }
183 bool StaticSuiteDescription::tearDown() { return true; }
185 CommonDynamicSuiteDescription::CommonDynamicSuiteDescription() {}
186 CommonDynamicSuiteDescription::CommonDynamicSuiteDescription( const char *argFile, unsigned argLine,
187 const char *argSuiteName, List &argTests,
188 unsigned argCreateLine, unsigned argDestroyLine ) :
189 RealSuiteDescription( argFile, argLine, argSuiteName, argTests )
190 {
191 doInitialize( argCreateLine, argDestroyLine );
192 }
194 void CommonDynamicSuiteDescription::initialize( const char *argFile, unsigned argLine,
195 const char *argSuiteName, List &argTests,
196 unsigned argCreateLine, unsigned argDestroyLine )
197 {
198 RealSuiteDescription::initialize( argFile, argLine, argSuiteName, argTests );
199 doInitialize( argCreateLine, argDestroyLine );
200 }
202 void CommonDynamicSuiteDescription::doInitialize( unsigned argCreateLine, unsigned argDestroyLine )
203 {
204 _createLine = argCreateLine;
205 _destroyLine = argDestroyLine;
206 }
208 List &RealWorldDescription::suites()
209 {
210 return RealSuiteDescription::_suites;
211 }
213 unsigned RealWorldDescription::numSuites( void ) const
214 {
215 return suites().size();
216 }
218 unsigned RealWorldDescription::numTotalTests( void ) const
219 {
220 unsigned count = 0;
221 for ( const SuiteDescription *sd = firstSuite(); sd != 0; sd = sd->next() )
222 count += sd->numTests();
223 return count;
224 }
226 SuiteDescription *RealWorldDescription::firstSuite()
227 {
228 return (RealSuiteDescription *)suites().head();
229 }
231 const SuiteDescription *RealWorldDescription::firstSuite() const
232 {
233 return (const RealSuiteDescription *)suites().head();
234 }
236 const SuiteDescription &RealWorldDescription::suiteDescription( unsigned i ) const
237 {
238 return *(const RealSuiteDescription *)suites().nth( i );
239 }
241 void RealWorldDescription::activateAllTests()
242 {
243 suites().activateAll();
244 for ( SuiteDescription *sd = firstSuite(); sd != 0; sd = sd->next() )
245 sd->activateAllTests();
246 }
248 bool RealWorldDescription::leaveOnly( const char *suiteName, const char *testName )
249 {
250 for ( SuiteDescription *sd = firstSuite(); sd != 0; sd = sd->next() ) {
251 if ( stringsEqual( sd->suiteName(), suiteName ) ) {
252 if ( testName )
253 if ( !sd->leaveOnly( testName ) )
254 return false;
255 suites().leaveOnly( *sd );
256 return true;
257 }
258 }
259 return false;
260 }
262 bool RealWorldDescription::setUp()
263 {
264 for ( GlobalFixture *gf = GlobalFixture::firstGlobalFixture(); gf != 0; gf = gf->nextGlobalFixture() ) {
265 bool ok;
266 _TS_TRY { ok = gf->setUpWorld(); }
267 _TS_LAST_CATCH( { ok = false; } );
269 if ( !ok ) {
270 reportError( "Error setting up world" );
271 return false;
272 }
273 }
275 return true;
276 }
278 bool RealWorldDescription::tearDown()
279 {
280 for ( GlobalFixture *gf = GlobalFixture::lastGlobalFixture(); gf != 0; gf = gf->prevGlobalFixture() ) {
281 bool ok;
282 _TS_TRY { ok = gf->tearDownWorld(); }
283 _TS_LAST_CATCH( { ok = false; } );
285 if ( !ok ) {
286 reportError( "Error tearing down world" );
287 return false;
288 }
289 }
291 return true;
292 }
294 void RealWorldDescription::reportError( const char *message )
295 {
296 doWarn( __FILE__, 5, message );
297 }
299 void activateAllTests()
300 {
301 RealWorldDescription().activateAllTests();
302 }
304 bool leaveOnly( const char *suiteName, const char *testName )
305 {
306 return RealWorldDescription().leaveOnly( suiteName, testName );
307 }
308 }
310 #endif // __cxxtest__RealDescriptions_cpp__