gmock-more-actions_test.cc 24.3 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
// Google Mock - a framework for writing C++ mock classes.
//
Abseil Team's avatar
Abseil Team committed
32
// This file tests the built-in actions in gmock-actions.h.
33
34

#include <functional>
misterg's avatar
misterg committed
35
#include <memory>
36
37
#include <sstream>
#include <string>
Abseil Team's avatar
Abseil Team committed
38

Abseil Team's avatar
Abseil Team committed
39
#include "gmock/gmock-actions.h"
40
#include "gmock/gmock.h"
Abseil Team's avatar
Abseil Team committed
41
#include "gtest/gtest-spi.h"
42
#include "gtest/gtest.h"
43
44
45
46
47
48
49
50
51
52
53
54
55

namespace testing {
namespace gmock_more_actions_test {

using ::std::plus;
using ::std::string;
using testing::_;
using testing::Action;
using testing::ActionInterface;
using testing::DeleteArg;
using testing::Invoke;
using testing::Return;
using testing::ReturnArg;
56
using testing::ReturnPointee;
57
using testing::SaveArg;
zhanyong.wan's avatar
zhanyong.wan committed
58
using testing::SaveArgPointee;
59
60
61
62
63
using testing::SetArgReferee;
using testing::Unused;
using testing::WithArg;
using testing::WithoutArgs;

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

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Sample functions and functors for testing Invoke() and etc.
int Nullary() { return 1; }

class NullaryFunctor {
 public:
  int operator()() { return 2; }
};

bool g_done = false;
void VoidNullary() { g_done = true; }

class VoidNullaryFunctor {
 public:
  void operator()() { g_done = true; }
};

bool Unary(int x) { return x < 0; }

const char* Plus1(const char* s) { return s + 1; }

88
void VoidUnary(int /* n */) { g_done = true; }
89

90
bool ByConstRef(const std::string& s) { return s == "Hi"; }
91
92
93
94

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

95
std::string ByNonConstRef(std::string& s) { return s += "+"; }  // NOLINT
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

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

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

void VoidBinary(int, char) { g_done = true; }

int Ternary(int x, char y, short z) { return x + y + z; }  // NOLINT

void VoidTernary(int, char, bool) { g_done = true; }

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

int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }

void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }

115
116
117
std::string Concat4(const char* s1, const char* s2, const char* s3,
                    const char* s4) {
  return std::string(s1) + s2 + s3 + s4;
118
119
120
121
122
123
124
125
126
127
}

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;
  }
};

128
129
130
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;
131
132
133
134
135
136
137
138
139
140
141
142
}

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;
  }
};

143
144
145
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;
146
147
}

148
149
150
151
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;
152
153
}

154
155
156
157
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;
158
159
}

160
161
162
163
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;
164
165
}

166
167
168
169
170
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;
171
172
173
174
175
176
177
178
179
180
}

class Foo {
 public:
  Foo() : value_(123) {}

  int Nullary() const { return value_; }

  short Unary(long x) { return static_cast<short>(value_ + x); }  // NOLINT

181
  std::string Binary(const std::string& str, char c) const { return str + c; }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

  int Ternary(int x, bool y, char z) { return value_ + x + y*z; }

  int SumOf4(int a, int b, int c, int d) const {
    return a + b + c + d + value_;
  }

  int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }

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

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

197
198
199
200
  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;
201
202
  }

203
204
205
206
  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;
207
208
  }

209
210
211
212
  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;
213
214
  }

215
216
217
218
219
  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;
220
  }
221

222
223
224
225
226
227
228
 private:
  int value_;
};

// Tests using Invoke() with a nullary function.
TEST(InvokeTest, Nullary) {
  Action<int()> a = Invoke(Nullary);  // NOLINT
Abseil Team's avatar
Abseil Team committed
229
  EXPECT_EQ(1, a.Perform(std::make_tuple()));
230
231
232
233
234
}

// Tests using Invoke() with a unary function.
TEST(InvokeTest, Unary) {
  Action<bool(int)> a = Invoke(Unary);  // NOLINT
Abseil Team's avatar
Abseil Team committed
235
236
  EXPECT_FALSE(a.Perform(std::make_tuple(1)));
  EXPECT_TRUE(a.Perform(std::make_tuple(-1)));
237
238
239
240
241
242
}

