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


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:30