1 #ifndef  __cxxtest_TestRunner_h__
2 #define  __cxxtest_TestRunner_h__
3 
4 //
5 // TestRunner is the class that runs all the tests.
6 // To use it, create an object that implements the TestListener
7 // interface and call TestRunner::runAllTests( myListener );
8 //
9 
10 #include <cxxtest/TestListener.h>
11 #include <cxxtest/RealDescriptions.h>
12 #include <cxxtest/TestSuite.h>
13 #include <cxxtest/TestTracker.h>
14 
15 namespace CxxTest {
16   class TestRunner {
17     public:
runAllTests(TestListener & listener)18     static void runAllTests( TestListener& listener ) {
19       tracker().setListener( &listener );
20       _TS_TRY { TestRunner().runWorld(); }
21       _TS_LAST_CATCH( {
22         tracker().failedTest(
23               __FILE__,   __LINE__, "Exception thrown from world" );
24       } );
25       tracker().setListener( 0 );
26     }
27 
runAllTests(TestListener * listener)28     static void runAllTests( TestListener* listener ) {
29       if ( listener ) {
30         listener->warning(   __FILE__,
31                              __LINE__,
32                            "Deprecated; Use runAllTests( TestListener & )" );
33         runAllTests( *listener );
34       }
35     }
36 
37     private:
runWorld()38     void runWorld() {
39       RealWorldDescription wd;
40       WorldGuard sg;
41 
42       tracker().enterWorld( wd );
43 
44       if ( wd.setUp() ) {
45         for ( SuiteDescription* sd = wd.firstSuite(); sd; sd = sd->next() )
46           if ( sd->active() ) runSuite( *sd );
47 
48         wd.tearDown();
49       }
50 
51       tracker().leaveWorld( wd );
52     }
53 
runSuite(SuiteDescription & sd)54     void runSuite( SuiteDescription& sd ) {
55       StateGuard sg;
56 
57       tracker().enterSuite( sd );
58 
59       if ( sd.setUp() ) {
60         for ( TestDescription* td = sd.firstTest(); td; td = td->next() )
61           if ( td->active() ) runTest( *td );
62 
63         sd.tearDown();
64       }
65 
66       tracker().leaveSuite( sd );
67     }
68 
runTest(TestDescription & td)69     void runTest( TestDescription& td ) {
70       StateGuard sg;
71 
72       tracker().enterTest( td );
73 
74       if ( td.setUp() ) {
75         td.run();
76         td.tearDown();
77       }
78 
79       tracker().leaveTest( td );
80     }
81 
82     class StateGuard {
83 #ifdef _CXXTEST_HAVE_EH
84       bool _abortTestOnFail;
85 #endif  // _CXXTEST_HAVE_EH
86       unsigned _maxDumpSize;
87 
88       public:
StateGuard()89       StateGuard() {
90 #ifdef _CXXTEST_HAVE_EH
91         _abortTestOnFail = abortTestOnFail();
92 #endif  // _CXXTEST_HAVE_EH
93         _maxDumpSize = maxDumpSize();
94       }
95 
~StateGuard()96       ~StateGuard() {
97 #ifdef _CXXTEST_HAVE_EH
98         setAbortTestOnFail( _abortTestOnFail );
99 #endif  // _CXXTEST_HAVE_EH
100         setMaxDumpSize( _maxDumpSize );
101       }
102     };
103 
104     class WorldGuard : public StateGuard {
105       public:
WorldGuard()106       WorldGuard()
107           : StateGuard() {
108 #ifdef _CXXTEST_HAVE_EH
109         setAbortTestOnFail( CXXTEST_DEFAULT_ABORT );
110 #endif  // _CXXTEST_HAVE_EH
111         setMaxDumpSize( CXXTEST_MAX_DUMP_SIZE );
112       }
113     };
114   };
115 
116   //
117   // For --no-static-init
118   //
119   void initialize();
120 }
121 
122 #endif  //  __cxxtest_TestRunner_h__
123