diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-05-25 08:29:07 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-05-25 08:29:07 +0000 |
| commit | b87cde884d03091d81e4d3fa2199155e152dda80 (patch) | |
| tree | 3a604be990b308ff85e2071116c4e1cc9fae74e1 /include/cppunit/extensions | |
| parent | 5ad4640702a80078748b38ebaeda37e69dce1189 (diff) | |
| download | cppunit-b87cde884d03091d81e4d3fa2199155e152dda80.tar.gz | |
Include/cppunit/extensions/TestSuiteBuilder.
include/cppunit/extensions/TestSuiteBuilder.h: updated to use TestNamer. Removed
template method addTestCallerForException() which should solve the compilation
issue with Sun 5.0/6.0 compiler.
* include/cppunit/extensions/HelperMacros.h: updated against TestSuiteBuilder
change. Added CPPUNIT_TEST_CUSTOM and CPPUNIT_TEST_CUSTOMS to add custom
tests to the fixture suite.
* include/cppunit/extensions/TestNamer.h:
* src/cppunit/TestNamer.cpp: added, TestNamer to name test case and fixture.
Diffstat (limited to 'include/cppunit/extensions')
| -rw-r--r-- | include/cppunit/extensions/HelperMacros.h | 153 | ||||
| -rw-r--r-- | include/cppunit/extensions/Makefile.am | 1 | ||||
| -rw-r--r-- | include/cppunit/extensions/TestNamer.h | 86 | ||||
| -rw-r--r-- | include/cppunit/extensions/TestSuiteBuilder.h | 179 |
4 files changed, 295 insertions, 124 deletions
diff --git a/include/cppunit/extensions/HelperMacros.h b/include/cppunit/extensions/HelperMacros.h index 65f3e5c..8b5b7c1 100644 --- a/include/cppunit/extensions/HelperMacros.h +++ b/include/cppunit/extensions/HelperMacros.h @@ -26,18 +26,6 @@ namespace CppUnit } // namespace CppUnit -// The macro __CPPUNIT_SUITE_CTOR_ARGS expand to an expression used to construct -// the TestSuiteBuilder with macro CPPUNIT_TEST_SUITE. -// -// The name of the suite is obtained using RTTI if CPPUNIT_USE_TYPEINFO_NAME -// is defined, otherwise it is extracted from the macro parameter -// -// This macro is for cppunit internal and should not be use otherwise. -#if CPPUNIT_USE_TYPEINFO_NAME -# define __CPPUNIT_SUITE_CTOR_ARGS( ATestFixtureType ) -#else -# define __CPPUNIT_SUITE_CTOR_ARGS( ATestFixtureType ) (std::string(#ATestFixtureType)) -#endif /*! \addtogroup WritingTestFixture Writing test fixture @@ -124,19 +112,25 @@ namespace CppUnit #define CPPUNIT_TEST_SUITE( ATestFixtureType ) \ private: \ typedef ATestFixtureType __ThisTestFixtureType; \ - class ThisTestFixtureFactory : public CppUnit::TestFixtureFactory \ + class __ThisTestFixtureFactory : public CppUnit::TestFixtureFactory \ { \ virtual CppUnit::TestFixture *makeFixture() \ { \ return new ATestFixtureType(); \ } \ }; \ + static const CppUnit::TestNamer &__getTestNamer() \ + { \ + static CPPUNIT_TESTNAMER_DECL( testNamer, ATestFixtureType ); \ + return testNamer; \ + } \ public: \ static void \ - registerTests( CppUnit::TestSuite *suite, \ - CppUnit::TestFixtureFactory *factory ) \ + __registerTests( CppUnit::TestSuite *suite, \ + CppUnit::TestFixtureFactory *factory, \ + const CppUnit::TestNamer &namer ) \ { \ - CppUnit::TestSuiteBuilder<__ThisTestFixtureType> builder( suite ); + CppUnit::TestSuiteBuilder<__ThisTestFixtureType> builder( suite, namer ) /*! \brief Begin test suite (includes parent suite) @@ -173,7 +167,7 @@ namespace CppUnit private: \ typedef ASuperClass __ThisSuperClassType; \ CPPUNIT_TEST_SUITE( ATestFixtureType ); \ - __ThisSuperClassType::registerTests( suite, factory ) + __ThisSuperClassType::__registerTests( suite, factory, namer ) /*! \brief Add a method to the suite. @@ -211,11 +205,13 @@ namespace CppUnit * \param ExceptionType Type of the exception that must be thrown by the test * method. */ -#define CPPUNIT_TEST_EXCEPTION( testMethod, ExceptionType ) \ - builder.addTestCallerForException( #testMethod, \ - &__ThisTestFixtureType::testMethod , \ - (__ThisTestFixtureType*)factory->makeFixture(), \ - (ExceptionType *)NULL ); +#define CPPUNIT_TEST_EXCEPTION( testMethod, ExceptionType ) \ + builder.addTest( new CppUnit::TestCaller<__ThisTestFixtureType, \ + ExceptionType>( \ + namer.getTestNameFor( #testMethod ), \ + &__ThisTestFixtureType::testMethod, \ + (__ThisTestFixtureType*)factory->makeFixture() ) ) + /*! \brief Adds a test case which is excepted to fail. * @@ -235,6 +231,93 @@ namespace CppUnit #define CPPUNIT_TEST_FAIL( testMethod ) \ CPPUNIT_TEST_EXCEPTION( testMethod, CppUnit::Exception ) +/*! \brief Adds a custom test case. + * + * Use this to add a custom test case. The specified method must have the following + * signature: + * \code + * static CppUnit::Test *makeCustomTest( CppUnit::TestFixtureFactory *factory, + * const CppUnit::TestNamer &namer ); + * \endcode + * + * Here is an example that add a custom test: + * + * \code + * #include <cppunit/extensions/HelperMacros.h> + * + * class MyTest : public CppUnit::TestFixture { + * CPPUNIT_TEST_SUITE( MyTest ); + * CPPUNIT_TEST_CUSTOM( makeTimeOutTest1 ); + * CPPUNIT_TEST_SUITE_END(); + * public: + * static CppUnit::Test *makeTimeOutTest1( CppUnit::TestFixtureFactory *factory, + * const CppUnit::TestNamer &namer ) + * { + * return new TimeOutTestCaller( namer.getTestNameFor( "test1" ), + * &MyTest::test1, + * (MyTest*)factory->makeFixture(), + * 5.0 ); + * } + * + * void test1() + * { + * // Do some test that may never end... + * } + * }; + * \endcode + */ +#define CPPUNIT_TEST_CUSTOM( testMakerMethod ) \ + builder.addTest( testMakerMethod( factory, namer ) ) + +/*! \brief Adds some custom test cases. + * + * Use this to add many custom test cases. The specified method must have the following + * signature: + * \code + * static CppUnit::Test *makeCustomTest( CppUnit::TestSuite *suite, \ + * CppUnit::TestFixtureFactory *factory, + * const CppUnit::TestNamer &namer ); + * \endcode + * + * Here is an example that add two custom tests: + * + * \code + * #include <cppunit/extensions/HelperMacros.h> + * + * class MyTest : public CppUnit::TestFixture { + * CPPUNIT_TEST_SUITE( MyTest ); + * CPPUNIT_TEST_CUSTOMs( addTimeOutTests ); + * CPPUNIT_TEST_SUITE_END(); + * public: + * static CppUnit::Test *makeTimeOutTest( CppUnit::TestSuite *suite, \ + * CppUnit::TestFixtureFactory *factory, + * const CppUnit::TestNamer &namer ) + * { + * suite->addTest( new TimeOutTestCaller( namer.getTestNameFor( "test1" ) ), + * &MyTest::test1, + * (MyTest*)factory->makeFixture(), + * 5.0 ); + * suite->addTest( new TimeOutTestCaller( namer.getTestNameFor( "test2" ) ), + * &MyTest::test2, + * (MyTest*)factory->makeFixture(), + * 5.0 ); + * } + * + * void test1() + * { + * // Do some test that may never end... + * } + * + * void test2() + * { + * // Do some test that may never end... + * } + * }; + * \endcode + */ +#define CPPUNIT_TEST_CUSTOMS( testAdderMethod ) \ + testAdderMethod( suite, factory, namer ) + /*! \brief End declaration of the test suite. * * After this macro, member access is set to "private". @@ -242,19 +325,19 @@ namespace CppUnit * \see CPPUNIT_TEST_SUITE. * \see CPPUNIT_TEST_SUITE_REGISTRATION. */ -#define CPPUNIT_TEST_SUITE_END() \ - builder.takeSuite(); \ - } \ - static CppUnit::TestSuite *suite() \ - { \ - CppUnit::TestSuiteBuilder<__ThisTestFixtureType> \ - builder __CPPUNIT_SUITE_CTOR_ARGS( ATestFixtureType ); \ - ThisTestFixtureFactory factory; \ - __ThisTestFixtureType::registerTests( builder.suite(), &factory ); \ - return builder.takeSuite(); \ - } \ - private: /* dummy typedef so that the macro can still end with ';'*/ \ - typedef ThisTestFixtureFactory __ThisTestFixtureFactory +#define CPPUNIT_TEST_SUITE_END() \ + builder.takeSuite(); \ + } \ + static CppUnit::TestSuite *suite() \ + { \ + const CppUnit::TestNamer &namer = __getTestNamer(); \ + CppUnit::TestSuiteBuilder<__ThisTestFixtureType> builder( namer ); \ + __ThisTestFixtureFactory factory; \ + __ThisTestFixtureType::__registerTests( builder.suite(), &factory, namer ); \ + return builder.takeSuite(); \ + } \ + private: /* dummy typedef so that the macro can still end with ';'*/ \ + typedef __ThisTestFixtureFactory __CppUnitDummyTypedefTestFixture /** @} */ diff --git a/include/cppunit/extensions/Makefile.am b/include/cppunit/extensions/Makefile.am index a69ab1f..684a439 100644 --- a/include/cppunit/extensions/Makefile.am +++ b/include/cppunit/extensions/Makefile.am @@ -8,6 +8,7 @@ libcppunitinclude_HEADERS = \ RepeatedTest.h \ TestDecorator.h \ TestFactoryRegistry.h \ + TestNamer.h \ TestSetUp.h \ TestSuiteBuilder.h \ TestSuiteFactory.h \ diff --git a/include/cppunit/extensions/TestNamer.h b/include/cppunit/extensions/TestNamer.h new file mode 100644 index 0000000..91262b9 --- /dev/null +++ b/include/cppunit/extensions/TestNamer.h @@ -0,0 +1,86 @@ +#ifndef CPPUNIT_EXTENSIONS_TESTNAMER_H +#define CPPUNIT_EXTENSIONS_TESTNAMER_H + +#include <cppunit/Portability.h> +#include <string> + + + + +/*! \def CPPUNIT_TESTNAMER_DECL( variableName, FixtureType ) + * \brief Declares a TestNamer. + * + * Declares a TestNamer for the specified type, using RTTI if enabled, otherwise + * using macro string expansion. + * + * RTTI is used if CPPUNIT_USE_TYPEINFO_NAME is defined and not null. + * + * \code + * void someMethod() + * { + * CPPUNIT_TESTNAMER_DECL( namer, AFixtureType ); + * std::string fixtureName = namer.getFixtureName(); + * ... + * \endcode + * + * \relates TestNamer + * \see TestNamer + */ +#if CPPUNIT_USE_TYPEINFO_NAME +# define CPPUNIT_TESTNAMER_DECL( variableName, FixtureType ) \ + CppUnit::TestNamer variableName( typeid(FixtureType) ) +#else +# define CPPUNIT_TESTNAMER_DECL( variableName, FixtureType ) \ + CppUnit::TestNamer variableName( std::string(#FixtureType) ) +#endif + + + +namespace CppUnit +{ + +/*! \brief Names a test or a fixture suite. + * + * TestNamer is usually instantiated using CPPUNIT_TESTNAMER_DECL. + * + */ +class CPPUNIT_API TestNamer +{ +public: +#if CPPUNIT_HAVE_RTTI + /*! Constructs a namer using the fixture's type-info. + * \param typeInfo Type-info of the fixture type. Use to name the fixture suite. + */ + TestNamer( const std::type_info &typeInfo ); +#endif + + /*! Constructs a namer using the specified fixture name. + * \param fixtureName Name of the fixture suite. Usually extracted using a macro. + */ + TestNamer( const std::string &fixtureName ); + + /*! Returns the name of the fixture. + * \return Name of the fixture. + */ + virtual std::string getFixtureName() const; + + /*! Returns the name of the test for the specified method. + * \param testMethodName Name of the method that implements a test. + * \return A string that is the concatenation of the test fixture name + * (returned by getFixtureName()) and\a testMethodName, + * separated using '::'. This provides a fairly unique name for a given + * test. + */ + virtual std::string getTestNameFor( const std::string &testMethodName ) const; + +protected: + std::string m_fixtureName; +}; + + + +} // namespace CppUnit + + + +#endif // CPPUNIT_EXTENSIONS_TESTNAMER_H
\ No newline at end of file diff --git a/include/cppunit/extensions/TestSuiteBuilder.h b/include/cppunit/extensions/TestSuiteBuilder.h index 709053b..f75e430 100644 --- a/include/cppunit/extensions/TestSuiteBuilder.h +++ b/include/cppunit/extensions/TestSuiteBuilder.h @@ -2,101 +2,102 @@ #define CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H #include <cppunit/Portability.h> -#include <memory> -#include <cppunit/TestSuite.h> #include <cppunit/TestCaller.h> -#include <cppunit/extensions/TypeInfoHelper.h> +#include <cppunit/TestSuite.h> +#include <cppunit/extensions/TestNamer.h> +#include <memory> namespace CppUnit { - /*! \brief Helper to add tests to a TestSuite. - * \ingroup WritingTestFixture - * - * All tests added to the TestSuite are prefixed by TestSuite name. The resulting - * TestCase name has the following pattern: - * - * MyTestSuiteName.myTestName - */ - template<typename Fixture> - class TestSuiteBuilder +/*! \brief Helper to add tests to a TestSuite. + * \ingroup WritingTestFixture + * + * All tests added to the TestSuite are prefixed by TestSuite name. The resulting + * TestCase name has the following pattern: + * + * MyTestSuiteName.myTestName + * \see TestNamer. + */ +template<typename Fixture> +class TestSuiteBuilder +{ +public: + typedef void (Fixture::*TestMethod)(); + TestSuiteBuilder( TestSuite *suite, + const TestNamer &namer ) + : m_suite( suite ) + , m_testNamer( namer ) + { + } + + + TestSuiteBuilder( const TestNamer &namer ) + : m_suite( new TestSuite( namer.getFixtureName() ) ) + , m_testNamer( namer ) + { + } + + + TestSuite *suite() const + { + return m_suite.get(); + } + + TestSuite *takeSuite() { - public: - typedef void (Fixture::*TestMethod)(); - -#if CPPUNIT_USE_TYPEINFO_NAME - TestSuiteBuilder() : - m_suite( new TestSuite( - TypeInfoHelper::getClassName( typeid(Fixture) ) ) ) - { - } -#endif - - TestSuiteBuilder( TestSuite *suite ) : m_suite( suite ) - { - } - - TestSuiteBuilder(std::string name) : m_suite( new TestSuite(name) ) - { - } - - TestSuite *suite() const - { - return m_suite.get(); - } - - TestSuite *takeSuite() - { - return m_suite.release(); - } - - void addTest( Test *test ) - { - m_suite->addTest( test ); - } - - void addTestCaller( std::string methodName, - TestMethod testMethod ) - { - Test *test = - new TestCaller<Fixture>( makeTestName( methodName ), - testMethod ); - addTest( test ); - } - - void addTestCaller( std::string methodName, - TestMethod testMethod, - Fixture *fixture ) - { - Test *test = - new TestCaller<Fixture>( makeTestName( methodName ), - testMethod, - fixture); - addTest( test ); - } - - template<typename ExceptionType> - void addTestCallerForException( std::string methodName, - TestMethod testMethod, - Fixture *fixture, - ExceptionType *dummyPointer ) - { - Test *test = new TestCaller<Fixture,ExceptionType>( - makeTestName( methodName ), - testMethod, - fixture); - addTest( test ); - } - - - std::string makeTestName( const std::string &methodName ) - { - return m_suite->getName() + "." + methodName; - } - - private: - std::auto_ptr<TestSuite> m_suite; - }; + return m_suite.release(); + } + + void addTest( Test *test ) + { + m_suite->addTest( test ); + } + + void addTestCaller( std::string methodName, + TestMethod testMethod ) + { + Test *test = + new TestCaller<Fixture>( makeTestName( methodName ), + testMethod ); + addTest( test ); + } + + void addTestCaller( std::string methodName, + TestMethod testMethod, + Fixture *fixture ) + { + Test *test = + new TestCaller<Fixture>( makeTestName( methodName ), + testMethod, + fixture); + addTest( test ); + } + + template<typename ExceptionType> + void addTestCallerForException( std::string methodName, + TestMethod testMethod, + Fixture *fixture, + ExceptionType *dummyPointer ) + { + Test *test = new TestCaller<Fixture,ExceptionType>( + makeTestName( methodName ), + testMethod, + fixture); + addTest( test ); + } + + + std::string makeTestName( const std::string &methodName ) + { + return m_testNamer.getTestNameFor( methodName ); + } + +private: + std::auto_ptr<TestSuite> m_suite; + const TestNamer &m_testNamer; +}; + } // namespace CppUnit |
