1 #ifndef __EXCEPTIONTEST_H
2 #define __EXCEPTIONTEST_H
4 #include <cxxtest/TestSuite.h>
6 //
7 // This test suite demonstrates the use of TS_ASSERT_THROWS
8 //
10 class ExceptionTest : public CxxTest::TestSuite
11 {
12 public:
13 void testAssertion( void )
14 {
15 // This assert passes, since throwThis() throws (Number)
16 TS_ASSERT_THROWS( throwThis(3), const Number & );
17 // This assert passes, since throwThis() throws something
18 TS_ASSERT_THROWS_ANYTHING( throwThis(-30) );
19 // This assert fails, since throwThis() doesn't throw char *
20 TS_ASSERT_THROWS( throwThis(5), const char * );
21 // This assert fails since goodFunction() throws nothing
22 TS_ASSERT_THROWS_ANYTHING( goodFunction(1) );
23 // The regular TS_ASSERT macros will catch unhandled exceptions
24 TS_ASSERT_EQUALS( throwThis(3), 333 );
25 // You can assert that a function throws nothing
26 TS_ASSERT_THROWS_NOTHING( throwThis(-1) );
27 // If you want to catch the exceptions yourself, use the ETS_ marcos
28 try {
29 ETS_ASSERT_EQUALS( throwThis(3), 333 );
30 } catch( const Number & ) {
31 TS_FAIL( "throwThis(3) failed" );
32 }
33 }
35 private:
36 void goodFunction( int )
37 {
38 }
40 class Number
41 {
42 public:
43 Number( int ) {}
44 };
46 int throwThis( int i )
47 {
48 throw Number( i );
49 }
50 };
52 #endif // __EXCEPTIONTEST_H