1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Google Mock - a framework for writing C++ mock classes.
32 // This file tests the built-in actions in gmock-actions.h.
36 #pragma warning(disable : 4577)
39 #include "gmock/gmock-more-actions.h"
46 #include "gmock/gmock.h"
47 #include "gtest/gtest-spi.h"
48 #include "gtest/gtest.h"
51 namespace gmock_more_actions_test {
55 using testing::Action;
56 using testing::DeleteArg;
57 using testing::Invoke;
58 using testing::ReturnArg;
59 using testing::ReturnPointee;
60 using testing::SaveArg;
61 using testing::SaveArgPointee;
62 using testing::SetArgReferee;
63 using testing::Unused;
64 using testing::WithArg;
65 using testing::WithoutArgs;
67 // For suppressing compiler warnings on conversion possibly losing precision.
68 inline short Short(short n) { return n; } // NOLINT
69 inline char Char(char ch) { return ch; }
71 // Sample functions and functors for testing Invoke() and etc.
72 int Nullary() { return 1; }
76 bool Unary(int x) { return x < 0; }
78 bool ByConstRef(const std::string& s) { return s == "Hi"; }
80 const double g_double = 0;
81 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
84 int operator()(bool x) { return x ? 1 : -1; }
87 const char* Binary(const char* input, short n) { return input + n; } // NOLINT
89 int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
91 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
93 int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
95 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
97 struct SumOf5Functor {
98 int operator()(int a, int b, int c, int d, int e) {
99 return a + b + c + d + e;
103 int SumOf6(int a, int b, int c, int d, int e, int f) {
104 return a + b + c + d + e + f;
107 struct SumOf6Functor {
108 int operator()(int a, int b, int c, int d, int e, int f) {
109 return a + b + c + d + e + f;
113 std::string Concat7(const char* s1, const char* s2, const char* s3,
114 const char* s4, const char* s5, const char* s6,
116 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
119 std::string Concat8(const char* s1, const char* s2, const char* s3,
120 const char* s4, const char* s5, const char* s6,
121 const char* s7, const char* s8) {
122 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
125 std::string Concat9(const char* s1, const char* s2, const char* s3,
126 const char* s4, const char* s5, const char* s6,
127 const char* s7, const char* s8, const char* s9) {
128 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
131 std::string Concat10(const char* s1, const char* s2, const char* s3,
132 const char* s4, const char* s5, const char* s6,
133 const char* s7, const char* s8, const char* s9,
135 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
140 Foo() : value_(123) {}
142 int Nullary() const { return value_; }
144 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
146 std::string Binary(const std::string& str, char c) const { return str + c; }
148 int Ternary(int x, bool y, char z) { return value_ + x + y * z; }
150 int SumOf4(int a, int b, int c, int d) const {
151 return a + b + c + d + value_;
154 int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
156 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
158 int SumOf6(int a, int b, int c, int d, int e, int f) {
159 return a + b + c + d + e + f;
162 std::string Concat7(const char* s1, const char* s2, const char* s3,
163 const char* s4, const char* s5, const char* s6,
165 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
168 std::string Concat8(const char* s1, const char* s2, const char* s3,
169 const char* s4, const char* s5, const char* s6,
170 const char* s7, const char* s8) {
171 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
174 std::string Concat9(const char* s1, const char* s2, const char* s3,
175 const char* s4, const char* s5, const char* s6,
176 const char* s7, const char* s8, const char* s9) {
177 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
180 std::string Concat10(const char* s1, const char* s2, const char* s3,
181 const char* s4, const char* s5, const char* s6,
182 const char* s7, const char* s8, const char* s9,
184 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
191 // Tests using Invoke() with a nullary function.
192 TEST(InvokeTest, Nullary) {
193 Action<int()> a = Invoke(Nullary); // NOLINT
194 EXPECT_EQ(1, a.Perform(std::make_tuple()));
197 // Tests using Invoke() with a unary function.
198 TEST(InvokeTest, Unary) {
199 Action<bool(int)> a = Invoke(Unary); // NOLINT
200 EXPECT_FALSE(a.Perform(std::make_tuple(1)));
201 EXPECT_TRUE(a.Perform(std::make_tuple(-1)));
204 // Tests using Invoke() with a binary function.
205 TEST(InvokeTest, Binary) {
206 Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
207 const char* p = "Hello";
208 EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2))));
211 // Tests using Invoke() with a ternary function.
212 TEST(InvokeTest, Ternary) {
213 Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
214 EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3))));
217 // Tests using Invoke() with a 4-argument function.
218 TEST(InvokeTest, FunctionThatTakes4Arguments) {
219 Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
220 EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4)));
223 // Tests using Invoke() with a 5-argument function.
224 TEST(InvokeTest, FunctionThatTakes5Arguments) {
225 Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
226 EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
229 // Tests using Invoke() with a 6-argument function.
230 TEST(InvokeTest, FunctionThatTakes6Arguments) {
231 Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
233 a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
236 // A helper that turns the type of a C-string literal from const
237 // char[N] to const char*.
238 inline const char* CharPtr(const char* s) { return s; }
240 // Tests using Invoke() with a 7-argument function.
241 TEST(InvokeTest, FunctionThatTakes7Arguments) {
242 Action<std::string(const char*, const char*, const char*, const char*,
243 const char*, const char*, const char*)>
246 a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
247 CharPtr("4"), CharPtr("5"), CharPtr("6"),
251 // Tests using Invoke() with a 8-argument function.
252 TEST(InvokeTest, FunctionThatTakes8Arguments) {
253 Action<std::string(const char*, const char*, const char*, const char*,
254 const char*, const char*, const char*, const char*)>
256 EXPECT_EQ("12345678",
257 a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
258 CharPtr("4"), CharPtr("5"), CharPtr("6"),
259 CharPtr("7"), CharPtr("8"))));
262 // Tests using Invoke() with a 9-argument function.
263 TEST(InvokeTest, FunctionThatTakes9Arguments) {
264 Action<std::string(const char*, const char*, const char*, const char*,
265 const char*, const char*, const char*, const char*,
268 EXPECT_EQ("123456789", a.Perform(std::make_tuple(
269 CharPtr("1"), CharPtr("2"), CharPtr("3"),
270 CharPtr("4"), CharPtr("5"), CharPtr("6"),
271 CharPtr("7"), CharPtr("8"), CharPtr("9"))));
274 // Tests using Invoke() with a 10-argument function.
275 TEST(InvokeTest, FunctionThatTakes10Arguments) {
276 Action<std::string(const char*, const char*, const char*, const char*,
277 const char*, const char*, const char*, const char*,
278 const char*, const char*)>
279 a = Invoke(Concat10);
280 EXPECT_EQ("1234567890",
281 a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
282 CharPtr("4"), CharPtr("5"), CharPtr("6"),
283 CharPtr("7"), CharPtr("8"), CharPtr("9"),
287 // Tests using Invoke() with functions with parameters declared as Unused.
288 TEST(InvokeTest, FunctionWithUnusedParameters) {
289 Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
290 std::tuple<int, int, double, std::string> dummy =
291 std::make_tuple(10, 2, 5.6, std::string("hi"));
292 EXPECT_EQ(12, a1.Perform(dummy));
294 Action<int(int, int, bool, int*)> a2 = Invoke(SumOfFirst2);
296 23, a2.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
299 // Tests using Invoke() with methods with parameters declared as Unused.
300 TEST(InvokeTest, MethodWithUnusedParameters) {
302 Action<int(std::string, bool, int, int)> a1 = Invoke(&foo, &Foo::SumOfLast2);
303 EXPECT_EQ(12, a1.Perform(std::make_tuple(CharPtr("hi"), true, 10, 2)));
305 Action<int(char, double, int, int)> a2 = Invoke(&foo, &Foo::SumOfLast2);
306 EXPECT_EQ(23, a2.Perform(std::make_tuple('a', 2.5, 20, 3)));
309 // Tests using Invoke() with a functor.
310 TEST(InvokeTest, Functor) {
311 Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT
312 EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2)));
315 // Tests using Invoke(f) as an action of a compatible type.
316 TEST(InvokeTest, FunctionWithCompatibleType) {
317 Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
318 EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
321 // Tests using Invoke() with an object pointer and a method pointer.
323 // Tests using Invoke() with a nullary method.
324 TEST(InvokeMethodTest, Nullary) {
326 Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT
327 EXPECT_EQ(123, a.Perform(std::make_tuple()));
330 // Tests using Invoke() with a unary method.
331 TEST(InvokeMethodTest, Unary) {
333 Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT
334 EXPECT_EQ(4123, a.Perform(std::make_tuple(4000)));
337 // Tests using Invoke() with a binary method.
338 TEST(InvokeMethodTest, Binary) {
340 Action<std::string(const std::string&, char)> a = Invoke(&foo, &Foo::Binary);
341 std::string s("Hell");
342 std::tuple<std::string, char> dummy = std::make_tuple(s, 'o');
343 EXPECT_EQ("Hello", a.Perform(dummy));
346 // Tests using Invoke() with a ternary method.
347 TEST(InvokeMethodTest, Ternary) {
349 Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT
350 EXPECT_EQ(1124, a.Perform(std::make_tuple(1000, true, Char(1))));
353 // Tests using Invoke() with a 4-argument method.
354 TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
356 Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT
357 EXPECT_EQ(1357, a.Perform(std::make_tuple(1000, 200, 30, 4)));
360 // Tests using Invoke() with a 5-argument method.
361 TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
363 Action<int(int, int, int, int, int)> a =
364 Invoke(&foo, &Foo::SumOf5); // NOLINT
365 EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
368 // Tests using Invoke() with a 6-argument method.
369 TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
371 Action<int(int, int, int, int, int, int)> a = // NOLINT
372 Invoke(&foo, &Foo::SumOf6);
374 a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
377 // Tests using Invoke() with a 7-argument method.
378 TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
380 Action<std::string(const char*, const char*, const char*, const char*,
381 const char*, const char*, const char*)>
382 a = Invoke(&foo, &Foo::Concat7);
384 a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
385 CharPtr("4"), CharPtr("5"), CharPtr("6"),
389 // Tests using Invoke() with a 8-argument method.
390 TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
392 Action<std::string(const char*, const char*, const char*, const char*,
393 const char*, const char*, const char*, const char*)>
394 a = Invoke(&foo, &Foo::Concat8);
395 EXPECT_EQ("12345678",
396 a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
397 CharPtr("4"), CharPtr("5"), CharPtr("6"),
398 CharPtr("7"), CharPtr("8"))));
401 // Tests using Invoke() with a 9-argument method.
402 TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
404 Action<std::string(const char*, const char*, const char*, const char*,
405 const char*, const char*, const char*, const char*,
407 a = Invoke(&foo, &Foo::Concat9);
408 EXPECT_EQ("123456789", a.Perform(std::make_tuple(
409 CharPtr("1"), CharPtr("2"), CharPtr("3"),
410 CharPtr("4"), CharPtr("5"), CharPtr("6"),
411 CharPtr("7"), CharPtr("8"), CharPtr("9"))));
414 // Tests using Invoke() with a 10-argument method.
415 TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
417 Action<std::string(const char*, const char*, const char*, const char*,
418 const char*, const char*, const char*, const char*,
419 const char*, const char*)>
420 a = Invoke(&foo, &Foo::Concat10);
421 EXPECT_EQ("1234567890",
422 a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
423 CharPtr("4"), CharPtr("5"), CharPtr("6"),
424 CharPtr("7"), CharPtr("8"), CharPtr("9"),
428 // Tests using Invoke(f) as an action of a compatible type.
429 TEST(InvokeMethodTest, MethodWithCompatibleType) {
431 Action<long(int, short, char, bool)> a = // NOLINT
432 Invoke(&foo, &Foo::SumOf4);
433 EXPECT_EQ(4444, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
436 // Tests using WithoutArgs with an action that takes no argument.
437 TEST(WithoutArgsTest, NoArg) {
438 Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
439 EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
442 // Tests using WithArg with an action that takes 1 argument.
443 TEST(WithArgTest, OneArg) {
444 Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
445 EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1)));
446 EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1)));
449 TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
450 const Action<int(int)> a = ReturnArg<0>();
451 EXPECT_EQ(5, a.Perform(std::make_tuple(5)));
454 TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
455 const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
456 EXPECT_TRUE(a.Perform(std::make_tuple(true, false, false)));
459 TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
460 const Action<std::string(int, int, std::string, int)> a = ReturnArg<2>();
461 EXPECT_EQ("seven", a.Perform(std::make_tuple(5, 6, std::string("seven"), 8)));
464 TEST(ReturnArgActionTest, WorksForNonConstRefArg0) {
465 const Action<std::string&(std::string&)> a = ReturnArg<0>();
466 std::string s = "12345";
467 EXPECT_EQ(&s, &a.Perform(std::forward_as_tuple(s)));
470 TEST(SaveArgActionTest, WorksForSameType) {
472 const Action<void(int n)> a1 = SaveArg<0>(&result);
473 a1.Perform(std::make_tuple(5));
474 EXPECT_EQ(5, result);
477 TEST(SaveArgActionTest, WorksForCompatibleType) {
479 const Action<void(bool, char)> a1 = SaveArg<1>(&result);
480 a1.Perform(std::make_tuple(true, 'a'));
481 EXPECT_EQ('a', result);
484 TEST(SaveArgPointeeActionTest, WorksForSameType) {
487 const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
488 a1.Perform(std::make_tuple(&value));
489 EXPECT_EQ(5, result);
492 TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
495 const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
496 a1.Perform(std::make_tuple(true, &value));
497 EXPECT_EQ('a', result);
500 TEST(SetArgRefereeActionTest, WorksForSameType) {
502 const Action<void(int&)> a1 = SetArgReferee<0>(1);
503 a1.Perform(std::tuple<int&>(value));
507 TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
509 const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
510 a1.Perform(std::tuple<int, int&>(0, value));
511 EXPECT_EQ('a', value);
514 TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
516 const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
517 a1.Perform(std::tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
518 EXPECT_EQ('a', value);
521 // A class that can be used to verify that its destructor is called: it will set
522 // the bool provided to the constructor to true when destroyed.
523 class DeletionTester {
525 explicit DeletionTester(bool* is_deleted) : is_deleted_(is_deleted) {
526 // Make sure the bit is set to false.
527 *is_deleted_ = false;
530 ~DeletionTester() { *is_deleted_ = true; }
536 TEST(DeleteArgActionTest, OneArg) {
537 bool is_deleted = false;
538 DeletionTester* t = new DeletionTester(&is_deleted);
539 const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT
540 EXPECT_FALSE(is_deleted);
541 a1.Perform(std::make_tuple(t));
542 EXPECT_TRUE(is_deleted);
545 TEST(DeleteArgActionTest, TenArgs) {
546 bool is_deleted = false;
547 DeletionTester* t = new DeletionTester(&is_deleted);
548 const Action<void(bool, int, int, const char*, bool, int, int, int, int,
551 EXPECT_FALSE(is_deleted);
552 a1.Perform(std::make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
553 EXPECT_TRUE(is_deleted);
556 #if GTEST_HAS_EXCEPTIONS
558 TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
559 const Action<void(int n)> a = Throw('a');
560 EXPECT_THROW(a.Perform(std::make_tuple(0)), char);
563 class MyException {};
565 TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
566 const Action<double(char ch)> a = Throw(MyException());
567 EXPECT_THROW(a.Perform(std::make_tuple('0')), MyException);
570 TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
571 const Action<double()> a = Throw(MyException());
572 EXPECT_THROW(a.Perform(std::make_tuple()), MyException);
578 virtual void Func() {}
581 class MockObject : public Object {
583 ~MockObject() override {}
584 MOCK_METHOD(void, Func, (), (override));
587 TEST(ThrowActionTest, Times0) {
588 EXPECT_NONFATAL_FAILURE(
592 ON_CALL(m, Func()).WillByDefault([] { throw "something"; });
593 EXPECT_CALL(m, Func()).Times(0);
596 // Exception is caught but Times(0) still triggers a failure.
602 #endif // GTEST_HAS_EXCEPTIONS
604 // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
605 // pointed to by the N-th (0-based) argument to values in range [first, last).
606 TEST(SetArrayArgumentTest, SetsTheNthArray) {
607 using MyFunction = void(bool, int*, char*);
608 int numbers[] = {1, 2, 3};
609 Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
615 a.Perform(std::make_tuple(true, pn, pch));
620 EXPECT_EQ('\0', ch[0]);
621 EXPECT_EQ('\0', ch[1]);
622 EXPECT_EQ('\0', ch[2]);
623 EXPECT_EQ('\0', ch[3]);
625 // Tests first and last are iterators.
626 std::string letters = "abc";
627 a = SetArrayArgument<2>(letters.begin(), letters.end());
628 std::fill_n(n, 4, 0);
629 std::fill_n(ch, 4, '\0');
630 a.Perform(std::make_tuple(true, pn, pch));
635 EXPECT_EQ('a', ch[0]);
636 EXPECT_EQ('b', ch[1]);
637 EXPECT_EQ('c', ch[2]);
638 EXPECT_EQ('\0', ch[3]);
641 // Tests SetArrayArgument<N>(first, last) where first == last.
642 TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
643 using MyFunction = void(bool, int*);
644 int numbers[] = {1, 2, 3};
645 Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
649 a.Perform(std::make_tuple(true, pn));
656 // Tests SetArrayArgument<N>(first, last) where *first is convertible
657 // (but not equal) to the argument type.
658 TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
659 using MyFunction = void(bool, int*);
660 char chars[] = {97, 98, 99};
661 Action<MyFunction> a = SetArrayArgument<1>(chars, chars + 3);
663 int codes[4] = {111, 222, 333, 444};
665 a.Perform(std::make_tuple(true, pcodes));
666 EXPECT_EQ(97, codes[0]);
667 EXPECT_EQ(98, codes[1]);
668 EXPECT_EQ(99, codes[2]);
669 EXPECT_EQ(444, codes[3]);
672 // Test SetArrayArgument<N>(first, last) with iterator as argument.
673 TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
674 using MyFunction = void(bool, std::back_insert_iterator<std::string>);
675 std::string letters = "abc";
676 Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
679 a.Perform(std::make_tuple(true, back_inserter(s)));
680 EXPECT_EQ(letters, s);
683 TEST(ReturnPointeeTest, Works) {
685 const Action<int()> a = ReturnPointee(&n);
686 EXPECT_EQ(42, a.Perform(std::make_tuple()));
689 EXPECT_EQ(43, a.Perform(std::make_tuple()));
692 // Tests InvokeArgument<N>(...).
694 // Tests using InvokeArgument with a nullary function.
695 TEST(InvokeArgumentTest, Function0) {
696 Action<int(int, int (*)())> a = InvokeArgument<1>(); // NOLINT
697 EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
700 // Tests using InvokeArgument with a unary function.
701 TEST(InvokeArgumentTest, Functor1) {
702 Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
703 EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
706 // Tests using InvokeArgument with a 5-ary function.
707 TEST(InvokeArgumentTest, Function5) {
708 Action<int(int (*)(int, int, int, int, int))> a = // NOLINT
709 InvokeArgument<0>(10000, 2000, 300, 40, 5);
710 EXPECT_EQ(12345, a.Perform(std::make_tuple(&SumOf5)));
713 // Tests using InvokeArgument with a 5-ary functor.
714 TEST(InvokeArgumentTest, Functor5) {
715 Action<int(SumOf5Functor)> a = // NOLINT
716 InvokeArgument<0>(10000, 2000, 300, 40, 5);
717 EXPECT_EQ(12345, a.Perform(std::make_tuple(SumOf5Functor())));
720 // Tests using InvokeArgument with a 6-ary function.
721 TEST(InvokeArgumentTest, Function6) {
722 Action<int(int (*)(int, int, int, int, int, int))> a = // NOLINT
723 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
724 EXPECT_EQ(123456, a.Perform(std::make_tuple(&SumOf6)));
727 // Tests using InvokeArgument with a 6-ary functor.
728 TEST(InvokeArgumentTest, Functor6) {
729 Action<int(SumOf6Functor)> a = // NOLINT
730 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
731 EXPECT_EQ(123456, a.Perform(std::make_tuple(SumOf6Functor())));
734 // Tests using InvokeArgument with a 7-ary function.
735 TEST(InvokeArgumentTest, Function7) {
736 Action<std::string(std::string(*)(const char*, const char*, const char*,
737 const char*, const char*, const char*,
739 a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
740 EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
743 // Tests using InvokeArgument with a 8-ary function.
744 TEST(InvokeArgumentTest, Function8) {
745 Action<std::string(std::string(*)(const char*, const char*, const char*,
746 const char*, const char*, const char*,
747 const char*, const char*))>
748 a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
749 EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
752 // Tests using InvokeArgument with a 9-ary function.
753 TEST(InvokeArgumentTest, Function9) {
754 Action<std::string(std::string(*)(const char*, const char*, const char*,
755 const char*, const char*, const char*,
756 const char*, const char*, const char*))>
757 a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
758 EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
761 // Tests using InvokeArgument with a 10-ary function.
762 TEST(InvokeArgumentTest, Function10) {
763 Action<std::string(std::string(*)(
764 const char*, const char*, const char*, const char*, const char*,
765 const char*, const char*, const char*, const char*, const char*))>
766 a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
767 EXPECT_EQ("1234567890", a.Perform(std::make_tuple(&Concat10)));
770 // Tests using InvokeArgument with a function that takes a pointer argument.
771 TEST(InvokeArgumentTest, ByPointerFunction) {
772 Action<const char*(const char* (*)(const char* input, short n))> // NOLINT
773 a = InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
774 EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
777 // Tests using InvokeArgument with a function that takes a const char*
778 // by passing it a C-string literal.
779 TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
780 Action<const char*(const char* (*)(const char* input, short n))> // NOLINT
781 a = InvokeArgument<0>("Hi", Short(1));
782 EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
785 // Tests using InvokeArgument with a function that takes a const reference.
786 TEST(InvokeArgumentTest, ByConstReferenceFunction) {
787 Action<bool(bool (*function)(const std::string& s))> a = // NOLINT
788 InvokeArgument<0>(std::string("Hi"));
789 // When action 'a' is constructed, it makes a copy of the temporary
790 // string object passed to it, so it's OK to use 'a' later, when the
791 // temporary object has already died.
792 EXPECT_TRUE(a.Perform(std::make_tuple(&ByConstRef)));
795 // Tests using InvokeArgument with ByRef() and a function that takes a
797 TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
798 Action<bool(bool (*)(const double& x))> a = // NOLINT
799 InvokeArgument<0>(ByRef(g_double));
800 // The above line calls ByRef() on a const value.
801 EXPECT_TRUE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
804 a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
805 EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
808 // Tests DoAll(a1, a2).
809 TEST(DoAllTest, TwoActions) {
811 Action<int(int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
813 EXPECT_EQ(2, a.Perform(std::make_tuple(&n)));
817 // Tests DoAll(a1, a2, a3).
818 TEST(DoAllTest, ThreeActions) {
820 Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
821 SetArgPointee<1>(2), Return(3));
822 EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n)));
827 // Tests DoAll(a1, a2, a3, a4).
828 TEST(DoAllTest, FourActions) {
831 Action<int(int*, int*, char*)> a = // NOLINT
832 DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
834 EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n, &ch)));
840 // Tests DoAll(a1, a2, a3, a4, a5).
841 TEST(DoAllTest, FiveActions) {
843 char a = '\0', b = '\0';
844 Action<int(int*, int*, char*, char*)> action = // NOLINT
845 DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
846 SetArgPointee<3>('b'), Return(3));
847 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b)));
854 // Tests DoAll(a1, a2, ..., a6).
855 TEST(DoAllTest, SixActions) {
857 char a = '\0', b = '\0', c = '\0';
858 Action<int(int*, int*, char*, char*, char*)> action = // NOLINT
859 DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
860 SetArgPointee<3>('b'), SetArgPointee<4>('c'), Return(3));
861 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c)));
869 // Tests DoAll(a1, a2, ..., a7).
870 TEST(DoAllTest, SevenActions) {
872 char a = '\0', b = '\0', c = '\0', d = '\0';
873 Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT
874 DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
875 SetArgPointee<3>('b'), SetArgPointee<4>('c'), SetArgPointee<5>('d'),
877 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d)));
886 // Tests DoAll(a1, a2, ..., a8).
887 TEST(DoAllTest, EightActions) {
889 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
890 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
893 DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
894 SetArgPointee<3>('b'), SetArgPointee<4>('c'),
895 SetArgPointee<5>('d'), SetArgPointee<6>('e'), Return(3));
896 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e)));
906 // Tests DoAll(a1, a2, ..., a9).
907 TEST(DoAllTest, NineActions) {
909 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
910 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
912 action = DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2),
913 SetArgPointee<2>('a'), SetArgPointee<3>('b'),
914 SetArgPointee<4>('c'), SetArgPointee<5>('d'),
915 SetArgPointee<6>('e'), SetArgPointee<7>('f'), Return(3));
916 EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
927 // Tests DoAll(a1, a2, ..., a10).
928 TEST(DoAllTest, TenActions) {
930 char a = '\0', b = '\0', c = '\0', d = '\0';
931 char e = '\0', f = '\0', g = '\0';
932 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
933 char*, char*, char*)>
935 DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
936 SetArgPointee<3>('b'), SetArgPointee<4>('c'),
937 SetArgPointee<5>('d'), SetArgPointee<6>('e'),
938 SetArgPointee<7>('f'), SetArgPointee<8>('g'), Return(3));
940 3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
952 TEST(DoAllTest, NoArgs) {
953 bool ran_first = false;
955 DoAll([&] { ran_first = true; }, [&] { return ran_first; });
956 EXPECT_TRUE(a.Perform({}));
959 TEST(DoAllTest, MoveOnlyArgs) {
960 bool ran_first = false;
961 Action<int(std::unique_ptr<int>)> a =
962 DoAll(InvokeWithoutArgs([&] { ran_first = true; }),
963 [](std::unique_ptr<int> p) { return *p; });
964 EXPECT_EQ(7, a.Perform(std::make_tuple(std::unique_ptr<int>(new int(7)))));
965 EXPECT_TRUE(ran_first);
968 TEST(DoAllTest, ImplicitlyConvertsActionArguments) {
969 bool ran_first = false;
970 // Action<void(std::vector<int>)> isn't an
971 // Action<void(const std::vector<int>&) but can be converted.
972 Action<void(std::vector<int>)> first = [&] { ran_first = true; };
973 Action<int(std::vector<int>)> a =
974 DoAll(first, [](std::vector<int> arg) { return arg.front(); });
975 EXPECT_EQ(7, a.Perform(std::make_tuple(std::vector<int>{7})));
976 EXPECT_TRUE(ran_first);
979 // The ACTION*() macros trigger warning C4100 (unreferenced formal
980 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
981 // the macro definition, as the warnings are generated when the macro
982 // is expanded and macro expansion cannot contain #pragma. Therefore
983 // we suppress them here.
984 // Also suppress C4503 decorated name length exceeded, name was truncated
986 #pragma warning(push)
987 #pragma warning(disable : 4100)
988 #pragma warning(disable : 4503)
990 // Tests the ACTION*() macro family.
992 // Tests that ACTION() can define an action that doesn't reference the
993 // mock function arguments.
994 ACTION(Return5) { return 5; }
996 TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
997 Action<double()> a1 = Return5();
998 EXPECT_DOUBLE_EQ(5, a1.Perform(std::make_tuple()));
1000 Action<int(double, bool)> a2 = Return5();
1001 EXPECT_EQ(5, a2.Perform(std::make_tuple(1, true)));
1004 // Tests that ACTION() can define an action that returns void.
1005 ACTION(IncrementArg1) { (*arg1)++; }
1007 TEST(ActionMacroTest, WorksWhenReturningVoid) {
1008 Action<void(int, int*)> a1 = IncrementArg1();
1010 a1.Perform(std::make_tuple(5, &n));
1014 // Tests that the body of ACTION() can reference the type of the
1016 ACTION(IncrementArg2) {
1017 StaticAssertTypeEq<int*, arg2_type>();
1018 arg2_type temp = arg2;
1022 TEST(ActionMacroTest, CanReferenceArgumentType) {
1023 Action<void(int, bool, int*)> a1 = IncrementArg2();
1025 a1.Perform(std::make_tuple(5, false, &n));
1029 // Tests that the body of ACTION() can reference the argument tuple
1030 // via args_type and args.
1032 StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
1033 args_type args_copy = args;
1034 return std::get<0>(args_copy) + std::get<1>(args_copy);
1037 TEST(ActionMacroTest, CanReferenceArgumentTuple) {
1038 Action<int(int, char, int*)> a1 = Sum2();
1040 EXPECT_EQ(11, a1.Perform(std::make_tuple(5, Char(6), &dummy)));
1045 // Tests that the body of ACTION() can reference the mock function
1047 int Dummy(bool flag) { return flag ? 1 : 0; }
1051 ACTION(InvokeDummy) {
1052 StaticAssertTypeEq<int(bool), function_type>();
1053 function_type* fp = &Dummy;
1057 TEST(ActionMacroTest, CanReferenceMockFunctionType) {
1058 Action<int(bool)> a1 = InvokeDummy();
1059 EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
1060 EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
1063 // Tests that the body of ACTION() can reference the mock function's
1065 ACTION(InvokeDummy2) {
1066 StaticAssertTypeEq<int, return_type>();
1067 return_type result = Dummy(true);
1071 TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
1072 Action<int(bool)> a1 = InvokeDummy2();
1073 EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
1074 EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
1077 // Tests that ACTION() works for arguments passed by const reference.
1078 ACTION(ReturnAddrOfConstBoolReferenceArg) {
1079 StaticAssertTypeEq<const bool&, arg1_type>();
1083 TEST(ActionMacroTest, WorksForConstReferenceArg) {
1084 Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
1085 const bool b = false;
1086 EXPECT_EQ(&b, a.Perform(std::tuple<int, const bool&>(0, b)));
1089 // Tests that ACTION() works for arguments passed by non-const reference.
1090 ACTION(ReturnAddrOfIntReferenceArg) {
1091 StaticAssertTypeEq<int&, arg0_type>();
1095 TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
1096 Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
1098 EXPECT_EQ(&n, a.Perform(std::tuple<int&, bool, int>(n, true, 1)));
1101 // Tests that ACTION() can be used in a namespace.
1102 namespace action_test {
1103 ACTION(Sum) { return arg0 + arg1; }
1104 } // namespace action_test
1106 TEST(ActionMacroTest, WorksInNamespace) {
1107 Action<int(int, int)> a1 = action_test::Sum();
1108 EXPECT_EQ(3, a1.Perform(std::make_tuple(1, 2)));
1111 // Tests that the same ACTION definition works for mock functions with
1112 // different argument numbers.
1113 ACTION(PlusTwo) { return arg0 + 2; }
1115 TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
1116 Action<int(int)> a1 = PlusTwo();
1117 EXPECT_EQ(4, a1.Perform(std::make_tuple(2)));
1119 Action<double(float, void*)> a2 = PlusTwo();
1121 EXPECT_DOUBLE_EQ(6, a2.Perform(std::make_tuple(4.0f, &dummy)));
1124 // Tests that ACTION_P can define a parameterized action.
1125 ACTION_P(Plus, n) { return arg0 + n; }
1127 TEST(ActionPMacroTest, DefinesParameterizedAction) {
1128 Action<int(int m, bool t)> a1 = Plus(9);
1129 EXPECT_EQ(10, a1.Perform(std::make_tuple(1, true)));
1132 // Tests that the body of ACTION_P can reference the argument types
1133 // and the parameter type.
1134 ACTION_P(TypedPlus, n) {
1135 arg0_type t1 = arg0;
1140 TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
1141 Action<int(char m, bool t)> a1 = TypedPlus(9);
1142 EXPECT_EQ(10, a1.Perform(std::make_tuple(Char(1), true)));
1145 // Tests that a parameterized action can be used in any mock function
1146 // whose type is compatible.
1147 TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
1148 Action<std::string(const std::string& s)> a1 = Plus("tail");
1149 const std::string re = "re";
1150 std::tuple<const std::string> dummy = std::make_tuple(re);
1151 EXPECT_EQ("retail", a1.Perform(dummy));
1154 // Tests that we can use ACTION*() to define actions overloaded on the
1155 // number of parameters.
1157 ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
1159 ACTION_P(OverloadedAction, default_value) {
1160 return arg0 ? arg1 : default_value;
1163 ACTION_P2(OverloadedAction, true_value, false_value) {
1164 return arg0 ? true_value : false_value;
1167 TEST(ActionMacroTest, CanDefineOverloadedActions) {
1168 using MyAction = Action<const char*(bool, const char*)>;
1170 const MyAction a1 = OverloadedAction();
1171 EXPECT_STREQ("hello", a1.Perform(std::make_tuple(false, CharPtr("world"))));
1172 EXPECT_STREQ("world", a1.Perform(std::make_tuple(true, CharPtr("world"))));
1174 const MyAction a2 = OverloadedAction("hi");
1175 EXPECT_STREQ("hi", a2.Perform(std::make_tuple(false, CharPtr("world"))));
1176 EXPECT_STREQ("world", a2.Perform(std::make_tuple(true, CharPtr("world"))));
1178 const MyAction a3 = OverloadedAction("hi", "you");
1179 EXPECT_STREQ("hi", a3.Perform(std::make_tuple(true, CharPtr("world"))));
1180 EXPECT_STREQ("you", a3.Perform(std::make_tuple(false, CharPtr("world"))));
1183 // Tests ACTION_Pn where n >= 3.
1185 ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
1187 TEST(ActionPnMacroTest, WorksFor3Parameters) {
1188 Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
1189 EXPECT_DOUBLE_EQ(3123.4, a1.Perform(std::make_tuple(3000, true)));
1191 Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
1192 const std::string re = "re";
1193 std::tuple<const std::string> dummy = std::make_tuple(re);
1194 EXPECT_EQ("retail->", a2.Perform(dummy));
1197 ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
1199 TEST(ActionPnMacroTest, WorksFor4Parameters) {
1200 Action<int(int)> a1 = Plus(1, 2, 3, 4);
1201 EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(std::make_tuple(10)));
1204 ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
1206 TEST(ActionPnMacroTest, WorksFor5Parameters) {
1207 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
1208 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(std::make_tuple(10)));
1211 ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
1212 return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
1215 TEST(ActionPnMacroTest, WorksFor6Parameters) {
1216 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
1217 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(std::make_tuple(10)));
1220 ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
1221 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
1224 TEST(ActionPnMacroTest, WorksFor7Parameters) {
1225 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
1226 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(std::make_tuple(10)));
1229 ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
1230 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
1233 TEST(ActionPnMacroTest, WorksFor8Parameters) {
1234 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
1235 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
1236 a1.Perform(std::make_tuple(10)));
1239 ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
1240 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
1243 TEST(ActionPnMacroTest, WorksFor9Parameters) {
1244 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
1245 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
1246 a1.Perform(std::make_tuple(10)));
1249 ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
1250 arg0_type t0 = arg0;
1251 last_param_type t9 = last_param;
1252 return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
1255 TEST(ActionPnMacroTest, WorksFor10Parameters) {
1256 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1257 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
1258 a1.Perform(std::make_tuple(10)));
1261 // Tests that the action body can promote the parameter types.
1263 ACTION_P2(PadArgument, prefix, suffix) {
1264 // The following lines promote the two parameters to desired types.
1265 std::string prefix_str(prefix);
1266 char suffix_char = static_cast<char>(suffix);
1267 return prefix_str + arg0 + suffix_char;
1270 TEST(ActionPnMacroTest, SimpleTypePromotion) {
1271 Action<std::string(const char*)> no_promo =
1272 PadArgument(std::string("foo"), 'r');
1273 Action<std::string(const char*)> promo =
1274 PadArgument("foo", static_cast<int>('r'));
1275 EXPECT_EQ("foobar", no_promo.Perform(std::make_tuple(CharPtr("ba"))));
1276 EXPECT_EQ("foobar", promo.Perform(std::make_tuple(CharPtr("ba"))));
1279 // Tests that we can partially restrict parameter types using a
1280 // straight-forward pattern.
1282 // Defines a generic action that doesn't restrict the types of its
1284 ACTION_P3(ConcatImpl, a, b, c) {
1285 std::stringstream ss;
1290 // Next, we try to restrict that either the first parameter is a
1291 // string, or the second parameter is an int.
1293 // Defines a partially specialized wrapper that restricts the first
1294 // parameter to std::string.
1295 template <typename T1, typename T2>
1296 // ConcatImplActionP3 is the class template ACTION_P3 uses to
1297 // implement ConcatImpl. We shouldn't change the name as this
1298 // pattern requires the user to use it directly.
1299 ConcatImplActionP3<std::string, T1, T2> Concat(const std::string& a, T1 b,
1301 GTEST_INTENTIONAL_CONST_COND_PUSH_()
1303 GTEST_INTENTIONAL_CONST_COND_POP_()
1304 // This branch verifies that ConcatImpl() can be invoked without
1305 // explicit template arguments.
1306 return ConcatImpl(a, b, c);
1308 // This branch verifies that ConcatImpl() can also be invoked with
1309 // explicit template arguments. It doesn't really need to be
1310 // executed as this is a compile-time verification.
1311 return ConcatImpl<std::string, T1, T2>(a, b, c);
1315 // Defines another partially specialized wrapper that restricts the
1316 // second parameter to int.
1317 template <typename T1, typename T2>
1318 ConcatImplActionP3<T1, int, T2> Concat(T1 a, int b, T2 c) {
1319 return ConcatImpl(a, b, c);
1322 TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
1323 Action<const std::string()> a1 = Concat("Hello", "1", 2);
1324 EXPECT_EQ("Hello12", a1.Perform(std::make_tuple()));
1326 a1 = Concat(1, 2, 3);
1327 EXPECT_EQ("123", a1.Perform(std::make_tuple()));
1330 // Verifies the type of an ACTION*.
1333 ACTION_P(DoFoo, p) {}
1334 ACTION_P2(DoFoo, p0, p1) {}
1336 TEST(ActionPnMacroTest, TypesAreCorrect) {
1337 // DoFoo() must be assignable to a DoFooAction variable.
1338 DoFooAction a0 = DoFoo();
1340 // DoFoo(1) must be assignable to a DoFooActionP variable.
1341 DoFooActionP<int> a1 = DoFoo(1);
1343 // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
1344 // variable, and so on.
1345 DoFooActionP2<int, char> a2 = DoFoo(1, '2');
1346 PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
1347 PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
1348 PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
1349 PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
1350 PlusActionP7<int, int, int, int, int, int, char> a7 =
1351 Plus(1, 2, 3, 4, 5, 6, '7');
1352 PlusActionP8<int, int, int, int, int, int, int, char> a8 =
1353 Plus(1, 2, 3, 4, 5, 6, 7, '8');
1354 PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
1355 Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
1356 PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
1357 Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1359 // Avoid "unused variable" warnings.
1373 // Tests that an ACTION_P*() action can be explicitly instantiated
1374 // with reference-typed parameters.
1376 ACTION_P(Plus1, x) { return x; }
1377 ACTION_P2(Plus2, x, y) { return x + y; }
1378 ACTION_P3(Plus3, x, y, z) { return x + y + z; }
1379 ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
1380 return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
1383 TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
1384 int x = 1, y = 2, z = 3;
1385 const std::tuple<> empty = std::make_tuple();
1387 Action<int()> a = Plus1<int&>(x);
1388 EXPECT_EQ(1, a.Perform(empty));
1390 a = Plus2<const int&, int&>(x, y);
1391 EXPECT_EQ(3, a.Perform(empty));
1393 a = Plus3<int&, const int&, int&>(x, y, z);
1394 EXPECT_EQ(6, a.Perform(empty));
1396 int n[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
1397 a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
1398 int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6],
1400 EXPECT_EQ(55, a.Perform(empty));
1403 class TenArgConstructorClass {
1405 TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7,
1406 int a8, int a9, int a10)
1407 : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {}
1411 // Tests that ACTION_TEMPLATE works when there is no value parameter.
1412 ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T),
1413 AND_0_VALUE_PARAMS()) {
1417 TEST(ActionTemplateTest, WorksWithoutValueParam) {
1418 const Action<int*()> a = CreateNew<int>();
1419 int* p = a.Perform(std::make_tuple());
1423 // Tests that ACTION_TEMPLATE works when there are value parameters.
1424 ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T),
1425 AND_1_VALUE_PARAMS(a0)) {
1429 TEST(ActionTemplateTest, WorksWithValueParams) {
1430 const Action<int*()> a = CreateNew<int>(42);
1431 int* p = a.Perform(std::make_tuple());
1436 // Tests that ACTION_TEMPLATE works for integral template parameters.
1437 ACTION_TEMPLATE(MyDeleteArg, HAS_1_TEMPLATE_PARAMS(int, k),
1438 AND_0_VALUE_PARAMS()) {
1439 delete std::get<k>(args);
1442 // Resets a bool variable in the destructor.
1443 class BoolResetter {
1445 explicit BoolResetter(bool* value) : value_(value) {}
1446 ~BoolResetter() { *value_ = false; }
1452 TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
1453 const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
1456 auto* resetter = new BoolResetter(&b);
1457 a.Perform(std::make_tuple(&n, resetter));
1458 EXPECT_FALSE(b); // Verifies that resetter is deleted.
1461 // Tests that ACTION_TEMPLATES works for template template parameters.
1462 ACTION_TEMPLATE(ReturnSmartPointer,
1463 HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
1465 AND_1_VALUE_PARAMS(pointee)) {
1466 return Pointer<pointee_type>(new pointee_type(pointee));
1469 TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
1470 const Action<std::shared_ptr<int>()> a =
1471 ReturnSmartPointer<std::shared_ptr>(42);
1472 std::shared_ptr<int> p = a.Perform(std::make_tuple());
1476 // Tests that ACTION_TEMPLATE works for 10 template parameters.
1477 template <typename T1, typename T2, typename T3, int k4, bool k5,
1478 unsigned int k6, typename T7, typename T8, typename T9>
1479 struct GiantTemplate {
1481 explicit GiantTemplate(int a_value) : value(a_value) {}
1485 ACTION_TEMPLATE(ReturnGiant,
1486 HAS_10_TEMPLATE_PARAMS(typename, T1, typename, T2, typename, T3,
1487 int, k4, bool, k5, unsigned int, k6,
1488 class, T7, class, T8, class, T9,
1489 template <typename T> class, T10),
1490 AND_1_VALUE_PARAMS(value)) {
1491 return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
1494 TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1495 using Giant = GiantTemplate<std::shared_ptr<int>, bool, double, 5, true, 6,
1496 char, unsigned, int>;
1497 const Action<Giant()> a = ReturnGiant<int, bool, double, 5, true, 6, char,
1498 unsigned, int, std::shared_ptr>(42);
1499 Giant giant = a.Perform(std::make_tuple());
1500 EXPECT_EQ(42, giant.value);
1503 // Tests that ACTION_TEMPLATE works for 10 value parameters.
1504 ACTION_TEMPLATE(ReturnSum, HAS_1_TEMPLATE_PARAMS(typename, Number),
1505 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1506 return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1509 TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1510 const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1511 EXPECT_EQ(55, a.Perform(std::make_tuple()));
1514 // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
1515 // on the number of value parameters.
1517 ACTION(ReturnSum) { return 0; }
1519 ACTION_P(ReturnSum, x) { return x; }
1521 ACTION_TEMPLATE(ReturnSum, HAS_1_TEMPLATE_PARAMS(typename, Number),
1522 AND_2_VALUE_PARAMS(v1, v2)) {
1523 return static_cast<Number>(v1) + v2;
1526 ACTION_TEMPLATE(ReturnSum, HAS_1_TEMPLATE_PARAMS(typename, Number),
1527 AND_3_VALUE_PARAMS(v1, v2, v3)) {
1528 return static_cast<Number>(v1) + v2 + v3;
1531 ACTION_TEMPLATE(ReturnSum, HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
1532 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1533 return static_cast<Number>(v1) + v2 + v3 + v4 + k;
1536 TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1537 const Action<int()> a0 = ReturnSum();
1538 const Action<int()> a1 = ReturnSum(1);
1539 const Action<int()> a2 = ReturnSum<int>(1, 2);
1540 const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
1541 const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1542 EXPECT_EQ(0, a0.Perform(std::make_tuple()));
1543 EXPECT_EQ(1, a1.Perform(std::make_tuple()));
1544 EXPECT_EQ(3, a2.Perform(std::make_tuple()));
1545 EXPECT_EQ(6, a3.Perform(std::make_tuple()));
1546 EXPECT_EQ(12345, a4.Perform(std::make_tuple()));
1549 } // namespace gmock_more_actions_test
1550 } // namespace testing