1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Google Mock - a framework for writing C++ mock classes.
33 // This file tests the built-in actions generated by a script.
35 #include "gmock/gmock-generated-actions.h"
41 #include "gmock/gmock.h"
42 #include "gtest/gtest.h"
45 namespace gmock_generated_actions_test {
50 using testing::Action;
51 using testing::ActionInterface;
54 using testing::Invoke;
55 using testing::Return;
56 using testing::ReturnNew;
57 using testing::SetArgPointee;
58 using testing::StaticAssertTypeEq;
59 using testing::Unused;
61 // For suppressing compiler warnings on conversion possibly losing precision.
62 inline short Short(short n) { return n; } // NOLINT
63 inline char Char(char ch) { return ch; }
65 // Sample functions and functors for testing various actions.
66 int Nullary() { return 1; }
70 bool ByConstRef(const std::string& s) { return s == "Hi"; }
72 const double g_double = 0;
73 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
76 int operator()(bool x) { return x ? 1 : -1; }
79 const char* Binary(const char* input, short n) { return input + n; } // NOLINT
81 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
83 struct SumOf5Functor {
84 int operator()(int a, int b, int c, int d, int e) {
85 return a + b + c + d + e;
89 std::string Concat5(const char* s1, const char* s2, const char* s3,
90 const char* s4, const char* s5) {
91 return std::string(s1) + s2 + s3 + s4 + s5;
94 int SumOf6(int a, int b, int c, int d, int e, int f) {
95 return a + b + c + d + e + f;
98 struct SumOf6Functor {
99 int operator()(int a, int b, int c, int d, int e, int f) {
100 return a + b + c + d + e + f;
104 std::string Concat6(const char* s1, const char* s2, const char* s3,
105 const char* s4, const char* s5, const char* s6) {
106 return std::string(s1) + s2 + s3 + s4 + s5 + s6;
109 std::string Concat7(const char* s1, const char* s2, const char* s3,
110 const char* s4, const char* s5, const char* s6,
112 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
115 std::string Concat8(const char* s1, const char* s2, const char* s3,
116 const char* s4, const char* s5, const char* s6,
117 const char* s7, const char* s8) {
118 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
121 std::string Concat9(const char* s1, const char* s2, const char* s3,
122 const char* s4, const char* s5, const char* s6,
123 const char* s7, const char* s8, const char* s9) {
124 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
127 std::string Concat10(const char* s1, const char* s2, const char* s3,
128 const char* s4, const char* s5, const char* s6,
129 const char* s7, const char* s8, const char* s9,
131 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
134 // A helper that turns the type of a C-string literal from const
135 // char[N] to const char*.
136 inline const char* CharPtr(const char* s) { return s; }
138 // Tests InvokeArgument<N>(...).
140 // Tests using InvokeArgument with a nullary function.
141 TEST(InvokeArgumentTest, Function0) {
142 Action<int(int, int(*)())> a = InvokeArgument<1>(); // NOLINT
143 EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
146 // Tests using InvokeArgument with a unary function.
147 TEST(InvokeArgumentTest, Functor1) {
148 Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
149 EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
152 // Tests using InvokeArgument with a 5-ary function.
153 TEST(InvokeArgumentTest, Function5) {
154 Action<int(int(*)(int, int, int, int, int))> a = // NOLINT
155 InvokeArgument<0>(10000, 2000, 300, 40, 5);
156 EXPECT_EQ(12345, a.Perform(std::make_tuple(&SumOf5)));
159 // Tests using InvokeArgument with a 5-ary functor.
160 TEST(InvokeArgumentTest, Functor5) {
161 Action<int(SumOf5Functor)> a = // NOLINT
162 InvokeArgument<0>(10000, 2000, 300, 40, 5);
163 EXPECT_EQ(12345, a.Perform(std::make_tuple(SumOf5Functor())));
166 // Tests using InvokeArgument with a 6-ary function.
167 TEST(InvokeArgumentTest, Function6) {
168 Action<int(int(*)(int, int, int, int, int, int))> a = // NOLINT
169 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
170 EXPECT_EQ(123456, a.Perform(std::make_tuple(&SumOf6)));
173 // Tests using InvokeArgument with a 6-ary functor.
174 TEST(InvokeArgumentTest, Functor6) {
175 Action<int(SumOf6Functor)> a = // NOLINT
176 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
177 EXPECT_EQ(123456, a.Perform(std::make_tuple(SumOf6Functor())));
180 // Tests using InvokeArgument with a 7-ary function.
181 TEST(InvokeArgumentTest, Function7) {
182 Action<std::string(std::string(*)(const char*, const char*, const char*,
183 const char*, const char*, const char*,
185 a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
186 EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
189 // Tests using InvokeArgument with a 8-ary function.
190 TEST(InvokeArgumentTest, Function8) {
191 Action<std::string(std::string(*)(const char*, const char*, const char*,
192 const char*, const char*, const char*,
193 const char*, const char*))>
194 a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
195 EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
198 // Tests using InvokeArgument with a 9-ary function.
199 TEST(InvokeArgumentTest, Function9) {
200 Action<std::string(std::string(*)(const char*, const char*, const char*,
201 const char*, const char*, const char*,
202 const char*, const char*, const char*))>
203 a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
204 EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
207 // Tests using InvokeArgument with a 10-ary function.
208 TEST(InvokeArgumentTest, Function10) {
209 Action<std::string(std::string(*)(
210 const char*, const char*, const char*, const char*, const char*,
211 const char*, const char*, const char*, const char*, const char*))>
212 a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
213 EXPECT_EQ("1234567890", a.Perform(std::make_tuple(&Concat10)));
216 // Tests using InvokeArgument with a function that takes a pointer argument.
217 TEST(InvokeArgumentTest, ByPointerFunction) {
218 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
219 InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
220 EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
223 // Tests using InvokeArgument with a function that takes a const char*
224 // by passing it a C-string literal.
225 TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
226 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
227 InvokeArgument<0>("Hi", Short(1));
228 EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
231 // Tests using InvokeArgument with a function that takes a const reference.
232 TEST(InvokeArgumentTest, ByConstReferenceFunction) {
233 Action<bool(bool (*function)(const std::string& s))> a = // NOLINT
234 InvokeArgument<0>(std::string("Hi"));
235 // When action 'a' is constructed, it makes a copy of the temporary
236 // string object passed to it, so it's OK to use 'a' later, when the
237 // temporary object has already died.
238 EXPECT_TRUE(a.Perform(std::make_tuple(&ByConstRef)));
241 // Tests using InvokeArgument with ByRef() and a function that takes a
243 TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
244 Action<bool(bool(*)(const double& x))> a = // NOLINT
245 InvokeArgument<0>(ByRef(g_double));
246 // The above line calls ByRef() on a const value.
247 EXPECT_TRUE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
250 a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
251 EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
254 // Tests DoAll(a1, a2).
255 TEST(DoAllTest, TwoActions) {
257 Action<int(int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
259 EXPECT_EQ(2, a.Perform(std::make_tuple(&n)));
263 // Tests DoAll(a1, a2, a3).
264 TEST(DoAllTest, ThreeActions) {
266 Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
269 EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n)));
274 // Tests DoAll(a1, a2, a3, a4).
275 TEST(DoAllTest, FourActions) {
278 Action<int(int*, int*, char*)> a = // NOLINT
279 DoAll(SetArgPointee<0>(1),
281 SetArgPointee<2>('a'),
283 EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n, &ch)));
289 // Tests DoAll(a1, a2, a3, a4, a5).
290 TEST(DoAllTest, FiveActions) {
292 char a = '\0', b = '\0';
293 Action<int(int*, int*, char*, char*)> action = // NOLINT
294 DoAll(SetArgPointee<0>(1),
296 SetArgPointee<2>('a'),
297 SetArgPointee<3>('b'),
299 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b)));
306 // Tests DoAll(a1, a2, ..., a6).
307 TEST(DoAllTest, SixActions) {
309 char a = '\0', b = '\0', c = '\0';
310 Action<int(int*, int*, char*, char*, char*)> action = // NOLINT
311 DoAll(SetArgPointee<0>(1),
313 SetArgPointee<2>('a'),
314 SetArgPointee<3>('b'),
315 SetArgPointee<4>('c'),
317 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c)));
325 // Tests DoAll(a1, a2, ..., a7).
326 TEST(DoAllTest, SevenActions) {
328 char a = '\0', b = '\0', c = '\0', d = '\0';
329 Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT
330 DoAll(SetArgPointee<0>(1),
332 SetArgPointee<2>('a'),
333 SetArgPointee<3>('b'),
334 SetArgPointee<4>('c'),
335 SetArgPointee<5>('d'),
337 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d)));
346 // Tests DoAll(a1, a2, ..., a8).
347 TEST(DoAllTest, EightActions) {
349 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
350 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
352 DoAll(SetArgPointee<0>(1),
354 SetArgPointee<2>('a'),
355 SetArgPointee<3>('b'),
356 SetArgPointee<4>('c'),
357 SetArgPointee<5>('d'),
358 SetArgPointee<6>('e'),
360 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e)));
370 // Tests DoAll(a1, a2, ..., a9).
371 TEST(DoAllTest, NineActions) {
373 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
374 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
375 char*, char*)> action =
376 DoAll(SetArgPointee<0>(1),
378 SetArgPointee<2>('a'),
379 SetArgPointee<3>('b'),
380 SetArgPointee<4>('c'),
381 SetArgPointee<5>('d'),
382 SetArgPointee<6>('e'),
383 SetArgPointee<7>('f'),
385 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
396 // Tests DoAll(a1, a2, ..., a10).
397 TEST(DoAllTest, TenActions) {
399 char a = '\0', b = '\0', c = '\0', d = '\0';
400 char e = '\0', f = '\0', g = '\0';
401 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
402 char*, char*, char*)> action =
403 DoAll(SetArgPointee<0>(1),
405 SetArgPointee<2>('a'),
406 SetArgPointee<3>('b'),
407 SetArgPointee<4>('c'),
408 SetArgPointee<5>('d'),
409 SetArgPointee<6>('e'),
410 SetArgPointee<7>('f'),
411 SetArgPointee<8>('g'),
414 3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
426 // The ACTION*() macros trigger warning C4100 (unreferenced formal
427 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
428 // the macro definition, as the warnings are generated when the macro
429 // is expanded and macro expansion cannot contain #pragma. Therefore
430 // we suppress them here.
431 // Also suppress C4503 decorated name length exceeded, name was truncated
433 # pragma warning(push)
434 # pragma warning(disable:4100)
435 # pragma warning(disable:4503)
437 // Tests the ACTION*() macro family.
439 // Tests that ACTION() can define an action that doesn't reference the
440 // mock function arguments.
441 ACTION(Return5) { return 5; }
443 TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
444 Action<double()> a1 = Return5();
445 EXPECT_DOUBLE_EQ(5, a1.Perform(std::make_tuple()));
447 Action<int(double, bool)> a2 = Return5();
448 EXPECT_EQ(5, a2.Perform(std::make_tuple(1, true)));
451 // Tests that ACTION() can define an action that returns void.
452 ACTION(IncrementArg1) { (*arg1)++; }
454 TEST(ActionMacroTest, WorksWhenReturningVoid) {
455 Action<void(int, int*)> a1 = IncrementArg1();
457 a1.Perform(std::make_tuple(5, &n));
461 // Tests that the body of ACTION() can reference the type of the
463 ACTION(IncrementArg2) {
464 StaticAssertTypeEq<int*, arg2_type>();
465 arg2_type temp = arg2;
469 TEST(ActionMacroTest, CanReferenceArgumentType) {
470 Action<void(int, bool, int*)> a1 = IncrementArg2();
472 a1.Perform(std::make_tuple(5, false, &n));
476 // Tests that the body of ACTION() can reference the argument tuple
477 // via args_type and args.
479 StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
480 args_type args_copy = args;
481 return std::get<0>(args_copy) + std::get<1>(args_copy);
484 TEST(ActionMacroTest, CanReferenceArgumentTuple) {
485 Action<int(int, char, int*)> a1 = Sum2();
487 EXPECT_EQ(11, a1.Perform(std::make_tuple(5, Char(6), &dummy)));
490 // Tests that the body of ACTION() can reference the mock function
492 int Dummy(bool flag) { return flag? 1 : 0; }
494 ACTION(InvokeDummy) {
495 StaticAssertTypeEq<int(bool), function_type>();
496 function_type* fp = &Dummy;
500 TEST(ActionMacroTest, CanReferenceMockFunctionType) {
501 Action<int(bool)> a1 = InvokeDummy();
502 EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
503 EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
506 // Tests that the body of ACTION() can reference the mock function's
508 ACTION(InvokeDummy2) {
509 StaticAssertTypeEq<int, return_type>();
510 return_type result = Dummy(true);
514 TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
515 Action<int(bool)> a1 = InvokeDummy2();
516 EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
517 EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
520 // Tests that ACTION() works for arguments passed by const reference.
521 ACTION(ReturnAddrOfConstBoolReferenceArg) {
522 StaticAssertTypeEq<const bool&, arg1_type>();
526 TEST(ActionMacroTest, WorksForConstReferenceArg) {
527 Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
528 const bool b = false;
529 EXPECT_EQ(&b, a.Perform(std::tuple<int, const bool&>(0, b)));
532 // Tests that ACTION() works for arguments passed by non-const reference.
533 ACTION(ReturnAddrOfIntReferenceArg) {
534 StaticAssertTypeEq<int&, arg0_type>();
538 TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
539 Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
541 EXPECT_EQ(&n, a.Perform(std::tuple<int&, bool, int>(n, true, 1)));
544 // Tests that ACTION() can be used in a namespace.
545 namespace action_test {
546 ACTION(Sum) { return arg0 + arg1; }
547 } // namespace action_test
549 TEST(ActionMacroTest, WorksInNamespace) {
550 Action<int(int, int)> a1 = action_test::Sum();
551 EXPECT_EQ(3, a1.Perform(std::make_tuple(1, 2)));
554 // Tests that the same ACTION definition works for mock functions with
555 // different argument numbers.
556 ACTION(PlusTwo) { return arg0 + 2; }
558 TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
559 Action<int(int)> a1 = PlusTwo();
560 EXPECT_EQ(4, a1.Perform(std::make_tuple(2)));
562 Action<double(float, void*)> a2 = PlusTwo();
564 EXPECT_DOUBLE_EQ(6, a2.Perform(std::make_tuple(4.0f, &dummy)));
567 // Tests that ACTION_P can define a parameterized action.
568 ACTION_P(Plus, n) { return arg0 + n; }
570 TEST(ActionPMacroTest, DefinesParameterizedAction) {
571 Action<int(int m, bool t)> a1 = Plus(9);
572 EXPECT_EQ(10, a1.Perform(std::make_tuple(1, true)));
575 // Tests that the body of ACTION_P can reference the argument types
576 // and the parameter type.
577 ACTION_P(TypedPlus, n) {
583 TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
584 Action<int(char m, bool t)> a1 = TypedPlus(9);
585 EXPECT_EQ(10, a1.Perform(std::make_tuple(Char(1), true)));
588 // Tests that a parameterized action can be used in any mock function
589 // whose type is compatible.
590 TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
591 Action<std::string(const std::string& s)> a1 = Plus("tail");
592 const std::string re = "re";
593 std::tuple<const std::string> dummy = std::make_tuple(re);
594 EXPECT_EQ("retail", a1.Perform(dummy));
597 // Tests that we can use ACTION*() to define actions overloaded on the
598 // number of parameters.
600 ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
602 ACTION_P(OverloadedAction, default_value) {
603 return arg0 ? arg1 : default_value;
606 ACTION_P2(OverloadedAction, true_value, false_value) {
607 return arg0 ? true_value : false_value;
610 TEST(ActionMacroTest, CanDefineOverloadedActions) {
611 typedef Action<const char*(bool, const char*)> MyAction;
613 const MyAction a1 = OverloadedAction();
614 EXPECT_STREQ("hello", a1.Perform(std::make_tuple(false, CharPtr("world"))));
615 EXPECT_STREQ("world", a1.Perform(std::make_tuple(true, CharPtr("world"))));
617 const MyAction a2 = OverloadedAction("hi");
618 EXPECT_STREQ("hi", a2.Perform(std::make_tuple(false, CharPtr("world"))));
619 EXPECT_STREQ("world", a2.Perform(std::make_tuple(true, CharPtr("world"))));
621 const MyAction a3 = OverloadedAction("hi", "you");
622 EXPECT_STREQ("hi", a3.Perform(std::make_tuple(true, CharPtr("world"))));
623 EXPECT_STREQ("you", a3.Perform(std::make_tuple(false, CharPtr("world"))));
626 // Tests ACTION_Pn where n >= 3.
628 ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
630 TEST(ActionPnMacroTest, WorksFor3Parameters) {
631 Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
632 EXPECT_DOUBLE_EQ(3123.4, a1.Perform(std::make_tuple(3000, true)));
634 Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
635 const std::string re = "re";
636 std::tuple<const std::string> dummy = std::make_tuple(re);
637 EXPECT_EQ("retail->", a2.Perform(dummy));
640 ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
642 TEST(ActionPnMacroTest, WorksFor4Parameters) {
643 Action<int(int)> a1 = Plus(1, 2, 3, 4);
644 EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(std::make_tuple(10)));
647 ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
649 TEST(ActionPnMacroTest, WorksFor5Parameters) {
650 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
651 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(std::make_tuple(10)));
654 ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
655 return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
658 TEST(ActionPnMacroTest, WorksFor6Parameters) {
659 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
660 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(std::make_tuple(10)));
663 ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
664 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
667 TEST(ActionPnMacroTest, WorksFor7Parameters) {
668 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
669 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(std::make_tuple(10)));
672 ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
673 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
676 TEST(ActionPnMacroTest, WorksFor8Parameters) {
677 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
678 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
679 a1.Perform(std::make_tuple(10)));
682 ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
683 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
686 TEST(ActionPnMacroTest, WorksFor9Parameters) {
687 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
688 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
689 a1.Perform(std::make_tuple(10)));
692 ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
694 last_param_type t9 = last_param;
695 return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
698 TEST(ActionPnMacroTest, WorksFor10Parameters) {
699 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
700 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
701 a1.Perform(std::make_tuple(10)));
704 // Tests that the action body can promote the parameter types.
706 ACTION_P2(PadArgument, prefix, suffix) {
707 // The following lines promote the two parameters to desired types.
708 std::string prefix_str(prefix);
709 char suffix_char = static_cast<char>(suffix);
710 return prefix_str + arg0 + suffix_char;
713 TEST(ActionPnMacroTest, SimpleTypePromotion) {
714 Action<std::string(const char*)> no_promo =
715 PadArgument(std::string("foo"), 'r');
716 Action<std::string(const char*)> promo =
717 PadArgument("foo", static_cast<int>('r'));
718 EXPECT_EQ("foobar", no_promo.Perform(std::make_tuple(CharPtr("ba"))));
719 EXPECT_EQ("foobar", promo.Perform(std::make_tuple(CharPtr("ba"))));
722 // Tests that we can partially restrict parameter types using a
723 // straight-forward pattern.
725 // Defines a generic action that doesn't restrict the types of its
727 ACTION_P3(ConcatImpl, a, b, c) {
728 std::stringstream ss;
733 // Next, we try to restrict that either the first parameter is a
734 // string, or the second parameter is an int.
736 // Defines a partially specialized wrapper that restricts the first
737 // parameter to std::string.
738 template <typename T1, typename T2>
739 // ConcatImplActionP3 is the class template ACTION_P3 uses to
740 // implement ConcatImpl. We shouldn't change the name as this
741 // pattern requires the user to use it directly.
742 ConcatImplActionP3<std::string, T1, T2>
743 Concat(const std::string& a, T1 b, T2 c) {
744 GTEST_INTENTIONAL_CONST_COND_PUSH_()
746 GTEST_INTENTIONAL_CONST_COND_POP_()
747 // This branch verifies that ConcatImpl() can be invoked without
748 // explicit template arguments.
749 return ConcatImpl(a, b, c);
751 // This branch verifies that ConcatImpl() can also be invoked with
752 // explicit template arguments. It doesn't really need to be
753 // executed as this is a compile-time verification.
754 return ConcatImpl<std::string, T1, T2>(a, b, c);
758 // Defines another partially specialized wrapper that restricts the
759 // second parameter to int.
760 template <typename T1, typename T2>
761 ConcatImplActionP3<T1, int, T2>
762 Concat(T1 a, int b, T2 c) {
763 return ConcatImpl(a, b, c);
766 TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
767 Action<const std::string()> a1 = Concat("Hello", "1", 2);
768 EXPECT_EQ("Hello12", a1.Perform(std::make_tuple()));
770 a1 = Concat(1, 2, 3);
771 EXPECT_EQ("123", a1.Perform(std::make_tuple()));
774 // Verifies the type of an ACTION*.
777 ACTION_P(DoFoo, p) {}
778 ACTION_P2(DoFoo, p0, p1) {}
780 TEST(ActionPnMacroTest, TypesAreCorrect) {
781 // DoFoo() must be assignable to a DoFooAction variable.
782 DoFooAction a0 = DoFoo();
784 // DoFoo(1) must be assignable to a DoFooActionP variable.
785 DoFooActionP<int> a1 = DoFoo(1);
787 // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
788 // variable, and so on.
789 DoFooActionP2<int, char> a2 = DoFoo(1, '2');
790 PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
791 PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
792 PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
793 PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
794 PlusActionP7<int, int, int, int, int, int, char> a7 =
795 Plus(1, 2, 3, 4, 5, 6, '7');
796 PlusActionP8<int, int, int, int, int, int, int, char> a8 =
797 Plus(1, 2, 3, 4, 5, 6, 7, '8');
798 PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
799 Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
800 PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
801 Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
803 // Avoid "unused variable" warnings.
817 // Tests that an ACTION_P*() action can be explicitly instantiated
818 // with reference-typed parameters.
820 ACTION_P(Plus1, x) { return x; }
821 ACTION_P2(Plus2, x, y) { return x + y; }
822 ACTION_P3(Plus3, x, y, z) { return x + y + z; }
823 ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
824 return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
827 TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
828 int x = 1, y = 2, z = 3;
829 const std::tuple<> empty = std::make_tuple();
831 Action<int()> a = Plus1<int&>(x);
832 EXPECT_EQ(1, a.Perform(empty));
834 a = Plus2<const int&, int&>(x, y);
835 EXPECT_EQ(3, a.Perform(empty));
837 a = Plus3<int&, const int&, int&>(x, y, z);
838 EXPECT_EQ(6, a.Perform(empty));
840 int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
841 a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
842 int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
844 EXPECT_EQ(55, a.Perform(empty));
847 class NullaryConstructorClass {
849 NullaryConstructorClass() : value_(123) {}
853 // Tests using ReturnNew() with a nullary constructor.
854 TEST(ReturnNewTest, NoArgs) {
855 Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>();
856 NullaryConstructorClass* c = a.Perform(std::make_tuple());
857 EXPECT_EQ(123, c->value_);
861 class UnaryConstructorClass {
863 explicit UnaryConstructorClass(int value) : value_(value) {}
867 // Tests using ReturnNew() with a unary constructor.
868 TEST(ReturnNewTest, Unary) {
869 Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
870 UnaryConstructorClass* c = a.Perform(std::make_tuple());
871 EXPECT_EQ(4000, c->value_);
875 TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
876 Action<UnaryConstructorClass*(bool, int)> a =
877 ReturnNew<UnaryConstructorClass>(4000);
878 UnaryConstructorClass* c = a.Perform(std::make_tuple(false, 5));
879 EXPECT_EQ(4000, c->value_);
883 TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
884 Action<const UnaryConstructorClass*()> a =
885 ReturnNew<UnaryConstructorClass>(4000);
886 const UnaryConstructorClass* c = a.Perform(std::make_tuple());
887 EXPECT_EQ(4000, c->value_);
891 class TenArgConstructorClass {
893 TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
894 int a6, int a7, int a8, int a9, int a10)
895 : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
900 // Tests using ReturnNew() with a 10-argument constructor.
901 TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
902 Action<TenArgConstructorClass*()> a =
903 ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
904 4000000, 500000, 60000,
906 TenArgConstructorClass* c = a.Perform(std::make_tuple());
907 EXPECT_EQ(1234567890, c->value_);
911 // Tests that ACTION_TEMPLATE works when there is no value parameter.
912 ACTION_TEMPLATE(CreateNew,
913 HAS_1_TEMPLATE_PARAMS(typename, T),
914 AND_0_VALUE_PARAMS()) {
918 TEST(ActionTemplateTest, WorksWithoutValueParam) {
919 const Action<int*()> a = CreateNew<int>();
920 int* p = a.Perform(std::make_tuple());
924 // Tests that ACTION_TEMPLATE works when there are value parameters.
925 ACTION_TEMPLATE(CreateNew,
926 HAS_1_TEMPLATE_PARAMS(typename, T),
927 AND_1_VALUE_PARAMS(a0)) {
931 TEST(ActionTemplateTest, WorksWithValueParams) {
932 const Action<int*()> a = CreateNew<int>(42);
933 int* p = a.Perform(std::make_tuple());
938 // Tests that ACTION_TEMPLATE works for integral template parameters.
939 ACTION_TEMPLATE(MyDeleteArg,
940 HAS_1_TEMPLATE_PARAMS(int, k),
941 AND_0_VALUE_PARAMS()) {
942 delete std::get<k>(args);
945 // Resets a bool variable in the destructor.
948 explicit BoolResetter(bool* value) : value_(value) {}
949 ~BoolResetter() { *value_ = false; }
954 TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
955 const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
958 BoolResetter* resetter = new BoolResetter(&b);
959 a.Perform(std::make_tuple(&n, resetter));
960 EXPECT_FALSE(b); // Verifies that resetter is deleted.
963 // Tests that ACTION_TEMPLATES works for template template parameters.
964 ACTION_TEMPLATE(ReturnSmartPointer,
965 HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
967 AND_1_VALUE_PARAMS(pointee)) {
968 return Pointer<pointee_type>(new pointee_type(pointee));
971 TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
972 const Action<std::shared_ptr<int>()> a =
973 ReturnSmartPointer<std::shared_ptr>(42);
974 std::shared_ptr<int> p = a.Perform(std::make_tuple());
978 // Tests that ACTION_TEMPLATE works for 10 template parameters.
979 template <typename T1, typename T2, typename T3, int k4, bool k5,
980 unsigned int k6, typename T7, typename T8, typename T9>
981 struct GiantTemplate {
983 explicit GiantTemplate(int a_value) : value(a_value) {}
987 ACTION_TEMPLATE(ReturnGiant,
988 HAS_10_TEMPLATE_PARAMS(
998 template <typename T> class, T10),
999 AND_1_VALUE_PARAMS(value)) {
1000 return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
1003 TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1004 using Giant = GiantTemplate<std::shared_ptr<int>, bool, double, 5, true, 6,
1005 char, unsigned, int>;
1006 const Action<Giant()> a = ReturnGiant<int, bool, double, 5, true, 6, char,
1007 unsigned, int, std::shared_ptr>(42);
1008 Giant giant = a.Perform(std::make_tuple());
1009 EXPECT_EQ(42, giant.value);
1012 // Tests that ACTION_TEMPLATE works for 10 value parameters.
1013 ACTION_TEMPLATE(ReturnSum,
1014 HAS_1_TEMPLATE_PARAMS(typename, Number),
1015 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1016 return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1019 TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1020 const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1021 EXPECT_EQ(55, a.Perform(std::make_tuple()));
1024 // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
1025 // on the number of value parameters.
1027 ACTION(ReturnSum) { return 0; }
1029 ACTION_P(ReturnSum, x) { return x; }
1031 ACTION_TEMPLATE(ReturnSum,
1032 HAS_1_TEMPLATE_PARAMS(typename, Number),
1033 AND_2_VALUE_PARAMS(v1, v2)) {
1034 return static_cast<Number>(v1) + v2;
1037 ACTION_TEMPLATE(ReturnSum,
1038 HAS_1_TEMPLATE_PARAMS(typename, Number),
1039 AND_3_VALUE_PARAMS(v1, v2, v3)) {
1040 return static_cast<Number>(v1) + v2 + v3;
1043 ACTION_TEMPLATE(ReturnSum,
1044 HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
1045 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1046 return static_cast<Number>(v1) + v2 + v3 + v4 + k;
1049 TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1050 const Action<int()> a0 = ReturnSum();
1051 const Action<int()> a1 = ReturnSum(1);
1052 const Action<int()> a2 = ReturnSum<int>(1, 2);
1053 const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
1054 const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1055 EXPECT_EQ(0, a0.Perform(std::make_tuple()));
1056 EXPECT_EQ(1, a1.Perform(std::make_tuple()));
1057 EXPECT_EQ(3, a2.Perform(std::make_tuple()));
1058 EXPECT_EQ(6, a3.Perform(std::make_tuple()));
1059 EXPECT_EQ(12345, a4.Perform(std::make_tuple()));
1063 } // namespace gmock_generated_actions_test
1064 } // namespace testing