-
Notifications
You must be signed in to change notification settings - Fork 412
/
expect_throws_message.h
59 lines (46 loc) · 2.08 KB
/
expect_throws_message.h
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
#ifndef FCL_EXPECT_THROWS_MESSAGE_H
#define FCL_EXPECT_THROWS_MESSAGE_H
#include <regex>
#include <string>
#define FCL_EXPECT_THROWS_MESSAGE_HELPER(expression, exception, regexp, \
must_throw, fatal_failure) \
try { \
expression; \
if (must_throw) { \
if (fatal_failure) { \
GTEST_FATAL_FAILURE_("\t" #expression " failed to throw " #exception); \
} else { \
GTEST_NONFATAL_FAILURE_("\t" #expression " failed to throw " #exception);\
} \
} \
} catch (const exception& err) { \
auto matcher = [](const char* s, const std::string& re) { \
return std::regex_match(s, std::regex(re)); }; \
if (fatal_failure) { \
ASSERT_PRED2(matcher, err.what(), regexp); \
} else { \
EXPECT_PRED2(matcher, err.what(), regexp); \
} \
}
#define FCL_EXPECT_THROWS_MESSAGE(expression, exception, regexp) \
FCL_EXPECT_THROWS_MESSAGE_HELPER(expression, exception, regexp, \
true /*must_throw*/, false /*non-fatal*/)
#define FCL_ASSERT_THROWS_MESSAGE(expression, exception, regexp) \
FCL_EXPECT_THROWS_MESSAGE_HELPER(expression, exception, regexp, \
true /*must_throw*/, true /*fatal*/)
#ifdef NDEBUG
// Throwing the expected message is optional in this case.
#define FCL_EXPECT_THROWS_MESSAGE_IF_DEBUG(expression, exception, regexp) \
FCL_EXPECT_THROWS_MESSAGE_HELPER(expression, exception, regexp, \
false /*optional*/, false /*non-fatal*/)
#define FCL_ASSERT_THROWS_MESSAGE_IF_DEBUG(expression, exception, regexp) \
FCL_EXPECT_THROWS_MESSAGE_HELPER(expression, exception, regexp, \
false /*optional*/, true /*fatal*/)
#else //
// Throwing the expected message is required in this case.
#define FCL_EXPECT_THROWS_MESSAGE_IF_DEBUG(expression, exception, regexp) \
FCL_EXPECT_THROWS_MESSAGE(expression, exception, regexp)
#define FCL_ASSERT_THROWS_MESSAGE_IF_DEBUG(expression, exception, regexp) \
FCL_ASSERT_THROWS_MESSAGE(expression, exception, regexp)
#endif
#endif // FCL_EXPECT_THROWS_MESSAGE_H