// Tests using Invoke() with a binary function.
TEST(InvokeTest, Binary) {
  Action<const char*(const char*, short)> a = Invoke(Binary);  // NOLINT
  const char* p = "Hello";
Abseil Team's avatar
Abseil Team committed
243
  EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2))));
244
245
246
247
248
}

// Tests using Invoke() with a ternary function.
TEST(InvokeTest, Ternary) {
  Action<int(int, char, short)> a = Invoke(Ternary);  // NOLINT
Abseil Team's avatar
Abseil Team committed
249
  EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3))));
250
251
252
253
254
}

// Tests using Invoke() with a 4-argument function.
TEST(InvokeTest, FunctionThatTakes4Arguments) {
  Action<int(int, int, int, int)> a = Invoke(SumOf4);  // NOLINT
Abseil Team's avatar
Abseil Team committed
255
  EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4)));
256
257
258
259
260
}

// Tests using Invoke() with a 5-argument function.
TEST(InvokeTest, FunctionThatTakes5Arguments) {
  Action<int(int, int, int, int, int)> a = Invoke(SumOf5);  // NOLINT
Abseil Team's avatar
Abseil Team committed
261
  EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
262
263
264
265
266
}

// Tests using Invoke() with a 6-argument function.
TEST(InvokeTest, FunctionThatTakes6Arguments) {
  Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6);  // NOLINT
Abseil Team's avatar
Abseil Team committed
267
268
  EXPECT_EQ(123456,
            a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
269
270
271
272
273
274
275
276
}

// 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; }

// Tests using Invoke() with a 7-argument function.
TEST(InvokeTest, FunctionThatTakes7Arguments) {
277
278
279
  Action<std::string(const char*, const char*, const char*, const char*,
                     const char*, const char*, const char*)>
      a = Invoke(Concat7);
280
  EXPECT_EQ("1234567",
Abseil Team's avatar
Abseil Team committed
281
282
283
            a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
                                      CharPtr("4"), CharPtr("5"), CharPtr("6"),
                                      CharPtr("7"))));
284
285
286
287
}

// Tests using Invoke() with a 8-argument function.
TEST(InvokeTest, FunctionThatTakes8Arguments) {
288
289
290
  Action<std::string(const char*, const char*, const char*, const char*,
                     const char*, const char*, const char*, const char*)>
      a = Invoke(Concat8);
291
  EXPECT_EQ("12345678",
Abseil Team's avatar
Abseil Team committed
292
293
294
            a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
                                      CharPtr("4"), CharPtr("5"), CharPtr("6"),
                                      CharPtr("7"), CharPtr("8"))));
295
296
297
298
}

// Tests using Invoke() with a 9-argument function.
TEST(InvokeTest, FunctionThatTakes9Arguments) {
299
300
301
302
  Action<std::string(const char*, const char*, const char*, const char*,
                     const char*, const char*, const char*, const char*,
                     const char*)>
      a = Invoke(Concat9);
Abseil Team's avatar
Abseil Team committed
303
304
305
306
  EXPECT_EQ("123456789", a.Perform(std::make_tuple(
                             CharPtr("1"), CharPtr("2"), CharPtr("3"),
                             CharPtr("4"), CharPtr("5"), CharPtr("6"),
                             CharPtr("7"), CharPtr("8"), CharPtr("9"))));
307
308
309
310
}

// Tests using Invoke() with a 10-argument function.
TEST(InvokeTest, FunctionThatTakes10Arguments) {
311
312
313
314
  Action<std::string(const char*, const char*, const char*, const char*,
                     const char*, const char*, const char*, const char*,
                     const char*, const char*)>
      a = Invoke(Concat10);
315
  EXPECT_EQ("1234567890",
Abseil Team's avatar
Abseil Team committed
316
317
318
319
            a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
                                      CharPtr("4"), CharPtr("5"), CharPtr("6"),
                                      CharPtr("7"), CharPtr("8"), CharPtr("9"),
                                      CharPtr("0"))));
320
321
322
323
}

