forked from five6516/doctest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertion_macros.cpp
193 lines (167 loc) · 5.99 KB
/
assertion_macros.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <doctest/doctest.h>
#include "header.h"
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
#include <stdexcept>
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
TEST_CASE("normal macros") {
int a = 5;
int b = 5;
CHECK(throw_if(true, std::runtime_error("whops!")) == 42);
CHECK_FALSE(!(a == b));
REQUIRE(a == b);
CHECK_EQ(a, b);
CHECK(doctest::Approx(0.1000001) == 0.1000002);
CHECK(doctest::Approx(0.502) == 0.501);
}
TEST_CASE("expressions should be evaluated only once") {
int a = 5;
REQUIRE(++a == 6);
REQUIRE_EQ(++a, 7);
}
TEST_CASE("exceptions-related macros") {
CHECK_THROWS(throw_if(true, 0));
CHECK_THROWS(throw_if(false, 0)); // fails
CHECK_THROWS_AS(throw_if(true, 0), int);
CHECK_THROWS_AS(throw_if(true, 0), char); // fails
CHECK_THROWS_AS(throw_if(false, 0), int); // fails
CHECK_THROWS_WITH(throw_if(true, "whops!"), "whops! no match!"); // fails
CHECK_THROWS_WITH_AS(throw_if(true, "whops!"), "whops! no match!", bool); // fails
CHECK_THROWS_WITH_AS(throw_if(true, "whops!"), "whops!", int); // fails
CHECK_NOTHROW(throw_if(true, 0)); // fails
CHECK_NOTHROW(throw_if(false, 0));
}
TEST_CASE("exceptions-related macros for std::exception") {
CHECK_THROWS(throw_if(false, 0));
CHECK_THROWS_AS(throw_if(false, std::runtime_error("whops!")), std::exception);
CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), const std::exception&);
CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), int);
CHECK_THROWS_WITH(throw_if(false, ""), "whops!");
REQUIRE_NOTHROW(throw_if(true, std::runtime_error("whops!")));
}
// =================================================================================================
// == TESTING (ALMOST) ALL ASSERTS THAT THEY ACT ACCORDINGLY - not interesting examples...
// =================================================================================================
TEST_CASE("WARN level of asserts don't fail the test case") {
WARN(0);
WARN_FALSE(1);
WARN_THROWS(throw_if(false, 0));
WARN_THROWS_WITH(throw_if(true, ""), "whops!");
WARN_THROWS_WITH(throw_if(false, ""), "whops!");
WARN_THROWS_AS(throw_if(false, 0), bool);
WARN_THROWS_AS(throw_if(true, 0), bool);
WARN_THROWS_WITH_AS(throw_if(false, ""), "whops!", int);
WARN_THROWS_WITH_AS(throw_if(true, ""), "whops!", int);
WARN_NOTHROW(throw_if(true, 0));
WARN_EQ(1, 0);
WARN_UNARY(0);
WARN_UNARY_FALSE(1);
}
TEST_CASE("CHECK level of asserts fail the test case but don't abort it") {
CHECK(0);
CHECK_FALSE(1);
CHECK_THROWS(throw_if(false, 0));
CHECK_THROWS_AS(throw_if(false, 0), bool);
CHECK_THROWS_AS(throw_if(true, 0), bool);
CHECK_THROWS_WITH(throw_if(true, 0), "unrecognized");
CHECK_THROWS_WITH_AS(throw_if(true, 0), "unrecognized", int);
CHECK_NOTHROW(throw_if(true, 0));
CHECK_EQ(1, 0);
CHECK_UNARY(0);
CHECK_UNARY_FALSE(1);
MESSAGE("reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 1") {
REQUIRE(0);
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 2") {
REQUIRE_FALSE(1);
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 3") {
REQUIRE_THROWS(throw_if(false, 0));
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 4") {
REQUIRE_THROWS_AS(throw_if(false, 0), bool);
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 5") {
REQUIRE_THROWS_AS(throw_if(true, 0), bool);
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 6") {
REQUIRE_THROWS_WITH(throw_if(false, ""), "whops!");
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 7") {
REQUIRE_THROWS_WITH(throw_if(true, ""), "whops!");
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 8") {
REQUIRE_THROWS_WITH_AS(throw_if(false, ""), "whops!", bool);
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 9") {
REQUIRE_THROWS_WITH_AS(throw_if(true, ""), "whops!", bool);
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 10") {
REQUIRE_NOTHROW(throw_if(true, 0));
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 11") {
REQUIRE_EQ(1, 0);
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 12") {
REQUIRE_UNARY(0);
MESSAGE("should not be reached!");
}
TEST_CASE("REQUIRE level of asserts fail and abort the test case - 13") {
REQUIRE_UNARY_FALSE(1);
MESSAGE("should not be reached!");
}
TEST_CASE("all binary assertions") {
WARN_EQ(1, 1);
CHECK_EQ(1, 1);
REQUIRE_EQ(1, 1);
WARN_NE(1, 0);
CHECK_NE(1, 0);
REQUIRE_NE(1, 0);
WARN_GT(1, 0);
CHECK_GT(1, 0);
REQUIRE_GT(1, 0);
WARN_LT(0, 1);
CHECK_LT(0, 1);
REQUIRE_LT(0, 1);
WARN_GE(1, 1);
CHECK_GE(1, 1);
REQUIRE_GE(1, 1);
WARN_LE(1, 1);
CHECK_LE(1, 1);
REQUIRE_LE(1, 1);
WARN_UNARY(1);
CHECK_UNARY(1);
REQUIRE_UNARY(1);
WARN_UNARY_FALSE(0);
CHECK_UNARY_FALSE(0);
REQUIRE_UNARY_FALSE(0);
}
static void someAssertsInFunction() {
int a = 5;
int b = 5;
CHECK(a == b);
CHECK_FALSE(a != b);
CHECK_THROWS(throw_if(true, 0));
CHECK_THROWS_AS(throw_if(true, 0), int);
CHECK_THROWS_WITH(throw_if(true, false), "unknown exception");
CHECK_THROWS_WITH_AS(throw_if(true, false), "unknown exception", int);
CHECK_NOTHROW(throw_if(false, 0));
CHECK_EQ(a, b);
CHECK_UNARY(a == b);
CHECK_UNARY_FALSE(a != b);
}
TEST_CASE("some asserts used in a function called by a test case") {
someAssertsInFunction();
}