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