Imported Upstream version 2.81
[platform/upstream/libbullet.git] / UnitTests / cppunit / include / cppunit / extensions / ExceptionTestCaseDecorator.h
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     {
73       checkException( e );
74       return;
75     }
76
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 ) ) ) );
85 #else
86       throw Exception( Message("expected exception not thrown") );
87 #endif
88   }
89
90 private:
91   /*! \brief Called when the exception is caught.
92    *
93    * Should be overriden to check the exception.
94    */
95   virtual void checkException( ExpectedExceptionType & )
96   {
97   }
98 };
99
100
101 CPPUNIT_NS_END
102
103 #endif // CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
104