1 // RUN: %check_clang_tidy %s bugprone-argument-comment %t
2
3 namespace testing {
4 namespace internal {
5
6 template <typename F>
7 struct Function;
8
9 template <typename R>
10 struct Function<R()> {
11 typedef R Result;
12 };
13
14 template <typename R, typename A1>
15 struct Function<R(A1)>
16 : Function<R()> {
17 typedef A1 Argument1;
18 };
19
20 template <typename R, typename A1, typename A2>
21 struct Function<R(A1, A2)>
22 : Function<R(A1)> {
23 typedef A2 Argument2;
24 };
25
26 } // namespace internal
27
28 template <typename F>
29 class MockSpec {
30 public:
31 void f();
32 };
33
34 template <typename T>
35 class Matcher {
36 public:
37 explicit Matcher();
38 Matcher(T value);
39 };
40
41 } // namespace testing
42
43 #define GMOCK_RESULT_(tn, ...) \
44 tn ::testing::internal::Function<__VA_ARGS__>::Result
45 #define GMOCK_ARG_(tn, N, ...) \
46 tn ::testing::internal::Function<__VA_ARGS__>::Argument##N
47 #define GMOCK_MATCHER_(tn, N, ...) \
48 const ::testing::Matcher<GMOCK_ARG_(tn, N, __VA_ARGS__)>&
49 #define GMOCK_METHOD2_(tn, constness, ct, Method, ...) \
50 GMOCK_RESULT_(tn, __VA_ARGS__) \
51 ct Method( \
52 GMOCK_ARG_(tn, 1, __VA_ARGS__) gmock_a1, \
53 GMOCK_ARG_(tn, 2, __VA_ARGS__) gmock_a2) constness; \
54 ::testing::MockSpec<__VA_ARGS__> \
55 gmock_##Method(GMOCK_MATCHER_(tn, 1, __VA_ARGS__) gmock_a1, \
56 GMOCK_MATCHER_(tn, 2, __VA_ARGS__) gmock_a2) constness
57 #define MOCK_METHOD2(m, ...) GMOCK_METHOD2_(, , , m, __VA_ARGS__)
58 #define MOCK_CONST_METHOD2(m, ...) GMOCK_METHOD2_(, const, , m, __VA_ARGS__)
59 #define GMOCK_EXPECT_CALL_IMPL_(obj, call) \
60 ((obj).gmock_##call).f()
61 #define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
62
63 class Base {
64 public:
65 virtual void Method(int param_one_base, int param_two_base);
66 };
67 class Derived : public Base {
68 public:
69 virtual void Method(int param_one, int param_two);
70 virtual void Method2(int p_one, int p_two) const;
71 };
72 class MockDerived : public Derived {
73 public:
74 MOCK_METHOD2(Method, void(int a, int b));
75 MOCK_CONST_METHOD2(Method2, void(int c, int d));
76 };
77
78 class MockStandalone {
79 public:
80 MOCK_METHOD2(Method, void(int aaa, int bbb));
81 };
82
test_gmock_expectations()83 void test_gmock_expectations() {
84 MockDerived m;
85 EXPECT_CALL(m, Method(/*param_one=*/1, /*param_tw=*/2));
86 // CHECK-NOTES: [[@LINE-1]]:42: warning: argument name 'param_tw' in comment does not match parameter name 'param_two'
87 // CHECK-NOTES: [[@LINE-18]]:42: note: 'param_two' declared here
88 // CHECK-NOTES: [[@LINE-14]]:3: note: actual callee ('gmock_Method') is declared here
89 // CHECK-NOTES: [[@LINE-32]]:30: note: expanded from macro 'MOCK_METHOD2'
90 // CHECK-NOTES: [[@LINE-35]]:7: note: expanded from macro 'GMOCK_METHOD2_'
91 // CHECK-NOTES: note: expanded from here
92 // CHECK-FIXES: EXPECT_CALL(m, Method(/*param_one=*/1, /*param_two=*/2));
93 EXPECT_CALL(m, Method2(/*p_on=*/3, /*p_two=*/4));
94 // CHECK-NOTES: [[@LINE-1]]:26: warning: argument name 'p_on' in comment does not match parameter name 'p_one'
95 // CHECK-NOTES: [[@LINE-25]]:28: note: 'p_one' declared here
96 // CHECK-NOTES: [[@LINE-21]]:3: note: actual callee ('gmock_Method2') is declared here
97 // CHECK-NOTES: [[@LINE-39]]:36: note: expanded from macro 'MOCK_CONST_METHOD2'
98 // CHECK-NOTES: [[@LINE-43]]:7: note: expanded from macro 'GMOCK_METHOD2_'
99 // CHECK-NOTES: note: expanded from here
100 // CHECK-FIXES: EXPECT_CALL(m, Method2(/*p_one=*/3, /*p_two=*/4));
101
102 #define PARAM1 11
103 #define PARAM2 22
104 EXPECT_CALL(m, Method2(/*p_on1=*/PARAM1, /*p_tw2=*/PARAM2));
105 // CHECK-NOTES: [[@LINE-1]]:26: warning: argument name 'p_on1' in comment does not match parameter name 'p_one'
106 // CHECK-NOTES: [[@LINE-36]]:28: note: 'p_one' declared here
107 // CHECK-NOTES: [[@LINE-32]]:3: note: actual callee ('gmock_Method2') is declared here
108 // CHECK-NOTES: [[@LINE-50]]:36: note: expanded from macro 'MOCK_CONST_METHOD2'
109 // CHECK-NOTES: [[@LINE-54]]:7: note: expanded from macro 'GMOCK_METHOD2_'
110 // CHECK-NOTES: note: expanded from here
111 // CHECK-NOTES: [[@LINE-7]]:44: warning: argument name 'p_tw2' in comment does not match parameter name 'p_two'
112 // CHECK-NOTES: [[@LINE-42]]:39: note: 'p_two' declared here
113 // CHECK-NOTES: [[@LINE-38]]:3: note: actual callee ('gmock_Method2') is declared here
114 // CHECK-NOTES: [[@LINE-56]]:36: note: expanded from macro 'MOCK_CONST_METHOD2'
115 // CHECK-NOTES: [[@LINE-60]]:7: note: expanded from macro 'GMOCK_METHOD2_'
116 // CHECK-NOTES: note: expanded from here
117 // CHECK-FIXES: EXPECT_CALL(m, Method2(/*p_one=*/PARAM1, /*p_two=*/PARAM2));
118
119 MockStandalone m2;
120 EXPECT_CALL(m2, Method(/*aaa=*/5, /*bbc=*/6));
121 }
122
test_gmock_direct_calls()123 void test_gmock_direct_calls() {
124 MockDerived m;
125 m.Method(/*param_one=*/1, /*param_tw=*/2);
126 // CHECK-NOTES: [[@LINE-1]]:29: warning: argument name 'param_tw' in comment does not match parameter name 'param_two'
127 // CHECK-NOTES: [[@LINE-58]]:42: note: 'param_two' declared here
128 // CHECK-NOTES: [[@LINE-54]]:16: note: actual callee ('Method') is declared here
129 // CHECK-FIXES: m.Method(/*param_one=*/1, /*param_two=*/2);
130 }
131