xref: /freebsd/contrib/googletest/googlemock/test/gmock-function-mocker_test.cc (revision 28f6c2f292806bf31230a959bc4b19d7081669a7)
1*28f6c2f2SEnji Cooper // Copyright 2007, Google Inc.
2*28f6c2f2SEnji Cooper // All rights reserved.
3*28f6c2f2SEnji Cooper //
4*28f6c2f2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5*28f6c2f2SEnji Cooper // modification, are permitted provided that the following conditions are
6*28f6c2f2SEnji Cooper // met:
7*28f6c2f2SEnji Cooper //
8*28f6c2f2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9*28f6c2f2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10*28f6c2f2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11*28f6c2f2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12*28f6c2f2SEnji Cooper // in the documentation and/or other materials provided with the
13*28f6c2f2SEnji Cooper // distribution.
14*28f6c2f2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15*28f6c2f2SEnji Cooper // contributors may be used to endorse or promote products derived from
16*28f6c2f2SEnji Cooper // this software without specific prior written permission.
17*28f6c2f2SEnji Cooper //
18*28f6c2f2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*28f6c2f2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*28f6c2f2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*28f6c2f2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*28f6c2f2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*28f6c2f2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*28f6c2f2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*28f6c2f2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*28f6c2f2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*28f6c2f2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*28f6c2f2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*28f6c2f2SEnji Cooper 
30*28f6c2f2SEnji Cooper // Google Mock - a framework for writing C++ mock classes.
31*28f6c2f2SEnji Cooper //
32*28f6c2f2SEnji Cooper // This file tests the function mocker classes.
33*28f6c2f2SEnji Cooper #include "gmock/gmock-function-mocker.h"
34*28f6c2f2SEnji Cooper 
35*28f6c2f2SEnji Cooper // Silence C4503 (decorated name length exceeded) for MSVC.
36*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4503)
37*28f6c2f2SEnji Cooper 
38*28f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
39*28f6c2f2SEnji Cooper // MSDN says the header file to be included for STDMETHOD is BaseTyps.h but
40*28f6c2f2SEnji Cooper // we are getting compiler errors if we use basetyps.h, hence including
41*28f6c2f2SEnji Cooper // objbase.h for definition of STDMETHOD.
42*28f6c2f2SEnji Cooper #include <objbase.h>
43*28f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS
44*28f6c2f2SEnji Cooper 
45*28f6c2f2SEnji Cooper #include <functional>
46*28f6c2f2SEnji Cooper #include <map>
47*28f6c2f2SEnji Cooper #include <string>
48*28f6c2f2SEnji Cooper #include <type_traits>
49*28f6c2f2SEnji Cooper 
50*28f6c2f2SEnji Cooper #include "gmock/gmock.h"
51*28f6c2f2SEnji Cooper #include "gtest/gtest.h"
52*28f6c2f2SEnji Cooper 
53*28f6c2f2SEnji Cooper namespace testing {
54*28f6c2f2SEnji Cooper namespace gmock_function_mocker_test {
55*28f6c2f2SEnji Cooper 
56*28f6c2f2SEnji Cooper using testing::_;
57*28f6c2f2SEnji Cooper using testing::A;
58*28f6c2f2SEnji Cooper using testing::An;
59*28f6c2f2SEnji Cooper using testing::AnyNumber;
60*28f6c2f2SEnji Cooper using testing::Const;
61*28f6c2f2SEnji Cooper using testing::DoDefault;
62*28f6c2f2SEnji Cooper using testing::Eq;
63*28f6c2f2SEnji Cooper using testing::Lt;
64*28f6c2f2SEnji Cooper using testing::MockFunction;
65*28f6c2f2SEnji Cooper using testing::Ref;
66*28f6c2f2SEnji Cooper using testing::Return;
67*28f6c2f2SEnji Cooper using testing::ReturnRef;
68*28f6c2f2SEnji Cooper using testing::TypedEq;
69*28f6c2f2SEnji Cooper 
70*28f6c2f2SEnji Cooper template <typename T>
71*28f6c2f2SEnji Cooper class TemplatedCopyable {
72*28f6c2f2SEnji Cooper  public:
73*28f6c2f2SEnji Cooper   TemplatedCopyable() = default;
74*28f6c2f2SEnji Cooper 
75*28f6c2f2SEnji Cooper   template <typename U>
TemplatedCopyable(const U & other)76*28f6c2f2SEnji Cooper   TemplatedCopyable(const U& other) {}  // NOLINT
77*28f6c2f2SEnji Cooper };
78*28f6c2f2SEnji Cooper 
79*28f6c2f2SEnji Cooper class FooInterface {
80*28f6c2f2SEnji Cooper  public:
81*28f6c2f2SEnji Cooper   virtual ~FooInterface() = default;
82*28f6c2f2SEnji Cooper 
83*28f6c2f2SEnji Cooper   virtual void VoidReturning(int x) = 0;
84*28f6c2f2SEnji Cooper 
85*28f6c2f2SEnji Cooper   virtual int Nullary() = 0;
86*28f6c2f2SEnji Cooper   virtual bool Unary(int x) = 0;
87*28f6c2f2SEnji Cooper   virtual long Binary(short x, int y) = 0;                     // NOLINT
88*28f6c2f2SEnji Cooper   virtual int Decimal(bool b, char c, short d, int e, long f,  // NOLINT
89*28f6c2f2SEnji Cooper                       float g, double h, unsigned i, char* j,
90*28f6c2f2SEnji Cooper                       const std::string& k) = 0;
91*28f6c2f2SEnji Cooper 
92*28f6c2f2SEnji Cooper   virtual bool TakesNonConstReference(int& n) = 0;  // NOLINT
93*28f6c2f2SEnji Cooper   virtual std::string TakesConstReference(const int& n) = 0;
94*28f6c2f2SEnji Cooper   virtual bool TakesConst(const int x) = 0;
95*28f6c2f2SEnji Cooper 
96*28f6c2f2SEnji Cooper   virtual int OverloadedOnArgumentNumber() = 0;
97*28f6c2f2SEnji Cooper   virtual int OverloadedOnArgumentNumber(int n) = 0;
98*28f6c2f2SEnji Cooper 
99*28f6c2f2SEnji Cooper   virtual int OverloadedOnArgumentType(int n) = 0;
100*28f6c2f2SEnji Cooper   virtual char OverloadedOnArgumentType(char c) = 0;
101*28f6c2f2SEnji Cooper 
102*28f6c2f2SEnji Cooper   virtual int OverloadedOnConstness() = 0;
103*28f6c2f2SEnji Cooper   virtual char OverloadedOnConstness() const = 0;
104*28f6c2f2SEnji Cooper 
105*28f6c2f2SEnji Cooper   virtual int TypeWithHole(int (*func)()) = 0;
106*28f6c2f2SEnji Cooper   virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0;
107*28f6c2f2SEnji Cooper   virtual int TypeWithTemplatedCopyCtor(const TemplatedCopyable<int>&) = 0;
108*28f6c2f2SEnji Cooper 
109*28f6c2f2SEnji Cooper   virtual int (*ReturnsFunctionPointer1(int))(bool) = 0;
110*28f6c2f2SEnji Cooper   using fn_ptr = int (*)(bool);
111*28f6c2f2SEnji Cooper   virtual fn_ptr ReturnsFunctionPointer2(int) = 0;
112*28f6c2f2SEnji Cooper 
113*28f6c2f2SEnji Cooper   virtual int RefQualifiedConstRef() const& = 0;
114*28f6c2f2SEnji Cooper   virtual int RefQualifiedConstRefRef() const&& = 0;
115*28f6c2f2SEnji Cooper   virtual int RefQualifiedRef() & = 0;
116*28f6c2f2SEnji Cooper   virtual int RefQualifiedRefRef() && = 0;
117*28f6c2f2SEnji Cooper 
118*28f6c2f2SEnji Cooper   virtual int RefQualifiedOverloaded() const& = 0;
119*28f6c2f2SEnji Cooper   virtual int RefQualifiedOverloaded() const&& = 0;
120*28f6c2f2SEnji Cooper   virtual int RefQualifiedOverloaded() & = 0;
121*28f6c2f2SEnji Cooper   virtual int RefQualifiedOverloaded() && = 0;
122*28f6c2f2SEnji Cooper 
123*28f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
124*28f6c2f2SEnji Cooper   STDMETHOD_(int, CTNullary)() = 0;
125*28f6c2f2SEnji Cooper   STDMETHOD_(bool, CTUnary)(int x) = 0;
126*28f6c2f2SEnji Cooper   STDMETHOD_(int, CTDecimal)
127*28f6c2f2SEnji Cooper   (bool b, char c, short d, int e, long f,  // NOLINT
128*28f6c2f2SEnji Cooper    float g, double h, unsigned i, char* j, const std::string& k) = 0;
129*28f6c2f2SEnji Cooper   STDMETHOD_(char, CTConst)(int x) const = 0;
130*28f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS
131*28f6c2f2SEnji Cooper };
132*28f6c2f2SEnji Cooper 
133*28f6c2f2SEnji Cooper // Const qualifiers on arguments were once (incorrectly) considered
134*28f6c2f2SEnji Cooper // significant in determining whether two virtual functions had the same
135*28f6c2f2SEnji Cooper // signature. This was fixed in Visual Studio 2008. However, the compiler
136*28f6c2f2SEnji Cooper // still emits a warning that alerts about this change in behavior.
137*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4373)
138*28f6c2f2SEnji Cooper class MockFoo : public FooInterface {
139*28f6c2f2SEnji Cooper  public:
140*28f6c2f2SEnji Cooper   MockFoo() = default;
141*28f6c2f2SEnji Cooper 
142*28f6c2f2SEnji Cooper   // Makes sure that a mock function parameter can be named.
143*28f6c2f2SEnji Cooper   MOCK_METHOD(void, VoidReturning, (int n));  // NOLINT
144*28f6c2f2SEnji Cooper 
145*28f6c2f2SEnji Cooper   MOCK_METHOD(int, Nullary, ());  // NOLINT
146*28f6c2f2SEnji Cooper 
147*28f6c2f2SEnji Cooper   // Makes sure that a mock function parameter can be unnamed.
148*28f6c2f2SEnji Cooper   MOCK_METHOD(bool, Unary, (int));          // NOLINT
149*28f6c2f2SEnji Cooper   MOCK_METHOD(long, Binary, (short, int));  // NOLINT
150*28f6c2f2SEnji Cooper   MOCK_METHOD(int, Decimal,
151*28f6c2f2SEnji Cooper               (bool, char, short, int, long, float,  // NOLINT
152*28f6c2f2SEnji Cooper                double, unsigned, char*, const std::string& str),
153*28f6c2f2SEnji Cooper               (override));
154*28f6c2f2SEnji Cooper 
155*28f6c2f2SEnji Cooper   MOCK_METHOD(bool, TakesNonConstReference, (int&));  // NOLINT
156*28f6c2f2SEnji Cooper   MOCK_METHOD(std::string, TakesConstReference, (const int&));
157*28f6c2f2SEnji Cooper   MOCK_METHOD(bool, TakesConst, (const int));  // NOLINT
158*28f6c2f2SEnji Cooper 
159*28f6c2f2SEnji Cooper   // Tests that the function return type can contain unprotected comma.
160*28f6c2f2SEnji Cooper   MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (), ());
161*28f6c2f2SEnji Cooper   MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (int),
162*28f6c2f2SEnji Cooper               (const));  // NOLINT
163*28f6c2f2SEnji Cooper 
164*28f6c2f2SEnji Cooper   MOCK_METHOD(int, OverloadedOnArgumentNumber, ());     // NOLINT
165*28f6c2f2SEnji Cooper   MOCK_METHOD(int, OverloadedOnArgumentNumber, (int));  // NOLINT
166*28f6c2f2SEnji Cooper 
167*28f6c2f2SEnji Cooper   MOCK_METHOD(int, OverloadedOnArgumentType, (int));    // NOLINT
168*28f6c2f2SEnji Cooper   MOCK_METHOD(char, OverloadedOnArgumentType, (char));  // NOLINT
169*28f6c2f2SEnji Cooper 
170*28f6c2f2SEnji Cooper   MOCK_METHOD(int, OverloadedOnConstness, (), (override));          // NOLINT
171*28f6c2f2SEnji Cooper   MOCK_METHOD(char, OverloadedOnConstness, (), (override, const));  // NOLINT
172*28f6c2f2SEnji Cooper 
173*28f6c2f2SEnji Cooper   MOCK_METHOD(int, TypeWithHole, (int (*)()), ());  // NOLINT
174*28f6c2f2SEnji Cooper   MOCK_METHOD(int, TypeWithComma, ((const std::map<int, std::string>&)));
175*28f6c2f2SEnji Cooper   MOCK_METHOD(int, TypeWithTemplatedCopyCtor,
176*28f6c2f2SEnji Cooper               (const TemplatedCopyable<int>&));  // NOLINT
177*28f6c2f2SEnji Cooper 
178*28f6c2f2SEnji Cooper   MOCK_METHOD(int (*)(bool), ReturnsFunctionPointer1, (int), ());
179*28f6c2f2SEnji Cooper   MOCK_METHOD(fn_ptr, ReturnsFunctionPointer2, (int), ());
180*28f6c2f2SEnji Cooper 
181*28f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
182*28f6c2f2SEnji Cooper   MOCK_METHOD(int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
183*28f6c2f2SEnji Cooper   MOCK_METHOD(bool, CTUnary, (int), (Calltype(STDMETHODCALLTYPE)));
184*28f6c2f2SEnji Cooper   MOCK_METHOD(int, CTDecimal,
185*28f6c2f2SEnji Cooper               (bool b, char c, short d, int e, long f, float g, double h,
186*28f6c2f2SEnji Cooper                unsigned i, char* j, const std::string& k),
187*28f6c2f2SEnji Cooper               (Calltype(STDMETHODCALLTYPE)));
188*28f6c2f2SEnji Cooper   MOCK_METHOD(char, CTConst, (int), (const, Calltype(STDMETHODCALLTYPE)));
189*28f6c2f2SEnji Cooper   MOCK_METHOD((std::map<int, std::string>), CTReturnTypeWithComma, (),
190*28f6c2f2SEnji Cooper               (Calltype(STDMETHODCALLTYPE)));
191*28f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS
192*28f6c2f2SEnji Cooper 
193*28f6c2f2SEnji Cooper   // Test reference qualified functions.
194*28f6c2f2SEnji Cooper   MOCK_METHOD(int, RefQualifiedConstRef, (), (const, ref(&), override));
195*28f6c2f2SEnji Cooper   MOCK_METHOD(int, RefQualifiedConstRefRef, (), (const, ref(&&), override));
196*28f6c2f2SEnji Cooper   MOCK_METHOD(int, RefQualifiedRef, (), (ref(&), override));
197*28f6c2f2SEnji Cooper   MOCK_METHOD(int, RefQualifiedRefRef, (), (ref(&&), override));
198*28f6c2f2SEnji Cooper 
199*28f6c2f2SEnji Cooper   MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&), override));
200*28f6c2f2SEnji Cooper   MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&&), override));
201*28f6c2f2SEnji Cooper   MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&), override));
202*28f6c2f2SEnji Cooper   MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&&), override));
203*28f6c2f2SEnji Cooper 
204*28f6c2f2SEnji Cooper  private:
205*28f6c2f2SEnji Cooper   MockFoo(const MockFoo&) = delete;
206*28f6c2f2SEnji Cooper   MockFoo& operator=(const MockFoo&) = delete;
207*28f6c2f2SEnji Cooper };
208*28f6c2f2SEnji Cooper 
209*28f6c2f2SEnji Cooper class LegacyMockFoo : public FooInterface {
210*28f6c2f2SEnji Cooper  public:
211*28f6c2f2SEnji Cooper   LegacyMockFoo() = default;
212*28f6c2f2SEnji Cooper 
213*28f6c2f2SEnji Cooper   // Makes sure that a mock function parameter can be named.
214*28f6c2f2SEnji Cooper   MOCK_METHOD1(VoidReturning, void(int n));  // NOLINT
215*28f6c2f2SEnji Cooper 
216*28f6c2f2SEnji Cooper   MOCK_METHOD0(Nullary, int());  // NOLINT
217*28f6c2f2SEnji Cooper 
218*28f6c2f2SEnji Cooper   // Makes sure that a mock function parameter can be unnamed.
219*28f6c2f2SEnji Cooper   MOCK_METHOD1(Unary, bool(int));                                  // NOLINT
220*28f6c2f2SEnji Cooper   MOCK_METHOD2(Binary, long(short, int));                          // NOLINT
221*28f6c2f2SEnji Cooper   MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float,  // NOLINT
222*28f6c2f2SEnji Cooper                              double, unsigned, char*, const std::string& str));
223*28f6c2f2SEnji Cooper 
224*28f6c2f2SEnji Cooper   MOCK_METHOD1(TakesNonConstReference, bool(int&));  // NOLINT
225*28f6c2f2SEnji Cooper   MOCK_METHOD1(TakesConstReference, std::string(const int&));
226*28f6c2f2SEnji Cooper   MOCK_METHOD1(TakesConst, bool(const int));  // NOLINT
227*28f6c2f2SEnji Cooper 
228*28f6c2f2SEnji Cooper   // Tests that the function return type can contain unprotected comma.
229*28f6c2f2SEnji Cooper   MOCK_METHOD0(ReturnTypeWithComma, std::map<int, std::string>());
230*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD1(ReturnTypeWithComma,
231*28f6c2f2SEnji Cooper                      std::map<int, std::string>(int));  // NOLINT
232*28f6c2f2SEnji Cooper 
233*28f6c2f2SEnji Cooper   MOCK_METHOD0(OverloadedOnArgumentNumber, int());     // NOLINT
234*28f6c2f2SEnji Cooper   MOCK_METHOD1(OverloadedOnArgumentNumber, int(int));  // NOLINT
235*28f6c2f2SEnji Cooper 
236*28f6c2f2SEnji Cooper   MOCK_METHOD1(OverloadedOnArgumentType, int(int));    // NOLINT
237*28f6c2f2SEnji Cooper   MOCK_METHOD1(OverloadedOnArgumentType, char(char));  // NOLINT
238*28f6c2f2SEnji Cooper 
239*28f6c2f2SEnji Cooper   MOCK_METHOD0(OverloadedOnConstness, int());         // NOLINT
240*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD0(OverloadedOnConstness, char());  // NOLINT
241*28f6c2f2SEnji Cooper 
242*28f6c2f2SEnji Cooper   MOCK_METHOD1(TypeWithHole, int(int (*)()));  // NOLINT
243*28f6c2f2SEnji Cooper   MOCK_METHOD1(TypeWithComma,
244*28f6c2f2SEnji Cooper                int(const std::map<int, std::string>&));  // NOLINT
245*28f6c2f2SEnji Cooper   MOCK_METHOD1(TypeWithTemplatedCopyCtor,
246*28f6c2f2SEnji Cooper                int(const TemplatedCopyable<int>&));  // NOLINT
247*28f6c2f2SEnji Cooper 
248*28f6c2f2SEnji Cooper   MOCK_METHOD1(ReturnsFunctionPointer1, int (*(int))(bool));
249*28f6c2f2SEnji Cooper   MOCK_METHOD1(ReturnsFunctionPointer2, fn_ptr(int));
250*28f6c2f2SEnji Cooper 
251*28f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
252*28f6c2f2SEnji Cooper   MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
253*28f6c2f2SEnji Cooper   MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int));  // NOLINT
254*28f6c2f2SEnji Cooper   MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal,
255*28f6c2f2SEnji Cooper                               int(bool b, char c, short d, int e,  // NOLINT
256*28f6c2f2SEnji Cooper                                   long f, float g, double h,       // NOLINT
257*28f6c2f2SEnji Cooper                                   unsigned i, char* j, const std::string& k));
258*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTConst,
259*28f6c2f2SEnji Cooper                                    char(int));  // NOLINT
260*28f6c2f2SEnji Cooper 
261*28f6c2f2SEnji Cooper   // Tests that the function return type can contain unprotected comma.
262*28f6c2f2SEnji Cooper   MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTReturnTypeWithComma,
263*28f6c2f2SEnji Cooper                              std::map<int, std::string>());
264*28f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS
265*28f6c2f2SEnji Cooper 
266*28f6c2f2SEnji Cooper   // We can't mock these with the old macros, but we need to define them to make
267*28f6c2f2SEnji Cooper   // it concrete.
RefQualifiedConstRef() const268*28f6c2f2SEnji Cooper   int RefQualifiedConstRef() const& override { return 0; }
RefQualifiedConstRefRef() const269*28f6c2f2SEnji Cooper   int RefQualifiedConstRefRef() const&& override { return 0; }
RefQualifiedRef()270*28f6c2f2SEnji Cooper   int RefQualifiedRef() & override { return 0; }
RefQualifiedRefRef()271*28f6c2f2SEnji Cooper   int RefQualifiedRefRef() && override { return 0; }
RefQualifiedOverloaded() const272*28f6c2f2SEnji Cooper   int RefQualifiedOverloaded() const& override { return 0; }
RefQualifiedOverloaded() const273*28f6c2f2SEnji Cooper   int RefQualifiedOverloaded() const&& override { return 0; }
RefQualifiedOverloaded()274*28f6c2f2SEnji Cooper   int RefQualifiedOverloaded() & override { return 0; }
RefQualifiedOverloaded()275*28f6c2f2SEnji Cooper   int RefQualifiedOverloaded() && override { return 0; }
276*28f6c2f2SEnji Cooper 
277*28f6c2f2SEnji Cooper  private:
278*28f6c2f2SEnji Cooper   LegacyMockFoo(const LegacyMockFoo&) = delete;
279*28f6c2f2SEnji Cooper   LegacyMockFoo& operator=(const LegacyMockFoo&) = delete;
280*28f6c2f2SEnji Cooper };
281*28f6c2f2SEnji Cooper 
282*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4373
283*28f6c2f2SEnji Cooper 
284*28f6c2f2SEnji Cooper template <class T>
285*28f6c2f2SEnji Cooper class FunctionMockerTest : public testing::Test {
286*28f6c2f2SEnji Cooper  protected:
FunctionMockerTest()287*28f6c2f2SEnji Cooper   FunctionMockerTest() : foo_(&mock_foo_) {}
288*28f6c2f2SEnji Cooper 
289*28f6c2f2SEnji Cooper   FooInterface* const foo_;
290*28f6c2f2SEnji Cooper   T mock_foo_;
291*28f6c2f2SEnji Cooper };
292*28f6c2f2SEnji Cooper using FunctionMockerTestTypes = ::testing::Types<MockFoo, LegacyMockFoo>;
293*28f6c2f2SEnji Cooper TYPED_TEST_SUITE(FunctionMockerTest, FunctionMockerTestTypes);
294*28f6c2f2SEnji Cooper 
295*28f6c2f2SEnji Cooper // Tests mocking a void-returning function.
TYPED_TEST(FunctionMockerTest,MocksVoidFunction)296*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksVoidFunction) {
297*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, VoidReturning(Lt(100)));
298*28f6c2f2SEnji Cooper   this->foo_->VoidReturning(0);
299*28f6c2f2SEnji Cooper }
300*28f6c2f2SEnji Cooper 
301*28f6c2f2SEnji Cooper // Tests mocking a nullary function.
TYPED_TEST(FunctionMockerTest,MocksNullaryFunction)302*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksNullaryFunction) {
303*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, Nullary())
304*28f6c2f2SEnji Cooper       .WillOnce(DoDefault())
305*28f6c2f2SEnji Cooper       .WillOnce(Return(1));
306*28f6c2f2SEnji Cooper 
307*28f6c2f2SEnji Cooper   EXPECT_EQ(0, this->foo_->Nullary());
308*28f6c2f2SEnji Cooper   EXPECT_EQ(1, this->foo_->Nullary());
309*28f6c2f2SEnji Cooper }
310*28f6c2f2SEnji Cooper 
311*28f6c2f2SEnji Cooper // Tests mocking a unary function.
TYPED_TEST(FunctionMockerTest,MocksUnaryFunction)312*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksUnaryFunction) {
313*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, Unary(Eq(2))).Times(2).WillOnce(Return(true));
314*28f6c2f2SEnji Cooper 
315*28f6c2f2SEnji Cooper   EXPECT_TRUE(this->foo_->Unary(2));
316*28f6c2f2SEnji Cooper   EXPECT_FALSE(this->foo_->Unary(2));
317*28f6c2f2SEnji Cooper }
318*28f6c2f2SEnji Cooper 
319*28f6c2f2SEnji Cooper // Tests mocking a binary function.
TYPED_TEST(FunctionMockerTest,MocksBinaryFunction)320*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksBinaryFunction) {
321*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, Binary(2, _)).WillOnce(Return(3));
322*28f6c2f2SEnji Cooper 
323*28f6c2f2SEnji Cooper   EXPECT_EQ(3, this->foo_->Binary(2, 1));
324*28f6c2f2SEnji Cooper }
325*28f6c2f2SEnji Cooper 
326*28f6c2f2SEnji Cooper // Tests mocking a decimal function.
TYPED_TEST(FunctionMockerTest,MocksDecimalFunction)327*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksDecimalFunction) {
328*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_,
329*28f6c2f2SEnji Cooper               Decimal(true, 'a', 0, 0, 1L, A<float>(), Lt(100), 5U, NULL, "hi"))
330*28f6c2f2SEnji Cooper       .WillOnce(Return(5));
331*28f6c2f2SEnji Cooper 
332*28f6c2f2SEnji Cooper   EXPECT_EQ(5, this->foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, nullptr, "hi"));
333*28f6c2f2SEnji Cooper }
334*28f6c2f2SEnji Cooper 
335*28f6c2f2SEnji Cooper // Tests mocking a function that takes a non-const reference.
TYPED_TEST(FunctionMockerTest,MocksFunctionWithNonConstReferenceArgument)336*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) {
337*28f6c2f2SEnji Cooper   int a = 0;
338*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, TakesNonConstReference(Ref(a)))
339*28f6c2f2SEnji Cooper       .WillOnce(Return(true));
340*28f6c2f2SEnji Cooper 
341*28f6c2f2SEnji Cooper   EXPECT_TRUE(this->foo_->TakesNonConstReference(a));
342*28f6c2f2SEnji Cooper }
343*28f6c2f2SEnji Cooper 
344*28f6c2f2SEnji Cooper // Tests mocking a function that takes a const reference.
TYPED_TEST(FunctionMockerTest,MocksFunctionWithConstReferenceArgument)345*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
346*28f6c2f2SEnji Cooper   int a = 0;
347*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, TakesConstReference(Ref(a)))
348*28f6c2f2SEnji Cooper       .WillOnce(Return("Hello"));
349*28f6c2f2SEnji Cooper 
350*28f6c2f2SEnji Cooper   EXPECT_EQ("Hello", this->foo_->TakesConstReference(a));
351*28f6c2f2SEnji Cooper }
352*28f6c2f2SEnji Cooper 
353*28f6c2f2SEnji Cooper // Tests mocking a function that takes a const variable.
TYPED_TEST(FunctionMockerTest,MocksFunctionWithConstArgument)354*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksFunctionWithConstArgument) {
355*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, TakesConst(Lt(10))).WillOnce(DoDefault());
356*28f6c2f2SEnji Cooper 
357*28f6c2f2SEnji Cooper   EXPECT_FALSE(this->foo_->TakesConst(5));
358*28f6c2f2SEnji Cooper }
359*28f6c2f2SEnji Cooper 
360*28f6c2f2SEnji Cooper // Tests mocking functions overloaded on the number of arguments.
TYPED_TEST(FunctionMockerTest,MocksFunctionsOverloadedOnArgumentNumber)361*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
362*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentNumber())
363*28f6c2f2SEnji Cooper       .WillOnce(Return(1));
364*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentNumber(_))
365*28f6c2f2SEnji Cooper       .WillOnce(Return(2));
366*28f6c2f2SEnji Cooper 
367*28f6c2f2SEnji Cooper   EXPECT_EQ(2, this->foo_->OverloadedOnArgumentNumber(1));
368*28f6c2f2SEnji Cooper   EXPECT_EQ(1, this->foo_->OverloadedOnArgumentNumber());
369*28f6c2f2SEnji Cooper }
370*28f6c2f2SEnji Cooper 
371*28f6c2f2SEnji Cooper // Tests mocking functions overloaded on the types of argument.
TYPED_TEST(FunctionMockerTest,MocksFunctionsOverloadedOnArgumentType)372*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
373*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentType(An<int>()))
374*28f6c2f2SEnji Cooper       .WillOnce(Return(1));
375*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
376*28f6c2f2SEnji Cooper       .WillOnce(Return('b'));
377*28f6c2f2SEnji Cooper 
378*28f6c2f2SEnji Cooper   EXPECT_EQ(1, this->foo_->OverloadedOnArgumentType(0));
379*28f6c2f2SEnji Cooper   EXPECT_EQ('b', this->foo_->OverloadedOnArgumentType('a'));
380*28f6c2f2SEnji Cooper }
381*28f6c2f2SEnji Cooper 
382*28f6c2f2SEnji Cooper // Tests mocking functions overloaded on the const-ness of this object.
TYPED_TEST(FunctionMockerTest,MocksFunctionsOverloadedOnConstnessOfThis)383*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
384*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, OverloadedOnConstness());
385*28f6c2f2SEnji Cooper   EXPECT_CALL(Const(this->mock_foo_), OverloadedOnConstness())
386*28f6c2f2SEnji Cooper       .WillOnce(Return('a'));
387*28f6c2f2SEnji Cooper 
388*28f6c2f2SEnji Cooper   EXPECT_EQ(0, this->foo_->OverloadedOnConstness());
389*28f6c2f2SEnji Cooper   EXPECT_EQ('a', Const(*this->foo_).OverloadedOnConstness());
390*28f6c2f2SEnji Cooper }
391*28f6c2f2SEnji Cooper 
TYPED_TEST(FunctionMockerTest,MocksReturnTypeWithComma)392*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksReturnTypeWithComma) {
393*28f6c2f2SEnji Cooper   const std::map<int, std::string> a_map;
394*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, ReturnTypeWithComma()).WillOnce(Return(a_map));
395*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, ReturnTypeWithComma(42)).WillOnce(Return(a_map));
396*28f6c2f2SEnji Cooper 
397*28f6c2f2SEnji Cooper   EXPECT_EQ(a_map, this->mock_foo_.ReturnTypeWithComma());
398*28f6c2f2SEnji Cooper   EXPECT_EQ(a_map, this->mock_foo_.ReturnTypeWithComma(42));
399*28f6c2f2SEnji Cooper }
400*28f6c2f2SEnji Cooper 
TYPED_TEST(FunctionMockerTest,MocksTypeWithTemplatedCopyCtor)401*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksTypeWithTemplatedCopyCtor) {
402*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, TypeWithTemplatedCopyCtor(_))
403*28f6c2f2SEnji Cooper       .WillOnce(Return(true));
404*28f6c2f2SEnji Cooper   EXPECT_TRUE(this->foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>()));
405*28f6c2f2SEnji Cooper }
406*28f6c2f2SEnji Cooper 
407*28f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
408*28f6c2f2SEnji Cooper // Tests mocking a nullary function with calltype.
TYPED_TEST(FunctionMockerTest,MocksNullaryFunctionWithCallType)409*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
410*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, CTNullary())
411*28f6c2f2SEnji Cooper       .WillOnce(Return(-1))
412*28f6c2f2SEnji Cooper       .WillOnce(Return(0));
413*28f6c2f2SEnji Cooper 
414*28f6c2f2SEnji Cooper   EXPECT_EQ(-1, this->foo_->CTNullary());
415*28f6c2f2SEnji Cooper   EXPECT_EQ(0, this->foo_->CTNullary());
416*28f6c2f2SEnji Cooper }
417*28f6c2f2SEnji Cooper 
418*28f6c2f2SEnji Cooper // Tests mocking a unary function with calltype.
TYPED_TEST(FunctionMockerTest,MocksUnaryFunctionWithCallType)419*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
420*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, CTUnary(Eq(2)))
421*28f6c2f2SEnji Cooper       .Times(2)
422*28f6c2f2SEnji Cooper       .WillOnce(Return(true))
423*28f6c2f2SEnji Cooper       .WillOnce(Return(false));
424*28f6c2f2SEnji Cooper 
425*28f6c2f2SEnji Cooper   EXPECT_TRUE(this->foo_->CTUnary(2));
426*28f6c2f2SEnji Cooper   EXPECT_FALSE(this->foo_->CTUnary(2));
427*28f6c2f2SEnji Cooper }
428*28f6c2f2SEnji Cooper 
429*28f6c2f2SEnji Cooper // Tests mocking a decimal function with calltype.
TYPED_TEST(FunctionMockerTest,MocksDecimalFunctionWithCallType)430*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
431*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
432*28f6c2f2SEnji Cooper                                          Lt(100), 5U, NULL, "hi"))
433*28f6c2f2SEnji Cooper       .WillOnce(Return(10));
434*28f6c2f2SEnji Cooper 
435*28f6c2f2SEnji Cooper   EXPECT_EQ(10, this->foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
436*28f6c2f2SEnji Cooper }
437*28f6c2f2SEnji Cooper 
438*28f6c2f2SEnji Cooper // Tests mocking functions overloaded on the const-ness of this object.
TYPED_TEST(FunctionMockerTest,MocksFunctionsConstFunctionWithCallType)439*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
440*28f6c2f2SEnji Cooper   EXPECT_CALL(Const(this->mock_foo_), CTConst(_)).WillOnce(Return('a'));
441*28f6c2f2SEnji Cooper 
442*28f6c2f2SEnji Cooper   EXPECT_EQ('a', Const(*this->foo_).CTConst(0));
443*28f6c2f2SEnji Cooper }
444*28f6c2f2SEnji Cooper 
TYPED_TEST(FunctionMockerTest,MocksReturnTypeWithCommaAndCallType)445*28f6c2f2SEnji Cooper TYPED_TEST(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
446*28f6c2f2SEnji Cooper   const std::map<int, std::string> a_map;
447*28f6c2f2SEnji Cooper   EXPECT_CALL(this->mock_foo_, CTReturnTypeWithComma()).WillOnce(Return(a_map));
448*28f6c2f2SEnji Cooper 
449*28f6c2f2SEnji Cooper   EXPECT_EQ(a_map, this->mock_foo_.CTReturnTypeWithComma());
450*28f6c2f2SEnji Cooper }
451*28f6c2f2SEnji Cooper 
452*28f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS
453*28f6c2f2SEnji Cooper 
TEST(FunctionMockerTest,RefQualified)454*28f6c2f2SEnji Cooper TEST(FunctionMockerTest, RefQualified) {
455*28f6c2f2SEnji Cooper   MockFoo mock_foo;
456*28f6c2f2SEnji Cooper 
457*28f6c2f2SEnji Cooper   EXPECT_CALL(mock_foo, RefQualifiedConstRef).WillOnce(Return(1));
458*28f6c2f2SEnji Cooper   EXPECT_CALL(std::move(mock_foo),  // NOLINT
459*28f6c2f2SEnji Cooper               RefQualifiedConstRefRef)
460*28f6c2f2SEnji Cooper       .WillOnce(Return(2));
461*28f6c2f2SEnji Cooper   EXPECT_CALL(mock_foo, RefQualifiedRef).WillOnce(Return(3));
462*28f6c2f2SEnji Cooper   EXPECT_CALL(std::move(mock_foo),  // NOLINT
463*28f6c2f2SEnji Cooper               RefQualifiedRefRef)
464*28f6c2f2SEnji Cooper       .WillOnce(Return(4));
465*28f6c2f2SEnji Cooper 
466*28f6c2f2SEnji Cooper   EXPECT_CALL(static_cast<const MockFoo&>(mock_foo), RefQualifiedOverloaded())
467*28f6c2f2SEnji Cooper       .WillOnce(Return(5));
468*28f6c2f2SEnji Cooper   EXPECT_CALL(static_cast<const MockFoo&&>(mock_foo), RefQualifiedOverloaded())
469*28f6c2f2SEnji Cooper       .WillOnce(Return(6));
470*28f6c2f2SEnji Cooper   EXPECT_CALL(static_cast<MockFoo&>(mock_foo), RefQualifiedOverloaded())
471*28f6c2f2SEnji Cooper       .WillOnce(Return(7));
472*28f6c2f2SEnji Cooper   EXPECT_CALL(static_cast<MockFoo&&>(mock_foo), RefQualifiedOverloaded())
473*28f6c2f2SEnji Cooper       .WillOnce(Return(8));
474*28f6c2f2SEnji Cooper 
475*28f6c2f2SEnji Cooper   EXPECT_EQ(mock_foo.RefQualifiedConstRef(), 1);
476*28f6c2f2SEnji Cooper   EXPECT_EQ(std::move(mock_foo).RefQualifiedConstRefRef(), 2);  // NOLINT
477*28f6c2f2SEnji Cooper   EXPECT_EQ(mock_foo.RefQualifiedRef(), 3);
478*28f6c2f2SEnji Cooper   EXPECT_EQ(std::move(mock_foo).RefQualifiedRefRef(), 4);  // NOLINT
479*28f6c2f2SEnji Cooper 
480*28f6c2f2SEnji Cooper   EXPECT_EQ(std::cref(mock_foo).get().RefQualifiedOverloaded(), 5);
481*28f6c2f2SEnji Cooper   EXPECT_EQ(std::move(std::cref(mock_foo).get())  // NOLINT
482*28f6c2f2SEnji Cooper                 .RefQualifiedOverloaded(),
483*28f6c2f2SEnji Cooper             6);
484*28f6c2f2SEnji Cooper   EXPECT_EQ(mock_foo.RefQualifiedOverloaded(), 7);
485*28f6c2f2SEnji Cooper   EXPECT_EQ(std::move(mock_foo).RefQualifiedOverloaded(), 8);  // NOLINT
486*28f6c2f2SEnji Cooper }
487*28f6c2f2SEnji Cooper 
488*28f6c2f2SEnji Cooper class MockB {
489*28f6c2f2SEnji Cooper  public:
490*28f6c2f2SEnji Cooper   MockB() = default;
491*28f6c2f2SEnji Cooper 
492*28f6c2f2SEnji Cooper   MOCK_METHOD(void, DoB, ());
493*28f6c2f2SEnji Cooper 
494*28f6c2f2SEnji Cooper  private:
495*28f6c2f2SEnji Cooper   MockB(const MockB&) = delete;
496*28f6c2f2SEnji Cooper   MockB& operator=(const MockB&) = delete;
497*28f6c2f2SEnji Cooper };
498*28f6c2f2SEnji Cooper 
499*28f6c2f2SEnji Cooper class LegacyMockB {
500*28f6c2f2SEnji Cooper  public:
501*28f6c2f2SEnji Cooper   LegacyMockB() = default;
502*28f6c2f2SEnji Cooper 
503*28f6c2f2SEnji Cooper   MOCK_METHOD0(DoB, void());
504*28f6c2f2SEnji Cooper 
505*28f6c2f2SEnji Cooper  private:
506*28f6c2f2SEnji Cooper   LegacyMockB(const LegacyMockB&) = delete;
507*28f6c2f2SEnji Cooper   LegacyMockB& operator=(const LegacyMockB&) = delete;
508*28f6c2f2SEnji Cooper };
509*28f6c2f2SEnji Cooper 
510*28f6c2f2SEnji Cooper template <typename T>
511*28f6c2f2SEnji Cooper class ExpectCallTest : public ::testing::Test {};
512*28f6c2f2SEnji Cooper using ExpectCallTestTypes = ::testing::Types<MockB, LegacyMockB>;
513*28f6c2f2SEnji Cooper TYPED_TEST_SUITE(ExpectCallTest, ExpectCallTestTypes);
514*28f6c2f2SEnji Cooper 
515*28f6c2f2SEnji Cooper // Tests that functions with no EXPECT_CALL() rules can be called any
516*28f6c2f2SEnji Cooper // number of times.
TYPED_TEST(ExpectCallTest,UnmentionedFunctionCanBeCalledAnyNumberOfTimes)517*28f6c2f2SEnji Cooper TYPED_TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
518*28f6c2f2SEnji Cooper   { TypeParam b; }
519*28f6c2f2SEnji Cooper 
520*28f6c2f2SEnji Cooper   {
521*28f6c2f2SEnji Cooper     TypeParam b;
522*28f6c2f2SEnji Cooper     b.DoB();
523*28f6c2f2SEnji Cooper   }
524*28f6c2f2SEnji Cooper 
525*28f6c2f2SEnji Cooper   {
526*28f6c2f2SEnji Cooper     TypeParam b;
527*28f6c2f2SEnji Cooper     b.DoB();
528*28f6c2f2SEnji Cooper     b.DoB();
529*28f6c2f2SEnji Cooper   }
530*28f6c2f2SEnji Cooper }
531*28f6c2f2SEnji Cooper 
532*28f6c2f2SEnji Cooper // Tests mocking template interfaces.
533*28f6c2f2SEnji Cooper 
534*28f6c2f2SEnji Cooper template <typename T>
535*28f6c2f2SEnji Cooper class StackInterface {
536*28f6c2f2SEnji Cooper  public:
537*28f6c2f2SEnji Cooper   virtual ~StackInterface() = default;
538*28f6c2f2SEnji Cooper 
539*28f6c2f2SEnji Cooper   // Template parameter appears in function parameter.
540*28f6c2f2SEnji Cooper   virtual void Push(const T& value) = 0;
541*28f6c2f2SEnji Cooper   virtual void Pop() = 0;
542*28f6c2f2SEnji Cooper   virtual int GetSize() const = 0;
543*28f6c2f2SEnji Cooper   // Template parameter appears in function return type.
544*28f6c2f2SEnji Cooper   virtual const T& GetTop() const = 0;
545*28f6c2f2SEnji Cooper };
546*28f6c2f2SEnji Cooper 
547*28f6c2f2SEnji Cooper template <typename T>
548*28f6c2f2SEnji Cooper class MockStack : public StackInterface<T> {
549*28f6c2f2SEnji Cooper  public:
550*28f6c2f2SEnji Cooper   MockStack() = default;
551*28f6c2f2SEnji Cooper 
552*28f6c2f2SEnji Cooper   MOCK_METHOD(void, Push, (const T& elem), ());
553*28f6c2f2SEnji Cooper   MOCK_METHOD(void, Pop, (), (final));
554*28f6c2f2SEnji Cooper   MOCK_METHOD(int, GetSize, (), (const, override));
555*28f6c2f2SEnji Cooper   MOCK_METHOD(const T&, GetTop, (), (const));
556*28f6c2f2SEnji Cooper 
557*28f6c2f2SEnji Cooper   // Tests that the function return type can contain unprotected comma.
558*28f6c2f2SEnji Cooper   MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (), ());
559*28f6c2f2SEnji Cooper   MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (int), (const));
560*28f6c2f2SEnji Cooper 
561*28f6c2f2SEnji Cooper  private:
562*28f6c2f2SEnji Cooper   MockStack(const MockStack&) = delete;
563*28f6c2f2SEnji Cooper   MockStack& operator=(const MockStack&) = delete;
564*28f6c2f2SEnji Cooper };
565*28f6c2f2SEnji Cooper 
566*28f6c2f2SEnji Cooper template <typename T>
567*28f6c2f2SEnji Cooper class LegacyMockStack : public StackInterface<T> {
568*28f6c2f2SEnji Cooper  public:
569*28f6c2f2SEnji Cooper   LegacyMockStack() = default;
570*28f6c2f2SEnji Cooper 
571*28f6c2f2SEnji Cooper   MOCK_METHOD1_T(Push, void(const T& elem));
572*28f6c2f2SEnji Cooper   MOCK_METHOD0_T(Pop, void());
573*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD0_T(GetSize, int());  // NOLINT
574*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD0_T(GetTop, const T&());
575*28f6c2f2SEnji Cooper 
576*28f6c2f2SEnji Cooper   // Tests that the function return type can contain unprotected comma.
577*28f6c2f2SEnji Cooper   MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>());
578*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int));  // NOLINT
579*28f6c2f2SEnji Cooper 
580*28f6c2f2SEnji Cooper  private:
581*28f6c2f2SEnji Cooper   LegacyMockStack(const LegacyMockStack&) = delete;
582*28f6c2f2SEnji Cooper   LegacyMockStack& operator=(const LegacyMockStack&) = delete;
583*28f6c2f2SEnji Cooper };
584*28f6c2f2SEnji Cooper 
585*28f6c2f2SEnji Cooper template <typename T>
586*28f6c2f2SEnji Cooper class TemplateMockTest : public ::testing::Test {};
587*28f6c2f2SEnji Cooper using TemplateMockTestTypes =
588*28f6c2f2SEnji Cooper     ::testing::Types<MockStack<int>, LegacyMockStack<int>>;
589*28f6c2f2SEnji Cooper TYPED_TEST_SUITE(TemplateMockTest, TemplateMockTestTypes);
590*28f6c2f2SEnji Cooper 
591*28f6c2f2SEnji Cooper // Tests that template mock works.
TYPED_TEST(TemplateMockTest,Works)592*28f6c2f2SEnji Cooper TYPED_TEST(TemplateMockTest, Works) {
593*28f6c2f2SEnji Cooper   TypeParam mock;
594*28f6c2f2SEnji Cooper 
595*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, GetSize())
596*28f6c2f2SEnji Cooper       .WillOnce(Return(0))
597*28f6c2f2SEnji Cooper       .WillOnce(Return(1))
598*28f6c2f2SEnji Cooper       .WillOnce(Return(0));
599*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, Push(_));
600*28f6c2f2SEnji Cooper   int n = 5;
601*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, GetTop()).WillOnce(ReturnRef(n));
602*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, Pop()).Times(AnyNumber());
603*28f6c2f2SEnji Cooper 
604*28f6c2f2SEnji Cooper   EXPECT_EQ(0, mock.GetSize());
605*28f6c2f2SEnji Cooper   mock.Push(5);
606*28f6c2f2SEnji Cooper   EXPECT_EQ(1, mock.GetSize());
607*28f6c2f2SEnji Cooper   EXPECT_EQ(5, mock.GetTop());
608*28f6c2f2SEnji Cooper   mock.Pop();
609*28f6c2f2SEnji Cooper   EXPECT_EQ(0, mock.GetSize());
610*28f6c2f2SEnji Cooper }
611*28f6c2f2SEnji Cooper 
TYPED_TEST(TemplateMockTest,MethodWithCommaInReturnTypeWorks)612*28f6c2f2SEnji Cooper TYPED_TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
613*28f6c2f2SEnji Cooper   TypeParam mock;
614*28f6c2f2SEnji Cooper 
615*28f6c2f2SEnji Cooper   const std::map<int, int> a_map;
616*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, ReturnTypeWithComma()).WillOnce(Return(a_map));
617*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, ReturnTypeWithComma(1)).WillOnce(Return(a_map));
618*28f6c2f2SEnji Cooper 
619*28f6c2f2SEnji Cooper   EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
620*28f6c2f2SEnji Cooper   EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
621*28f6c2f2SEnji Cooper }
622*28f6c2f2SEnji Cooper 
623*28f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
624*28f6c2f2SEnji Cooper // Tests mocking template interfaces with calltype.
625*28f6c2f2SEnji Cooper 
626*28f6c2f2SEnji Cooper template <typename T>
627*28f6c2f2SEnji Cooper class StackInterfaceWithCallType {
628*28f6c2f2SEnji Cooper  public:
~StackInterfaceWithCallType()629*28f6c2f2SEnji Cooper   virtual ~StackInterfaceWithCallType() {}
630*28f6c2f2SEnji Cooper 
631*28f6c2f2SEnji Cooper   // Template parameter appears in function parameter.
632*28f6c2f2SEnji Cooper   STDMETHOD_(void, Push)(const T& value) = 0;
633*28f6c2f2SEnji Cooper   STDMETHOD_(void, Pop)() = 0;
634*28f6c2f2SEnji Cooper   STDMETHOD_(int, GetSize)() const = 0;
635*28f6c2f2SEnji Cooper   // Template parameter appears in function return type.
636*28f6c2f2SEnji Cooper   STDMETHOD_(const T&, GetTop)() const = 0;
637*28f6c2f2SEnji Cooper };
638*28f6c2f2SEnji Cooper 
639*28f6c2f2SEnji Cooper template <typename T>
640*28f6c2f2SEnji Cooper class MockStackWithCallType : public StackInterfaceWithCallType<T> {
641*28f6c2f2SEnji Cooper  public:
MockStackWithCallType()642*28f6c2f2SEnji Cooper   MockStackWithCallType() {}
643*28f6c2f2SEnji Cooper 
644*28f6c2f2SEnji Cooper   MOCK_METHOD(void, Push, (const T& elem),
645*28f6c2f2SEnji Cooper               (Calltype(STDMETHODCALLTYPE), override));
646*28f6c2f2SEnji Cooper   MOCK_METHOD(void, Pop, (), (Calltype(STDMETHODCALLTYPE), override));
647*28f6c2f2SEnji Cooper   MOCK_METHOD(int, GetSize, (), (Calltype(STDMETHODCALLTYPE), override, const));
648*28f6c2f2SEnji Cooper   MOCK_METHOD(const T&, GetTop, (),
649*28f6c2f2SEnji Cooper               (Calltype(STDMETHODCALLTYPE), override, const));
650*28f6c2f2SEnji Cooper 
651*28f6c2f2SEnji Cooper  private:
652*28f6c2f2SEnji Cooper   MockStackWithCallType(const MockStackWithCallType&) = delete;
653*28f6c2f2SEnji Cooper   MockStackWithCallType& operator=(const MockStackWithCallType&) = delete;
654*28f6c2f2SEnji Cooper };
655*28f6c2f2SEnji Cooper 
656*28f6c2f2SEnji Cooper template <typename T>
657*28f6c2f2SEnji Cooper class LegacyMockStackWithCallType : public StackInterfaceWithCallType<T> {
658*28f6c2f2SEnji Cooper  public:
LegacyMockStackWithCallType()659*28f6c2f2SEnji Cooper   LegacyMockStackWithCallType() {}
660*28f6c2f2SEnji Cooper 
661*28f6c2f2SEnji Cooper   MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem));
662*28f6c2f2SEnji Cooper   MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void());
663*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int());
664*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&());
665*28f6c2f2SEnji Cooper 
666*28f6c2f2SEnji Cooper  private:
667*28f6c2f2SEnji Cooper   LegacyMockStackWithCallType(const LegacyMockStackWithCallType&) = delete;
668*28f6c2f2SEnji Cooper   LegacyMockStackWithCallType& operator=(const LegacyMockStackWithCallType&) =
669*28f6c2f2SEnji Cooper       delete;
670*28f6c2f2SEnji Cooper };
671*28f6c2f2SEnji Cooper 
672*28f6c2f2SEnji Cooper template <typename T>
673*28f6c2f2SEnji Cooper class TemplateMockTestWithCallType : public ::testing::Test {};
674*28f6c2f2SEnji Cooper using TemplateMockTestWithCallTypeTypes =
675*28f6c2f2SEnji Cooper     ::testing::Types<MockStackWithCallType<int>,
676*28f6c2f2SEnji Cooper                      LegacyMockStackWithCallType<int>>;
677*28f6c2f2SEnji Cooper TYPED_TEST_SUITE(TemplateMockTestWithCallType,
678*28f6c2f2SEnji Cooper                  TemplateMockTestWithCallTypeTypes);
679*28f6c2f2SEnji Cooper 
680*28f6c2f2SEnji Cooper // Tests that template mock with calltype works.
TYPED_TEST(TemplateMockTestWithCallType,Works)681*28f6c2f2SEnji Cooper TYPED_TEST(TemplateMockTestWithCallType, Works) {
682*28f6c2f2SEnji Cooper   TypeParam mock;
683*28f6c2f2SEnji Cooper 
684*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, GetSize())
685*28f6c2f2SEnji Cooper       .WillOnce(Return(0))
686*28f6c2f2SEnji Cooper       .WillOnce(Return(1))
687*28f6c2f2SEnji Cooper       .WillOnce(Return(0));
688*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, Push(_));
689*28f6c2f2SEnji Cooper   int n = 5;
690*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, GetTop()).WillOnce(ReturnRef(n));
691*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, Pop()).Times(AnyNumber());
692*28f6c2f2SEnji Cooper 
693*28f6c2f2SEnji Cooper   EXPECT_EQ(0, mock.GetSize());
694*28f6c2f2SEnji Cooper   mock.Push(5);
695*28f6c2f2SEnji Cooper   EXPECT_EQ(1, mock.GetSize());
696*28f6c2f2SEnji Cooper   EXPECT_EQ(5, mock.GetTop());
697*28f6c2f2SEnji Cooper   mock.Pop();
698*28f6c2f2SEnji Cooper   EXPECT_EQ(0, mock.GetSize());
699*28f6c2f2SEnji Cooper }
700*28f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS
701*28f6c2f2SEnji Cooper 
702*28f6c2f2SEnji Cooper #define MY_MOCK_METHODS1_                       \
703*28f6c2f2SEnji Cooper   MOCK_METHOD(void, Overloaded, ());            \
704*28f6c2f2SEnji Cooper   MOCK_METHOD(int, Overloaded, (int), (const)); \
705*28f6c2f2SEnji Cooper   MOCK_METHOD(bool, Overloaded, (bool f, int n))
706*28f6c2f2SEnji Cooper 
707*28f6c2f2SEnji Cooper #define LEGACY_MY_MOCK_METHODS1_              \
708*28f6c2f2SEnji Cooper   MOCK_METHOD0(Overloaded, void());           \
709*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD1(Overloaded, int(int n)); \
710*28f6c2f2SEnji Cooper   MOCK_METHOD2(Overloaded, bool(bool f, int n))
711*28f6c2f2SEnji Cooper 
712*28f6c2f2SEnji Cooper class MockOverloadedOnArgNumber {
713*28f6c2f2SEnji Cooper  public:
714*28f6c2f2SEnji Cooper   MockOverloadedOnArgNumber() = default;
715*28f6c2f2SEnji Cooper 
716*28f6c2f2SEnji Cooper   MY_MOCK_METHODS1_;
717*28f6c2f2SEnji Cooper 
718*28f6c2f2SEnji Cooper  private:
719*28f6c2f2SEnji Cooper   MockOverloadedOnArgNumber(const MockOverloadedOnArgNumber&) = delete;
720*28f6c2f2SEnji Cooper   MockOverloadedOnArgNumber& operator=(const MockOverloadedOnArgNumber&) =
721*28f6c2f2SEnji Cooper       delete;
722*28f6c2f2SEnji Cooper };
723*28f6c2f2SEnji Cooper 
724*28f6c2f2SEnji Cooper class LegacyMockOverloadedOnArgNumber {
725*28f6c2f2SEnji Cooper  public:
726*28f6c2f2SEnji Cooper   LegacyMockOverloadedOnArgNumber() = default;
727*28f6c2f2SEnji Cooper 
728*28f6c2f2SEnji Cooper   LEGACY_MY_MOCK_METHODS1_;
729*28f6c2f2SEnji Cooper 
730*28f6c2f2SEnji Cooper  private:
731*28f6c2f2SEnji Cooper   LegacyMockOverloadedOnArgNumber(const LegacyMockOverloadedOnArgNumber&) =
732*28f6c2f2SEnji Cooper       delete;
733*28f6c2f2SEnji Cooper   LegacyMockOverloadedOnArgNumber& operator=(
734*28f6c2f2SEnji Cooper       const LegacyMockOverloadedOnArgNumber&) = delete;
735*28f6c2f2SEnji Cooper };
736*28f6c2f2SEnji Cooper 
737*28f6c2f2SEnji Cooper template <typename T>
738*28f6c2f2SEnji Cooper class OverloadedMockMethodTest : public ::testing::Test {};
739*28f6c2f2SEnji Cooper using OverloadedMockMethodTestTypes =
740*28f6c2f2SEnji Cooper     ::testing::Types<MockOverloadedOnArgNumber,
741*28f6c2f2SEnji Cooper                      LegacyMockOverloadedOnArgNumber>;
742*28f6c2f2SEnji Cooper TYPED_TEST_SUITE(OverloadedMockMethodTest, OverloadedMockMethodTestTypes);
743*28f6c2f2SEnji Cooper 
TYPED_TEST(OverloadedMockMethodTest,CanOverloadOnArgNumberInMacroBody)744*28f6c2f2SEnji Cooper TYPED_TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
745*28f6c2f2SEnji Cooper   TypeParam mock;
746*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, Overloaded());
747*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
748*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
749*28f6c2f2SEnji Cooper 
750*28f6c2f2SEnji Cooper   mock.Overloaded();
751*28f6c2f2SEnji Cooper   EXPECT_EQ(2, mock.Overloaded(1));
752*28f6c2f2SEnji Cooper   EXPECT_TRUE(mock.Overloaded(true, 1));
753*28f6c2f2SEnji Cooper }
754*28f6c2f2SEnji Cooper 
755*28f6c2f2SEnji Cooper #define MY_MOCK_METHODS2_                     \
756*28f6c2f2SEnji Cooper   MOCK_CONST_METHOD1(Overloaded, int(int n)); \
757*28f6c2f2SEnji Cooper   MOCK_METHOD1(Overloaded, int(int n))
758*28f6c2f2SEnji Cooper 
759*28f6c2f2SEnji Cooper class MockOverloadedOnConstness {
760*28f6c2f2SEnji Cooper  public:
761*28f6c2f2SEnji Cooper   MockOverloadedOnConstness() = default;
762*28f6c2f2SEnji Cooper 
763*28f6c2f2SEnji Cooper   MY_MOCK_METHODS2_;
764*28f6c2f2SEnji Cooper 
765*28f6c2f2SEnji Cooper  private:
766*28f6c2f2SEnji Cooper   MockOverloadedOnConstness(const MockOverloadedOnConstness&) = delete;
767*28f6c2f2SEnji Cooper   MockOverloadedOnConstness& operator=(const MockOverloadedOnConstness&) =
768*28f6c2f2SEnji Cooper       delete;
769*28f6c2f2SEnji Cooper };
770*28f6c2f2SEnji Cooper 
TEST(MockMethodOverloadedMockMethodTest,CanOverloadOnConstnessInMacroBody)771*28f6c2f2SEnji Cooper TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
772*28f6c2f2SEnji Cooper   MockOverloadedOnConstness mock;
773*28f6c2f2SEnji Cooper   const MockOverloadedOnConstness* const_mock = &mock;
774*28f6c2f2SEnji Cooper   EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
775*28f6c2f2SEnji Cooper   EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
776*28f6c2f2SEnji Cooper 
777*28f6c2f2SEnji Cooper   EXPECT_EQ(2, mock.Overloaded(1));
778*28f6c2f2SEnji Cooper   EXPECT_EQ(3, const_mock->Overloaded(1));
779*28f6c2f2SEnji Cooper }
780*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,WorksForVoidNullary)781*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, WorksForVoidNullary) {
782*28f6c2f2SEnji Cooper   MockFunction<void()> foo;
783*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call());
784*28f6c2f2SEnji Cooper   foo.Call();
785*28f6c2f2SEnji Cooper }
786*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,WorksForNonVoidNullary)787*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, WorksForNonVoidNullary) {
788*28f6c2f2SEnji Cooper   MockFunction<int()> foo;
789*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call()).WillOnce(Return(1)).WillOnce(Return(2));
790*28f6c2f2SEnji Cooper   EXPECT_EQ(1, foo.Call());
791*28f6c2f2SEnji Cooper   EXPECT_EQ(2, foo.Call());
792*28f6c2f2SEnji Cooper }
793*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,WorksForVoidUnary)794*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, WorksForVoidUnary) {
795*28f6c2f2SEnji Cooper   MockFunction<void(int)> foo;
796*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call(1));
797*28f6c2f2SEnji Cooper   foo.Call(1);
798*28f6c2f2SEnji Cooper }
799*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,WorksForNonVoidBinary)800*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, WorksForNonVoidBinary) {
801*28f6c2f2SEnji Cooper   MockFunction<int(bool, int)> foo;
802*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call(false, 42)).WillOnce(Return(1)).WillOnce(Return(2));
803*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call(true, Ge(100))).WillOnce(Return(3));
804*28f6c2f2SEnji Cooper   EXPECT_EQ(1, foo.Call(false, 42));
805*28f6c2f2SEnji Cooper   EXPECT_EQ(2, foo.Call(false, 42));
806*28f6c2f2SEnji Cooper   EXPECT_EQ(3, foo.Call(true, 120));
807*28f6c2f2SEnji Cooper }
808*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,WorksFor10Arguments)809*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, WorksFor10Arguments) {
810*28f6c2f2SEnji Cooper   MockFunction<int(bool a0, char a1, int a2, int a3, int a4, int a5, int a6,
811*28f6c2f2SEnji Cooper                    char a7, int a8, bool a9)>
812*28f6c2f2SEnji Cooper       foo;
813*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
814*28f6c2f2SEnji Cooper       .WillOnce(Return(1))
815*28f6c2f2SEnji Cooper       .WillOnce(Return(2));
816*28f6c2f2SEnji Cooper   EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
817*28f6c2f2SEnji Cooper   EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
818*28f6c2f2SEnji Cooper }
819*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,AsStdFunction)820*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, AsStdFunction) {
821*28f6c2f2SEnji Cooper   MockFunction<int(int)> foo;
822*28f6c2f2SEnji Cooper   auto call = [](const std::function<int(int)>& f, int i) { return f(i); };
823*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1));
824*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2));
825*28f6c2f2SEnji Cooper   EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
826*28f6c2f2SEnji Cooper   EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
827*28f6c2f2SEnji Cooper }
828*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,AsStdFunctionReturnsReference)829*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, AsStdFunctionReturnsReference) {
830*28f6c2f2SEnji Cooper   MockFunction<int&()> foo;
831*28f6c2f2SEnji Cooper   int value = 1;
832*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value));
833*28f6c2f2SEnji Cooper   int& ref = foo.AsStdFunction()();
834*28f6c2f2SEnji Cooper   EXPECT_EQ(1, ref);
835*28f6c2f2SEnji Cooper   value = 2;
836*28f6c2f2SEnji Cooper   EXPECT_EQ(2, ref);
837*28f6c2f2SEnji Cooper }
838*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,AsStdFunctionWithReferenceParameter)839*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) {
840*28f6c2f2SEnji Cooper   MockFunction<int(int&)> foo;
841*28f6c2f2SEnji Cooper   auto call = [](const std::function<int(int&)>& f, int& i) { return f(i); };
842*28f6c2f2SEnji Cooper   int i = 42;
843*28f6c2f2SEnji Cooper   EXPECT_CALL(foo, Call(i)).WillOnce(Return(-1));
844*28f6c2f2SEnji Cooper   EXPECT_EQ(-1, call(foo.AsStdFunction(), i));
845*28f6c2f2SEnji Cooper }
846*28f6c2f2SEnji Cooper 
847*28f6c2f2SEnji Cooper namespace {
848*28f6c2f2SEnji Cooper 
849*28f6c2f2SEnji Cooper template <typename Expected, typename F>
IsMockFunctionTemplateArgumentDeducedTo(const internal::MockFunction<F> &)850*28f6c2f2SEnji Cooper static constexpr bool IsMockFunctionTemplateArgumentDeducedTo(
851*28f6c2f2SEnji Cooper     const internal::MockFunction<F>&) {
852*28f6c2f2SEnji Cooper   return std::is_same<F, Expected>::value;
853*28f6c2f2SEnji Cooper }
854*28f6c2f2SEnji Cooper 
855*28f6c2f2SEnji Cooper }  // namespace
856*28f6c2f2SEnji Cooper 
857*28f6c2f2SEnji Cooper template <typename F>
858*28f6c2f2SEnji Cooper class MockMethodMockFunctionSignatureTest : public Test {};
859*28f6c2f2SEnji Cooper 
860*28f6c2f2SEnji Cooper using MockMethodMockFunctionSignatureTypes =
861*28f6c2f2SEnji Cooper     Types<void(), int(), void(int), int(int), int(bool, int),
862*28f6c2f2SEnji Cooper           int(bool, char, int, int, int, int, int, char, int, bool)>;
863*28f6c2f2SEnji Cooper TYPED_TEST_SUITE(MockMethodMockFunctionSignatureTest,
864*28f6c2f2SEnji Cooper                  MockMethodMockFunctionSignatureTypes);
865*28f6c2f2SEnji Cooper 
TYPED_TEST(MockMethodMockFunctionSignatureTest,IsMockFunctionTemplateArgumentDeducedForRawSignature)866*28f6c2f2SEnji Cooper TYPED_TEST(MockMethodMockFunctionSignatureTest,
867*28f6c2f2SEnji Cooper            IsMockFunctionTemplateArgumentDeducedForRawSignature) {
868*28f6c2f2SEnji Cooper   using Argument = TypeParam;
869*28f6c2f2SEnji Cooper   MockFunction<Argument> foo;
870*28f6c2f2SEnji Cooper   EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
871*28f6c2f2SEnji Cooper }
872*28f6c2f2SEnji Cooper 
TYPED_TEST(MockMethodMockFunctionSignatureTest,IsMockFunctionTemplateArgumentDeducedForStdFunction)873*28f6c2f2SEnji Cooper TYPED_TEST(MockMethodMockFunctionSignatureTest,
874*28f6c2f2SEnji Cooper            IsMockFunctionTemplateArgumentDeducedForStdFunction) {
875*28f6c2f2SEnji Cooper   using Argument = std::function<TypeParam>;
876*28f6c2f2SEnji Cooper   MockFunction<Argument> foo;
877*28f6c2f2SEnji Cooper   EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
878*28f6c2f2SEnji Cooper }
879*28f6c2f2SEnji Cooper 
TYPED_TEST(MockMethodMockFunctionSignatureTest,IsMockFunctionCallMethodSignatureTheSameForRawSignatureAndStdFunction)880*28f6c2f2SEnji Cooper TYPED_TEST(
881*28f6c2f2SEnji Cooper     MockMethodMockFunctionSignatureTest,
882*28f6c2f2SEnji Cooper     IsMockFunctionCallMethodSignatureTheSameForRawSignatureAndStdFunction) {
883*28f6c2f2SEnji Cooper   using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
884*28f6c2f2SEnji Cooper   using ForStdFunction =
885*28f6c2f2SEnji Cooper       decltype(&MockFunction<std::function<TypeParam>>::Call);
886*28f6c2f2SEnji Cooper   EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
887*28f6c2f2SEnji Cooper }
888*28f6c2f2SEnji Cooper 
889*28f6c2f2SEnji Cooper template <typename F>
890*28f6c2f2SEnji Cooper struct AlternateCallable {};
891*28f6c2f2SEnji Cooper 
TYPED_TEST(MockMethodMockFunctionSignatureTest,IsMockFunctionTemplateArgumentDeducedForAlternateCallable)892*28f6c2f2SEnji Cooper TYPED_TEST(MockMethodMockFunctionSignatureTest,
893*28f6c2f2SEnji Cooper            IsMockFunctionTemplateArgumentDeducedForAlternateCallable) {
894*28f6c2f2SEnji Cooper   using Argument = AlternateCallable<TypeParam>;
895*28f6c2f2SEnji Cooper   MockFunction<Argument> foo;
896*28f6c2f2SEnji Cooper   EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
897*28f6c2f2SEnji Cooper }
898*28f6c2f2SEnji Cooper 
TYPED_TEST(MockMethodMockFunctionSignatureTest,IsMockFunctionCallMethodSignatureTheSameForAlternateCallable)899*28f6c2f2SEnji Cooper TYPED_TEST(MockMethodMockFunctionSignatureTest,
900*28f6c2f2SEnji Cooper            IsMockFunctionCallMethodSignatureTheSameForAlternateCallable) {
901*28f6c2f2SEnji Cooper   using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
902*28f6c2f2SEnji Cooper   using ForStdFunction =
903*28f6c2f2SEnji Cooper       decltype(&MockFunction<std::function<TypeParam>>::Call);
904*28f6c2f2SEnji Cooper   EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
905*28f6c2f2SEnji Cooper }
906*28f6c2f2SEnji Cooper 
907*28f6c2f2SEnji Cooper struct MockMethodSizes0 {
908*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func, ());
909*28f6c2f2SEnji Cooper };
910*28f6c2f2SEnji Cooper struct MockMethodSizes1 {
911*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func, (int));
912*28f6c2f2SEnji Cooper };
913*28f6c2f2SEnji Cooper struct MockMethodSizes2 {
914*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func, (int, int));
915*28f6c2f2SEnji Cooper };
916*28f6c2f2SEnji Cooper struct MockMethodSizes3 {
917*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func, (int, int, int));
918*28f6c2f2SEnji Cooper };
919*28f6c2f2SEnji Cooper struct MockMethodSizes4 {
920*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func, (int, int, int, int));
921*28f6c2f2SEnji Cooper };
922*28f6c2f2SEnji Cooper 
923*28f6c2f2SEnji Cooper struct LegacyMockMethodSizes0 {
924*28f6c2f2SEnji Cooper   MOCK_METHOD0(func, void());
925*28f6c2f2SEnji Cooper };
926*28f6c2f2SEnji Cooper struct LegacyMockMethodSizes1 {
927*28f6c2f2SEnji Cooper   MOCK_METHOD1(func, void(int));
928*28f6c2f2SEnji Cooper };
929*28f6c2f2SEnji Cooper struct LegacyMockMethodSizes2 {
930*28f6c2f2SEnji Cooper   MOCK_METHOD2(func, void(int, int));
931*28f6c2f2SEnji Cooper };
932*28f6c2f2SEnji Cooper struct LegacyMockMethodSizes3 {
933*28f6c2f2SEnji Cooper   MOCK_METHOD3(func, void(int, int, int));
934*28f6c2f2SEnji Cooper };
935*28f6c2f2SEnji Cooper struct LegacyMockMethodSizes4 {
936*28f6c2f2SEnji Cooper   MOCK_METHOD4(func, void(int, int, int, int));
937*28f6c2f2SEnji Cooper };
938*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,MockMethodSizeOverhead)939*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) {
940*28f6c2f2SEnji Cooper   EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes1));
941*28f6c2f2SEnji Cooper   EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes2));
942*28f6c2f2SEnji Cooper   EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes3));
943*28f6c2f2SEnji Cooper   EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4));
944*28f6c2f2SEnji Cooper 
945*28f6c2f2SEnji Cooper   EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(LegacyMockMethodSizes1));
946*28f6c2f2SEnji Cooper   EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(LegacyMockMethodSizes2));
947*28f6c2f2SEnji Cooper   EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(LegacyMockMethodSizes3));
948*28f6c2f2SEnji Cooper   EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(LegacyMockMethodSizes4));
949*28f6c2f2SEnji Cooper 
950*28f6c2f2SEnji Cooper   EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(MockMethodSizes0));
951*28f6c2f2SEnji Cooper }
952*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,EnsureNoUnusedMemberFunction)953*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, EnsureNoUnusedMemberFunction) {
954*28f6c2f2SEnji Cooper #ifdef __clang__
955*28f6c2f2SEnji Cooper #pragma clang diagnostic push
956*28f6c2f2SEnji Cooper #pragma clang diagnostic error "-Wunused-member-function"
957*28f6c2f2SEnji Cooper #endif
958*28f6c2f2SEnji Cooper   // https://github.com/google/googletest/issues/4052
959*28f6c2f2SEnji Cooper   struct Foo {
960*28f6c2f2SEnji Cooper     MOCK_METHOD(void, foo, ());
961*28f6c2f2SEnji Cooper   };
962*28f6c2f2SEnji Cooper   EXPECT_CALL(Foo(), foo()).Times(0);
963*28f6c2f2SEnji Cooper #ifdef __clang__
964*28f6c2f2SEnji Cooper #pragma clang diagnostic pop
965*28f6c2f2SEnji Cooper #endif
966*28f6c2f2SEnji Cooper }
967*28f6c2f2SEnji Cooper 
968*28f6c2f2SEnji Cooper void hasTwoParams(int, int);
969*28f6c2f2SEnji Cooper void MaybeThrows();
970*28f6c2f2SEnji Cooper void DoesntThrow() noexcept;
971*28f6c2f2SEnji Cooper struct MockMethodNoexceptSpecifier {
972*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func1, (), (noexcept));
973*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func2, (), (noexcept(true)));
974*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func3, (), (noexcept(false)));
975*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func4, (), (noexcept(noexcept(MaybeThrows()))));
976*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func5, (), (noexcept(noexcept(DoesntThrow()))));
977*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func6, (), (noexcept(noexcept(DoesntThrow())), const));
978*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func7, (), (const, noexcept(noexcept(DoesntThrow()))));
979*28f6c2f2SEnji Cooper   // Put commas in the noexcept expression
980*28f6c2f2SEnji Cooper   MOCK_METHOD(void, func8, (), (noexcept(noexcept(hasTwoParams(1, 2))), const));
981*28f6c2f2SEnji Cooper };
982*28f6c2f2SEnji Cooper 
TEST(MockMethodMockFunctionTest,NoexceptSpecifierPreserved)983*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, NoexceptSpecifierPreserved) {
984*28f6c2f2SEnji Cooper   EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func1()));
985*28f6c2f2SEnji Cooper   EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func2()));
986*28f6c2f2SEnji Cooper   EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func3()));
987*28f6c2f2SEnji Cooper   EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func4()));
988*28f6c2f2SEnji Cooper   EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func5()));
989*28f6c2f2SEnji Cooper   EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func6()));
990*28f6c2f2SEnji Cooper   EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func7()));
991*28f6c2f2SEnji Cooper   EXPECT_EQ(noexcept(std::declval<MockMethodNoexceptSpecifier>().func8()),
992*28f6c2f2SEnji Cooper             noexcept(hasTwoParams(1, 2)));
993*28f6c2f2SEnji Cooper }
994*28f6c2f2SEnji Cooper 
995*28f6c2f2SEnji Cooper }  // namespace gmock_function_mocker_test
996*28f6c2f2SEnji Cooper }  // namespace testing
997*28f6c2f2SEnji Cooper 
998*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4503
999