File: ExceptionTest.h

package info (click to toggle)
cxxtest 4.4%2Bgit171022-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 2,836 kB
  • sloc: cpp: 9,567; python: 9,553; xml: 435; sh: 258; ruby: 229; makefile: 86; ansic: 68; perl: 16
file content (49 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download | duplicates (13)
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
#ifndef __EXCEPTIONTEST_H
#define __EXCEPTIONTEST_H

#include <cxxtest/TestSuite.h>

//
// This test suite demonstrates the use of TS_ASSERT_THROWS
//

class ExceptionTest : public CxxTest::TestSuite {
public:
    void testAssertion(void) {
        // This assert passes, since throwThis() throws (Number)
        TS_ASSERT_THROWS(throwThis(3), const Number &);
        // This assert passes, since throwThis() throws something
        TS_ASSERT_THROWS_ANYTHING(throwThis(-30));
        // This assert fails, since throwThis() doesn't throw char *
        TS_ASSERT_THROWS(throwThis(5), const char *);
        // This assert fails since goodFunction() throws nothing
        TS_ASSERT_THROWS_ANYTHING(goodFunction(1));
        // The regular TS_ASSERT macros will catch unhandled exceptions
        TS_ASSERT_EQUALS(throwThis(3), 333);
        // You can assert that a function throws nothing
        TS_ASSERT_THROWS_NOTHING(throwThis(-1));
        // This assert fails, since throwThis() throws (Number)
        TS_ASSERT_THROWS(throwThis(3), std::exception&);
        // If you want to catch the exceptions yourself, use the ETS_ marcos
        try {
            ETS_ASSERT_EQUALS(throwThis(3), 333);
        } catch (const Number &) {
            TS_FAIL("throwThis(3) failed");
        }
    }

private:
    void goodFunction(int) {
    }

    class Number {
    public:
        Number(int) {}
    };

    int throwThis(int i) {
        throw Number(i);
    }
};

#endif // __EXCEPTIONTEST_H