CppUnit project page CppUnit home page

testfixture.h

Go to the documentation of this file.
00001 #ifndef CPPUT_TESTFIXTURE_H_INCLUDED
00002 # define CPPUT_TESTFIXTURE_H_INCLUDED
00003 
00004 # include <cpput/forwards.h>
00005 # include <cpput/extendeddata.h>
00006 # include <cpput/testcase.h>
00007 # include <cpput/testsuite.h>
00008 # include <cpput/translate.h>
00009 # include <cpptl/typename.h>
00010 
00011 
00012 namespace CppUT {
00013 
00014 
00017 class CPPUT_API TestFixture : public CppTL::IntrusiveCount
00018                             , public TestExtendedDataFactory
00019 {
00020 public:
00021    virtual ~TestFixture()
00022    {
00023    }
00024 
00025    virtual void setUp()
00026    {
00027    }
00028 
00029    virtual void tearDown()
00030    {
00031    }
00032 
00033    static void addTestWithSpecifics( TestSuite &suite,
00034                                      const TestPtr &test,
00035                                      const TestExtendedData &specifics )
00036    {
00037       ::CppUT::TestExtendedDataHelper specificHelper( *test );
00038       specificHelper( specifics );
00039       suite.add( test );
00040    }
00041 };
00042 
00043 
00044 
00046 namespace Impl {
00047 
00048    typedef CppTL::IntrusivePtr<TestFixture> TestFixturePtr;
00049 
00050    class CPPUT_API FixtureFactory
00051    {
00052    public:
00053       virtual ~FixtureFactory()
00054       {
00055       }
00056 
00057       TestFixturePtr operator()()
00058       {
00059          return createNewFixture();
00060       }
00061 
00062    private:
00063       virtual TestFixturePtr createNewFixture() =0;
00064    };
00065 
00066 
00067    template<typename FixtureType>
00068    class FixtureFactoryImpl : public FixtureFactory
00069    {
00070    public:
00071       TestFixturePtr createNewFixture()
00072       {
00073          return TestFixturePtr( new FixtureType() );
00074       }
00075    };
00076 
00077    template<typename FixtureType>
00078    class FixtureFactoryWrapper
00079    {
00080    public:
00081       FixtureFactoryWrapper( FixtureFactory &factory )
00082          : factory_( factory )
00083       {
00084       }
00085 
00086       CppTL::IntrusivePtr<FixtureType> operator()()
00087       {
00088         return ::CppTL::staticPointerCast<FixtureType>( factory_() );
00089       }
00090 
00091    private:
00092       FixtureFactory &factory_;
00093    };
00094 } // namespace Impl
00096 
00097 
00098 /* Code generated by the helper macros is roughly like this:
00099 
00100 class SomeTest : public CppUT::TestFixture
00101 {
00102 public:     // CPPUT_TESTSUITE_BEGIN
00103    typedef FixtureType CppUT_ThisType;
00104 
00105    static std::string defaultSuiteName()
00106    {
00107       return ::CppTL::getTypeName<CppUT_ThisType>( #FixtureType );
00108    }
00109 
00110    void addTests_( const ::CppUT::TestSuitePtr &suite,
00111                    ::CppUT::Impl::FixtureFactory &factory_ )
00112    {
00113       ::CppUT::Impl::FixtureFactoryWrapper<CppUT_ThisType> fixtureFactory( factory_ );
00114       ::CppTL::IntrusivePtr<FixtureType> fixture;
00115 
00116       // CPPUT_TESTSUITE_EXTEND (if defined)
00117       ParentFixtureType::addTests_( suite, factory_ );
00118 
00119       // CPPUT_TEST( testMethod )
00120       fixture = fixtureFactory();
00121       suite->add( ::CppUT::makeFixtureTestCase( fixture, 
00122                                                 &FixtureType::testMethod, 
00123                                                 #testMethod ) );
00124 
00125       
00126    }  // if ended using CPPUT_ABSTRACT_TESTSUITE_END, stop there (suite() is not defined)
00127 
00128    // if ended using CPPUT_TESTSUITE_END, define the static function suite
00129    static ::CppUT::TestSuitePtr suite()
00130    {
00131       ::CppUT::TestSuitePtr testSuite( ::CppUT::makeTestSuite( defaultSuiteName() ) );
00132       ::CppUT::Impl::FixtureFactoryImpl<CppUT_ThisType> factory;
00133       addTests_( testSuite, factory );
00134       return testSuite;
00135    }
00136 };
00137 
00138 */
00139 
00140 
00141 
00142 # if CPPUT_USE_RTTI_TO_NAME_SUITE
00143 #  define CPPUT_TESTSUITE_NAME_FROM_TYPE( FixtureType )              \
00144       ::CppTL::getTypeName( ::CppTL::Type<FixtureType>(), #FixtureType )
00145 # else // if CPPUT_USE_RTTI_TO_NAME_SUITE
00146 #  define CPPUT_TESTSUITE_NAME_FROM_TYPE( FixtureType )              \
00147       std::string( #FixtureType )
00148 # endif // if CPPUT_USE_RTTI_TO_NAME_SUITE
00149 
00150 
00151 
00154 # define CPPUT_TESTSUITE_BEGIN( FixtureType )                                \
00155 public:                                                                      \
00156    typedef FixtureType CppUT_ThisType;                                       \
00157                                                                              \
00158    static std::string defaultSuiteName()                                     \
00159    {                                                                         \
00160       return CPPUT_TESTSUITE_NAME_FROM_TYPE( FixtureType );                  \
00161    }                                                                         \
00162                                                                              \
00163    static void addTests_( const ::CppUT::TestSuitePtr &suite,                \
00164                           ::CppUT::Impl::FixtureFactory &factory_ )          \
00165    {                                                                         \
00166       ::CppUT::Impl::FixtureFactoryWrapper<CppUT_ThisType> fixtureFactory(   \
00167          factory_ );                                                         \
00168       ::CppTL::IntrusivePtr<FixtureType> fixture
00169   
00172 # define CPPUT_TESTSUITE_EXTEND( FixtureType, ParentFixtureType )            \
00173       CPPUT_TESTSUITE_BEGIN( FixtureType );                                  \
00174       ParentFixtureType::addTests_( suite, factory_ )
00175 
00178 # define CPPUT_TESTSUITE_END()                                                \
00179    }                                                                          \
00180                                                                               \
00181    static ::CppUT::TestPtr suite( const std::string &name = std::string("") ) \
00182    {                                                                          \
00183       std::string suiteName = name;                                           \
00184       if ( suiteName.empty() )                                                \
00185           suiteName = defaultSuiteName();                                     \
00186       ::CppUT::TestSuitePtr testSuite = ::CppUT::makeTestSuite( suiteName );  \
00187       ::CppUT::Impl::FixtureFactoryImpl<CppUT_ThisType> factory;              \
00188       addTests_( testSuite, factory );                                        \
00189       return ::CppTL::staticPointerCast< ::CppUT::Test >( testSuite );        \
00190    }
00191 
00194 # define CPPUT_ABSTRACT_TESTSUITE_END()                                      \
00195    }
00196 
00199 # define CPPUT_TEST( testMethod )                                            \
00200       fixture = fixtureFactory();                                            \
00201       suite->add( ::CppUT::makeFixtureTestCase( fixture,                     \
00202                    ::CppTL::memfn0( fixture,                                 \
00203                                     &CppUT_ThisType::testMethod ),           \
00204                    #testMethod ) )
00205 
00208 # define CPPUT_TEST_WITH_SPECIFICS( testMethod, specifics )                  \
00209       fixture = fixtureFactory();                                            \
00210       addTestWithSpecifics( *suite,                                          \
00211                             ::CppUT::makeFixtureTestCase( fixture,           \
00212                                ::CppTL::memfn0( fixture,                     \
00213                                   &CppUT_ThisType::testMethod ),             \
00214                                                           #testMethod ),     \
00215                             specifics )
00216          
00217 } // namespace CppUT
00218 
00219 
00220 #endif // CPPUT_TESTFIXTURE_H_INCLUDED

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