From 90296fa9c8fdedb9104faba5a8bac62210a9a80d Mon Sep 17 00:00:00 2001 From: Bastiaan Bakker Date: Sun, 6 May 2001 19:04:02 +0000 Subject: Added constructors to TestCaller which accept an already constucted Fixture. --- include/cppunit/TestCaller.h | 74 ++++++++++++++++++++------- include/cppunit/extensions/TestSuiteBuilder.h | 7 +-- 2 files changed, 59 insertions(+), 22 deletions(-) (limited to 'include/cppunit') diff --git a/include/cppunit/TestCaller.h b/include/cppunit/TestCaller.h index 9262b86..f71734e 100644 --- a/include/cppunit/TestCaller.h +++ b/include/cppunit/TestCaller.h @@ -1,7 +1,6 @@ #ifndef CPPUNIT_TESTCALLER_H #define CPPUNIT_TESTCALLER_H -#include #include namespace CppUnit { @@ -36,7 +35,6 @@ namespace CppUnit { * * You can use a TestCaller to bind any test method on a TestCase * class, as long as it accepts void and returns void. - * TestCallers are automatically registered in the TestRegistry. * * \see TestCase */ @@ -47,26 +45,74 @@ namespace CppUnit { 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_fixture (new Fixture ()), + 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. + **/ + TestCaller(std::string name, TestMethod test, Fixture* fixture) : + TestCase (name), + m_ownFixture(true), + m_fixture (fixture), + m_test (test) + {} + + ~TestCaller() { + if (m_ownFixture) { + if (m_fixture) { + delete m_fixture; + m_fixture = NULL; + } + } + } + protected: void runTest () { - (m_fixture.get ()->*m_test)(); + (m_fixture->*m_test)(); } void setUp () { - m_fixture.get ()->setUp (); + m_fixture->setUp (); } void tearDown () { - m_fixture.get ()->tearDown (); + m_fixture->tearDown (); } std::string toString () const @@ -79,22 +125,12 @@ namespace CppUnit { TestCaller& operator= (const TestCaller& other); private: - std::auto_ptr m_fixture; - TestMethod m_test; + bool m_ownFixture; + Fixture* m_fixture; + TestMethod m_test; }; - /** Returns a TestCaller for the specified method. - * \param name Name for the TestCaller. - * \param testMethod Method called by the TestCaller. - * \return TestCaller for the specified method. - */ - template - Test *makeTestCaller( std::string name, void (Fixture::*testMethod)() ) - { - return new TestCaller( name, testMethod ); - } - } // namespace CppUnit #endif // CPPUNIT_TESTCALLER_H diff --git a/include/cppunit/extensions/TestSuiteBuilder.h b/include/cppunit/extensions/TestSuiteBuilder.h index d85683d..ab553e9 100644 --- a/include/cppunit/extensions/TestSuiteBuilder.h +++ b/include/cppunit/extensions/TestSuiteBuilder.h @@ -38,9 +38,10 @@ namespace CppUnit { void addTestCaller( std::string name, TestMethod testMethod ) { - Test *test = makeTestCaller( m_suite->getName() + "." + name, - testMethod ); - addTest( test ); + Test *test = + new TestCaller( m_suite->getName() + "." + name, + testMethod ); + addTest( test ); } private: -- cgit v1.2.1