Skip to content

Commit d89b363

Browse files
Abseil Teamsuertreus
Abseil Team
authored andcommitted
Googletest export
Add support for ref qualifiers in MOCK_METHOD. PiperOrigin-RevId: 341047839
1 parent 710f9c1 commit d89b363

File tree

3 files changed

+118
-28
lines changed

3 files changed

+118
-28
lines changed

googlemock/docs/cook_book.md

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ generated method:
3737
`noexcept` method.
3838
* **`Calltype(...)`** - Sets the call type for the method (e.g. to
3939
`STDMETHODCALLTYPE`), useful in Windows.
40+
* **`ref(...)`** - Marks the method with the reference qualification
41+
specified. Required if overriding a method that has reference
42+
qualifications. Eg `ref(&)` or `ref(&&)`.
4043

4144
### Dealing with unprotected commas
4245

googlemock/include/gmock/gmock-function-mocker.h

+49-28
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,21 @@ namespace internal {
4848
template <typename T>
4949
using identity_t = T;
5050

51-
template <typename MockType>
52-
const MockType* AdjustConstness_const(const MockType* mock) {
53-
return mock;
54-
}
55-
56-
template <typename MockType>
57-
MockType* AdjustConstness_(const MockType* mock) {
58-
return const_cast<MockType*>(mock);
59-
}
51+
template <typename Pattern>
52+
struct ThisRefAdjuster {
53+
template <typename T>
54+
using AdjustT = typename std::conditional<
55+
std::is_const<typename std::remove_reference<Pattern>::type>::value,
56+
typename std::conditional<std::is_lvalue_reference<Pattern>::value,
57+
const T&, const T&&>::type,
58+
typename std::conditional<std::is_lvalue_reference<Pattern>::value, T&,
59+
T&&>::type>::type;
60+
61+
template <typename MockType>
62+
static AdjustT<MockType> Adjust(const MockType& mock) {
63+
return static_cast<AdjustT<MockType>>(const_cast<MockType&>(mock));
64+
}
65+
};
6066

6167
} // namespace internal
6268

@@ -80,17 +86,17 @@ using internal::FunctionMocker;
8086
#define GMOCK_INTERNAL_MOCK_METHOD_ARG_3(_Ret, _MethodName, _Args) \
8187
GMOCK_INTERNAL_MOCK_METHOD_ARG_4(_Ret, _MethodName, _Args, ())
8288

83-
#define GMOCK_INTERNAL_MOCK_METHOD_ARG_4(_Ret, _MethodName, _Args, _Spec) \
84-
GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Args); \
85-
GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Spec); \
86-
GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE( \
87-
GMOCK_PP_NARG0 _Args, GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)); \
88-
GMOCK_INTERNAL_ASSERT_VALID_SPEC(_Spec) \
89-
GMOCK_INTERNAL_MOCK_METHOD_IMPL( \
90-
GMOCK_PP_NARG0 _Args, _MethodName, GMOCK_INTERNAL_HAS_CONST(_Spec), \
91-
GMOCK_INTERNAL_HAS_OVERRIDE(_Spec), GMOCK_INTERNAL_HAS_FINAL(_Spec), \
92-
GMOCK_INTERNAL_GET_NOEXCEPT_SPEC(_Spec), \
93-
GMOCK_INTERNAL_GET_CALLTYPE(_Spec), \
89+
#define GMOCK_INTERNAL_MOCK_METHOD_ARG_4(_Ret, _MethodName, _Args, _Spec) \
90+
GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Args); \
91+
GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Spec); \
92+
GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE( \
93+
GMOCK_PP_NARG0 _Args, GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)); \
94+
GMOCK_INTERNAL_ASSERT_VALID_SPEC(_Spec) \
95+
GMOCK_INTERNAL_MOCK_METHOD_IMPL( \
96+
GMOCK_PP_NARG0 _Args, _MethodName, GMOCK_INTERNAL_HAS_CONST(_Spec), \
97+
GMOCK_INTERNAL_HAS_OVERRIDE(_Spec), GMOCK_INTERNAL_HAS_FINAL(_Spec), \
98+
GMOCK_INTERNAL_GET_NOEXCEPT_SPEC(_Spec), \
99+
GMOCK_INTERNAL_GET_CALLTYPE(_Spec), GMOCK_INTERNAL_GET_REF_SPEC(_Spec), \
94100
(GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)))
95101