// Tests using Invoke() with functions with parameters declared as Unused.
TEST(InvokeTest, FunctionWithUnusedParameters) {
Gennadiy Civil's avatar
 
Gennadiy Civil committed
324
  Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
Abseil Team's avatar
Abseil Team committed
325
326
  std::tuple<int, int, double, std::string> dummy =
      std::make_tuple(10, 2, 5.6, std::string("hi"));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
327
  EXPECT_EQ(12, a1.Perform(dummy));
328
329
330

  Action<int(int, int, bool, int*)> a2 =
      Invoke(SumOfFirst2);
Abseil Team's avatar
Abseil Team committed
331
332
  EXPECT_EQ(
      23, a2.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
333
334
335
336
337
}

// Tests using Invoke() with methods with parameters declared as Unused.
TEST(InvokeTest, MethodWithUnusedParameters) {
  Foo foo;
338
  Action<int(std::string, bool, int, int)> a1 = Invoke(&foo, &Foo::SumOfLast2);
Abseil Team's avatar
Abseil Team committed
339
  EXPECT_EQ(12, a1.Perform(std::make_tuple(CharPtr("hi"), true, 10, 2)));
340
341
342

  Action<int(char, double, int, int)> a2 =
      Invoke(&foo, &Foo::SumOfLast2);
Abseil Team's avatar
Abseil Team committed
343
  EXPECT_EQ(23, a2.Perform(std::make_tuple('a', 2.5, 20, 3)));
344
345
346
347
}

// Tests using Invoke() with a functor.
TEST(InvokeTest, Functor) {
348
  Action<long(long, int)> a = Invoke(plus<long>());  // NOLINT
Abseil Team's avatar
Abseil Team committed
349
  EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2)));
350
351
352
353
354
}

// Tests using Invoke(f) as an action of a compatible type.
TEST(InvokeTest, FunctionWithCompatibleType) {
  Action<long(int, short, char, bool)> a = Invoke(SumOf4);  // NOLINT
Abseil Team's avatar
Abseil Team committed
355
  EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
356
357
358
359
360
361
362
363
}

// Tests using Invoke() with an object pointer and a method pointer.

// Tests using Invoke() with a nullary method.
TEST(InvokeMethodTest, Nullary) {
  Foo foo;
  Action<int()> a = Invoke(&foo, &Foo::Nullary);  // NOLINT
Abseil Team's avatar
Abseil Team committed
364
  EXPECT_EQ(123, a.Perform(std::make_tuple()));
365
366
367
368
369
370
}

// Tests using Invoke() with a unary method.
TEST(InvokeMethodTest, Unary) {
  Foo foo;
  Action<short(long)> a = Invoke(&foo, &Foo::Unary);  // NOLINT
Abseil Team's avatar
Abseil Team committed
371
  EXPECT_EQ(4123, a.Perform(std::make_tuple(4000)));
372
373
374
375
376
}

// Tests using Invoke() with a binary method.
TEST(InvokeMethodTest, Binary) {
  Foo foo;
Gennadiy Civil's avatar
 
Gennadiy Civil committed
377
378
  Action<std::string(const std::string&, char)> a = Invoke(&foo, &Foo::Binary);
  std::string s("Hell");
Abseil Team's avatar
Abseil Team committed
379
  std::tuple<std::string, char> dummy = std::make_tuple(s, 'o');
Gennadiy Civil's avatar
 
Gennadiy Civil committed
380
  EXPECT_EQ("Hello", a.Perform(dummy));
381
382
383
384
385
386
}

// Tests using Invoke() with a ternary method.
TEST(InvokeMethodTest, Ternary) {
  Foo foo;
  Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary);  // NOLINT
Abseil Team's avatar
Abseil Team committed
387
  EXPECT_EQ(1124, a.Perform(std::make_tuple(1000, true, Char(1))));
388
389
390
391
392
393
}

// Tests using Invoke() with a 4-argument method.
TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
  Foo foo;
  Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4);  // NOLINT
Abseil Team's avatar
Abseil Team committed
394
  EXPECT_EQ(1357, a.Perform(std::make_tuple(1000, 200, 30, 4)));
395
396
397
398
399
400
}

// Tests using Invoke() with a 5-argument method.
TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
  Foo foo;
  Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5);  // NOLINT
Abseil Team's avatar
Abseil Team committed
401
  EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
402
403
404
405
406
407
408
}

// Tests using Invoke() with a 6-argument method.
TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
  Foo foo;
  Action<int(int, int, int, int, int, int)> a =  // NOLINT
      Invoke(&foo, &Foo::SumOf6);
Abseil Team's avatar
Abseil Team committed
409
410
  EXPECT_EQ(123456,
            a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
411
412
413
414
415
}

// Tests using Invoke() with a 7-argument method.
TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
  Foo foo;
