Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/googletest/googlemock/test/gmock-more-actions_test.cc
110032 views
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
// Google Mock - a framework for writing C++ mock classes.
31
//
32
// This file tests the built-in actions in gmock-actions.h.
33
34
#include "gmock/gmock-more-actions.h"
35
36
#include <algorithm>
37
#include <functional>
38
#include <iterator>
39
#include <memory>
40
#include <sstream>
41
#include <string>
42
#include <tuple>
43
#include <vector>
44
45
#include "gmock/gmock.h"
46
#include "gtest/gtest-spi.h"
47
#include "gtest/gtest.h"
48
49
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4577)
50
51
namespace testing {
52
namespace gmock_more_actions_test {
53
54
using ::std::plus;
55
using ::std::string;
56
using testing::Action;
57
using testing::DeleteArg;
58
using testing::Invoke;
59
using testing::ReturnArg;
60
using testing::ReturnPointee;
61
using testing::SaveArg;
62
using testing::SaveArgByMove;
63
using testing::SaveArgPointee;
64
using testing::SetArgReferee;
65
using testing::Unused;
66
using testing::WithArg;
67
using testing::WithoutArgs;
68
69
// For suppressing compiler warnings on conversion possibly losing precision.
70
inline short Short(short n) { return n; } // NOLINT
71
inline char Char(char ch) { return ch; }
72
73
// Sample functions and functors for testing Invoke() and etc.
74
int Nullary() { return 1; }
75
76
bool g_done = false;
77
78
bool Unary(int x) { return x < 0; }
79
80
bool ByConstRef(const std::string& s) { return s == "Hi"; }
81
82
const double g_double = 0;
83
bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
84
85
struct UnaryFunctor {
86
int operator()(bool x) { return x ? 1 : -1; }
87
};
88
89
struct UnaryMoveOnlyFunctor : UnaryFunctor {
90
UnaryMoveOnlyFunctor() = default;
91
UnaryMoveOnlyFunctor(const UnaryMoveOnlyFunctor&) = delete;
92
UnaryMoveOnlyFunctor(UnaryMoveOnlyFunctor&&) = default;
93
};
94
95
struct OneShotUnaryFunctor {
96
int operator()(bool x) && { return x ? 1 : -1; }
97
};
98
99
const char* Binary(const char* input, short n) { return input + n; } // NOLINT
100
101
int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
102
103
int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
104
105
int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
106
107
int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
108
109
struct SumOf5Functor {
110
int operator()(int a, int b, int c, int d, int e) {
111
return a + b + c + d + e;
112
}
113
};
114
115
int SumOf6(int a, int b, int c, int d, int e, int f) {
116
return a + b + c + d + e + f;
117
}
118
119
struct SumOf6Functor {
120
int operator()(int a, int b, int c, int d, int e, int f) {
121
return a + b + c + d + e + f;
122
}
123
};
124
125
std::string Concat7(const char* s1, const char* s2, const char* s3,
126
const char* s4, const char* s5, const char* s6,
127
const char* s7) {
128
return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
129
}
130
131
std::string Concat8(const char* s1, const char* s2, const char* s3,
132
const char* s4, const char* s5, const char* s6,
133
const char* s7, const char* s8) {
134
return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
135
}
136
137
std::string Concat9(const char* s1, const char* s2, const char* s3,
138
const char* s4, const char* s5, const char* s6,
139
const char* s7, const char* s8, const char* s9) {
140
return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
141
}
142
143
std::string Concat10(const char* s1, const char* s2, const char* s3,
144
const char* s4, const char* s5, const char* s6,
145
const char* s7, const char* s8, const char* s9,
146
const char* s10) {
147
return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
148
}
149
150
class Foo {
151
public:
152
Foo() : value_(123) {}
153
154
int Nullary() const { return value_; }
155
156
short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
157
158
std::string Binary(const std::string& str, char c) const { return str + c; }
159
160
int Ternary(int x, bool y, char z) { return value_ + x + y * z; }
161
162
int SumOf4(int a, int b, int c, int d) const {
163
return a + b + c + d + value_;
164
}
165
166
int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
167
168
int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
169
170
int SumOf6(int a, int b, int c, int d, int e, int f) {
171
return a + b + c + d + e + f;
172
}
173
174
std::string Concat7(const char* s1, const char* s2, const char* s3,
175
const char* s4, const char* s5, const char* s6,
176
const char* s7) {
177
return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
178
}
179
180
std::string Concat8(const char* s1, const char* s2, const char* s3,
181
const char* s4, const char* s5, const char* s6,
182
const char* s7, const char* s8) {
183
return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
184
}
185
186
std::string Concat9(const char* s1, const char* s2, const char* s3,
187
const char* s4, const char* s5, const char* s6,
188
const char* s7, const char* s8, const char* s9) {
189
return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
190
}
191
192
std::string Concat10(const char* s1, const char* s2, const char* s3,
193
const char* s4, const char* s5, const char* s6,
194
const char* s7, const char* s8, const char* s9,
195
const char* s10) {
196
return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
197
}
198
199
private:
200
int value_;
201
};
202
203
// Tests using Invoke() with a nullary function.
204
TEST(InvokeTest, Nullary) {
205
Action<int()> a = Invoke(Nullary); // NOLINT
206
EXPECT_EQ(1, a.Perform(std::make_tuple()));
207
}
208
209
// Tests using Invoke() with a unary function.
210
TEST(InvokeTest, Unary) {
211
Action<bool(int)> a = Invoke(Unary); // NOLINT
212
EXPECT_FALSE(a.Perform(std::make_tuple(1)));
213
EXPECT_TRUE(a.Perform(std::make_tuple(-1)));
214
}
215
216
// Tests using Invoke() with a binary function.
217
TEST(InvokeTest, Binary) {
218
Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
219
const char* p = "Hello";
220
EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2))));
221
}
222
223
// Tests using Invoke() with a ternary function.
224
TEST(InvokeTest, Ternary) {
225
Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
226
EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3))));
227
}
228
229
// Tests using Invoke() with a 4-argument function.
230
TEST(InvokeTest, FunctionThatTakes4Arguments) {
231
Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
232
EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4)));
233
}
234
235
// Tests using Invoke() with a 5-argument function.
236
TEST(InvokeTest, FunctionThatTakes5Arguments) {
237
Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
238
EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
239
}
240
241
// Tests using Invoke() with a 6-argument function.
242
TEST(InvokeTest, FunctionThatTakes6Arguments) {
243
Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
244
EXPECT_EQ(123456,
245
a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
246
}
247
248
// A helper that turns the type of a C-string literal from const
249
// char[N] to const char*.
250
inline const char* CharPtr(const char* s) { return s; }
251
252
// Tests using Invoke() with a 7-argument function.
253
TEST(InvokeTest, FunctionThatTakes7Arguments) {
254
Action<std::string(const char*, const char*, const char*, const char*,
255
const char*, const char*, const char*)>
256
a = Invoke(Concat7);
257
EXPECT_EQ("1234567",
258
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
259
CharPtr("4"), CharPtr("5"), CharPtr("6"),
260
CharPtr("7"))));
261
}
262
263
// Tests using Invoke() with a 8-argument function.
264
TEST(InvokeTest, FunctionThatTakes8Arguments) {
265
Action<std::string(const char*, const char*, const char*, const char*,
266
const char*, const char*, const char*, const char*)>
267
a = Invoke(Concat8);
268
EXPECT_EQ("12345678",
269
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
270
CharPtr("4"), CharPtr("5"), CharPtr("6"),
271
CharPtr("7"), CharPtr("8"))));
272
}
273
274
// Tests using Invoke() with a 9-argument function.
275
TEST(InvokeTest, FunctionThatTakes9Arguments) {
276
Action<std::string(const char*, const char*, const char*, const char*,
277
const char*, const char*, const char*, const char*,
278
const char*)>
279
a = Invoke(Concat9);
280
EXPECT_EQ("123456789", a.Perform(std::make_tuple(
281
CharPtr("1"), CharPtr("2"), CharPtr("3"),
282
CharPtr("4"), CharPtr("5"), CharPtr("6"),
283
CharPtr("7"), CharPtr("8"), CharPtr("9"))));
284
}
285
286
// Tests using Invoke() with a 10-argument function.
287
TEST(InvokeTest, FunctionThatTakes10Arguments) {
288
Action<std::string(const char*, const char*, const char*, const char*,
289
const char*, const char*, const char*, const char*,
290
const char*, const char*)>
291
a = Invoke(Concat10);
292
EXPECT_EQ("1234567890",
293
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
294
CharPtr("4"), CharPtr("5"), CharPtr("6"),
295
CharPtr("7"), CharPtr("8"), CharPtr("9"),
296
CharPtr("0"))));
297
}
298
299
// Tests using Invoke() with functions with parameters declared as Unused.
300
TEST(InvokeTest, FunctionWithUnusedParameters) {
301
Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
302
std::tuple<int, int, double, std::string> dummy =
303
std::make_tuple(10, 2, 5.6, std::string("hi"));
304
EXPECT_EQ(12, a1.Perform(dummy));
305
306
Action<int(int, int, bool, int*)> a2 = Invoke(SumOfFirst2);
307
EXPECT_EQ(
308
23, a2.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
309
}
310
311
// Tests using Invoke() with methods with parameters declared as Unused.
312
TEST(InvokeTest, MethodWithUnusedParameters) {
313
Foo foo;
314
Action<int(std::string, bool, int, int)> a1 = Invoke(&foo, &Foo::SumOfLast2);
315
EXPECT_EQ(12, a1.Perform(std::make_tuple(CharPtr("hi"), true, 10, 2)));
316
317
Action<int(char, double, int, int)> a2 = Invoke(&foo, &Foo::SumOfLast2);
318
EXPECT_EQ(23, a2.Perform(std::make_tuple('a', 2.5, 20, 3)));
319
}
320
321
// Tests using Invoke() with a functor.
322
TEST(InvokeTest, Functor) {
323
Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT
324
EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2)));
325
}
326
327
// Tests using Invoke(f) as an action of a compatible type.
328
TEST(InvokeTest, FunctionWithCompatibleType) {
329
Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
330
EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
331
}
332
333
// Tests using Invoke() with an object pointer and a method pointer.
334
335
// Tests using Invoke() with a nullary method.
336
TEST(InvokeMethodTest, Nullary) {
337
Foo foo;
338
Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT
339
EXPECT_EQ(123, a.Perform(std::make_tuple()));
340
}
341
342
// Tests using Invoke() with a unary method.
343
TEST(InvokeMethodTest, Unary) {
344
Foo foo;
345
Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT
346
EXPECT_EQ(4123, a.Perform(std::make_tuple(4000)));
347
}
348
349
// Tests using Invoke() with a binary method.
350
TEST(InvokeMethodTest, Binary) {
351
Foo foo;
352
Action<std::string(const std::string&, char)> a = Invoke(&foo, &Foo::Binary);
353
std::string s("Hell");
354
std::tuple<std::string, char> dummy = std::make_tuple(s, 'o');
355
EXPECT_EQ("Hello", a.Perform(dummy));
356
}
357
358
// Tests using Invoke() with a ternary method.
359
TEST(InvokeMethodTest, Ternary) {
360
Foo foo;
361
Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT
362
EXPECT_EQ(1124, a.Perform(std::make_tuple(1000, true, Char(1))));
363
}
364
365
// Tests using Invoke() with a 4-argument method.
366
TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
367
Foo foo;
368
Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT
369
EXPECT_EQ(1357, a.Perform(std::make_tuple(1000, 200, 30, 4)));
370
}
371
372
// Tests using Invoke() with a 5-argument method.
373
TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
374
Foo foo;
375
Action<int(int, int, int, int, int)> a =
376
Invoke(&foo, &Foo::SumOf5); // NOLINT
377
EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
378
}
379
380
// Tests using Invoke() with a 6-argument method.
381
TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
382
Foo foo;
383
Action<int(int, int, int, int, int, int)> a = // NOLINT
384
Invoke(&foo, &Foo::SumOf6);
385
EXPECT_EQ(123456,
386
a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
387
}
388
389
// Tests using Invoke() with a 7-argument method.
390
TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
391
Foo foo;
392
Action<std::string(const char*, const char*, const char*, const char*,
393
const char*, const char*, const char*)>
394
a = Invoke(&foo, &Foo::Concat7);
395
EXPECT_EQ("1234567",
396
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
397
CharPtr("4"), CharPtr("5"), CharPtr("6"),
398
CharPtr("7"))));
399
}
400
401
// Tests using Invoke() with a 8-argument method.
402
TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
403
Foo foo;
404
Action<std::string(const char*, const char*, const char*, const char*,
405
const char*, const char*, const char*, const char*)>
406
a = Invoke(&foo, &Foo::Concat8);
407
EXPECT_EQ("12345678",
408
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
409
CharPtr("4"), CharPtr("5"), CharPtr("6"),
410
CharPtr("7"), CharPtr("8"))));
411
}
412
413
// Tests using Invoke() with a 9-argument method.
414
TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
415
Foo foo;
416
Action<std::string(const char*, const char*, const char*, const char*,
417
const char*, const char*, const char*, const char*,
418
const char*)>
419
a = Invoke(&foo, &Foo::Concat9);
420
EXPECT_EQ("123456789", a.Perform(std::make_tuple(
421
CharPtr("1"), CharPtr("2"), CharPtr("3"),
422
CharPtr("4"), CharPtr("5"), CharPtr("6"),
423
CharPtr("7"), CharPtr("8"), CharPtr("9"))));
424
}
425
426
// Tests using Invoke() with a 10-argument method.
427
TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
428
Foo foo;
429
Action<std::string(const char*, const char*, const char*, const char*,
430
const char*, const char*, const char*, const char*,
431
const char*, const char*)>
432
a = Invoke(&foo, &Foo::Concat10);
433
EXPECT_EQ("1234567890",
434
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
435
CharPtr("4"), CharPtr("5"), CharPtr("6"),
436
CharPtr("7"), CharPtr("8"), CharPtr("9"),
437
CharPtr("0"))));
438
}
439
440
// Tests using Invoke(f) as an action of a compatible type.
441
TEST(InvokeMethodTest, MethodWithCompatibleType) {
442
Foo foo;
443
Action<long(int, short, char, bool)> a = // NOLINT
444
Invoke(&foo, &Foo::SumOf4);
445
EXPECT_EQ(4444, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
446
}
447
448
// Tests using WithoutArgs with an action that takes no argument.
449
TEST(WithoutArgsTest, NoArg) {
450
Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
451
EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
452
}
453
454
// Tests using WithArg with an action that takes 1 argument.
455
TEST(WithArgTest, OneArg) {
456
Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
457
EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1)));
458
EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1)));
459
}
460
461
TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
462
const Action<int(int)> a = ReturnArg<0>();
463
EXPECT_EQ(5, a.Perform(std::make_tuple(5)));
464
}
465
466
TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
467
const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
468
EXPECT_TRUE(a.Perform(std::make_tuple(true, false, false)));
469
}
470
471
TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
472
const Action<std::string(int, int, std::string, int)> a = ReturnArg<2>();
473
EXPECT_EQ("seven", a.Perform(std::make_tuple(5, 6, std::string("seven"), 8)));
474
}
475
476
TEST(ReturnArgActionTest, WorksForNonConstRefArg0) {
477
const Action<std::string&(std::string&)> a = ReturnArg<0>();
478
std::string s = "12345";
479
EXPECT_EQ(&s, &a.Perform(std::forward_as_tuple(s)));
480
}
481
482
TEST(SaveArgActionTest, WorksForSameType) {
483
int result = 0;
484
const Action<void(int n)> a1 = SaveArg<0>(&result);
485
a1.Perform(std::make_tuple(5));
486
EXPECT_EQ(5, result);
487
}
488
489
TEST(SaveArgActionTest, WorksForCompatibleType) {
490
int result = 0;
491
const Action<void(bool, char)> a1 = SaveArg<1>(&result);
492
a1.Perform(std::make_tuple(true, 'a'));
493
EXPECT_EQ('a', result);
494
}
495
496
struct MoveOnly {
497
explicit MoveOnly(int v) : i(v) {}
498
MoveOnly(MoveOnly&& o) {
499
i = o.i;
500
o.i = -1;
501
}
502
MoveOnly& operator=(MoveOnly&& o) {
503
i = o.i;
504
o.i = -1;
505
return *this;
506
}
507
int i;
508
};
509
510
TEST(SaveArgByMoveActionTest, WorksForSameType) {
511
MoveOnly result{0};
512
const Action<void(MoveOnly v)> a1 = SaveArgByMove<0>(&result);
513
a1.Perform(std::make_tuple(MoveOnly{5}));
514
EXPECT_EQ(5, result.i);
515
}
516
517
TEST(SaveArgByMoveActionTest, WorksForCompatibleType) {
518
MoveOnly result{0};
519
const Action<void(bool, MoveOnly)> a1 = SaveArgByMove<1>(&result);
520
a1.Perform(std::make_tuple(true, MoveOnly{7}));
521
EXPECT_EQ(7, result.i);
522
}
523
524
TEST(SaveArgPointeeActionTest, WorksForSameType) {
525
int result = 0;
526
const int value = 5;
527
const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
528
a1.Perform(std::make_tuple(&value));
529
EXPECT_EQ(5, result);
530
}
531
532
TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
533
int result = 0;
534
char value = 'a';
535
const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
536
a1.Perform(std::make_tuple(true, &value));
537
EXPECT_EQ('a', result);
538
}
539
540
TEST(SetArgRefereeActionTest, WorksForSameType) {
541
int value = 0;
542
const Action<void(int&)> a1 = SetArgReferee<0>(1);
543
a1.Perform(std::tuple<int&>(value));
544
EXPECT_EQ(1, value);
545
}
546
547
TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
548
int value = 0;
549
const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
550
a1.Perform(std::tuple<int, int&>(0, value));
551
EXPECT_EQ('a', value);
552
}
553
554
TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
555
int value = 0;
556
const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
557
a1.Perform(std::tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
558
EXPECT_EQ('a', value);
559
}
560
561
// A class that can be used to verify that its destructor is called: it will set
562
// the bool provided to the constructor to true when destroyed.
563
class DeletionTester {
564
public:
565
explicit DeletionTester(bool* is_deleted) : is_deleted_(is_deleted) {
566
// Make sure the bit is set to false.
567
*is_deleted_ = false;
568
}
569
570
~DeletionTester() { *is_deleted_ = true; }
571
572
private:
573
bool* is_deleted_;
574
};
575
576
TEST(DeleteArgActionTest, OneArg) {
577
bool is_deleted = false;
578
DeletionTester* t = new DeletionTester(&is_deleted);
579
const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT
580
EXPECT_FALSE(is_deleted);
581
a1.Perform(std::make_tuple(t));
582
EXPECT_TRUE(is_deleted);
583
}
584
585
TEST(DeleteArgActionTest, TenArgs) {
586
bool is_deleted = false;
587
DeletionTester* t = new DeletionTester(&is_deleted);
588
const Action<void(bool, int, int, const char*, bool, int, int, int, int,
589
DeletionTester*)>
590
a1 = DeleteArg<9>();
591
EXPECT_FALSE(is_deleted);
592
a1.Perform(std::make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
593
EXPECT_TRUE(is_deleted);
594
}
595
596
#if GTEST_HAS_EXCEPTIONS
597
598
TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
599
const Action<void(int n)> a = Throw('a');
600
EXPECT_THROW(a.Perform(std::make_tuple(0)), char);
601
}
602
603
class MyException {};
604
605
TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
606
const Action<double(char ch)> a = Throw(MyException());
607
EXPECT_THROW(a.Perform(std::make_tuple('0')), MyException);
608
}
609
610
TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
611
const Action<double()> a = Throw(MyException());
612
EXPECT_THROW(a.Perform(std::make_tuple()), MyException);
613
}
614
615
class Object {
616
public:
617
virtual ~Object() {}
618
virtual void Func() {}
619
};
620
621
class MockObject : public Object {
622
public:
623
~MockObject() override {}
624
MOCK_METHOD(void, Func, (), (override));
625
};
626
627
TEST(ThrowActionTest, Times0) {
628
EXPECT_NONFATAL_FAILURE(
629
[] {
630
try {
631
MockObject m;
632
ON_CALL(m, Func()).WillByDefault([] { throw "something"; });
633
EXPECT_CALL(m, Func()).Times(0);
634
m.Func();
635
} catch (...) {
636
// Exception is caught but Times(0) still triggers a failure.
637
}
638
}(),
639
"");
640
}
641
642
#endif // GTEST_HAS_EXCEPTIONS
643
644
// Tests that SetArrayArgument<N>(first, last) sets the elements of the array
645
// pointed to by the N-th (0-based) argument to values in range [first, last).
646
TEST(SetArrayArgumentTest, SetsTheNthArray) {
647
using MyFunction = void(bool, int*, char*);
648
int numbers[] = {1, 2, 3};
649
Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
650
651
int n[4] = {};
652
int* pn = n;
653
char ch[4] = {};
654
char* pch = ch;
655
a.Perform(std::make_tuple(true, pn, pch));
656
EXPECT_EQ(1, n[0]);
657
EXPECT_EQ(2, n[1]);
658
EXPECT_EQ(3, n[2]);
659
EXPECT_EQ(0, n[3]);
660
EXPECT_EQ('\0', ch[0]);
661
EXPECT_EQ('\0', ch[1]);
662
EXPECT_EQ('\0', ch[2]);
663
EXPECT_EQ('\0', ch[3]);
664
665
// Tests first and last are iterators.
666
std::string letters = "abc";
667
a = SetArrayArgument<2>(letters.begin(), letters.end());
668
std::fill_n(n, 4, 0);
669
std::fill_n(ch, 4, '\0');
670
a.Perform(std::make_tuple(true, pn, pch));
671
EXPECT_EQ(0, n[0]);
672
EXPECT_EQ(0, n[1]);
673
EXPECT_EQ(0, n[2]);
674
EXPECT_EQ(0, n[3]);
675
EXPECT_EQ('a', ch[0]);
676
EXPECT_EQ('b', ch[1]);
677
EXPECT_EQ('c', ch[2]);
678
EXPECT_EQ('\0', ch[3]);
679
}
680
681
// Tests SetArrayArgument<N>(first, last) where first == last.
682
TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
683
using MyFunction = void(bool, int*);
684
int numbers[] = {1, 2, 3};
685
Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
686
687
int n[4] = {};
688
int* pn = n;
689
a.Perform(std::make_tuple(true, pn));
690
EXPECT_EQ(0, n[0]);
691
EXPECT_EQ(0, n[1]);
692
EXPECT_EQ(0, n[2]);
693
EXPECT_EQ(0, n[3]);
694
}
695
696
// Tests SetArrayArgument<N>(first, last) where *first is convertible
697
// (but not equal) to the argument type.
698
TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
699
using MyFunction = void(bool, int*);
700
char chars[] = {97, 98, 99};
701
Action<MyFunction> a = SetArrayArgument<1>(chars, chars + 3);
702
703
int codes[4] = {111, 222, 333, 444};
704
int* pcodes = codes;
705
a.Perform(std::make_tuple(true, pcodes));
706
EXPECT_EQ(97, codes[0]);
707
EXPECT_EQ(98, codes[1]);
708
EXPECT_EQ(99, codes[2]);
709
EXPECT_EQ(444, codes[3]);
710
}
711
712
// Test SetArrayArgument<N>(first, last) with iterator as argument.
713
TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
714
using MyFunction = void(bool, std::back_insert_iterator<std::string>);
715
std::string letters = "abc";
716
Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
717
718
std::string s;
719
a.Perform(std::make_tuple(true, std::back_inserter(s)));
720
EXPECT_EQ(letters, s);
721
}
722
723
TEST(ReturnPointeeTest, Works) {
724
int n = 42;
725
const Action<int()> a = ReturnPointee(&n);
726
EXPECT_EQ(42, a.Perform(std::make_tuple()));
727
728
n = 43;
729
EXPECT_EQ(43, a.Perform(std::make_tuple()));
730
}
731
732
// Tests InvokeArgument<N>(...).
733
734
// Tests using InvokeArgument with a nullary function.
735
TEST(InvokeArgumentTest, Function0) {
736
Action<int(int, int (*)())> a = InvokeArgument<1>(); // NOLINT
737
EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
738
}
739
740
// Tests using InvokeArgument with a unary functor.
741
TEST(InvokeArgumentTest, Functor1) {
742
Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
743
EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
744
}
745
746
// Tests using InvokeArgument with a unary move-only functor.
747
TEST(InvokeArgumentTest, Functor1MoveOnly) {
748
Action<int(UnaryMoveOnlyFunctor)> a = InvokeArgument<0>(true); // NOLINT
749
EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryMoveOnlyFunctor())));
750
}
751
752
// Tests using InvokeArgument with a one-shot unary functor.
753
TEST(InvokeArgumentTest, OneShotFunctor1) {
754
Action<int(OneShotUnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
755
EXPECT_EQ(1, a.Perform(std::make_tuple(OneShotUnaryFunctor())));
756
}
757
758
// Tests using InvokeArgument with a 5-ary function.
759
TEST(InvokeArgumentTest, Function5) {
760
Action<int(int (*)(int, int, int, int, int))> a = // NOLINT
761
InvokeArgument<0>(10000, 2000, 300, 40, 5);
762
EXPECT_EQ(12345, a.Perform(std::make_tuple(&SumOf5)));
763
}
764
765
// Tests using InvokeArgument with a 5-ary functor.
766
TEST(InvokeArgumentTest, Functor5) {
767
Action<int(SumOf5Functor)> a = // NOLINT
768
InvokeArgument<0>(10000, 2000, 300, 40, 5);
769
EXPECT_EQ(12345, a.Perform(std::make_tuple(SumOf5Functor())));
770
}
771
772
// Tests using InvokeArgument with a 6-ary function.
773
TEST(InvokeArgumentTest, Function6) {
774
Action<int(int (*)(int, int, int, int, int, int))> a = // NOLINT
775
InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
776
EXPECT_EQ(123456, a.Perform(std::make_tuple(&SumOf6)));
777
}
778
779
// Tests using InvokeArgument with a 6-ary functor.
780
TEST(InvokeArgumentTest, Functor6) {
781
Action<int(SumOf6Functor)> a = // NOLINT
782
InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
783
EXPECT_EQ(123456, a.Perform(std::make_tuple(SumOf6Functor())));
784
}
785
786
// Tests using InvokeArgument with a 7-ary function.
787
TEST(InvokeArgumentTest, Function7) {
788
Action<std::string(std::string (*)(const char*, const char*, const char*,
789
const char*, const char*, const char*,
790
const char*))>
791
a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
792
EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
793
}
794
795
// Tests using InvokeArgument with a 8-ary function.
796
TEST(InvokeArgumentTest, Function8) {
797
Action<std::string(std::string (*)(const char*, const char*, const char*,
798
const char*, const char*, const char*,
799
const char*, const char*))>
800
a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
801
EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
802
}
803
804
// Tests using InvokeArgument with a 9-ary function.
805
TEST(InvokeArgumentTest, Function9) {
806
Action<std::string(std::string (*)(const char*, const char*, const char*,
807
const char*, const char*, const char*,
808
const char*, const char*, const char*))>
809
a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
810
EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
811
}
812
813
// Tests using InvokeArgument with a 10-ary function.
814
TEST(InvokeArgumentTest, Function10) {
815
Action<std::string(std::string (*)(
816
const char*, const char*, const char*, const char*, const char*,
817
const char*, const char*, const char*, const char*, const char*))>
818
a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
819
EXPECT_EQ("1234567890", a.Perform(std::make_tuple(&Concat10)));
820
}
821
822
// Tests using InvokeArgument with a function that takes a pointer argument.
823
TEST(InvokeArgumentTest, ByPointerFunction) {
824
Action<const char*(const char* (*)(const char* input, short n))> // NOLINT
825
a = InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
826
EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
827
}
828
829
// Tests using InvokeArgument with a function that takes a const char*
830
// by passing it a C-string literal.
831
TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
832
Action<const char*(const char* (*)(const char* input, short n))> // NOLINT
833
a = InvokeArgument<0>("Hi", Short(1));
834
EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
835
}
836
837
// Tests using InvokeArgument with a function that takes a const reference.
838
TEST(InvokeArgumentTest, ByConstReferenceFunction) {
839
Action<bool(bool (*function)(const std::string& s))> a = // NOLINT
840
InvokeArgument<0>(std::string("Hi"));
841
// When action 'a' is constructed, it makes a copy of the temporary
842
// string object passed to it, so it's OK to use 'a' later, when the
843
// temporary object has already died.
844
EXPECT_TRUE(a.Perform(std::make_tuple(&ByConstRef)));
845
}
846
847
// Tests using InvokeArgument with ByRef() and a function that takes a
848
// const reference.
849
TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
850
Action<bool(bool (*)(const double& x))> a = // NOLINT
851
InvokeArgument<0>(ByRef(g_double));
852
// The above line calls ByRef() on a const value.
853
EXPECT_TRUE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
854
855
double x = 0;
856
a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
857
EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
858
}
859
860
TEST(InvokeArgumentTest, MoveOnlyType) {
861
struct Marker {};
862
struct {
863
// Method takes a unique_ptr (to a type we don't care about), and an
864
// invocable type.
865
MOCK_METHOD(bool, MockMethod,
866
(std::unique_ptr<Marker>, std::function<int()>), ());
867
} mock;
868
869
ON_CALL(mock, MockMethod(_, _)).WillByDefault(InvokeArgument<1>());
870
871
// This compiles, but is a little opaque as a workaround:
872
ON_CALL(mock, MockMethod(_, _))
873
.WillByDefault(WithArg<1>(InvokeArgument<0>()));
874
}
875
876
// Tests DoAll(a1, a2).
877
TEST(DoAllTest, TwoActions) {
878
int n = 0;
879
Action<int(int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
880
Return(2));
881
EXPECT_EQ(2, a.Perform(std::make_tuple(&n)));
882
EXPECT_EQ(1, n);
883
}
884
885
// Tests DoAll(a1, a2, a3).
886
TEST(DoAllTest, ThreeActions) {
887
int m = 0, n = 0;
888
Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
889
SetArgPointee<1>(2), Return(3));
890
EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n)));
891
EXPECT_EQ(1, m);
892
EXPECT_EQ(2, n);
893
}
894
895
// Tests DoAll(a1, a2, a3, a4).
896
TEST(DoAllTest, FourActions) {
897
int m = 0, n = 0;
898
char ch = '\0';
899
Action<int(int*, int*, char*)> a = // NOLINT
900
DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
901
Return(3));
902
EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n, &ch)));
903
EXPECT_EQ(1, m);
904
EXPECT_EQ(2, n);
905
EXPECT_EQ('a', ch);
906
}
907
908
// Tests DoAll(a1, a2, a3, a4, a5).
909
TEST(DoAllTest, FiveActions) {
910
int m = 0, n = 0;
911
char a = '\0', b = '\0';
912
Action<int(int*, int*, char*, char*)> action = // NOLINT
913
DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
914
SetArgPointee<3>('b'), Return(3));
915
EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b)));
916
EXPECT_EQ(1, m);
917
EXPECT_EQ(2, n);
918
EXPECT_EQ('a', a);
919
EXPECT_EQ('b', b);
920
}
921
922
// Tests DoAll(a1, a2, ..., a6).
923
TEST(DoAllTest, SixActions) {
924
int m = 0, n = 0;
925
char a = '\0', b = '\0', c = '\0';
926
Action<int(int*, int*, char*, char*, char*)> action = // NOLINT
927
DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
928
SetArgPointee<3>('b'), SetArgPointee<4>('c'), Return(3));
929
EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c)));
930
EXPECT_EQ(1, m);
931
EXPECT_EQ(2, n);
932
EXPECT_EQ('a', a);
933
EXPECT_EQ('b', b);
934
EXPECT_EQ('c', c);
935
}
936
937
// Tests DoAll(a1, a2, ..., a7).
938
TEST(DoAllTest, SevenActions) {
939
int m = 0, n = 0;
940
char a = '\0', b = '\0', c = '\0', d = '\0';
941
Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT
942
DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
943
SetArgPointee<3>('b'), SetArgPointee<4>('c'), SetArgPointee<5>('d'),
944
Return(3));
945
EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d)));
946
EXPECT_EQ(1, m);
947
EXPECT_EQ(2, n);
948
EXPECT_EQ('a', a);
949
EXPECT_EQ('b', b);
950
EXPECT_EQ('c', c);
951
EXPECT_EQ('d', d);
952
}
953
954
// Tests DoAll(a1, a2, ..., a8).
955
TEST(DoAllTest, EightActions) {
956
int m = 0, n = 0;
957
char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
958
Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
959
char*)>
960
action =
961
DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
962
SetArgPointee<3>('b'), SetArgPointee<4>('c'),
963
SetArgPointee<5>('d'), SetArgPointee<6>('e'), Return(3));
964
EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e)));
965
EXPECT_EQ(1, m);
966
EXPECT_EQ(2, n);
967
EXPECT_EQ('a', a);
968
EXPECT_EQ('b', b);
969
EXPECT_EQ('c', c);
970
EXPECT_EQ('d', d);
971
EXPECT_EQ('e', e);
972
}
973
974
// Tests DoAll(a1, a2, ..., a9).
975
TEST(DoAllTest, NineActions) {
976
int m = 0, n = 0;
977
char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
978
Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
979
char*, char*)>
980
action = DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2),
981
SetArgPointee<2>('a'), SetArgPointee<3>('b'),
982
SetArgPointee<4>('c'), SetArgPointee<5>('d'),
983
SetArgPointee<6>('e'), SetArgPointee<7>('f'), Return(3));
984
EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
985
EXPECT_EQ(1, m);
986
EXPECT_EQ(2, n);
987
EXPECT_EQ('a', a);
988
EXPECT_EQ('b', b);
989
EXPECT_EQ('c', c);
990
EXPECT_EQ('d', d);
991
EXPECT_EQ('e', e);
992
EXPECT_EQ('f', f);
993
}
994
995
// Tests DoAll(a1, a2, ..., a10).
996
TEST(DoAllTest, TenActions) {
997
int m = 0, n = 0;
998
char a = '\0', b = '\0', c = '\0', d = '\0';
999
char e = '\0', f = '\0', g = '\0';
1000
Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
1001
char*, char*, char*)>
1002
action =
1003
DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
1004
SetArgPointee<3>('b'), SetArgPointee<4>('c'),
1005
SetArgPointee<5>('d'), SetArgPointee<6>('e'),
1006
SetArgPointee<7>('f'), SetArgPointee<8>('g'), Return(3));
1007
EXPECT_EQ(
1008
3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
1009
EXPECT_EQ(1, m);
1010
EXPECT_EQ(2, n);
1011
EXPECT_EQ('a', a);
1012
EXPECT_EQ('b', b);
1013
EXPECT_EQ('c', c);
1014
EXPECT_EQ('d', d);
1015
EXPECT_EQ('e', e);
1016
EXPECT_EQ('f', f);
1017
EXPECT_EQ('g', g);
1018
}
1019
1020
TEST(DoAllTest, NoArgs) {
1021
bool ran_first = false;
1022
Action<bool()> a =
1023
DoAll([&] { ran_first = true; }, [&] { return ran_first; });
1024
EXPECT_TRUE(a.Perform({}));
1025
}
1026
1027
TEST(DoAllTest, MoveOnlyArgs) {
1028
bool ran_first = false;
1029
Action<int(std::unique_ptr<int>)> a =
1030
DoAll(InvokeWithoutArgs([&] { ran_first = true; }),
1031
[](std::unique_ptr<int> p) { return *p; });
1032
EXPECT_EQ(7, a.Perform(std::make_tuple(std::unique_ptr<int>(new int(7)))));
1033
EXPECT_TRUE(ran_first);
1034
}
1035
1036
TEST(DoAllTest, ImplicitlyConvertsActionArguments) {
1037
bool ran_first = false;
1038
// Action<void(std::vector<int>)> isn't an
1039
// Action<void(const std::vector<int>&) but can be converted.
1040
Action<void(std::vector<int>)> first = [&] { ran_first = true; };
1041
Action<int(std::vector<int>)> a =
1042
DoAll(first, [](std::vector<int> arg) { return arg.front(); });
1043
EXPECT_EQ(7, a.Perform(std::make_tuple(std::vector<int>{7})));
1044
EXPECT_TRUE(ran_first);
1045
}
1046
1047
// The ACTION*() macros trigger warning C4100 (unreferenced formal
1048
// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
1049
// the macro definition, as the warnings are generated when the macro
1050
// is expanded and macro expansion cannot contain #pragma. Therefore
1051
// we suppress them here.
1052
// Also suppress C4503 decorated name length exceeded, name was truncated
1053
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100 4503)
1054
// Tests the ACTION*() macro family.
1055
1056
// Tests that ACTION() can define an action that doesn't reference the
1057
// mock function arguments.
1058
ACTION(Return5) { return 5; }
1059
1060
TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
1061
Action<double()> a1 = Return5();
1062
EXPECT_DOUBLE_EQ(5, a1.Perform(std::make_tuple()));
1063
1064
Action<int(double, bool)> a2 = Return5();
1065
EXPECT_EQ(5, a2.Perform(std::make_tuple(1, true)));
1066
}
1067
1068
// Tests that ACTION() can define an action that returns void.
1069
ACTION(IncrementArg1) { (*arg1)++; }
1070
1071
TEST(ActionMacroTest, WorksWhenReturningVoid) {
1072
Action<void(int, int*)> a1 = IncrementArg1();
1073
int n = 0;
1074
a1.Perform(std::make_tuple(5, &n));
1075
EXPECT_EQ(1, n);
1076
}
1077
1078
// Tests that the body of ACTION() can reference the type of the
1079
// argument.
1080
ACTION(IncrementArg2) {
1081
StaticAssertTypeEq<int*, arg2_type>();
1082
arg2_type temp = arg2;
1083
(*temp)++;
1084
}
1085
1086
TEST(ActionMacroTest, CanReferenceArgumentType) {
1087
Action<void(int, bool, int*)> a1 = IncrementArg2();
1088
int n = 0;
1089
a1.Perform(std::make_tuple(5, false, &n));
1090
EXPECT_EQ(1, n);
1091
}
1092
1093
// Tests that the body of ACTION() can reference the argument tuple
1094
// via args_type and args.
1095
ACTION(Sum2) {
1096
StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
1097
args_type args_copy = args;
1098
return std::get<0>(args_copy) + std::get<1>(args_copy);
1099
}
1100
1101
TEST(ActionMacroTest, CanReferenceArgumentTuple) {
1102
Action<int(int, char, int*)> a1 = Sum2();
1103
int dummy = 0;
1104
EXPECT_EQ(11, a1.Perform(std::make_tuple(5, Char(6), &dummy)));
1105
}
1106
1107
namespace {
1108
1109
// Tests that the body of ACTION() can reference the mock function
1110
// type.
1111
int Dummy(bool flag) { return flag ? 1 : 0; }
1112
1113
} // namespace
1114
1115
ACTION(InvokeDummy) {
1116
StaticAssertTypeEq<int(bool), function_type>();
1117
function_type* fp = &Dummy;
1118
return (*fp)(true);
1119
}
1120
1121
TEST(ActionMacroTest, CanReferenceMockFunctionType) {
1122
Action<int(bool)> a1 = InvokeDummy();
1123
EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
1124
EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
1125
}
1126
1127
// Tests that the body of ACTION() can reference the mock function's
1128
// return type.
1129
ACTION(InvokeDummy2) {
1130
StaticAssertTypeEq<int, return_type>();
1131
return_type result = Dummy(true);
1132
return result;
1133
}
1134
1135
TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
1136
Action<int(bool)> a1 = InvokeDummy2();
1137
EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
1138
EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
1139
}
1140
1141
// Tests that ACTION() works for arguments passed by const reference.
1142
ACTION(ReturnAddrOfConstBoolReferenceArg) {
1143
StaticAssertTypeEq<const bool&, arg1_type>();
1144
return &arg1;
1145
}
1146
1147
TEST(ActionMacroTest, WorksForConstReferenceArg) {
1148
Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
1149
const bool b = false;
1150
EXPECT_EQ(&b, a.Perform(std::tuple<int, const bool&>(0, b)));
1151
}
1152
1153
// Tests that ACTION() works for arguments passed by non-const reference.
1154
ACTION(ReturnAddrOfIntReferenceArg) {
1155
StaticAssertTypeEq<int&, arg0_type>();
1156
return &arg0;
1157
}
1158
1159
TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
1160
Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
1161
int n = 0;
1162
EXPECT_EQ(&n, a.Perform(std::tuple<int&, bool, int>(n, true, 1)));
1163
}
1164
1165
// Tests that ACTION() can be used in a namespace.
1166
namespace action_test {
1167
ACTION(Sum) { return arg0 + arg1; }
1168
} // namespace action_test
1169
1170
TEST(ActionMacroTest, WorksInNamespace) {
1171
Action<int(int, int)> a1 = action_test::Sum();
1172
EXPECT_EQ(3, a1.Perform(std::make_tuple(1, 2)));
1173
}
1174
1175
// Tests that the same ACTION definition works for mock functions with
1176
// different argument numbers.
1177
ACTION(PlusTwo) { return arg0 + 2; }
1178
1179
TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
1180
Action<int(int)> a1 = PlusTwo();
1181
EXPECT_EQ(4, a1.Perform(std::make_tuple(2)));
1182
1183
Action<double(float, void*)> a2 = PlusTwo();
1184
int dummy;
1185
EXPECT_DOUBLE_EQ(6, a2.Perform(std::make_tuple(4.0f, &dummy)));
1186
}
1187
1188
// Tests that ACTION_P can define a parameterized action.
1189
ACTION_P(Plus, n) { return arg0 + n; }
1190
1191
TEST(ActionPMacroTest, DefinesParameterizedAction) {
1192
Action<int(int m, bool t)> a1 = Plus(9);
1193
EXPECT_EQ(10, a1.Perform(std::make_tuple(1, true)));
1194
}
1195
1196
// Tests that the body of ACTION_P can reference the argument types
1197
// and the parameter type.
1198
ACTION_P(TypedPlus, n) {
1199
arg0_type t1 = arg0;
1200
n_type t2 = n;
1201
return t1 + t2;
1202
}
1203
1204
TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
1205
Action<int(char m, bool t)> a1 = TypedPlus(9);
1206
EXPECT_EQ(10, a1.Perform(std::make_tuple(Char(1), true)));
1207
}
1208
1209
// Tests that a parameterized action can be used in any mock function
1210
// whose type is compatible.
1211
TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
1212
Action<std::string(const std::string& s)> a1 = Plus("tail");
1213
const std::string re = "re";
1214
std::tuple<const std::string> dummy = std::make_tuple(re);
1215
EXPECT_EQ("retail", a1.Perform(dummy));
1216
}
1217
1218
// Tests that we can use ACTION*() to define actions overloaded on the
1219
// number of parameters.
1220
1221
ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
1222
1223
ACTION_P(OverloadedAction, default_value) {
1224
return arg0 ? arg1 : default_value;
1225
}
1226
1227
ACTION_P2(OverloadedAction, true_value, false_value) {
1228
return arg0 ? true_value : false_value;
1229
}
1230
1231
TEST(ActionMacroTest, CanDefineOverloadedActions) {
1232
using MyAction = Action<const char*(bool, const char*)>;
1233
1234
const MyAction a1 = OverloadedAction();
1235
EXPECT_STREQ("hello", a1.Perform(std::make_tuple(false, CharPtr("world"))));
1236
EXPECT_STREQ("world", a1.Perform(std::make_tuple(true, CharPtr("world"))));
1237
1238
const MyAction a2 = OverloadedAction("hi");
1239
EXPECT_STREQ("hi", a2.Perform(std::make_tuple(false, CharPtr("world"))));
1240
EXPECT_STREQ("world", a2.Perform(std::make_tuple(true, CharPtr("world"))));
1241
1242
const MyAction a3 = OverloadedAction("hi", "you");
1243
EXPECT_STREQ("hi", a3.Perform(std::make_tuple(true, CharPtr("world"))));
1244
EXPECT_STREQ("you", a3.Perform(std::make_tuple(false, CharPtr("world"))));
1245
}
1246
1247
// Tests ACTION_Pn where n >= 3.
1248
1249
ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
1250
1251
TEST(ActionPnMacroTest, WorksFor3Parameters) {
1252
Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
1253
EXPECT_DOUBLE_EQ(3123.4, a1.Perform(std::make_tuple(3000, true)));
1254
1255
Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
1256
const std::string re = "re";
1257
std::tuple<const std::string> dummy = std::make_tuple(re);
1258
EXPECT_EQ("retail->", a2.Perform(dummy));
1259
}
1260
1261
ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
1262
1263
TEST(ActionPnMacroTest, WorksFor4Parameters) {
1264
Action<int(int)> a1 = Plus(1, 2, 3, 4);
1265
EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(std::make_tuple(10)));
1266
}
1267
1268
ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
1269
1270
TEST(ActionPnMacroTest, WorksFor5Parameters) {
1271
Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
1272
EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(std::make_tuple(10)));
1273
}
1274
1275
ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
1276
return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
1277
}
1278
1279
TEST(ActionPnMacroTest, WorksFor6Parameters) {
1280
Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
1281
EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(std::make_tuple(10)));
1282
}
1283
1284
ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
1285
return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
1286
}
1287
1288
TEST(ActionPnMacroTest, WorksFor7Parameters) {
1289
Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
1290
EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(std::make_tuple(10)));
1291
}
1292
1293
ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
1294
return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
1295
}
1296
1297
TEST(ActionPnMacroTest, WorksFor8Parameters) {
1298
Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
1299
EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
1300
a1.Perform(std::make_tuple(10)));
1301
}
1302
1303
ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
1304
return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
1305
}
1306
1307
TEST(ActionPnMacroTest, WorksFor9Parameters) {
1308
Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
1309
EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
1310
a1.Perform(std::make_tuple(10)));
1311
}
1312
1313
ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
1314
arg0_type t0 = arg0;
1315
last_param_type t9 = last_param;
1316
return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
1317
}
1318
1319
TEST(ActionPnMacroTest, WorksFor10Parameters) {
1320
Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1321
EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
1322
a1.Perform(std::make_tuple(10)));
1323
}
1324
1325
// Tests that the action body can promote the parameter types.
1326
1327
ACTION_P2(PadArgument, prefix, suffix) {
1328
// The following lines promote the two parameters to desired types.
1329
std::string prefix_str(prefix);
1330
char suffix_char = static_cast<char>(suffix);
1331
return prefix_str + arg0 + suffix_char;
1332
}
1333
1334
TEST(ActionPnMacroTest, SimpleTypePromotion) {
1335
Action<std::string(const char*)> no_promo =
1336
PadArgument(std::string("foo"), 'r');
1337
Action<std::string(const char*)> promo =
1338
PadArgument("foo", static_cast<int>('r'));
1339
EXPECT_EQ("foobar", no_promo.Perform(std::make_tuple(CharPtr("ba"))));
1340
EXPECT_EQ("foobar", promo.Perform(std::make_tuple(CharPtr("ba"))));
1341
}
1342
1343
// Tests that we can partially restrict parameter types using a
1344
// straight-forward pattern.
1345
1346
// Defines a generic action that doesn't restrict the types of its
1347
// parameters.
1348
ACTION_P3(ConcatImpl, a, b, c) {
1349
std::stringstream ss;
1350
ss << a << b << c;
1351
return ss.str();
1352
}
1353
1354
// Next, we try to restrict that either the first parameter is a
1355
// string, or the second parameter is an int.
1356
1357
// Defines a partially specialized wrapper that restricts the first
1358
// parameter to std::string.
1359
template <typename T1, typename T2>
1360
// ConcatImplActionP3 is the class template ACTION_P3 uses to
1361
// implement ConcatImpl. We shouldn't change the name as this
1362
// pattern requires the user to use it directly.
1363
ConcatImplActionP3<std::string, T1, T2> Concat(const std::string& a, T1 b,
1364
T2 c) {
1365
GTEST_INTENTIONAL_CONST_COND_PUSH_()
1366
if (true) {
1367
GTEST_INTENTIONAL_CONST_COND_POP_()
1368
// This branch verifies that ConcatImpl() can be invoked without
1369
// explicit template arguments.
1370
return ConcatImpl(a, b, c);
1371
} else {
1372
// This branch verifies that ConcatImpl() can also be invoked with
1373
// explicit template arguments. It doesn't really need to be
1374
// executed as this is a compile-time verification.
1375
return ConcatImpl<std::string, T1, T2>(a, b, c);
1376
}
1377
}
1378
1379
// Defines another partially specialized wrapper that restricts the
1380
// second parameter to int.
1381
template <typename T1, typename T2>
1382
ConcatImplActionP3<T1, int, T2> Concat(T1 a, int b, T2 c) {
1383
return ConcatImpl(a, b, c);
1384
}
1385
1386
TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
1387
Action<const std::string()> a1 = Concat("Hello", "1", 2);
1388
EXPECT_EQ("Hello12", a1.Perform(std::make_tuple()));
1389
1390
a1 = Concat(1, 2, 3);
1391
EXPECT_EQ("123", a1.Perform(std::make_tuple()));
1392
}
1393
1394
// Verifies the type of an ACTION*.
1395
1396
ACTION(DoFoo) {}
1397
ACTION_P(DoFoo, p) {}
1398
ACTION_P2(DoFoo, p0, p1) {}
1399
1400
TEST(ActionPnMacroTest, TypesAreCorrect) {
1401
// DoFoo() must be assignable to a DoFooAction variable.
1402
DoFooAction a0 = DoFoo();
1403
1404
// DoFoo(1) must be assignable to a DoFooActionP variable.
1405
DoFooActionP<int> a1 = DoFoo(1);
1406
1407
// DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
1408
// variable, and so on.
1409
DoFooActionP2<int, char> a2 = DoFoo(1, '2');
1410
PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
1411
PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
1412
PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
1413
PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
1414
PlusActionP7<int, int, int, int, int, int, char> a7 =
1415
Plus(1, 2, 3, 4, 5, 6, '7');
1416
PlusActionP8<int, int, int, int, int, int, int, char> a8 =
1417
Plus(1, 2, 3, 4, 5, 6, 7, '8');
1418
PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
1419
Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
1420
PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
1421
Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1422
1423
// Avoid "unused variable" warnings.
1424
(void)a0;
1425
(void)a1;
1426
(void)a2;
1427
(void)a3;
1428
(void)a4;
1429
(void)a5;
1430
(void)a6;
1431
(void)a7;
1432
(void)a8;
1433
(void)a9;
1434
(void)a10;
1435
}
1436
1437
// Tests that an ACTION_P*() action can be explicitly instantiated
1438
// with reference-typed parameters.
1439
1440
ACTION_P(Plus1, x) { return x; }
1441
ACTION_P2(Plus2, x, y) { return x + y; }
1442
ACTION_P3(Plus3, x, y, z) { return x + y + z; }
1443
ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
1444
return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
1445
}
1446
1447
TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
1448
int x = 1, y = 2, z = 3;
1449
const std::tuple<> empty = std::make_tuple();
1450
1451
Action<int()> a = Plus1<int&>(x);
1452
EXPECT_EQ(1, a.Perform(empty));
1453
1454
a = Plus2<const int&, int&>(x, y);
1455
EXPECT_EQ(3, a.Perform(empty));
1456
1457
a = Plus3<int&, const int&, int&>(x, y, z);
1458
EXPECT_EQ(6, a.Perform(empty));
1459
1460
int n[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
1461
a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
1462
int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6],
1463
n[7], n[8], n[9]);
1464
EXPECT_EQ(55, a.Perform(empty));
1465
}
1466
1467
class TenArgConstructorClass {
1468
public:
1469
TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7,
1470
int a8, int a9, int a10)
1471
: value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {}
1472
int value_;
1473
};
1474
1475
// Tests that ACTION_TEMPLATE works when there is no value parameter.
1476
ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T),
1477
AND_0_VALUE_PARAMS()) {
1478
return new T;
1479
}
1480
1481
TEST(ActionTemplateTest, WorksWithoutValueParam) {
1482
const Action<int*()> a = CreateNew<int>();
1483
int* p = a.Perform(std::make_tuple());
1484
delete p;
1485
}
1486
1487
// Tests that ACTION_TEMPLATE works when there are value parameters.
1488
ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T),
1489
AND_1_VALUE_PARAMS(a0)) {
1490
return new T(a0);
1491
}
1492
1493
TEST(ActionTemplateTest, WorksWithValueParams) {
1494
const Action<int*()> a = CreateNew<int>(42);
1495
int* p = a.Perform(std::make_tuple());
1496
EXPECT_EQ(42, *p);
1497
delete p;
1498
}
1499
1500
// Tests that ACTION_TEMPLATE works for integral template parameters.
1501
ACTION_TEMPLATE(MyDeleteArg, HAS_1_TEMPLATE_PARAMS(int, k),
1502
AND_0_VALUE_PARAMS()) {
1503
delete std::get<k>(args);
1504
}
1505
1506
// Resets a bool variable in the destructor.
1507
class BoolResetter {
1508
public:
1509
explicit BoolResetter(bool* value) : value_(value) {}
1510
~BoolResetter() { *value_ = false; }
1511
1512
private:
1513
bool* value_;
1514
};
1515
1516
TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
1517
const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
1518
int n = 0;
1519
bool b = true;
1520
auto* resetter = new BoolResetter(&b);
1521
a.Perform(std::make_tuple(&n, resetter));
1522
EXPECT_FALSE(b); // Verifies that resetter is deleted.
1523
}
1524
1525
// Tests that ACTION_TEMPLATES works for template template parameters.
1526
ACTION_TEMPLATE(ReturnSmartPointer,
1527
HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
1528
Pointer),
1529
AND_1_VALUE_PARAMS(pointee)) {
1530
return Pointer<pointee_type>(new pointee_type(pointee));
1531
}
1532
1533
TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
1534
const Action<std::shared_ptr<int>()> a =
1535
ReturnSmartPointer<std::shared_ptr>(42);
1536
std::shared_ptr<int> p = a.Perform(std::make_tuple());
1537
EXPECT_EQ(42, *p);
1538
}
1539
1540
// Tests that ACTION_TEMPLATE works for 10 template parameters.
1541
template <typename T1, typename T2, typename T3, int k4, bool k5,
1542
unsigned int k6, typename T7, typename T8, typename T9>
1543
struct GiantTemplate {
1544
public:
1545
explicit GiantTemplate(int a_value) : value(a_value) {}
1546
int value;
1547
};
1548
1549
ACTION_TEMPLATE(ReturnGiant,
1550
HAS_10_TEMPLATE_PARAMS(typename, T1, typename, T2, typename, T3,
1551
int, k4, bool, k5, unsigned int, k6,
1552
class, T7, class, T8, class, T9,
1553
template <typename T> class, T10),
1554
AND_1_VALUE_PARAMS(value)) {
1555
return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
1556
}
1557
1558
TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1559
using Giant = GiantTemplate<std::shared_ptr<int>, bool, double, 5, true, 6,
1560
char, unsigned, int>;
1561
const Action<Giant()> a = ReturnGiant<int, bool, double, 5, true, 6, char,
1562
unsigned, int, std::shared_ptr>(42);
1563
Giant giant = a.Perform(std::make_tuple());
1564
EXPECT_EQ(42, giant.value);
1565
}
1566
1567
// Tests that ACTION_TEMPLATE works for 10 value parameters.
1568
ACTION_TEMPLATE(ReturnSum, HAS_1_TEMPLATE_PARAMS(typename, Number),
1569
AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1570
return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1571
}
1572
1573
TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1574
const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1575
EXPECT_EQ(55, a.Perform(std::make_tuple()));
1576
}
1577
1578
// Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
1579
// on the number of value parameters.
1580
1581
ACTION(ReturnSum) { return 0; }
1582
1583
ACTION_P(ReturnSum, x) { return x; }
1584
1585
ACTION_TEMPLATE(ReturnSum, HAS_1_TEMPLATE_PARAMS(typename, Number),
1586
AND_2_VALUE_PARAMS(v1, v2)) {
1587
return static_cast<Number>(v1) + v2;
1588
}
1589
1590
ACTION_TEMPLATE(ReturnSum, HAS_1_TEMPLATE_PARAMS(typename, Number),
1591
AND_3_VALUE_PARAMS(v1, v2, v3)) {
1592
return static_cast<Number>(v1) + v2 + v3;
1593
}
1594
1595
ACTION_TEMPLATE(ReturnSum, HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
1596
AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1597
return static_cast<Number>(v1) + v2 + v3 + v4 + k;
1598
}
1599
1600
TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1601
const Action<int()> a0 = ReturnSum();
1602
const Action<int()> a1 = ReturnSum(1);
1603
const Action<int()> a2 = ReturnSum<int>(1, 2);
1604
const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
1605
const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1606
EXPECT_EQ(0, a0.Perform(std::make_tuple()));
1607
EXPECT_EQ(1, a1.Perform(std::make_tuple()));
1608
EXPECT_EQ(3, a2.Perform(std::make_tuple()));
1609
EXPECT_EQ(6, a3.Perform(std::make_tuple()));
1610
EXPECT_EQ(12345, a4.Perform(std::make_tuple()));
1611
}
1612
1613
} // namespace gmock_more_actions_test
1614
} // namespace testing
1615
1616
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100 4503
1617
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4577
1618
1619