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-generated-function-mockers.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 <map>
44 #include <string>
45 #include "gmock/gmock.h"
46 #include "gtest/gtest.h"
47
48 namespace testing {
49 namespace gmock_function_mocker_test {
50
51 using testing::_;
52 using testing::A;
53 using testing::An;
54 using testing::AnyNumber;
55 using testing::Const;
56 using testing::DoDefault;
57 using testing::Eq;
58 using testing::Lt;
59 using testing::MockFunction;
60 using testing::Ref;
61 using testing::Return;
62 using testing::ReturnRef;
63 using testing::TypedEq;
64
65 template<typename T>
66 class TemplatedCopyable {
67 public:
TemplatedCopyable()68 TemplatedCopyable() {}
69
70 template <typename U>
TemplatedCopyable(const U & other)71 TemplatedCopyable(const U& other) {} // NOLINT
72 };
73
74 class FooInterface {
75 public:
~FooInterface()76 virtual ~FooInterface() {}
77
78 virtual void VoidReturning(int x) = 0;
79
80 virtual int Nullary() = 0;
81 virtual bool Unary(int x) = 0;
82 virtual long Binary(short x, int y) = 0; // NOLINT
83 virtual int Decimal(bool b, char c, short d, int e, long f, // NOLINT
84 float g, double h, unsigned i, char* j,
85 const std::string& k) = 0;
86
87 virtual bool TakesNonConstReference(int& n) = 0; // NOLINT
88 virtual std::string TakesConstReference(const int& n) = 0;
89 virtual bool TakesConst(const int x) = 0;
90
91 virtual int OverloadedOnArgumentNumber() = 0;
92 virtual int OverloadedOnArgumentNumber(int n) = 0;
93
94 virtual int OverloadedOnArgumentType(int n) = 0;
95 virtual char OverloadedOnArgumentType(char c) = 0;
96
97 virtual int OverloadedOnConstness() = 0;
98 virtual char OverloadedOnConstness() const = 0;
99
100 virtual int TypeWithHole(int (*func)()) = 0;
101 virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0;
102 virtual int TypeWithTemplatedCopyCtor(const TemplatedCopyable<int>&) = 0;
103
104 #if GTEST_OS_WINDOWS
105 STDMETHOD_(int, CTNullary)() = 0;
106 STDMETHOD_(bool, CTUnary)(int x) = 0;
107 STDMETHOD_(int, CTDecimal)
108 (bool b, char c, short d, int e, long f, // NOLINT
109 float g, double h, unsigned i, char* j, const std::string& k) = 0;
110 STDMETHOD_(char, CTConst)(int x) const = 0;
111 #endif // GTEST_OS_WINDOWS
112 };
113
114 // Const qualifiers on arguments were once (incorrectly) considered
115 // significant in determining whether two virtual functions had the same
116 // signature. This was fixed in Visual Studio 2008. However, the compiler
117 // still emits a warning that alerts about this change in behavior.
118 #ifdef _MSC_VER
119 # pragma warning(push)
120 # pragma warning(disable : 4373)
121 #endif
122 class MockFoo : public FooInterface {
123 public:
MockFoo()124 MockFoo() {}
125
126 // Makes sure that a mock function parameter can be named.
127 MOCK_METHOD(void, VoidReturning, (int n)); // NOLINT
128
129 MOCK_METHOD(int, Nullary, ()); // NOLINT
130
131 // Makes sure that a mock function parameter can be unnamed.
132 MOCK_METHOD(bool, Unary, (int)); // NOLINT
133 MOCK_METHOD(long, Binary, (short, int)); // NOLINT
134 MOCK_METHOD(int, Decimal,
135 (bool, char, short, int, long, float, // NOLINT
136 double, unsigned, char*, const std::string& str),
137 (override));
138
139 MOCK_METHOD(bool, TakesNonConstReference, (int&)); // NOLINT
140 MOCK_METHOD(std::string, TakesConstReference, (const int&));
141 MOCK_METHOD(bool, TakesConst, (const int)); // NOLINT
142
143 // Tests that the function return type can contain unprotected comma.
144 MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (), ());
145 MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (int),
146 (const)); // NOLINT
147
148 MOCK_METHOD(int, OverloadedOnArgumentNumber, ()); // NOLINT
149 MOCK_METHOD(int, OverloadedOnArgumentNumber, (int)); // NOLINT
150
151 MOCK_METHOD(int, OverloadedOnArgumentType, (int)); // NOLINT
152 MOCK_METHOD(char, OverloadedOnArgumentType, (char)); // NOLINT
153
154 MOCK_METHOD(int, OverloadedOnConstness, (), (override)); // NOLINT
155 MOCK_METHOD(char, OverloadedOnConstness, (), (override, const)); // NOLINT
156
157 MOCK_METHOD(int, TypeWithHole, (int (*)()), ()); // NOLINT
158 MOCK_METHOD(int, TypeWithComma, ((const std::map<int, std::string>&)));
159 MOCK_METHOD(int, TypeWithTemplatedCopyCtor,
160 (const TemplatedCopyable<int>&)); // NOLINT
161
162 #if GTEST_OS_WINDOWS
163 MOCK_METHOD(int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
164 MOCK_METHOD(bool, CTUnary, (int), (Calltype(STDMETHODCALLTYPE)));
165 MOCK_METHOD(int, CTDecimal,
166 (bool b, char c, short d, int e, long f, float g, double h,
167 unsigned i, char* j, const std::string& k),
168 (Calltype(STDMETHODCALLTYPE)));
169 MOCK_METHOD(char, CTConst, (int), (const, Calltype(STDMETHODCALLTYPE)));
170 MOCK_METHOD((std::map<int, std::string>), CTReturnTypeWithComma, (),
171 (Calltype(STDMETHODCALLTYPE)));
172 #endif // GTEST_OS_WINDOWS
173
174 private:
175 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
176 };
177 #ifdef _MSC_VER
178 # pragma warning(pop)
179 #endif
180
181 class MockMethodFunctionMockerTest : public testing::Test {
182 protected:
MockMethodFunctionMockerTest()183 MockMethodFunctionMockerTest() : foo_(&mock_foo_) {}
184
185 FooInterface* const foo_;
186 MockFoo mock_foo_;
187 };
188
189 // Tests mocking a void-returning function.
TEST_F(MockMethodFunctionMockerTest,MocksVoidFunction)190 TEST_F(MockMethodFunctionMockerTest, MocksVoidFunction) {
191 EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
192 foo_->VoidReturning(0);
193 }
194
195 // Tests mocking a nullary function.
TEST_F(MockMethodFunctionMockerTest,MocksNullaryFunction)196 TEST_F(MockMethodFunctionMockerTest, MocksNullaryFunction) {
197 EXPECT_CALL(mock_foo_, Nullary())
198 .WillOnce(DoDefault())
199 .WillOnce(Return(1));
200
201 EXPECT_EQ(0, foo_->Nullary());
202 EXPECT_EQ(1, foo_->Nullary());
203 }
204
205 // Tests mocking a unary function.
TEST_F(MockMethodFunctionMockerTest,MocksUnaryFunction)206 TEST_F(MockMethodFunctionMockerTest, MocksUnaryFunction) {
207 EXPECT_CALL(mock_foo_, Unary(Eq(2)))
208 .Times(2)
209 .WillOnce(Return(true));
210
211 EXPECT_TRUE(foo_->Unary(2));
212 EXPECT_FALSE(foo_->Unary(2));
213 }
214
215 // Tests mocking a binary function.
TEST_F(MockMethodFunctionMockerTest,MocksBinaryFunction)216 TEST_F(MockMethodFunctionMockerTest, MocksBinaryFunction) {
217 EXPECT_CALL(mock_foo_, Binary(2, _))
218 .WillOnce(Return(3));
219
220 EXPECT_EQ(3, foo_->Binary(2, 1));
221 }
222
223 // Tests mocking a decimal function.
TEST_F(MockMethodFunctionMockerTest,MocksDecimalFunction)224 TEST_F(MockMethodFunctionMockerTest, MocksDecimalFunction) {
225 EXPECT_CALL(mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(),
226 Lt(100), 5U, NULL, "hi"))
227 .WillOnce(Return(5));
228
229 EXPECT_EQ(5, foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, nullptr, "hi"));
230 }
231
232 // Tests mocking a function that takes a non-const reference.
TEST_F(MockMethodFunctionMockerTest,MocksFunctionWithNonConstReferenceArgument)233 TEST_F(MockMethodFunctionMockerTest,
234 MocksFunctionWithNonConstReferenceArgument) {
235 int a = 0;
236 EXPECT_CALL(mock_foo_, TakesNonConstReference(Ref(a)))
237 .WillOnce(Return(true));
238
239 EXPECT_TRUE(foo_->TakesNonConstReference(a));
240 }
241
242 // Tests mocking a function that takes a const reference.
TEST_F(MockMethodFunctionMockerTest,MocksFunctionWithConstReferenceArgument)243 TEST_F(MockMethodFunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
244 int a = 0;
245 EXPECT_CALL(mock_foo_, TakesConstReference(Ref(a)))
246 .WillOnce(Return("Hello"));
247
248 EXPECT_EQ("Hello", foo_->TakesConstReference(a));
249 }
250
251 // Tests mocking a function that takes a const variable.
TEST_F(MockMethodFunctionMockerTest,MocksFunctionWithConstArgument)252 TEST_F(MockMethodFunctionMockerTest, MocksFunctionWithConstArgument) {
253 EXPECT_CALL(mock_foo_, TakesConst(Lt(10)))
254 .WillOnce(DoDefault());
255
256 EXPECT_FALSE(foo_->TakesConst(5));
257 }
258
259 // Tests mocking functions overloaded on the number of arguments.
TEST_F(MockMethodFunctionMockerTest,MocksFunctionsOverloadedOnArgumentNumber)260 TEST_F(MockMethodFunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
261 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
262 .WillOnce(Return(1));
263 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber(_))
264 .WillOnce(Return(2));
265
266 EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
267 EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
268 }
269
270 // Tests mocking functions overloaded on the types of argument.
TEST_F(MockMethodFunctionMockerTest,MocksFunctionsOverloadedOnArgumentType)271 TEST_F(MockMethodFunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
272 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
273 .WillOnce(Return(1));
274 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
275 .WillOnce(Return('b'));
276
277 EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
278 EXPECT_EQ('b', foo_->OverloadedOnArgumentType('a'));
279 }
280
281 // Tests mocking functions overloaded on the const-ness of this object.
TEST_F(MockMethodFunctionMockerTest,MocksFunctionsOverloadedOnConstnessOfThis)282 TEST_F(MockMethodFunctionMockerTest,
283 MocksFunctionsOverloadedOnConstnessOfThis) {
284 EXPECT_CALL(mock_foo_, OverloadedOnConstness());
285 EXPECT_CALL(Const(mock_foo_), OverloadedOnConstness())
286 .WillOnce(Return('a'));
287
288 EXPECT_EQ(0, foo_->OverloadedOnConstness());
289 EXPECT_EQ('a', Const(*foo_).OverloadedOnConstness());
290 }
291
TEST_F(MockMethodFunctionMockerTest,MocksReturnTypeWithComma)292 TEST_F(MockMethodFunctionMockerTest, MocksReturnTypeWithComma) {
293 const std::map<int, std::string> a_map;
294 EXPECT_CALL(mock_foo_, ReturnTypeWithComma())
295 .WillOnce(Return(a_map));
296 EXPECT_CALL(mock_foo_, ReturnTypeWithComma(42))
297 .WillOnce(Return(a_map));
298
299 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma());
300 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
301 }
302
TEST_F(MockMethodFunctionMockerTest,MocksTypeWithTemplatedCopyCtor)303 TEST_F(MockMethodFunctionMockerTest, MocksTypeWithTemplatedCopyCtor) {
304 EXPECT_CALL(mock_foo_, TypeWithTemplatedCopyCtor(_)).WillOnce(Return(true));
305 EXPECT_TRUE(foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>()));
306 }
307
308 #if GTEST_OS_WINDOWS
309 // Tests mocking a nullary function with calltype.
TEST_F(MockMethodFunctionMockerTest,MocksNullaryFunctionWithCallType)310 TEST_F(MockMethodFunctionMockerTest, MocksNullaryFunctionWithCallType) {
311 EXPECT_CALL(mock_foo_, CTNullary())
312 .WillOnce(Return(-1))
313 .WillOnce(Return(0));
314
315 EXPECT_EQ(-1, foo_->CTNullary());
316 EXPECT_EQ(0, foo_->CTNullary());
317 }
318
319 // Tests mocking a unary function with calltype.
TEST_F(MockMethodFunctionMockerTest,MocksUnaryFunctionWithCallType)320 TEST_F(MockMethodFunctionMockerTest, MocksUnaryFunctionWithCallType) {
321 EXPECT_CALL(mock_foo_, CTUnary(Eq(2)))
322 .Times(2)
323 .WillOnce(Return(true))
324 .WillOnce(Return(false));
325
326 EXPECT_TRUE(foo_->CTUnary(2));
327 EXPECT_FALSE(foo_->CTUnary(2));
328 }
329
330 // Tests mocking a decimal function with calltype.
TEST_F(MockMethodFunctionMockerTest,MocksDecimalFunctionWithCallType)331 TEST_F(MockMethodFunctionMockerTest, MocksDecimalFunctionWithCallType) {
332 EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
333 Lt(100), 5U, NULL, "hi"))
334 .WillOnce(Return(10));
335
336 EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
337 }
338
339 // Tests mocking functions overloaded on the const-ness of this object.
TEST_F(MockMethodFunctionMockerTest,MocksFunctionsConstFunctionWithCallType)340 TEST_F(MockMethodFunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
341 EXPECT_CALL(Const(mock_foo_), CTConst(_))
342 .WillOnce(Return('a'));
343
344 EXPECT_EQ('a', Const(*foo_).CTConst(0));
345 }
346
TEST_F(MockMethodFunctionMockerTest,MocksReturnTypeWithCommaAndCallType)347 TEST_F(MockMethodFunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
348 const std::map<int, std::string> a_map;
349 EXPECT_CALL(mock_foo_, CTReturnTypeWithComma())
350 .WillOnce(Return(a_map));
351
352 EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
353 }
354
355 #endif // GTEST_OS_WINDOWS
356
357 class MockB {
358 public:
MockB()359 MockB() {}
360
361 MOCK_METHOD(void, DoB, ());
362
363 private:
364 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
365 };
366
367 // Tests that functions with no EXPECT_CALL() rules can be called any
368 // number of times.
TEST(MockMethodExpectCallTest,UnmentionedFunctionCanBeCalledAnyNumberOfTimes)369 TEST(MockMethodExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
370 {
371 MockB b;
372 }
373
374 {
375 MockB b;
376 b.DoB();
377 }
378
379 {
380 MockB b;
381 b.DoB();
382 b.DoB();
383 }
384 }
385
386 // Tests mocking template interfaces.
387
388 template <typename T>
389 class StackInterface {
390 public:
~StackInterface()391 virtual ~StackInterface() {}
392
393 // Template parameter appears in function parameter.
394 virtual void Push(const T& value) = 0;
395 virtual void Pop() = 0;
396 virtual int GetSize() const = 0;
397 // Template parameter appears in function return type.
398 virtual const T& GetTop() const = 0;
399 };
400
401 template <typename T>
402 class MockStack : public StackInterface<T> {
403 public:
MockStack()404 MockStack() {}
405
406 MOCK_METHOD(void, Push, (const T& elem), ());
407 MOCK_METHOD(void, Pop, (), (final));
408 MOCK_METHOD(int, GetSize, (), (const, override));
409 MOCK_METHOD(const T&, GetTop, (), (const));
410
411 // Tests that the function return type can contain unprotected comma.
412 MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (), ());
413 MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (int), (const));
414
415 private:
416 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack);
417 };
418
419 // Tests that template mock works.
TEST(MockMethodTemplateMockTest,Works)420 TEST(MockMethodTemplateMockTest, Works) {
421 MockStack<int> mock;
422
423 EXPECT_CALL(mock, GetSize())
424 .WillOnce(Return(0))
425 .WillOnce(Return(1))
426 .WillOnce(Return(0));
427 EXPECT_CALL(mock, Push(_));
428 int n = 5;
429 EXPECT_CALL(mock, GetTop())
430 .WillOnce(ReturnRef(n));
431 EXPECT_CALL(mock, Pop())
432 .Times(AnyNumber());
433
434 EXPECT_EQ(0, mock.GetSize());
435 mock.Push(5);
436 EXPECT_EQ(1, mock.GetSize());
437 EXPECT_EQ(5, mock.GetTop());
438 mock.Pop();
439 EXPECT_EQ(0, mock.GetSize());
440 }
441
TEST(MockMethodTemplateMockTest,MethodWithCommaInReturnTypeWorks)442 TEST(MockMethodTemplateMockTest, MethodWithCommaInReturnTypeWorks) {
443 MockStack<int> mock;
444
445 const std::map<int, int> a_map;
446 EXPECT_CALL(mock, ReturnTypeWithComma())
447 .WillOnce(Return(a_map));
448 EXPECT_CALL(mock, ReturnTypeWithComma(1))
449 .WillOnce(Return(a_map));
450
451 EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
452 EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
453 }
454
455 #if GTEST_OS_WINDOWS
456 // Tests mocking template interfaces with calltype.
457
458 template <typename T>
459 class StackInterfaceWithCallType {
460 public:
~StackInterfaceWithCallType()461 virtual ~StackInterfaceWithCallType() {}
462
463 // Template parameter appears in function parameter.
464 STDMETHOD_(void, Push)(const T& value) = 0;
465 STDMETHOD_(void, Pop)() = 0;
466 STDMETHOD_(int, GetSize)() const = 0;
467 // Template parameter appears in function return type.
468 STDMETHOD_(const T&, GetTop)() const = 0;
469 };
470
471 template <typename T>
472 class MockStackWithCallType : public StackInterfaceWithCallType<T> {
473 public:
MockStackWithCallType()474 MockStackWithCallType() {}
475
476 MOCK_METHOD(void, Push, (const T& elem),
477 (Calltype(STDMETHODCALLTYPE), override));
478 MOCK_METHOD(void, Pop, (), (Calltype(STDMETHODCALLTYPE), override));
479 MOCK_METHOD(int, GetSize, (), (Calltype(STDMETHODCALLTYPE), override, const));
480 MOCK_METHOD(const T&, GetTop, (),
481 (Calltype(STDMETHODCALLTYPE), override, const));
482
483 private:
484 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType);
485 };
486
487 // Tests that template mock with calltype works.
TEST(MockMethodTemplateMockTestWithCallType,Works)488 TEST(MockMethodTemplateMockTestWithCallType, Works) {
489 MockStackWithCallType<int> mock;
490
491 EXPECT_CALL(mock, GetSize())
492 .WillOnce(Return(0))
493 .WillOnce(Return(1))
494 .WillOnce(Return(0));
495 EXPECT_CALL(mock, Push(_));
496 int n = 5;
497 EXPECT_CALL(mock, GetTop())
498 .WillOnce(ReturnRef(n));
499 EXPECT_CALL(mock, Pop())
500 .Times(AnyNumber());
501
502 EXPECT_EQ(0, mock.GetSize());
503 mock.Push(5);
504 EXPECT_EQ(1, mock.GetSize());
505 EXPECT_EQ(5, mock.GetTop());
506 mock.Pop();
507 EXPECT_EQ(0, mock.GetSize());
508 }
509 #endif // GTEST_OS_WINDOWS
510
511 #define MY_MOCK_METHODS1_ \
512 MOCK_METHOD(void, Overloaded, ()); \
513 MOCK_METHOD(int, Overloaded, (int), (const)); \
514 MOCK_METHOD(bool, Overloaded, (bool f, int n))
515
516 class MockOverloadedOnArgNumber {
517 public:
MockOverloadedOnArgNumber()518 MockOverloadedOnArgNumber() {}
519
520 MY_MOCK_METHODS1_;
521
522 private:
523 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber);
524 };
525
TEST(MockMethodOverloadedMockMethodTest,CanOverloadOnArgNumberInMacroBody)526 TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
527 MockOverloadedOnArgNumber mock;
528 EXPECT_CALL(mock, Overloaded());
529 EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
530 EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
531
532 mock.Overloaded();
533 EXPECT_EQ(2, mock.Overloaded(1));
534 EXPECT_TRUE(mock.Overloaded(true, 1));
535 }
536
537 #define MY_MOCK_METHODS2_ \
538 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
539 MOCK_METHOD1(Overloaded, int(int n))
540
541 class MockOverloadedOnConstness {
542 public:
MockOverloadedOnConstness()543 MockOverloadedOnConstness() {}
544
545 MY_MOCK_METHODS2_;
546
547 private:
548 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness);
549 };
550
TEST(MockMethodOverloadedMockMethodTest,CanOverloadOnConstnessInMacroBody)551 TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
552 MockOverloadedOnConstness mock;
553 const MockOverloadedOnConstness* const_mock = &mock;
554 EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
555 EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
556
557 EXPECT_EQ(2, mock.Overloaded(1));
558 EXPECT_EQ(3, const_mock->Overloaded(1));
559 }
560
TEST(MockMethodMockFunctionTest,WorksForVoidNullary)561 TEST(MockMethodMockFunctionTest, WorksForVoidNullary) {
562 MockFunction<void()> foo;
563 EXPECT_CALL(foo, Call());
564 foo.Call();
565 }
566
TEST(MockMethodMockFunctionTest,WorksForNonVoidNullary)567 TEST(MockMethodMockFunctionTest, WorksForNonVoidNullary) {
568 MockFunction<int()> foo;
569 EXPECT_CALL(foo, Call())
570 .WillOnce(Return(1))
571 .WillOnce(Return(2));
572 EXPECT_EQ(1, foo.Call());
573 EXPECT_EQ(2, foo.Call());
574 }
575
TEST(MockMethodMockFunctionTest,WorksForVoidUnary)576 TEST(MockMethodMockFunctionTest, WorksForVoidUnary) {
577 MockFunction<void(int)> foo;
578 EXPECT_CALL(foo, Call(1));
579 foo.Call(1);
580 }
581
TEST(MockMethodMockFunctionTest,WorksForNonVoidBinary)582 TEST(MockMethodMockFunctionTest, WorksForNonVoidBinary) {
583 MockFunction<int(bool, int)> foo;
584 EXPECT_CALL(foo, Call(false, 42))
585 .WillOnce(Return(1))
586 .WillOnce(Return(2));
587 EXPECT_CALL(foo, Call(true, Ge(100)))
588 .WillOnce(Return(3));
589 EXPECT_EQ(1, foo.Call(false, 42));
590 EXPECT_EQ(2, foo.Call(false, 42));
591 EXPECT_EQ(3, foo.Call(true, 120));
592 }
593
TEST(MockMethodMockFunctionTest,WorksFor10Arguments)594 TEST(MockMethodMockFunctionTest, WorksFor10Arguments) {
595 MockFunction<int(bool a0, char a1, int a2, int a3, int a4,
596 int a5, int a6, char a7, int a8, bool a9)> foo;
597 EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
598 .WillOnce(Return(1))
599 .WillOnce(Return(2));
600 EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
601 EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
602 }
603
TEST(MockMethodMockFunctionTest,AsStdFunction)604 TEST(MockMethodMockFunctionTest, AsStdFunction) {
605 MockFunction<int(int)> foo;
606 auto call = [](const std::function<int(int)> &f, int i) {
607 return f(i);
608 };
609 EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1));
610 EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2));
611 EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
612 EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
613 }
614
TEST(MockMethodMockFunctionTest,AsStdFunctionReturnsReference)615 TEST(MockMethodMockFunctionTest, AsStdFunctionReturnsReference) {
616 MockFunction<int&()> foo;
617 int value = 1;
618 EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value));
619 int& ref = foo.AsStdFunction()();
620 EXPECT_EQ(1, ref);
621 value = 2;
622 EXPECT_EQ(2, ref);
623 }
624
TEST(MockMethodMockFunctionTest,AsStdFunctionWithReferenceParameter)625 TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) {
626 MockFunction<int(int &)> foo;
627 auto call = [](const std::function<int(int& )> &f, int &i) {
628 return f(i);
629 };
630 int i = 42;
631 EXPECT_CALL(foo, Call(i)).WillOnce(Return(-1));
632 EXPECT_EQ(-1, call(foo.AsStdFunction(), i));
633 }
634
635
636 struct MockMethodSizes0 {
637 MOCK_METHOD(void, func, ());
638 };
639 struct MockMethodSizes1 {
640 MOCK_METHOD(void, func, (int));
641 };
642 struct MockMethodSizes2 {
643 MOCK_METHOD(void, func, (int, int));
644 };
645 struct MockMethodSizes3 {
646 MOCK_METHOD(void, func, (int, int, int));
647 };
648 struct MockMethodSizes4 {
649 MOCK_METHOD(void, func, (int, int, int, int));
650 };
651
TEST(MockMethodMockFunctionTest,MockMethodSizeOverhead)652 TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) {
653 EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes1));
654 EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes2));
655 EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes3));
656 EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4));
657 }
658
659 } // namespace gmock_function_mocker_test
660 } // namespace testing
661