gmock-generated-actions_test.cc 34.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Gennadiy Civil's avatar
 
Gennadiy Civil committed
29

30
31
32
33
34

// Google Mock - a framework for writing C++ mock classes.
//
// This file tests the built-in actions generated by a script.

35
#include "gmock/gmock-generated-actions.h"
36
37

#include <functional>
misterg's avatar
misterg committed
38
#include <memory>
39
#include <sstream>
40
#include <string>
41
42
#include "gmock/gmock.h"
#include "gtest/gtest.h"
43
44
45
46
47
48
49
50
51
52
53
54
55

namespace testing {
namespace gmock_generated_actions_test {

using ::std::plus;
using ::std::string;
using testing::_;
using testing::Action;
using testing::ActionInterface;
using testing::ByRef;
using testing::DoAll;
using testing::Invoke;
using testing::Return;
56
using testing::ReturnNew;
57
using testing::SetArgPointee;
shiqian's avatar
shiqian committed
58
using testing::StaticAssertTypeEq;
59
60
using testing::Unused;

61
62
63
64
// For suppressing compiler warnings on conversion possibly losing precision.
inline short Short(short n) { return n; }  // NOLINT
inline char Char(char ch) { return ch; }

65
// Sample functions and functors for testing various actions.
66
67
68
69
int Nullary() { return 1; }

bool g_done = false;

70
bool ByConstRef(const std::string& s) { return s == "Hi"; }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

const double g_double = 0;
bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }

struct UnaryFunctor {
  int operator()(bool x) { return x ? 1 : -1; }
};

const char* Binary(const char* input, short n) { return input + n; }  // NOLINT

int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }

struct SumOf5Functor {
  int operator()(int a, int b, int c, int d, int e) {
    return a + b + c + d + e;
  }
};

89
90
91
std::string Concat5(const char* s1, const char* s2, const char* s3,
                    const char* s4, const char* s5) {
  return std::string(s1) + s2 + s3 + s4 + s5;
92
93
94
95
96
97
98
99
100
101
102
103
}

int SumOf6(int a, int b, int c, int d, int e, int f) {
  return a + b + c + d + e + f;
}

struct SumOf6Functor {
  int operator()(int a, int b, int c, int d, int e, int f) {
    return a + b + c + d + e + f;
  }
};

104
105
106
std::string Concat6(const char* s1, const char* s2, const char* s3,
                    const char* s4, const char* s5, const char* s6) {
  return std::string(s1) + s2 + s3 + s4 + s5 + s6;
107
108
}

109
110
111
112
std::string Concat7(const char* s1, const char* s2, const char* s3,
                    const char* s4, const char* s5, const char* s6,
                    const char* s7) {
  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
113
114
}

115
116
117
118
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) {
  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
119
120
}

121
122
123
124
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) {
  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
125
126
}

127
128
129
130
131
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) {
  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
132
133
}

134
135
136
137
// A helper that turns the type of a C-string literal from const
// char[N] to const char*.
inline const char* CharPtr(const char* s) { return s; }

138
139
140
141
142
// Tests InvokeArgument<N>(...).

// Tests using InvokeArgument with a nullary function.
TEST(InvokeArgumentTest, Function0) {
  Action<int(int, int(*)())> a = InvokeArgument<1>();  // NOLINT
Abseil Team's avatar
Abseil Team committed
143
  EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
144
145
146
147
148
}

// Tests using InvokeArgument with a unary function.
TEST(InvokeArgumentTest, Functor1) {
  Action<int(UnaryFunctor)> a = InvokeArgument<0>(true);  // NOLINT
Abseil Team's avatar
Abseil Team committed
149
  EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
150
151
152
153
154
155
}

// Tests using InvokeArgument with a 5-ary function.
TEST(InvokeArgumentTest, Function5) {
  Action<int(int(*)(int, int, int, int, int))> a =  // NOLINT
      InvokeArgument<0>(10000, 2000, 300, 40, 5);
Abseil Team's avatar
Abseil Team committed
156
  EXPECT_EQ(12345, a.Perform(std::make_tuple(&SumOf5)));
157
158
159
160
161
162
}

// Tests using InvokeArgument with a 5-ary functor.
TEST(InvokeArgumentTest, Functor5) {
  Action<int(SumOf5Functor)> a =  // NOLINT
      InvokeArgument<0>(10000, 2000, 300, 40, 5);
Abseil Team's avatar
Abseil Team committed
163
  EXPECT_EQ(12345, a.Perform(std::make_tuple(SumOf5Functor())));
164
165
166
167
168
169
}

// Tests using InvokeArgument with a 6-ary function.
TEST(InvokeArgumentTest, Function6) {
  Action<int(int(*)(int, int, int, int, int, int))> a =  // NOLINT
      InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
Abseil Team's avatar
Abseil Team committed
170
  EXPECT_EQ(123456, a.Perform(std::make_tuple(&SumOf6)));
171
172
173
174
175
176
}

// Tests using InvokeArgument with a 6-ary functor.
TEST(InvokeArgumentTest, Functor6) {
  Action<int(SumOf6Functor)> a =  // NOLINT
      InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
Abseil Team's avatar
Abseil Team committed
177
  EXPECT_EQ(123456, a.Perform(std::make_tuple(SumOf6Functor())));
178
179
180
181
}

