1 //===- unittests/ASTMatchers/GTestMatchersTest.cpp - GTest matcher unit tests //
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
7 //===----------------------------------------------------------------------===//
9 #include "ASTMatchersTest.h"
10 #include "clang/ASTMatchers/ASTMatchers.h"
11 #include "clang/ASTMatchers/GtestMatchers.h"
14 namespace ast_matchers
{
16 constexpr llvm::StringLiteral GtestMockDecls
= R
"cc(
19 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
24 #define GTEST_NONFATAL_FAILURE_(code) testerr = code
26 #define GTEST_FATAL_FAILURE_(code) testerr = code
28 #define GTEST_ASSERT_(expression, on_failure) \
29 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
30 if (const int gtest_ar = (expression)) \
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)
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_)
45 #define GTEST_PRED_FORMAT1_(pred_format, v1, on_failure) \
46 GTEST_ASSERT_(pred_format(#v1, v1), on_failure)
48 #define EXPECT_PRED_FORMAT1(pred_format, v1) \
49 GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_NONFATAL_FAILURE_)
50 #define ASSERT_PRED_FORMAT1(pred_format, v1) \
51 GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_FATAL_FAILURE_)
53 #define EXPECT_EQ(val1, val2) \
54 EXPECT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)
55 #define EXPECT_NE(val1, val2) \
56 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
57 #define EXPECT_GE(val1, val2) \
58 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
59 #define EXPECT_GT(val1, val2) \
60 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
61 #define EXPECT_LE(val1, val2) \
62 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
63 #define EXPECT_LT(val1, val2) \
64 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
66 #define ASSERT_THAT(value, matcher) \
67 ASSERT_PRED_FORMAT1( \
68 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
69 #define EXPECT_THAT(value, matcher) \
70 EXPECT_PRED_FORMAT1( \
71 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
73 #define ASSERT_EQ(val1, val2) \
74 ASSERT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)
75 #define ASSERT_NE(val1, val2) \
76 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
78 #define GMOCK_ON_CALL_IMPL_(mock_expr, Setter, call) \
79 ((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), \
81 .Setter(nullptr, 0, #mock_expr, #call)
83 #define ON_CALL(obj, call) \
84 GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call)
86 #define EXPECT_CALL(obj, call) \
87 GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call)
93 // This templatized version is for the general case.
94 template <typename T1, typename T2>
95 static int Compare(const char* lhs_expression, const char* rhs_expression,
96 const T1& lhs, const T2& rhs) {
100 template <typename T1, typename T2>
101 int CmpHelperNE(const char* expr1, const char* expr2, const T1& val1,
105 template <typename T1, typename T2>
106 int CmpHelperGE(const char* expr1, const char* expr2, const T1& val1,
110 template <typename T1, typename T2>
111 int CmpHelperGT(const char* expr1, const char* expr2, const T1& val1,
115 template <typename T1, typename T2>
116 int CmpHelperLE(const char* expr1, const char* expr2, const T1& val1,
120 template <typename T1, typename T2>
121 int CmpHelperLT(const char* expr1, const char* expr2, const T1& val1,
126 // For implementing ASSERT_THAT() and EXPECT_THAT(). The template
127 // argument M must be a type that can be converted to a matcher.
128 template <typename M>
129 class PredicateFormatterFromMatcher {
131 explicit PredicateFormatterFromMatcher(M m) : matcher_(m) {}
133 // This template () operator allows a PredicateFormatterFromMatcher
134 // object to act as a predicate-formatter suitable for using with
135 // Google Test's EXPECT_PRED_FORMAT1() macro.
136 template <typename T>
137 int operator()(const char* value_text, const T& x) const {
145 template <typename M>
146 inline PredicateFormatterFromMatcher<M> MakePredicateFormatterFromMatcher(
148 return PredicateFormatterFromMatcher<M>(matcher);
151 bool GetWithoutMatchers() { return false; }
153 template <typename F>
158 bool InternalDefaultActionSetAt(
159 const char* file, int line, const char* obj, const char* call) {
163 bool InternalExpectedAt(
164 const char* file, int line, const char* obj, const char* call) {
168 MockSpec<F> operator()(bool, void*) {
173 } // namespace internal
175 template <typename T>
179 template <typename T>
184 } // namespace testing
189 testing::internal::MockSpec<int> gmock_TwoArgsMethod(int, int) {
190 return testing::internal::MockSpec<int>();
192 testing::internal::MockSpec<int> gmock_TwoArgsMethod(bool, void*) {
193 return testing::internal::MockSpec<int>();
198 static std::string
wrapGtest(llvm::StringRef Input
) {
199 return (GtestMockDecls
+ Input
).str();
202 TEST(GtestAssertTest
, ShouldMatchAssert
) {
203 std::string Input
= R
"cc(
204 void Test() { ASSERT_EQ(1010, 4321); }
206 EXPECT_TRUE(matches(wrapGtest(Input
),
207 gtestAssert(GtestCmp::Eq
, integerLiteral(equals(1010)),
208 integerLiteral(equals(4321)))));
211 TEST(GtestAssertTest
, ShouldNotMatchExpect
) {
212 std::string Input
= R
"cc(
213 void Test() { EXPECT_EQ(2, 3); }
216 notMatches(wrapGtest(Input
), gtestAssert(GtestCmp::Eq
, expr(), expr())));
219 TEST(GtestAssertTest
, ShouldMatchNestedAssert
) {
220 std::string Input
= R
"cc(
221 #define WRAPPER(a, b) ASSERT_EQ(a, b)
222 void Test() { WRAPPER(2, 3); }
225 matches(wrapGtest(Input
), gtestAssert(GtestCmp::Eq
, expr(), expr())));
228 TEST(GtestExpectTest
, ShouldMatchExpect
) {
229 std::string Input
= R
"cc(
230 void Test() { EXPECT_EQ(1010, 4321); }
232 EXPECT_TRUE(matches(wrapGtest(Input
),
233 gtestExpect(GtestCmp::Eq
, integerLiteral(equals(1010)),
234 integerLiteral(equals(4321)))));
237 TEST(GtestExpectTest
, ShouldNotMatchAssert
) {
238 std::string Input
= R
"cc(
239 void Test() { ASSERT_EQ(2, 3); }
242 notMatches(wrapGtest(Input
), gtestExpect(GtestCmp::Eq
, expr(), expr())));
245 TEST(GtestExpectTest
, NeShouldMatchExpectNe
) {
246 std::string Input
= R
"cc(
247 void Test() { EXPECT_NE(2, 3); }
250 matches(wrapGtest(Input
), gtestExpect(GtestCmp::Ne
, expr(), expr())));
253 TEST(GtestExpectTest
, LeShouldMatchExpectLe
) {
254 std::string Input
= R
"cc(
255 void Test() { EXPECT_LE(2, 3); }
258 matches(wrapGtest(Input
), gtestExpect(GtestCmp::Le
, expr(), expr())));
261 TEST(GtestExpectTest
, LtShouldMatchExpectLt
) {
262 std::string Input
= R
"cc(
263 void Test() { EXPECT_LT(2, 3); }
266 matches(wrapGtest(Input
), gtestExpect(GtestCmp::Lt
, expr(), expr())));
269 TEST(GtestExpectTest
, GeShouldMatchExpectGe
) {
270 std::string Input
= R
"cc(
271 void Test() { EXPECT_GE(2, 3); }
274 matches(wrapGtest(Input
), gtestExpect(GtestCmp::Ge
, expr(), expr())));
277 TEST(GtestExpectTest
, GtShouldMatchExpectGt
) {
278 std::string Input
= R
"cc(
279 void Test() { EXPECT_GT(2, 3); }
282 matches(wrapGtest(Input
), gtestExpect(GtestCmp::Gt
, expr(), expr())));
285 TEST(GtestExpectTest
, ThatShouldMatchAssertThat
) {
286 std::string Input
= R
"cc(
288 void Test() { ASSERT_THAT(2, Eq(2)); }
293 expr(), callExpr(callee(functionDecl(hasName("::testing::Eq")))))));
296 TEST(GtestExpectTest
, ThatShouldMatchExpectThat
) {
297 std::string Input
= R
"cc(
299 void Test() { EXPECT_THAT(2, Eq(2)); }
304 expr(), callExpr(callee(functionDecl(hasName("::testing::Eq")))))));
307 TEST(GtestOnCallTest
, CallShouldMatchOnCallWithoutParams1
) {
308 std::string Input
= R
"cc(
311 ON_CALL(mock, TwoArgsMethod);
314 EXPECT_TRUE(matches(wrapGtest(Input
),
315 gtestOnCall(expr(hasType(cxxRecordDecl(hasName("Mock")))),
316 "TwoArgsMethod", MockArgs::None
)));
319 TEST(GtestOnCallTest
, CallShouldMatchOnCallWithoutParams2
) {
320 std::string Input
= R
"cc(
323 ON_CALL(mock, TwoArgsMethod);
328 gtestOnCall(cxxMemberCallExpr(
329 callee(functionDecl(hasName("gmock_TwoArgsMethod"))))
334 TEST(GtestOnCallTest
, CallShouldMatchOnCallWithParams1
) {
335 std::string Input
= R
"cc(
338 ON_CALL(mock, TwoArgsMethod(1, 2));
341 EXPECT_TRUE(matches(wrapGtest(Input
),
342 gtestOnCall(expr(hasType(cxxRecordDecl(hasName("Mock")))),
343 "TwoArgsMethod", MockArgs::Some
)));
346 TEST(GtestOnCallTest
, CallShouldMatchOnCallWithParams2
) {
347 std::string Input
= R
"cc(
350 ON_CALL(mock, TwoArgsMethod(1, 2));
355 gtestOnCall(cxxMemberCallExpr(
356 callee(functionDecl(hasName("gmock_TwoArgsMethod"))))
361 TEST(GtestExpectCallTest
, CallShouldMatchExpectCallWithoutParams1
) {
362 std::string Input
= R
"cc(
365 EXPECT_CALL(mock, TwoArgsMethod);
369 matches(wrapGtest(Input
),
370 gtestExpectCall(expr(hasType(cxxRecordDecl(hasName("Mock")))),
371 "TwoArgsMethod", MockArgs::None
)));
374 TEST(GtestExpectCallTest
, CallShouldMatchExpectCallWithoutParams2
) {
375 std::string Input
= R
"cc(
378 EXPECT_CALL(mock, TwoArgsMethod);
383 gtestExpectCall(cxxMemberCallExpr(
384 callee(functionDecl(hasName("gmock_TwoArgsMethod"))))
389 TEST(GtestExpectCallTest
, CallShouldMatchExpectCallWithParams1
) {
390 std::string Input
= R
"cc(
393 EXPECT_CALL(mock, TwoArgsMethod(1, 2));
397 matches(wrapGtest(Input
),
398 gtestExpectCall(expr(hasType(cxxRecordDecl(hasName("Mock")))),
399 "TwoArgsMethod", MockArgs::Some
)));
402 TEST(GtestExpectCallTest
, CallShouldMatchExpectCallWithParams2
) {
403 std::string Input
= R
"cc(
406 EXPECT_CALL(mock, TwoArgsMethod(1, 2));
411 gtestExpectCall(cxxMemberCallExpr(
412 callee(functionDecl(hasName("gmock_TwoArgsMethod"))))
417 } // end namespace ast_matchers
418 } // end namespace clang