1 //===- unittests/ASTMatchers/GTestMatchersTest.cpp - GTest matcher unit tests //
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "ASTMatchersTest.h"
10 #include "clang/ASTMatchers/ASTMatchers.h"
11 #include "clang/ASTMatchers/GtestMatchers.h"
12
13 namespace clang {
14 namespace ast_matchers {
15
16 constexpr llvm::StringLiteral GtestMockDecls = R"cc(
17 static int testerr;
18
19 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
20 switch (0) \
21 case 0: \
22 default: // NOLINT
23
24 #define GTEST_NONFATAL_FAILURE_(code) testerr = code
25
26 #define GTEST_FATAL_FAILURE_(code) testerr = code
27
28 #define GTEST_ASSERT_(expression, on_failure) \
29 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
30 if (const int gtest_ar = (expression)) \
31 ; \
32 else \
33 on_failure(gtest_ar)
34
35 // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT2.
36 // Don't use this in your code.
37 #define GTEST_PRED_FORMAT2_(pred_format, v1, v2, on_failure) \
38 GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2), on_failure)
39
40 #define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \
41 GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_FATAL_FAILURE_)
42 #define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \
43 GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_)
44
45 #define EXPECT_EQ(val1, val2) \
46 EXPECT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)
47 #define EXPECT_NE(val1, val2) \
48 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
49 #define EXPECT_GE(val1, val2) \
50 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
51 #define EXPECT_GT(val1, val2) \
52 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
53 #define EXPECT_LE(val1, val2) \
54 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
55 #define EXPECT_LT(val1, val2) \
56 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
57
58 #define ASSERT_EQ(val1, val2) \
59 ASSERT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)
60 #define ASSERT_NE(val1, val2) \
61 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
62
63 namespace testing {
64 namespace internal {
65 class EqHelper {
66 public:
67 // This templatized version is for the general case.
68 template <typename T1, typename T2>
69 static int Compare(const char* lhs_expression, const char* rhs_expression,
70 const T1& lhs, const T2& rhs) {
71 return 0;
72 }
73 };
74 template <typename T1, typename T2>
75 int CmpHelperNE(const char* expr1, const char* expr2, const T1& val1,
76 const T2& val2) {
77 return 0;
78 }
79 template <typename T1, typename T2>
80 int CmpHelperGE(const char* expr1, const char* expr2, const T1& val1,
81 const T2& val2) {
82 return 0;
83 }
84 template <typename T1, typename T2>
85 int CmpHelperGT(const char* expr1, const char* expr2, const T1& val1,
86 const T2& val2) {
87 return 0;
88 }
89 template <typename T1, typename T2>
90 int CmpHelperLE(const char* expr1, const char* expr2, const T1& val1,
91 const T2& val2) {
92 return 0;
93 }
94 template <typename T1, typename T2>
95 int CmpHelperLT(const char* expr1, const char* expr2, const T1& val1,
96 const T2& val2) {
97 return 0;
98 }
99 } // namespace internal
100 } // namespace testing
101 )cc";
102
wrapGtest(llvm::StringRef Input)103 static std::string wrapGtest(llvm::StringRef Input) {
104 return (GtestMockDecls + Input).str();
105 }
106
TEST(GtestAssertTest,ShouldMatchAssert)107 TEST(GtestAssertTest, ShouldMatchAssert) {
108 std::string Input = R"cc(
109 void Test() { ASSERT_EQ(1010, 4321); }
110 )cc";
111 EXPECT_TRUE(matches(wrapGtest(Input),
112 gtestAssert(GtestCmp::Eq, integerLiteral(equals(1010)),
113 integerLiteral(equals(4321)))));
114 }
115
TEST(GtestAssertTest,ShouldNotMatchExpect)116 TEST(GtestAssertTest, ShouldNotMatchExpect) {
117 std::string Input = R"cc(
118 void Test() { EXPECT_EQ(2, 3); }
119 )cc";
120 EXPECT_TRUE(
121 notMatches(wrapGtest(Input), gtestAssert(GtestCmp::Eq, expr(), expr())));
122 }
123
TEST(GtestAssertTest,ShouldMatchNestedAssert)124 TEST(GtestAssertTest, ShouldMatchNestedAssert) {
125 std::string Input = R"cc(
126 #define WRAPPER(a, b) ASSERT_EQ(a, b)
127 void Test() { WRAPPER(2, 3); }
128 )cc";
129 EXPECT_TRUE(
130 matches(wrapGtest(Input), gtestAssert(GtestCmp::Eq, expr(), expr())));
131 }
132
TEST(GtestExpectTest,ShouldMatchExpect)133 TEST(GtestExpectTest, ShouldMatchExpect) {
134 std::string Input = R"cc(
135 void Test() { EXPECT_EQ(1010, 4321); }
136 )cc";
137 EXPECT_TRUE(matches(wrapGtest(Input),
138 gtestExpect(GtestCmp::Eq, integerLiteral(equals(1010)),
139 integerLiteral(equals(4321)))));
140 }
141
TEST(GtestExpectTest,ShouldNotMatchAssert)142 TEST(GtestExpectTest, ShouldNotMatchAssert) {
143 std::string Input = R"cc(
144 void Test() { ASSERT_EQ(2, 3); }
145 )cc";
146 EXPECT_TRUE(
147 notMatches(wrapGtest(Input), gtestExpect(GtestCmp::Eq, expr(), expr())));
148 }
149
TEST(GtestExpectTest,NeShouldMatchExpectNe)150 TEST(GtestExpectTest, NeShouldMatchExpectNe) {
151 std::string Input = R"cc(
152 void Test() { EXPECT_NE(2, 3); }
153 )cc";
154 EXPECT_TRUE(
155 matches(wrapGtest(Input), gtestExpect(GtestCmp::Ne, expr(), expr())));
156 }
157
TEST(GtestExpectTest,LeShouldMatchExpectLe)158 TEST(GtestExpectTest, LeShouldMatchExpectLe) {
159 std::string Input = R"cc(
160 void Test() { EXPECT_LE(2, 3); }
161 )cc";
162 EXPECT_TRUE(
163 matches(wrapGtest(Input), gtestExpect(GtestCmp::Le, expr(), expr())));
164 }
165
TEST(GtestExpectTest,LtShouldMatchExpectLt)166 TEST(GtestExpectTest, LtShouldMatchExpectLt) {
167 std::string Input = R"cc(
168 void Test() { EXPECT_LT(2, 3); }
169 )cc";
170 EXPECT_TRUE(
171 matches(wrapGtest(Input), gtestExpect(GtestCmp::Lt, expr(), expr())));
172 }
173
TEST(GtestExpectTest,GeShouldMatchExpectGe)174 TEST(GtestExpectTest, GeShouldMatchExpectGe) {
175 std::string Input = R"cc(
176 void Test() { EXPECT_GE(2, 3); }
177 )cc";
178 EXPECT_TRUE(
179 matches(wrapGtest(Input), gtestExpect(GtestCmp::Ge, expr(), expr())));
180 }
181
TEST(GtestExpectTest,GtShouldMatchExpectGt)182 TEST(GtestExpectTest, GtShouldMatchExpectGt) {
183 std::string Input = R"cc(
184 void Test() { EXPECT_GT(2, 3); }
185 )cc";
186 EXPECT_TRUE(
187 matches(wrapGtest(Input), gtestExpect(GtestCmp::Gt, expr(), expr())));
188 }
189
190 } // end namespace ast_matchers
191 } // end namespace clang
192