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