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