blob: 4f2e877aab219b19a6282fb0874213051c2960ce [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests the built-in actions generated by a script.
35
36#include <gmock/gmock-generated-actions.h>
37
38#include <functional>
39#include <string>
40#include <gmock/gmock.h>
41#include <gtest/gtest.h>
42
43namespace testing {
44namespace gmock_generated_actions_test {
45
46using ::std::plus;
47using ::std::string;
48using ::std::tr1::get;
49using ::std::tr1::make_tuple;
50using ::std::tr1::tuple;
51using ::std::tr1::tuple_element;
52using testing::_;
53using testing::Action;
54using testing::ActionInterface;
55using testing::ByRef;
56using testing::DoAll;
57using testing::Invoke;
58using testing::InvokeArgument;
59using testing::Return;
60using testing::SetArgumentPointee;
61using testing::Unused;
62using testing::WithArg;
63using testing::WithArgs;
64using testing::WithoutArgs;
65
66// Sample functions and functors for testing Invoke() and etc.
67int Nullary() { return 1; }
68
69class NullaryFunctor {
70 public:
71 int operator()() { return 2; }
72};
73
74bool g_done = false;
75void VoidNullary() { g_done = true; }
76
77class VoidNullaryFunctor {
78 public:
79 void operator()() { g_done = true; }
80};
81
82bool Unary(int x) { return x < 0; }
83
84const char* Plus1(const char* s) { return s + 1; }
85
86void VoidUnary(int n) { g_done = true; }
87
88bool ByConstRef(const string& s) { return s == "Hi"; }
89
90const double g_double = 0;
91bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
92
93string ByNonConstRef(string& s) { return s += "+"; } // NOLINT
94
95struct UnaryFunctor {
96 int operator()(bool x) { return x ? 1 : -1; }
97};
98
99const char* Binary(const char* input, short n) { return input + n; } // NOLINT
100
101void VoidBinary(int, char) { g_done = true; }
102
103int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
104
105void VoidTernary(int, char, bool) { g_done = true; }
106
107int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
108
109int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
110
111void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
112
113string Concat4(const char* s1, const char* s2, const char* s3,
114 const char* s4) {
115 return string(s1) + s2 + s3 + s4;
116}
117
118int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
119
120struct SumOf5Functor {
121 int operator()(int a, int b, int c, int d, int e) {
122 return a + b + c + d + e;
123 }
124};
125
126string Concat5(const char* s1, const char* s2, const char* s3,
127 const char* s4, const char* s5) {
128 return string(s1) + s2 + s3 + s4 + s5;
129}
130
131int SumOf6(int a, int b, int c, int d, int e, int f) {
132 return a + b + c + d + e + f;
133}
134
135struct SumOf6Functor {
136 int operator()(int a, int b, int c, int d, int e, int f) {
137 return a + b + c + d + e + f;
138 }
139};
140
141string Concat6(const char* s1, const char* s2, const char* s3,
142 const char* s4, const char* s5, const char* s6) {
143 return string(s1) + s2 + s3 + s4 + s5 + s6;
144}
145
146string Concat7(const char* s1, const char* s2, const char* s3,
147 const char* s4, const char* s5, const char* s6,
148 const char* s7) {
149 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
150}
151
152string Concat8(const char* s1, const char* s2, const char* s3,
153 const char* s4, const char* s5, const char* s6,
154 const char* s7, const char* s8) {
155 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
156}
157
158string Concat9(const char* s1, const char* s2, const char* s3,
159 const char* s4, const char* s5, const char* s6,
160 const char* s7, const char* s8, const char* s9) {
161 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
162}
163
164string Concat10(const char* s1, const char* s2, const char* s3,
165 const char* s4, const char* s5, const char* s6,
166 const char* s7, const char* s8, const char* s9,
167 const char* s10) {
168 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
169}
170
171class Foo {
172 public:
173 Foo() : value_(123) {}
174
175 int Nullary() const { return value_; }
176
177 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
178
179 string Binary(const string& str, char c) const { return str + c; }
180
181 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
182
183 int SumOf4(int a, int b, int c, int d) const {
184 return a + b + c + d + value_;
185 }
186
187 int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
188
189 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
190
191 int SumOf6(int a, int b, int c, int d, int e, int f) {
192 return a + b + c + d + e + f;
193 }
194
195 string Concat7(const char* s1, const char* s2, const char* s3,
196 const char* s4, const char* s5, const char* s6,
197 const char* s7) {
198 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
199 }
200
201 string Concat8(const char* s1, const char* s2, const char* s3,
202 const char* s4, const char* s5, const char* s6,
203 const char* s7, const char* s8) {
204 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
205 }
206
207 string Concat9(const char* s1, const char* s2, const char* s3,
208 const char* s4, const char* s5, const char* s6,
209 const char* s7, const char* s8, const char* s9) {
210 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
211 }
212
213 string Concat10(const char* s1, const char* s2, const char* s3,
214 const char* s4, const char* s5, const char* s6,
215 const char* s7, const char* s8, const char* s9,
216 const char* s10) {
217 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
218 }
219 private:
220 int value_;
221};
222
223// Tests using Invoke() with a nullary function.
224TEST(InvokeTest, Nullary) {
225 Action<int()> a = Invoke(Nullary); // NOLINT
226 EXPECT_EQ(1, a.Perform(make_tuple()));
227}
228
229// Tests using Invoke() with a unary function.
230TEST(InvokeTest, Unary) {
231 Action<bool(int)> a = Invoke(Unary); // NOLINT
232 EXPECT_FALSE(a.Perform(make_tuple(1)));
233 EXPECT_TRUE(a.Perform(make_tuple(-1)));
234}
235
236// Tests using Invoke() with a binary function.
237TEST(InvokeTest, Binary) {
238 Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
239 const char* p = "Hello";
240 EXPECT_EQ(p + 2, a.Perform(make_tuple(p, 2)));
241}
242
243// Tests using Invoke() with a ternary function.
244TEST(InvokeTest, Ternary) {
245 Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
246 EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', 3)));
247}
248
249// Tests using Invoke() with a 4-argument function.
250TEST(InvokeTest, FunctionThatTakes4Arguments) {
251 Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
252 EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4)));
253}
254
255// Tests using Invoke() with a 5-argument function.
256TEST(InvokeTest, FunctionThatTakes5Arguments) {
257 Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
258 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
259}
260
261// Tests using Invoke() with a 6-argument function.
262TEST(InvokeTest, FunctionThatTakes6Arguments) {
263 Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
264 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
265}
266
267// Tests using Invoke() with a 7-argument function.
268TEST(InvokeTest, FunctionThatTakes7Arguments) {
269 Action<string(const char*, const char*, const char*, const char*,
270 const char*, const char*, const char*)> a =
271 Invoke(Concat7);
272 EXPECT_EQ("1234567",
273 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7")));
274}
275
276// Tests using Invoke() with a 8-argument function.
277TEST(InvokeTest, FunctionThatTakes8Arguments) {
278 Action<string(const char*, const char*, const char*, const char*,
279 const char*, const char*, const char*, const char*)> a =
280 Invoke(Concat8);
281 EXPECT_EQ("12345678",
282 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8")));
283}
284
285// Tests using Invoke() with a 9-argument function.
286TEST(InvokeTest, FunctionThatTakes9Arguments) {
287 Action<string(const char*, const char*, const char*, const char*,
288 const char*, const char*, const char*, const char*,
289 const char*)> a = Invoke(Concat9);
290 EXPECT_EQ("123456789",
291 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9")));
292}
293
294// Tests using Invoke() with a 10-argument function.
295TEST(InvokeTest, FunctionThatTakes10Arguments) {
296 Action<string(const char*, const char*, const char*, const char*,
297 const char*, const char*, const char*, const char*,
298 const char*, const char*)> a = Invoke(Concat10);
299 EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6",
300 "7", "8", "9", "0")));
301}
302
303// Tests using Invoke() with functions with parameters declared as Unused.
304TEST(InvokeTest, FunctionWithUnusedParameters) {
305 Action<int(int, int, double, const string&)> a1 =
306 Invoke(SumOfFirst2);
307 EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, "hi")));
308
309 Action<int(int, int, bool, int*)> a2 =
310 Invoke(SumOfFirst2);
311 EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL))));
312}
313
314// Tests using Invoke() with methods with parameters declared as Unused.
315TEST(InvokeTest, MethodWithUnusedParameters) {
316 Foo foo;
317 Action<int(string, bool, int, int)> a1 =
318 Invoke(&foo, &Foo::SumOfLast2);
319 EXPECT_EQ(12, a1.Perform(make_tuple("hi", true, 10, 2)));
320
321 Action<int(char, double, int, int)> a2 =
322 Invoke(&foo, &Foo::SumOfLast2);
323 EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3)));
324}
325
326// Tests using Invoke() with a functor.
327TEST(InvokeTest, Functor) {
328 Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT
329 EXPECT_EQ(3, a.Perform(make_tuple(1, 2)));
330}
331
332// Tests using Invoke(f) as an action of a compatible type.
333TEST(InvokeTest, FunctionWithCompatibleType) {
334 Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
335 EXPECT_EQ(4321, a.Perform(make_tuple(4000, 300, 20, true)));
336}
337
338// Tests using Invoke() with an object pointer and a method pointer.
339
340// Tests using Invoke() with a nullary method.
341TEST(InvokeMethodTest, Nullary) {
342 Foo foo;
343 Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT
344 EXPECT_EQ(123, a.Perform(make_tuple()));
345}
346
347// Tests using Invoke() with a unary method.
348TEST(InvokeMethodTest, Unary) {
349 Foo foo;
350 Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT
351 EXPECT_EQ(4123, a.Perform(make_tuple(4000)));
352}
353
354// Tests using Invoke() with a binary method.
355TEST(InvokeMethodTest, Binary) {
356 Foo foo;
357 Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary);
358 string s("Hell");
359 EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o')));
360}
361
362// Tests using Invoke() with a ternary method.
363TEST(InvokeMethodTest, Ternary) {
364 Foo foo;
365 Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT
366 EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, 1)));
367}
368
369// Tests using Invoke() with a 4-argument method.
370TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
371 Foo foo;
372 Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT
373 EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4)));
374}
375
376// Tests using Invoke() with a 5-argument method.
377TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
378 Foo foo;
379 Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT
380 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
381}
382
383// Tests using Invoke() with a 6-argument method.
384TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
385 Foo foo;
386 Action<int(int, int, int, int, int, int)> a = // NOLINT
387 Invoke(&foo, &Foo::SumOf6);
388 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
389}
390
391// Tests using Invoke() with a 7-argument method.
392TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
393 Foo foo;
394 Action<string(const char*, const char*, const char*, const char*,
395 const char*, const char*, const char*)> a =
396 Invoke(&foo, &Foo::Concat7);
397 EXPECT_EQ("1234567",
398 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7")));
399}
400
401// Tests using Invoke() with a 8-argument method.
402TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
403 Foo foo;
404 Action<string(const char*, const char*, const char*, const char*,
405 const char*, const char*, const char*, const char*)> a =
406 Invoke(&foo, &Foo::Concat8);
407 EXPECT_EQ("12345678",
408 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8")));
409}
410
411// Tests using Invoke() with a 9-argument method.
412TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
413 Foo foo;
414 Action<string(const char*, const char*, const char*, const char*,
415 const char*, const char*, const char*, const char*,
416 const char*)> a = Invoke(&foo, &Foo::Concat9);
417 EXPECT_EQ("123456789",
418 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9")));
419}
420
421// Tests using Invoke() with a 10-argument method.
422TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
423 Foo foo;
424 Action<string(const char*, const char*, const char*, const char*,
425 const char*, const char*, const char*, const char*,
426 const char*, const char*)> a = Invoke(&foo, &Foo::Concat10);
427 EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6",
428 "7", "8", "9", "0")));
429}
430
431// Tests using Invoke(f) as an action of a compatible type.
432TEST(InvokeMethodTest, MethodWithCompatibleType) {
433 Foo foo;
434 Action<long(int, short, char, bool)> a = // NOLINT
435 Invoke(&foo, &Foo::SumOf4);
436 EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true)));
437}
438
439// Tests ByRef().
440
441// Tests that ReferenceWrapper<T> is copyable.
442TEST(ByRefTest, IsCopyable) {
443 const string s1 = "Hi";
444 const string s2 = "Hello";
445
446 ::testing::internal::ReferenceWrapper<const string> ref_wrapper = ByRef(s1);
447 const string& r1 = ref_wrapper;
448 EXPECT_EQ(&s1, &r1);
449
450 // Assigns a new value to ref_wrapper.
451 ref_wrapper = ByRef(s2);
452 const string& r2 = ref_wrapper;
453 EXPECT_EQ(&s2, &r2);
454
455 ::testing::internal::ReferenceWrapper<const string> ref_wrapper1 = ByRef(s1);
456 // Copies ref_wrapper1 to ref_wrapper.
457 ref_wrapper = ref_wrapper1;
458 const string& r3 = ref_wrapper;
459 EXPECT_EQ(&s1, &r3);
460}
461
462// Tests using ByRef() on a const value.
463TEST(ByRefTest, ConstValue) {
464 const int n = 0;
465 // int& ref = ByRef(n); // This shouldn't compile - we have a
466 // negative compilation test to catch it.
467 const int& const_ref = ByRef(n);
468 EXPECT_EQ(&n, &const_ref);
469}
470
471// Tests using ByRef() on a non-const value.
472TEST(ByRefTest, NonConstValue) {
473 int n = 0;
474
475 // ByRef(n) can be used as either an int&,
476 int& ref = ByRef(n);
477 EXPECT_EQ(&n, &ref);
478
479 // or a const int&.
480 const int& const_ref = ByRef(n);
481 EXPECT_EQ(&n, &const_ref);
482}
483
484struct Base {
485 bool operator==(const Base&) { return true; }
486};
487
488struct Derived : public Base {
489 bool operator==(const Derived&) { return true; }
490};
491
492// Tests explicitly specifying the type when using ByRef().
493TEST(ByRefTest, ExplicitType) {
494 int n = 0;
495 const int& r1 = ByRef<const int>(n);
496 EXPECT_EQ(&n, &r1);
497
498 // ByRef<char>(n); // This shouldn't compile - we have a negative
499 // compilation test to catch it.
500
501
502 Derived d;
503 Derived& r2 = ByRef<Derived>(d);
504 EXPECT_EQ(&d, &r2);
505
506 const Derived& r3 = ByRef<const Derived>(d);
507 EXPECT_EQ(&d, &r3);
508
509 Base& r4 = ByRef<Base>(d);
510 EXPECT_EQ(&d, &r4);
511
512 const Base& r5 = ByRef<const Base>(d);
513 EXPECT_EQ(&d, &r5);
514
515 // The following shouldn't compile - we have a negative compilation
516 // test for it.
517 //
518 // Base b;
519 // ByRef<Derived>(b);
520}
521
522// Tests InvokeArgument<N>(...).
523
524// Tests using InvokeArgument with a nullary function.
525TEST(InvokeArgumentTest, Function0) {
526 Action<int(int, int(*)())> a = InvokeArgument<1>(); // NOLINT
527 EXPECT_EQ(1, a.Perform(make_tuple(2, &Nullary)));
528}
529
530// Tests using InvokeArgument with a unary function.
531TEST(InvokeArgumentTest, Functor1) {
532 Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
533 EXPECT_EQ(1, a.Perform(make_tuple(UnaryFunctor())));
534}
535
536// Tests using InvokeArgument with a 5-ary function.
537TEST(InvokeArgumentTest, Function5) {
538 Action<int(int(*)(int, int, int, int, int))> a = // NOLINT
539 InvokeArgument<0>(10000, 2000, 300, 40, 5);
540 EXPECT_EQ(12345, a.Perform(make_tuple(&SumOf5)));
541}
542
543// Tests using InvokeArgument with a 5-ary functor.
544TEST(InvokeArgumentTest, Functor5) {
545 Action<int(SumOf5Functor)> a = // NOLINT
546 InvokeArgument<0>(10000, 2000, 300, 40, 5);
547 EXPECT_EQ(12345, a.Perform(make_tuple(SumOf5Functor())));
548}
549
550// Tests using InvokeArgument with a 6-ary function.
551TEST(InvokeArgumentTest, Function6) {
552 Action<int(int(*)(int, int, int, int, int, int))> a = // NOLINT
553 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
554 EXPECT_EQ(123456, a.Perform(make_tuple(&SumOf6)));
555}
556
557// Tests using InvokeArgument with a 6-ary functor.
558TEST(InvokeArgumentTest, Functor6) {
559 Action<int(SumOf6Functor)> a = // NOLINT
560 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
561 EXPECT_EQ(123456, a.Perform(make_tuple(SumOf6Functor())));
562}
563
564// Tests using InvokeArgument with a 7-ary function.
565TEST(InvokeArgumentTest, Function7) {
566 Action<string(string(*)(const char*, const char*, const char*,
567 const char*, const char*, const char*,
568 const char*))> a =
569 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
570 EXPECT_EQ("1234567", a.Perform(make_tuple(&Concat7)));
571}
572
573// Tests using InvokeArgument with a 8-ary function.
574TEST(InvokeArgumentTest, Function8) {
575 Action<string(string(*)(const char*, const char*, const char*,
576 const char*, const char*, const char*,
577 const char*, const char*))> a =
578 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
579 EXPECT_EQ("12345678", a.Perform(make_tuple(&Concat8)));
580}
581
582// Tests using InvokeArgument with a 9-ary function.
583TEST(InvokeArgumentTest, Function9) {
584 Action<string(string(*)(const char*, const char*, const char*,
585 const char*, const char*, const char*,
586 const char*, const char*, const char*))> a =
587 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
588 EXPECT_EQ("123456789", a.Perform(make_tuple(&Concat9)));
589}
590
591// Tests using InvokeArgument with a 10-ary function.
592TEST(InvokeArgumentTest, Function10) {
593 Action<string(string(*)(const char*, const char*, const char*,
594 const char*, const char*, const char*,
595 const char*, const char*, const char*,
596 const char*))> a =
597 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
598 EXPECT_EQ("1234567890", a.Perform(make_tuple(&Concat10)));
599}
600
601// Tests using InvokeArgument with a function that takes a pointer argument.
602TEST(InvokeArgumentTest, ByPointerFunction) {
603 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
604 InvokeArgument<0>(static_cast<const char*>("Hi"), 1);
605 EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
606}
607
608// Tests using InvokeArgument with a function that takes a const char*
609// by passing it a C-string literal.
610TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
611 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
612 InvokeArgument<0>("Hi", 1);
613 EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
614}
615
616// Tests using InvokeArgument with a function that takes a const reference.
617TEST(InvokeArgumentTest, ByConstReferenceFunction) {
618 Action<bool(bool(*function)(const string& s))> a = // NOLINT
619 InvokeArgument<0>(string("Hi"));
620 // When action 'a' is constructed, it makes a copy of the temporary
621 // string object passed to it, so it's OK to use 'a' later, when the
622 // temporary object has already died.
623 EXPECT_TRUE(a.Perform(make_tuple(&ByConstRef)));
624}
625
626// Tests using InvokeArgument with ByRef() and a function that takes a
627// const reference.
628TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
629 Action<bool(bool(*)(const double& x))> a = // NOLINT
630 InvokeArgument<0>(ByRef(g_double));
631 // The above line calls ByRef() on a const value.
632 EXPECT_TRUE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
633
634 double x = 0;
635 a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
636 EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
637}
638
639// Tests using WithoutArgs with an action that takes no argument.
640TEST(WithoutArgsTest, NoArg) {
641 Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
642 EXPECT_EQ(1, a.Perform(make_tuple(2)));
643}
644
645// Tests using WithArgs and WithArg with an action that takes 1 argument.
646TEST(WithArgsTest, OneArg) {
647 Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
648 EXPECT_TRUE(a.Perform(make_tuple(1.5, -1)));
649 EXPECT_FALSE(a.Perform(make_tuple(1.5, 1)));
650
651 // Also tests the synonym WithArg.
652 Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
653 EXPECT_TRUE(a.Perform(make_tuple(1.5, -1)));
654 EXPECT_FALSE(a.Perform(make_tuple(1.5, 1)));
655
656}
657
658// Tests using WithArgs with an action that takes 2 arguments.
659TEST(WithArgsTest, TwoArgs) {
660 Action<const char*(const char* s, double x, int n)> a =
661 WithArgs<0, 2>(Invoke(Binary));
662 const char s[] = "Hello";
663 EXPECT_EQ(s + 2, a.Perform(make_tuple(s, 0.5, 2)));
664}
665
666// Tests using WithArgs with an action that takes 3 arguments.
667TEST(WithArgsTest, ThreeArgs) {
668 Action<int(int, double, char, short)> a = // NOLINT
669 WithArgs<0, 2, 3>(Invoke(Ternary));
670 EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, 20, 3)));
671}
672
673// Tests using WithArgs with an action that takes 4 arguments.
674TEST(WithArgsTest, FourArgs) {
675 Action<string(const char*, const char*, double, const char*, const char*)> a =
676 WithArgs<4, 3, 1, 0>(Invoke(Concat4));
677 EXPECT_EQ("4310", a.Perform(make_tuple("0", "1", 2.5, "3", "4")));
678}
679
680// Tests using WithArgs with an action that takes 5 arguments.
681TEST(WithArgsTest, FiveArgs) {
682 Action<string(const char*, const char*, const char*,
683 const char*, const char*)> a =
684 WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5));
685 EXPECT_EQ("43210", a.Perform(make_tuple("0", "1", "2", "3", "4")));
686}
687
688// Tests using WithArgs with an action that takes 6 arguments.
689TEST(WithArgsTest, SixArgs) {
690 Action<string(const char*, const char*, const char*)> a =
691 WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6));
692 EXPECT_EQ("012210", a.Perform(make_tuple("0", "1", "2")));
693}
694
695// Tests using WithArgs with an action that takes 7 arguments.
696TEST(WithArgsTest, SevenArgs) {
697 Action<string(const char*, const char*, const char*, const char*)> a =
698 WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7));
699 EXPECT_EQ("0123210", a.Perform(make_tuple("0", "1", "2", "3")));
700}
701
702// Tests using WithArgs with an action that takes 8 arguments.
703TEST(WithArgsTest, EightArgs) {
704 Action<string(const char*, const char*, const char*, const char*)> a =
705 WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8));
706 EXPECT_EQ("01230123", a.Perform(make_tuple("0", "1", "2", "3")));
707}
708
709// Tests using WithArgs with an action that takes 9 arguments.
710TEST(WithArgsTest, NineArgs) {
711 Action<string(const char*, const char*, const char*, const char*)> a =
712 WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9));
713 EXPECT_EQ("012312323", a.Perform(make_tuple("0", "1", "2", "3")));
714}
715
716// Tests using WithArgs with an action that takes 10 arguments.
717TEST(WithArgsTest, TenArgs) {
718 Action<string(const char*, const char*, const char*, const char*)> a =
719 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10));
720 EXPECT_EQ("0123210123", a.Perform(make_tuple("0", "1", "2", "3")));
721}
722
723// Tests using WithArgs with an action that is not Invoke().
724class SubstractAction : public ActionInterface<int(int, int)> { // NOLINT
725 public:
726 virtual int Perform(const tuple<int, int>& args) {
727 return get<0>(args) - get<1>(args);
728 }
729};
730
731TEST(WithArgsTest, NonInvokeAction) {
732 Action<int(const string&, int, int)> a = // NOLINT
733 WithArgs<2, 1>(MakeAction(new SubstractAction));
734 EXPECT_EQ(8, a.Perform(make_tuple("hi", 2, 10)));
735}
736
737// Tests using WithArgs to pass all original arguments in the original order.
738TEST(WithArgsTest, Identity) {
739 Action<int(int x, char y, short z)> a = // NOLINT
740 WithArgs<0, 1, 2>(Invoke(Ternary));
741 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 3)));
742}
743
744// Tests using WithArgs with repeated arguments.
745TEST(WithArgsTest, RepeatedArguments) {
746 Action<int(bool, int m, int n)> a = // NOLINT
747 WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
748 EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10)));
749}
750
751// Tests using WithArgs with reversed argument order.
752TEST(WithArgsTest, ReversedArgumentOrder) {
753 Action<const char*(short n, const char* input)> a = // NOLINT
754 WithArgs<1, 0>(Invoke(Binary));
755 const char s[] = "Hello";
756 EXPECT_EQ(s + 2, a.Perform(make_tuple(2, s)));
757}
758
759// Tests using WithArgs with compatible, but not identical, argument types.
760TEST(WithArgsTest, ArgsOfCompatibleTypes) {
761 Action<long(short x, int y, double z, char c)> a = // NOLINT
762 WithArgs<0, 1, 3>(Invoke(Ternary));
763 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 5.6, 3)));
764}
765
766// Tests using WithArgs with an action that returns void.
767TEST(WithArgsTest, VoidAction) {
768 Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
769 g_done = false;
770 a.Perform(make_tuple(1.5, 'a', 3));
771 EXPECT_TRUE(g_done);
772}
773
774// Tests DoAll(a1, a2).
775TEST(DoAllTest, TwoActions) {
776 int n = 0;
777 Action<int(int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT
778 Return(2));
779 EXPECT_EQ(2, a.Perform(make_tuple(&n)));
780 EXPECT_EQ(1, n);
781}
782
783// Tests DoAll(a1, a2, a3).
784TEST(DoAllTest, ThreeActions) {
785 int m = 0, n = 0;
786 Action<int(int*, int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT
787 SetArgumentPointee<1>(2),
788 Return(3));
789 EXPECT_EQ(3, a.Perform(make_tuple(&m, &n)));
790 EXPECT_EQ(1, m);
791 EXPECT_EQ(2, n);
792}
793
794// Tests DoAll(a1, a2, a3, a4).
795TEST(DoAllTest, FourActions) {
796 int m = 0, n = 0;
797 char ch = '\0';
798 Action<int(int*, int*, char*)> a = // NOLINT
799 DoAll(SetArgumentPointee<0>(1),
800 SetArgumentPointee<1>(2),
801 SetArgumentPointee<2>('a'),
802 Return(3));
803 EXPECT_EQ(3, a.Perform(make_tuple(&m, &n, &ch)));
804 EXPECT_EQ(1, m);
805 EXPECT_EQ(2, n);
806 EXPECT_EQ('a', ch);
807}
808
809// Tests DoAll(a1, a2, a3, a4, a5).
810TEST(DoAllTest, FiveActions) {
811 int m = 0, n = 0;
812 char a = '\0', b = '\0';
813 Action<int(int*, int*, char*, char*)> action = // NOLINT
814 DoAll(SetArgumentPointee<0>(1),
815 SetArgumentPointee<1>(2),
816 SetArgumentPointee<2>('a'),
817 SetArgumentPointee<3>('b'),
818 Return(3));
819 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b)));
820 EXPECT_EQ(1, m);
821 EXPECT_EQ(2, n);
822 EXPECT_EQ('a', a);
823 EXPECT_EQ('b', b);
824}
825
826// Tests DoAll(a1, a2, ..., a6).
827TEST(DoAllTest, SixActions) {
828 int m = 0, n = 0;
829 char a = '\0', b = '\0', c = '\0';
830 Action<int(int*, int*, char*, char*, char*)> action = // NOLINT
831 DoAll(SetArgumentPointee<0>(1),
832 SetArgumentPointee<1>(2),
833 SetArgumentPointee<2>('a'),
834 SetArgumentPointee<3>('b'),
835 SetArgumentPointee<4>('c'),
836 Return(3));
837 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c)));
838 EXPECT_EQ(1, m);
839 EXPECT_EQ(2, n);
840 EXPECT_EQ('a', a);
841 EXPECT_EQ('b', b);
842 EXPECT_EQ('c', c);
843}
844
845// Tests DoAll(a1, a2, ..., a7).
846TEST(DoAllTest, SevenActions) {
847 int m = 0, n = 0;
848 char a = '\0', b = '\0', c = '\0', d = '\0';
849 Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT
850 DoAll(SetArgumentPointee<0>(1),
851 SetArgumentPointee<1>(2),
852 SetArgumentPointee<2>('a'),
853 SetArgumentPointee<3>('b'),
854 SetArgumentPointee<4>('c'),
855 SetArgumentPointee<5>('d'),
856 Return(3));
857 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d)));
858 EXPECT_EQ(1, m);
859 EXPECT_EQ(2, n);
860 EXPECT_EQ('a', a);
861 EXPECT_EQ('b', b);
862 EXPECT_EQ('c', c);
863 EXPECT_EQ('d', d);
864}
865
866// Tests DoAll(a1, a2, ..., a8).
867TEST(DoAllTest, EightActions) {
868 int m = 0, n = 0;
869 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
870 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
871 char*)> action =
872 DoAll(SetArgumentPointee<0>(1),
873 SetArgumentPointee<1>(2),
874 SetArgumentPointee<2>('a'),
875 SetArgumentPointee<3>('b'),
876 SetArgumentPointee<4>('c'),
877 SetArgumentPointee<5>('d'),
878 SetArgumentPointee<6>('e'),
879 Return(3));
880 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e)));
881 EXPECT_EQ(1, m);
882 EXPECT_EQ(2, n);
883 EXPECT_EQ('a', a);
884 EXPECT_EQ('b', b);
885 EXPECT_EQ('c', c);
886 EXPECT_EQ('d', d);
887 EXPECT_EQ('e', e);
888}
889
890// Tests DoAll(a1, a2, ..., a9).
891TEST(DoAllTest, NineActions) {
892 int m = 0, n = 0;
893 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
894 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
895 char*, char*)> action =
896 DoAll(SetArgumentPointee<0>(1),
897 SetArgumentPointee<1>(2),
898 SetArgumentPointee<2>('a'),
899 SetArgumentPointee<3>('b'),
900 SetArgumentPointee<4>('c'),
901 SetArgumentPointee<5>('d'),
902 SetArgumentPointee<6>('e'),
903 SetArgumentPointee<7>('f'),
904 Return(3));
905 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
906 EXPECT_EQ(1, m);
907 EXPECT_EQ(2, n);
908 EXPECT_EQ('a', a);
909 EXPECT_EQ('b', b);
910 EXPECT_EQ('c', c);
911 EXPECT_EQ('d', d);
912 EXPECT_EQ('e', e);
913 EXPECT_EQ('f', f);
914}
915
916// Tests DoAll(a1, a2, ..., a10).
917TEST(DoAllTest, TenActions) {
918 int m = 0, n = 0;
919 char a = '\0', b = '\0', c = '\0', d = '\0';
920 char e = '\0', f = '\0', g = '\0';
921 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
922 char*, char*, char*)> action =
923 DoAll(SetArgumentPointee<0>(1),
924 SetArgumentPointee<1>(2),
925 SetArgumentPointee<2>('a'),
926 SetArgumentPointee<3>('b'),
927 SetArgumentPointee<4>('c'),
928 SetArgumentPointee<5>('d'),
929 SetArgumentPointee<6>('e'),
930 SetArgumentPointee<7>('f'),
931 SetArgumentPointee<8>('g'),
932 Return(3));
933 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
934 EXPECT_EQ(1, m);
935 EXPECT_EQ(2, n);
936 EXPECT_EQ('a', a);
937 EXPECT_EQ('b', b);
938 EXPECT_EQ('c', c);
939 EXPECT_EQ('d', d);
940 EXPECT_EQ('e', e);
941 EXPECT_EQ('f', f);
942 EXPECT_EQ('g', g);
943}
944
945} // namespace gmock_generated_actions_test
946} // namespace testing