1
#ifndef CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
2
#define CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
4
#include <cppunit/Portability.h>
5
#include <cppunit/Exception.h>
6
#include <cppunit/extensions/TestCaseDecorator.h>
11
/*! \brief Expected exception test case decorator.
13
* A decorator used to assert that a specific test case should throw an
14
* exception of a given type.
16
* You should use this class only if you need to check the exception object
17
* state (that a specific cause is set for example). If you don't need to
18
* do that, you might consider using CPPUNIT_TEST_EXCEPTION() instead.
20
* Intended use is to subclass and override checkException(). Example:
24
* class NetworkErrorTestCaseDecorator :
25
* public ExceptionTestCaseDecorator<NetworkError>
28
* NetworkErrorTestCaseDecorator( NetworkError::Cause expectedCause )
29
* : m_expectedCause( expectedCause )
33
* void checkException( ExpectedExceptionType &e )
35
* CPPUNIT_ASSERT_EQUAL( m_expectedCause, e.getCause() );
38
* NetworkError::Cause m_expectedCause;
43
template<class ExpectedException>
44
class ExceptionTestCaseDecorator : public TestCaseDecorator
47
typedef ExpectedException ExpectedExceptionType;
49
/*! \brief Decorates the specified test.
50
* \param test TestCase to decorate. Assumes ownership of the test.
52
ExceptionTestCaseDecorator( TestCase *test )
53
: TestCaseDecorator( test )
57
/*! \brief Checks that the expected exception is thrown by the decorated test.
60
* Calls the decorated test runTest() and checks that an exception of
61
* type ExpectedException is thrown. Call checkException() passing the
62
* exception that was caught so that some assertions can be made if
69
TestCaseDecorator::runTest();
71
catch ( ExpectedExceptionType &e )
77
// Moved outside the try{} statement to handle the case where the
78
// expected exception type is Exception (expecting assertion failure).
79
#if CPPUNIT_USE_TYPEINFO_NAME
80
throw Exception( Message(
81
"expected exception not thrown",
82
"Expected exception type: " +
83
TypeInfoHelper::getClassName(
84
typeid( ExpectedExceptionType ) ) ) );
86
throw Exception( Message("expected exception not thrown") );
91
/*! \brief Called when the exception is caught.
93
* Should be overriden to check the exception.
95
virtual void checkException( ExpectedExceptionType &e )
103
#endif // CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H