96102
#define GMOCK_INTERNAL_MOCK_METHOD_ARG_5(...) \
@@ -131,12 +137,12 @@ using internal::FunctionMocker;
131137

132138
#define GMOCK_INTERNAL_MOCK_METHOD_IMPL(_N, _MethodName, _Constness, \
133139
_Override, _Final, _NoexceptSpec, \
134-
_CallType, _Signature) \
140+
_CallType, _RefSpec, _Signature) \
135141
typename ::testing::internal::Function<GMOCK_PP_REMOVE_PARENS( \
136142
_Signature)>::Result \
137143
GMOCK_INTERNAL_EXPAND(_CallType) \
138144
_MethodName(GMOCK_PP_REPEAT(GMOCK_INTERNAL_PARAMETER, _Signature, _N)) \
139-
GMOCK_PP_IF(_Constness, const, ) _NoexceptSpec \
145+
GMOCK_PP_IF(_Constness, const, ) _RefSpec _NoexceptSpec \
140146
GMOCK_PP_IF(_Override, override, ) GMOCK_PP_IF(_Final, final, ) { \
141147
GMOCK_MOCKER_(_N, _Constness, _MethodName) \
142148
.SetOwnerAndName(this, #_MethodName); \
@@ -145,18 +151,18 @@ using internal::FunctionMocker;
145151
} \
146152
::testing::MockSpec<GMOCK_PP_REMOVE_PARENS(_Signature)> gmock_##_MethodName( \
147153
GMOCK_PP_REPEAT(GMOCK_INTERNAL_MATCHER_PARAMETER, _Signature, _N)) \
148-
GMOCK_PP_IF(_Constness, const, ) { \
154+
GMOCK_PP_IF(_Constness, const, ) _RefSpec { \
149155
GMOCK_MOCKER_(_N, _Constness, _MethodName).RegisterOwner(this); \
150156
return GMOCK_MOCKER_(_N, _Constness, _MethodName) \
151157
.With(GMOCK_PP_REPEAT(GMOCK_INTERNAL_MATCHER_ARGUMENT, , _N)); \
152158
} \
153159
::testing::MockSpec<GMOCK_PP_REMOVE_PARENS(_Signature)> gmock_##_MethodName( \
154160
const ::testing::internal::WithoutMatchers&, \
155161
GMOCK_PP_IF(_Constness, const, )::testing::internal::Function< \
156-
GMOCK_PP_REMOVE_PARENS(_Signature)>*) const _NoexceptSpec { \
157-
return GMOCK_PP_CAT(::testing::internal::AdjustConstness_, \
158-
GMOCK_PP_IF(_Constness, const, ))(this) \
159-
->gmock_##_MethodName(GMOCK_PP_REPEAT( \
162+
GMOCK_PP_REMOVE_PARENS(_Signature)>*) const _RefSpec _NoexceptSpec { \
163+
return ::testing::internal::ThisRefAdjuster<GMOCK_PP_IF( \
164+
_Constness, const, ) int _RefSpec>::Adjust(*this) \
165+
.gmock_##_MethodName(GMOCK_PP_REPEAT( \
160166
GMOCK_INTERNAL_A_MATCHER_ARGUMENT, _Signature, _N)); \
161167
} \
162168
mutable ::testing::FunctionMocker<GMOCK_PP_REMOVE_PARENS(_Signature)> \
@@ -183,6 +189,13 @@ using internal::FunctionMocker;
183189
GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_NOEXCEPT(_i, _, _elem)), \
184190
_elem, )
185191

