File: GtestMatchersTest.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (418 lines) | stat: -rw-r--r-- 12,834 bytes parent folder | download | duplicates (18)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//===- unittests/ASTMatchers/GTestMatchersTest.cpp - GTest matcher unit tests //
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ASTMatchersTest.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/GtestMatchers.h"

namespace clang {
namespace ast_matchers {

constexpr llvm::StringLiteral GtestMockDecls = R"cc(
  static int testerr;

#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
    switch (0)                          \
    case 0:                             \
    default:  // NOLINT

#define GTEST_NONFATAL_FAILURE_(code) testerr = code

#define GTEST_FATAL_FAILURE_(code) testerr = code

#define GTEST_ASSERT_(expression, on_failure) \
    GTEST_AMBIGUOUS_ELSE_BLOCKER_               \
    if (const int gtest_ar = (expression))      \
      ;                                         \
    else                                        \
      on_failure(gtest_ar)

  // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT2.
  // Don't use this in your code.
#define GTEST_PRED_FORMAT2_(pred_format, v1, v2, on_failure) \
    GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2), on_failure)

#define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \
    GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_FATAL_FAILURE_)
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \
    GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_)

#define GTEST_PRED_FORMAT1_(pred_format, v1, on_failure) \
  GTEST_ASSERT_(pred_format(#v1, v1), on_failure)

#define EXPECT_PRED_FORMAT1(pred_format, v1) \
  GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_NONFATAL_FAILURE_)
#define ASSERT_PRED_FORMAT1(pred_format, v1) \
  GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_FATAL_FAILURE_)

#define EXPECT_EQ(val1, val2) \
    EXPECT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)
#define EXPECT_NE(val1, val2) \
    EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
#define EXPECT_GE(val1, val2) \
    EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
#define EXPECT_GT(val1, val2) \
    EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
#define EXPECT_LE(val1, val2) \
    EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
#define EXPECT_LT(val1, val2) \
    EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)

#define ASSERT_THAT(value, matcher) \
  ASSERT_PRED_FORMAT1(              \
      ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
#define EXPECT_THAT(value, matcher) \
  EXPECT_PRED_FORMAT1(              \
      ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)

#define ASSERT_EQ(val1, val2) \
    ASSERT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)
#define ASSERT_NE(val1, val2) \
    ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)

#define GMOCK_ON_CALL_IMPL_(mock_expr, Setter, call)                    \
  ((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), \
                             nullptr)                                   \
      .Setter(nullptr, 0, #mock_expr, #call)

#define ON_CALL(obj, call) \
  GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call)

#define EXPECT_CALL(obj, call) \
  GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call)

  namespace testing {
  namespace internal {
  class EqHelper {
   public:
    // This templatized version is for the general case.
    template <typename T1, typename T2>
    static int Compare(const char* lhs_expression, const char* rhs_expression,
                       const T1& lhs, const T2& rhs) {
      return 0;
    }
  };
  template <typename T1, typename T2>
  int CmpHelperNE(const char* expr1, const char* expr2, const T1& val1,
                  const T2& val2) {
    return 0;
  }
  template <typename T1, typename T2>
  int CmpHelperGE(const char* expr1, const char* expr2, const T1& val1,
                  const T2& val2) {
    return 0;
  }
  template <typename T1, typename T2>
  int CmpHelperGT(const char* expr1, const char* expr2, const T1& val1,
                  const T2& val2) {
    return 0;
  }
  template <typename T1, typename T2>
  int CmpHelperLE(const char* expr1, const char* expr2, const T1& val1,
                  const T2& val2) {
    return 0;
  }
  template <typename T1, typename T2>
  int CmpHelperLT(const char* expr1, const char* expr2, const T1& val1,
                  const T2& val2) {
    return 0;
  }

  // For implementing ASSERT_THAT() and EXPECT_THAT().  The template
  // argument M must be a type that can be converted to a matcher.
  template <typename M>
  class PredicateFormatterFromMatcher {
   public:
    explicit PredicateFormatterFromMatcher(M m) : matcher_(m) {}

    // This template () operator allows a PredicateFormatterFromMatcher
    // object to act as a predicate-formatter suitable for using with
    // Google Test's EXPECT_PRED_FORMAT1() macro.
    template <typename T>
    int operator()(const char* value_text, const T& x) const {
      return 0;
    }

   private:
    const M matcher_;
  };

  template <typename M>
  inline PredicateFormatterFromMatcher<M> MakePredicateFormatterFromMatcher(
      M matcher) {
    return PredicateFormatterFromMatcher<M>(matcher);
  }

  bool GetWithoutMatchers() { return false; }

  template <typename F>
  class MockSpec {
   public:
    MockSpec<F>() {}

    bool InternalDefaultActionSetAt(
        const char* file, int line, const char* obj, const char* call) {
      return false;
    }

    bool InternalExpectedAt(
        const char* file, int line, const char* obj, const char* call) {
      return false;
    }

    MockSpec<F> operator()(bool, void*) {
      return *this;
    }
  };  // class MockSpec

  }  // namespace internal

  template <typename T>
  int StrEq(T val) {
    return 0;
  }
  template <typename T>
  int Eq(T val) {
    return 0;
  }

  }  // namespace testing

  class Mock {
    public:
    Mock() {}
    testing::internal::MockSpec<int> gmock_TwoArgsMethod(int, int) {
      return testing::internal::MockSpec<int>();
    }
    testing::internal::MockSpec<int> gmock_TwoArgsMethod(bool, void*) {
      return testing::internal::MockSpec<int>();
    }
  };  // class Mock
)cc";

