|
1 | 1 | #ifndef UNITTEST_CHECKMACROS_H
|
2 | 2 | #define UNITTEST_CHECKMACROS_H
|
3 | 3 |
|
| 4 | +#include "ExceptionMacros.h" |
4 | 5 | #include "Checks.h"
|
5 | 6 | #include "AssertException.h"
|
6 | 7 | #include "MemoryOutStream.h"
|
|
34 | 35 | #define CHECK(value) \
|
35 | 36 | do \
|
36 | 37 | { \
|
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 | + }) \ |
45 | 48 | } while (0)
|
46 | 49 |
|
47 | 50 | #define CHECK_EQUAL(expected, actual) \
|
48 | 51 | do \
|
49 | 52 | { \
|
50 |
| - try { \ |
| 53 | + UT_TRY \ |
| 54 | + ({ \ |
51 | 55 | UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
|
52 |
| - } \ |
53 |
| - catch (...) { \ |
| 56 | + }) \ |
| 57 | + UT_CATCH_ALL \ |
| 58 | + ({ \ |
54 | 59 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
|
55 | 60 | "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
|
56 |
| - } \ |
| 61 | + }) \ |
57 | 62 | } while (0)
|
58 | 63 |
|
59 | 64 | #define CHECK_CLOSE(expected, actual, tolerance) \
|
60 | 65 | do \
|
61 | 66 | { \
|
62 |
| - try { \ |
| 67 | + UT_TRY \ |
| 68 | + ({ \ |
63 | 69 | UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
|
64 |
| - } \ |
65 |
| - catch (...) { \ |
| 70 | + }) \ |
| 71 | + UT_CATCH_ALL \ |
| 72 | + ({ \ |
66 | 73 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
|
67 | 74 | "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \
|
68 |
| - } \ |
| 75 | + }) \ |
69 | 76 | } while (0)
|
70 | 77 |
|
71 | 78 | #define CHECK_ARRAY_EQUAL(expected, actual, count) \
|
72 | 79 | do \
|
73 | 80 | { \
|
74 |
| - try { \ |
| 81 | + UT_TRY \ |
| 82 | + ({ \ |
75 | 83 | UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
|
76 |
| - } \ |
77 |
| - catch (...) { \ |
| 84 | + }) \ |
| 85 | + UT_CATCH_ALL \ |
| 86 | + ({ \ |
78 | 87 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
|
79 | 88 | "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \
|
80 |
| - } \ |
| 89 | + }) \ |
81 | 90 | } while (0)
|
82 | 91 |
|
83 | 92 | #define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \
|
84 | 93 | do \
|
85 | 94 | { \
|
86 |
| - try { \ |
| 95 | + UT_TRY \ |
| 96 | + ({ \ |
87 | 97 | UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
|
88 |
| - } \ |
89 |
| - catch (...) { \ |
| 98 | + }) \ |
| 99 | + UT_CATCH_ALL \ |
| 100 | + ({ \ |
90 | 101 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
|
91 | 102 | "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
|
92 |
| - } \ |
| 103 | + }) \ |
93 | 104 | } while (0)
|
94 | 105 |
|
95 | 106 | #define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \
|
96 | 107 | do \
|
97 | 108 | { \
|
98 |
| - try { \ |
| 109 | + UT_TRY \ |
| 110 | + ({ \ |
99 | 111 | 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 | + ({ \ |
102 | 115 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
|
103 | 116 | "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
|
104 |
| - } \ |
| 117 | + }) \ |
105 | 118 | } while (0)
|
106 | 119 |
|
107 | 120 |
|
| 121 | +// CHECK_THROW only exists when UNITTEST_USE_EXCEPTIONS is defined (see Config.h) |
| 122 | +#ifdef UNITTEST_USE_EXCEPTIONS |
108 | 123 | #define CHECK_THROW(expression, ExpectedExceptionType) \
|
109 | 124 | do \
|
110 | 125 | { \
|
111 | 126 | bool caught_ = false; \
|
112 | 127 | try { expression; } \
|
113 | 128 | catch (ExpectedExceptionType const&) { caught_ = true; } \
|
114 |
| - catch (...) {} \ |
| 129 | + catch (...) {} \ |
115 | 130 | 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"); \ |
117 | 132 | } while(0)
|
| 133 | +#endif |
| 134 | + |
118 | 135 |
|
119 | 136 | #define CHECK_ASSERT(expression) \
|
120 | 137 | CHECK_THROW(expression, UnitTest::AssertException);
|
|
0 commit comments