blob: ea49b9c15402b92c77fb26535ed7ee93912098a3 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests the function mocker classes.
35
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock-generated-function-mockers.h"
shiqiane35fdd92008-12-10 05:08:54 +000037
zhanyong.wan652540a2009-02-23 23:37:29 +000038#if GTEST_OS_WINDOWS
shiqiane35fdd92008-12-10 05:08:54 +000039// MSDN says the header file to be included for STDMETHOD is BaseTyps.h but
40// we are getting compiler errors if we use basetyps.h, hence including
41// objbase.h for definition of STDMETHOD.
zhanyong.wan658ac0b2011-02-24 07:29:13 +000042# include <objbase.h>
shiqiane35fdd92008-12-10 05:08:54 +000043#endif // GTEST_OS_WINDOWS
44
jgm79a367e2012-04-10 16:02:11 +000045#include <map>
46#include <string>
47#include "gmock/gmock.h"
48#include "gtest/gtest.h"
49
shiqiane35fdd92008-12-10 05:08:54 +000050// There is a bug in MSVC (fixed in VS 2008) that prevents creating a
51// mock for a function with const arguments, so we don't test such
52// cases for MSVC versions older than 2008.
zhanyong.wan652540a2009-02-23 23:37:29 +000053#if !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
zhanyong.wan658ac0b2011-02-24 07:29:13 +000054# define GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
zhanyong.wan652540a2009-02-23 23:37:29 +000055#endif // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
shiqiane35fdd92008-12-10 05:08:54 +000056
57namespace testing {
58namespace gmock_generated_function_mockers_test {
59
60using testing::internal::string;
61using testing::_;
62using testing::A;
63using testing::An;
64using testing::AnyNumber;
65using testing::Const;
66using testing::DoDefault;
67using testing::Eq;
68using testing::Lt;
zhanyong.wanf3aa4d22009-09-25 22:34:47 +000069using testing::MockFunction;
shiqiane35fdd92008-12-10 05:08:54 +000070using testing::Ref;
71using testing::Return;
72using testing::ReturnRef;
73using testing::TypedEq;
74
75class FooInterface {
76 public:
77 virtual ~FooInterface() {}
78
79 virtual void VoidReturning(int x) = 0;
80
81 virtual int Nullary() = 0;
82 virtual bool Unary(int x) = 0;
83 virtual long Binary(short x, int y) = 0; // NOLINT
84 virtual int Decimal(bool b, char c, short d, int e, long f, // NOLINT
85 float g, double h, unsigned i, char* j, const string& k)
86 = 0;
87
88 virtual bool TakesNonConstReference(int& n) = 0; // NOLINT
89 virtual string TakesConstReference(const int& n) = 0;
90#ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
91 virtual bool TakesConst(const int x) = 0;
92#endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
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, string>& a_map) = 0;
105
zhanyong.wan652540a2009-02-23 23:37:29 +0000106#if GTEST_OS_WINDOWS
shiqiane35fdd92008-12-10 05:08:54 +0000107 STDMETHOD_(int, CTNullary)() = 0;
108 STDMETHOD_(bool, CTUnary)(int x) = 0;
109 STDMETHOD_(int, CTDecimal)(bool b, char c, short d, int e, long f, // NOLINT
110 float g, double h, unsigned i, char* j, const string& k) = 0;
111 STDMETHOD_(char, CTConst)(int x) const = 0;
112#endif // GTEST_OS_WINDOWS
113};
114
115class MockFoo : public FooInterface {
116 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000117 MockFoo() {}
118
shiqiane35fdd92008-12-10 05:08:54 +0000119 // Makes sure that a mock function parameter can be named.
120 MOCK_METHOD1(VoidReturning, void(int n)); // NOLINT
121
122 MOCK_METHOD0(Nullary, int()); // NOLINT
123
124 // Makes sure that a mock function parameter can be unnamed.
125 MOCK_METHOD1(Unary, bool(int)); // NOLINT
126 MOCK_METHOD2(Binary, long(short, int)); // NOLINT
127 MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float, // NOLINT
128 double, unsigned, char*, const string& str));
129
130 MOCK_METHOD1(TakesNonConstReference, bool(int&)); // NOLINT
131 MOCK_METHOD1(TakesConstReference, string(const int&));
zhanyong.wan20d1a232013-03-01 06:58:38 +0000132
shiqiane35fdd92008-12-10 05:08:54 +0000133#ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
134 MOCK_METHOD1(TakesConst, bool(const int)); // NOLINT
zhanyong.wan20d1a232013-03-01 06:58:38 +0000135#endif
136
137 // Tests that the function return type can contain unprotected comma.
138 MOCK_METHOD0(ReturnTypeWithComma, std::map<int, string>());
139 MOCK_CONST_METHOD1(ReturnTypeWithComma,
140 std::map<int, string>(int)); // NOLINT
141
shiqiane35fdd92008-12-10 05:08:54 +0000142 MOCK_METHOD0(OverloadedOnArgumentNumber, int()); // NOLINT
143 MOCK_METHOD1(OverloadedOnArgumentNumber, int(int)); // NOLINT
144
145 MOCK_METHOD1(OverloadedOnArgumentType, int(int)); // NOLINT
146 MOCK_METHOD1(OverloadedOnArgumentType, char(char)); // NOLINT
147
148 MOCK_METHOD0(OverloadedOnConstness, int()); // NOLINT
149 MOCK_CONST_METHOD0(OverloadedOnConstness, char()); // NOLINT
150
151 MOCK_METHOD1(TypeWithHole, int(int (*)())); // NOLINT
152 MOCK_METHOD1(TypeWithComma, int(const std::map<int, string>&)); // NOLINT
zhanyong.wan20d1a232013-03-01 06:58:38 +0000153
zhanyong.wan652540a2009-02-23 23:37:29 +0000154#if GTEST_OS_WINDOWS
shiqiane35fdd92008-12-10 05:08:54 +0000155 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
156 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int));
157 MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal, int(bool b, char c,
158 short d, int e, long f, float g, double h, unsigned i, char* j,
159 const string& k));
160 MOCK_CONST_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTConst, char(int));
zhanyong.wan20d1a232013-03-01 06:58:38 +0000161
162 // Tests that the function return type can contain unprotected comma.
163 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTReturnTypeWithComma,
164 std::map<int, string>());
shiqiane35fdd92008-12-10 05:08:54 +0000165#endif // GTEST_OS_WINDOWS
zhanyong.wan32de5f52009-12-23 00:13:23 +0000166
167 private:
168 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
shiqiane35fdd92008-12-10 05:08:54 +0000169};
170
171class FunctionMockerTest : public testing::Test {
172 protected:
173 FunctionMockerTest() : foo_(&mock_foo_) {}
174
175 FooInterface* const foo_;
176 MockFoo mock_foo_;
177};
178
179// Tests mocking a void-returning function.
180TEST_F(FunctionMockerTest, MocksVoidFunction) {
181 EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
182 foo_->VoidReturning(0);
183}
184
185// Tests mocking a nullary function.
186TEST_F(FunctionMockerTest, MocksNullaryFunction) {
187 EXPECT_CALL(mock_foo_, Nullary())
188 .WillOnce(DoDefault())
189 .WillOnce(Return(1));
190
191 EXPECT_EQ(0, foo_->Nullary());
192 EXPECT_EQ(1, foo_->Nullary());
193}
194
195// Tests mocking a unary function.
196TEST_F(FunctionMockerTest, MocksUnaryFunction) {
197 EXPECT_CALL(mock_foo_, Unary(Eq(2)))
198 .Times(2)
199 .WillOnce(Return(true));
200
201 EXPECT_TRUE(foo_->Unary(2));
202 EXPECT_FALSE(foo_->Unary(2));
203}
204
205// Tests mocking a binary function.
206TEST_F(FunctionMockerTest, MocksBinaryFunction) {
207 EXPECT_CALL(mock_foo_, Binary(2, _))
208 .WillOnce(Return(3));
209
210 EXPECT_EQ(3, foo_->Binary(2, 1));
211}
212
213// Tests mocking a decimal function.
214TEST_F(FunctionMockerTest, MocksDecimalFunction) {
215 EXPECT_CALL(mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(),
216 Lt(100), 5U, NULL, "hi"))
217 .WillOnce(Return(5));
218
219 EXPECT_EQ(5, foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
220}
221
222// Tests mocking a function that takes a non-const reference.
223TEST_F(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) {
224 int a = 0;
225 EXPECT_CALL(mock_foo_, TakesNonConstReference(Ref(a)))
226 .WillOnce(Return(true));
227
228 EXPECT_TRUE(foo_->TakesNonConstReference(a));
229}
230
231// Tests mocking a function that takes a const reference.
232TEST_F(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
233 int a = 0;
234 EXPECT_CALL(mock_foo_, TakesConstReference(Ref(a)))
235 .WillOnce(Return("Hello"));
236
237 EXPECT_EQ("Hello", foo_->TakesConstReference(a));
238}
239
240#ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
241// Tests mocking a function that takes a const variable.
242TEST_F(FunctionMockerTest, MocksFunctionWithConstArgument) {
243 EXPECT_CALL(mock_foo_, TakesConst(Lt(10)))
244 .WillOnce(DoDefault());
245
246 EXPECT_FALSE(foo_->TakesConst(5));
247}
248#endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
249
250// Tests mocking functions overloaded on the number of arguments.
251TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
252 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
253 .WillOnce(Return(1));
254 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber(_))
255 .WillOnce(Return(2));
256
257 EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
258 EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
259}
260
261// Tests mocking functions overloaded on the types of argument.
262TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
263 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
264 .WillOnce(Return(1));
265 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
266 .WillOnce(Return('b'));
267
268 EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
269 EXPECT_EQ('b', foo_->OverloadedOnArgumentType('a'));
270}
271
272// Tests mocking functions overloaded on the const-ness of this object.
273TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
274 EXPECT_CALL(mock_foo_, OverloadedOnConstness());
275 EXPECT_CALL(Const(mock_foo_), OverloadedOnConstness())
276 .WillOnce(Return('a'));
277
278 EXPECT_EQ(0, foo_->OverloadedOnConstness());
279 EXPECT_EQ('a', Const(*foo_).OverloadedOnConstness());
280}
281
zhanyong.wan20d1a232013-03-01 06:58:38 +0000282TEST_F(FunctionMockerTest, MocksReturnTypeWithComma) {
283 const std::map<int, string> a_map;
284 EXPECT_CALL(mock_foo_, ReturnTypeWithComma())
285 .WillOnce(Return(a_map));
286 EXPECT_CALL(mock_foo_, ReturnTypeWithComma(42))
287 .WillOnce(Return(a_map));
288
289 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma());
290 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
291}
292
zhanyong.wan652540a2009-02-23 23:37:29 +0000293#if GTEST_OS_WINDOWS
shiqiane35fdd92008-12-10 05:08:54 +0000294// Tests mocking a nullary function with calltype.
295TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
296 EXPECT_CALL(mock_foo_, CTNullary())
297 .WillOnce(Return(-1))
298 .WillOnce(Return(0));
299
300 EXPECT_EQ(-1, foo_->CTNullary());
301 EXPECT_EQ(0, foo_->CTNullary());
302}
303
304// Tests mocking a unary function with calltype.
305TEST_F(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
306 EXPECT_CALL(mock_foo_, CTUnary(Eq(2)))
307 .Times(2)
308 .WillOnce(Return(true))
309 .WillOnce(Return(false));
310
311 EXPECT_TRUE(foo_->CTUnary(2));
312 EXPECT_FALSE(foo_->CTUnary(2));
313}
314
315// Tests mocking a decimal function with calltype.
316TEST_F(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
317 EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
318 Lt(100), 5U, NULL, "hi"))
319 .WillOnce(Return(10));
320
321 EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
322}
323
324// Tests mocking functions overloaded on the const-ness of this object.
325TEST_F(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
326 EXPECT_CALL(Const(mock_foo_), CTConst(_))
327 .WillOnce(Return('a'));
328
329 EXPECT_EQ('a', Const(*foo_).CTConst(0));
330}
331
zhanyong.wan20d1a232013-03-01 06:58:38 +0000332TEST_F(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
333 const std::map<int, string> a_map;
334 EXPECT_CALL(mock_foo_, CTReturnTypeWithComma())
335 .WillOnce(Return(a_map));
336
337 EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
338}
339
shiqiane35fdd92008-12-10 05:08:54 +0000340#endif // GTEST_OS_WINDOWS
341
342class MockB {
343 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000344 MockB() {}
345
shiqiane35fdd92008-12-10 05:08:54 +0000346 MOCK_METHOD0(DoB, void());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000347
348 private:
349 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
shiqiane35fdd92008-12-10 05:08:54 +0000350};
351
352// Tests that functions with no EXPECT_CALL() ruls can be called any
353// number of times.
354TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
355 {
356 MockB b;
357 }
358
359 {
360 MockB b;
361 b.DoB();
362 }
363
364 {
365 MockB b;
366 b.DoB();
367 b.DoB();
368 }
369}
370
371// Tests mocking template interfaces.
372
373template <typename T>
374class StackInterface {
375 public:
376 virtual ~StackInterface() {}
377
378 // Template parameter appears in function parameter.
379 virtual void Push(const T& value) = 0;
380 virtual void Pop() = 0;
381 virtual int GetSize() const = 0;
382 // Template parameter appears in function return type.
383 virtual const T& GetTop() const = 0;
384};
385
386template <typename T>
387class MockStack : public StackInterface<T> {
388 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000389 MockStack() {}
390
shiqiane35fdd92008-12-10 05:08:54 +0000391 MOCK_METHOD1_T(Push, void(const T& elem));
392 MOCK_METHOD0_T(Pop, void());
393 MOCK_CONST_METHOD0_T(GetSize, int()); // NOLINT
394 MOCK_CONST_METHOD0_T(GetTop, const T&());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000395
zhanyong.wan20d1a232013-03-01 06:58:38 +0000396 // Tests that the function return type can contain unprotected comma.
397 MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>());
398 MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int)); // NOLINT
399
zhanyong.wan32de5f52009-12-23 00:13:23 +0000400 private:
401 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack);
shiqiane35fdd92008-12-10 05:08:54 +0000402};
403
404// Tests that template mock works.
405TEST(TemplateMockTest, Works) {
406 MockStack<int> mock;
407
408 EXPECT_CALL(mock, GetSize())
409 .WillOnce(Return(0))
410 .WillOnce(Return(1))
411 .WillOnce(Return(0));
412 EXPECT_CALL(mock, Push(_));
413 int n = 5;
414 EXPECT_CALL(mock, GetTop())
415 .WillOnce(ReturnRef(n));
416 EXPECT_CALL(mock, Pop())
417 .Times(AnyNumber());
418
419 EXPECT_EQ(0, mock.GetSize());
420 mock.Push(5);
421 EXPECT_EQ(1, mock.GetSize());
422 EXPECT_EQ(5, mock.GetTop());
423 mock.Pop();
424 EXPECT_EQ(0, mock.GetSize());
425}
426
zhanyong.wan20d1a232013-03-01 06:58:38 +0000427TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
428 MockStack<int> mock;
429
430 const std::map<int, int> a_map;
431 EXPECT_CALL(mock, ReturnTypeWithComma())
432 .WillOnce(Return(a_map));
433 EXPECT_CALL(mock, ReturnTypeWithComma(1))
434 .WillOnce(Return(a_map));
435
436 EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
437 EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
438}
439
zhanyong.wan652540a2009-02-23 23:37:29 +0000440#if GTEST_OS_WINDOWS
shiqiane35fdd92008-12-10 05:08:54 +0000441// Tests mocking template interfaces with calltype.
442
443template <typename T>
444class StackInterfaceWithCallType {
445 public:
446 virtual ~StackInterfaceWithCallType() {}
447
448 // Template parameter appears in function parameter.
449 STDMETHOD_(void, Push)(const T& value) = 0;
450 STDMETHOD_(void, Pop)() = 0;
451 STDMETHOD_(int, GetSize)() const = 0;
452 // Template parameter appears in function return type.
453 STDMETHOD_(const T&, GetTop)() const = 0;
454};
455
456template <typename T>
457class MockStackWithCallType : public StackInterfaceWithCallType<T> {
458 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000459 MockStackWithCallType() {}
460
shiqiane35fdd92008-12-10 05:08:54 +0000461 MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem));
462 MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void());
463 MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int());
464 MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000465
466 private:
467 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType);
shiqiane35fdd92008-12-10 05:08:54 +0000468};
469
470// Tests that template mock with calltype works.
471TEST(TemplateMockTestWithCallType, Works) {
472 MockStackWithCallType<int> mock;
473
474 EXPECT_CALL(mock, GetSize())
475 .WillOnce(Return(0))
476 .WillOnce(Return(1))
477 .WillOnce(Return(0));
478 EXPECT_CALL(mock, Push(_));
479 int n = 5;
480 EXPECT_CALL(mock, GetTop())
481 .WillOnce(ReturnRef(n));
482 EXPECT_CALL(mock, Pop())
483 .Times(AnyNumber());
484
485 EXPECT_EQ(0, mock.GetSize());
486 mock.Push(5);
487 EXPECT_EQ(1, mock.GetSize());
488 EXPECT_EQ(5, mock.GetTop());
489 mock.Pop();
490 EXPECT_EQ(0, mock.GetSize());
491}
492#endif // GTEST_OS_WINDOWS
493
zhanyong.wan68be1112009-03-25 03:56:48 +0000494#define MY_MOCK_METHODS1_ \
495 MOCK_METHOD0(Overloaded, void()); \
496 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
497 MOCK_METHOD2(Overloaded, bool(bool f, int n))
498
499class MockOverloadedOnArgNumber {
500 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000501 MockOverloadedOnArgNumber() {}
502
zhanyong.wan68be1112009-03-25 03:56:48 +0000503 MY_MOCK_METHODS1_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000504
505 private:
506 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber);
zhanyong.wan68be1112009-03-25 03:56:48 +0000507};
508
509TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
510 MockOverloadedOnArgNumber mock;
511 EXPECT_CALL(mock, Overloaded());
512 EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
513 EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
514
515 mock.Overloaded();
516 EXPECT_EQ(2, mock.Overloaded(1));
517 EXPECT_TRUE(mock.Overloaded(true, 1));
518}
519
520#define MY_MOCK_METHODS2_ \
521 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
522 MOCK_METHOD1(Overloaded, int(int n));
523
524class MockOverloadedOnConstness {
525 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000526 MockOverloadedOnConstness() {}
527
zhanyong.wan68be1112009-03-25 03:56:48 +0000528 MY_MOCK_METHODS2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000529
530 private:
531 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness);
zhanyong.wan68be1112009-03-25 03:56:48 +0000532};
533
534TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
535 MockOverloadedOnConstness mock;
536 const MockOverloadedOnConstness* const_mock = &mock;
537 EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
538 EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
539
540 EXPECT_EQ(2, mock.Overloaded(1));
541 EXPECT_EQ(3, const_mock->Overloaded(1));
542}
543
zhanyong.wanf3aa4d22009-09-25 22:34:47 +0000544TEST(MockFunctionTest, WorksForVoidNullary) {
545 MockFunction<void()> foo;
546 EXPECT_CALL(foo, Call());
547 foo.Call();
548}
549
550TEST(MockFunctionTest, WorksForNonVoidNullary) {
551 MockFunction<int()> foo;
552 EXPECT_CALL(foo, Call())
553 .WillOnce(Return(1))
554 .WillOnce(Return(2));
555 EXPECT_EQ(1, foo.Call());
556 EXPECT_EQ(2, foo.Call());
557}
558
559TEST(MockFunctionTest, WorksForVoidUnary) {
560 MockFunction<void(int)> foo;
561 EXPECT_CALL(foo, Call(1));
562 foo.Call(1);
563}
564
565TEST(MockFunctionTest, WorksForNonVoidBinary) {
566 MockFunction<int(bool, int)> foo;
567 EXPECT_CALL(foo, Call(false, 42))
568 .WillOnce(Return(1))
569 .WillOnce(Return(2));
570 EXPECT_CALL(foo, Call(true, Ge(100)))
571 .WillOnce(Return(3));
572 EXPECT_EQ(1, foo.Call(false, 42));
573 EXPECT_EQ(2, foo.Call(false, 42));
574 EXPECT_EQ(3, foo.Call(true, 120));
575}
576
577TEST(MockFunctionTest, WorksFor10Arguments) {
578 MockFunction<int(bool a0, char a1, int a2, int a3, int a4,
579 int a5, int a6, char a7, int a8, bool a9)> foo;
580 EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
581 .WillOnce(Return(1))
582 .WillOnce(Return(2));
583 EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
584 EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
585}
586
shiqiane35fdd92008-12-10 05:08:54 +0000587} // namespace gmock_generated_function_mockers_test
588} // namespace testing