// Tests using InvokeArgument with a 7-ary function.
TEST(InvokeArgumentTest, Function7) {
182
183
184
185
  Action<std::string(std::string(*)(const char*, const char*, const char*,
                                    const char*, const char*, const char*,
                                    const char*))>
      a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
Abseil Team's avatar
Abseil Team committed
186
  EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
187
188
189
190
}

// Tests using InvokeArgument with a 8-ary function.
TEST(InvokeArgumentTest, Function8) {
191
192
193
194
  Action<std::string(std::string(*)(const char*, const char*, const char*,
                                    const char*, const char*, const char*,
                                    const char*, const char*))>
      a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
Abseil Team's avatar
Abseil Team committed
195
  EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
196
197
198
199
}

// Tests using InvokeArgument with a 9-ary function.
TEST(InvokeArgumentTest, Function9) {
200
201
202
203
  Action<std::string(std::string(*)(const char*, const char*, const char*,
                                    const char*, const char*, const char*,
                                    const char*, const char*, const char*))>
      a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
Abseil Team's avatar
Abseil Team committed
204
  EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
205
206
207
208
}

// Tests using InvokeArgument with a 10-ary function.
TEST(InvokeArgumentTest, Function10) {
209
210
211
212
  Action<std::string(std::string(*)(
      const char*, const char*, const char*, const char*, const char*,
      const char*, const char*, const char*, const char*, const char*))>
      a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
Abseil Team's avatar
Abseil Team committed
213
  EXPECT_EQ("1234567890", a.Perform(std::make_tuple(&Concat10)));
214
215
216
217
218
}

// Tests using InvokeArgument with a function that takes a pointer argument.
TEST(InvokeArgumentTest, ByPointerFunction) {
  Action<const char*(const char*(*)(const char* input, short n))> a =  // NOLINT
219
      InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
Abseil Team's avatar
Abseil Team committed
220
  EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
221
222
223
224
225
226
}

// Tests using InvokeArgument with a function that takes a const char*
// by passing it a C-string literal.
TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
  Action<const char*(const char*(*)(const char* input, short n))> a =  // NOLINT
227
      InvokeArgument<0>("Hi", Short(1));
Abseil Team's avatar
Abseil Team committed
228
  EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
229
230
231
232
}

// Tests using InvokeArgument with a function that takes a const reference.
TEST(InvokeArgumentTest, ByConstReferenceFunction) {
233
234
  Action<bool(bool (*function)(const std::string& s))> a =  // NOLINT
      InvokeArgument<0>(std::string("Hi"));
235
236
237
  // When action 'a' is constructed, it makes a copy of the temporary
  // string object passed to it, so it's OK to use 'a' later, when the
  // temporary object has already died.
Abseil Team's avatar
Abseil Team committed
238
  EXPECT_TRUE(a.Perform(std::make_tuple(&ByConstRef)));
239
240
241
242
243
244
245
246
}

// Tests using InvokeArgument with ByRef() and a function that takes a
// const reference.
TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
  Action<bool(bool(*)(const double& x))> a =  // NOLINT
      InvokeArgument<0>(ByRef(g_double));
  // The above line calls ByRef() on a const value.
Abseil Team's avatar
Abseil Team committed
247
  EXPECT_TRUE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
248
249
250

  double x = 0;
  a = InvokeArgument<0>(ByRef(x));  // This calls ByRef() on a non-const.
Abseil Team's avatar
Abseil Team committed
251
  EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
252
253
254
255
256
}

// Tests DoAll(a1, a2).
TEST(DoAllTest, TwoActions) {
  int n = 0;
257
  Action<int(int*)> a = DoAll(SetArgPointee<0>(1),  // NOLINT
258
                              Return(2));
Abseil Team's avatar
Abseil Team committed
259
  EXPECT_EQ(2, a.Perform(std::make_tuple(&n)));
260
261
262
263
264
265
  EXPECT_EQ(1, n);
}

// Tests DoAll(a1, a2, a3).
TEST(DoAllTest, ThreeActions) {
  int m = 0, n = 0;
266
267
  Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1),  // NOLINT
                                    SetArgPointee<1>(2),
268
                                    Return(3));
Abseil Team's avatar
Abseil Team committed
269
  EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n)));
270
271
272
273
274
275
276
277
278
  EXPECT_EQ(1, m);
  EXPECT_EQ(2, n);
}

// Tests DoAll(a1, a2, a3, a4).
TEST(DoAllTest, FourActions) {
  int m = 0, n = 0;
  char ch = '\0';
  Action<int(int*, int*, char*)> a =  // NOLINT
279
280
281
      DoAll(SetArgPointee<0>(1),
            SetArgPointee<1>(2),
            SetArgPointee<2>('a'),
282
            Return(3));
Abseil Team's avatar
Abseil Team committed
283
  EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n, &ch)));
284
285
286
287
288
289
290
291
292
293
  EXPECT_EQ(1, m);
  EXPECT_EQ(2, n);
  EXPECT_EQ('a', ch);
}

// Tests DoAll(a1, a2, a3, a4, a5).
TEST(DoAllTest, FiveActions) {
  int m = 0, n = 0;
  char a = '\0', b = '\0';
  Action<int(int*, int*, char*, char*)> action =  // NOLINT
294
295
296
297
      DoAll(SetArgPointee<0>(1),
            SetArgPointee<1>(2),
            SetArgPointee<2>('a'),
            SetArgPointee<3>('b'),
298
            Return(3));
Abseil Team's avatar
Abseil Team committed
299
  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b)));
