CppUnit project page FAQ CppUnit home page

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

HelperMacros.h

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////////
00002 // Header file HelperMacros.h
00003 // (c)Copyright 2000, Baptiste Lepilleur.
00004 // Created: 2001/04/15
00005 // //////////////////////////////////////////////////////////////////////////
00006 #ifndef CPPUNIT_EXTENSIONS_HELPERMACROS_H
00007 #define CPPUNIT_EXTENSIONS_HELPERMACROS_H
00008 
00009 #include <cppunit/Portability.h>
00010 #include <cppunit/extensions/AutoRegisterSuite.h>
00011 #include <cppunit/extensions/TestSuiteBuilder.h>
00012 #include <string>
00013 
00014 namespace CppUnit
00015 {
00016   class TestFixture;
00017 
00020   class TestFixtureFactory
00021   {
00022   public:
00024     virtual CppUnit::TestFixture *makeFixture() =0;
00025   };
00026 } // namespace CppUnit
00027 
00028 
00029 // The macro __CPPUNIT_SUITE_CTOR_ARGS expand to an expression used to construct
00030 // the TestSuiteBuilder with macro CPPUNIT_TEST_SUITE.
00031 //
00032 // The name of the suite is obtained using RTTI if CPPUNIT_USE_TYPEINFO_NAME 
00033 // is defined, otherwise it is extracted from the macro parameter
00034 //
00035 // This macro is for cppunit internal and should not be use otherwise.
00036 #if CPPUNIT_USE_TYPEINFO_NAME
00037 #  define __CPPUNIT_SUITE_CTOR_ARGS( ATestFixtureType )
00038 #else
00039 #  define __CPPUNIT_SUITE_CTOR_ARGS( ATestFixtureType ) (std::string(#ATestFixtureType))
00040 #endif
00041 
00042 
00124 #define CPPUNIT_TEST_SUITE( ATestFixtureType )                            \
00125   private:                                                                \
00126     typedef ATestFixtureType __ThisTestFixtureType;                       \
00127     class ThisTestFixtureFactory : public CppUnit::TestFixtureFactory     \
00128     {                                                                     \
00129       virtual CppUnit::TestFixture *makeFixture()                         \
00130       {                                                                   \
00131         return new ATestFixtureType();                                    \
00132       }                                                                   \
00133     };                                                                    \
00134   public:                                                                 \
00135     static void                                                           \
00136     registerTests( CppUnit::TestSuite *suite,                             \
00137                    CppUnit::TestFixtureFactory *factory )                 \
00138     {                                                                     \
00139       CppUnit::TestSuiteBuilder<__ThisTestFixtureType> builder( suite );
00140 
00141 
00172 #define CPPUNIT_TEST_SUB_SUITE( ATestFixtureType, ASuperClass )  \
00173   private:                                                       \
00174     typedef ASuperClass __ThisSuperClassType;                    \
00175     CPPUNIT_TEST_SUITE( ATestFixtureType );                      \
00176       __ThisSuperClassType::registerTests( suite, factory )
00177 
00178 
00185 #define CPPUNIT_TEST( testMethod )                                           \
00186       builder.addTestCaller( #testMethod,                                    \
00187                              &__ThisTestFixtureType::testMethod ,            \
00188                              (__ThisTestFixtureType*)factory->makeFixture() ) 
00189 
00190 
00214 #define CPPUNIT_TEST_EXCEPTION( testMethod, ExceptionType )                   \
00215       builder.addTestCallerForException( #testMethod,                         \
00216                              &__ThisTestFixtureType::testMethod ,             \
00217                              (__ThisTestFixtureType*)factory->makeFixture(),  \
00218                              (ExceptionType *)NULL ); 
00219 
00235 #define CPPUNIT_TEST_FAIL( testMethod ) \
00236               CPPUNIT_TEST_EXCEPTION( testMethod, CppUnit::Exception )
00237 
00245 #define CPPUNIT_TEST_SUITE_END()                                          \
00246       builder.takeSuite();                                                \
00247     }                                                                     \
00248     static CppUnit::TestSuite *suite()                                    \
00249     {                                                                     \
00250       CppUnit::TestSuiteBuilder<__ThisTestFixtureType>                    \
00251           builder __CPPUNIT_SUITE_CTOR_ARGS( ATestFixtureType );          \
00252       ThisTestFixtureFactory factory;                                     \
00253       __ThisTestFixtureType::registerTests( builder.suite(), &factory );  \
00254       return builder.takeSuite();                                         \
00255     }                                                                     \
00256   private: /* dummy typedef so that the macro can still end with ';'*/    \
00257     typedef ThisTestFixtureFactory __ThisTestFixtureFactory                   
00258 
00262 #define __CPPUNIT_CONCATENATE_DIRECT( s1, s2 ) s1##s2
00263 #define __CPPUNIT_CONCATENATE( s1, s2 ) __CPPUNIT_CONCATENATE_DIRECT( s1, s2 )
00264 
00268 #define __CPPUNIT_MAKE_UNIQUE_NAME( str ) __CPPUNIT_CONCATENATE( str, __LINE__ )
00269 
00270 
00286 #define CPPUNIT_TEST_SUITE_REGISTRATION( ATestFixtureType )      \
00287   static CppUnit::AutoRegisterSuite< ATestFixtureType >          \
00288              __CPPUNIT_MAKE_UNIQUE_NAME(__autoRegisterSuite )
00289 
00290 
00326 #define CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ATestFixtureType, suiteName ) \
00327   static CppUnit::AutoRegisterSuite< ATestFixtureType >                      \
00328              __CPPUNIT_MAKE_UNIQUE_NAME(__autoRegisterSuite )(suiteName)
00329 
00330 
00331 // Backwards compatibility
00332 // (Not tested!)
00333 
00334 #if CPPUNIT_ENABLE_CU_TEST_MACROS
00335 
00336 #define CU_TEST_SUITE(tc) CPPUNIT_TEST_SUITE(tc)
00337 #define CU_TEST_SUB_SUITE(tc,sc) CPPUNIT_TEST_SUB_SUITE(tc,sc)
00338 #define CU_TEST(tm) CPPUNIT_TEST(tm)
00339 #define CU_TEST_SUITE_END() CPPUNIT_TEST_SUITE_END()
00340 #define CU_TEST_SUITE_REGISTRATION(tc) CPPUNIT_TEST_SUITE_REGISTRATION(tc)
00341 
00342 #endif
00343 
00344 
00345 #endif  // CPPUNIT_EXTENSIONS_HELPERMACROS_H

SourceForge Logo hosts this site. Send comments to:
CppUnit Developers