416
417
418
  Action<std::string(const char*, const char*, const char*, const char*,
                     const char*, const char*, const char*)>
      a = Invoke(&foo, &Foo::Concat7);
419
  EXPECT_EQ("1234567",
Abseil Team's avatar
Abseil Team committed
420
421
422
            a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
                                      CharPtr("4"), CharPtr("5"), CharPtr("6"),
                                      CharPtr("7"))));
423
424
425
426
427
}

// Tests using Invoke() with a 8-argument method.
TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
  Foo foo;
428
429
430
  Action<std::string(const char*, const char*, const char*, const char*,
                     const char*, const char*, const char*, const char*)>
      a = Invoke(&foo, &Foo::Concat8);
431
  EXPECT_EQ("12345678",
Abseil Team's avatar
Abseil Team committed
432
433
434
            a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
                                      CharPtr("4"), CharPtr("5"), CharPtr("6"),
                                      CharPtr("7"), CharPtr("8"))));
435
436
437
438
439
}

// Tests using Invoke() with a 9-argument method.
TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
  Foo foo;
440
441
442
443
  Action<std::string(const char*, const char*, const char*, const char*,
                     const char*, const char*, const char*, const char*,
                     const char*)>
      a = Invoke(&foo, &Foo::Concat9);
Abseil Team's avatar
Abseil Team committed
444
445
446
447
  EXPECT_EQ("123456789", a.Perform(std::make_tuple(
                             CharPtr("1"), CharPtr("2"), CharPtr("3"),
                             CharPtr("4"), CharPtr("5"), CharPtr("6"),
                             CharPtr("7"), CharPtr("8"), CharPtr("9"))));
448
449
450
451
452
}

// Tests using Invoke() with a 10-argument method.
TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
  Foo foo;
453
454
455
456
  Action<std::string(const char*, const char*, const char*, const char*,
                     const char*, const char*, const char*, const char*,
                     const char*, const char*)>
      a = Invoke(&foo, &Foo::Concat10);
457
  EXPECT_EQ("1234567890",
Abseil Team's avatar
Abseil Team committed
458
459
460
461
            a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
                                      CharPtr("4"), CharPtr("5"), CharPtr("6"),
                                      CharPtr("7"), CharPtr("8"), CharPtr("9"),
                                      CharPtr("0"))));
462
463
464
465
466
467
468
}

// Tests using Invoke(f) as an action of a compatible type.
TEST(InvokeMethodTest, MethodWithCompatibleType) {
  Foo foo;
  Action<long(int, short, char, bool)> a =  // NOLINT
      Invoke(&foo, &Foo::SumOf4);
Abseil Team's avatar
Abseil Team committed
469
  EXPECT_EQ(4444, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
470
471
472
473
474
}

// Tests using WithoutArgs with an action that takes no argument.
TEST(WithoutArgsTest, NoArg) {
  Action<int(int n)> a = WithoutArgs(Invoke(Nullary));  // NOLINT
Abseil Team's avatar
Abseil Team committed
475
  EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
476
477
478
479
480
}

// Tests using WithArg with an action that takes 1 argument.
TEST(WithArgTest, OneArg) {
  Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary));  // NOLINT
Abseil Team's avatar
Abseil Team committed
481
482
  EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1)));
  EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1)));
483
484
485
486
}

TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
  const Action<int(int)> a = ReturnArg<0>();
Abseil Team's avatar
Abseil Team committed
487
  EXPECT_EQ(5, a.Perform(std::make_tuple(5)));
488
489
490
491
}

TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
  const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
Abseil Team's avatar
Abseil Team committed
492
  EXPECT_TRUE(a.Perform(std::make_tuple(true, false, false)));
493
494
495
}

TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
496
  const Action<std::string(int, int, std::string, int)> a = ReturnArg<2>();
Abseil Team's avatar
Abseil Team committed
497
  EXPECT_EQ("seven", a.Perform(std::make_tuple(5, 6, std::string("seven"), 8)));
498
499
500
501
502
}

TEST(SaveArgActionTest, WorksForSameType) {
  int result = 0;
  const Action<void(int n)> a1 = SaveArg<0>(&result);
Abseil Team's avatar
Abseil Team committed
503
  a1.Perform(std::make_tuple(5));
504
505
506
507
508
509
  EXPECT_EQ(5, result);
}

