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