static std::string wrapGtest(llvm::StringRef Input) {
  return (GtestMockDecls + Input).str();
}

TEST(GtestAssertTest, ShouldMatchAssert) {
  std::string Input = R"cc(
    void Test() { ASSERT_EQ(1010, 4321); }
  )cc";
  EXPECT_TRUE(matches(wrapGtest(Input),
                      gtestAssert(GtestCmp::Eq, integerLiteral(equals(1010)),
                                  integerLiteral(equals(4321)))));
}

TEST(GtestAssertTest, ShouldNotMatchExpect) {
  std::string Input = R"cc(
    void Test() { EXPECT_EQ(2, 3); }
  )cc";
  EXPECT_TRUE(
      notMatches(wrapGtest(Input), gtestAssert(GtestCmp::Eq, expr(), expr())));
}

TEST(GtestAssertTest, ShouldMatchNestedAssert) {
  std::string Input = R"cc(
    #define WRAPPER(a, b) ASSERT_EQ(a, b)
    void Test() { WRAPPER(2, 3); }
  )cc";
  EXPECT_TRUE(
      matches(wrapGtest(Input), gtestAssert(GtestCmp::Eq, expr(), expr())));
}

TEST(GtestExpectTest, ShouldMatchExpect) {
  std::string Input = R"cc(
    void Test() { EXPECT_EQ(1010, 4321); }
  )cc";
  EXPECT_TRUE(matches(wrapGtest(Input),
                      gtestExpect(GtestCmp::Eq, integerLiteral(equals(1010)),
                                  integerLiteral(equals(4321)))));
}

TEST(GtestExpectTest, ShouldNotMatchAssert) {
  std::string Input = R"cc(
    void Test() { ASSERT_EQ(2, 3); }
  )cc";
  EXPECT_TRUE(
      notMatches(wrapGtest(Input), gtestExpect(GtestCmp::Eq, expr(), expr())));
}

TEST(GtestExpectTest, NeShouldMatchExpectNe) {
  std::string Input = R"cc(
    void Test() { EXPECT_NE(2, 3); }
  )cc";
  EXPECT_TRUE(
      matches(wrapGtest(Input), gtestExpect(GtestCmp::Ne, expr(), expr())));
}

TEST(GtestExpectTest, LeShouldMatchExpectLe) {
  std::string Input = R"cc(
    void Test() { EXPECT_LE(2, 3); }
  )cc";
  EXPECT_TRUE(
      matches(wrapGtest(Input), gtestExpect(GtestCmp::Le, expr(), expr())));
}

TEST(GtestExpectTest, LtShouldMatchExpectLt) {
  std::string Input = R"cc(
    void Test() { EXPECT_LT(2, 3); }
  )cc";
  EXPECT_TRUE(
      matches(wrapGtest(Input), gtestExpect(GtestCmp::Lt, expr(), expr())));
}

TEST(GtestExpectTest, GeShouldMatchExpectGe) {
  std::string Input = R"cc(
    void Test() { EXPECT_GE(2, 3); }
  )cc";
  EXPECT_TRUE(
      matches(wrapGtest(Input), gtestExpect(GtestCmp::Ge, expr(), expr())));
}

TEST(GtestExpectTest, GtShouldMatchExpectGt) {
  std::string Input = R"cc(
    void Test() { EXPECT_GT(2, 3); }
  )cc";
  EXPECT_TRUE(
      matches(wrapGtest(Input), gtestExpect(GtestCmp::Gt, expr(), expr())));
}

TEST(GtestExpectTest, ThatShouldMatchAssertThat) {
  std::string Input = R"cc(
    using ::testing::Eq;
    void Test() { ASSERT_THAT(2, Eq(2)); }
  )cc";
  EXPECT_TRUE(matches(
      wrapGtest(Input),
      gtestAssertThat(
          expr(), callExpr(callee(functionDecl(hasName("::testing::Eq")))))));
}

