gmock-generated-actions_test.cc revision dd1c93d5709e32713961cfd95fe30489a4ad2d26
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// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests the built-in actions generated by a script.
35
36#include <gmock/gmock-generated-actions.h>
37
38#include <functional>
39#include <string>
40#include <gmock/gmock.h>
41#include <gtest/gtest.h>
42
43namespace testing {
44namespace gmock_generated_actions_test {
45
46using ::std::plus;
47using ::std::string;
48using ::std::tr1::get;
49using ::std::tr1::make_tuple;
50using ::std::tr1::tuple;
51using ::std::tr1::tuple_element;
52using testing::_;
53using testing::Action;
54using testing::ActionInterface;
55using testing::ByRef;
56using testing::DeleteArg;
57using testing::DoAll;
58using testing::Invoke;
59using testing::InvokeArgument;
60using testing::Return;
61using testing::ReturnNew;
62using testing::SaveArg;
63using testing::SetArgReferee;
64using testing::SetArgumentPointee;
65using testing::StaticAssertTypeEq;
66using testing::Unused;
67using testing::WithArg;
68using testing::WithArgs;
69using testing::WithoutArgs;
70
71// Sample functions and functors for testing Invoke() and etc.
72int Nullary() { return 1; }
73
74class NullaryFunctor {
75 public:
76  int operator()() { return 2; }
77};
78
79bool g_done = false;
80void VoidNullary() { g_done = true; }
81
82class VoidNullaryFunctor {
83 public:
84  void operator()() { g_done = true; }
85};
86
87bool Unary(int x) { return x < 0; }
88
89const char* Plus1(const char* s) { return s + 1; }
90
91void VoidUnary(int n) { g_done = true; }
92
93bool ByConstRef(const string& s) { return s == "Hi"; }
94
95const double g_double = 0;
96bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
97
98string ByNonConstRef(string& s) { return s += "+"; }  // NOLINT
99
100struct UnaryFunctor {
101  int operator()(bool x) { return x ? 1 : -1; }
102};
103
104const char* Binary(const char* input, short n) { return input + n; }  // NOLINT
105
106void VoidBinary(int, char) { g_done = true; }
107
108int Ternary(int x, char y, short z) { return x + y + z; }  // NOLINT
109
110void VoidTernary(int, char, bool) { g_done = true; }
111
112int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
113
114int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
115
116void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
117
118string Concat4(const char* s1, const char* s2, const char* s3,
119               const char* s4) {
120  return string(s1) + s2 + s3 + s4;
121}
122
123int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
124
125struct SumOf5Functor {
126  int operator()(int a, int b, int c, int d, int e) {
127    return a + b + c + d + e;
128  }
129};
130
131string Concat5(const char* s1, const char* s2, const char* s3,
132               const char* s4, const char* s5) {
133  return string(s1) + s2 + s3 + s4 + s5;
134}
135
136int SumOf6(int a, int b, int c, int d, int e, int f) {
137  return a + b + c + d + e + f;
138}
139
140struct SumOf6Functor {
141  int operator()(int a, int b, int c, int d, int e, int f) {
142    return a + b + c + d + e + f;
143  }
144};
145
146string Concat6(const char* s1, const char* s2, const char* s3,
147               const char* s4, const char* s5, const char* s6) {
148  return string(s1) + s2 + s3 + s4 + s5 + s6;
149}
150
151string Concat7(const char* s1, const char* s2, const char* s3,
152               const char* s4, const char* s5, const char* s6,
153               const char* s7) {
154  return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
155}
156
157string Concat8(const char* s1, const char* s2, const char* s3,
158               const char* s4, const char* s5, const char* s6,
159               const char* s7, const char* s8) {
160  return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
161}
162
163string Concat9(const char* s1, const char* s2, const char* s3,
164               const char* s4, const char* s5, const char* s6,
165               const char* s7, const char* s8, const char* s9) {
166  return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
167}
168
169string Concat10(const char* s1, const char* s2, const char* s3,
170                const char* s4, const char* s5, const char* s6,
171                const char* s7, const char* s8, const char* s9,
172                const char* s10) {
173  return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
174}
175
176class Foo {
177 public:
178  Foo() : value_(123) {}
179
180  int Nullary() const { return value_; }
181
182  short Unary(long x) { return static_cast<short>(value_ + x); }  // NOLINT
183
184  string Binary(const string& str, char c) const { return str + c; }
185
186  int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
187
188  int SumOf4(int a, int b, int c, int d) const {
189    return a + b + c + d + value_;
190  }
191
192  int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
193
194  int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
195
196  int SumOf6(int a, int b, int c, int d, int e, int f) {
197    return a + b + c + d + e + f;
198  }
199
200  string Concat7(const char* s1, const char* s2, const char* s3,
201                 const char* s4, const char* s5, const char* s6,
202                 const char* s7) {
203    return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
204  }
205
206  string Concat8(const char* s1, const char* s2, const char* s3,
207                 const char* s4, const char* s5, const char* s6,
208                 const char* s7, const char* s8) {
209    return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
210  }
211
212  string Concat9(const char* s1, const char* s2, const char* s3,
213                 const char* s4, const char* s5, const char* s6,
214                 const char* s7, const char* s8, const char* s9) {
215    return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
216  }
217
218  string Concat10(const char* s1, const char* s2, const char* s3,
219                  const char* s4, const char* s5, const char* s6,
220                  const char* s7, const char* s8, const char* s9,
221                  const char* s10) {
222    return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
223  }
224 private:
225  int value_;
226};
227
228// Tests using Invoke() with a nullary function.
229TEST(InvokeTest, Nullary) {
230  Action<int()> a = Invoke(Nullary);  // NOLINT
231  EXPECT_EQ(1, a.Perform(make_tuple()));
232}
233
234// Tests using Invoke() with a unary function.
235TEST(InvokeTest, Unary) {
236  Action<bool(int)> a = Invoke(Unary);  // NOLINT
237  EXPECT_FALSE(a.Perform(make_tuple(1)));
238  EXPECT_TRUE(a.Perform(make_tuple(-1)));
239}
240
241// Tests using Invoke() with a binary function.
242TEST(InvokeTest, Binary) {
243  Action<const char*(const char*, short)> a = Invoke(Binary);  // NOLINT
244  const char* p = "Hello";
245  EXPECT_EQ(p + 2, a.Perform(make_tuple(p, 2)));
246}
247
248// Tests using Invoke() with a ternary function.
249TEST(InvokeTest, Ternary) {
250  Action<int(int, char, short)> a = Invoke(Ternary);  // NOLINT
251  EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', 3)));
252}
253
254// Tests using Invoke() with a 4-argument function.
255TEST(InvokeTest, FunctionThatTakes4Arguments) {
256  Action<int(int, int, int, int)> a = Invoke(SumOf4);  // NOLINT
257  EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4)));
258}
259
260// Tests using Invoke() with a 5-argument function.
261TEST(InvokeTest, FunctionThatTakes5Arguments) {
262  Action<int(int, int, int, int, int)> a = Invoke(SumOf5);  // NOLINT
263  EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
264}
265
266// Tests using Invoke() with a 6-argument function.
267TEST(InvokeTest, FunctionThatTakes6Arguments) {
268  Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6);  // NOLINT
269  EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
270}
271
272// A helper that turns the type of a C-string literal from const
273// char[N] to const char*.
274inline const char* CharPtr(const char* s) { return s; }
275
276// Tests using Invoke() with a 7-argument function.
277TEST(InvokeTest, FunctionThatTakes7Arguments) {
278  Action<string(const char*, const char*, const char*, const char*,
279                const char*, const char*, const char*)> a =
280      Invoke(Concat7);
281  EXPECT_EQ("1234567",
282            a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
283                                 CharPtr("4"), CharPtr("5"), CharPtr("6"),
284                                 CharPtr("7"))));
285}
286
287// Tests using Invoke() with a 8-argument function.
288TEST(InvokeTest, FunctionThatTakes8Arguments) {
289  Action<string(const char*, const char*, const char*, const char*,
290                const char*, const char*, const char*, const char*)> a =
291      Invoke(Concat8);
292  EXPECT_EQ("12345678",
293            a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
294                                 CharPtr("4"), CharPtr("5"), CharPtr("6"),
295                                 CharPtr("7"), CharPtr("8"))));
296}
297
298// Tests using Invoke() with a 9-argument function.
299TEST(InvokeTest, FunctionThatTakes9Arguments) {
300  Action<string(const char*, const char*, const char*, const char*,
301                const char*, const char*, const char*, const char*,
302                const char*)> a = Invoke(Concat9);
303  EXPECT_EQ("123456789",
304            a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
305                                 CharPtr("4"), CharPtr("5"), CharPtr("6"),
306                                 CharPtr("7"), CharPtr("8"), CharPtr("9"))));
307}
308
309// Tests using Invoke() with a 10-argument function.
310TEST(InvokeTest, FunctionThatTakes10Arguments) {
311  Action<string(const char*, const char*, const char*, const char*,
312                const char*, const char*, const char*, const char*,
313                const char*, const char*)> a = Invoke(Concat10);
314  EXPECT_EQ("1234567890",
315            a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
316                                 CharPtr("4"), CharPtr("5"), CharPtr("6"),
317                                 CharPtr("7"), CharPtr("8"), CharPtr("9"),
318                                 CharPtr("0"))));
319}
320
321// Tests using Invoke() with functions with parameters declared as Unused.
322TEST(InvokeTest, FunctionWithUnusedParameters) {
323  Action<int(int, int, double, const string&)> a1 =
324      Invoke(SumOfFirst2);
325  EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi"))));
326
327  Action<int(int, int, bool, int*)> a2 =
328      Invoke(SumOfFirst2);
329  EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL))));
330}
331
332// Tests using Invoke() with methods with parameters declared as Unused.
333TEST(InvokeTest, MethodWithUnusedParameters) {
334  Foo foo;
335  Action<int(string, bool, int, int)> a1 =
336      Invoke(&foo, &Foo::SumOfLast2);
337  EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2)));
338
339  Action<int(char, double, int, int)> a2 =
340      Invoke(&foo, &Foo::SumOfLast2);
341  EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3)));
342}
343
344// Tests using Invoke() with a functor.
345TEST(InvokeTest, Functor) {
346  Action<int(short, char)> a = Invoke(plus<short>());  // NOLINT
347  EXPECT_EQ(3, a.Perform(make_tuple(1, 2)));
348}
349
350// Tests using Invoke(f) as an action of a compatible type.
351TEST(InvokeTest, FunctionWithCompatibleType) {
352  Action<long(int, short, char, bool)> a = Invoke(SumOf4);  // NOLINT
353  EXPECT_EQ(4321, a.Perform(make_tuple(4000, 300, 20, true)));
354}
355
356// Tests using Invoke() with an object pointer and a method pointer.
357
358// Tests using Invoke() with a nullary method.
359TEST(InvokeMethodTest, Nullary) {
360  Foo foo;
361  Action<int()> a = Invoke(&foo, &Foo::Nullary);  // NOLINT
362  EXPECT_EQ(123, a.Perform(make_tuple()));
363}
364
365// Tests using Invoke() with a unary method.
366TEST(InvokeMethodTest, Unary) {
367  Foo foo;
368  Action<short(long)> a = Invoke(&foo, &Foo::Unary);  // NOLINT
369  EXPECT_EQ(4123, a.Perform(make_tuple(4000)));
370}
371
372// Tests using Invoke() with a binary method.
373TEST(InvokeMethodTest, Binary) {
374  Foo foo;
375  Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary);
376  string s("Hell");
377  EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o')));
378}
379
380// Tests using Invoke() with a ternary method.
381TEST(InvokeMethodTest, Ternary) {
382  Foo foo;
383  Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary);  // NOLINT
384  EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, 1)));
385}
386
387// Tests using Invoke() with a 4-argument method.
388TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
389  Foo foo;
390  Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4);  // NOLINT
391  EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4)));
392}
393
394// Tests using Invoke() with a 5-argument method.
395TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
396  Foo foo;
397  Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5);  // NOLINT
398  EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
399}
400
401// Tests using Invoke() with a 6-argument method.
402TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
403  Foo foo;
404  Action<int(int, int, int, int, int, int)> a =  // NOLINT
405      Invoke(&foo, &Foo::SumOf6);
406  EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
407}
408
409// Tests using Invoke() with a 7-argument method.
410TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
411  Foo foo;
412  Action<string(const char*, const char*, const char*, const char*,
413                const char*, const char*, const char*)> a =
414      Invoke(&foo, &Foo::Concat7);
415  EXPECT_EQ("1234567",
416            a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
417                                 CharPtr("4"), CharPtr("5"), CharPtr("6"),
418                                 CharPtr("7"))));
419}
420
421// Tests using Invoke() with a 8-argument method.
422TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
423  Foo foo;
424  Action<string(const char*, const char*, const char*, const char*,
425                const char*, const char*, const char*, const char*)> a =
426      Invoke(&foo, &Foo::Concat8);
427  EXPECT_EQ("12345678",
428            a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
429                                 CharPtr("4"), CharPtr("5"), CharPtr("6"),
430                                 CharPtr("7"), CharPtr("8"))));
431}
432
433// Tests using Invoke() with a 9-argument method.
434TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
435  Foo foo;
436  Action<string(const char*, const char*, const char*, const char*,
437                const char*, const char*, const char*, const char*,
438                const char*)> a = Invoke(&foo, &Foo::Concat9);
439  EXPECT_EQ("123456789",
440            a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
441                                 CharPtr("4"), CharPtr("5"), CharPtr("6"),
442                                 CharPtr("7"), CharPtr("8"), CharPtr("9"))));
443}
444
445// Tests using Invoke() with a 10-argument method.
446TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
447  Foo foo;
448  Action<string(const char*, const char*, const char*, const char*,
449                const char*, const char*, const char*, const char*,
450                const char*, const char*)> a = Invoke(&foo, &Foo::Concat10);
451  EXPECT_EQ("1234567890",
452            a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
453                                 CharPtr("4"), CharPtr("5"), CharPtr("6"),
454                                 CharPtr("7"), CharPtr("8"), CharPtr("9"),
455                                 CharPtr("0"))));
456}
457
458// Tests using Invoke(f) as an action of a compatible type.
459TEST(InvokeMethodTest, MethodWithCompatibleType) {
460  Foo foo;
461  Action<long(int, short, char, bool)> a =  // NOLINT
462      Invoke(&foo, &Foo::SumOf4);
463  EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true)));
464}
465
466// Tests ByRef().
467
468// Tests that ReferenceWrapper<T> is copyable.
469TEST(ByRefTest, IsCopyable) {
470  const string s1 = "Hi";
471  const string s2 = "Hello";
472
473  ::testing::internal::ReferenceWrapper<const string> ref_wrapper = ByRef(s1);
474  const string& r1 = ref_wrapper;
475  EXPECT_EQ(&s1, &r1);
476
477  // Assigns a new value to ref_wrapper.
478  ref_wrapper = ByRef(s2);
479  const string& r2 = ref_wrapper;
480  EXPECT_EQ(&s2, &r2);
481
482  ::testing::internal::ReferenceWrapper<const string> ref_wrapper1 = ByRef(s1);
483  // Copies ref_wrapper1 to ref_wrapper.
484  ref_wrapper = ref_wrapper1;
485  const string& r3 = ref_wrapper;
486  EXPECT_EQ(&s1, &r3);
487}
488
489// Tests using ByRef() on a const value.
490TEST(ByRefTest, ConstValue) {
491  const int n = 0;
492  // int& ref = ByRef(n);  // This shouldn't compile - we have a
493                           // negative compilation test to catch it.
494  const int& const_ref = ByRef(n);
495  EXPECT_EQ(&n, &const_ref);
496}
497
498// Tests using ByRef() on a non-const value.
499TEST(ByRefTest, NonConstValue) {
500  int n = 0;
501
502  // ByRef(n) can be used as either an int&,
503  int& ref = ByRef(n);
504  EXPECT_EQ(&n, &ref);
505
506  // or a const int&.
507  const int& const_ref = ByRef(n);
508  EXPECT_EQ(&n, &const_ref);
509}
510
511struct Base {
512  bool operator==(const Base&) { return true; }
513};
514
515struct Derived : public Base {
516  bool operator==(const Derived&) { return true; }
517};
518
519// Tests explicitly specifying the type when using ByRef().
520TEST(ByRefTest, ExplicitType) {
521  int n = 0;
522  const int& r1 = ByRef<const int>(n);
523  EXPECT_EQ(&n, &r1);
524
525  // ByRef<char>(n);  // This shouldn't compile - we have a negative
526                      // compilation test to catch it.
527
528
529  Derived d;
530  Derived& r2 = ByRef<Derived>(d);
531  EXPECT_EQ(&d, &r2);
532
533  const Derived& r3 = ByRef<const Derived>(d);
534  EXPECT_EQ(&d, &r3);
535
536  Base& r4 = ByRef<Base>(d);
537  EXPECT_EQ(&d, &r4);
538
539  const Base& r5 = ByRef<const Base>(d);
540  EXPECT_EQ(&d, &r5);
541
542  // The following shouldn't compile - we have a negative compilation
543  // test for it.
544  //
545  // Base b;
546  // ByRef<Derived>(b);
547}
548
549// Tests InvokeArgument<N>(...).
550
551// Tests using InvokeArgument with a nullary function.
552TEST(InvokeArgumentTest, Function0) {
553  Action<int(int, int(*)())> a = InvokeArgument<1>();  // NOLINT
554  EXPECT_EQ(1, a.Perform(make_tuple(2, &Nullary)));
555}
556
557// Tests using InvokeArgument with a unary function.
558TEST(InvokeArgumentTest, Functor1) {
559  Action<int(UnaryFunctor)> a = InvokeArgument<0>(true);  // NOLINT
560  EXPECT_EQ(1, a.Perform(make_tuple(UnaryFunctor())));
561}
562
563// Tests using InvokeArgument with a 5-ary function.
564TEST(InvokeArgumentTest, Function5) {
565  Action<int(int(*)(int, int, int, int, int))> a =  // NOLINT
566      InvokeArgument<0>(10000, 2000, 300, 40, 5);
567  EXPECT_EQ(12345, a.Perform(make_tuple(&SumOf5)));
568}
569
570// Tests using InvokeArgument with a 5-ary functor.
571TEST(InvokeArgumentTest, Functor5) {
572  Action<int(SumOf5Functor)> a =  // NOLINT
573      InvokeArgument<0>(10000, 2000, 300, 40, 5);
574  EXPECT_EQ(12345, a.Perform(make_tuple(SumOf5Functor())));
575}
576
577// Tests using InvokeArgument with a 6-ary function.
578TEST(InvokeArgumentTest, Function6) {
579  Action<int(int(*)(int, int, int, int, int, int))> a =  // NOLINT
580      InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
581  EXPECT_EQ(123456, a.Perform(make_tuple(&SumOf6)));
582}
583
584// Tests using InvokeArgument with a 6-ary functor.
585TEST(InvokeArgumentTest, Functor6) {
586  Action<int(SumOf6Functor)> a =  // NOLINT
587      InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
588  EXPECT_EQ(123456, a.Perform(make_tuple(SumOf6Functor())));
589}
590
591// Tests using InvokeArgument with a 7-ary function.
592TEST(InvokeArgumentTest, Function7) {
593  Action<string(string(*)(const char*, const char*, const char*,
594                          const char*, const char*, const char*,
595                          const char*))> a =
596      InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
597  EXPECT_EQ("1234567", a.Perform(make_tuple(&Concat7)));
598}
599
600// Tests using InvokeArgument with a 8-ary function.
601TEST(InvokeArgumentTest, Function8) {
602  Action<string(string(*)(const char*, const char*, const char*,
603                          const char*, const char*, const char*,
604                          const char*, const char*))> a =
605      InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
606  EXPECT_EQ("12345678", a.Perform(make_tuple(&Concat8)));
607}
608
609// Tests using InvokeArgument with a 9-ary function.
610TEST(InvokeArgumentTest, Function9) {
611  Action<string(string(*)(const char*, const char*, const char*,
612                          const char*, const char*, const char*,
613                          const char*, const char*, const char*))> a =
614      InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
615  EXPECT_EQ("123456789", a.Perform(make_tuple(&Concat9)));
616}
617
618// Tests using InvokeArgument with a 10-ary function.
619TEST(InvokeArgumentTest, Function10) {
620  Action<string(string(*)(const char*, const char*, const char*,
621                          const char*, const char*, const char*,
622                          const char*, const char*, const char*,
623                          const char*))> a =
624      InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
625  EXPECT_EQ("1234567890", a.Perform(make_tuple(&Concat10)));
626}
627
628// Tests using InvokeArgument with a function that takes a pointer argument.
629TEST(InvokeArgumentTest, ByPointerFunction) {
630  Action<const char*(const char*(*)(const char* input, short n))> a =  // NOLINT
631      InvokeArgument<0>(static_cast<const char*>("Hi"), 1);
632  EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
633}
634
635// Tests using InvokeArgument with a function that takes a const char*
636// by passing it a C-string literal.
637TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
638  Action<const char*(const char*(*)(const char* input, short n))> a =  // NOLINT
639      InvokeArgument<0>("Hi", 1);
640  EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
641}
642
643// Tests using InvokeArgument with a function that takes a const reference.
644TEST(InvokeArgumentTest, ByConstReferenceFunction) {
645  Action<bool(bool(*function)(const string& s))> a =  // NOLINT
646      InvokeArgument<0>(string("Hi"));
647  // When action 'a' is constructed, it makes a copy of the temporary
648  // string object passed to it, so it's OK to use 'a' later, when the
649  // temporary object has already died.
650  EXPECT_TRUE(a.Perform(make_tuple(&ByConstRef)));
651}
652
653// Tests using InvokeArgument with ByRef() and a function that takes a
654// const reference.
655TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
656  Action<bool(bool(*)(const double& x))> a =  // NOLINT
657      InvokeArgument<0>(ByRef(g_double));
658  // The above line calls ByRef() on a const value.
659  EXPECT_TRUE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
660
661  double x = 0;
662  a = InvokeArgument<0>(ByRef(x));  // This calls ByRef() on a non-const.
663  EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
664}
665
666// Tests using WithoutArgs with an action that takes no argument.
667TEST(WithoutArgsTest, NoArg) {
668  Action<int(int n)> a = WithoutArgs(Invoke(Nullary));  // NOLINT
669  EXPECT_EQ(1, a.Perform(make_tuple(2)));
670}
671
672// Tests using WithArgs and WithArg with an action that takes 1 argument.
673TEST(WithArgsTest, OneArg) {
674  Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary));  // NOLINT
675  EXPECT_TRUE(a.Perform(make_tuple(1.5, -1)));
676  EXPECT_FALSE(a.Perform(make_tuple(1.5, 1)));
677
678  // Also tests the synonym WithArg.
679  Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary));  // NOLINT
680  EXPECT_TRUE(a.Perform(make_tuple(1.5, -1)));
681  EXPECT_FALSE(a.Perform(make_tuple(1.5, 1)));
682
683}
684
685// Tests using WithArgs with an action that takes 2 arguments.
686TEST(WithArgsTest, TwoArgs) {
687  Action<const char*(const char* s, double x, int n)> a =
688      WithArgs<0, 2>(Invoke(Binary));
689  const char s[] = "Hello";
690  EXPECT_EQ(s + 2, a.Perform(make_tuple(CharPtr(s), 0.5, 2)));
691}
692
693// Tests using WithArgs with an action that takes 3 arguments.
694TEST(WithArgsTest, ThreeArgs) {
695  Action<int(int, double, char, short)> a =  // NOLINT
696      WithArgs<0, 2, 3>(Invoke(Ternary));
697  EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, 20, 3)));
698}
699
700// Tests using WithArgs with an action that takes 4 arguments.
701TEST(WithArgsTest, FourArgs) {
702  Action<string(const char*, const char*, double, const char*, const char*)> a =
703      WithArgs<4, 3, 1, 0>(Invoke(Concat4));
704  EXPECT_EQ("4310", a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), 2.5,
705                                         CharPtr("3"), CharPtr("4"))));
706}
707
708// Tests using WithArgs with an action that takes 5 arguments.
709TEST(WithArgsTest, FiveArgs) {
710  Action<string(const char*, const char*, const char*,
711                const char*, const char*)> a =
712      WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5));
713  EXPECT_EQ("43210",
714            a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
715                                 CharPtr("3"), CharPtr("4"))));
716}
717
718// Tests using WithArgs with an action that takes 6 arguments.
719TEST(WithArgsTest, SixArgs) {
720  Action<string(const char*, const char*, const char*)> a =
721      WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6));
722  EXPECT_EQ("012210",
723            a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"))));
724}
725
726// Tests using WithArgs with an action that takes 7 arguments.
727TEST(WithArgsTest, SevenArgs) {
728  Action<string(const char*, const char*, const char*, const char*)> a =
729      WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7));
730  EXPECT_EQ("0123210",
731            a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
732                                 CharPtr("3"))));
733}
734
735// Tests using WithArgs with an action that takes 8 arguments.
736TEST(WithArgsTest, EightArgs) {
737  Action<string(const char*, const char*, const char*, const char*)> a =
738      WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8));
739  EXPECT_EQ("01230123",
740            a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
741                                 CharPtr("3"))));
742}
743
744// Tests using WithArgs with an action that takes 9 arguments.
745TEST(WithArgsTest, NineArgs) {
746  Action<string(const char*, const char*, const char*, const char*)> a =
747      WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9));
748  EXPECT_EQ("012312323",
749            a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
750                                 CharPtr("3"))));
751}
752
753// Tests using WithArgs with an action that takes 10 arguments.
754TEST(WithArgsTest, TenArgs) {
755  Action<string(const char*, const char*, const char*, const char*)> a =
756      WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10));
757  EXPECT_EQ("0123210123",
758            a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
759                                 CharPtr("3"))));
760}
761
762// Tests using WithArgs with an action that is not Invoke().
763class SubstractAction : public ActionInterface<int(int, int)> {  // NOLINT
764 public:
765  virtual int Perform(const tuple<int, int>& args) {
766    return get<0>(args) - get<1>(args);
767  }
768};
769
770TEST(WithArgsTest, NonInvokeAction) {
771  Action<int(const string&, int, int)> a =  // NOLINT
772      WithArgs<2, 1>(MakeAction(new SubstractAction));
773  EXPECT_EQ(8, a.Perform(make_tuple(CharPtr("hi"), 2, 10)));
774}
775
776// Tests using WithArgs to pass all original arguments in the original order.
777TEST(WithArgsTest, Identity) {
778  Action<int(int x, char y, short z)> a =  // NOLINT
779      WithArgs<0, 1, 2>(Invoke(Ternary));
780  EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 3)));
781}
782
783// Tests using WithArgs with repeated arguments.
784TEST(WithArgsTest, RepeatedArguments) {
785  Action<int(bool, int m, int n)> a =  // NOLINT
786      WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
787  EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10)));
788}
789
790// Tests using WithArgs with reversed argument order.
791TEST(WithArgsTest, ReversedArgumentOrder) {
792  Action<const char*(short n, const char* input)> a =  // NOLINT
793      WithArgs<1, 0>(Invoke(Binary));
794  const char s[] = "Hello";
795  EXPECT_EQ(s + 2, a.Perform(make_tuple(2, CharPtr(s))));
796}
797
798// Tests using WithArgs with compatible, but not identical, argument types.
799TEST(WithArgsTest, ArgsOfCompatibleTypes) {
800  Action<long(short x, int y, double z, char c)> a =  // NOLINT
801      WithArgs<0, 1, 3>(Invoke(Ternary));
802  EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 5.6, 3)));
803}
804
805// Tests using WithArgs with an action that returns void.
806TEST(WithArgsTest, VoidAction) {
807  Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
808  g_done = false;
809  a.Perform(make_tuple(1.5, 'a', 3));
810  EXPECT_TRUE(g_done);
811}
812
813// Tests DoAll(a1, a2).
814TEST(DoAllTest, TwoActions) {
815  int n = 0;
816  Action<int(int*)> a = DoAll(SetArgumentPointee<0>(1),  // NOLINT
817                              Return(2));
818  EXPECT_EQ(2, a.Perform(make_tuple(&n)));
819  EXPECT_EQ(1, n);
820}
821
822// Tests DoAll(a1, a2, a3).
823TEST(DoAllTest, ThreeActions) {
824  int m = 0, n = 0;
825  Action<int(int*, int*)> a = DoAll(SetArgumentPointee<0>(1),  // NOLINT
826                                    SetArgumentPointee<1>(2),
827                                    Return(3));
828  EXPECT_EQ(3, a.Perform(make_tuple(&m, &n)));
829  EXPECT_EQ(1, m);
830  EXPECT_EQ(2, n);
831}
832
833// Tests DoAll(a1, a2, a3, a4).
834TEST(DoAllTest, FourActions) {
835  int m = 0, n = 0;
836  char ch = '\0';
837  Action<int(int*, int*, char*)> a =  // NOLINT
838      DoAll(SetArgumentPointee<0>(1),
839            SetArgumentPointee<1>(2),
840            SetArgumentPointee<2>('a'),
841            Return(3));
842  EXPECT_EQ(3, a.Perform(make_tuple(&m, &n, &ch)));
843  EXPECT_EQ(1, m);
844  EXPECT_EQ(2, n);
845  EXPECT_EQ('a', ch);
846}
847
848// Tests DoAll(a1, a2, a3, a4, a5).
849TEST(DoAllTest, FiveActions) {
850  int m = 0, n = 0;
851  char a = '\0', b = '\0';
852  Action<int(int*, int*, char*, char*)> action =  // NOLINT
853      DoAll(SetArgumentPointee<0>(1),
854            SetArgumentPointee<1>(2),
855            SetArgumentPointee<2>('a'),
856            SetArgumentPointee<3>('b'),
857            Return(3));
858  EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b)));
859  EXPECT_EQ(1, m);
860  EXPECT_EQ(2, n);
861  EXPECT_EQ('a', a);
862  EXPECT_EQ('b', b);
863}
864
865// Tests DoAll(a1, a2, ..., a6).
866TEST(DoAllTest, SixActions) {
867  int m = 0, n = 0;
868  char a = '\0', b = '\0', c = '\0';
869  Action<int(int*, int*, char*, char*, char*)> action =  // NOLINT
870      DoAll(SetArgumentPointee<0>(1),
871            SetArgumentPointee<1>(2),
872            SetArgumentPointee<2>('a'),
873            SetArgumentPointee<3>('b'),
874            SetArgumentPointee<4>('c'),
875            Return(3));
876  EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c)));
877  EXPECT_EQ(1, m);
878  EXPECT_EQ(2, n);
879  EXPECT_EQ('a', a);
880  EXPECT_EQ('b', b);
881  EXPECT_EQ('c', c);
882}
883
884// Tests DoAll(a1, a2, ..., a7).
885TEST(DoAllTest, SevenActions) {
886  int m = 0, n = 0;
887  char a = '\0', b = '\0', c = '\0', d = '\0';
888  Action<int(int*, int*, char*, char*, char*, char*)> action =  // NOLINT
889      DoAll(SetArgumentPointee<0>(1),
890            SetArgumentPointee<1>(2),
891            SetArgumentPointee<2>('a'),
892            SetArgumentPointee<3>('b'),
893            SetArgumentPointee<4>('c'),
894            SetArgumentPointee<5>('d'),
895            Return(3));
896  EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d)));
897  EXPECT_EQ(1, m);
898  EXPECT_EQ(2, n);
899  EXPECT_EQ('a', a);
900  EXPECT_EQ('b', b);
901  EXPECT_EQ('c', c);
902  EXPECT_EQ('d', d);
903}
904
905// Tests DoAll(a1, a2, ..., a8).
906TEST(DoAllTest, EightActions) {
907  int m = 0, n = 0;
908  char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
909  Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
910             char*)> action =
911      DoAll(SetArgumentPointee<0>(1),
912            SetArgumentPointee<1>(2),
913            SetArgumentPointee<2>('a'),
914            SetArgumentPointee<3>('b'),
915            SetArgumentPointee<4>('c'),
916            SetArgumentPointee<5>('d'),
917            SetArgumentPointee<6>('e'),
918            Return(3));
919  EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e)));
920  EXPECT_EQ(1, m);
921  EXPECT_EQ(2, n);
922  EXPECT_EQ('a', a);
923  EXPECT_EQ('b', b);
924  EXPECT_EQ('c', c);
925  EXPECT_EQ('d', d);
926  EXPECT_EQ('e', e);
927}
928
929// Tests DoAll(a1, a2, ..., a9).
930TEST(DoAllTest, NineActions) {
931  int m = 0, n = 0;
932  char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
933  Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
934             char*, char*)> action =
935      DoAll(SetArgumentPointee<0>(1),
936            SetArgumentPointee<1>(2),
937            SetArgumentPointee<2>('a'),
938            SetArgumentPointee<3>('b'),
939            SetArgumentPointee<4>('c'),
940            SetArgumentPointee<5>('d'),
941            SetArgumentPointee<6>('e'),
942            SetArgumentPointee<7>('f'),
943            Return(3));
944  EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
945  EXPECT_EQ(1, m);
946  EXPECT_EQ(2, n);
947  EXPECT_EQ('a', a);
948  EXPECT_EQ('b', b);
949  EXPECT_EQ('c', c);
950  EXPECT_EQ('d', d);
951  EXPECT_EQ('e', e);
952  EXPECT_EQ('f', f);
953}
954
955// Tests DoAll(a1, a2, ..., a10).
956TEST(DoAllTest, TenActions) {
957  int m = 0, n = 0;
958  char a = '\0', b = '\0', c = '\0', d = '\0';
959  char e = '\0', f = '\0', g = '\0';
960  Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
961             char*, char*, char*)> action =
962      DoAll(SetArgumentPointee<0>(1),
963            SetArgumentPointee<1>(2),
964            SetArgumentPointee<2>('a'),
965            SetArgumentPointee<3>('b'),
966            SetArgumentPointee<4>('c'),
967            SetArgumentPointee<5>('d'),
968            SetArgumentPointee<6>('e'),
969            SetArgumentPointee<7>('f'),
970            SetArgumentPointee<8>('g'),
971            Return(3));
972  EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
973  EXPECT_EQ(1, m);
974  EXPECT_EQ(2, n);
975  EXPECT_EQ('a', a);
976  EXPECT_EQ('b', b);
977  EXPECT_EQ('c', c);
978  EXPECT_EQ('d', d);
979  EXPECT_EQ('e', e);
980  EXPECT_EQ('f', f);
981  EXPECT_EQ('g', g);
982}
983
984// Tests the ACTION*() macro family.
985
986// Tests that ACTION() can define an action that doesn't reference the
987// mock function arguments.
988ACTION(Return5) { return 5; }
989
990TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
991  Action<double()> a1 = Return5();
992  EXPECT_DOUBLE_EQ(5, a1.Perform(make_tuple()));
993
994  Action<int(double, bool)> a2 = Return5();
995  EXPECT_EQ(5, a2.Perform(make_tuple(1, true)));
996}
997
998// Tests that ACTION() can define an action that returns void.
999ACTION(IncrementArg1) { (*arg1)++; }
1000
1001TEST(ActionMacroTest, WorksWhenReturningVoid) {
1002  Action<void(int, int*)> a1 = IncrementArg1();
1003  int n = 0;
1004  a1.Perform(make_tuple(5, &n));
1005  EXPECT_EQ(1, n);
1006}
1007
1008// Tests that the body of ACTION() can reference the type of the
1009// argument.
1010ACTION(IncrementArg2) {
1011  StaticAssertTypeEq<int*, arg2_type>();
1012  arg2_type temp = arg2;
1013  (*temp)++;
1014}
1015
1016TEST(ActionMacroTest, CanReferenceArgumentType) {
1017  Action<void(int, bool, int*)> a1 = IncrementArg2();
1018  int n = 0;
1019  a1.Perform(make_tuple(5, false, &n));
1020  EXPECT_EQ(1, n);
1021}
1022
1023// Tests that the body of ACTION() can reference the argument tuple
1024// via args_type and args.
1025ACTION(Sum2) {
1026  StaticAssertTypeEq< ::std::tr1::tuple<int, char, int*>, args_type>();
1027  args_type args_copy = args;
1028  return get<0>(args_copy) + get<1>(args_copy);
1029}
1030
1031TEST(ActionMacroTest, CanReferenceArgumentTuple) {
1032  Action<int(int, char, int*)> a1 = Sum2();
1033  int dummy = 0;
1034  EXPECT_EQ(11, a1.Perform(make_tuple(5, static_cast<char>(6), &dummy)));
1035}
1036
1037// Tests that the body of ACTION() can reference the mock function
1038// type.
1039int Dummy(bool flag) { return flag? 1 : 0; }
1040
1041ACTION(InvokeDummy) {
1042  StaticAssertTypeEq<int(bool), function_type>();
1043  function_type* fp = &Dummy;
1044  return (*fp)(true);
1045}
1046
1047TEST(ActionMacroTest, CanReferenceMockFunctionType) {
1048  Action<int(bool)> a1 = InvokeDummy();
1049  EXPECT_EQ(1, a1.Perform(make_tuple(true)));
1050  EXPECT_EQ(1, a1.Perform(make_tuple(false)));
1051}
1052
1053// Tests that the body of ACTION() can reference the mock function's
1054// return type.
1055ACTION(InvokeDummy2) {
1056  StaticAssertTypeEq<int, return_type>();
1057  return_type result = Dummy(true);
1058  return result;
1059}
1060
1061TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
1062  Action<int(bool)> a1 = InvokeDummy2();
1063  EXPECT_EQ(1, a1.Perform(make_tuple(true)));
1064  EXPECT_EQ(1, a1.Perform(make_tuple(false)));
1065}
1066
1067// Tests that ACTION() works for arguments passed by const reference.
1068ACTION(ReturnAddrOfConstBoolReferenceArg) {
1069  StaticAssertTypeEq<const bool&, arg1_type>();
1070  return &arg1;
1071}
1072
1073TEST(ActionMacroTest, WorksForConstReferenceArg) {
1074  Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
1075  const bool b = false;
1076  EXPECT_EQ(&b, a.Perform(tuple<int, const bool&>(0, b)));
1077}
1078
1079// Tests that ACTION() works for arguments passed by non-const reference.
1080ACTION(ReturnAddrOfIntReferenceArg) {
1081  StaticAssertTypeEq<int&, arg0_type>();
1082  return &arg0;
1083}
1084
1085TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
1086  Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
1087  int n = 0;
1088  EXPECT_EQ(&n, a.Perform(tuple<int&, bool, int>(n, true, 1)));
1089}
1090
1091// Tests that ACTION() can be used in a namespace.
1092namespace action_test {
1093ACTION(Sum) { return arg0 + arg1; }
1094}  // namespace action_test
1095
1096TEST(ActionMacroTest, WorksInNamespace) {
1097  Action<int(int, int)> a1 = action_test::Sum();
1098  EXPECT_EQ(3, a1.Perform(make_tuple(1, 2)));
1099}
1100
1101// Tests that the same ACTION definition works for mock functions with
1102// different argument numbers.
1103ACTION(PlusTwo) { return arg0 + 2; }
1104
1105TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
1106  Action<int(int)> a1 = PlusTwo();
1107  EXPECT_EQ(4, a1.Perform(make_tuple(2)));
1108
1109  Action<double(float, void*)> a2 = PlusTwo();
1110  int dummy;
1111  EXPECT_DOUBLE_EQ(6, a2.Perform(make_tuple(4.0f, &dummy)));
1112}
1113
1114// Tests that ACTION_P can define a parameterized action.
1115ACTION_P(Plus, n) { return arg0 + n; }
1116
1117TEST(ActionPMacroTest, DefinesParameterizedAction) {
1118  Action<int(int m, bool t)> a1 = Plus(9);
1119  EXPECT_EQ(10, a1.Perform(make_tuple(1, true)));
1120}
1121
1122// Tests that the body of ACTION_P can reference the argument types
1123// and the parameter type.
1124ACTION_P(TypedPlus, n) {
1125  arg0_type t1 = arg0;
1126  n_type t2 = n;
1127  return t1 + t2;
1128}
1129
1130TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
1131  Action<int(char m, bool t)> a1 = TypedPlus(9);
1132  EXPECT_EQ(10, a1.Perform(make_tuple(static_cast<char>(1), true)));
1133}
1134
1135// Tests that a parameterized action can be used in any mock function
1136// whose type is compatible.
1137TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
1138  Action<std::string(const std::string& s)> a1 = Plus("tail");
1139  const std::string re = "re";
1140  EXPECT_EQ("retail", a1.Perform(make_tuple(re)));
1141}
1142
1143// Tests that we can use ACTION*() to define actions overloaded on the
1144// number of parameters.
1145
1146ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
1147
1148ACTION_P(OverloadedAction, default_value) {
1149  return arg0 ? arg1 : default_value;
1150}
1151
1152ACTION_P2(OverloadedAction, true_value, false_value) {
1153  return arg0 ? true_value : false_value;
1154}
1155
1156TEST(ActionMacroTest, CanDefineOverloadedActions) {
1157  typedef Action<const char*(bool, const char*)> MyAction;
1158
1159  const MyAction a1 = OverloadedAction();
1160  EXPECT_STREQ("hello", a1.Perform(make_tuple(false, CharPtr("world"))));
1161  EXPECT_STREQ("world", a1.Perform(make_tuple(true, CharPtr("world"))));
1162
1163  const MyAction a2 = OverloadedAction("hi");
1164  EXPECT_STREQ("hi", a2.Perform(make_tuple(false, CharPtr("world"))));
1165  EXPECT_STREQ("world", a2.Perform(make_tuple(true, CharPtr("world"))));
1166
1167  const MyAction a3 = OverloadedAction("hi", "you");
1168  EXPECT_STREQ("hi", a3.Perform(make_tuple(true, CharPtr("world"))));
1169  EXPECT_STREQ("you", a3.Perform(make_tuple(false, CharPtr("world"))));
1170}
1171
1172// Tests ACTION_Pn where n >= 3.
1173
1174ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
1175
1176TEST(ActionPnMacroTest, WorksFor3Parameters) {
1177  Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
1178  EXPECT_DOUBLE_EQ(3123.4, a1.Perform(make_tuple(3000, true)));
1179
1180  Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
1181  const std::string re = "re";
1182  EXPECT_EQ("retail->", a2.Perform(make_tuple(re)));
1183}
1184
1185ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
1186
1187TEST(ActionPnMacroTest, WorksFor4Parameters) {
1188  Action<int(int)> a1 = Plus(1, 2, 3, 4);
1189  EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(make_tuple(10)));
1190}
1191
1192ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
1193
1194TEST(ActionPnMacroTest, WorksFor5Parameters) {
1195  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
1196  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(make_tuple(10)));
1197}
1198
1199ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
1200  return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
1201}
1202
1203TEST(ActionPnMacroTest, WorksFor6Parameters) {
1204  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
1205  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(make_tuple(10)));
1206}
1207
1208ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
1209  return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
1210}
1211
1212TEST(ActionPnMacroTest, WorksFor7Parameters) {
1213  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
1214  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(make_tuple(10)));
1215}
1216
1217ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
1218  return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
1219}
1220
1221TEST(ActionPnMacroTest, WorksFor8Parameters) {
1222  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
1223  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, a1.Perform(make_tuple(10)));
1224}
1225
1226ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
1227  return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
1228}
1229
1230TEST(ActionPnMacroTest, WorksFor9Parameters) {
1231  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
1232  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, a1.Perform(make_tuple(10)));
1233}
1234
1235ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
1236  arg0_type t0 = arg0;
1237  last_param_type t9 = last_param;
1238  return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
1239}
1240
1241TEST(ActionPnMacroTest, WorksFor10Parameters) {
1242  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1243  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
1244            a1.Perform(make_tuple(10)));
1245}
1246
1247// Tests that the action body can promote the parameter types.
1248
1249ACTION_P2(PadArgument, prefix, suffix) {
1250  // The following lines promote the two parameters to desired types.
1251  std::string prefix_str(prefix);
1252  char suffix_char(suffix);
1253  return prefix_str + arg0 + suffix_char;
1254}
1255
1256TEST(ActionPnMacroTest, SimpleTypePromotion) {
1257  Action<std::string(const char*)> no_promo =
1258      PadArgument(std::string("foo"), 'r');
1259  Action<std::string(const char*)> promo =
1260      PadArgument("foo", static_cast<int>('r'));
1261  EXPECT_EQ("foobar", no_promo.Perform(make_tuple(CharPtr("ba"))));
1262  EXPECT_EQ("foobar", promo.Perform(make_tuple(CharPtr("ba"))));
1263}
1264
1265// Tests that we can partially restrict parameter types using a
1266// straight-forward pattern.
1267
1268// Defines a generic action that doesn't restrict the types of its
1269// parameters.
1270ACTION_P3(ConcatImpl, a, b, c) {
1271  std::stringstream ss;
1272  ss << a << b << c;
1273  return ss.str();
1274}
1275
1276// Next, we try to restrict that either the first parameter is a
1277// string, or the second parameter is an int.
1278
1279// Defines a partially specialized wrapper that restricts the first
1280// parameter to std::string.
1281template <typename T1, typename T2>
1282// ConcatImplActionP3 is the class template ACTION_P3 uses to
1283// implement ConcatImpl.  We shouldn't change the name as this
1284// pattern requires the user to use it directly.
1285ConcatImplActionP3<std::string, T1, T2>
1286Concat(const std::string& a, T1 b, T2 c) {
1287  if (true) {
1288    // This branch verifies that ConcatImpl() can be invoked without
1289    // explicit template arguments.
1290    return ConcatImpl(a, b, c);
1291  } else {
1292    // This branch verifies that ConcatImpl() can also be invoked with
1293    // explicit template arguments.  It doesn't really need to be
1294    // executed as this is a compile-time verification.
1295    return ConcatImpl<std::string, T1, T2>(a, b, c);
1296  }
1297}
1298
1299// Defines another partially specialized wrapper that restricts the
1300// second parameter to int.
1301template <typename T1, typename T2>
1302ConcatImplActionP3<T1, int, T2>
1303Concat(T1 a, int b, T2 c) {
1304  return ConcatImpl(a, b, c);
1305}
1306
1307TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
1308  Action<const std::string()> a1 = Concat("Hello", "1", 2);
1309  EXPECT_EQ("Hello12", a1.Perform(make_tuple()));
1310
1311  a1 = Concat(1, 2, 3);
1312  EXPECT_EQ("123", a1.Perform(make_tuple()));
1313}
1314
1315// Verifies the type of an ACTION*.
1316
1317ACTION(DoFoo) {}
1318ACTION_P(DoFoo, p) {}
1319ACTION_P2(DoFoo, p0, p1) {}
1320
1321TEST(ActionPnMacroTest, TypesAreCorrect) {
1322  // DoFoo() must be assignable to a DoFooAction variable.
1323  DoFooAction a0 = DoFoo();
1324
1325  // DoFoo(1) must be assignable to a DoFooActionP variable.
1326  DoFooActionP<int> a1 = DoFoo(1);
1327
1328  // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
1329  // variable, and so on.
1330  DoFooActionP2<int, char> a2 = DoFoo(1, '2');
1331  PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
1332  PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
1333  PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
1334  PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
1335  PlusActionP7<int, int, int, int, int, int, char> a7 =
1336      Plus(1, 2, 3, 4, 5, 6, '7');
1337  PlusActionP8<int, int, int, int, int, int, int, char> a8 =
1338      Plus(1, 2, 3, 4, 5, 6, 7, '8');
1339  PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
1340      Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
1341  PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
1342      Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1343}
1344
1345// Tests that an ACTION_P*() action can be explicitly instantiated
1346// with reference-typed parameters.
1347
1348ACTION_P(Plus1, x) { return x; }
1349ACTION_P2(Plus2, x, y) { return x + y; }
1350ACTION_P3(Plus3, x, y, z) { return x + y + z; }
1351ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
1352  return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
1353}
1354
1355TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
1356  int x = 1, y = 2, z = 3;
1357  const tuple<> empty = make_tuple();
1358
1359  Action<int()> a = Plus1<int&>(x);
1360  EXPECT_EQ(1, a.Perform(empty));
1361
1362  a = Plus2<const int&, int&>(x, y);
1363  EXPECT_EQ(3, a.Perform(empty));
1364
1365  a = Plus3<int&, const int&, int&>(x, y, z);
1366  EXPECT_EQ(6, a.Perform(empty));
1367
1368  int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
1369  a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
1370      int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
1371                              n[8], n[9]);
1372  EXPECT_EQ(55, a.Perform(empty));
1373}
1374
1375TEST(SaveArgActionTest, WorksForSameType) {
1376  int result = 0;
1377  const Action<void(int n)> a1 = SaveArg<0>(&result);
1378  a1.Perform(make_tuple(5));
1379  EXPECT_EQ(5, result);
1380}
1381
1382TEST(SaveArgActionTest, WorksForCompatibleType) {
1383  int result = 0;
1384  const Action<void(bool, char)> a1 = SaveArg<1>(&result);
1385  a1.Perform(make_tuple(true, 'a'));
1386  EXPECT_EQ('a', result);
1387}
1388
1389TEST(SetArgRefereeActionTest, WorksForSameType) {
1390  int value = 0;
1391  const Action<void(int&)> a1 = SetArgReferee<0>(1);
1392  a1.Perform(tuple<int&>(value));
1393  EXPECT_EQ(1, value);
1394}
1395
1396TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
1397  int value = 0;
1398  const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
1399  a1.Perform(tuple<int, int&>(0, value));
1400  EXPECT_EQ('a', value);
1401}
1402
1403TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
1404  int value = 0;
1405  const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
1406  a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
1407  EXPECT_EQ('a', value);
1408}
1409
1410class NullaryConstructorClass {
1411 public:
1412  NullaryConstructorClass() : value_(123) {}
1413  int value_;
1414};
1415
1416// Tests using ReturnNew() with a nullary constructor.
1417TEST(ReturnNewTest, NoArgs) {
1418  Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>();
1419  NullaryConstructorClass* c = a.Perform(make_tuple());
1420  EXPECT_EQ(123, c->value_);
1421  delete c;
1422}
1423
1424class UnaryConstructorClass {
1425 public:
1426  explicit UnaryConstructorClass(int value) : value_(value) {}
1427  int value_;
1428};
1429
1430// Tests using ReturnNew() with a unary constructor.
1431TEST(ReturnNewTest, Unary) {
1432  Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
1433  UnaryConstructorClass* c = a.Perform(make_tuple());
1434  EXPECT_EQ(4000, c->value_);
1435  delete c;
1436}
1437
1438TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
1439  Action<UnaryConstructorClass*(bool, int)> a =
1440      ReturnNew<UnaryConstructorClass>(4000);
1441  UnaryConstructorClass* c = a.Perform(make_tuple(false, 5));
1442  EXPECT_EQ(4000, c->value_);
1443  delete c;
1444}
1445
1446TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
1447  Action<const UnaryConstructorClass*()> a =
1448      ReturnNew<UnaryConstructorClass>(4000);
1449  const UnaryConstructorClass* c = a.Perform(make_tuple());
1450  EXPECT_EQ(4000, c->value_);
1451  delete c;
1452}
1453
1454class TenArgConstructorClass {
1455 public:
1456  TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
1457                         int a6, int a7, int a8, int a9, int a10)
1458    : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
1459  }
1460  int value_;
1461};
1462
1463// Tests using ReturnNew() with a 10-argument constructor.
1464TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
1465  Action<TenArgConstructorClass*()> a =
1466      ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
1467                                        4000000, 500000, 60000,
1468                                        7000, 800, 90, 0);
1469  TenArgConstructorClass* c = a.Perform(make_tuple());
1470  EXPECT_EQ(1234567890, c->value_);
1471  delete c;
1472}
1473
1474// A class that can be used to verify that its destructor is called: it will set
1475// the bool provided to the constructor to true when destroyed.
1476class DeletionTester {
1477 public:
1478  explicit DeletionTester(bool* is_deleted)
1479    : is_deleted_(is_deleted) {
1480    // Make sure the bit is set to false.
1481    *is_deleted_ = false;
1482  }
1483
1484  ~DeletionTester() {
1485    *is_deleted_ = true;
1486  }
1487
1488 private:
1489  bool* is_deleted_;
1490};
1491
1492TEST(DeleteArgActionTest, OneArg) {
1493  bool is_deleted = false;
1494  DeletionTester* t = new DeletionTester(&is_deleted);
1495  const Action<void(DeletionTester*)> a1 = DeleteArg<0>();      // NOLINT
1496  EXPECT_FALSE(is_deleted);
1497  a1.Perform(make_tuple(t));
1498  EXPECT_TRUE(is_deleted);
1499}
1500
1501TEST(DeleteArgActionTest, TenArgs) {
1502  bool is_deleted = false;
1503  DeletionTester* t = new DeletionTester(&is_deleted);
1504  const Action<void(bool, int, int, const char*, bool,
1505                    int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>();
1506  EXPECT_FALSE(is_deleted);
1507  a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
1508  EXPECT_TRUE(is_deleted);
1509}
1510
1511#if GTEST_HAS_EXCEPTIONS
1512
1513TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
1514  const Action<void(int n)> a = Throw('a');
1515  EXPECT_THROW(a.Perform(make_tuple(0)), char);
1516}
1517
1518class MyException {};
1519
1520TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
1521  const Action<double(char ch)> a = Throw(MyException());
1522  EXPECT_THROW(a.Perform(make_tuple('0')), MyException);
1523}
1524
1525TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
1526  const Action<double()> a = Throw(MyException());
1527  EXPECT_THROW(a.Perform(make_tuple()), MyException);
1528}
1529
1530#endif  // GTEST_HAS_EXCEPTIONS
1531
1532// Tests that ACTION_TEMPLATE works when there is no value parameter.
1533ACTION_TEMPLATE(CreateNew,
1534                HAS_1_TEMPLATE_PARAMS(typename, T),
1535                AND_0_VALUE_PARAMS()) {
1536  return new T;
1537}
1538
1539TEST(ActionTemplateTest, WorksWithoutValueParam) {
1540  const Action<int*()> a = CreateNew<int>();
1541  int* p = a.Perform(make_tuple());
1542  delete p;
1543}
1544
1545// Tests that ACTION_TEMPLATE works when there are value parameters.
1546ACTION_TEMPLATE(CreateNew,
1547                HAS_1_TEMPLATE_PARAMS(typename, T),
1548                AND_1_VALUE_PARAMS(a0)) {
1549  return new T(a0);
1550}
1551
1552TEST(ActionTemplateTest, WorksWithValueParams) {
1553  const Action<int*()> a = CreateNew<int>(42);
1554  int* p = a.Perform(make_tuple());
1555  EXPECT_EQ(42, *p);
1556  delete p;
1557}
1558
1559// Tests that ACTION_TEMPLATE works for integral template parameters.
1560ACTION_TEMPLATE(MyDeleteArg,
1561                HAS_1_TEMPLATE_PARAMS(int, k),
1562                AND_0_VALUE_PARAMS()) {
1563  delete std::tr1::get<k>(args);
1564}
1565
1566// Resets a bool variable in the destructor.
1567class BoolResetter {
1568 public:
1569  explicit BoolResetter(bool* value) : value_(value) {}
1570  ~BoolResetter() { *value_ = false; }
1571 private:
1572  bool* const value_;
1573};
1574
1575TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
1576  const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
1577  int n = 0;
1578  bool b = true;
1579  BoolResetter* resetter = new BoolResetter(&b);
1580  a.Perform(make_tuple(&n, resetter));
1581  EXPECT_FALSE(b);  // Verifies that resetter is deleted.
1582}
1583
1584// Tests that ACTION_TEMPLATES works for template template parameters.
1585ACTION_TEMPLATE(ReturnSmartPointer,
1586                HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
1587                                      Pointer),
1588                AND_1_VALUE_PARAMS(pointee)) {
1589  return Pointer<pointee_type>(new pointee_type(pointee));
1590}
1591
1592TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
1593  using ::testing::internal::linked_ptr;
1594  const Action<linked_ptr<int>()> a = ReturnSmartPointer<linked_ptr>(42);
1595  linked_ptr<int> p = a.Perform(make_tuple());
1596  EXPECT_EQ(42, *p);
1597}
1598
1599// Tests that ACTION_TEMPLATE works for 10 template parameters.
1600template <typename T1, typename T2, typename T3, int k4, bool k5,
1601          unsigned int k6, typename T7, typename T8, typename T9>
1602struct GiantTemplate {
1603 public:
1604  explicit GiantTemplate(int a_value) : value(a_value) {}
1605  int value;
1606};
1607
1608ACTION_TEMPLATE(ReturnGiant,
1609                HAS_10_TEMPLATE_PARAMS(
1610                    typename, T1,
1611                    typename, T2,
1612                    typename, T3,
1613                    int, k4,
1614                    bool, k5,
1615                    unsigned int, k6,
1616                    class, T7,
1617                    class, T8,
1618                    class, T9,
1619                    template <typename T> class, T10),
1620                AND_1_VALUE_PARAMS(value)) {
1621  return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
1622}
1623
1624TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1625  using ::testing::internal::linked_ptr;
1626  typedef GiantTemplate<linked_ptr<int>, bool, double, 5,
1627      true, 6, char, unsigned, int> Giant;
1628  const Action<Giant()> a = ReturnGiant<
1629      int, bool, double, 5, true, 6, char, unsigned, int, linked_ptr>(42);
1630  Giant giant = a.Perform(make_tuple());
1631  EXPECT_EQ(42, giant.value);
1632}
1633
1634// Tests that ACTION_TEMPLATE works for 10 value parameters.
1635ACTION_TEMPLATE(ReturnSum,
1636                HAS_1_TEMPLATE_PARAMS(typename, Number),
1637                AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1638  return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1639}
1640
1641TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1642  const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1643  EXPECT_EQ(55, a.Perform(make_tuple()));
1644}
1645
1646// Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
1647// on the number of value parameters.
1648
1649ACTION(ReturnSum) { return 0; }
1650
1651ACTION_P(ReturnSum, x) { return x; }
1652
1653ACTION_TEMPLATE(ReturnSum,
1654                HAS_1_TEMPLATE_PARAMS(typename, Number),
1655                AND_2_VALUE_PARAMS(v1, v2)) {
1656  return static_cast<Number>(v1) + v2;
1657}
1658
1659ACTION_TEMPLATE(ReturnSum,
1660                HAS_1_TEMPLATE_PARAMS(typename, Number),
1661                AND_3_VALUE_PARAMS(v1, v2, v3)) {
1662  return static_cast<Number>(v1) + v2 + v3;
1663}
1664
1665ACTION_TEMPLATE(ReturnSum,
1666                HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
1667                AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1668  return static_cast<Number>(v1) + v2 + v3 + v4 + k;
1669}
1670
1671TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1672  const Action<int()> a0 = ReturnSum();
1673  const Action<int()> a1 = ReturnSum(1);
1674  const Action<int()> a2 = ReturnSum<int>(1, 2);
1675  const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
1676  const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1677  EXPECT_EQ(0, a0.Perform(make_tuple()));
1678  EXPECT_EQ(1, a1.Perform(make_tuple()));
1679  EXPECT_EQ(3, a2.Perform(make_tuple()));
1680  EXPECT_EQ(6, a3.Perform(make_tuple()));
1681  EXPECT_EQ(12345, a4.Perform(make_tuple()));
1682}
1683
1684}  // namespace gmock_generated_actions_test
1685}  // namespace testing
1686