Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gmock-generated-actions_test.cc
Go to the documentation of this file.
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 generated by a script.
34 
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_generated_actions_test {
46 
47 using ::std::plus;
48 using ::std::string;
49 using testing::_;
50 using testing::Action;
52 using testing::ByRef;
53 using testing::DoAll;
54 using testing::Invoke;
55 using testing::Return;
58 using testing::Unused;
59 
60 // For suppressing compiler warnings on conversion possibly losing precision.
61 inline short Short(short n) { return n; } // NOLINT
62 inline char Char(char ch) { return ch; }
63 
64 // Sample functions and functors for testing various actions.
65 int Nullary() { return 1; }
66 
67 bool g_done = false;
68 
69 bool ByConstRef(const std::string& s) { return s == "Hi"; }
70 
71 const double g_double = 0;
72 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
73 
74 struct UnaryFunctor {
75  int operator()(bool x) { return x ? 1 : -1; }
76 };
77 
78 const char* Binary(const char* input, short n) { return input + n; } // NOLINT
79 
80 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
81 
82 struct SumOf5Functor {
83  int operator()(int a, int b, int c, int d, int e) {
84  return a + b + c + d + e;
85  }
86 };
87 
88 std::string Concat5(const char* s1, const char* s2, const char* s3,
89  const char* s4, const char* s5) {
90  return std::string(s1) + s2 + s3 + s4 + s5;
91 }
92 
93 int SumOf6(int a, int b, int c, int d, int e, int f) {
94  return a + b + c + d + e + f;
95 }
96 
97 struct SumOf6Functor {
98  int operator()(int a, int b, int c, int d, int e, int f) {
99  return a + b + c + d + e + f;
100  }
101 };
102 
103 std::string Concat6(const char* s1, const char* s2, const char* s3,
104  const char* s4, const char* s5, const char* s6) {
105  return std::string(s1) + s2 + s3 + s4 + s5 + s6;
106 }
107 
108 std::string Concat7(const char* s1, const char* s2, const char* s3,
109  const char* s4, const char* s5, const char* s6,
110  const char* s7) {
111  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
112 }
113 
114 std::string Concat8(const char* s1, const char* s2, const char* s3,
115  const char* s4, const char* s5, const char* s6,
116  const char* s7, const char* s8) {
117  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
118 }
119 
120 std::string Concat9(const char* s1, const char* s2, const char* s3,
121  const char* s4, const char* s5, const char* s6,
122  const char* s7, const char* s8, const char* s9) {
123  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
124 }
125 
126 std::string Concat10(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, const char* s9,
129  const char* s10) {
130  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
131 }
132 
133 // A helper that turns the type of a C-string literal from const
134 // char[N] to const char*.
135 inline const char* CharPtr(const char* s) { return s; }
136 
137 // Tests InvokeArgument<N>(...).
138 
139 // Tests using InvokeArgument with a nullary function.
140 TEST(InvokeArgumentTest, Function0) {
141  Action<int(int, int(*)())> a = InvokeArgument<1>(); // NOLINT
142  EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
143 }
144 
145 // Tests using InvokeArgument with a unary function.
146 TEST(InvokeArgumentTest, Functor1) {
147  Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
148  EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
149 }
150 
151 // Tests using InvokeArgument with a 5-ary function.
152 TEST(InvokeArgumentTest, Function5) {
154  InvokeArgument<0>(10000, 2000, 300, 40, 5);
155  EXPECT_EQ(12345, a.Perform(std::make_tuple(&SumOf5)));
156 }
157 
158 // Tests using InvokeArgument with a 5-ary functor.
159 TEST(InvokeArgumentTest, Functor5) {
160  Action<int(SumOf5Functor)> a = // NOLINT
161  InvokeArgument<0>(10000, 2000, 300, 40, 5);
162  EXPECT_EQ(12345, a.Perform(std::make_tuple(SumOf5Functor())));
163 }
164 
165 // Tests using InvokeArgument with a 6-ary function.
166 TEST(InvokeArgumentTest, Function6) {
168  InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
169  EXPECT_EQ(123456, a.Perform(std::make_tuple(&SumOf6)));
170 }
171 
172 // Tests using InvokeArgument with a 6-ary functor.
173 TEST(InvokeArgumentTest, Functor6) {
174  Action<int(SumOf6Functor)> a = // NOLINT
175  InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
176  EXPECT_EQ(123456, a.Perform(std::make_tuple(SumOf6Functor())));
177 }
178 
179 // Tests using InvokeArgument with a 7-ary function.
180 TEST(InvokeArgumentTest, Function7) {
181  Action<std::string(std::string(*)(const char*, const char*, const char*,
182  const char*, const char*, const char*,
183  const char*))>
184  a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
185  EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
186 }
187 
188 // Tests using InvokeArgument with a 8-ary function.
189 TEST(InvokeArgumentTest, Function8) {
190  Action<std::string(std::string(*)(const char*, const char*, const char*,
191  const char*, const char*, const char*,
192  const char*, const char*))>
193  a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
194  EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
195 }
196 
197 // Tests using InvokeArgument with a 9-ary function.
198 TEST(InvokeArgumentTest, Function9) {
199  Action<std::string(std::string(*)(const char*, const char*, const char*,
200  const char*, const char*, const char*,
201  const char*, const char*, const char*))>
202  a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
203  EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
204 }
205 
206 // Tests using InvokeArgument with a 10-ary function.
207 TEST(InvokeArgumentTest, Function10) {
208  Action<std::string(std::string(*)(
209  const char*, const char*, const char*, const char*, const char*,
210  const char*, const char*, const char*, const char*, const char*))>
211  a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
212  EXPECT_EQ("1234567890", a.Perform(std::make_tuple(&Concat10)));
213 }
214 
215 // Tests using InvokeArgument with a function that takes a pointer argument.
216 TEST(InvokeArgumentTest, ByPointerFunction) {
218  InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
219  EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
220 }
221 
222 // Tests using InvokeArgument with a function that takes a const char*
223 // by passing it a C-string literal.
224 TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
226  InvokeArgument<0>("Hi", Short(1));
227  EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
228 }
229 
230 // Tests using InvokeArgument with a function that takes a const reference.
231 TEST(InvokeArgumentTest, ByConstReferenceFunction) {
233  InvokeArgument<0>(std::string("Hi"));
234  // When action 'a' is constructed, it makes a copy of the temporary
235  // string object passed to it, so it's OK to use 'a' later, when the
236  // temporary object has already died.
237  EXPECT_TRUE(a.Perform(std::make_tuple(&ByConstRef)));
238 }
239 
240 // Tests using InvokeArgument with ByRef() and a function that takes a
241 // const reference.
242 TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
244  InvokeArgument<0>(ByRef(g_double));
245  // The above line calls ByRef() on a const value.
246  EXPECT_TRUE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
247 
248  double x = 0;
249  a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
250  EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
251 }
252 
253 // Tests DoAll(a1, a2).
254 TEST(DoAllTest, TwoActions) {
255  int n = 0;
256  Action<int(int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
257  Return(2));
258  EXPECT_EQ(2, a.Perform(std::make_tuple(&n)));
259  EXPECT_EQ(1, n);
260 }
261 
262 // Tests DoAll(a1, a2, a3).
263 TEST(DoAllTest, ThreeActions) {
264  int m = 0, n = 0;
265  Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
266  SetArgPointee<1>(2),
267  Return(3));
268  EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n)));
269  EXPECT_EQ(1, m);
270  EXPECT_EQ(2, n);
271 }
272 
273 // Tests DoAll(a1, a2, a3, a4).
274 TEST(DoAllTest, FourActions) {
275  int m = 0, n = 0;
276  char ch = '\0';
278  DoAll(SetArgPointee<0>(1),
279  SetArgPointee<1>(2),
280  SetArgPointee<2>('a'),
281  Return(3));
282  EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n, &ch)));
283  EXPECT_EQ(1, m);
284  EXPECT_EQ(2, n);
285  EXPECT_EQ('a', ch);
286 }
287 
288 // Tests DoAll(a1, a2, a3, a4, a5).
289 TEST(DoAllTest, FiveActions) {
290  int m = 0, n = 0;
291  char a = '\0', b = '\0';
292  Action<int(int*, int*, char*, char*)> action = // NOLINT
293  DoAll(SetArgPointee<0>(1),
294  SetArgPointee<1>(2),
295  SetArgPointee<2>('a'),
296  SetArgPointee<3>('b'),
297  Return(3));
298  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b)));
299  EXPECT_EQ(1, m);
300  EXPECT_EQ(2, n);
301  EXPECT_EQ('a', a);
302  EXPECT_EQ('b', b);
303 }
304 
305 // Tests DoAll(a1, a2, ..., a6).
306 TEST(DoAllTest, SixActions) {
307  int m = 0, n = 0;
308  char a = '\0', b = '\0', c = '\0';
310  DoAll(SetArgPointee<0>(1),
311  SetArgPointee<1>(2),
312  SetArgPointee<2>('a'),
313  SetArgPointee<3>('b'),
314  SetArgPointee<4>('c'),
315  Return(3));
316  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c)));
317  EXPECT_EQ(1, m);
318  EXPECT_EQ(2, n);
319  EXPECT_EQ('a', a);
320  EXPECT_EQ('b', b);
321  EXPECT_EQ('c', c);
322 }
323 
324 // Tests DoAll(a1, a2, ..., a7).
325 TEST(DoAllTest, SevenActions) {
326  int m = 0, n = 0;
327  char a = '\0', b = '\0', c = '\0', d = '\0';
329  DoAll(SetArgPointee<0>(1),
330  SetArgPointee<1>(2),
331  SetArgPointee<2>('a'),
332  SetArgPointee<3>('b'),
333  SetArgPointee<4>('c'),
334  SetArgPointee<5>('d'),
335  Return(3));
336  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d)));
337  EXPECT_EQ(1, m);
338  EXPECT_EQ(2, n);
339  EXPECT_EQ('a', a);
340  EXPECT_EQ('b', b);
341  EXPECT_EQ('c', c);
342  EXPECT_EQ('d', d);
343 }
344 
345 // Tests DoAll(a1, a2, ..., a8).
346 TEST(DoAllTest, EightActions) {
347  int m = 0, n = 0;
348  char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
349  Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
350  char*)> action =
351  DoAll(SetArgPointee<0>(1),
352  SetArgPointee<1>(2),
353  SetArgPointee<2>('a'),
354  SetArgPointee<3>('b'),
355  SetArgPointee<4>('c'),
356  SetArgPointee<5>('d'),
357  SetArgPointee<6>('e'),
358  Return(3));
359  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e)));
360  EXPECT_EQ(1, m);
361  EXPECT_EQ(2, n);
362  EXPECT_EQ('a', a);
363  EXPECT_EQ('b', b);
364  EXPECT_EQ('c', c);
365  EXPECT_EQ('d', d);
366  EXPECT_EQ('e', e);
367 }
368 
369 // Tests DoAll(a1, a2, ..., a9).
370 TEST(DoAllTest, NineActions) {
371  int m = 0, n = 0;
372  char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
373  Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
374  char*, char*)> action =
375  DoAll(SetArgPointee<0>(1),
376  SetArgPointee<1>(2),
377  SetArgPointee<2>('a'),
378  SetArgPointee<3>('b'),
379  SetArgPointee<4>('c'),
380  SetArgPointee<5>('d'),
381  SetArgPointee<6>('e'),
382  SetArgPointee<7>('f'),
383  Return(3));
384  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
385  EXPECT_EQ(1, m);
386  EXPECT_EQ(2, n);
387  EXPECT_EQ('a', a);
388  EXPECT_EQ('b', b);
389  EXPECT_EQ('c', c);
390  EXPECT_EQ('d', d);
391  EXPECT_EQ('e', e);
392  EXPECT_EQ('f', f);
393 }
394 
395 // Tests DoAll(a1, a2, ..., a10).
396 TEST(DoAllTest, TenActions) {
397  int m = 0, n = 0;
398  char a = '\0', b = '\0', c = '\0', d = '\0';
399  char e = '\0', f = '\0', g = '\0';
400  Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
401  char*, char*, char*)> action =
402  DoAll(SetArgPointee<0>(1),
403  SetArgPointee<1>(2),
404  SetArgPointee<2>('a'),
405  SetArgPointee<3>('b'),
406  SetArgPointee<4>('c'),
407  SetArgPointee<5>('d'),
408  SetArgPointee<6>('e'),
409  SetArgPointee<7>('f'),
410  SetArgPointee<8>('g'),
411  Return(3));
412  EXPECT_EQ(
413  3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
414  EXPECT_EQ(1, m);
415  EXPECT_EQ(2, n);
416  EXPECT_EQ('a', a);
417  EXPECT_EQ('b', b);
418  EXPECT_EQ('c', c);
419  EXPECT_EQ('d', d);
420  EXPECT_EQ('e', e);
421  EXPECT_EQ('f', f);
422  EXPECT_EQ('g', g);
423 }
424 
425 // The ACTION*() macros trigger warning C4100 (unreferenced formal
426 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
427 // the macro definition, as the warnings are generated when the macro
428 // is expanded and macro expansion cannot contain #pragma. Therefore
429 // we suppress them here.
430 // Also suppress C4503 decorated name length exceeded, name was truncated
431 #ifdef _MSC_VER
432 # pragma warning(push)
433 # pragma warning(disable:4100)
434 # pragma warning(disable:4503)
435 #endif
436 // Tests the ACTION*() macro family.
437 
438 // Tests that ACTION() can define an action that doesn't reference the
439 // mock function arguments.
440 ACTION(Return5) { return 5; }
441 
442 TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
443  Action<double()> a1 = Return5();
444  EXPECT_DOUBLE_EQ(5, a1.Perform(std::make_tuple()));
445 
446  Action<int(double, bool)> a2 = Return5();
447  EXPECT_EQ(5, a2.Perform(std::make_tuple(1, true)));
448 }
449 
450 // Tests that ACTION() can define an action that returns void.
451 ACTION(IncrementArg1) { (*arg1)++; }
452 
453 TEST(ActionMacroTest, WorksWhenReturningVoid) {
454  Action<void(int, int*)> a1 = IncrementArg1();
455  int n = 0;
456  a1.Perform(std::make_tuple(5, &n));
457  EXPECT_EQ(1, n);
458 }
459 
460 // Tests that the body of ACTION() can reference the type of the
461 // argument.
462 ACTION(IncrementArg2) {
463  StaticAssertTypeEq<int*, arg2_type>();
464  arg2_type temp = arg2;
465  (*temp)++;
466 }
467 
468 TEST(ActionMacroTest, CanReferenceArgumentType) {
469  Action<void(int, bool, int*)> a1 = IncrementArg2();
470  int n = 0;
471  a1.Perform(std::make_tuple(5, false, &n));
472  EXPECT_EQ(1, n);
473 }
474 
475 // Tests that the body of ACTION() can reference the argument tuple
476 // via args_type and args.
477 ACTION(Sum2) {
478  StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
479  args_type args_copy = args;
480  return std::get<0>(args_copy) + std::get<1>(args_copy);
481 }
482 
483 TEST(ActionMacroTest, CanReferenceArgumentTuple) {
484  Action<int(int, char, int*)> a1 = Sum2();
485  int dummy = 0;
486  EXPECT_EQ(11, a1.Perform(std::make_tuple(5, Char(6), &dummy)));
487 }
488 
489 // Tests that the body of ACTION() can reference the mock function
490 // type.
491 int Dummy(bool flag) { return flag? 1 : 0; }
492 
493 ACTION(InvokeDummy) {
494  StaticAssertTypeEq<int(bool), function_type>();
495  function_type* fp = &Dummy;
496  return (*fp)(true);
497 }
498 
499 TEST(ActionMacroTest, CanReferenceMockFunctionType) {
500  Action<int(bool)> a1 = InvokeDummy();
501  EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
502  EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
503 }
504 
505 // Tests that the body of ACTION() can reference the mock function's
506 // return type.
507 ACTION(InvokeDummy2) {
508  StaticAssertTypeEq<int, return_type>();
509  return_type result = Dummy(true);
510  return result;
511 }
512 
513 TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
514  Action<int(bool)> a1 = InvokeDummy2();
515  EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
516  EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
517 }
518 
519 // Tests that ACTION() works for arguments passed by const reference.
520 ACTION(ReturnAddrOfConstBoolReferenceArg) {
521  StaticAssertTypeEq<const bool&, arg1_type>();
522  return &arg1;
523 }
524 
525 TEST(ActionMacroTest, WorksForConstReferenceArg) {
526  Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
527  const bool b = false;
528  EXPECT_EQ(&b, a.Perform(std::tuple<int, const bool&>(0, b)));
529 }
530 
531 // Tests that ACTION() works for arguments passed by non-const reference.
532 ACTION(ReturnAddrOfIntReferenceArg) {
533  StaticAssertTypeEq<int&, arg0_type>();
534  return &arg0;
535 }
536 
537 TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
538  Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
539  int n = 0;
540  EXPECT_EQ(&n, a.Perform(std::tuple<int&, bool, int>(n, true, 1)));
541 }
542 
543 // Tests that ACTION() can be used in a namespace.
544 namespace action_test {
545 ACTION(Sum) { return arg0 + arg1; }
546 } // namespace action_test
547 
548 TEST(ActionMacroTest, WorksInNamespace) {
549  Action<int(int, int)> a1 = action_test::Sum();
550  EXPECT_EQ(3, a1.Perform(std::make_tuple(1, 2)));
551 }
552 
553 // Tests that the same ACTION definition works for mock functions with
554 // different argument numbers.
555 ACTION(PlusTwo) { return arg0 + 2; }
556 
557 TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
558  Action<int(int)> a1 = PlusTwo();
559  EXPECT_EQ(4, a1.Perform(std::make_tuple(2)));
560 
561  Action<double(float, void*)> a2 = PlusTwo();
562  int dummy;
563  EXPECT_DOUBLE_EQ(6, a2.Perform(std::make_tuple(4.0f, &dummy)));
564 }
565 
566 // Tests that ACTION_P can define a parameterized action.
567 ACTION_P(Plus, n) { return arg0 + n; }
568 
569 TEST(ActionPMacroTest, DefinesParameterizedAction) {
571  EXPECT_EQ(10, a1.Perform(std::make_tuple(1, true)));
572 }
573 
574 // Tests that the body of ACTION_P can reference the argument types
575 // and the parameter type.
576 ACTION_P(TypedPlus, n) {
577  arg0_type t1 = arg0;
578  n_type t2 = n;
579  return t1 + t2;
580 }
581 
582 TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
583  Action<int(char m, bool t)> a1 = TypedPlus(9);
584  EXPECT_EQ(10, a1.Perform(std::make_tuple(Char(1), true)));
585 }
586 
587 // Tests that a parameterized action can be used in any mock function
588 // whose type is compatible.
589 TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
591  const std::string re = "re";
592  std::tuple<const std::string> dummy = std::make_tuple(re);
593  EXPECT_EQ("retail", a1.Perform(dummy));
594 }
595 
596 // Tests that we can use ACTION*() to define actions overloaded on the
597 // number of parameters.
598 
599 ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
600 
601 ACTION_P(OverloadedAction, default_value) {
602  return arg0 ? arg1 : default_value;
603 }
604 
605 ACTION_P2(OverloadedAction, true_value, false_value) {
606  return arg0 ? true_value : false_value;
607 }
608 
609 TEST(ActionMacroTest, CanDefineOverloadedActions) {
611 
612  const MyAction a1 = OverloadedAction();
613  EXPECT_STREQ("hello", a1.Perform(std::make_tuple(false, CharPtr("world"))));
614  EXPECT_STREQ("world", a1.Perform(std::make_tuple(true, CharPtr("world"))));
615 
616  const MyAction a2 = OverloadedAction("hi");
617  EXPECT_STREQ("hi", a2.Perform(std::make_tuple(false, CharPtr("world"))));
618  EXPECT_STREQ("world", a2.Perform(std::make_tuple(true, CharPtr("world"))));
619 
620  const MyAction a3 = OverloadedAction("hi", "you");
621  EXPECT_STREQ("hi", a3.Perform(std::make_tuple(true, CharPtr("world"))));
622  EXPECT_STREQ("you", a3.Perform(std::make_tuple(false, CharPtr("world"))));
623 }
624 
625 // Tests ACTION_Pn where n >= 3.
626 
627 ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
628 
629 TEST(ActionPnMacroTest, WorksFor3Parameters) {
630  Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
631  EXPECT_DOUBLE_EQ(3123.4, a1.Perform(std::make_tuple(3000, true)));
632 
633  Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
634  const std::string re = "re";
635  std::tuple<const std::string> dummy = std::make_tuple(re);
636  EXPECT_EQ("retail->", a2.Perform(dummy));
637 }
638 
639 ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
640 
641 TEST(ActionPnMacroTest, WorksFor4Parameters) {
642  Action<int(int)> a1 = Plus(1, 2, 3, 4);
643  EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(std::make_tuple(10)));
644 }
645 
646 ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
647 
648 TEST(ActionPnMacroTest, WorksFor5Parameters) {
649  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
650  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(std::make_tuple(10)));
651 }
652 
653 ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
654  return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
655 }
656 
657 TEST(ActionPnMacroTest, WorksFor6Parameters) {
658  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
659  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(std::make_tuple(10)));
660 }
661 
662 ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
663  return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
664 }
665 
666 TEST(ActionPnMacroTest, WorksFor7Parameters) {
667  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
668  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(std::make_tuple(10)));
669 }
670 
671 ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
672  return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
673 }
674 
675 TEST(ActionPnMacroTest, WorksFor8Parameters) {
676  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
677  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
678  a1.Perform(std::make_tuple(10)));
679 }
680 
681 ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
682  return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
683 }
684 
685 TEST(ActionPnMacroTest, WorksFor9Parameters) {
686  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
687  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
688  a1.Perform(std::make_tuple(10)));
689 }
690 
691 ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
692  arg0_type t0 = arg0;
693  last_param_type t9 = last_param;
694  return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
695 }
696 
697 TEST(ActionPnMacroTest, WorksFor10Parameters) {
698  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
699  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
700  a1.Perform(std::make_tuple(10)));
701 }
702 
703 // Tests that the action body can promote the parameter types.
704 
705 ACTION_P2(PadArgument, prefix, suffix) {
706  // The following lines promote the two parameters to desired types.
707  std::string prefix_str(prefix);
708  char suffix_char = static_cast<char>(suffix);
709  return prefix_str + arg0 + suffix_char;
710 }
711 
712 TEST(ActionPnMacroTest, SimpleTypePromotion) {
714  PadArgument(std::string("foo"), 'r');
716  PadArgument("foo", static_cast<int>('r'));
717  EXPECT_EQ("foobar", no_promo.Perform(std::make_tuple(CharPtr("ba"))));
718  EXPECT_EQ("foobar", promo.Perform(std::make_tuple(CharPtr("ba"))));
719 }
720 
721 // Tests that we can partially restrict parameter types using a
722 // straight-forward pattern.
723 
724 // Defines a generic action that doesn't restrict the types of its
725 // parameters.
726 ACTION_P3(ConcatImpl, a, b, c) {
727  std::stringstream ss;
728  ss << a << b << c;
729  return ss.str();
730 }
731 
732 // Next, we try to restrict that either the first parameter is a
733 // string, or the second parameter is an int.
734 
735 // Defines a partially specialized wrapper that restricts the first
736 // parameter to std::string.
737 template <typename T1, typename T2>
738 // ConcatImplActionP3 is the class template ACTION_P3 uses to
739 // implement ConcatImpl. We shouldn't change the name as this
740 // pattern requires the user to use it directly.
741 ConcatImplActionP3<std::string, T1, T2>
742 Concat(const std::string& a, T1 b, T2 c) {
744  if (true) {
746  // This branch verifies that ConcatImpl() can be invoked without
747  // explicit template arguments.
748  return ConcatImpl(a, b, c);
749  } else {
750  // This branch verifies that ConcatImpl() can also be invoked with
751  // explicit template arguments. It doesn't really need to be
752  // executed as this is a compile-time verification.
753  return ConcatImpl<std::string, T1, T2>(a, b, c);
754  }
755 }
756 
757 // Defines another partially specialized wrapper that restricts the
758 // second parameter to int.
759 template <typename T1, typename T2>
760 ConcatImplActionP3<T1, int, T2>
761 Concat(T1 a, int b, T2 c) {
762  return ConcatImpl(a, b, c);
763 }
764 
765 TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
766  Action<const std::string()> a1 = Concat("Hello", "1", 2);
767  EXPECT_EQ("Hello12", a1.Perform(std::make_tuple()));
768 
769  a1 = Concat(1, 2, 3);
770  EXPECT_EQ("123", a1.Perform(std::make_tuple()));
771 }
772 
773 // Verifies the type of an ACTION*.
774 
775 ACTION(DoFoo) {}
776 ACTION_P(DoFoo, p) {}
777 ACTION_P2(DoFoo, p0, p1) {}
778 
779 TEST(ActionPnMacroTest, TypesAreCorrect) {
780  // DoFoo() must be assignable to a DoFooAction variable.
781  DoFooAction a0 = DoFoo();
782 
783  // DoFoo(1) must be assignable to a DoFooActionP variable.
784  DoFooActionP<int> a1 = DoFoo(1);
785 
786  // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
787  // variable, and so on.
788  DoFooActionP2<int, char> a2 = DoFoo(1, '2');
789  PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
790  PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
791  PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
792  PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
793  PlusActionP7<int, int, int, int, int, int, char> a7 =
794  Plus(1, 2, 3, 4, 5, 6, '7');
795  PlusActionP8<int, int, int, int, int, int, int, char> a8 =
796  Plus(1, 2, 3, 4, 5, 6, 7, '8');
797  PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
798  Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
799  PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
800  Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
801 
802  // Avoid "unused variable" warnings.
803  (void)a0;
804  (void)a1;
805  (void)a2;
806  (void)a3;
807  (void)a4;
808  (void)a5;
809  (void)a6;
810  (void)a7;
811  (void)a8;
812  (void)a9;
813  (void)a10;
814 }
815 
816 // Tests that an ACTION_P*() action can be explicitly instantiated
817 // with reference-typed parameters.
818 
819 ACTION_P(Plus1, x) { return x; }
820 ACTION_P2(Plus2, x, y) { return x + y; }
821 ACTION_P3(Plus3, x, y, z) { return x + y + z; }
822 ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
823  return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
824 }
825 
826 TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
827  int x = 1, y = 2, z = 3;
828  const std::tuple<> empty = std::make_tuple();
829 
830  Action<int()> a = Plus1<int&>(x);
831  EXPECT_EQ(1, a.Perform(empty));
832 
833  a = Plus2<const int&, int&>(x, y);
834  EXPECT_EQ(3, a.Perform(empty));
835 
836  a = Plus3<int&, const int&, int&>(x, y, z);
837  EXPECT_EQ(6, a.Perform(empty));
838 
839  int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
840  a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
841  int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
842  n[8], n[9]);
843  EXPECT_EQ(55, a.Perform(empty));
844 }
845 
846 
848  public:
849  TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
850  int a6, int a7, int a8, int a9, int a10)
851  : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
852  }
853  int value_;
854 };
855 
856 // Tests that ACTION_TEMPLATE works when there is no value parameter.
857 ACTION_TEMPLATE(CreateNew,
858  HAS_1_TEMPLATE_PARAMS(typename, T),
859  AND_0_VALUE_PARAMS()) {
860  return new T;
861 }
862 
863 TEST(ActionTemplateTest, WorksWithoutValueParam) {
864  const Action<int*()> a = CreateNew<int>();
865  int* p = a.Perform(std::make_tuple());
866  delete p;
867 }
868 
869 // Tests that ACTION_TEMPLATE works when there are value parameters.
870 ACTION_TEMPLATE(CreateNew,
871  HAS_1_TEMPLATE_PARAMS(typename, T),
872  AND_1_VALUE_PARAMS(a0)) {
873  return new T(a0);
874 }
875 
876 TEST(ActionTemplateTest, WorksWithValueParams) {
877  const Action<int*()> a = CreateNew<int>(42);
878  int* p = a.Perform(std::make_tuple());
879  EXPECT_EQ(42, *p);
880  delete p;
881 }
882 
883 // Tests that ACTION_TEMPLATE works for integral template parameters.
884 ACTION_TEMPLATE(MyDeleteArg,
885  HAS_1_TEMPLATE_PARAMS(int, k),
886  AND_0_VALUE_PARAMS()) {
887  delete std::get<k>(args);
888 }
889 
890 // Resets a bool variable in the destructor.
892  public:
893  explicit BoolResetter(bool* value) : value_(value) {}
894  ~BoolResetter() { *value_ = false; }
895  private:
896  bool* value_;
897 };
898 
899 TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
900  const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
901  int n = 0;
902  bool b = true;
903  BoolResetter* resetter = new BoolResetter(&b);
904  a.Perform(std::make_tuple(&n, resetter));
905  EXPECT_FALSE(b); // Verifies that resetter is deleted.
906 }
907 
908 // Tests that ACTION_TEMPLATES works for template template parameters.
909 ACTION_TEMPLATE(ReturnSmartPointer,
910  HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
911  Pointer),
912  AND_1_VALUE_PARAMS(pointee)) {
913  return Pointer<pointee_type>(new pointee_type(pointee));
914 }
915 
916 TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
917  const Action<std::shared_ptr<int>()> a =
918  ReturnSmartPointer<std::shared_ptr>(42);
919  std::shared_ptr<int> p = a.Perform(std::make_tuple());
920  EXPECT_EQ(42, *p);
921 }
922 
923 // Tests that ACTION_TEMPLATE works for 10 template parameters.
924 template <typename T1, typename T2, typename T3, int k4, bool k5,
925  unsigned int k6, typename T7, typename T8, typename T9>
927  public:
928  explicit GiantTemplate(int a_value) : value(a_value) {}
929  int value;
930 };
931 
932 ACTION_TEMPLATE(ReturnGiant,
933  HAS_10_TEMPLATE_PARAMS(
934  typename, T1,
935  typename, T2,
936  typename, T3,
937  int, k4,
938  bool, k5,
939  unsigned int, k6,
940  class, T7,
941  class, T8,
942  class, T9,
943  template <typename T> class, T10),
944  AND_1_VALUE_PARAMS(value)) {
945  return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
946 }
947 
948 TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
949  using Giant = GiantTemplate<std::shared_ptr<int>, bool, double, 5, true, 6,
950  char, unsigned, int>;
951  const Action<Giant()> a = ReturnGiant<int, bool, double, 5, true, 6, char,
952  unsigned, int, std::shared_ptr>(42);
953  Giant giant = a.Perform(std::make_tuple());
954  EXPECT_EQ(42, giant.value);
955 }
956 
957 // Tests that ACTION_TEMPLATE works for 10 value parameters.
958 ACTION_TEMPLATE(ReturnSum,
959  HAS_1_TEMPLATE_PARAMS(typename, Number),
960  AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
961  return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
962 }
963 
964 TEST(ActionTemplateTest, WorksFor10ValueParameters) {
965  const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
966  EXPECT_EQ(55, a.Perform(std::make_tuple()));
967 }
968 
969 // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
970 // on the number of value parameters.
971 
972 ACTION(ReturnSum) { return 0; }
973 
974 ACTION_P(ReturnSum, x) { return x; }
975 
976 ACTION_TEMPLATE(ReturnSum,
977  HAS_1_TEMPLATE_PARAMS(typename, Number),
978  AND_2_VALUE_PARAMS(v1, v2)) {
979  return static_cast<Number>(v1) + v2;
980 }
981 
982 ACTION_TEMPLATE(ReturnSum,
983  HAS_1_TEMPLATE_PARAMS(typename, Number),
984  AND_3_VALUE_PARAMS(v1, v2, v3)) {
985  return static_cast<Number>(v1) + v2 + v3;
986 }
987 
988 ACTION_TEMPLATE(ReturnSum,
989  HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
990  AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
991  return static_cast<Number>(v1) + v2 + v3 + v4 + k;
992 }
993 
994 TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
995  const Action<int()> a0 = ReturnSum();
996  const Action<int()> a1 = ReturnSum(1);
997  const Action<int()> a2 = ReturnSum<int>(1, 2);
998  const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
999  const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1000  EXPECT_EQ(0, a0.Perform(std::make_tuple()));
1001  EXPECT_EQ(1, a1.Perform(std::make_tuple()));
1002  EXPECT_EQ(3, a2.Perform(std::make_tuple()));
1003  EXPECT_EQ(6, a3.Perform(std::make_tuple()));
1004  EXPECT_EQ(12345, a4.Perform(std::make_tuple()));
1005 }
1006 
1007 
1008 } // namespace gmock_generated_actions_test
1009 } // namespace testing
const char * p
int SumOf5(int a, int b, int c, int d, int e)
int operator()(int a, int b, int c, int d, int e, int f)
#define EXPECT_DOUBLE_EQ(val1, val2)
Definition: gtest.h:2143
void f()
internal::IgnoredValue Unused
std::string 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)
ACTION_P2(OverloadedAction, true_value, false_value)
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
Definition: gtest-port.h:727
ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T), AND_0_VALUE_PARAMS())
std::string 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)
internal::DoAllAction< typename std::decay< Action >::type...> DoAll(Action &&...action)
std::string 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)
inline::std::reference_wrapper< T > ByRef(T &l_value)
ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param)
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
#define T
Definition: Sacado_rad.hpp:553
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
#define T2(r, f)
Definition: Sacado_rad.hpp:558
double Plus(double a, double b)
ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8)
TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10)
int SumOf6(int a, int b, int c, int d, int e, int f)
std::string Concat6(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6)
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:2107
#define T1(r, f)
Definition: Sacado_rad.hpp:583
void g()
const char * Binary(const char *input, short n)
internal::SetArgumentPointeeAction< N, T > SetArgPointee(T value)
void
Definition: uninit.c:105
int value
Uncopyable z
internal::ReturnAction< R > Return(R value)
ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2038
ConcatImplActionP3< std::string, T1, T2 > Concat(const std::string &a, T1 b, T2 c)
std::string Concat7(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1979
constexpr bool StaticAssertTypeEq() noexcept
Definition: gtest.h:2311
#define EXPECT_FALSE(condition)
Definition: gtest.h:1982
int n
Result Perform(ArgumentTuple args) const
#define GTEST_INTENTIONAL_CONST_COND_POP_()
Definition: gtest-port.h:729
const double y
std::string Concat5(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5)