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