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