300
301
302
303
304
305
306
307
308
309
310
  EXPECT_EQ(1, m);
  EXPECT_EQ(2, n);
  EXPECT_EQ('a', a);
  EXPECT_EQ('b', b);
}

// Tests DoAll(a1, a2, ..., a6).
TEST(DoAllTest, SixActions) {
  int m = 0, n = 0;
  char a = '\0', b = '\0', c = '\0';
  Action<int(int*, int*, char*, char*, char*)> action =  // NOLINT
311
312
313
314
315
      DoAll(SetArgPointee<0>(1),
            SetArgPointee<1>(2),
            SetArgPointee<2>('a'),
            SetArgPointee<3>('b'),
            SetArgPointee<4>('c'),
316
            Return(3));
Abseil Team's avatar
Abseil Team committed
317
  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c)));
318
319
320
321
322
323
324
325
326
327
328
329
  EXPECT_EQ(1, m);
  EXPECT_EQ(2, n);
  EXPECT_EQ('a', a);
  EXPECT_EQ('b', b);
  EXPECT_EQ('c', c);
}

// Tests DoAll(a1, a2, ..., a7).
TEST(DoAllTest, SevenActions) {
  int m = 0, n = 0;
  char a = '\0', b = '\0', c = '\0', d = '\0';
  Action<int(int*, int*, char*, char*, char*, char*)> action =  // NOLINT
330
331
332
333
334
335
      DoAll(SetArgPointee<0>(1),
            SetArgPointee<1>(2),
            SetArgPointee<2>('a'),
            SetArgPointee<3>('b'),
            SetArgPointee<4>('c'),
            SetArgPointee<5>('d'),
336
            Return(3));
Abseil Team's avatar
Abseil Team committed
337
  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d)));
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  EXPECT_EQ(1, m);
  EXPECT_EQ(2, n);
  EXPECT_EQ('a', a);
  EXPECT_EQ('b', b);
  EXPECT_EQ('c', c);
  EXPECT_EQ('d', d);
}

// Tests DoAll(a1, a2, ..., a8).
TEST(DoAllTest, EightActions) {
  int m = 0, n = 0;
  char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
  Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
             char*)> action =
352
353
354
355
356
357
358
      DoAll(SetArgPointee<0>(1),
            SetArgPointee<1>(2),
            SetArgPointee<2>('a'),
            SetArgPointee<3>('b'),
            SetArgPointee<4>('c'),
            SetArgPointee<5>('d'),
            SetArgPointee<6>('e'),
359
            Return(3));
Abseil Team's avatar
Abseil Team committed
360
  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e)));
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  EXPECT_EQ(1, m);
  EXPECT_EQ(2, n);
  EXPECT_EQ('a', a);
  EXPECT_EQ('b', b);
  EXPECT_EQ('c', c);
  EXPECT_EQ('d', d);
  EXPECT_EQ('e', e);
}

// Tests DoAll(a1, a2, ..., a9).
TEST(DoAllTest, NineActions) {
  int m = 0, n = 0;
  char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
  Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
             char*, char*)> action =
376
377
378
379
380
381
382
383
      DoAll(SetArgPointee<0>(1),
            SetArgPointee<1>(2),
            SetArgPointee<2>('a'),
            SetArgPointee<3>('b'),
            SetArgPointee<4>('c'),
            SetArgPointee<5>('d'),
            SetArgPointee<6>('e'),
            SetArgPointee<7>('f'),
384
            Return(3));
Abseil Team's avatar
Abseil Team committed
385
  EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  EXPECT_EQ(1, m);
  EXPECT_EQ(2, n);
  EXPECT_EQ('a', a);
  EXPECT_EQ('b', b);
  EXPECT_EQ('c', c);
  EXPECT_EQ('d', d);
  EXPECT_EQ('e', e);
  EXPECT_EQ('f', f);
}

// Tests DoAll(a1, a2, ..., a10).
TEST(DoAllTest, TenActions) {
  int m = 0, n = 0;
  char a = '\0', b = '\0', c = '\0', d = '\0';
  char e = '\0', f = '\0', g = '\0';
  Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
             char*, char*, char*)> action =
403
404
405
406
407
408
409
410
411
      DoAll(SetArgPointee<0>(1),
            SetArgPointee<1>(2),
            SetArgPointee<2>('a'),
            SetArgPointee<3>('b'),
            SetArgPointee<4>('c'),
            SetArgPointee<5>('d'),
            SetArgPointee<6>('e'),
            SetArgPointee<7>('f'),
            SetArgPointee<8>('g'),
412
            Return(3));
Abseil Team's avatar
Abseil Team committed
413
414
  EXPECT_EQ(
      3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
415
416
417
418
419
420
421
422
423
424
425
  EXPECT_EQ(1, m);
  EXPECT_EQ(2, n);
  EXPECT_EQ('a', a);
  EXPECT_EQ('b', b);
  EXPECT_EQ('c', c);
  EXPECT_EQ('d', d);
  EXPECT_EQ('e', e);
  EXPECT_EQ('f', f);
  EXPECT_EQ('g', g);
}

