diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-08-03 14:50:09 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-08-03 14:50:09 +0000 |
| commit | e82ccd481800f8f0d36af5310c535b83a6cec788 (patch) | |
| tree | 2b312dbac05563f3ed7baf63ddbd1fcdcbefc686 /include/cppunit/extensions | |
| parent | 2a31073734be6e44e477079699578820282b7345 (diff) | |
| download | cppunit-e82ccd481800f8f0d36af5310c535b83a6cec788.tar.gz | |
Include/cppunit/Exception.
include/cppunit/Exception.h:
* src/cppunit/Exception.h: added setMessage().
* include/cppunit/Protector.h:
* src/cppunit/Protector.cpp: added class ProtectorGuard. Change the
reportXXX() method to support Exception passing and SourceLine.
* include/cppunit/TestCaller.h: removed 'expect exception' features.
It is now handled by ExceptionTestCaseDecorator and TestCaller no
longer need default template argument support.
* include/cppunit/TestCase.h:
* include/cppunit/extensions/TestCaller.h: runTest() is now public
instead of protected, so that it can be decorated.
* include/cppunit/TestResult.h:
* src/cppunit/TestResult.h: added pushProtector() and popProtector()
methods. This allow user to specify their own exception trap when
running test case.
* include/cppunit/extensions/TestDecorator.h:
* src/cppunit/TestDecorator.cpp: added. Extracted from TestDecorator.h.
The test passed to the constructor is now owned by the decorator.
* include/cppunit/extensions/TestCaseDecorator.h:
* src/cppunit/TestCaseDecorator.cpp: added. Decorator for TestCase
setUp(), tearDown() and runTest().
* include/cppunit/extensions/ExceptionTestCaseDecorator.h: added.
TestCaseDecorator to expect that a specific exception is thrown.
* include/cppunit/extensions/HelperMacros.h: updated against TestCaller
change.
* src/cppunit/DefaultFunctor.h: fixed bug (did not return underlying
test return code).
* src/cppunit/ProtectorChain.cpp: fixed bug in chaing return code.
* src/cppunit/DefaultFunctor.h: fixed bug.
* src/msvc6/testrunner/ActiveTest.h:
* src/msvc6/testrunner/ActiveTest.cpp: updated against
TestCaseDecorator ownership policy change. Moved inline functions
to .cpp.
* examples/cppunittest/TestSetUpTest.cpp: updated to use MockTestCase
and against the new ownership policy.
* examples/cppunittest/TestDecoratorTest.cpp:
* examples/cppunittest/RepeatedTestTest.cpp: updated against
TestDecorator ownership policy change.
* examples/cppunittest/ExceptionTestCaseDecoratorTest.h:
* examples/cppunittest/ExceptionTestCaseDecoratorTest.cpp: added. Unit
tests for ExceptionTestCaseDecoratorTest.
Diffstat (limited to 'include/cppunit/extensions')
| -rw-r--r-- | include/cppunit/extensions/ExceptionTestCaseDecorator.h | 104 | ||||
| -rw-r--r-- | include/cppunit/extensions/HelperMacros.h | 13 | ||||
| -rw-r--r-- | include/cppunit/extensions/Makefile.am | 2 | ||||
| -rw-r--r-- | include/cppunit/extensions/TestCaseDecorator.h | 40 | ||||
| -rw-r--r-- | include/cppunit/extensions/TestDecorator.h | 48 |
5 files changed, 156 insertions, 51 deletions
diff --git a/include/cppunit/extensions/ExceptionTestCaseDecorator.h b/include/cppunit/extensions/ExceptionTestCaseDecorator.h new file mode 100644 index 0000000..d8d1e82 --- /dev/null +++ b/include/cppunit/extensions/ExceptionTestCaseDecorator.h @@ -0,0 +1,104 @@ +#ifndef CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H +#define CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H + +#include <cppunit/Portability.h> +#include <cppunit/Exception.h> +#include <cppunit/extensions/TestCaseDecorator.h> + +CPPUNIT_NS_BEGIN + + +/*! \brief Expected exception test case decorator. + * + * A decorator used to assert that a specific test case should throw an + * exception of a given type. + * + * You should use this class only if you need to check the exception object + * state (that a specific cause is set for example). If you don't need to + * do that, you might consider using CPPUNIT_TEST_EXCEPTION() instead. + * + * Intended use is to subclass and override checkException(). Example: + * + * \code + * + * class NetworkErrorTestCaseDecorator : + * public ExceptionTestCaseDecorator<NetworkError> + * { + * public: + * NetworkErrorTestCaseDecorator( NetworkError::Cause expectedCause ) + * : m_expectedCause( expectedCause ) + * { + * } + * private: + * void checkException( ExpectedExceptionType &e ) + * { + * CPPUNIT_ASSERT_EQUAL( m_expectedCause, e.getCause() ); + * } + * + * NetworkError::Cause m_expectedCause; + * }; + * \endcode + * + */ +template<class ExpectedException> +class CPPUNIT_API ExceptionTestCaseDecorator : public TestCaseDecorator +{ +public: + typedef ExpectedException ExpectedExceptionType; + + /*! \brief Decorates the specified test. + * \param test TestCase to decorate. Assumes ownership of the test. + */ + ExceptionTestCaseDecorator( TestCase *test ) + : TestCaseDecorator( test ) + { + } + + /*! \brief Checks that the expected exception is thrown by the decorated test. + * is thrown. + * + * Calls the decorated test runTest() and checks that an exception of + * type ExpectedException is thrown. Call checkException() passing the + * exception that was caught so that some assertions can be made if + * needed. + */ + void runTest() + { + try + { + TestCaseDecorator::runTest(); + } + catch ( ExpectedExceptionType &e ) + { + checkException( e ); + return; + } + + // Moved outside the try{} statement to handle the case where the + // expected exception type is Exception (expecting assertion failure). +#if CPPUNIT_USE_TYPEINFO_NAME + throw Exception( Message( + "expected exception not thrown", + "Expected exception type: " + + TypeInfoHelper::getClassName( + typeid( ExpectedExceptionType ) ) ) ); +#else + throw Exception( "expected exception not thrown" ); +#endif + } + +private: + /*! \brief Called when the exception is caught. + * + * Should be overriden to check the exception. + */ + virtual void checkException( ExpectedExceptionType &e ) + { + } +}; + + +CPPUNIT_NS_END + +#endif // CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H + diff --git a/include/cppunit/extensions/HelperMacros.h b/include/cppunit/extensions/HelperMacros.h index 379187f..05b3631 100644 --- a/include/cppunit/extensions/HelperMacros.h +++ b/include/cppunit/extensions/HelperMacros.h @@ -9,6 +9,7 @@ #include <cppunit/Portability.h> #include <cppunit/extensions/AutoRegisterSuite.h> #include <cppunit/extensions/TestSuiteBuilder.h> +#include <cppunit/extensions/ExceptionTestCaseDecorator.h> #include <string> CPPUNIT_NS_BEGIN @@ -272,11 +273,17 @@ CPPUNIT_NS_END * method. */ #define CPPUNIT_TEST_EXCEPTION( testMethod, ExceptionType ) \ - CPPUNIT_TEST_ADD( (new CPPUNIT_NS::TestCaller<ThisTestFixtureType, \ - ExceptionType>( \ + CPPUNIT_TEST_ADD( \ + (new CPPUNIT_NS::ExceptionTestCaseDecorator< ExceptionType >( \ + new CPPUNIT_NS::TestCaller< ThisTestFixtureType >( \ namer.getTestNameFor( #testMethod ), \ &ThisTestFixtureType::testMethod, \ - factory.makeFixture() ) ) ) + factory.makeFixture() ) ) ) ) +// CPPUNIT_TEST_ADD( (new CPPUNIT_NS::TestCaller<ThisTestFixtureType, \ +// ExceptionType>( \ +// namer.getTestNameFor( #testMethod ), \ +// &ThisTestFixtureType::testMethod, \ +// factory.makeFixture() ) ) ) /*! \brief Adds a test case which is excepted to fail. * diff --git a/include/cppunit/extensions/Makefile.am b/include/cppunit/extensions/Makefile.am index 684a439..1db3ec3 100644 --- a/include/cppunit/extensions/Makefile.am +++ b/include/cppunit/extensions/Makefile.am @@ -6,6 +6,8 @@ libcppunitinclude_HEADERS = \ HelperMacros.h \ Orthodox.h \ RepeatedTest.h \ + ExceptionTestCaseDecorator.h \ + TestCaseDecorator.h \ TestDecorator.h \ TestFactoryRegistry.h \ TestNamer.h \ diff --git a/include/cppunit/extensions/TestCaseDecorator.h b/include/cppunit/extensions/TestCaseDecorator.h new file mode 100644 index 0000000..3a15ba9 --- /dev/null +++ b/include/cppunit/extensions/TestCaseDecorator.h @@ -0,0 +1,40 @@ +#ifndef CPPUNIT_EXTENSIONS_TESTCASEDECORATOR_H +#define CPPUNIT_EXTENSIONS_TESTCASEDECORATOR_H + +#include <cppunit/Portability.h> +#include <cppunit/TestCase.h> + +CPPUNIT_NS_BEGIN + + +/*! \brief Decorator for Test cases. + * + * TestCaseDecorator provides an alternate means to extend functionality + * of a test class without subclassing the test. Instead, one can + * subclass the decorater and use it to wrap the test class. + * + * Does not assume ownership of the test it decorates + */ +class CPPUNIT_API TestCaseDecorator : public TestCase +{ +public: + TestCaseDecorator( TestCase *test ); + ~TestCaseDecorator(); + + std::string getName() const; + + void setUp(); + + void tearDown(); + + void runTest(); + +protected: + TestCase *m_test; +}; + + +CPPUNIT_NS_END + +#endif + diff --git a/include/cppunit/extensions/TestDecorator.h b/include/cppunit/extensions/TestDecorator.h index 408c743..59d9a30 100644 --- a/include/cppunit/extensions/TestDecorator.h +++ b/include/cppunit/extensions/TestDecorator.h @@ -43,54 +43,6 @@ private: }; -inline -TestDecorator::TestDecorator( Test *test ) -{ - m_test = test; -} - - -inline -TestDecorator::~TestDecorator() -{ -} - - -inline int -TestDecorator::countTestCases() const -{ - return m_test->countTestCases(); -} - - -inline void -TestDecorator::run( TestResult *result ) -{ - m_test->run(result); -} - - -inline std::string -TestDecorator::getName() const -{ - return m_test->getName(); -} - - -inline int -TestDecorator::getChildTestCount() const -{ - return m_test->getChildTestCount(); -} - - -inline Test * -TestDecorator::doGetChildTestAt( int index ) const -{ - return m_test->getChildTestAt( index ); -} - - CPPUNIT_NS_END #endif |