TEST(SaveArgActionTest, WorksForCompatibleType) {
  int result = 0;
  const Action<void(bool, char)> a1 = SaveArg<1>(&result);
Abseil Team's avatar
Abseil Team committed
510
  a1.Perform(std::make_tuple(true, 'a'));
511
512
513
  EXPECT_EQ('a', result);
}

zhanyong.wan's avatar
zhanyong.wan committed
514
515
516
517
TEST(SaveArgPointeeActionTest, WorksForSameType) {
  int result = 0;
  const int value = 5;
  const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
Abseil Team's avatar
Abseil Team committed
518
  a1.Perform(std::make_tuple(&value));
zhanyong.wan's avatar
zhanyong.wan committed
519
520
521
522
523
524
525
  EXPECT_EQ(5, result);
}

TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
  int result = 0;
  char value = 'a';
  const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
Abseil Team's avatar
Abseil Team committed
526
  a1.Perform(std::make_tuple(true, &value));
zhanyong.wan's avatar
zhanyong.wan committed
527
528
529
  EXPECT_EQ('a', result);
}

530
531
532
TEST(SetArgRefereeActionTest, WorksForSameType) {
  int value = 0;
  const Action<void(int&)> a1 = SetArgReferee<0>(1);
Abseil Team's avatar
Abseil Team committed
533
  a1.Perform(std::tuple<int&>(value));
534
535
536
537
538
539
  EXPECT_EQ(1, value);
}

TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
  int value = 0;
  const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
Abseil Team's avatar
Abseil Team committed
540
  a1.Perform(std::tuple<int, int&>(0, value));
541
542
543
544
545
546
  EXPECT_EQ('a', value);
}

TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
  int value = 0;
  const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
Abseil Team's avatar
Abseil Team committed
547
  a1.Perform(std::tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
  EXPECT_EQ('a', value);
}

// A class that can be used to verify that its destructor is called: it will set
// the bool provided to the constructor to true when destroyed.
class DeletionTester {
 public:
  explicit DeletionTester(bool* is_deleted)
    : is_deleted_(is_deleted) {
    // Make sure the bit is set to false.
    *is_deleted_ = false;
  }

  ~DeletionTester() {
    *is_deleted_ = true;
  }

 private:
  bool* is_deleted_;
};

TEST(DeleteArgActionTest, OneArg) {
  bool is_deleted = false;
  DeletionTester* t = new DeletionTester(&is_deleted);
  const Action<void(DeletionTester*)> a1 = DeleteArg<0>();      // NOLINT
  EXPECT_FALSE(is_deleted);
Abseil Team's avatar
Abseil Team committed
574
  a1.Perform(std::make_tuple(t));
575
576
577
578
579
580
581
582
583
  EXPECT_TRUE(is_deleted);
}

TEST(DeleteArgActionTest, TenArgs) {
  bool is_deleted = false;
  DeletionTester* t = new DeletionTester(&is_deleted);
  const Action<void(bool, int, int, const char*, bool,
                    int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>();
  EXPECT_FALSE(is_deleted);
Abseil Team's avatar
Abseil Team committed
584
  a1.Perform(std::make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
585
586
587
588
589
590
591
  EXPECT_TRUE(is_deleted);
}

#if GTEST_HAS_EXCEPTIONS

TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
  const Action<void(int n)> a = Throw('a');
Abseil Team's avatar
Abseil Team committed
592
  EXPECT_THROW(a.Perform(std::make_tuple(0)), char);
593
594
595
596
597
598
}

class MyException {};

TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
  const Action<double(char ch)> a = Throw(MyException());
Abseil Team's avatar
Abseil Team committed
599
  EXPECT_THROW(a.Perform(std::make_tuple('0')), MyException);
600
601
602
603
}

TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
  const Action<double()> a = Throw(MyException());
Abseil Team's avatar
Abseil Team committed
604
  EXPECT_THROW(a.Perform(std::make_tuple()), MyException);
605
606
}

Abseil Team's avatar
Abseil Team committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
class Object {
 public:
  virtual ~Object() {}
  virtual void Func() {}
};

class MockObject : public Object {
 public:
  ~MockObject() override {}
  MOCK_METHOD(void, Func, (), (override));
};

TEST(ThrowActionTest, Times0) {
  EXPECT_NONFATAL_FAILURE(
      [] {
        try {
          MockObject m;
          ON_CALL(m, Func()).WillByDefault([] { throw "something"; });
          EXPECT_CALL(m, Func()).Times(0);
          m.Func();
        } catch (...) {
          // Exception is caught but Times(0) still triggers a failure.
        }
      }(),
      "");
}