426
427
428
429
430
// The ACTION*() macros trigger warning C4100 (unreferenced formal
// parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
// the macro definition, as the warnings are generated when the macro
// is expanded and macro expansion cannot contain #pragma.  Therefore
// we suppress them here.
misterg's avatar
misterg committed
431
// Also suppress C4503 decorated name length exceeded, name was truncated
432
#ifdef _MSC_VER
433
434
# pragma warning(push)
# pragma warning(disable:4100)
misterg's avatar
misterg committed
435
# pragma warning(disable:4503)
436
437
#endif

shiqian's avatar
shiqian committed
438
439
440
441
442
443
444
445
// Tests the ACTION*() macro family.

// Tests that ACTION() can define an action that doesn't reference the
// mock function arguments.
ACTION(Return5) { return 5; }

TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
  Action<double()> a1 = Return5();
Abseil Team's avatar
Abseil Team committed
446
  EXPECT_DOUBLE_EQ(5, a1.Perform(std::make_tuple()));
shiqian's avatar
shiqian committed
447
448

  Action<int(double, bool)> a2 = Return5();
Abseil Team's avatar
Abseil Team committed
449
  EXPECT_EQ(5, a2.Perform(std::make_tuple(1, true)));
shiqian's avatar
shiqian committed
450
451
452
453
454
455
456
457
}

// Tests that ACTION() can define an action that returns void.
ACTION(IncrementArg1) { (*arg1)++; }

TEST(ActionMacroTest, WorksWhenReturningVoid) {
  Action<void(int, int*)> a1 = IncrementArg1();
  int n = 0;
Abseil Team's avatar
Abseil Team committed
458
  a1.Perform(std::make_tuple(5, &n));
shiqian's avatar
shiqian committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  EXPECT_EQ(1, n);
}

// Tests that the body of ACTION() can reference the type of the
// argument.
ACTION(IncrementArg2) {
  StaticAssertTypeEq<int*, arg2_type>();
  arg2_type temp = arg2;
  (*temp)++;
}

TEST(ActionMacroTest, CanReferenceArgumentType) {
  Action<void(int, bool, int*)> a1 = IncrementArg2();
  int n = 0;
Abseil Team's avatar
Abseil Team committed
473
  a1.Perform(std::make_tuple(5, false, &n));
shiqian's avatar
shiqian committed
474
475
476
477
478
479
  EXPECT_EQ(1, n);
}

// Tests that the body of ACTION() can reference the argument tuple
// via args_type and args.
ACTION(Sum2) {
Abseil Team's avatar
Abseil Team committed
480
  StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
shiqian's avatar
shiqian committed
481
  args_type args_copy = args;
Abseil Team's avatar
Abseil Team committed
482
  return std::get<0>(args_copy) + std::get<1>(args_copy);
shiqian's avatar
shiqian committed
483
484
485
486
487
}

TEST(ActionMacroTest, CanReferenceArgumentTuple) {
  Action<int(int, char, int*)> a1 = Sum2();
  int dummy = 0;
Abseil Team's avatar
Abseil Team committed
488
  EXPECT_EQ(11, a1.Perform(std::make_tuple(5, Char(6), &dummy)));
shiqian's avatar
shiqian committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
}

// Tests that the body of ACTION() can reference the mock function
// type.
int Dummy(bool flag) { return flag? 1 : 0; }

ACTION(InvokeDummy) {
  StaticAssertTypeEq<int(bool), function_type>();
  function_type* fp = &Dummy;
  return (*fp)(true);
}

TEST(ActionMacroTest, CanReferenceMockFunctionType) {
  Action<int(bool)> a1 = InvokeDummy();
Abseil Team's avatar
Abseil Team committed
503
504
  EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
  EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
shiqian's avatar
shiqian committed
505
506
507
508
509
510
511
512
513
514
515
516
}

// Tests that the body of ACTION() can reference the mock function's
// return type.
ACTION(InvokeDummy2) {
  StaticAssertTypeEq<int, return_type>();
  return_type result = Dummy(true);
  return result;
}

TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
  Action<int(bool)> a1 = InvokeDummy2();
Abseil Team's avatar
Abseil Team committed
517
518
  EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
  EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
shiqian's avatar
shiqian committed
519
520
}

521
522
523
524
525
526
527
528
529
// Tests that ACTION() works for arguments passed by const reference.
ACTION(ReturnAddrOfConstBoolReferenceArg) {
  StaticAssertTypeEq<const bool&, arg1_type>();
  return &arg1;
}

TEST(ActionMacroTest, WorksForConstReferenceArg) {
  Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
  const bool b = false;
Abseil Team's avatar
Abseil Team committed
530
  EXPECT_EQ(&b, a.Perform(std::tuple<int, const bool&>(0, b)));
531
532
533
534
535
536
537
538
539
540
541
}

// Tests that ACTION() works for arguments passed by non-const reference.
ACTION(ReturnAddrOfIntReferenceArg) {
  StaticAssertTypeEq<int&, arg0_type>();
  return &arg0;
}

TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
  Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
  int n = 0;
Abseil Team's avatar
Abseil Team committed
542
  EXPECT_EQ(&n, a.Perform(std::tuple<int&, bool, int>(n, true, 1)));
543
544
}

