[876] | 1 | #ifndef CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H |
---|
| 2 | #define CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H |
---|
| 3 | |
---|
| 4 | #include <cppunit/Portability.h> |
---|
| 5 | #include <cppunit/Exception.h> |
---|
| 6 | #include <cppunit/extensions/TestCaseDecorator.h> |
---|
| 7 | |
---|
| 8 | CPPUNIT_NS_BEGIN |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | /*! \brief Expected exception test case decorator. |
---|
| 12 | * |
---|
| 13 | * A decorator used to assert that a specific test case should throw an |
---|
| 14 | * exception of a given type. |
---|
| 15 | * |
---|
| 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. |
---|
| 19 | * |
---|
| 20 | * Intended use is to subclass and override checkException(). Example: |
---|
| 21 | * |
---|
| 22 | * \code |
---|
| 23 | * |
---|
| 24 | * class NetworkErrorTestCaseDecorator : |
---|
| 25 | * public ExceptionTestCaseDecorator<NetworkError> |
---|
| 26 | * { |
---|
| 27 | * public: |
---|
| 28 | * NetworkErrorTestCaseDecorator( NetworkError::Cause expectedCause ) |
---|
| 29 | * : m_expectedCause( expectedCause ) |
---|
| 30 | * { |
---|
| 31 | * } |
---|
| 32 | * private: |
---|
| 33 | * void checkException( ExpectedExceptionType &e ) |
---|
| 34 | * { |
---|
| 35 | * CPPUNIT_ASSERT_EQUAL( m_expectedCause, e.getCause() ); |
---|
| 36 | * } |
---|
| 37 | * |
---|
| 38 | * NetworkError::Cause m_expectedCause; |
---|
| 39 | * }; |
---|
| 40 | * \endcode |
---|
| 41 | * |
---|
| 42 | */ |
---|
| 43 | template<class ExpectedException> |
---|
| 44 | class ExceptionTestCaseDecorator : public TestCaseDecorator |
---|
| 45 | { |
---|
| 46 | public: |
---|
| 47 | typedef ExpectedException ExpectedExceptionType; |
---|
| 48 | |
---|
| 49 | /*! \brief Decorates the specified test. |
---|
| 50 | * \param test TestCase to decorate. Assumes ownership of the test. |
---|
| 51 | */ |
---|
| 52 | ExceptionTestCaseDecorator( TestCase *test ) |
---|
| 53 | : TestCaseDecorator( test ) |
---|
| 54 | { |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /*! \brief Checks that the expected exception is thrown by the decorated test. |
---|
| 58 | * is thrown. |
---|
| 59 | * |
---|
| 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 |
---|
| 63 | * needed. |
---|
| 64 | */ |
---|
| 65 | void runTest() |
---|
| 66 | { |
---|
| 67 | try |
---|
| 68 | { |
---|
| 69 | TestCaseDecorator::runTest(); |
---|
| 70 | } |
---|
| 71 | catch ( ExpectedExceptionType &e ) |
---|
| 72 | { |
---|
[881] | 73 | #if defined __EXCEPTIONS // lol begin |
---|
[876] | 74 | checkException( e ); |
---|
[881] | 75 | #endif // lol end |
---|
[876] | 76 | return; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | // Moved outside the try{} statement to handle the case where the |
---|
| 80 | // expected exception type is Exception (expecting assertion failure). |
---|
| 81 | #if CPPUNIT_USE_TYPEINFO_NAME |
---|
| 82 | throw Exception( Message( |
---|
| 83 | "expected exception not thrown", |
---|
| 84 | "Expected exception type: " + |
---|
| 85 | TypeInfoHelper::getClassName( |
---|
| 86 | typeid( ExpectedExceptionType ) ) ) ); |
---|
| 87 | #else |
---|
| 88 | throw Exception( Message("expected exception not thrown") ); |
---|
| 89 | #endif |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | private: |
---|
| 93 | /*! \brief Called when the exception is caught. |
---|
| 94 | * |
---|
| 95 | * Should be overriden to check the exception. |
---|
| 96 | */ |
---|
| 97 | virtual void checkException( ExpectedExceptionType & ) |
---|
| 98 | { |
---|
| 99 | } |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | CPPUNIT_NS_END |
---|
| 104 | |
---|
| 105 | #endif // CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H |
---|
| 106 | |
---|