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// 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
36#include "gmock/gmock-generated-function-mockers.h"
37
38#if GTEST_OS_WINDOWS
39// 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.
42# include <objbase.h>
43#endif  // GTEST_OS_WINDOWS
44
45#include <map>
46#include <string>
47#include "gmock/gmock.h"
48#include "gtest/gtest.h"
49
50// 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.
53#if !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
54# define GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
55#endif  // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
56
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;
69using testing::MockFunction;
70using 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
106#if GTEST_OS_WINDOWS
107  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:
117  MockFoo() {}
118
119  // 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&));
132#ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
133  MOCK_METHOD1(TakesConst, bool(const int));  // NOLINT
134#endif  // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
135  MOCK_METHOD0(OverloadedOnArgumentNumber, int());  // NOLINT
136  MOCK_METHOD1(OverloadedOnArgumentNumber, int(int));  // NOLINT
137
138  MOCK_METHOD1(OverloadedOnArgumentType, int(int));  // NOLINT
139  MOCK_METHOD1(OverloadedOnArgumentType, char(char));  // NOLINT
140
141  MOCK_METHOD0(OverloadedOnConstness, int());  // NOLINT
142  MOCK_CONST_METHOD0(OverloadedOnConstness, char());  // NOLINT
143
144  MOCK_METHOD1(TypeWithHole, int(int (*)()));  // NOLINT
145  MOCK_METHOD1(TypeWithComma, int(const std::map<int, string>&));  // NOLINT
146#if GTEST_OS_WINDOWS
147  MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
148  MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int));
149  MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal, int(bool b, char c,
150      short d, int e, long f, float g, double h, unsigned i, char* j,
151      const string& k));
152  MOCK_CONST_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTConst, char(int));
153#endif  // GTEST_OS_WINDOWS
154
155 private:
156  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
157};
158
159class FunctionMockerTest : public testing::Test {
160 protected:
161  FunctionMockerTest() : foo_(&mock_foo_) {}
162
163  FooInterface* const foo_;
164  MockFoo mock_foo_;
165};
166
167// Tests mocking a void-returning function.
168TEST_F(FunctionMockerTest, MocksVoidFunction) {
169  EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
170  foo_->VoidReturning(0);
171}
172
173// Tests mocking a nullary function.
174TEST_F(FunctionMockerTest, MocksNullaryFunction) {
175  EXPECT_CALL(mock_foo_, Nullary())
176      .WillOnce(DoDefault())
177      .WillOnce(Return(1));
178
179  EXPECT_EQ(0, foo_->Nullary());
180  EXPECT_EQ(1, foo_->Nullary());
181}
182
183// Tests mocking a unary function.
184TEST_F(FunctionMockerTest, MocksUnaryFunction) {
185  EXPECT_CALL(mock_foo_, Unary(Eq(2)))
186      .Times(2)
187      .WillOnce(Return(true));
188
189  EXPECT_TRUE(foo_->Unary(2));
190  EXPECT_FALSE(foo_->Unary(2));
191}
192
193// Tests mocking a binary function.
194TEST_F(FunctionMockerTest, MocksBinaryFunction) {
195  EXPECT_CALL(mock_foo_, Binary(2, _))
196      .WillOnce(Return(3));
197
198  EXPECT_EQ(3, foo_->Binary(2, 1));
199}
200
201// Tests mocking a decimal function.
202TEST_F(FunctionMockerTest, MocksDecimalFunction) {
203  EXPECT_CALL(mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(),
204                                 Lt(100), 5U, NULL, "hi"))
205      .WillOnce(Return(5));
206
207  EXPECT_EQ(5, foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
208}
209
210// Tests mocking a function that takes a non-const reference.
211TEST_F(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) {
212  int a = 0;
213  EXPECT_CALL(mock_foo_, TakesNonConstReference(Ref(a)))
214      .WillOnce(Return(true));
215
216  EXPECT_TRUE(foo_->TakesNonConstReference(a));
217}
218
219// Tests mocking a function that takes a const reference.
220TEST_F(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
221  int a = 0;
222  EXPECT_CALL(mock_foo_, TakesConstReference(Ref(a)))
223      .WillOnce(Return("Hello"));
224
225  EXPECT_EQ("Hello", foo_->TakesConstReference(a));
226}
227
228#ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
229// Tests mocking a function that takes a const variable.
230TEST_F(FunctionMockerTest, MocksFunctionWithConstArgument) {
231  EXPECT_CALL(mock_foo_, TakesConst(Lt(10)))
232      .WillOnce(DoDefault());
233
234  EXPECT_FALSE(foo_->TakesConst(5));
235}
236#endif  // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
237
238// Tests mocking functions overloaded on the number of arguments.
239TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
240  EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
241      .WillOnce(Return(1));
242  EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber(_))
243      .WillOnce(Return(2));
244
245  EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
246  EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
247}
248
249// Tests mocking functions overloaded on the types of argument.
250TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
251  EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
252      .WillOnce(Return(1));
253  EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
254      .WillOnce(Return('b'));
255
256  EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
257  EXPECT_EQ('b', foo_->OverloadedOnArgumentType('a'));
258}
259
260// Tests mocking functions overloaded on the const-ness of this object.
261TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
262  EXPECT_CALL(mock_foo_, OverloadedOnConstness());
263  EXPECT_CALL(Const(mock_foo_), OverloadedOnConstness())
264      .WillOnce(Return('a'));
265
266  EXPECT_EQ(0, foo_->OverloadedOnConstness());
267  EXPECT_EQ('a', Const(*foo_).OverloadedOnConstness());
268}
269
270#if GTEST_OS_WINDOWS
271// Tests mocking a nullary function with calltype.
272TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
273  EXPECT_CALL(mock_foo_, CTNullary())
274      .WillOnce(Return(-1))
275      .WillOnce(Return(0));
276
277  EXPECT_EQ(-1, foo_->CTNullary());
278  EXPECT_EQ(0, foo_->CTNullary());
279}
280
281// Tests mocking a unary function with calltype.
282TEST_F(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
283  EXPECT_CALL(mock_foo_, CTUnary(Eq(2)))
284      .Times(2)
285      .WillOnce(Return(true))
286      .WillOnce(Return(false));
287
288  EXPECT_TRUE(foo_->CTUnary(2));
289  EXPECT_FALSE(foo_->CTUnary(2));
290}
291
292// Tests mocking a decimal function with calltype.
293TEST_F(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
294  EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
295                                   Lt(100), 5U, NULL, "hi"))
296      .WillOnce(Return(10));
297
298  EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
299}
300
301// Tests mocking functions overloaded on the const-ness of this object.
302TEST_F(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
303  EXPECT_CALL(Const(mock_foo_), CTConst(_))
304      .WillOnce(Return('a'));
305
306  EXPECT_EQ('a', Const(*foo_).CTConst(0));
307}
308
309#endif  // GTEST_OS_WINDOWS
310
311class MockB {
312 public:
313  MockB() {}
314
315  MOCK_METHOD0(DoB, void());
316
317 private:
318  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
319};
320
321// Tests that functions with no EXPECT_CALL() ruls can be called any
322// number of times.
323TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
324  {
325    MockB b;
326  }
327
328  {
329    MockB b;
330    b.DoB();
331  }
332
333  {
334    MockB b;
335    b.DoB();
336    b.DoB();
337  }
338}
339
340// Tests mocking template interfaces.
341
342template <typename T>
343class StackInterface {
344 public:
345  virtual ~StackInterface() {}
346
347  // Template parameter appears in function parameter.
348  virtual void Push(const T& value) = 0;
349  virtual void Pop() = 0;
350  virtual int GetSize() const = 0;
351  // Template parameter appears in function return type.
352  virtual const T& GetTop() const = 0;
353};
354
355template <typename T>
356class MockStack : public StackInterface<T> {
357 public:
358  MockStack() {}
359
360  MOCK_METHOD1_T(Push, void(const T& elem));
361  MOCK_METHOD0_T(Pop, void());
362  MOCK_CONST_METHOD0_T(GetSize, int());  // NOLINT
363  MOCK_CONST_METHOD0_T(GetTop, const T&());
364
365 private:
366  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack);
367};
368
369// Tests that template mock works.
370TEST(TemplateMockTest, Works) {
371  MockStack<int> mock;
372
373  EXPECT_CALL(mock, GetSize())
374      .WillOnce(Return(0))
375      .WillOnce(Return(1))
376      .WillOnce(Return(0));
377  EXPECT_CALL(mock, Push(_));
378  int n = 5;
379  EXPECT_CALL(mock, GetTop())
380      .WillOnce(ReturnRef(n));
381  EXPECT_CALL(mock, Pop())
382      .Times(AnyNumber());
383
384  EXPECT_EQ(0, mock.GetSize());
385  mock.Push(5);
386  EXPECT_EQ(1, mock.GetSize());
387  EXPECT_EQ(5, mock.GetTop());
388  mock.Pop();
389  EXPECT_EQ(0, mock.GetSize());
390}
391
392#if GTEST_OS_WINDOWS
393// Tests mocking template interfaces with calltype.
394
395template <typename T>
396class StackInterfaceWithCallType {
397 public:
398  virtual ~StackInterfaceWithCallType() {}
399
400  // Template parameter appears in function parameter.
401  STDMETHOD_(void, Push)(const T& value) = 0;
402  STDMETHOD_(void, Pop)() = 0;
403  STDMETHOD_(int, GetSize)() const = 0;
404  // Template parameter appears in function return type.
405  STDMETHOD_(const T&, GetTop)() const = 0;
406};
407
408template <typename T>
409class MockStackWithCallType : public StackInterfaceWithCallType<T> {
410 public:
411  MockStackWithCallType() {}
412
413  MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem));
414  MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void());
415  MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int());
416  MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&());
417
418 private:
419  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType);
420};
421
422// Tests that template mock with calltype works.
423TEST(TemplateMockTestWithCallType, Works) {
424  MockStackWithCallType<int> mock;
425
426  EXPECT_CALL(mock, GetSize())
427      .WillOnce(Return(0))
428      .WillOnce(Return(1))
429      .WillOnce(Return(0));
430  EXPECT_CALL(mock, Push(_));
431  int n = 5;
432  EXPECT_CALL(mock, GetTop())
433      .WillOnce(ReturnRef(n));
434  EXPECT_CALL(mock, Pop())
435      .Times(AnyNumber());
436
437  EXPECT_EQ(0, mock.GetSize());
438  mock.Push(5);
439  EXPECT_EQ(1, mock.GetSize());
440  EXPECT_EQ(5, mock.GetTop());
441  mock.Pop();
442  EXPECT_EQ(0, mock.GetSize());
443}
444#endif  // GTEST_OS_WINDOWS
445
446#define MY_MOCK_METHODS1_ \
447    MOCK_METHOD0(Overloaded, void()); \
448    MOCK_CONST_METHOD1(Overloaded, int(int n)); \
449    MOCK_METHOD2(Overloaded, bool(bool f, int n))
450
451class MockOverloadedOnArgNumber {
452 public:
453  MockOverloadedOnArgNumber() {}
454
455  MY_MOCK_METHODS1_;
456
457 private:
458  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber);
459};
460
461TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
462  MockOverloadedOnArgNumber mock;
463  EXPECT_CALL(mock, Overloaded());
464  EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
465  EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
466
467  mock.Overloaded();
468  EXPECT_EQ(2, mock.Overloaded(1));
469  EXPECT_TRUE(mock.Overloaded(true, 1));
470}
471
472#define MY_MOCK_METHODS2_ \
473    MOCK_CONST_METHOD1(Overloaded, int(int n)); \
474    MOCK_METHOD1(Overloaded, int(int n));
475
476class MockOverloadedOnConstness {
477 public:
478  MockOverloadedOnConstness() {}
479
480  MY_MOCK_METHODS2_;
481
482 private:
483  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness);
484};
485
486TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
487  MockOverloadedOnConstness mock;
488  const MockOverloadedOnConstness* const_mock = &mock;
489  EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
490  EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
491
492  EXPECT_EQ(2, mock.Overloaded(1));
493  EXPECT_EQ(3, const_mock->Overloaded(1));
494}
495
496TEST(MockFunctionTest, WorksForVoidNullary) {
497  MockFunction<void()> foo;
498  EXPECT_CALL(foo, Call());
499  foo.Call();
500}
501
502TEST(MockFunctionTest, WorksForNonVoidNullary) {
503  MockFunction<int()> foo;
504  EXPECT_CALL(foo, Call())
505      .WillOnce(Return(1))
506      .WillOnce(Return(2));
507  EXPECT_EQ(1, foo.Call());
508  EXPECT_EQ(2, foo.Call());
509}
510
511TEST(MockFunctionTest, WorksForVoidUnary) {
512  MockFunction<void(int)> foo;
513  EXPECT_CALL(foo, Call(1));
514  foo.Call(1);
515}
516
517TEST(MockFunctionTest, WorksForNonVoidBinary) {
518  MockFunction<int(bool, int)> foo;
519  EXPECT_CALL(foo, Call(false, 42))
520      .WillOnce(Return(1))
521      .WillOnce(Return(2));
522  EXPECT_CALL(foo, Call(true, Ge(100)))
523      .WillOnce(Return(3));
524  EXPECT_EQ(1, foo.Call(false, 42));
525  EXPECT_EQ(2, foo.Call(false, 42));
526  EXPECT_EQ(3, foo.Call(true, 120));
527}
528
529TEST(MockFunctionTest, WorksFor10Arguments) {
530  MockFunction<int(bool a0, char a1, int a2, int a3, int a4,
531                   int a5, int a6, char a7, int a8, bool a9)> foo;
532  EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
533      .WillOnce(Return(1))
534      .WillOnce(Return(2));
535  EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
536  EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
537}
538
539}  // namespace gmock_generated_function_mockers_test
540}  // namespace testing
541