shiqian's avatar
shiqian committed
545
546
547
548
549
550
551
// Tests that ACTION() can be used in a namespace.
namespace action_test {
ACTION(Sum) { return arg0 + arg1; }
}  // namespace action_test

TEST(ActionMacroTest, WorksInNamespace) {
  Action<int(int, int)> a1 = action_test::Sum();
Abseil Team's avatar
Abseil Team committed
552
  EXPECT_EQ(3, a1.Perform(std::make_tuple(1, 2)));
shiqian's avatar
shiqian committed
553
554
555
556
557
558
559
560
}

// Tests that the same ACTION definition works for mock functions with
// different argument numbers.
ACTION(PlusTwo) { return arg0 + 2; }

TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
  Action<int(int)> a1 = PlusTwo();
Abseil Team's avatar
Abseil Team committed
561
  EXPECT_EQ(4, a1.Perform(std::make_tuple(2)));
shiqian's avatar
shiqian committed
562
563
564

  Action<double(float, void*)> a2 = PlusTwo();
  int dummy;
Abseil Team's avatar
Abseil Team committed
565
  EXPECT_DOUBLE_EQ(6, a2.Perform(std::make_tuple(4.0f, &dummy)));
shiqian's avatar
shiqian committed
566
567
568
569
570
571
572
}

// Tests that ACTION_P can define a parameterized action.
ACTION_P(Plus, n) { return arg0 + n; }

TEST(ActionPMacroTest, DefinesParameterizedAction) {
  Action<int(int m, bool t)> a1 = Plus(9);
Abseil Team's avatar
Abseil Team committed
573
  EXPECT_EQ(10, a1.Perform(std::make_tuple(1, true)));
shiqian's avatar
shiqian committed
574
575
576
577
578
579
580
581
582
583
584
585
}

// Tests that the body of ACTION_P can reference the argument types
// and the parameter type.
ACTION_P(TypedPlus, n) {
  arg0_type t1 = arg0;
  n_type t2 = n;
  return t1 + t2;
}

TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
  Action<int(char m, bool t)> a1 = TypedPlus(9);
Abseil Team's avatar
Abseil Team committed
586
  EXPECT_EQ(10, a1.Perform(std::make_tuple(Char(1), true)));
shiqian's avatar
shiqian committed
587
588
589
590
591
592
593
}

// Tests that a parameterized action can be used in any mock function
// whose type is compatible.
TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
  Action<std::string(const std::string& s)> a1 = Plus("tail");
  const std::string re = "re";
Abseil Team's avatar
Abseil Team committed
594
  std::tuple<const std::string> dummy = std::make_tuple(re);
Gennadiy Civil's avatar
 
Gennadiy Civil committed
595
  EXPECT_EQ("retail", a1.Perform(dummy));
shiqian's avatar
shiqian committed
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
}

// Tests that we can use ACTION*() to define actions overloaded on the
// number of parameters.

ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }

ACTION_P(OverloadedAction, default_value) {
  return arg0 ? arg1 : default_value;
}

ACTION_P2(OverloadedAction, true_value, false_value) {
  return arg0 ? true_value : false_value;
}

TEST(ActionMacroTest, CanDefineOverloadedActions) {
  typedef Action<const char*(bool, const char*)> MyAction;

  const MyAction a1 = OverloadedAction();
Abseil Team's avatar
Abseil Team committed
615
616
  EXPECT_STREQ("hello", a1.Perform(std::make_tuple(false, CharPtr("world"))));
  EXPECT_STREQ("world", a1.Perform(std::make_tuple(true, CharPtr("world"))));
shiqian's avatar
shiqian committed
617
618

  const MyAction a2 = OverloadedAction("hi");
Abseil Team's avatar
Abseil Team committed
619
620
  EXPECT_STREQ("hi", a2.Perform(std::make_tuple(false, CharPtr("world"))));
  EXPECT_STREQ("world", a2.Perform(std::make_tuple(true, CharPtr("world"))));
shiqian's avatar
shiqian committed
621
622

  const MyAction a3 = OverloadedAction("hi", "you");
Abseil Team's avatar
Abseil Team committed
623
624
  EXPECT_STREQ("hi", a3.Perform(std::make_tuple(true, CharPtr("world"))));
  EXPECT_STREQ("you", a3.Perform(std::make_tuple(false, CharPtr("world"))));
shiqian's avatar
shiqian committed
625
626
627
628
629
630
631
632
}

// Tests ACTION_Pn where n >= 3.

ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }

TEST(ActionPnMacroTest, WorksFor3Parameters) {
  Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
Abseil Team's avatar
Abseil Team committed
633
  EXPECT_DOUBLE_EQ(3123.4, a1.Perform(std::make_tuple(3000, true)));
shiqian's avatar
shiqian committed
634
635
636

  Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
  const std::string re = "re";
Abseil Team's avatar
Abseil Team committed
637
  std::tuple<const std::string> dummy = std::make_tuple(re);
Gennadiy Civil's avatar
 
Gennadiy Civil committed
638
  EXPECT_EQ("retail->", a2.Perform(dummy));
shiqian's avatar
shiqian committed
639
640
641
642
643
644
}

ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }

TEST(ActionPnMacroTest, WorksFor4Parameters) {
  Action<int(int)> a1 = Plus(1, 2, 3, 4);
Abseil Team's avatar
Abseil Team committed
645
  EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(std::make_tuple(10)));
