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 built-in actions generated by a script.
34 
35 #include "gmock/gmock-generated-actions.h"
36 
37 #include <functional>
38 #include <memory>
39 #include <sstream>
40 #include <string>
41 #include "gmock/gmock.h"
42 #include "gtest/gtest.h"
43 
44 namespace testing {
45 namespace gmock_generated_actions_test {
46 
47 using ::std::plus;
48 using ::std::string;
49 using testing::_;
50 using testing::Action;
51 using testing::ActionInterface;
52 using testing::ByRef;
53 using testing::DoAll;
54 using testing::Invoke;
55 using testing::Return;
56 using testing::SetArgPointee;
57 using testing::StaticAssertTypeEq;
58 using testing::Unused;
59 
60 // For suppressing compiler warnings on conversion possibly losing precision.
Short(short n)61 inline short Short(short n) { return n; }  // NOLINT
Char(char ch)62 inline char Char(char ch) { return ch; }
63 
64 // Sample functions and functors for testing various actions.
Nullary()65 int Nullary() { return 1; }
66 
67 bool g_done = false;
68 
ByConstRef(const std::string & s)69 bool ByConstRef(const std::string& s) { return s == "Hi"; }
70 
71 const double g_double = 0;
ReferencesGlobalDouble(const double & x)72 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
73 
74 struct UnaryFunctor {
operator ()testing::gmock_generated_actions_test::UnaryFunctor75   int operator()(bool x) { return x ? 1 : -1; }
76 };
77 
Binary(const char * input,short n)78 const char* Binary(const char* input, short n) { return input + n; }  // NOLINT
79 
SumOf5(int a,int b,int c,int d,int e)80 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
81 
82 struct SumOf5Functor {
operator ()testing::gmock_generated_actions_test::SumOf5Functor83   int operator()(int a, int b, int c, int d, int e) {
84     return a + b + c + d + e;
85   }
86 };
87 
Concat5(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5)88 std::string Concat5(const char* s1, const char* s2, const char* s3,
89                     const char* s4, const char* s5) {
90   return std::string(s1) + s2 + s3 + s4 + s5;
91 }
92 
SumOf6(int a,int b,int c,int d,int e,int f)93 int SumOf6(int a, int b, int c, int d, int e, int f) {
94   return a + b + c + d + e + f;
95 }
96 
97 struct SumOf6Functor {
operator ()testing::gmock_generated_actions_test::SumOf6Functor98   int operator()(int a, int b, int c, int d, int e, int f) {
99     return a + b + c + d + e + f;
100   }
101 };
102 
Concat6(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5,const char * s6)103 std::string Concat6(const char* s1, const char* s2, const char* s3,
104                     const char* s4, const char* s5, const char* s6) {
105   return std::string(s1) + s2 + s3 + s4 + s5 + s6;
106 }
107 
Concat7(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5,const char * s6,const char * s7)108 std::string Concat7(const char* s1, const char* s2, const char* s3,
109                     const char* s4, const char* s5, const char* s6,
110                     const char* s7) {
111   return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
112 }
113 
Concat8(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5,const char * s6,const char * s7,const char * s8)114 std::string Concat8(const char* s1, const char* s2, const char* s3,
115                     const char* s4, const char* s5, const char* s6,
116                     const char* s7, const char* s8) {
117   return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
118 }
119 
Concat9(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5,const char * s6,const char * s7,const char * s8,const char * s9)120 std::string Concat9(const char* s1, const char* s2, const char* s3,
121                     const char* s4, const char* s5, const char* s6,
122                     const char* s7, const char* s8, const char* s9) {
123   return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
124 }
125 
Concat10(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5,const char * s6,const char * s7,const char * s8,const char * s9,const char * s10)126 std::string Concat10(const char* s1, const char* s2, const char* s3,
127                      const char* s4, const char* s5, const char* s6,
128                      const char* s7, const char* s8, const char* s9,
129                      const char* s10) {
130   return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
131 }
132 
133 // A helper that turns the type of a C-string literal from const
134 // char[N] to const char*.
CharPtr(const char * s)135 inline const char* CharPtr(const char* s) { return s; }
136 
137 // Tests InvokeArgument<N>(...).
138 
139 // Tests using InvokeArgument with a nullary function.
TEST(InvokeArgumentTest,Function0)140 TEST(InvokeArgumentTest, Function0) {
141   Action<int(int, int(*)())> a = InvokeArgument<1>();  // NOLINT
142   EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
143 }
144 
145 // Tests using InvokeArgument with a unary function.
TEST(InvokeArgumentTest,Functor1)146 TEST(InvokeArgumentTest, Functor1) {
147   Action<int(UnaryFunctor)> a = InvokeArgument<0>(true);  // NOLINT
148   EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
149 }
150 
151 // Tests using InvokeArgument with a 5-ary function.
TEST(InvokeArgumentTest,Function5)152 TEST(InvokeArgumentTest, Function5) {
153   Action<int(int(*)(int, int, int, int, int))> a =  // NOLINT
154       InvokeArgument<0>(10000, 2000, 300, 40, 5);
155   EXPECT_EQ(12345, a.Perform(std::make_tuple(&SumOf5)));
156 }
157 
158 // Tests using InvokeArgument with a 5-ary functor.
TEST(InvokeArgumentTest,Functor5)159 TEST(InvokeArgumentTest, Functor5) {
160   Action<int(SumOf5Functor)> a =  // NOLINT
161       InvokeArgument<0>(10000, 2000, 300, 40, 5);
162   EXPECT_EQ(12345, a.Perform(std::make_tuple(SumOf5Functor())));
163 }
164 
165 // Tests using InvokeArgument with a 6-ary function.
TEST(InvokeArgumentTest,Function6)166 TEST(InvokeArgumentTest, Function6) {
167   Action<int(int(*)(int, int, int, int, int, int))> a =  // NOLINT
168       InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
169   EXPECT_EQ(123456, a.Perform(std::make_tuple(&SumOf6)));
170 }
171 
172 // Tests using InvokeArgument with a 6-ary functor.
TEST(InvokeArgumentTest,Functor6)173 TEST(InvokeArgumentTest, Functor6) {
174   Action<int(SumOf6Functor)> a =  // NOLINT
175       InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
176   EXPECT_EQ(123456, a.Perform(std::make_tuple(SumOf6Functor())));
177 }
178 
179 // Tests using InvokeArgument with a 7-ary function.
TEST(InvokeArgumentTest,Function7)180 TEST(InvokeArgumentTest, Function7) {
181   Action<std::string(std::string(*)(const char*, const char*, const char*,
182                                     const char*, const char*, const char*,
183                                     const char*))>
184       a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
185   EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
186 }
187 
188 // Tests using InvokeArgument with a 8-ary function.
TEST(InvokeArgumentTest,Function8)189 TEST(InvokeArgumentTest, Function8) {
190   Action<std::string(std::string(*)(const char*, const char*, const char*,
191                                     const char*, const char*, const char*,
192                                     const char*, const char*))>
193       a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
194   EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
195 }
196 
197 // Tests using InvokeArgument with a 9-ary function.
TEST(InvokeArgumentTest,Function9)198 TEST(InvokeArgumentTest, Function9) {
199   Action<std::string(std::string(*)(const char*, const char*, const char*,
200                                     const char*, const char*, const char*,
201                                     const char*, const char*, const char*))>
202       a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
203   EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
204 }
205 
206 // Tests using InvokeArgument with a 10-ary function.
TEST(InvokeArgumentTest,Function10)207 TEST(InvokeArgumentTest, Function10) {
208   Action<std::string(std::string(*)(
209       const char*, const char*, const char*, const char*, const char*,
210       const char*, const char*, const char*, const char*, const char*))>
211       a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
212   EXPECT_EQ("1234567890", a.Perform(std::make_tuple(&Concat10)));
213 }
214 
215 // Tests using InvokeArgument with a function that takes a pointer argument.
TEST(InvokeArgumentTest,ByPointerFunction)216 TEST(InvokeArgumentTest, ByPointerFunction) {
217   Action<const char*(const char*(*)(const char* input, short n))> a =  // NOLINT
218       InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
219   EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
220 }
221 
222 // Tests using InvokeArgument with a function that takes a const char*
223 // by passing it a C-string literal.
TEST(InvokeArgumentTest,FunctionWithCStringLiteral)224 TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
225   Action<const char*(const char*(*)(const char* input, short n))> a =  // NOLINT
226       InvokeArgument<0>("Hi", Short(1));
227   EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
228 }
229 
230 // Tests using InvokeArgument with a function that takes a const reference.
TEST(InvokeArgumentTest,ByConstReferenceFunction)231 TEST(InvokeArgumentTest, ByConstReferenceFunction) {
232   Action<bool(bool (*function)(const std::string& s))> a =  // NOLINT
233       InvokeArgument<0>(std::string("Hi"));
234   // When action 'a' is constructed, it makes a copy of the temporary
235   // string object passed to it, so it's OK to use 'a' later, when the
236   // temporary object has already died.
237   EXPECT_TRUE(a.Perform(std::make_tuple(&ByConstRef)));
238 }
239 
240 // Tests using InvokeArgument with ByRef() and a function that takes a
241 // const reference.
TEST(InvokeArgumentTest,ByExplicitConstReferenceFunction)242 TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
243   Action<bool(bool(*)(const double& x))> a =  // NOLINT
244       InvokeArgument<0>(ByRef(g_double));
245   // The above line calls ByRef() on a const value.
246   EXPECT_TRUE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
247 
248   double x = 0;
249   a = InvokeArgument<0>(ByRef(x));  // This calls ByRef() on a non-const.
250   EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
251 }
252 
253 // Tests DoAll(a1, a2).
TEST(DoAllTest,TwoActions)254 TEST(DoAllTest, TwoActions) {
255   int n = 0;
256   Action<int(int*)> a = DoAll(SetArgPointee<0>(1),  // NOLINT
257                               Return(2));
258   EXPECT_EQ(2, a.Perform(std::make_tuple(&n)));
259   EXPECT_EQ(1, n);
260 }
261 
262 // Tests DoAll(a1, a2, a3).
TEST(DoAllTest,ThreeActions)263 TEST(DoAllTest, ThreeActions) {
264   int m = 0, n = 0;
265   Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1),  // NOLINT
266                                     SetArgPointee<1>(2),
267                                     Return(3));
268   EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n)));
269   EXPECT_EQ(1, m);
270   EXPECT_EQ(2, n);
271 }
272 
273 // Tests DoAll(a1, a2, a3, a4).
TEST(DoAllTest,FourActions)274 TEST(DoAllTest, FourActions) {
275   int m = 0, n = 0;
276   char ch = '\0';
277   Action<int(int*, int*, char*)> a =  // NOLINT
278       DoAll(SetArgPointee<0>(1),
279             SetArgPointee<1>(2),
280             SetArgPointee<2>('a'),
281             Return(3));
282   EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n, &ch)));
283   EXPECT_EQ(1, m);
284   EXPECT_EQ(2, n);
285   EXPECT_EQ('a', ch);
286 }
287 
288 // Tests DoAll(a1, a2, a3, a4, a5).
TEST(DoAllTest,FiveActions)289 TEST(DoAllTest, FiveActions) {
290   int m = 0, n = 0;
291   char a = '\0', b = '\0';
292   Action<int(int*, int*, char*, char*)> action =  // NOLINT
293       DoAll(SetArgPointee<0>(1),
294             SetArgPointee<1>(2),
295             SetArgPointee<2>('a'),
296             SetArgPointee<3>('b'),
297             Return(3));
298   EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b)));
299   EXPECT_EQ(1, m);
300   EXPECT_EQ(2, n);
301   EXPECT_EQ('a', a);
302   EXPECT_EQ('b', b);
303 }
304 
305 // Tests DoAll(a1, a2, ..., a6).
TEST(DoAllTest,SixActions)306 TEST(DoAllTest, SixActions) {
307   int m = 0, n = 0;
308   char a = '\0', b = '\0', c = '\0';
309   Action<int(int*, int*, char*, char*, char*)> action =  // NOLINT
310       DoAll(SetArgPointee<0>(1),
311             SetArgPointee<1>(2),
312             SetArgPointee<2>('a'),
313             SetArgPointee<3>('b'),
314             SetArgPointee<4>('c'),
315             Return(3));
316   EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c)));
317   EXPECT_EQ(1, m);
318   EXPECT_EQ(2, n);
319   EXPECT_EQ('a', a);
320   EXPECT_EQ('b', b);
321   EXPECT_EQ('c', c);
322 }
323 
324 // Tests DoAll(a1, a2, ..., a7).
TEST(DoAllTest,SevenActions)325 TEST(DoAllTest, SevenActions) {
326   int m = 0, n = 0;
327   char a = '\0', b = '\0', c = '\0', d = '\0';
328   Action<int(int*, int*, char*, char*, char*, char*)> action =  // NOLINT
329       DoAll(SetArgPointee<0>(1),
330             SetArgPointee<1>(2),
331             SetArgPointee<2>('a'),
332             SetArgPointee<3>('b'),
333             SetArgPointee<4>('c'),
334             SetArgPointee<5>('d'),
335             Return(3));
336   EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d)));
337   EXPECT_EQ(1, m);
338   EXPECT_EQ(2, n);
339   EXPECT_EQ('a', a);
340   EXPECT_EQ('b', b);
341   EXPECT_EQ('c', c);
342   EXPECT_EQ('d', d);
343 }
344 
345 // Tests DoAll(a1, a2, ..., a8).
TEST(DoAllTest,EightActions)346 TEST(DoAllTest, EightActions) {
347   int m = 0, n = 0;
348   char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
349   Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
350              char*)> action =
351       DoAll(SetArgPointee<0>(1),
352             SetArgPointee<1>(2),
353             SetArgPointee<2>('a'),
354             SetArgPointee<3>('b'),
355             SetArgPointee<4>('c'),
356             SetArgPointee<5>('d'),
357             SetArgPointee<6>('e'),
358             Return(3));
359   EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e)));
360   EXPECT_EQ(1, m);
361   EXPECT_EQ(2, n);
362   EXPECT_EQ('a', a);
363   EXPECT_EQ('b', b);
364   EXPECT_EQ('c', c);
365   EXPECT_EQ('d', d);
366   EXPECT_EQ('e', e);
367 }
368 
369 // Tests DoAll(a1, a2, ..., a9).
TEST(DoAllTest,NineActions)370 TEST(DoAllTest, NineActions) {
371   int m = 0, n = 0;
372   char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
373   Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
374              char*, char*)> action =
375       DoAll(SetArgPointee<0>(1),
376             SetArgPointee<1>(2),
377             SetArgPointee<2>('a'),
378             SetArgPointee<3>('b'),
379             SetArgPointee<4>('c'),
380             SetArgPointee<5>('d'),
381             SetArgPointee<6>('e'),
382             SetArgPointee<7>('f'),
383             Return(3));
384   EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
385   EXPECT_EQ(1, m);
386   EXPECT_EQ(2, n);
387   EXPECT_EQ('a', a);
388   EXPECT_EQ('b', b);
389   EXPECT_EQ('c', c);
390   EXPECT_EQ('d', d);
391   EXPECT_EQ('e', e);
392   EXPECT_EQ('f', f);
393 }
394 
395 // Tests DoAll(a1, a2, ..., a10).
TEST(DoAllTest,TenActions)396 TEST(DoAllTest, TenActions) {
397   int m = 0, n = 0;
398   char a = '\0', b = '\0', c = '\0', d = '\0';
399   char e = '\0', f = '\0', g = '\0';
400   Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
401              char*, char*, char*)> action =
402       DoAll(SetArgPointee<0>(1),
403             SetArgPointee<1>(2),
404             SetArgPointee<2>('a'),
405             SetArgPointee<3>('b'),
406             SetArgPointee<4>('c'),
407             SetArgPointee<5>('d'),
408             SetArgPointee<6>('e'),
409             SetArgPointee<7>('f'),
410             SetArgPointee<8>('g'),
411             Return(3));
412   EXPECT_EQ(
413       3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
414   EXPECT_EQ(1, m);
415   EXPECT_EQ(2, n);
416   EXPECT_EQ('a', a);
417   EXPECT_EQ('b', b);
418   EXPECT_EQ('c', c);
419   EXPECT_EQ('d', d);
420   EXPECT_EQ('e', e);
421   EXPECT_EQ('f', f);
422   EXPECT_EQ('g', g);
423 }
424 
TEST(DoAllTest,NoArgs)425 TEST(DoAllTest, NoArgs) {
426   bool ran_first = false;
427   Action<bool()> a =
428       DoAll([&] { ran_first = true; }, [&] { return ran_first; });
429   EXPECT_TRUE(a.Perform({}));
430 }
431 
TEST(DoAllTest,MoveOnlyArgs)432 TEST(DoAllTest, MoveOnlyArgs) {
433   bool ran_first = false;
434   Action<int(std::unique_ptr<int>)> a =
435       DoAll(InvokeWithoutArgs([&] { ran_first = true; }),
436             [](std::unique_ptr<int> p) { return *p; });
437   EXPECT_EQ(7, a.Perform(std::make_tuple(std::unique_ptr<int>(new int(7)))));
438   EXPECT_TRUE(ran_first);
439 }
440 
TEST(DoAllTest,ImplicitlyConvertsActionArguments)441 TEST(DoAllTest, ImplicitlyConvertsActionArguments) {
442   bool ran_first = false;
443   // Action<void(std::vector<int>)> isn't an
444   // Action<void(const std::vector<int>&) but can be converted.
445   Action<void(std::vector<int>)> first = [&] { ran_first = true; };
446   Action<int(std::vector<int>)> a =
447       DoAll(first, [](std::vector<int> arg) { return arg.front(); });
448   EXPECT_EQ(7, a.Perform(std::make_tuple(std::vector<int>{7})));
449   EXPECT_TRUE(ran_first);
450 }
451 
452 // The ACTION*() macros trigger warning C4100 (unreferenced formal
453 // parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
454 // the macro definition, as the warnings are generated when the macro
455 // is expanded and macro expansion cannot contain #pragma.  Therefore
456 // we suppress them here.
457 // Also suppress C4503 decorated name length exceeded, name was truncated
458 #ifdef _MSC_VER
459 # pragma warning(push)
460 # pragma warning(disable:4100)
461 # pragma warning(disable:4503)
462 #endif
463 // Tests the ACTION*() macro family.
464 
465 // Tests that ACTION() can define an action that doesn't reference the
466 // mock function arguments.
ACTION(Return5)467 ACTION(Return5) { return 5; }
468 
TEST(ActionMacroTest,WorksWhenNotReferencingArguments)469 TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
470   Action<double()> a1 = Return5();
471   EXPECT_DOUBLE_EQ(5, a1.Perform(std::make_tuple()));
472 
473   Action<int(double, bool)> a2 = Return5();
474   EXPECT_EQ(5, a2.Perform(std::make_tuple(1, true)));
475 }
476 
477 // Tests that ACTION() can define an action that returns void.
ACTION(IncrementArg1)478 ACTION(IncrementArg1) { (*arg1)++; }
479 
TEST(ActionMacroTest,WorksWhenReturningVoid)480 TEST(ActionMacroTest, WorksWhenReturningVoid) {
481   Action<void(int, int*)> a1 = IncrementArg1();
482   int n = 0;
483   a1.Perform(std::make_tuple(5, &n));
484   EXPECT_EQ(1, n);
485 }
486 
487 // Tests that the body of ACTION() can reference the type of the
488 // argument.
ACTION(IncrementArg2)489 ACTION(IncrementArg2) {
490   StaticAssertTypeEq<int*, arg2_type>();
491   arg2_type temp = arg2;
492   (*temp)++;
493 }
494 
TEST(ActionMacroTest,CanReferenceArgumentType)495 TEST(ActionMacroTest, CanReferenceArgumentType) {
496   Action<void(int, bool, int*)> a1 = IncrementArg2();
497   int n = 0;
498   a1.Perform(std::make_tuple(5, false, &n));
499   EXPECT_EQ(1, n);
500 }
501 
502 // Tests that the body of ACTION() can reference the argument tuple
503 // via args_type and args.
ACTION(Sum2)504 ACTION(Sum2) {
505   StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
506   args_type args_copy = args;
507   return std::get<0>(args_copy) + std::get<1>(args_copy);
508 }
509 
TEST(ActionMacroTest,CanReferenceArgumentTuple)510 TEST(ActionMacroTest, CanReferenceArgumentTuple) {
511   Action<int(int, char, int*)> a1 = Sum2();
512   int dummy = 0;
513   EXPECT_EQ(11, a1.Perform(std::make_tuple(5, Char(6), &dummy)));
514 }
515 
516 // Tests that the body of ACTION() can reference the mock function
517 // type.
Dummy(bool flag)518 int Dummy(bool flag) { return flag? 1 : 0; }
519 
ACTION(InvokeDummy)520 ACTION(InvokeDummy) {
521   StaticAssertTypeEq<int(bool), function_type>();
522   function_type* fp = &Dummy;
523   return (*fp)(true);
524 }
525 
TEST(ActionMacroTest,CanReferenceMockFunctionType)526 TEST(ActionMacroTest, CanReferenceMockFunctionType) {
527   Action<int(bool)> a1 = InvokeDummy();
528   EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
529   EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
530 }
531 
532 // Tests that the body of ACTION() can reference the mock function's
533 // return type.
ACTION(InvokeDummy2)534 ACTION(InvokeDummy2) {
535   StaticAssertTypeEq<int, return_type>();
536   return_type result = Dummy(true);
537   return result;
538 }
539 
TEST(ActionMacroTest,CanReferenceMockFunctionReturnType)540 TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
541   Action<int(bool)> a1 = InvokeDummy2();
542   EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
543   EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
544 }
545 
546 // Tests that ACTION() works for arguments passed by const reference.
ACTION(ReturnAddrOfConstBoolReferenceArg)547 ACTION(ReturnAddrOfConstBoolReferenceArg) {
548   StaticAssertTypeEq<const bool&, arg1_type>();
549   return &arg1;
550 }
551 
TEST(ActionMacroTest,WorksForConstReferenceArg)552 TEST(ActionMacroTest, WorksForConstReferenceArg) {
553   Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
554   const bool b = false;
555   EXPECT_EQ(&b, a.Perform(std::tuple<int, const bool&>(0, b)));
556 }
557 
558 // Tests that ACTION() works for arguments passed by non-const reference.
ACTION(ReturnAddrOfIntReferenceArg)559 ACTION(ReturnAddrOfIntReferenceArg) {
560   StaticAssertTypeEq<int&, arg0_type>();
561   return &arg0;
562 }
563 
TEST(ActionMacroTest,WorksForNonConstReferenceArg)564 TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
565   Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
566   int n = 0;
567   EXPECT_EQ(&n, a.Perform(std::tuple<int&, bool, int>(n, true, 1)));
568 }
569 
570 // Tests that ACTION() can be used in a namespace.
571 namespace action_test {
ACTION(Sum)572 ACTION(Sum) { return arg0 + arg1; }
573 }  // namespace action_test
574 
TEST(ActionMacroTest,WorksInNamespace)575 TEST(ActionMacroTest, WorksInNamespace) {
576   Action<int(int, int)> a1 = action_test::Sum();
577   EXPECT_EQ(3, a1.Perform(std::make_tuple(1, 2)));
578 }
579 
580 // Tests that the same ACTION definition works for mock functions with
581 // different argument numbers.
ACTION(PlusTwo)582 ACTION(PlusTwo) { return arg0 + 2; }
583 
TEST(ActionMacroTest,WorksForDifferentArgumentNumbers)584 TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
585   Action<int(int)> a1 = PlusTwo();
586   EXPECT_EQ(4, a1.Perform(std::make_tuple(2)));
587 
588   Action<double(float, void*)> a2 = PlusTwo();
589   int dummy;
590   EXPECT_DOUBLE_EQ(6, a2.Perform(std::make_tuple(4.0f, &dummy)));
591 }
592 
593 // Tests that ACTION_P can define a parameterized action.
ACTION_P(Plus,n)594 ACTION_P(Plus, n) { return arg0 + n; }
595 
TEST(ActionPMacroTest,DefinesParameterizedAction)596 TEST(ActionPMacroTest, DefinesParameterizedAction) {
597   Action<int(int m, bool t)> a1 = Plus(9);
598   EXPECT_EQ(10, a1.Perform(std::make_tuple(1, true)));
599 }
600 
601 // Tests that the body of ACTION_P can reference the argument types
602 // and the parameter type.
ACTION_P(TypedPlus,n)603 ACTION_P(TypedPlus, n) {
604   arg0_type t1 = arg0;
605   n_type t2 = n;
606   return t1 + t2;
607 }
608 
TEST(ActionPMacroTest,CanReferenceArgumentAndParameterTypes)609 TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
610   Action<int(char m, bool t)> a1 = TypedPlus(9);
611   EXPECT_EQ(10, a1.Perform(std::make_tuple(Char(1), true)));
612 }
613 
614 // Tests that a parameterized action can be used in any mock function
615 // whose type is compatible.
TEST(ActionPMacroTest,WorksInCompatibleMockFunction)616 TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
617   Action<std::string(const std::string& s)> a1 = Plus("tail");
618   const std::string re = "re";
619   std::tuple<const std::string> dummy = std::make_tuple(re);
620   EXPECT_EQ("retail", a1.Perform(dummy));
621 }
622 
623 // Tests that we can use ACTION*() to define actions overloaded on the
624 // number of parameters.
625 
ACTION(OverloadedAction)626 ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
627 
ACTION_P(OverloadedAction,default_value)628 ACTION_P(OverloadedAction, default_value) {
629   return arg0 ? arg1 : default_value;
630 }
631 
ACTION_P2(OverloadedAction,true_value,false_value)632 ACTION_P2(OverloadedAction, true_value, false_value) {
633   return arg0 ? true_value : false_value;
634 }
635 
TEST(ActionMacroTest,CanDefineOverloadedActions)636 TEST(ActionMacroTest, CanDefineOverloadedActions) {
637   typedef Action<const char*(bool, const char*)> MyAction;
638 
639   const MyAction a1 = OverloadedAction();
640   EXPECT_STREQ("hello", a1.Perform(std::make_tuple(false, CharPtr("world"))));
641   EXPECT_STREQ("world", a1.Perform(std::make_tuple(true, CharPtr("world"))));
642 
643   const MyAction a2 = OverloadedAction("hi");
644   EXPECT_STREQ("hi", a2.Perform(std::make_tuple(false, CharPtr("world"))));
645   EXPECT_STREQ("world", a2.Perform(std::make_tuple(true, CharPtr("world"))));
646 
647   const MyAction a3 = OverloadedAction("hi", "you");
648   EXPECT_STREQ("hi", a3.Perform(std::make_tuple(true, CharPtr("world"))));
649   EXPECT_STREQ("you", a3.Perform(std::make_tuple(false, CharPtr("world"))));
650 }
651 
652 // Tests ACTION_Pn where n >= 3.
653 
ACTION_P3(Plus,m,n,k)654 ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
655 
TEST(ActionPnMacroTest,WorksFor3Parameters)656 TEST(ActionPnMacroTest, WorksFor3Parameters) {
657   Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
658   EXPECT_DOUBLE_EQ(3123.4, a1.Perform(std::make_tuple(3000, true)));
659 
660   Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
661   const std::string re = "re";
662   std::tuple<const std::string> dummy = std::make_tuple(re);
663   EXPECT_EQ("retail->", a2.Perform(dummy));
664 }
665 
ACTION_P4(Plus,p0,p1,p2,p3)666 ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
667 
TEST(ActionPnMacroTest,WorksFor4Parameters)668 TEST(ActionPnMacroTest, WorksFor4Parameters) {
669   Action<int(int)> a1 = Plus(1, 2, 3, 4);
670   EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(std::make_tuple(10)));
671 }
672 
ACTION_P5(Plus,p0,p1,p2,p3,p4)673 ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
674 
TEST(ActionPnMacroTest,WorksFor5Parameters)675 TEST(ActionPnMacroTest, WorksFor5Parameters) {
676   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
677   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(std::make_tuple(10)));
678 }
679 
ACTION_P6(Plus,p0,p1,p2,p3,p4,p5)680 ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
681   return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
682 }
683 
TEST(ActionPnMacroTest,WorksFor6Parameters)684 TEST(ActionPnMacroTest, WorksFor6Parameters) {
685   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
686   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(std::make_tuple(10)));
687 }
688 
ACTION_P7(Plus,p0,p1,p2,p3,p4,p5,p6)689 ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
690   return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
691 }
692 
TEST(ActionPnMacroTest,WorksFor7Parameters)693 TEST(ActionPnMacroTest, WorksFor7Parameters) {
694   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
695   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(std::make_tuple(10)));
696 }
697 
ACTION_P8(Plus,p0,p1,p2,p3,p4,p5,p6,p7)698 ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
699   return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
700 }
701 
TEST(ActionPnMacroTest,WorksFor8Parameters)702 TEST(ActionPnMacroTest, WorksFor8Parameters) {
703   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
704   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
705             a1.Perform(std::make_tuple(10)));
706 }
707 
ACTION_P9(Plus,p0,p1,p2,p3,p4,p5,p6,p7,p8)708 ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
709   return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
710 }
711 
TEST(ActionPnMacroTest,WorksFor9Parameters)712 TEST(ActionPnMacroTest, WorksFor9Parameters) {
713   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
714   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
715             a1.Perform(std::make_tuple(10)));
716 }
717 
ACTION_P10(Plus,p0,p1,p2,p3,p4,p5,p6,p7,p8,last_param)718 ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
719   arg0_type t0 = arg0;
720   last_param_type t9 = last_param;
721   return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
722 }
723 
TEST(ActionPnMacroTest,WorksFor10Parameters)724 TEST(ActionPnMacroTest, WorksFor10Parameters) {
725   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
726   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
727             a1.Perform(std::make_tuple(10)));
728 }
729 
730 // Tests that the action body can promote the parameter types.
731 
ACTION_P2(PadArgument,prefix,suffix)732 ACTION_P2(PadArgument, prefix, suffix) {
733   // The following lines promote the two parameters to desired types.
734   std::string prefix_str(prefix);
735   char suffix_char = static_cast<char>(suffix);
736   return prefix_str + arg0 + suffix_char;
737 }
738 
TEST(ActionPnMacroTest,SimpleTypePromotion)739 TEST(ActionPnMacroTest, SimpleTypePromotion) {
740   Action<std::string(const char*)> no_promo =
741       PadArgument(std::string("foo"), 'r');
742   Action<std::string(const char*)> promo =
743       PadArgument("foo", static_cast<int>('r'));
744   EXPECT_EQ("foobar", no_promo.Perform(std::make_tuple(CharPtr("ba"))));
745   EXPECT_EQ("foobar", promo.Perform(std::make_tuple(CharPtr("ba"))));
746 }
747 
748 // Tests that we can partially restrict parameter types using a
749 // straight-forward pattern.
750 
751 // Defines a generic action that doesn't restrict the types of its
752 // parameters.
ACTION_P3(ConcatImpl,a,b,c)753 ACTION_P3(ConcatImpl, a, b, c) {
754   std::stringstream ss;
755   ss << a << b << c;
756   return ss.str();
757 }
758 
759 // Next, we try to restrict that either the first parameter is a
760 // string, or the second parameter is an int.
761 
762 // Defines a partially specialized wrapper that restricts the first
763 // parameter to std::string.
764 template <typename T1, typename T2>
765 // ConcatImplActionP3 is the class template ACTION_P3 uses to
766 // implement ConcatImpl.  We shouldn't change the name as this
767 // pattern requires the user to use it directly.
768 ConcatImplActionP3<std::string, T1, T2>
Concat(const std::string & a,T1 b,T2 c)769 Concat(const std::string& a, T1 b, T2 c) {
770   GTEST_INTENTIONAL_CONST_COND_PUSH_()
771   if (true) {
772   GTEST_INTENTIONAL_CONST_COND_POP_()
773     // This branch verifies that ConcatImpl() can be invoked without
774     // explicit template arguments.
775     return ConcatImpl(a, b, c);
776   } else {
777     // This branch verifies that ConcatImpl() can also be invoked with
778     // explicit template arguments.  It doesn't really need to be
779     // executed as this is a compile-time verification.
780     return ConcatImpl<std::string, T1, T2>(a, b, c);
781   }
782 }
783 
784 // Defines another partially specialized wrapper that restricts the
785 // second parameter to int.
786 template <typename T1, typename T2>
787 ConcatImplActionP3<T1, int, T2>
Concat(T1 a,int b,T2 c)788 Concat(T1 a, int b, T2 c) {
789   return ConcatImpl(a, b, c);
790 }
791 
TEST(ActionPnMacroTest,CanPartiallyRestrictParameterTypes)792 TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
793   Action<const std::string()> a1 = Concat("Hello", "1", 2);
794   EXPECT_EQ("Hello12", a1.Perform(std::make_tuple()));
795 
796   a1 = Concat(1, 2, 3);
797   EXPECT_EQ("123", a1.Perform(std::make_tuple()));
798 }
799 
800 // Verifies the type of an ACTION*.
801 
ACTION(DoFoo)802 ACTION(DoFoo) {}
ACTION_P(DoFoo,p)803 ACTION_P(DoFoo, p) {}
ACTION_P2(DoFoo,p0,p1)804 ACTION_P2(DoFoo, p0, p1) {}
805 
TEST(ActionPnMacroTest,TypesAreCorrect)806 TEST(ActionPnMacroTest, TypesAreCorrect) {
807   // DoFoo() must be assignable to a DoFooAction variable.
808   DoFooAction a0 = DoFoo();
809 
810   // DoFoo(1) must be assignable to a DoFooActionP variable.
811   DoFooActionP<int> a1 = DoFoo(1);
812 
813   // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
814   // variable, and so on.
815   DoFooActionP2<int, char> a2 = DoFoo(1, '2');
816   PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
817   PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
818   PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
819   PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
820   PlusActionP7<int, int, int, int, int, int, char> a7 =
821       Plus(1, 2, 3, 4, 5, 6, '7');
822   PlusActionP8<int, int, int, int, int, int, int, char> a8 =
823       Plus(1, 2, 3, 4, 5, 6, 7, '8');
824   PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
825       Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
826   PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
827       Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
828 
829   // Avoid "unused variable" warnings.
830   (void)a0;
831   (void)a1;
832   (void)a2;
833   (void)a3;
834   (void)a4;
835   (void)a5;
836   (void)a6;
837   (void)a7;
838   (void)a8;
839   (void)a9;
840   (void)a10;
841 }
842 
843 // Tests that an ACTION_P*() action can be explicitly instantiated
844 // with reference-typed parameters.
845 
ACTION_P(Plus1,x)846 ACTION_P(Plus1, x) { return x; }
ACTION_P2(Plus2,x,y)847 ACTION_P2(Plus2, x, y) { return x + y; }
ACTION_P3(Plus3,x,y,z)848 ACTION_P3(Plus3, x, y, z) { return x + y + z; }
ACTION_P10(Plus10,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9)849 ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
850   return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
851 }
852 
TEST(ActionPnMacroTest,CanExplicitlyInstantiateWithReferenceTypes)853 TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
854   int x = 1, y = 2, z = 3;
855   const std::tuple<> empty = std::make_tuple();
856 
857   Action<int()> a = Plus1<int&>(x);
858   EXPECT_EQ(1, a.Perform(empty));
859 
860   a = Plus2<const int&, int&>(x, y);
861   EXPECT_EQ(3, a.Perform(empty));
862 
863   a = Plus3<int&, const int&, int&>(x, y, z);
864   EXPECT_EQ(6, a.Perform(empty));
865 
866   int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
867   a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
868       int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
869                               n[8], n[9]);
870   EXPECT_EQ(55, a.Perform(empty));
871 }
872 
873 
874 class TenArgConstructorClass {
875  public:
TenArgConstructorClass(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int a10)876   TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
877                          int a6, int a7, int a8, int a9, int a10)
878     : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
879   }
880   int value_;
881 };
882 
883 // Tests that ACTION_TEMPLATE works when there is no value parameter.
ACTION_TEMPLATE(CreateNew,HAS_1_TEMPLATE_PARAMS (typename,T),AND_0_VALUE_PARAMS ())884 ACTION_TEMPLATE(CreateNew,
885                 HAS_1_TEMPLATE_PARAMS(typename, T),
886                 AND_0_VALUE_PARAMS()) {
887   return new T;
888 }
889 
TEST(ActionTemplateTest,WorksWithoutValueParam)890 TEST(ActionTemplateTest, WorksWithoutValueParam) {
891   const Action<int*()> a = CreateNew<int>();
892   int* p = a.Perform(std::make_tuple());
893   delete p;
894 }
895 
896 // Tests that ACTION_TEMPLATE works when there are value parameters.
ACTION_TEMPLATE(CreateNew,HAS_1_TEMPLATE_PARAMS (typename,T),AND_1_VALUE_PARAMS (a0))897 ACTION_TEMPLATE(CreateNew,
898                 HAS_1_TEMPLATE_PARAMS(typename, T),
899                 AND_1_VALUE_PARAMS(a0)) {
900   return new T(a0);
901 }
902 
TEST(ActionTemplateTest,WorksWithValueParams)903 TEST(ActionTemplateTest, WorksWithValueParams) {
904   const Action<int*()> a = CreateNew<int>(42);
905   int* p = a.Perform(std::make_tuple());
906   EXPECT_EQ(42, *p);
907   delete p;
908 }
909 
910 // Tests that ACTION_TEMPLATE works for integral template parameters.
ACTION_TEMPLATE(MyDeleteArg,HAS_1_TEMPLATE_PARAMS (int,k),AND_0_VALUE_PARAMS ())911 ACTION_TEMPLATE(MyDeleteArg,
912                 HAS_1_TEMPLATE_PARAMS(int, k),
913                 AND_0_VALUE_PARAMS()) {
914   delete std::get<k>(args);
915 }
916 
917 // Resets a bool variable in the destructor.
918 class BoolResetter {
919  public:
BoolResetter(bool * value)920   explicit BoolResetter(bool* value) : value_(value) {}
~BoolResetter()921   ~BoolResetter() { *value_ = false; }
922  private:
923   bool* value_;
924 };
925 
TEST(ActionTemplateTest,WorksForIntegralTemplateParams)926 TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
927   const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
928   int n = 0;
929   bool b = true;
930   BoolResetter* resetter = new BoolResetter(&b);
931   a.Perform(std::make_tuple(&n, resetter));
932   EXPECT_FALSE(b);  // Verifies that resetter is deleted.
933 }
934 
935 // Tests that ACTION_TEMPLATES works for template template parameters.
ACTION_TEMPLATE(ReturnSmartPointer,HAS_1_TEMPLATE_PARAMS (template<typename Pointee> class,Pointer),AND_1_VALUE_PARAMS (pointee))936 ACTION_TEMPLATE(ReturnSmartPointer,
937                 HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
938                                       Pointer),
939                 AND_1_VALUE_PARAMS(pointee)) {
940   return Pointer<pointee_type>(new pointee_type(pointee));
941 }
942 
TEST(ActionTemplateTest,WorksForTemplateTemplateParameters)943 TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
944   const Action<std::shared_ptr<int>()> a =
945       ReturnSmartPointer<std::shared_ptr>(42);
946   std::shared_ptr<int> p = a.Perform(std::make_tuple());
947   EXPECT_EQ(42, *p);
948 }
949 
950 // Tests that ACTION_TEMPLATE works for 10 template parameters.
951 template <typename T1, typename T2, typename T3, int k4, bool k5,
952           unsigned int k6, typename T7, typename T8, typename T9>
953 struct GiantTemplate {
954  public:
GiantTemplatetesting::gmock_generated_actions_test::GiantTemplate955   explicit GiantTemplate(int a_value) : value(a_value) {}
956   int value;
957 };
958 
ACTION_TEMPLATE(ReturnGiant,HAS_10_TEMPLATE_PARAMS (typename,T1,typename,T2,typename,T3,int,k4,bool,k5,unsigned int,k6,class,T7,class,T8,class,T9,template<typename T> class,T10),AND_1_VALUE_PARAMS (value))959 ACTION_TEMPLATE(ReturnGiant,
960                 HAS_10_TEMPLATE_PARAMS(
961                     typename, T1,
962                     typename, T2,
963                     typename, T3,
964                     int, k4,
965                     bool, k5,
966                     unsigned int, k6,
967                     class, T7,
968                     class, T8,
969                     class, T9,
970                     template <typename T> class, T10),
971                 AND_1_VALUE_PARAMS(value)) {
972   return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
973 }
974 
TEST(ActionTemplateTest,WorksFor10TemplateParameters)975 TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
976   using Giant = GiantTemplate<std::shared_ptr<int>, bool, double, 5, true, 6,
977                               char, unsigned, int>;
978   const Action<Giant()> a = ReturnGiant<int, bool, double, 5, true, 6, char,
979                                         unsigned, int, std::shared_ptr>(42);
980   Giant giant = a.Perform(std::make_tuple());
981   EXPECT_EQ(42, giant.value);
982 }
983 
984 // Tests that ACTION_TEMPLATE works for 10 value parameters.
ACTION_TEMPLATE(ReturnSum,HAS_1_TEMPLATE_PARAMS (typename,Number),AND_10_VALUE_PARAMS (v1,v2,v3,v4,v5,v6,v7,v8,v9,v10))985 ACTION_TEMPLATE(ReturnSum,
986                 HAS_1_TEMPLATE_PARAMS(typename, Number),
987                 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
988   return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
989 }
990 
TEST(ActionTemplateTest,WorksFor10ValueParameters)991 TEST(ActionTemplateTest, WorksFor10ValueParameters) {
992   const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
993   EXPECT_EQ(55, a.Perform(std::make_tuple()));
994 }
995 
996 // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
997 // on the number of value parameters.
998 
ACTION(ReturnSum)999 ACTION(ReturnSum) { return 0; }
1000 
ACTION_P(ReturnSum,x)1001 ACTION_P(ReturnSum, x) { return x; }
1002 
ACTION_TEMPLATE(ReturnSum,HAS_1_TEMPLATE_PARAMS (typename,Number),AND_2_VALUE_PARAMS (v1,v2))1003 ACTION_TEMPLATE(ReturnSum,
1004                 HAS_1_TEMPLATE_PARAMS(typename, Number),
1005                 AND_2_VALUE_PARAMS(v1, v2)) {
1006   return static_cast<Number>(v1) + v2;
1007 }
1008 
ACTION_TEMPLATE(ReturnSum,HAS_1_TEMPLATE_PARAMS (typename,Number),AND_3_VALUE_PARAMS (v1,v2,v3))1009 ACTION_TEMPLATE(ReturnSum,
1010                 HAS_1_TEMPLATE_PARAMS(typename, Number),
1011                 AND_3_VALUE_PARAMS(v1, v2, v3)) {
1012   return static_cast<Number>(v1) + v2 + v3;
1013 }
1014 
ACTION_TEMPLATE(ReturnSum,HAS_2_TEMPLATE_PARAMS (typename,Number,int,k),AND_4_VALUE_PARAMS (v1,v2,v3,v4))1015 ACTION_TEMPLATE(ReturnSum,
1016                 HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
1017                 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1018   return static_cast<Number>(v1) + v2 + v3 + v4 + k;
1019 }
1020 
TEST(ActionTemplateTest,CanBeOverloadedOnNumberOfValueParameters)1021 TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1022   const Action<int()> a0 = ReturnSum();
1023   const Action<int()> a1 = ReturnSum(1);
1024   const Action<int()> a2 = ReturnSum<int>(1, 2);
1025   const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
1026   const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1027   EXPECT_EQ(0, a0.Perform(std::make_tuple()));
1028   EXPECT_EQ(1, a1.Perform(std::make_tuple()));
1029   EXPECT_EQ(3, a2.Perform(std::make_tuple()));
1030   EXPECT_EQ(6, a3.Perform(std::make_tuple()));
1031   EXPECT_EQ(12345, a4.Perform(std::make_tuple()));
1032 }
1033 
1034 
1035 }  // namespace gmock_generated_actions_test
1036 }  // namespace testing
1037