192+
#define GMOCK_INTERNAL_GET_REF_SPEC(_Tuple) \
193+
GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_REF_SPEC_IF_REF, ~, _Tuple)
194+
195+
#define GMOCK_INTERNAL_REF_SPEC_IF_REF(_i, _, _elem) \
196+
GMOCK_PP_IF(GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_REF(_i, _, _elem)), \
197+
GMOCK_PP_CAT(GMOCK_INTERNAL_UNPACK_, _elem), )
198+
186199
#define GMOCK_INTERNAL_GET_CALLTYPE(_Tuple) \
187200
GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_GET_CALLTYPE_IMPL, ~, _Tuple)
188201

@@ -192,6 +205,7 @@ using internal::FunctionMocker;
192205
GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_OVERRIDE(_i, _, _elem)) + \
193206
GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_FINAL(_i, _, _elem)) + \
194207
GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_NOEXCEPT(_i, _, _elem)) + \
208+
GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_REF(_i, _, _elem)) + \
195209
GMOCK_INTERNAL_IS_CALLTYPE(_elem)) == 1, \
196210
GMOCK_PP_STRINGIZE( \
197211
_elem) " cannot be recognized as a valid specification modifier.");
@@ -217,6 +231,13 @@ using internal::FunctionMocker;
217231

218232
#define GMOCK_INTERNAL_DETECT_NOEXCEPT_I_noexcept ,
219233

234+
#define GMOCK_INTERNAL_DETECT_REF(_i, _, _elem) \
235+
GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_REF_I_, _elem)
236+
237+
#define GMOCK_INTERNAL_DETECT_REF_I_ref ,
238+
239+
#define GMOCK_INTERNAL_UNPACK_ref(x) x
240+
220241
#define GMOCK_INTERNAL_GET_CALLTYPE_IMPL(_i, _, _elem) \
221242
GMOCK_PP_IF(GMOCK_INTERNAL_IS_CALLTYPE(_elem), \
222243
GMOCK_INTERNAL_GET_VALUE_CALLTYPE, GMOCK_PP_EMPTY) \
@@ -449,7 +470,7 @@ using internal::FunctionMocker;
449470
GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE( \
450471
args_num, ::testing::internal::identity_t<__VA_ARGS__>); \
451472
GMOCK_INTERNAL_MOCK_METHOD_IMPL( \
452-
args_num, Method, GMOCK_PP_NARG0(constness), 0, 0, , ct, \
473+
args_num, Method, GMOCK_PP_NARG0(constness), 0, 0, , ct, , \
453474
(::testing::internal::identity_t<__VA_ARGS__>))
454475

455476
#define GMOCK_MOCKER_(arity, constness, Method) \

googlemock/test/gmock-function-mocker_test.cc

+66
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ class FooInterface {
108108
using fn_ptr = int (*)(bool);
109109
virtual fn_ptr ReturnsFunctionPointer2(int) = 0;
110110

111+
virtual int RefQualifiedConstRef() const& = 0;
112+
virtual int RefQualifiedConstRefRef() const&& = 0;
113+
virtual int RefQualifiedRef() & = 0;
114+
virtual int RefQualifiedRefRef() && = 0;
115+
116+
virtual int RefQualifiedOverloaded() const& = 0;
117+
virtual int RefQualifiedOverloaded() const&& = 0;
118+
virtual int RefQualifiedOverloaded() & = 0;
119+
virtual int RefQualifiedOverloaded() && = 0;
120+
111121
#if GTEST_OS_WINDOWS
112122
STDMETHOD_(int, CTNullary)() = 0;
113123
STDMETHOD_(bool, CTUnary)(int x) = 0;
@@ -181,6 +191,17 @@ class MockFoo : public FooInterface {
181191
(Calltype(STDMETHODCALLTYPE)));
182192
#endif // GTEST_OS_WINDOWS
183193

194+
// Test reference qualified functions.
195+
MOCK_METHOD(int, RefQualifiedConstRef, (), (const, ref(&), override));
196+
MOCK_METHOD(int, RefQualifiedConstRefRef, (), (const, ref(&&), override));
197+
MOCK_METHOD(int, RefQualifiedRef, (), (ref(&), override));
198+
MOCK_METHOD(int, RefQualifiedRefRef, (), (ref(&&), override));
199+
200+
MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&), override));
201+
MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&&), override));
202+
MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&), override));
203+
MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&&), override));
204+
184205
private:
185206
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
186207
};
@@ -242,6 +263,17 @@ class LegacyMockFoo : public FooInterface {
242263
std::map<int, std::string>());
243264
#endif // GTEST_OS_WINDOWS
244265