shiqian's avatar
shiqian committed
646
647
648
649
650
651
}

ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }

TEST(ActionPnMacroTest, WorksFor5Parameters) {
  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
Abseil Team's avatar
Abseil Team committed
652
  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(std::make_tuple(10)));
shiqian's avatar
shiqian committed
653
654
655
656
657
658
659
660
}

ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
  return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
}

TEST(ActionPnMacroTest, WorksFor6Parameters) {
  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
Abseil Team's avatar
Abseil Team committed
661
  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(std::make_tuple(10)));
shiqian's avatar
shiqian committed
662
663
664
665
666
667
668
669
}

ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
  return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
}

TEST(ActionPnMacroTest, WorksFor7Parameters) {
  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
Abseil Team's avatar
Abseil Team committed
670
  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(std::make_tuple(10)));
shiqian's avatar
shiqian committed
671
672
673
674
675
676
677
678
}

ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
  return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
}

TEST(ActionPnMacroTest, WorksFor8Parameters) {
  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
Abseil Team's avatar
Abseil Team committed
679
680
  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
            a1.Perform(std::make_tuple(10)));
shiqian's avatar
shiqian committed
681
682
683
684
685
686
687
688
}

ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
  return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
}

TEST(ActionPnMacroTest, WorksFor9Parameters) {
  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
Abseil Team's avatar
Abseil Team committed
689
690
  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
            a1.Perform(std::make_tuple(10)));
shiqian's avatar
shiqian committed
691
692
693
694
695
696
697
698
699
700
701
}

ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
  arg0_type t0 = arg0;
  last_param_type t9 = last_param;
  return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
}

TEST(ActionPnMacroTest, WorksFor10Parameters) {
  Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
Abseil Team's avatar
Abseil Team committed
702
            a1.Perform(std::make_tuple(10)));
shiqian's avatar
shiqian committed
703
704
705
706
707
708
709
}

// Tests that the action body can promote the parameter types.

ACTION_P2(PadArgument, prefix, suffix) {
  // The following lines promote the two parameters to desired types.
  std::string prefix_str(prefix);
710
  char suffix_char = static_cast<char>(suffix);
shiqian's avatar
shiqian committed
711
712
713
714
715
716
717
718
  return prefix_str + arg0 + suffix_char;
}

TEST(ActionPnMacroTest, SimpleTypePromotion) {
  Action<std::string(const char*)> no_promo =
      PadArgument(std::string("foo"), 'r');
  Action<std::string(const char*)> promo =
      PadArgument("foo", static_cast<int>('r'));
Abseil Team's avatar
Abseil Team committed
719
720
  EXPECT_EQ("foobar", no_promo.Perform(std::make_tuple(CharPtr("ba"))));
  EXPECT_EQ("foobar", promo.Perform(std::make_tuple(CharPtr("ba"))));
shiqian's avatar
shiqian committed
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
}

// Tests that we can partially restrict parameter types using a
// straight-forward pattern.

// Defines a generic action that doesn't restrict the types of its
// parameters.
ACTION_P3(ConcatImpl, a, b, c) {
  std::stringstream ss;
  ss << a << b << c;
  return ss.str();
}

// Next, we try to restrict that either the first parameter is a
// string, or the second parameter is an int.

// Defines a partially specialized wrapper that restricts the first
// parameter to std::string.
template <typename T1, typename T2>
// ConcatImplActionP3 is the class template ACTION_P3 uses to
// implement ConcatImpl.  We shouldn't change the name as this
// pattern requires the user to use it directly.
ConcatImplActionP3<std::string, T1, T2>
Concat(const std::string& a, T1 b, T2 c) {
billydonahue's avatar
billydonahue committed
745
  GTEST_INTENTIONAL_CONST_COND_PUSH_()
shiqian's avatar
shiqian committed
746
  if (true) {
billydonahue's avatar
billydonahue committed
747
  GTEST_INTENTIONAL_CONST_COND_POP_()
shiqian's avatar
shiqian committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
    // This branch verifies that ConcatImpl() can be invoked without
    // explicit template arguments.
    return ConcatImpl(a, b, c);
  } else {
    // This branch verifies that ConcatImpl() can also be invoked with
    // explicit template arguments.  It doesn't really need to be
    // executed as this is a compile-time verification.
    return ConcatImpl<std::string, T1, T2>(a, b, c);
  }
}

// Defines another partially specialized wrapper that restricts the
// second parameter to int.
template <typename T1, typename T2>
ConcatImplActionP3<T1, int, T2>
Concat(T1 a, int b, T2 c) {
  return ConcatImpl(a, b, c);
}

TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
  Action<const std::string()> a1 = Concat("Hello", "1", 2);
Abseil Team's avatar
Abseil Team committed
769
  EXPECT_EQ("Hello12", a1.Perform(std::make_tuple()));
shiqian's avatar
shiqian committed
770
771

  a1 = Concat(1, 2, 3);
Abseil Team's avatar
Abseil Team committed
772
  EXPECT_EQ("123", a1.Perform(std::make_tuple()));
shiqian's avatar
shiqian committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
}

// Verifies the type of an ACTION*.

ACTION(DoFoo) {}
ACTION_P(DoFoo, p) {}
ACTION_P2(DoFoo, p0, p1) {}

