|
Packit |
8c9aa0 |
#ifndef CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
|
|
Packit |
8c9aa0 |
#define CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
#include <cppunit/Portability.h>
|
|
Packit |
8c9aa0 |
#include <cppunit/Exception.h>
|
|
Packit |
8c9aa0 |
#include <cppunit/extensions/TestCaseDecorator.h>
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
CPPUNIT_NS_BEGIN
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
/*! \brief Expected exception test case decorator.
|
|
Packit |
8c9aa0 |
*
|
|
Packit |
8c9aa0 |
* A decorator used to assert that a specific test case should throw an
|
|
Packit |
8c9aa0 |
* exception of a given type.
|
|
Packit |
8c9aa0 |
*
|
|
Packit |
8c9aa0 |
* You should use this class only if you need to check the exception object
|
|
Packit |
8c9aa0 |
* state (that a specific cause is set for example). If you don't need to
|
|
Packit |
8c9aa0 |
* do that, you might consider using CPPUNIT_TEST_EXCEPTION() instead.
|
|
Packit |
8c9aa0 |
*
|
|
Packit |
8c9aa0 |
* Intended use is to subclass and override checkException(). Example:
|
|
Packit |
8c9aa0 |
*
|
|
Packit |
8c9aa0 |
* \code
|
|
Packit |
8c9aa0 |
*
|
|
Packit |
8c9aa0 |
* class NetworkErrorTestCaseDecorator :
|
|
Packit |
8c9aa0 |
* public ExceptionTestCaseDecorator<NetworkError>
|
|
Packit |
8c9aa0 |
* {
|
|
Packit |
8c9aa0 |
* public:
|
|
Packit |
8c9aa0 |
* NetworkErrorTestCaseDecorator( NetworkError::Cause expectedCause )
|
|
Packit |
8c9aa0 |
* : m_expectedCause( expectedCause )
|
|
Packit |
8c9aa0 |
* {
|
|
Packit |
8c9aa0 |
* }
|
|
Packit |
8c9aa0 |
* private:
|
|
Packit |
8c9aa0 |
* void checkException( ExpectedExceptionType &e )
|
|
Packit |
8c9aa0 |
* {
|
|
Packit |
8c9aa0 |
* CPPUNIT_ASSERT_EQUAL( m_expectedCause, e.getCause() );
|
|
Packit |
8c9aa0 |
* }
|
|
Packit |
8c9aa0 |
*
|
|
Packit |
8c9aa0 |
* NetworkError::Cause m_expectedCause;
|
|
Packit |
8c9aa0 |
* };
|
|
Packit |
8c9aa0 |
* \endcode
|
|
Packit |
8c9aa0 |
*
|
|
Packit |
8c9aa0 |
*/
|
|
Packit |
8c9aa0 |
template<class ExpectedException>
|
|
Packit |
8c9aa0 |
class ExceptionTestCaseDecorator : public TestCaseDecorator
|
|
Packit |
8c9aa0 |
{
|
|
Packit |
8c9aa0 |
public:
|
|
Packit |
8c9aa0 |
typedef ExpectedException ExpectedExceptionType;
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
/*! \brief Decorates the specified test.
|
|
Packit |
8c9aa0 |
* \param test TestCase to decorate. Assumes ownership of the test.
|
|
Packit |
8c9aa0 |
*/
|
|
Packit |
8c9aa0 |
ExceptionTestCaseDecorator( TestCase *test )
|
|
Packit |
8c9aa0 |
: TestCaseDecorator( test )
|
|
Packit |
8c9aa0 |
{
|
|
Packit |
8c9aa0 |
}
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
/*! \brief Checks that the expected exception is thrown by the decorated test.
|
|
Packit |
8c9aa0 |
* is thrown.
|
|
Packit |
8c9aa0 |
*
|
|
Packit |
8c9aa0 |
* Calls the decorated test runTest() and checks that an exception of
|
|
Packit |
8c9aa0 |
* type ExpectedException is thrown. Call checkException() passing the
|
|
Packit |
8c9aa0 |
* exception that was caught so that some assertions can be made if
|
|
Packit |
8c9aa0 |
* needed.
|
|
Packit |
8c9aa0 |
*/
|
|
Packit |
8c9aa0 |
void runTest()
|
|
Packit |
8c9aa0 |
{
|
|
Packit |
8c9aa0 |
try
|
|
Packit |
8c9aa0 |
{
|
|
Packit |
8c9aa0 |
TestCaseDecorator::runTest();
|
|
Packit |
8c9aa0 |
}
|
|
Packit |
8c9aa0 |
catch ( ExpectedExceptionType &e )
|
|
Packit |
8c9aa0 |
{
|
|
Packit |
8c9aa0 |
checkException( e );
|
|
Packit |
8c9aa0 |
return;
|
|
Packit |
8c9aa0 |
}
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
// Moved outside the try{} statement to handle the case where the
|
|
Packit |
8c9aa0 |
// expected exception type is Exception (expecting assertion failure).
|
|
Packit |
8c9aa0 |
#if defined(CPPUNIT_USE_TYPEINFO_NAME)
|
|
Packit |
8c9aa0 |
throw Exception( Message(
|
|
Packit |
8c9aa0 |
"expected exception not thrown",
|
|
Packit |
8c9aa0 |
"Expected exception type: " +
|
|
Packit |
8c9aa0 |
TypeInfoHelper::getClassName(
|
|
Packit |
8c9aa0 |
typeid( ExpectedExceptionType ) ) ) );
|
|
Packit |
8c9aa0 |
#else
|
|
Packit |
8c9aa0 |
throw Exception( Message("expected exception not thrown") );
|
|
Packit |
8c9aa0 |
#endif
|
|
Packit |
8c9aa0 |
}
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
private:
|
|
Packit |
8c9aa0 |
/*! \brief Called when the exception is caught.
|
|
Packit |
8c9aa0 |
*
|
|
Packit |
8c9aa0 |
* Should be overriden to check the exception.
|
|
Packit |
8c9aa0 |
*/
|
|
Packit |
8c9aa0 |
virtual void checkException( ExpectedExceptionType & )
|
|
Packit |
8c9aa0 |
{
|
|
Packit |
8c9aa0 |
}
|
|
Packit |
8c9aa0 |
};
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
CPPUNIT_NS_END
|
|
Packit |
8c9aa0 |
|
|
Packit |
8c9aa0 |
#endif // CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
|
|
Packit |
8c9aa0 |
|