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