TEST(ActionPnMacroTest, TypesAreCorrect) {
  // DoFoo() must be assignable to a DoFooAction variable.
  DoFooAction a0 = DoFoo();

  // DoFoo(1) must be assignable to a DoFooActionP variable.
  DoFooActionP<int> a1 = DoFoo(1);

  // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
  // variable, and so on.
  DoFooActionP2<int, char> a2 = DoFoo(1, '2');
  PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
  PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
  PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
  PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
  PlusActionP7<int, int, int, int, int, int, char> a7 =
      Plus(1, 2, 3, 4, 5, 6, '7');
  PlusActionP8<int, int, int, int, int, int, int, char> a8 =
      Plus(1, 2, 3, 4, 5, 6, 7, '8');
  PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
      Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
  PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
      Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
803
804
805
806
807
808
809
810
811
812
813
814
815

  // Avoid "unused variable" warnings.
  (void)a0;
  (void)a1;
  (void)a2;
  (void)a3;
  (void)a4;
  (void)a5;
  (void)a6;
  (void)a7;
  (void)a8;
  (void)a9;
  (void)a10;
shiqian's avatar
shiqian committed
816
817
}

818
819
820
821
822
823
824
825
826
827
828
829
// Tests that an ACTION_P*() action can be explicitly instantiated
// with reference-typed parameters.

ACTION_P(Plus1, x) { return x; }
ACTION_P2(Plus2, x, y) { return x + y; }
ACTION_P3(Plus3, x, y, z) { return x + y + z; }
ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
  return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
}

TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
  int x = 1, y = 2, z = 3;
Abseil Team's avatar
Abseil Team committed
830
  const std::tuple<> empty = std::make_tuple();
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847

  Action<int()> a = Plus1<int&>(x);
  EXPECT_EQ(1, a.Perform(empty));

  a = Plus2<const int&, int&>(x, y);
  EXPECT_EQ(3, a.Perform(empty));

  a = Plus3<int&, const int&, int&>(x, y, z);
  EXPECT_EQ(6, a.Perform(empty));

  int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
      int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
                              n[8], n[9]);
  EXPECT_EQ(55, a.Perform(empty));
}

848
849
850
851
852
853
854
855
856
class NullaryConstructorClass {
 public:
  NullaryConstructorClass() : value_(123) {}
  int value_;
};

// Tests using ReturnNew() with a nullary constructor.
TEST(ReturnNewTest, NoArgs) {
  Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>();
Abseil Team's avatar
Abseil Team committed
857
  NullaryConstructorClass* c = a.Perform(std::make_tuple());
858
859
860
861
862
863
864
865
866
867
868
869
870
  EXPECT_EQ(123, c->value_);
  delete c;
}

class UnaryConstructorClass {
 public:
  explicit UnaryConstructorClass(int value) : value_(value) {}
  int value_;
};

// Tests using ReturnNew() with a unary constructor.
TEST(ReturnNewTest, Unary) {
  Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
Abseil Team's avatar
Abseil Team committed
871
  UnaryConstructorClass* c = a.Perform(std::make_tuple());
872
873
874
875
876
877
878
  EXPECT_EQ(4000, c->value_);
  delete c;
}

TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
  Action<UnaryConstructorClass*(bool, int)> a =
      ReturnNew<UnaryConstructorClass>(4000);
Abseil Team's avatar
Abseil Team committed
879
  UnaryConstructorClass* c = a.Perform(std::make_tuple(false, 5));
880
881
882
883
884
885
886
  EXPECT_EQ(4000, c->value_);
  delete c;
}

TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
  Action<const UnaryConstructorClass*()> a =
      ReturnNew<UnaryConstructorClass>(4000);
Abseil Team's avatar
Abseil Team committed
887
  const UnaryConstructorClass* c = a.Perform(std::make_tuple());
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
  EXPECT_EQ(4000, c->value_);
  delete c;
}

class TenArgConstructorClass {
 public:
  TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
                         int a6, int a7, int a8, int a9, int a10)
    : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
  }
  int value_;
};

// Tests using ReturnNew() with a 10-argument constructor.
TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
  Action<TenArgConstructorClass*()> a =
      ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
                                        4000000, 500000, 60000,
                                        7000, 800, 90, 0);
Abseil Team's avatar
Abseil Team committed
907
  TenArgConstructorClass* c = a.Perform(std::make_tuple());
908
909
910
911
  EXPECT_EQ(1234567890, c->value_);
  delete c;
}

912
913
914
915
916
917
918
919
920
// Tests that ACTION_TEMPLATE works when there is no value parameter.
ACTION_TEMPLATE(CreateNew,
                HAS_1_TEMPLATE_PARAMS(typename, T),
                AND_0_VALUE_PARAMS()) {
  return new T;
}

TEST(ActionTemplateTest, WorksWithoutValueParam) {
  const Action<int*()> a = CreateNew<int>();
Abseil Team's avatar
Abseil Team committed
921
  int* p = a.Perform(std::make_tuple());
922
923
924
925
926
927
928
929
930
931
932
933
  delete p;
}

// Tests that ACTION_TEMPLATE works when there are value parameters.
ACTION_TEMPLATE(CreateNew,
                HAS_1_TEMPLATE_PARAMS(typename, T),
                AND_1_VALUE_PARAMS(a0)) {
  return new T(a0);
}

