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