634
635
636
637
638
639
640
641
642
643
644
645
646
#endif  // GTEST_HAS_EXCEPTIONS

// Tests that SetArrayArgument<N>(first, last) sets the elements of the array
// pointed to by the N-th (0-based) argument to values in range [first, last).
TEST(SetArrayArgumentTest, SetsTheNthArray) {
  typedef void MyFunction(bool, int*, char*);
  int numbers[] = { 1, 2, 3 };
  Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);

  int n[4] = {};
  int* pn = n;
  char ch[4] = {};
  char* pch = ch;
Abseil Team's avatar
Abseil Team committed
647
  a.Perform(std::make_tuple(true, pn, pch));
648
649
650
651
652
653
654
655
656
657
658
659
660
661
  EXPECT_EQ(1, n[0]);
  EXPECT_EQ(2, n[1]);
  EXPECT_EQ(3, n[2]);
  EXPECT_EQ(0, n[3]);
  EXPECT_EQ('\0', ch[0]);
  EXPECT_EQ('\0', ch[1]);
  EXPECT_EQ('\0', ch[2]);
  EXPECT_EQ('\0', ch[3]);

  // Tests first and last are iterators.
  std::string letters = "abc";
  a = SetArrayArgument<2>(letters.begin(), letters.end());
  std::fill_n(n, 4, 0);
  std::fill_n(ch, 4, '\0');
Abseil Team's avatar
Abseil Team committed
662
  a.Perform(std::make_tuple(true, pn, pch));
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
  EXPECT_EQ(0, n[0]);
  EXPECT_EQ(0, n[1]);
  EXPECT_EQ(0, n[2]);
  EXPECT_EQ(0, n[3]);
  EXPECT_EQ('a', ch[0]);
  EXPECT_EQ('b', ch[1]);
  EXPECT_EQ('c', ch[2]);
  EXPECT_EQ('\0', ch[3]);
}

// Tests SetArrayArgument<N>(first, last) where first == last.
TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
  typedef void MyFunction(bool, int*);
  int numbers[] = { 1, 2, 3 };
  Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);

  int n[4] = {};
  int* pn = n;
Abseil Team's avatar
Abseil Team committed
681
  a.Perform(std::make_tuple(true, pn));
682
683
684
685
686
687
688
689
690
  EXPECT_EQ(0, n[0]);
  EXPECT_EQ(0, n[1]);
  EXPECT_EQ(0, n[2]);
  EXPECT_EQ(0, n[3]);
}

// Tests SetArrayArgument<N>(first, last) where *first is convertible
// (but not equal) to the argument type.
TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
691
692
693
694
695
696
  typedef void MyFunction(bool, int*);
  char chars[] = { 97, 98, 99 };
  Action<MyFunction> a = SetArrayArgument<1>(chars, chars + 3);

  int codes[4] = { 111, 222, 333, 444 };
  int* pcodes = codes;
Abseil Team's avatar
Abseil Team committed
697
  a.Perform(std::make_tuple(true, pcodes));
698
699
700
701
  EXPECT_EQ(97, codes[0]);
  EXPECT_EQ(98, codes[1]);
  EXPECT_EQ(99, codes[2]);
  EXPECT_EQ(444, codes[3]);
702
703
704
705
706
707
708
709
710
}

// Test SetArrayArgument<N>(first, last) with iterator as argument.
TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
  typedef void MyFunction(bool, std::back_insert_iterator<std::string>);
  std::string letters = "abc";
  Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());

  std::string s;
Abseil Team's avatar
Abseil Team committed
711
  a.Perform(std::make_tuple(true, back_inserter(s)));
712
713
714
  EXPECT_EQ(letters, s);
}

715
716
717
TEST(ReturnPointeeTest, Works) {
  int n = 42;
  const Action<int()> a = ReturnPointee(&n);
Abseil Team's avatar
Abseil Team committed
718
  EXPECT_EQ(42, a.Perform(std::make_tuple()));
719
720

  n = 43;
Abseil Team's avatar
Abseil Team committed
721
  EXPECT_EQ(43, a.Perform(std::make_tuple()));
722
723
}

724
725
}  // namespace gmock_generated_actions_test
}  // namespace testing