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