blob: 9c816addb3808a5cab63256f6acec237134c0edf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 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( Message("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
|