TEST(ActionTemplateTest, WorksWithValueParams) {
  const Action<int*()> a = CreateNew<int>(42);
Abseil Team's avatar
Abseil Team committed
934
  int* p = a.Perform(std::make_tuple());
935
936
937
938
939
940
941
942
  EXPECT_EQ(42, *p);
  delete p;
}

// Tests that ACTION_TEMPLATE works for integral template parameters.
ACTION_TEMPLATE(MyDeleteArg,
                HAS_1_TEMPLATE_PARAMS(int, k),
                AND_0_VALUE_PARAMS()) {
Abseil Team's avatar
Abseil Team committed
943
  delete std::get<k>(args);
944
945
946
947
948
949
950
951
}

// Resets a bool variable in the destructor.
class BoolResetter {
 public:
  explicit BoolResetter(bool* value) : value_(value) {}
  ~BoolResetter() { *value_ = false; }
 private:
952
  bool* value_;
953
954
955
956
957
958
959
};

TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
  const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
  int n = 0;
  bool b = true;
  BoolResetter* resetter = new BoolResetter(&b);
Abseil Team's avatar
Abseil Team committed
960
  a.Perform(std::make_tuple(&n, resetter));
961
962
963
  EXPECT_FALSE(b);  // Verifies that resetter is deleted.
}

Gennadiy Civil's avatar
 
Gennadiy Civil committed
964
// Tests that ACTION_TEMPLATES works for template template parameters.
965
966
967
968
969
970
971
972
ACTION_TEMPLATE(ReturnSmartPointer,
                HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
                                      Pointer),
                AND_1_VALUE_PARAMS(pointee)) {
  return Pointer<pointee_type>(new pointee_type(pointee));
}

TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
misterg's avatar
misterg committed
973
974
975
  const Action<std::shared_ptr<int>()> a =
      ReturnSmartPointer<std::shared_ptr>(42);
  std::shared_ptr<int> p = a.Perform(std::make_tuple());
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  EXPECT_EQ(42, *p);
}

// Tests that ACTION_TEMPLATE works for 10 template parameters.
template <typename T1, typename T2, typename T3, int k4, bool k5,
          unsigned int k6, typename T7, typename T8, typename T9>
struct GiantTemplate {
 public:
  explicit GiantTemplate(int a_value) : value(a_value) {}
  int value;
};

ACTION_TEMPLATE(ReturnGiant,
                HAS_10_TEMPLATE_PARAMS(
                    typename, T1,
                    typename, T2,
                    typename, T3,
                    int, k4,
                    bool, k5,
                    unsigned int, k6,
                    class, T7,
                    class, T8,
                    class, T9,
                    template <typename T> class, T10),
                AND_1_VALUE_PARAMS(value)) {
  return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
}

TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
misterg's avatar
misterg committed
1005
1006
1007
1008
  using Giant = GiantTemplate<std::shared_ptr<int>, bool, double, 5, true, 6,
                              char, unsigned, int>;
  const Action<Giant()> a = ReturnGiant<int, bool, double, 5, true, 6, char,
                                        unsigned, int, std::shared_ptr>(42);
Abseil Team's avatar
Abseil Team committed
1009
  Giant giant = a.Perform(std::make_tuple());
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  EXPECT_EQ(42, giant.value);
}

// Tests that ACTION_TEMPLATE works for 10 value parameters.
ACTION_TEMPLATE(ReturnSum,
                HAS_1_TEMPLATE_PARAMS(typename, Number),
                AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
  return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
}

TEST(ActionTemplateTest, WorksFor10ValueParameters) {
  const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Abseil Team's avatar
Abseil Team committed
1022
  EXPECT_EQ(55, a.Perform(std::make_tuple()));
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
}

// Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
// on the number of value parameters.

ACTION(ReturnSum) { return 0; }

ACTION_P(ReturnSum, x) { return x; }

ACTION_TEMPLATE(ReturnSum,
                HAS_1_TEMPLATE_PARAMS(typename, Number),
                AND_2_VALUE_PARAMS(v1, v2)) {
  return static_cast<Number>(v1) + v2;
}

ACTION_TEMPLATE(ReturnSum,
                HAS_1_TEMPLATE_PARAMS(typename, Number),
                AND_3_VALUE_PARAMS(v1, v2, v3)) {
  return static_cast<Number>(v1) + v2 + v3;
}

ACTION_TEMPLATE(ReturnSum,
                HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
                AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
  return static_cast<Number>(v1) + v2 + v3 + v4 + k;
}

TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
  const Action<int()> a0 = ReturnSum();
  const Action<int()> a1 = ReturnSum(1);
  const Action<int()> a2 = ReturnSum<int>(1, 2);
  const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
  const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
Abseil Team's avatar
Abseil Team committed
1056
1057
1058
1059
1060
  EXPECT_EQ(0, a0.Perform(std::make_tuple()));
  EXPECT_EQ(1, a1.Perform(std::make_tuple()));
  EXPECT_EQ(3, a2.Perform(std::make_tuple()));
  EXPECT_EQ(6, a3.Perform(std::make_tuple()));
  EXPECT_EQ(12345, a4.Perform(std::make_tuple()));
1061
1062
}

1063
#ifdef _MSC_VER
1064
# pragma warning(pop)
1065
1066
#endif

1067
1068
}  // namespace gmock_generated_actions_test
}  // namespace testing