1 #ifndef CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H 2 #define CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H 3 4 #include <cppunit/Portability.h> 5 #include <memory> 6 #include <cppunit/TestSuite.h> 7 #include <cppunit/TestCaller.h> 8 9 #if CPPUNIT_USE_TYPEINFO_NAME 10 # include <cppunit/extensions/TypeInfoHelper.h> 11 #endif 12 13 namespace CppUnit { 14 15 /*! \brief Helper to add tests to a TestSuite. 16 * \ingroup WritingTestFixture 17 * 18 * All tests added to the TestSuite are prefixed by TestSuite name. The resulting 19 * TestCase name has the following pattern: 20 * 21 * MyTestSuiteName.myTestName 22 */ 23 template<typename Fixture> 24 class TestSuiteBuilder 25 { 26 public: 27 typedef void (Fixture::*TestMethod)(); 28 29 #if CPPUNIT_USE_TYPEINFO_NAME 30 TestSuiteBuilder() : 31 m_suite( new TestSuite( 32 TypeInfoHelper::getClassName( typeid(Fixture) ) ) ) 33 { 34 } 35 #endif 36 37 TestSuiteBuilder( TestSuite *suite ) : m_suite( suite ) 38 { 39 } 40 41 TestSuiteBuilder(string name) : m_suite( new TestSuite(name) ) 42 { 43 } 44 45 TestSuite *suite() const 46 { 47 return m_suite.get(); 48 } 49 50 TestSuite *takeSuite() 51 { 52 return m_suite.release(); 53 } 54 55 void addTest( Test *test ) 56 { 57 m_suite->addTest( test ); 58 } 59 60 void addTestCaller( string methodName, 61 TestMethod testMethod ) 62 { 63 Test *test = 64 new TestCaller<Fixture>( makeTestName( methodName ), 65 testMethod ); 66 addTest( test ); 67 } 68 69 void addTestCaller( string methodName, 70 TestMethod testMethod, 71 Fixture *fixture ) 72 { 73 Test *test = 74 new TestCaller<Fixture>( makeTestName( methodName ), 75 testMethod, 76 fixture); 77 addTest( test ); 78 } 79 80 template<typename ExceptionType> 81 void addTestCallerForException( string methodName, 82 TestMethod testMethod, 83 Fixture *fixture, 84 ExceptionType *dummyPointer ) 85 { 86 Test *test = new TestCaller<Fixture,ExceptionType>( 87 makeTestName( methodName ), 88 testMethod, 89 fixture); 90 addTest( test ); 91 } 92 93 94 string makeTestName( const string &methodName ) 95 { 96 return m_suite->getName() + "." + methodName; 97 } 98 99 private: 100 auto_ptr<TestSuite> m_suite; 101 }; 102 103 } // namespace CppUnit 104 105 #endif // CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H 106