Sacado Package Browser (Single Doxygen Collection)  Version of the Day
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gmock-function-mocker_test.cc
Go to the documentation of this file.
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 // Google Mock - a framework for writing C++ mock classes.
31 //
32 // This file tests the function mocker classes.
34 
35 // Silence C4503 (decorated name length exceeded) for MSVC.
37 
38 #ifdef 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 <functional>
46 #include <map>
47 #include <string>
48 #include <type_traits>
49 
50 #include "gmock/gmock.h"
51 #include "gtest/gtest.h"
52 
53 namespace testing {
54 namespace gmock_function_mocker_test {
55 
56 using testing::_;
57 using testing::A;
58 using testing::An;
59 using testing::AnyNumber;
60 using testing::Const;
61 using testing::DoDefault;
62 using testing::Eq;
63 using testing::Lt;
64 using testing::MockFunction;
65 using testing::Ref;
66 using testing::Return;
67 using testing::ReturnRef;
68 using testing::TypedEq;
69 
70 template <typename T>
72  public:
73  TemplatedCopyable() = default;
74 
75  template <typename U>
76  TemplatedCopyable(const U& other) {} // NOLINT
77 };
78 
79 class FooInterface {
80  public:
81  virtual ~FooInterface() = default;
82 
83  virtual void VoidReturning(int x) = 0;
84 
85  virtual int Nullary() = 0;
86  virtual bool Unary(int x) = 0;
87  virtual long Binary(short x, int y) = 0; // NOLINT
88  virtual int Decimal(bool b, char c, short d, int e, long f, // NOLINT
89  float g, double h, unsigned i, char* j,
90  const std::string& k) = 0;
91 
92  virtual bool TakesNonConstReference(int& n) = 0; // NOLINT
93  virtual std::string TakesConstReference(const int& n) = 0;
94  virtual bool TakesConst(const int x) = 0;
95 
96  virtual int OverloadedOnArgumentNumber() = 0;
97  virtual int OverloadedOnArgumentNumber(int n) = 0;
98 
99  virtual int OverloadedOnArgumentType(int n) = 0;
100  virtual char OverloadedOnArgumentType(char c) = 0;
101 
102  virtual int OverloadedOnConstness() = 0;
103  virtual char OverloadedOnConstness() const = 0;
104 
105  virtual int TypeWithHole(int (*func)()) = 0;
106  virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0;
107  virtual int TypeWithTemplatedCopyCtor(const TemplatedCopyable<int>&) = 0;
108 
109  virtual int (*ReturnsFunctionPointer1(int))(bool) = 0;
110  using fn_ptr = int (*)(bool);
111  virtual fn_ptr ReturnsFunctionPointer2(int) = 0;
112 
113  virtual int RefQualifiedConstRef() const& = 0;
114  virtual int RefQualifiedConstRefRef() const&& = 0;
115  virtual int RefQualifiedRef() & = 0;
116  virtual int RefQualifiedRefRef() && = 0;
117 
118  virtual int RefQualifiedOverloaded() const& = 0;
119  virtual int RefQualifiedOverloaded() const&& = 0;
120  virtual int RefQualifiedOverloaded() & = 0;
121  virtual int RefQualifiedOverloaded() && = 0;
122 
123 #ifdef GTEST_OS_WINDOWS
124  STDMETHOD_(int, CTNullary)() = 0;
125  STDMETHOD_(bool, CTUnary)(int x) = 0;
126  STDMETHOD_(int, CTDecimal)
127  (bool b, char c, short d, int e, long f, // NOLINT
128  float g, double h, unsigned i, char* j, const std::string& k) = 0;
129  STDMETHOD_(char, CTConst)(int x) const = 0;
130 #endif // GTEST_OS_WINDOWS
131 };
132 
133 // Const qualifiers on arguments were once (incorrectly) considered
134 // significant in determining whether two virtual functions had the same
135 // signature. This was fixed in Visual Studio 2008. However, the compiler
136 // still emits a warning that alerts about this change in behavior.
138 class MockFoo : public FooInterface {
139  public:
140  MockFoo() = default;
141 
142  // Makes sure that a mock function parameter can be named.
143  MOCK_METHOD(void, VoidReturning, (int n)); // NOLINT
144 
145  MOCK_METHOD(int, Nullary, ()); // NOLINT
146 
147  // Makes sure that a mock function parameter can be unnamed.
148  MOCK_METHOD(bool, Unary, (int)); // NOLINT
149  MOCK_METHOD(long, Binary, (short, int)); // NOLINT
150  MOCK_METHOD(int, Decimal,
151  (bool, char, short, int, long, float, // NOLINT
152  double, unsigned, char*, const std::string& str),
153  (override));
154 
155  MOCK_METHOD(bool, TakesNonConstReference, (int&)); // NOLINT
156  MOCK_METHOD(std::string, TakesConstReference, (const int&));
157  MOCK_METHOD(bool, TakesConst, (const int)); // NOLINT
158 
159  // Tests that the function return type can contain unprotected comma.
160  MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (), ());
161  MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (int),
162  (const)); // NOLINT
163 
164  MOCK_METHOD(int, OverloadedOnArgumentNumber, ()); // NOLINT
165  MOCK_METHOD(int, OverloadedOnArgumentNumber, (int)); // NOLINT
166 
167  MOCK_METHOD(int, OverloadedOnArgumentType, (int)); // NOLINT
168  MOCK_METHOD(char, OverloadedOnArgumentType, (char)); // NOLINT
169 
170  MOCK_METHOD(int, OverloadedOnConstness, (), (override)); // NOLINT
171  MOCK_METHOD(char, OverloadedOnConstness, (), (override, const)); // NOLINT
172 
173  MOCK_METHOD(int, TypeWithHole, (int (*)()), ()); // NOLINT
174  MOCK_METHOD(int, TypeWithComma, ((const std::map<int, std::string>&)));
175  MOCK_METHOD(int, TypeWithTemplatedCopyCtor,
176  (const TemplatedCopyable<int>&)); // NOLINT
177 
178  MOCK_METHOD(int (*)(bool), ReturnsFunctionPointer1, (int), ());
179  MOCK_METHOD(fn_ptr, ReturnsFunctionPointer2, (int), ());
180 
181 #ifdef GTEST_OS_WINDOWS
182  MOCK_METHOD(int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
183  MOCK_METHOD(bool, CTUnary, (int), (Calltype(STDMETHODCALLTYPE)));
184  MOCK_METHOD(int, CTDecimal,
185  (bool b, char c, short d, int e, long f, float g, double h,
186  unsigned i, char* j, const std::string& k),
187  (Calltype(STDMETHODCALLTYPE)));
188  MOCK_METHOD(char, CTConst, (int), (const, Calltype(STDMETHODCALLTYPE)));
189  MOCK_METHOD((std::map<int, std::string>), CTReturnTypeWithComma, (),
190  (Calltype(STDMETHODCALLTYPE)));
191 #endif // GTEST_OS_WINDOWS
192 
193  // Test reference qualified functions.
194  MOCK_METHOD(int, RefQualifiedConstRef, (), (const, ref(&), override));
195  MOCK_METHOD(int, RefQualifiedConstRefRef, (), (const, ref(&&), override));
196  MOCK_METHOD(int, RefQualifiedRef, (), (ref(&), override));
197  MOCK_METHOD(int, RefQualifiedRefRef, (), (ref(&&), override));
198 
199  MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&), override));
200  MOCK_METHOD(int, RefQualifiedOverloaded, (), (const, ref(&&), override));
201  MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&), override));
202  MOCK_METHOD(int, RefQualifiedOverloaded, (), (ref(&&), override));
203 
204  private:
205  MockFoo(const MockFoo&) = delete;
206  MockFoo& operator=(const MockFoo&) = delete;
207 };
208 
209 class LegacyMockFoo : public FooInterface {
210  public:
211  LegacyMockFoo() = default;
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
244  int(const std::map<int, std::string>&)); // NOLINT
246  int(const TemplatedCopyable<int>&)); // NOLINT
247 
248  MOCK_METHOD1(ReturnsFunctionPointer1, int (*(int))(bool));
250 
251 #ifdef 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  LegacyMockFoo(const LegacyMockFoo&) = delete;
279  LegacyMockFoo& operator=(const LegacyMockFoo&) = delete;
280 };
281 
283 
284 template <class T>
285 class FunctionMockerTest : public testing::Test {
286  protected:
287  FunctionMockerTest() : foo_(&mock_foo_) {}
288 
291 };
293 TYPED_TEST_SUITE(FunctionMockerTest, FunctionMockerTestTypes);
294 
295 // Tests mocking a void-returning function.
296 TYPED_TEST(FunctionMockerTest, MocksVoidFunction) {
297  EXPECT_CALL(this->mock_foo_, VoidReturning(Lt(100)));
298  this->foo_->VoidReturning(0);
299 }
300 
301 // Tests mocking a nullary function.
302 TYPED_TEST(FunctionMockerTest, MocksNullaryFunction) {
303  EXPECT_CALL(this->mock_foo_, Nullary())
304  .WillOnce(DoDefault())
305  .WillOnce(Return(1));
306 
307  EXPECT_EQ(0, this->foo_->Nullary());
308  EXPECT_EQ(1, this->foo_->Nullary());
309 }
310 
311 // Tests mocking a unary function.
312 TYPED_TEST(FunctionMockerTest, MocksUnaryFunction) {
313  EXPECT_CALL(this->mock_foo_, Unary(Eq(2))).Times(2).WillOnce(Return(true));
314 
315  EXPECT_TRUE(this->foo_->Unary(2));
316  EXPECT_FALSE(this->foo_->Unary(2));
317 }
318 
319 // Tests mocking a binary function.
320 TYPED_TEST(FunctionMockerTest, MocksBinaryFunction) {
321  EXPECT_CALL(this->mock_foo_, Binary(2, _)).WillOnce(Return(3));
322 
323  EXPECT_EQ(3, this->foo_->Binary(2, 1));
324 }
325 
326 // Tests mocking a decimal function.
327 TYPED_TEST(FunctionMockerTest, MocksDecimalFunction) {
328  EXPECT_CALL(this->mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(), Lt(100),
329  5U, nullptr, "hi"))
330  .WillOnce(Return(5));
331 
332  EXPECT_EQ(5, this->foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, nullptr, "hi"));
333 }
334 
335 // Tests mocking a function that takes a non-const reference.
336 TYPED_TEST(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) {
337  int a = 0;
338  EXPECT_CALL(this->mock_foo_, TakesNonConstReference(Ref(a)))
339  .WillOnce(Return(true));
340 
342 }
343 
344 // Tests mocking a function that takes a const reference.
345 TYPED_TEST(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
346  int a = 0;
347  EXPECT_CALL(this->mock_foo_, TakesConstReference(Ref(a)))
348  .WillOnce(Return("Hello"));
349 
350  EXPECT_EQ("Hello", this->foo_->TakesConstReference(a));
351 }
352 
353 // Tests mocking a function that takes a const variable.
354 TYPED_TEST(FunctionMockerTest, MocksFunctionWithConstArgument) {
355  EXPECT_CALL(this->mock_foo_, TakesConst(Lt(10))).WillOnce(DoDefault());
356 
357  EXPECT_FALSE(this->foo_->TakesConst(5));
358 }
359 
360 // Tests mocking functions overloaded on the number of arguments.
361 TYPED_TEST(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
362  EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentNumber())
363  .WillOnce(Return(1));
364  EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentNumber(_))
365  .WillOnce(Return(2));
366 
369 }
370 
371 // Tests mocking functions overloaded on the types of argument.
372 TYPED_TEST(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
373  EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentType(An<int>()))
374  .WillOnce(Return(1));
375  EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
376  .WillOnce(Return('b'));
377 
379  EXPECT_EQ('b', this->foo_->OverloadedOnArgumentType('a'));
380 }
381 
382 // Tests mocking functions overloaded on the const-ness of this object.
383 TYPED_TEST(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
384  EXPECT_CALL(this->mock_foo_, OverloadedOnConstness());
385  EXPECT_CALL(Const(this->mock_foo_), OverloadedOnConstness())
386  .WillOnce(Return('a'));
387 
388  EXPECT_EQ(0, this->foo_->OverloadedOnConstness());
389  EXPECT_EQ('a', Const(*this->foo_).OverloadedOnConstness());
390 }
391 
392 TYPED_TEST(FunctionMockerTest, MocksReturnTypeWithComma) {
393  const std::map<int, std::string> a_map;
394  EXPECT_CALL(this->mock_foo_, ReturnTypeWithComma()).WillOnce(Return(a_map));
395  EXPECT_CALL(this->mock_foo_, ReturnTypeWithComma(42)).WillOnce(Return(a_map));
396 
397  EXPECT_EQ(a_map, this->mock_foo_.ReturnTypeWithComma());
398  EXPECT_EQ(a_map, this->mock_foo_.ReturnTypeWithComma(42));
399 }
400 
401 TYPED_TEST(FunctionMockerTest, MocksTypeWithTemplatedCopyCtor) {
402  EXPECT_CALL(this->mock_foo_, TypeWithTemplatedCopyCtor(_))
403  .WillOnce(Return(true));
405 }
406 
407 #ifdef GTEST_OS_WINDOWS
408 // Tests mocking a nullary function with calltype.
409 TYPED_TEST(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
410  EXPECT_CALL(this->mock_foo_, CTNullary())
411  .WillOnce(Return(-1))
412  .WillOnce(Return(0));
413 
414  EXPECT_EQ(-1, this->foo_->CTNullary());
415  EXPECT_EQ(0, this->foo_->CTNullary());
416 }
417 
418 // Tests mocking a unary function with calltype.
419 TYPED_TEST(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
420  EXPECT_CALL(this->mock_foo_, CTUnary(Eq(2)))
421  .Times(2)
422  .WillOnce(Return(true))
423  .WillOnce(Return(false));
424 
425  EXPECT_TRUE(this->foo_->CTUnary(2));
426  EXPECT_FALSE(this->foo_->CTUnary(2));
427 }
428 
429 // Tests mocking a decimal function with calltype.
430 TYPED_TEST(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
431  EXPECT_CALL(this->mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
432  Lt(100), 5U, NULL, "hi"))
433  .WillOnce(Return(10));
434 
435  EXPECT_EQ(10, this->foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
436 }
437 
438 // Tests mocking functions overloaded on the const-ness of this object.
439 TYPED_TEST(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
440  EXPECT_CALL(Const(this->mock_foo_), CTConst(_)).WillOnce(Return('a'));
441 
442  EXPECT_EQ('a', Const(*this->foo_).CTConst(0));
443 }
444 
445 TYPED_TEST(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
446  const std::map<int, std::string> a_map;
447  EXPECT_CALL(this->mock_foo_, CTReturnTypeWithComma()).WillOnce(Return(a_map));
448 
449  EXPECT_EQ(a_map, this->mock_foo_.CTReturnTypeWithComma());
450 }
451 
452 #endif // GTEST_OS_WINDOWS
453 
454 TEST(FunctionMockerTest, RefQualified) {
456 
457  EXPECT_CALL(mock_foo, RefQualifiedConstRef).WillOnce(Return(1));
458  EXPECT_CALL(std::move(mock_foo), // NOLINT
459  RefQualifiedConstRefRef)
460  .WillOnce(Return(2));
461  EXPECT_CALL(mock_foo, RefQualifiedRef).WillOnce(Return(3));
462  EXPECT_CALL(std::move(mock_foo), // NOLINT
463  RefQualifiedRefRef)
464  .WillOnce(Return(4));
465 
466  EXPECT_CALL(static_cast<const MockFoo&>(mock_foo), RefQualifiedOverloaded())
467  .WillOnce(Return(5));
468  EXPECT_CALL(static_cast<const MockFoo&&>(mock_foo), RefQualifiedOverloaded())
469  .WillOnce(Return(6));
470  EXPECT_CALL(static_cast<MockFoo&>(mock_foo), RefQualifiedOverloaded())
471  .WillOnce(Return(7));
472  EXPECT_CALL(static_cast<MockFoo&&>(mock_foo), RefQualifiedOverloaded())
473  .WillOnce(Return(8));
474 
475  EXPECT_EQ(mock_foo.RefQualifiedConstRef(), 1);
476  EXPECT_EQ(std::move(mock_foo).RefQualifiedConstRefRef(), 2); // NOLINT
477  EXPECT_EQ(mock_foo.RefQualifiedRef(), 3);
478  EXPECT_EQ(std::move(mock_foo).RefQualifiedRefRef(), 4); // NOLINT
479 
480  EXPECT_EQ(std::cref(mock_foo).get().RefQualifiedOverloaded(), 5);
481  EXPECT_EQ(std::move(std::cref(mock_foo).get()) // NOLINT
482  .RefQualifiedOverloaded(),
483  6);
484  EXPECT_EQ(mock_foo.RefQualifiedOverloaded(), 7);
485  EXPECT_EQ(std::move(mock_foo).RefQualifiedOverloaded(), 8); // NOLINT
486 }
487 
488 class MockB {
489  public:
490  MockB() = default;
491 
492  MOCK_METHOD(void, DoB, ());
493 
494  private:
495  MockB(const MockB&) = delete;
496  MockB& operator=(const MockB&) = delete;
497 };
498 
499 class LegacyMockB {
500  public:
501  LegacyMockB() = default;
502 
503  MOCK_METHOD0(DoB, void());
504 
505  private:
506  LegacyMockB(const LegacyMockB&) = delete;
507  LegacyMockB& operator=(const LegacyMockB&) = delete;
508 };
509 
510 template <typename T>
511 class ExpectCallTest : public ::testing::Test {};
514 
515 // Tests that functions with no EXPECT_CALL() rules can be called any
516 // number of times.
517 TYPED_TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
518  { TypeParam b; }
519 
520  {
521  TypeParam b;
522  b.DoB();
523  }
524 
525  {
526  TypeParam b;
527  b.DoB();
528  b.DoB();
529  }
530 }
531 
532 // Tests mocking template interfaces.
533 
534 template <typename T>
536  public:
537  virtual ~StackInterface() = default;
538 
539  // Template parameter appears in function parameter.
540  virtual void Push(const T& value) = 0;
541  virtual void Pop() = 0;
542  virtual int GetSize() const = 0;
543  // Template parameter appears in function return type.
544  virtual const T& GetTop() const = 0;
545 };
546 
547 template <typename T>
548 class MockStack : public StackInterface<T> {
549  public:
550  MockStack() = default;
551 
552  MOCK_METHOD(void, Push, (const T& elem), ());
553  MOCK_METHOD(void, Pop, (), (final));
554  MOCK_METHOD(int, GetSize, (), (const, override));
555  MOCK_METHOD(const T&, GetTop, (), (const));
556 
557  // Tests that the function return type can contain unprotected comma.
558  MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (), ());
559  MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (int), (const));
560 
561  private:
562  MockStack(const MockStack&) = delete;
563  MockStack& operator=(const MockStack&) = delete;
564 };
565 
566 template <typename T>
567 class LegacyMockStack : public StackInterface<T> {
568  public:
569  LegacyMockStack() = default;
570 
571  MOCK_METHOD1_T(Push, void(const T& elem));
572  MOCK_METHOD0_T(Pop, void());
573  MOCK_CONST_METHOD0_T(GetSize, int()); // NOLINT
574  MOCK_CONST_METHOD0_T(GetTop, const T&());
575 
576  // Tests that the function return type can contain unprotected comma.
577  MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>());
578  MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int)); // NOLINT
579 
580  private:
581  LegacyMockStack(const LegacyMockStack&) = delete;
582  LegacyMockStack& operator=(const LegacyMockStack&) = delete;
583 };
584 
585 template <typename T>
587 using TemplateMockTestTypes =
590 
591 // Tests that template mock works.
593  TypeParam mock;
594 
595  EXPECT_CALL(mock, GetSize())
596  .WillOnce(Return(0))
597  .WillOnce(Return(1))
598  .WillOnce(Return(0));
599  EXPECT_CALL(mock, Push(_));
600  int n = 5;
601  EXPECT_CALL(mock, GetTop()).WillOnce(ReturnRef(n));
602  EXPECT_CALL(mock, Pop()).Times(AnyNumber());
603 
604  EXPECT_EQ(0, mock.GetSize());
605  mock.Push(5);
606  EXPECT_EQ(1, mock.GetSize());
607  EXPECT_EQ(5, mock.GetTop());
608  mock.Pop();
609  EXPECT_EQ(0, mock.GetSize());
610 }
611 
612 TYPED_TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
613  TypeParam mock;
614 
615  const std::map<int, int> a_map;
616  EXPECT_CALL(mock, ReturnTypeWithComma()).WillOnce(Return(a_map));
617  EXPECT_CALL(mock, ReturnTypeWithComma(1)).WillOnce(Return(a_map));
618 
619  EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
620  EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
621 }
622 
623 #ifdef GTEST_OS_WINDOWS
624 // Tests mocking template interfaces with calltype.
625 
626 template <typename T>
627 class StackInterfaceWithCallType {
628  public:
629  virtual ~StackInterfaceWithCallType() {}
630 
631  // Template parameter appears in function parameter.
632  STDMETHOD_(void, Push)(const T& value) = 0;
633  STDMETHOD_(void, Pop)() = 0;
634  STDMETHOD_(int, GetSize)() const = 0;
635  // Template parameter appears in function return type.
636  STDMETHOD_(const T&, GetTop)() const = 0;
637 };
638 
639 template <typename T>
640 class MockStackWithCallType : public StackInterfaceWithCallType<T> {
641  public:
642  MockStackWithCallType() {}
643 
644  MOCK_METHOD(void, Push, (const T& elem),
645  (Calltype(STDMETHODCALLTYPE), override));
646  MOCK_METHOD(void, Pop, (), (Calltype(STDMETHODCALLTYPE), override));
647  MOCK_METHOD(int, GetSize, (), (Calltype(STDMETHODCALLTYPE), override, const));
648  MOCK_METHOD(const T&, GetTop, (),
649  (Calltype(STDMETHODCALLTYPE), override, const));
650 
651  private:
652  MockStackWithCallType(const MockStackWithCallType&) = delete;
653  MockStackWithCallType& operator=(const MockStackWithCallType&) = delete;
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  LegacyMockStackWithCallType(const LegacyMockStackWithCallType&) = delete;
668  LegacyMockStackWithCallType& operator=(const LegacyMockStackWithCallType&) =
669  delete;
670 };
671 
672 template <typename T>
673 class TemplateMockTestWithCallType : public ::testing::Test {};
674 using TemplateMockTestWithCallTypeTypes =
676  LegacyMockStackWithCallType<int>>;
677 TYPED_TEST_SUITE(TemplateMockTestWithCallType,
678  TemplateMockTestWithCallTypeTypes);
679 
680 // Tests that template mock with calltype works.
681 TYPED_TEST(TemplateMockTestWithCallType, Works) {
682  TypeParam mock;
683 
684  EXPECT_CALL(mock, GetSize())
685  .WillOnce(Return(0))
686  .WillOnce(Return(1))
687  .WillOnce(Return(0));
688  EXPECT_CALL(mock, Push(_));
689  int n = 5;
690  EXPECT_CALL(mock, GetTop()).WillOnce(ReturnRef(n));
691  EXPECT_CALL(mock, Pop()).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 
713  public:
714  MockOverloadedOnArgNumber() = default;
715 
717 
718  private:
721  delete;
722 };
723 
725  public:
727 
729 
730  private:
732  delete;
734  const LegacyMockOverloadedOnArgNumber&) = delete;
735 };
736 
737 template <typename T>
743 
744 TYPED_TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
745  TypeParam mock;
746  EXPECT_CALL(mock, Overloaded());
747  EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
748  EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
749 
750  mock.Overloaded();
751  EXPECT_EQ(2, mock.Overloaded(1));
752  EXPECT_TRUE(mock.Overloaded(true, 1));
753 }
754 
755 #define MY_MOCK_METHODS2_ \
756  MOCK_CONST_METHOD1(Overloaded, int(int n)); \
757  MOCK_METHOD1(Overloaded, int(int n))
758 
760  public:
761  MockOverloadedOnConstness() = default;
762 
764 
765  private:
768  delete;
769 };
770 
771 TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
773  const MockOverloadedOnConstness* const_mock = &mock;
774  EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
775  EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
776 
777  EXPECT_EQ(2, mock.Overloaded(1));
778  EXPECT_EQ(3, const_mock->Overloaded(1));
779 }
780 
781 TEST(MockMethodMockFunctionTest, WorksForVoidNullary) {
782  MockFunction<void()> foo;
783  EXPECT_CALL(foo, Call());
784  foo.Call();
785 }
786 
787 TEST(MockMethodMockFunctionTest, WorksForNonVoidNullary) {
788  MockFunction<int()> foo;
789  EXPECT_CALL(foo, Call()).WillOnce(Return(1)).WillOnce(Return(2));
790  EXPECT_EQ(1, foo.Call());
791  EXPECT_EQ(2, foo.Call());
792 }
793 
794 TEST(MockMethodMockFunctionTest, WorksForVoidUnary) {
795  MockFunction<void(int)> foo;
796  EXPECT_CALL(foo, Call(1));
797  foo.Call(1);
798 }
799 
800 TEST(MockMethodMockFunctionTest, WorksForNonVoidBinary) {
801  MockFunction<int(bool, int)> foo;
802  EXPECT_CALL(foo, Call(false, 42)).WillOnce(Return(1)).WillOnce(Return(2));
803  EXPECT_CALL(foo, Call(true, Ge(100))).WillOnce(Return(3));
804  EXPECT_EQ(1, foo.Call(false, 42));
805  EXPECT_EQ(2, foo.Call(false, 42));
806  EXPECT_EQ(3, foo.Call(true, 120));
807 }
808 
809 TEST(MockMethodMockFunctionTest, WorksFor10Arguments) {
810  MockFunction<int(bool a0, char a1, int a2, int a3, int a4, int a5, int a6,
811  char a7, int a8, bool a9)>
812  foo;
813  EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
814  .WillOnce(Return(1))
815  .WillOnce(Return(2));
816  EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
817  EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
818 }
819 
820 TEST(MockMethodMockFunctionTest, AsStdFunction) {
821  MockFunction<int(int)> foo;
822  auto call = [](const std::function<int(int)>& f, int i) { return f(i); };
823  EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1));
824  EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2));
825  EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
826  EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
827 }
828 
829 TEST(MockMethodMockFunctionTest, AsStdFunctionReturnsReference) {
830  MockFunction<int&()> foo;
831  int value = 1;
832  EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value));
833  int& ref = foo.AsStdFunction()();
834  EXPECT_EQ(1, ref);
835  value = 2;
836  EXPECT_EQ(2, ref);
837 }
838 
839 TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) {
840  MockFunction<int(int&)> foo;
841  auto call = [](const std::function<int(int&)>& f, int& i) { return f(i); };
842  int i = 42;
843  EXPECT_CALL(foo, Call(i)).WillOnce(Return(-1));
844  EXPECT_EQ(-1, call(foo.AsStdFunction(), i));
845 }
846 
847 namespace {
848 
849 template <typename Expected, typename F>
850 static constexpr bool IsMockFunctionTemplateArgumentDeducedTo(
851  const internal::MockFunction<F>&) {
853 }
854 
855 } // namespace
856 
857 template <typename F>
859 
861  Types<void(), int(), void(int), int(int), int(bool, int),
862  int(bool, char, int, int, int, int, int, char, int, bool)>;
865 
867  IsMockFunctionTemplateArgumentDeducedForRawSignature) {
868  using Argument = TypeParam;
869  MockFunction<Argument> foo;
870  EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
871 }
872 
874  IsMockFunctionTemplateArgumentDeducedForStdFunction) {
875  using Argument = std::function<TypeParam>;
876  MockFunction<Argument> foo;
877  EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
878 }
879 
882  IsMockFunctionCallMethodSignatureTheSameForRawSignatureAndStdFunction) {
883  using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
884  using ForStdFunction =
885  decltype(&MockFunction<std::function<TypeParam>>::Call);
887 }
888 
889 template <typename F>
891 
893  IsMockFunctionTemplateArgumentDeducedForAlternateCallable) {
894  using Argument = AlternateCallable<TypeParam>;
895  MockFunction<Argument> foo;
896  EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
897 }
898 
900  IsMockFunctionCallMethodSignatureTheSameForAlternateCallable) {
901  using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
902  using ForStdFunction =
903  decltype(&MockFunction<std::function<TypeParam>>::Call);
905 }
906 
908  MOCK_METHOD(void, func, ());
909 };
911  MOCK_METHOD(void, func, (int));
912 };
914  MOCK_METHOD(void, func, (int, int));
915 };
917  MOCK_METHOD(void, func, (int, int, int));
918 };
920  MOCK_METHOD(void, func, (int, int, int, int));
921 };
922 
924  MOCK_METHOD0(func, void());
925 };
927  MOCK_METHOD1(func, void(int));
928 };
930  MOCK_METHOD2(func, void(int, int));
931 };
933  MOCK_METHOD3(func, void(int, int, int));
934 };
936  MOCK_METHOD4(func, void(int, int, int, int));
937 };
938 
939 TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) {
940  EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes1));
941  EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes2));
942  EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes3));
943  EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4));
944 
949 
951 }
952 
953 TEST(MockMethodMockFunctionTest, EnsureNoUnusedMemberFunction) {
954 #ifdef __clang__
955 #pragma clang diagnostic push
956 #pragma clang diagnostic error "-Wunused-member-function"
957 #endif
958  // https://github.com/google/googletest/issues/4052
959  struct Foo {
960  MOCK_METHOD(void, foo, ());
961  };
962  EXPECT_CALL(Foo(), foo()).Times(0);
963 #ifdef __clang__
964 #pragma clang diagnostic pop
965 #endif
966 }
967 
968 void hasTwoParams(int, int);
969 void MaybeThrows();
970 void DoesntThrow() noexcept;
972  MOCK_METHOD(void, func1, (), (noexcept));
973  MOCK_METHOD(void, func2, (), (noexcept(true)));
974  MOCK_METHOD(void, func3, (), (noexcept(false)));
975  MOCK_METHOD(void, func4, (), (noexcept(noexcept(MaybeThrows()))));
976  MOCK_METHOD(void, func5, (), (noexcept(noexcept(DoesntThrow()))));
977  MOCK_METHOD(void, func6, (), (noexcept(noexcept(DoesntThrow())), const));
978  MOCK_METHOD(void, func7, (), (const, noexcept(noexcept(DoesntThrow()))));
979  // Put commas in the noexcept expression
980  MOCK_METHOD(void, func8, (), (noexcept(noexcept(hasTwoParams(1, 2))), const));
981 };
982 
983 TEST(MockMethodMockFunctionTest, NoexceptSpecifierPreserved) {
984  EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func1()));
985  EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func2()));
986  EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func3()));
987  EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func4()));
988  EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func5()));
989  EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func6()));
990  EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func7()));
991  EXPECT_EQ(noexcept(std::declval<MockMethodNoexceptSpecifier>().func8()),
992  noexcept(hasTwoParams(1, 2)));
993 }
994 
995 } // namespace gmock_function_mocker_test
996 } // namespace testing
997 
#define MOCK_METHOD1_T_WITH_CALLTYPE(ct, m,...)
MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map< int, int >(int))
virtual int TypeWithTemplatedCopyCtor(const TemplatedCopyable< int > &)=0
void f()
TEST(FunctionMockerTest, RefQualified)
#define MOCK_METHOD1_WITH_CALLTYPE(ct, m,...)
const T func2(T &a, T &b)
Definition: ad_example.cpp:25
LegacyMockB & operator=(const LegacyMockB &)=delete
#define MOCK_METHOD(...)
#define MOCK_METHOD0_WITH_CALLTYPE(ct, m,...)
MOCK_METHOD(void, func,(int, int, int, int))
MockStack & operator=(const MockStack &)=delete
GTEST_DISABLE_MSC_WARNINGS_POP_() template< class T > class FunctionMockerTest
#define MOCK_CONST_METHOD1_WITH_CALLTYPE(ct, m,...)
internal::ReturnRefAction< R > ReturnRef(R &x)
#define T
Definition: Sacado_rad.hpp:553
MockOverloadedOnConstness & operator=(const MockOverloadedOnConstness &)=delete
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
#define A
Definition: Sacado_rad.hpp:552
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition: gtest-port.h:377
MockOverloadedOnArgNumber & operator=(const MockOverloadedOnArgNumber &)=delete
ADVar foo(double d, ADVar x, ADVar y)
int value
void g()
MOCK_METHOD(void, Push,(const T &elem),())
void
Definition: uninit.c:105
MockFoo * mock_foo
MockB & operator=(const MockB &)=delete
TYPED_TEST(FunctionMockerTest, MocksVoidFunction)
TYPED_TEST_SUITE(FunctionMockerTest, FunctionMockerTestTypes)
GTEST_API_ Cardinality AnyNumber()
internal::DoDefaultAction DoDefault()
internal::ReturnAction< R > Return(R value)
#define EXPECT_CALL(obj, call)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1884
virtual std::string TakesConstReference(const int &n)=0
const T func(int n, T *x)
Definition: ad_example.cpp:29
#define MOCK_METHOD0_T_WITH_CALLTYPE(ct, m,...)
MOCK_CONST_METHOD0(OverloadedOnConstness, char())
const double y
#define EXPECT_TRUE(condition)
Definition: gtest.h:1823
void func1(const T &x1, const T &x2, T &y)
Definition: fad_expr.cpp:29
#define MOCK_METHOD10_WITH_CALLTYPE(ct, m,...)
LegacyMockFoo & operator=(const LegacyMockFoo &)=delete
#define EXPECT_FALSE(condition)
Definition: gtest.h:1827
virtual int TypeWithComma(const std::map< int, std::string > &a_map)=0
LegacyMockStack & operator=(const LegacyMockStack &)=delete
int n
#define MOCK_CONST_METHOD0_T_WITH_CALLTYPE(ct, m,...)
MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float, double, unsigned, char *, const std::string &str))
MOCK_CONST_METHOD1(ReturnTypeWithComma, std::map< int, std::string >(int))
const char * Binary(const char *input, short n)
virtual int Decimal(bool b, char c, short d, int e, long f, float g, double h, unsigned i, char *j, const std::string &k)=0
LegacyMockOverloadedOnArgNumber & operator=(const LegacyMockOverloadedOnArgNumber &)=delete