source: trunk/contrib/cppunit-1.12.2/include/cppunit/extensions/ExceptionTestCaseDecorator.h @ 881

Last change on this file since 881 was 881, checked in by sam, 9 years ago

contrib: rebuild the Win32 CppUnit under Linux and fix the includes.

Apparently g++ 4.5.2 on native mingw32 inserts references to Unwind_Resume
when exceptions are supported. This function is not present in the g++ 4.4
cross-compiler found in Debian, leading to linker errors when cross-
compiling. So we use the latter to build CppUnit itself.

Another problem arises with g++ 4.4, which is the incomplete stripping of
exceptions when -fno-exceptions is used. Since ExceptionTestCaseDecorator.h
has such code, we protect it with #ifdef EXCEPTIONS in the meantime.

File size: 2.8 KB
Line 
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
8CPPUNIT_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 */
43template<class ExpectedException>
44class ExceptionTestCaseDecorator : public TestCaseDecorator
45{
46public:
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    {
73#if defined __EXCEPTIONS // lol begin
74      checkException( e );
75#endif // lol end
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
92private:
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
103CPPUNIT_NS_END
104
105#endif // CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
106
Note: See TracBrowser for help on using the repository browser.