TEST(GtestExpectTest, ThatShouldMatchExpectThat) {
  std::string Input = R"cc(
    using ::testing::Eq;
    void Test() { EXPECT_THAT(2, Eq(2)); }
  )cc";
  EXPECT_TRUE(matches(
      wrapGtest(Input),
      gtestExpectThat(
          expr(), callExpr(callee(functionDecl(hasName("::testing::Eq")))))));
}

TEST(GtestOnCallTest, CallShouldMatchOnCallWithoutParams1) {
  std::string Input = R"cc(
    void Test() {
      Mock mock;
      ON_CALL(mock, TwoArgsMethod);
    }
  )cc";
  EXPECT_TRUE(matches(wrapGtest(Input),
                      gtestOnCall(expr(hasType(cxxRecordDecl(hasName("Mock")))),
                                  "TwoArgsMethod", MockArgs::None)));
}

TEST(GtestOnCallTest, CallShouldMatchOnCallWithoutParams2) {
  std::string Input = R"cc(
    void Test() {
      Mock mock;
      ON_CALL(mock, TwoArgsMethod);
    }
  )cc";
  EXPECT_TRUE(matches(
      wrapGtest(Input),
      gtestOnCall(cxxMemberCallExpr(
                      callee(functionDecl(hasName("gmock_TwoArgsMethod"))))
                      .bind("mock_call"),
                  MockArgs::None)));
}

TEST(GtestOnCallTest, CallShouldMatchOnCallWithParams1) {
  std::string Input = R"cc(
    void Test() {
      Mock mock;
      ON_CALL(mock, TwoArgsMethod(1, 2));
    }
  )cc";
  EXPECT_TRUE(matches(wrapGtest(Input),
                      gtestOnCall(expr(hasType(cxxRecordDecl(hasName("Mock")))),
                                  "TwoArgsMethod", MockArgs::Some)));
}

TEST(GtestOnCallTest, CallShouldMatchOnCallWithParams2) {
  std::string Input = R"cc(
    void Test() {
      Mock mock;
      ON_CALL(mock, TwoArgsMethod(1, 2));
    }
  )cc";
  EXPECT_TRUE(matches(
      wrapGtest(Input),
      gtestOnCall(cxxMemberCallExpr(
                      callee(functionDecl(hasName("gmock_TwoArgsMethod"))))
                      .bind("mock_call"),
                  MockArgs::Some)));
}

TEST(GtestExpectCallTest, CallShouldMatchExpectCallWithoutParams1) {
  std::string Input = R"cc(
    void Test() {
      Mock mock;
      EXPECT_CALL(mock, TwoArgsMethod);
    }
  )cc";
  EXPECT_TRUE(
      matches(wrapGtest(Input),
              gtestExpectCall(expr(hasType(cxxRecordDecl(hasName("Mock")))),
                              "TwoArgsMethod", MockArgs::None)));
}

TEST(GtestExpectCallTest, CallShouldMatchExpectCallWithoutParams2) {
  std::string Input = R"cc(
    void Test() {
      Mock mock;
      EXPECT_CALL(mock, TwoArgsMethod);
    }
  )cc";
  EXPECT_TRUE(matches(
      wrapGtest(Input),
      gtestExpectCall(cxxMemberCallExpr(
                          callee(functionDecl(hasName("gmock_TwoArgsMethod"))))
                          .bind("mock_call"),
                      MockArgs::None)));
}

TEST(GtestExpectCallTest, CallShouldMatchExpectCallWithParams1) {
  std::string Input = R"cc(
    void Test() {
      Mock mock;
      EXPECT_CALL(mock, TwoArgsMethod(1, 2));
    }
  )cc";
  EXPECT_TRUE(
      matches(wrapGtest(Input),
              gtestExpectCall(expr(hasType(cxxRecordDecl(hasName("Mock")))),
                              "TwoArgsMethod", MockArgs::Some)));
}

TEST(GtestExpectCallTest, CallShouldMatchExpectCallWithParams2) {
  std::string Input = R"cc(
    void Test() {
      Mock mock;
      EXPECT_CALL(mock, TwoArgsMethod(1, 2));
    }
  )cc";
  EXPECT_TRUE(matches(
      wrapGtest(Input),
      gtestExpectCall(cxxMemberCallExpr(
                          callee(functionDecl(hasName("gmock_TwoArgsMethod"))))
                          .bind("mock_call"),
                      MockArgs::Some)));
}

} // end namespace ast_matchers
} // end namespace clang