1 #ifndef __cxxtest__RealDescriptions_h__ 2 #define __cxxtest__RealDescriptions_h__ 3 4 // 5 // The "real" description classes 6 // 7 8 #include <cxxtest/Descriptions.h> 9 #include <cxxtest/TestSuite.h> 10 #include <cxxtest/GlobalFixture.h> 11 12 namespace CxxTest 13 { 14 class RealTestDescription : public TestDescription 15 { 16 public: 17 RealTestDescription(); 18 RealTestDescription( List &argList, SuiteDescription &argSuite, unsigned argLine, const char *argTestName ); 19 void initialize( List &argList, SuiteDescription &argSuite, unsigned argLine, const char *argTestName ); 20 21 const char *file() const; 22 unsigned line() const; 23 const char *testName() const; 24 const char *suiteName() const; 25 26 TestDescription *next(); 27 const TestDescription *next() const; 28 29 TestSuite *suite() const; 30 31 bool setUp(); 32 void run(); 33 bool tearDown(); 34 35 private: 36 RealTestDescription( const RealTestDescription & ); 37 RealTestDescription &operator=( const RealTestDescription & ); 38 39 virtual void runTest() = 0; 40 41 SuiteDescription *_suite; 42 unsigned _line; 43 const char *_testName; 44 }; 45 46 class RealSuiteDescription : public SuiteDescription 47 { 48 public: 49 RealSuiteDescription(); 50 RealSuiteDescription( const char *argFile, unsigned argLine, const char *argSuiteName, List &argTests ); 51 52 void initialize( const char *argFile, unsigned argLine, const char *argSuiteName, List &argTests ); 53 54 const char *file() const; 55 unsigned line() const; 56 const char *suiteName() const; 57 58 TestDescription *firstTest(); 59 const TestDescription *firstTest() const; 60 SuiteDescription *next(); 61 const SuiteDescription *next() const; 62 63 unsigned numTests() const; 64 const TestDescription &testDescription( unsigned i ) const; 65 66 void activateAllTests(); 67 bool leaveOnly( const char *testName ); 68 69 private: 70 RealSuiteDescription( const RealSuiteDescription & ); 71 RealSuiteDescription &operator=( const RealSuiteDescription & ); 72 73 const char *_file; 74 unsigned _line; 75 const char *_suiteName; 76 List *_tests; 77 78 static List _suites; 79 friend class RealWorldDescription; 80 }; 81 82 class StaticSuiteDescription : public RealSuiteDescription 83 { 84 public: 85 StaticSuiteDescription(); 86 StaticSuiteDescription( const char *argFile, unsigned argLine, 87 const char *argSuiteName, TestSuite &argSuite, 88 List &argTests ); 89 90 void initialize( const char *argFile, unsigned argLine, 91 const char *argSuiteName, TestSuite &argSuite, 92 List &argTests ); 93 TestSuite *suite() const; 94 95 bool setUp(); 96 bool tearDown(); 97 98 private: 99 StaticSuiteDescription( const StaticSuiteDescription & ); 100 StaticSuiteDescription &operator=( const StaticSuiteDescription & ); 101 102 void doInitialize( TestSuite &argSuite ); 103 104 TestSuite *_suite; 105 }; 106 107 class CommonDynamicSuiteDescription : public RealSuiteDescription 108 { 109 public: 110 CommonDynamicSuiteDescription(); 111 CommonDynamicSuiteDescription( const char *argFile, unsigned argLine, 112 const char *argSuiteName, List &argTests, 113 unsigned argCreateLine, unsigned argDestroyLine ); 114 115 void initialize( const char *argFile, unsigned argLine, 116 const char *argSuiteName, List &argTests, 117 unsigned argCreateLine, unsigned argDestroyLine ); 118 119 protected: 120 unsigned _createLine, _destroyLine; 121 122 private: 123 void doInitialize( unsigned argCreateLine, unsigned argDestroyLine ); 124 }; 125 126 template<class S> 127 class DynamicSuiteDescription : public CommonDynamicSuiteDescription 128 { 129 public: DynamicSuiteDescription()130 DynamicSuiteDescription() {} DynamicSuiteDescription(const char * argFile,unsigned argLine,const char * argSuiteName,List & argTests,S * & argSuite,unsigned argCreateLine,unsigned argDestroyLine)131 DynamicSuiteDescription( const char *argFile, unsigned argLine, 132 const char *argSuiteName, List &argTests, 133 S *&argSuite, unsigned argCreateLine, 134 unsigned argDestroyLine ) : 135 CommonDynamicSuiteDescription( argFile, argLine, argSuiteName, argTests, argCreateLine, argDestroyLine ) 136 { 137 _suite = &argSuite; 138 } 139 initialize(const char * argFile,unsigned argLine,const char * argSuiteName,List & argTests,S * & argSuite,unsigned argCreateLine,unsigned argDestroyLine)140 void initialize( const char *argFile, unsigned argLine, 141 const char *argSuiteName, List &argTests, 142 S *&argSuite, unsigned argCreateLine, 143 unsigned argDestroyLine ) 144 { 145 CommonDynamicSuiteDescription::initialize( argFile, argLine, 146 argSuiteName, argTests, 147 argCreateLine, argDestroyLine ); 148 _suite = &argSuite; 149 } 150 suite()151 TestSuite *suite() const { return realSuite(); } 152 153 bool setUp(); 154 bool tearDown(); 155 156 private: realSuite()157 S *realSuite() const { return *_suite; } setSuite(S * s)158 void setSuite( S *s ) { *_suite = s; } 159 createSuite()160 void createSuite() 161 { 162 setSuite( S::createSuite() ); 163 } 164 destroySuite()165 void destroySuite() 166 { 167 S *s = realSuite(); 168 setSuite( 0 ); 169 S::destroySuite( s ); 170 } 171 172 S **_suite; 173 }; 174 175 template<class S> setUp()176 bool DynamicSuiteDescription<S>::setUp() 177 { 178 _TS_TRY { 179 _TSM_ASSERT_THROWS_NOTHING( file(), _createLine, "Exception thrown from createSuite()", createSuite() ); 180 _TSM_ASSERT( file(), _createLine, "createSuite() failed", suite() != 0 ); 181 } 182 _TS_CATCH_ABORT( { return false; } ); 183 184 return (suite() != 0); 185 } 186 187 template<class S> tearDown()188 bool DynamicSuiteDescription<S>::tearDown() 189 { 190 if ( !_suite ) 191 return true; 192 193 _TS_TRY { 194 _TSM_ASSERT_THROWS_NOTHING( file(), _destroyLine, "destroySuite() failed", destroySuite() ); 195 } 196 _TS_CATCH_ABORT( { return false; } ); 197 198 return true; 199 } 200 201 class RealWorldDescription : public WorldDescription 202 { 203 public: 204 static List &suites(); 205 unsigned numSuites( void ) const; 206 unsigned numTotalTests( void ) const; 207 SuiteDescription *firstSuite(); 208 const SuiteDescription *firstSuite() const; 209 const SuiteDescription &suiteDescription( unsigned i ) const; 210 void activateAllTests(); 211 bool leaveOnly( const char *suiteName, const char *testName = 0 ); 212 213 bool setUp(); 214 bool tearDown(); 215 static void reportError( const char *message ); 216 }; 217 218 void activateAllTests(); 219 bool leaveOnly( const char *suiteName, const char *testName = 0 ); 220 } 221 222 #endif // __cxxtest__RealDescriptions_h__ 223 224