Skip to content

Commit 67d4ecb

Browse files
committed
r3 | charles.nicholson | 2010-03-08 22:58:59 -0600 (Mon, 08 Mar 2010) | 1 line
first pass at adding exception macros
1 parent 72c2581 commit 67d4ecb

File tree

6 files changed

+373
-292
lines changed

6 files changed

+373
-292
lines changed

src/CheckMacros.h

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef UNITTEST_CHECKMACROS_H
22
#define UNITTEST_CHECKMACROS_H
33

4+
#include "ExceptionMacros.h"
45
#include "Checks.h"
56
#include "AssertException.h"
67
#include "MemoryOutStream.h"
@@ -34,87 +35,103 @@
3435
#define CHECK(value) \
3536
do \
3637
{ \
37-
try { \
38-
if (!UnitTest::Check(value)) \
39-
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \
40-
} \
41-
catch (...) { \
42-
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
43-
"Unhandled exception in CHECK(" #value ")"); \
44-
} \
38+
UT_TRY \
39+
({ \
40+
if (!UnitTest::Check(value)) \
41+
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \
42+
}) \
43+
UT_CATCH_ALL \
44+
({ \
45+
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
46+
"Unhandled exception in CHECK(" #value ")"); \
47+
}) \
4548
} while (0)
4649

4750
#define CHECK_EQUAL(expected, actual) \
4851
do \
4952
{ \
50-
try { \
53+
UT_TRY \
54+
({ \
5155
UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
52-
} \
53-
catch (...) { \
56+
}) \
57+
UT_CATCH_ALL \
58+
({ \
5459
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
5560
"Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
56-
} \
61+
}) \
5762
} while (0)
5863

5964
#define CHECK_CLOSE(expected, actual, tolerance) \
6065
do \
6166
{ \
62-
try { \
67+
UT_TRY \
68+
({ \
6369
UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
64-
} \
65-
catch (...) { \
70+
}) \
71+
UT_CATCH_ALL \
72+
({ \
6673
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
6774
"Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \
68-
} \
75+
}) \
6976
} while (0)
7077

7178
#define CHECK_ARRAY_EQUAL(expected, actual, count) \
7279
do \
7380
{ \
74-
try { \
81+
UT_TRY \
82+
({ \
7583
UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
76-
} \
77-
catch (...) { \
84+
}) \
85+
UT_CATCH_ALL \
86+
({ \
7887
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
7988
"Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \
80-
} \
89+
}) \
8190
} while (0)
8291

8392
#define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \
8493
do \
8594
{ \
86-
try { \
95+
UT_TRY \
96+
({ \
8797
UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
88-
} \
89-
catch (...) { \
98+
}) \
99+
UT_CATCH_ALL \
100+
({ \
90101
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
91102
"Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
92-
} \
103+
}) \
93104
} while (0)
94105

95106
#define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \
96107
do \
97108
{ \
98-
try { \
109+
UT_TRY \
110+
({ \
99111
UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
100-
} \
101-
catch (...) { \
112+
}) \
113+
UT_CATCH_ALL \
114+
({ \
102115
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
103116
"Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
104-
} \
117+
}) \
105118
} while (0)
106119

107120

121+
// CHECK_THROW only exists when UNITTEST_USE_EXCEPTIONS is defined (see Config.h)
122+
#ifdef UNITTEST_USE_EXCEPTIONS
108123
#define CHECK_THROW(expression, ExpectedExceptionType) \
109124
do \
110125
{ \
111126
bool caught_ = false; \
112127
try { expression; } \
113128
catch (ExpectedExceptionType const&) { caught_ = true; } \
114-
catch (...) {} \
129+
catch (...) {} \
115130
if (!caught_) \
116-
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \
131+
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \
117132
} while(0)
133+
#endif
134+
118135

119136
#define CHECK_ASSERT(expression) \
120137
CHECK_THROW(expression, UnitTest::AssertException);

src/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@
2828

2929
//#define UNITTEST_USE_CUSTOM_STREAMS
3030

31+
#define UNITTEST_USE_EXCEPTIONS
32+
3133
#endif

src/ExceptionMacros.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef UNITTEST_EXCEPTIONMACROS_H
2+
#define UNITTEST_EXCEPTIONMACROS_H
3+
4+
#include "Config.h"
5+
6+
#ifdef UNITTEST_USE_EXCEPTIONS
7+
#define UT_TRY(x) try x
8+
#define UT_THROW(x) throw x
9+
#define UT_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody
10+
#define UT_CATCH_ALL(CatchBody) catch(...) CatchBody
11+
#else
12+
#define UT_TRY(x) x
13+
#define UT_THROW(x)
14+
#define UT_CATCH(ExceptionType, ExceptionName, CatchBody)
15+
#define UT_CATCH_ALL(CatchBody)
16+
#endif
17+
18+
#endif

0 commit comments

Comments
 (0)