• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 
31 // Google Mock - a framework for writing C++ mock classes.
32 //
33 // This file tests the built-in actions in gmock-more-actions.h.
34 
35 #include "gmock/gmock-more-actions.h"
36 
37 #include <functional>
38 #include <memory>
39 #include <sstream>
40 #include <string>
41 #include "gmock/gmock.h"
42 #include "gtest/gtest.h"
43 
44 namespace testing {
45 namespace gmock_more_actions_test {
46 
47 using ::std::plus;
48 using ::std::string;
49 using testing::_;
50 using testing::Action;
51 using testing::ActionInterface;
52 using testing::DeleteArg;
53 using testing::Invoke;
54 using testing::Return;
55 using testing::ReturnArg;
56 using testing::ReturnPointee;
57 using testing::SaveArg;
58 using testing::SaveArgPointee;
59 using testing::SetArgReferee;
60 using testing::StaticAssertTypeEq;
61 using testing::Unused;
62 using testing::WithArg;
63 using testing::WithoutArgs;
64 
65 // For suppressing compiler warnings on conversion possibly losing precision.
Short(short n)66 inline short Short(short n) { return n; }  // NOLINT
Char(char ch)67 inline char Char(char ch) { return ch; }
68 
69 // Sample functions and functors for testing Invoke() and etc.
Nullary()70 int Nullary() { return 1; }
71 
72 class NullaryFunctor {
73  public:
operator ()()74   int operator()() { return 2; }
75 };
76 
77 bool g_done = false;
VoidNullary()78 void VoidNullary() { g_done = true; }
79 
80 class VoidNullaryFunctor {
81  public:
operator ()()82   void operator()() { g_done = true; }
83 };
84 
Unary(int x)85 bool Unary(int x) { return x < 0; }
86 
Plus1(const char * s)87 const char* Plus1(const char* s) { return s + 1; }
88 
VoidUnary(int)89 void VoidUnary(int /* n */) { g_done = true; }
90 
ByConstRef(const std::string & s)91 bool ByConstRef(const std::string& s) { return s == "Hi"; }
92 
93 const double g_double = 0;
ReferencesGlobalDouble(const double & x)94 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
95 
ByNonConstRef(std::string & s)96 std::string ByNonConstRef(std::string& s) { return s += "+"; }  // NOLINT
97 
98 struct UnaryFunctor {
operator ()testing::gmock_more_actions_test::UnaryFunctor99   int operator()(bool x) { return x ? 1 : -1; }
100 };
101 
Binary(const char * input,short n)102 const char* Binary(const char* input, short n) { return input + n; }  // NOLINT
103 
VoidBinary(int,char)104 void VoidBinary(int, char) { g_done = true; }
105 
Ternary(int x,char y,short z)106 int Ternary(int x, char y, short z) { return x + y + z; }  // NOLINT
107 
VoidTernary(int,char,bool)108 void VoidTernary(int, char, bool) { g_done = true; }
109 
SumOf4(int a,int b,int c,int d)110 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
111 
SumOfFirst2(int a,int b,Unused,Unused)112 int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
113 
VoidFunctionWithFourArguments(char,int,float,double)114 void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
115 
Concat4(const char * s1,const char * s2,const char * s3,const char * s4)116 std::string Concat4(const char* s1, const char* s2, const char* s3,
117                     const char* s4) {
118   return std::string(s1) + s2 + s3 + s4;
119 }
120 
SumOf5(int a,int b,int c,int d,int e)121 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
122 
123 struct SumOf5Functor {
operator ()testing::gmock_more_actions_test::SumOf5Functor124   int operator()(int a, int b, int c, int d, int e) {
125     return a + b + c + d + e;
126   }
127 };
128 
Concat5(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5)129 std::string Concat5(const char* s1, const char* s2, const char* s3,
130                     const char* s4, const char* s5) {
131   return std::string(s1) + s2 + s3 + s4 + s5;
132 }
133 
SumOf6(int a,int b,int c,int d,int e,int f)134 int SumOf6(int a, int b, int c, int d, int e, int f) {
135   return a + b + c + d + e + f;
136 }
137 
138 struct SumOf6Functor {
operator ()testing::gmock_more_actions_test::SumOf6Functor139   int operator()(int a, int b, int c, int d, int e, int f) {
140     return a + b + c + d + e + f;
141   }
142 };
143 
Concat6(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5,const char * s6)144 std::string Concat6(const char* s1, const char* s2, const char* s3,
145                     const char* s4, const char* s5, const char* s6) {
146   return std::string(s1) + s2 + s3 + s4 + s5 + s6;
147 }
148 
Concat7(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5,const char * s6,const char * s7)149 std::string Concat7(const char* s1, const char* s2, const char* s3,
150                     const char* s4, const char* s5, const char* s6,
151                     const char* s7) {
152   return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
153 }
154 
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)155 std::string Concat8(const char* s1, const char* s2, const char* s3,
156                     const char* s4, const char* s5, const char* s6,
157                     const char* s7, const char* s8) {
158   return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
159 }
160 
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)161 std::string Concat9(const char* s1, const char* s2, const char* s3,
162                     const char* s4, const char* s5, const char* s6,
163                     const char* s7, const char* s8, const char* s9) {
164   return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
165 }
166 
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)167 std::string Concat10(const char* s1, const char* s2, const char* s3,
168                      const char* s4, const char* s5, const char* s6,
169                      const char* s7, const char* s8, const char* s9,
170                      const char* s10) {
171   return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
172 }
173 
174 class Foo {
175  public:
Foo()176   Foo() : value_(123) {}
177 
Nullary() const178   int Nullary() const { return value_; }
179 
Unary(long x)180   short Unary(long x) { return static_cast<short>(value_ + x); }  // NOLINT
181 
Binary(const std::string & str,char c) const182   std::string Binary(const std::string& str, char c) const { return str + c; }
183 
Ternary(int x,bool y,char z)184   int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
185 
SumOf4(int a,int b,int c,int d) const186   int SumOf4(int a, int b, int c, int d) const {
187     return a + b + c + d + value_;
188   }
189 
SumOfLast2(Unused,Unused,int a,int b) const190   int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
191 
SumOf5(int a,int b,int c,int d,int e)192   int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
193 
SumOf6(int a,int b,int c,int d,int e,int f)194   int SumOf6(int a, int b, int c, int d, int e, int f) {
195     return a + b + c + d + e + f;
196   }
197 
Concat7(const char * s1,const char * s2,const char * s3,const char * s4,const char * s5,const char * s6,const char * s7)198   std::string Concat7(const char* s1, const char* s2, const char* s3,
199                       const char* s4, const char* s5, const char* s6,
200                       const char* s7) {
201     return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
202   }
203 
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)204   std::string Concat8(const char* s1, const char* s2, const char* s3,
205                       const char* s4, const char* s5, const char* s6,
206                       const char* s7, const char* s8) {
207     return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
208   }
209 
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)210   std::string Concat9(const char* s1, const char* s2, const char* s3,
211                       const char* s4, const char* s5, const char* s6,
212                       const char* s7, const char* s8, const char* s9) {
213     return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
214   }
215 
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)216   std::string Concat10(const char* s1, const char* s2, const char* s3,
217                        const char* s4, const char* s5, const char* s6,
218                        const char* s7, const char* s8, const char* s9,
219                        const char* s10) {
220     return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
221   }
222 
223  private:
224   int value_;
225 };
226 
227 // Tests using Invoke() with a nullary function.
TEST(InvokeTest,Nullary)228 TEST(InvokeTest, Nullary) {
229   Action<int()> a = Invoke(Nullary);  // NOLINT
230   EXPECT_EQ(1, a.Perform(std::make_tuple()));
231 }
232 
233 // Tests using Invoke() with a unary function.
TEST(InvokeTest,Unary)234 TEST(InvokeTest, Unary) {
235   Action<bool(int)> a = Invoke(Unary);  // NOLINT
236   EXPECT_FALSE(a.Perform(std::make_tuple(1)));
237   EXPECT_TRUE(a.Perform(std::make_tuple(-1)));
238 }
239 
240 // Tests using Invoke() with a binary function.
TEST(InvokeTest,Binary)241 TEST(InvokeTest, Binary) {
242   Action<const char*(const char*, short)> a = Invoke(Binary);  // NOLINT
243   const char* p = "Hello";
244   EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2))));
245 }
246 
247 // Tests using Invoke() with a ternary function.
TEST(InvokeTest,Ternary)248 TEST(InvokeTest, Ternary) {
249   Action<int(int, char, short)> a = Invoke(Ternary);  // NOLINT
250   EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3))));
251 }
252 
253 // Tests using Invoke() with a 4-argument function.
TEST(InvokeTest,FunctionThatTakes4Arguments)254 TEST(InvokeTest, FunctionThatTakes4Arguments) {
255   Action<int(int, int, int, int)> a = Invoke(SumOf4);  // NOLINT
256   EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4)));
257 }
258 
259 // Tests using Invoke() with a 5-argument function.
TEST(InvokeTest,FunctionThatTakes5Arguments)260 TEST(InvokeTest, FunctionThatTakes5Arguments) {
261   Action<int(int, int, int, int, int)> a = Invoke(SumOf5);  // NOLINT
262   EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
263 }
264 
265 // Tests using Invoke() with a 6-argument function.
TEST(InvokeTest,FunctionThatTakes6Arguments)266 TEST(InvokeTest, FunctionThatTakes6Arguments) {
267   Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6);  // NOLINT
268   EXPECT_EQ(123456,
269             a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
270 }
271 
272 // A helper that turns the type of a C-string literal from const
273 // char[N] to const char*.
CharPtr(const char * s)274 inline const char* CharPtr(const char* s) { return s; }
275 
276 // Tests using Invoke() with a 7-argument function.
TEST(InvokeTest,FunctionThatTakes7Arguments)277 TEST(InvokeTest, FunctionThatTakes7Arguments) {
278   Action<std::string(const char*, const char*, const char*, const char*,
279                      const char*, const char*, const char*)>
280       a = Invoke(Concat7);
281   EXPECT_EQ("1234567",
282             a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
283                                       CharPtr("4"), CharPtr("5"), CharPtr("6"),
284                                       CharPtr("7"))));
285 }
286 
287 // Tests using Invoke() with a 8-argument function.
TEST(InvokeTest,FunctionThatTakes8Arguments)288 TEST(InvokeTest, FunctionThatTakes8Arguments) {
289   Action<std::string(const char*, const char*, const char*, const char*,
290                      const char*, const char*, const char*, const char*)>
291       a = Invoke(Concat8);
292   EXPECT_EQ("12345678",
293             a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
294                                       CharPtr("4"), CharPtr("5"), CharPtr("6"),
295                                       CharPtr("7"), CharPtr("8"))));
296 }
297 
298 // Tests using Invoke() with a 9-argument function.
TEST(InvokeTest,FunctionThatTakes9Arguments)299 TEST(InvokeTest, FunctionThatTakes9Arguments) {
300   Action<std::string(const char*, const char*, const char*, const char*,
301                      const char*, const char*, const char*, const char*,
302                      const char*)>
303       a = Invoke(Concat9);
304   EXPECT_EQ("123456789", a.Perform(std::make_tuple(
305                              CharPtr("1"), CharPtr("2"), CharPtr("3"),
306                              CharPtr("4"), CharPtr("5"), CharPtr("6"),
307                              CharPtr("7"), CharPtr("8"), CharPtr("9"))));
308 }
309 
310 // Tests using Invoke() with a 10-argument function.
TEST(InvokeTest,FunctionThatTakes10Arguments)311 TEST(InvokeTest, FunctionThatTakes10Arguments) {
312   Action<std::string(const char*, const char*, const char*, const char*,
313                      const char*, const char*, const char*, const char*,
314                      const char*, const char*)>
315       a = Invoke(Concat10);
316   EXPECT_EQ("1234567890",
317             a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
318                                       CharPtr("4"), CharPtr("5"), CharPtr("6"),
319                                       CharPtr("7"), CharPtr("8"), CharPtr("9"),
320                                       CharPtr("0"))));
321 }
322 
323 // Tests using Invoke() with functions with parameters declared as Unused.
TEST(InvokeTest,FunctionWithUnusedParameters)324 TEST(InvokeTest, FunctionWithUnusedParameters) {
325   Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
326   std::tuple<int, int, double, std::string> dummy =
327       std::make_tuple(10, 2, 5.6, std::string("hi"));
328   EXPECT_EQ(12, a1.Perform(dummy));
329 
330   Action<int(int, int, bool, int*)> a2 =
331       Invoke(SumOfFirst2);
332   EXPECT_EQ(
333       23, a2.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
334 }
335 
336 // Tests using Invoke() with methods with parameters declared as Unused.
TEST(InvokeTest,MethodWithUnusedParameters)337 TEST(InvokeTest, MethodWithUnusedParameters) {
338   Foo foo;
339   Action<int(std::string, bool, int, int)> a1 = Invoke(&foo, &Foo::SumOfLast2);
340   EXPECT_EQ(12, a1.Perform(std::make_tuple(CharPtr("hi"), true, 10, 2)));
341 
342   Action<int(char, double, int, int)> a2 =
343       Invoke(&foo, &Foo::SumOfLast2);
344   EXPECT_EQ(23, a2.Perform(std::make_tuple('a', 2.5, 20, 3)));
345 }
346 
347 // Tests using Invoke() with a functor.
TEST(InvokeTest,Functor)348 TEST(InvokeTest, Functor) {
349   Action<long(long, int)> a = Invoke(plus<long>());  // NOLINT
350   EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2)));
351 }
352 
353 // Tests using Invoke(f) as an action of a compatible type.
TEST(InvokeTest,FunctionWithCompatibleType)354 TEST(InvokeTest, FunctionWithCompatibleType) {
355   Action<long(int, short, char, bool)> a = Invoke(SumOf4);  // NOLINT
356   EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
357 }
358 
359 // Tests using Invoke() with an object pointer and a method pointer.
360 
361 // Tests using Invoke() with a nullary method.
TEST(InvokeMethodTest,Nullary)362 TEST(InvokeMethodTest, Nullary) {
363   Foo foo;
364   Action<int()> a = Invoke(&foo, &Foo::Nullary);  // NOLINT
365   EXPECT_EQ(123, a.Perform(std::make_tuple()));
366 }
367 
368 // Tests using Invoke() with a unary method.
TEST(InvokeMethodTest,Unary)369 TEST(InvokeMethodTest, Unary) {
370   Foo foo;
371   Action<short(long)> a = Invoke(&foo, &Foo::Unary);  // NOLINT
372   EXPECT_EQ(4123, a.Perform(std::make_tuple(4000)));
373 }
374 
375 // Tests using Invoke() with a binary method.
TEST(InvokeMethodTest,Binary)376 TEST(InvokeMethodTest, Binary) {
377   Foo foo;
378   Action<std::string(const std::string&, char)> a = Invoke(&foo, &Foo::Binary);
379   std::string s("Hell");
380   std::tuple<std::string, char> dummy = std::make_tuple(s, 'o');
381   EXPECT_EQ("Hello", a.Perform(dummy));
382 }
383 
384 // Tests using Invoke() with a ternary method.
TEST(InvokeMethodTest,Ternary)385 TEST(InvokeMethodTest, Ternary) {
386   Foo foo;
387   Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary);  // NOLINT
388   EXPECT_EQ(1124, a.Perform(std::make_tuple(1000, true, Char(1))));
389 }
390 
391 // Tests using Invoke() with a 4-argument method.
TEST(InvokeMethodTest,MethodThatTakes4Arguments)392 TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
393   Foo foo;
394   Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4);  // NOLINT
395   EXPECT_EQ(1357, a.Perform(std::make_tuple(1000, 200, 30, 4)));
396 }
397 
398 // Tests using Invoke() with a 5-argument method.
TEST(InvokeMethodTest,MethodThatTakes5Arguments)399 TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
400   Foo foo;
401   Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5);  // NOLINT
402   EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
403 }
404 
405 // Tests using Invoke() with a 6-argument method.
TEST(InvokeMethodTest,MethodThatTakes6Arguments)406 TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
407   Foo foo;
408   Action<int(int, int, int, int, int, int)> a =  // NOLINT
409       Invoke(&foo, &Foo::SumOf6);
410   EXPECT_EQ(123456,
411             a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
412 }
413 
414 // Tests using Invoke() with a 7-argument method.
TEST(InvokeMethodTest,MethodThatTakes7Arguments)415 TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
416   Foo foo;
417   Action<std::string(const char*, const char*, const char*, const char*,
418                      const char*, const char*, const char*)>
419       a = Invoke(&foo, &Foo::Concat7);
420   EXPECT_EQ("1234567",
421             a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
422                                       CharPtr("4"), CharPtr("5"), CharPtr("6"),
423                                       CharPtr("7"))));
424 }
425 
426 // Tests using Invoke() with a 8-argument method.
TEST(InvokeMethodTest,MethodThatTakes8Arguments)427 TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
428   Foo foo;
429   Action<std::string(const char*, const char*, const char*, const char*,
430                      const char*, const char*, const char*, const char*)>
431       a = Invoke(&foo, &Foo::Concat8);
432   EXPECT_EQ("12345678",
433             a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
434                                       CharPtr("4"), CharPtr("5"), CharPtr("6"),
435                                       CharPtr("7"), CharPtr("8"))));
436 }
437 
438 // Tests using Invoke() with a 9-argument method.
TEST(InvokeMethodTest,MethodThatTakes9Arguments)439 TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
440   Foo foo;
441   Action<std::string(const char*, const char*, const char*, const char*,
442                      const char*, const char*, const char*, const char*,
443                      const char*)>
444       a = Invoke(&foo, &Foo::Concat9);
445   EXPECT_EQ("123456789", a.Perform(std::make_tuple(
446                              CharPtr("1"), CharPtr("2"), CharPtr("3"),
447                              CharPtr("4"), CharPtr("5"), CharPtr("6"),
448                              CharPtr("7"), CharPtr("8"), CharPtr("9"))));
449 }
450 
451 // Tests using Invoke() with a 10-argument method.
TEST(InvokeMethodTest,MethodThatTakes10Arguments)452 TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
453   Foo foo;
454   Action<std::string(const char*, const char*, const char*, const char*,
455                      const char*, const char*, const char*, const char*,
456                      const char*, const char*)>
457       a = Invoke(&foo, &Foo::Concat10);
458   EXPECT_EQ("1234567890",
459             a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
460                                       CharPtr("4"), CharPtr("5"), CharPtr("6"),
461                                       CharPtr("7"), CharPtr("8"), CharPtr("9"),
462                                       CharPtr("0"))));
463 }
464 
465 // Tests using Invoke(f) as an action of a compatible type.
TEST(InvokeMethodTest,MethodWithCompatibleType)466 TEST(InvokeMethodTest, MethodWithCompatibleType) {
467   Foo foo;
468   Action<long(int, short, char, bool)> a =  // NOLINT
469       Invoke(&foo, &Foo::SumOf4);
470   EXPECT_EQ(4444, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
471 }
472 
473 // Tests using WithoutArgs with an action that takes no argument.
TEST(WithoutArgsTest,NoArg)474 TEST(WithoutArgsTest, NoArg) {
475   Action<int(int n)> a = WithoutArgs(Invoke(Nullary));  // NOLINT
476   EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
477 }
478 
479 // Tests using WithArg with an action that takes 1 argument.
TEST(WithArgTest,OneArg)480 TEST(WithArgTest, OneArg) {
481   Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary));  // NOLINT
482   EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1)));
483   EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1)));
484 }
485 
TEST(ReturnArgActionTest,WorksForOneArgIntArg0)486 TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
487   const Action<int(int)> a = ReturnArg<0>();
488   EXPECT_EQ(5, a.Perform(std::make_tuple(5)));
489 }
490 
TEST(ReturnArgActionTest,WorksForMultiArgBoolArg0)491 TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
492   const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
493   EXPECT_TRUE(a.Perform(std::make_tuple(true, false, false)));
494 }
495 
TEST(ReturnArgActionTest,WorksForMultiArgStringArg2)496 TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
497   const Action<std::string(int, int, std::string, int)> a = ReturnArg<2>();
498   EXPECT_EQ("seven", a.Perform(std::make_tuple(5, 6, std::string("seven"), 8)));
499 }
500 
TEST(SaveArgActionTest,WorksForSameType)501 TEST(SaveArgActionTest, WorksForSameType) {
502   int result = 0;
503   const Action<void(int n)> a1 = SaveArg<0>(&result);
504   a1.Perform(std::make_tuple(5));
505   EXPECT_EQ(5, result);
506 }
507 
TEST(SaveArgActionTest,WorksForCompatibleType)508 TEST(SaveArgActionTest, WorksForCompatibleType) {
509   int result = 0;
510   const Action<void(bool, char)> a1 = SaveArg<1>(&result);
511   a1.Perform(std::make_tuple(true, 'a'));
512   EXPECT_EQ('a', result);
513 }
514 
TEST(SaveArgPointeeActionTest,WorksForSameType)515 TEST(SaveArgPointeeActionTest, WorksForSameType) {
516   int result = 0;
517   const int value = 5;
518   const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
519   a1.Perform(std::make_tuple(&value));
520   EXPECT_EQ(5, result);
521 }
522 
TEST(SaveArgPointeeActionTest,WorksForCompatibleType)523 TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
524   int result = 0;
525   char value = 'a';
526   const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
527   a1.Perform(std::make_tuple(true, &value));
528   EXPECT_EQ('a', result);
529 }
530 
TEST(SetArgRefereeActionTest,WorksForSameType)531 TEST(SetArgRefereeActionTest, WorksForSameType) {
532   int value = 0;
533   const Action<void(int&)> a1 = SetArgReferee<0>(1);
534   a1.Perform(std::tuple<int&>(value));
535   EXPECT_EQ(1, value);
536 }
537 
TEST(SetArgRefereeActionTest,WorksForCompatibleType)538 TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
539   int value = 0;
540   const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
541   a1.Perform(std::tuple<int, int&>(0, value));
542   EXPECT_EQ('a', value);
543 }
544 
TEST(SetArgRefereeActionTest,WorksWithExtraArguments)545 TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
546   int value = 0;
547   const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
548   a1.Perform(std::tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
549   EXPECT_EQ('a', value);
550 }
551 
552 // A class that can be used to verify that its destructor is called: it will set
553 // the bool provided to the constructor to true when destroyed.
554 class DeletionTester {
555  public:
DeletionTester(bool * is_deleted)556   explicit DeletionTester(bool* is_deleted)
557     : is_deleted_(is_deleted) {
558     // Make sure the bit is set to false.
559     *is_deleted_ = false;
560   }
561 
~DeletionTester()562   ~DeletionTester() {
563     *is_deleted_ = true;
564   }
565 
566  private:
567   bool* is_deleted_;
568 };
569 
TEST(DeleteArgActionTest,OneArg)570 TEST(DeleteArgActionTest, OneArg) {
571   bool is_deleted = false;
572   DeletionTester* t = new DeletionTester(&is_deleted);
573   const Action<void(DeletionTester*)> a1 = DeleteArg<0>();      // NOLINT
574   EXPECT_FALSE(is_deleted);
575   a1.Perform(std::make_tuple(t));
576   EXPECT_TRUE(is_deleted);
577 }
578 
TEST(DeleteArgActionTest,TenArgs)579 TEST(DeleteArgActionTest, TenArgs) {
580   bool is_deleted = false;
581   DeletionTester* t = new DeletionTester(&is_deleted);
582   const Action<void(bool, int, int, const char*, bool,
583                     int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>();
584   EXPECT_FALSE(is_deleted);
585   a1.Perform(std::make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
586   EXPECT_TRUE(is_deleted);
587 }
588 
589 #if GTEST_HAS_EXCEPTIONS
590 
TEST(ThrowActionTest,ThrowsGivenExceptionInVoidFunction)591 TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
592   const Action<void(int n)> a = Throw('a');
593   EXPECT_THROW(a.Perform(std::make_tuple(0)), char);
594 }
595 
596 class MyException {};
597 
TEST(ThrowActionTest,ThrowsGivenExceptionInNonVoidFunction)598 TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
599   const Action<double(char ch)> a = Throw(MyException());
600   EXPECT_THROW(a.Perform(std::make_tuple('0')), MyException);
601 }
602 
TEST(ThrowActionTest,ThrowsGivenExceptionInNullaryFunction)603 TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
604   const Action<double()> a = Throw(MyException());
605   EXPECT_THROW(a.Perform(std::make_tuple()), MyException);
606 }
607 
608 #endif  // GTEST_HAS_EXCEPTIONS
609 
610 // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
611 // pointed to by the N-th (0-based) argument to values in range [first, last).
TEST(SetArrayArgumentTest,SetsTheNthArray)612 TEST(SetArrayArgumentTest, SetsTheNthArray) {
613   typedef void MyFunction(bool, int*, char*);
614   int numbers[] = { 1, 2, 3 };
615   Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
616 
617   int n[4] = {};
618   int* pn = n;
619   char ch[4] = {};
620   char* pch = ch;
621   a.Perform(std::make_tuple(true, pn, pch));
622   EXPECT_EQ(1, n[0]);
623   EXPECT_EQ(2, n[1]);
624   EXPECT_EQ(3, n[2]);
625   EXPECT_EQ(0, n[3]);
626   EXPECT_EQ('\0', ch[0]);
627   EXPECT_EQ('\0', ch[1]);
628   EXPECT_EQ('\0', ch[2]);
629   EXPECT_EQ('\0', ch[3]);
630 
631   // Tests first and last are iterators.
632   std::string letters = "abc";
633   a = SetArrayArgument<2>(letters.begin(), letters.end());
634   std::fill_n(n, 4, 0);
635   std::fill_n(ch, 4, '\0');
636   a.Perform(std::make_tuple(true, pn, pch));
637   EXPECT_EQ(0, n[0]);
638   EXPECT_EQ(0, n[1]);
639   EXPECT_EQ(0, n[2]);
640   EXPECT_EQ(0, n[3]);
641   EXPECT_EQ('a', ch[0]);
642   EXPECT_EQ('b', ch[1]);
643   EXPECT_EQ('c', ch[2]);
644   EXPECT_EQ('\0', ch[3]);
645 }
646 
647 // Tests SetArrayArgument<N>(first, last) where first == last.
TEST(SetArrayArgumentTest,SetsTheNthArrayWithEmptyRange)648 TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
649   typedef void MyFunction(bool, int*);
650   int numbers[] = { 1, 2, 3 };
651   Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
652 
653   int n[4] = {};
654   int* pn = n;
655   a.Perform(std::make_tuple(true, pn));
656   EXPECT_EQ(0, n[0]);
657   EXPECT_EQ(0, n[1]);
658   EXPECT_EQ(0, n[2]);
659   EXPECT_EQ(0, n[3]);
660 }
661 
662 // Tests SetArrayArgument<N>(first, last) where *first is convertible
663 // (but not equal) to the argument type.
TEST(SetArrayArgumentTest,SetsTheNthArrayWithConvertibleType)664 TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
665   typedef void MyFunction(bool, int*);
666   char chars[] = { 97, 98, 99 };
667   Action<MyFunction> a = SetArrayArgument<1>(chars, chars + 3);
668 
669   int codes[4] = { 111, 222, 333, 444 };
670   int* pcodes = codes;
671   a.Perform(std::make_tuple(true, pcodes));
672   EXPECT_EQ(97, codes[0]);
673   EXPECT_EQ(98, codes[1]);
674   EXPECT_EQ(99, codes[2]);
675   EXPECT_EQ(444, codes[3]);
676 }
677 
678 // Test SetArrayArgument<N>(first, last) with iterator as argument.
TEST(SetArrayArgumentTest,SetsTheNthArrayWithIteratorArgument)679 TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
680   typedef void MyFunction(bool, std::back_insert_iterator<std::string>);
681   std::string letters = "abc";
682   Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
683 
684   std::string s;
685   a.Perform(std::make_tuple(true, back_inserter(s)));
686   EXPECT_EQ(letters, s);
687 }
688 
TEST(ReturnPointeeTest,Works)689 TEST(ReturnPointeeTest, Works) {
690   int n = 42;
691   const Action<int()> a = ReturnPointee(&n);
692   EXPECT_EQ(42, a.Perform(std::make_tuple()));
693 
694   n = 43;
695   EXPECT_EQ(43, a.Perform(std::make_tuple()));
696 }
697 
698 }  // namespace gmock_generated_actions_test
699 }  // namespace testing
700