266+
// We can't mock these with the old macros, but we need to define them to make
267+
// it concrete.
268+
int RefQualifiedConstRef() const& override { return 0; }
269+
int RefQualifiedConstRefRef() const&& override { return 0; }
270+
int RefQualifiedRef() & override { return 0; }
271+
int RefQualifiedRefRef() && override { return 0; }
272+
int RefQualifiedOverloaded() const& override { return 0; }
273+
int RefQualifiedOverloaded() const&& override { return 0; }
274+
int RefQualifiedOverloaded() & override { return 0; }
275+
int RefQualifiedOverloaded() && override { return 0; }
276+
245277
private:
246278
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockFoo);
247279
};
@@ -420,6 +452,40 @@ TYPED_TEST(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
420452

421453
#endif // GTEST_OS_WINDOWS
422454

455+
TEST(FunctionMockerTest, RefQualified) {
456+
MockFoo mock_foo;
457+
458+
EXPECT_CALL(mock_foo, RefQualifiedConstRef).WillOnce(Return(1));
459+
EXPECT_CALL(std::move(mock_foo), // NOLINT
460+
RefQualifiedConstRefRef)
461+
.WillOnce(Return(2));
462+
EXPECT_CALL(mock_foo, RefQualifiedRef).WillOnce(Return(3));
463+
EXPECT_CALL(std::move(mock_foo), // NOLINT
464+
RefQualifiedRefRef)
465+
.WillOnce(Return(4));
466+
467+
EXPECT_CALL(static_cast<const MockFoo&>(mock_foo), RefQualifiedOverloaded())
468+
.WillOnce(Return(5));
469+
EXPECT_CALL(static_cast<const MockFoo&&>(mock_foo), RefQualifiedOverloaded())
470+
.WillOnce(Return(6));
471+
EXPECT_CALL(static_cast<MockFoo&>(mock_foo), RefQualifiedOverloaded())
472+
.WillOnce(Return(7));
473+
EXPECT_CALL(static_cast<MockFoo&&>(mock_foo), RefQualifiedOverloaded())
474+
.WillOnce(Return(8));
475+
476+
EXPECT_EQ(mock_foo.RefQualifiedConstRef(), 1);
477+
EXPECT_EQ(std::move(mock_foo).RefQualifiedConstRefRef(), 2); // NOLINT
478+
EXPECT_EQ(mock_foo.RefQualifiedRef(), 3);
479+
EXPECT_EQ(std::move(mock_foo).RefQualifiedRefRef(), 4); // NOLINT
480+
481+
EXPECT_EQ(std::cref(mock_foo).get().RefQualifiedOverloaded(), 5);
482+
EXPECT_EQ(std::move(std::cref(mock_foo).get()) // NOLINT
483+
.RefQualifiedOverloaded(),
484+
6);
485+
EXPECT_EQ(mock_foo.RefQualifiedOverloaded(), 7);
486+
EXPECT_EQ(std::move(mock_foo).RefQualifiedOverloaded(), 8); // NOLINT
487+
}
488+
423489
class MockB {
424490
public:
425491
MockB() {}

0 commit comments

Comments
 (0)