diff --git a/include/cppunit/TestCaller.h b/include/cppunit/TestCaller.h index dc4d82e..9e90edd 100644 --- a/include/cppunit/TestCaller.h +++ b/include/cppunit/TestCaller.h @@ -3,6 +3,7 @@ #include #include +#include #if CPPUNIT_USE_TYPEINFO_NAME @@ -106,80 +107,41 @@ class TestCaller : public TestCase typedef void (Fixture::*TestMethod)(); public: - /*! - * Constructor for TestCaller. This constructor builds a new Fixture - * instance owned by the TestCaller. - * \param name name of this TestCaller - * \param test the method this TestCaller calls in runTest() - */ - TestCaller( std::string name, TestMethod test ) : - TestCase( name ), - m_ownFixture( true ), - m_fixture( new Fixture() ), - m_test( test ) - { - } - - /*! - * Constructor for TestCaller. - * This constructor does not create a new Fixture instance but accepts - * an existing one as parameter. The TestCaller will not own the - * Fixture object. - * \param name name of this TestCaller - * \param test the method this TestCaller calls in runTest() - * \param fixture the Fixture to invoke the test method on. - */ - TestCaller(std::string name, TestMethod test, Fixture& fixture) : - TestCase( name ), - m_ownFixture( false ), - m_fixture( &fixture ), - m_test( test ) - { - } /*! * Constructor for TestCaller. - * This constructor does not create a new Fixture instance but accepts - * an existing one as parameter. The TestCaller will own the - * Fixture object and delete it in its destructor. * \param name name of this TestCaller * \param test the method this TestCaller calls in runTest() - * \param fixture the Fixture to invoke the test method on. + * \param factory the TestFixtureFactory. */ - TestCaller(std::string name, TestMethod test, Fixture* fixture) : + TestCaller(std::string name, TestMethod test, TestFixtureFactory* factory) : TestCase( name ), - m_ownFixture( true ), - m_fixture( fixture ), - m_test( test ) + m_fixture( 0 ), + m_test( test ), + m_factory( factory ) { } - - ~TestCaller() - { - if (m_ownFixture) - delete m_fixture; - } + ~TestCaller() + { + delete m_factory; + } + void runTest() { -// try { (m_fixture->*m_test)(); -// } -// catch ( ExpectedException & ) { -// return; -// } - -// ExpectedExceptionTraits::expectedException(); } void setUp() { + m_fixture = static_cast(m_factory->makeFixture()); m_fixture->setUp (); } void tearDown() { m_fixture->tearDown (); + delete m_fixture; } std::string toString() const @@ -192,9 +154,9 @@ private: TestCaller &operator =( const TestCaller &other ); private: - bool m_ownFixture; Fixture *m_fixture; TestMethod m_test; + TestFixtureFactory *m_factory; }; diff --git a/include/cppunit/extensions/HelperMacros.h b/include/cppunit/extensions/HelperMacros.h index 12431e4..479e104 100644 --- a/include/cppunit/extensions/HelperMacros.h +++ b/include/cppunit/extensions/HelperMacros.h @@ -298,7 +298,7 @@ ( new CPPUNIT_NS::TestCaller( \ context.getTestNameFor( #testMethod), \ &TestFixtureType::testMethod, \ - context.makeFixture() ) ) ) + context.cloneFactory() ) ) ) /*! \brief Add a test which fail if the specified exception is not caught. * diff --git a/include/cppunit/extensions/TestFixtureFactory.h b/include/cppunit/extensions/TestFixtureFactory.h index 45354c6..d1a1295 100644 --- a/include/cppunit/extensions/TestFixtureFactory.h +++ b/include/cppunit/extensions/TestFixtureFactory.h @@ -18,6 +18,7 @@ class TestFixtureFactory public: //! Creates a new TestFixture instance. virtual TestFixture *makeFixture() =0; + virtual TestFixtureFactory *clone() =0; virtual ~TestFixtureFactory() {} }; @@ -40,6 +41,11 @@ class ConcretTestFixtureFactory : public CPPUNIT_NS::TestFixtureFactory { return new TestFixtureType(); } + TestFixtureFactory *clone() + { + return new ConcretTestFixtureFactory(*this); + } + }; diff --git a/include/cppunit/extensions/TestSuiteBuilderContext.h b/include/cppunit/extensions/TestSuiteBuilderContext.h index db26926..f978bfd 100644 --- a/include/cppunit/extensions/TestSuiteBuilderContext.h +++ b/include/cppunit/extensions/TestSuiteBuilderContext.h @@ -74,6 +74,11 @@ public: */ const std::string getStringProperty( const std::string &key ) const; + TestFixtureFactory *cloneFactory() const + { + return m_factory.clone(); + } + protected: TestFixture *makeTestFixture() const;