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
=
297 23, a2
.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
300 // Tests using Invoke() with methods with parameters declared as Unused.
301 TEST(InvokeTest
, MethodWithUnusedParameters
) {
303 Action
<int(std::string
, bool, int, int)> a1
= Invoke(&foo
, &Foo::SumOfLast2
);
304 EXPECT_EQ(12, a1
.Perform(std::make_tuple(CharPtr("hi"), true, 10, 2)));
306 Action
<int(char, double, int, int)> a2
=
307 Invoke(&foo
, &Foo::SumOfLast2
);
308 EXPECT_EQ(23, a2
.Perform(std::make_tuple('a', 2.5, 20, 3)));
311 // Tests using Invoke() with a functor.
312 TEST(InvokeTest
, Functor
) {
313 Action
<long(long, int)> a
= Invoke(plus
<long>()); // NOLINT
314 EXPECT_EQ(3L, a
.Perform(std::make_tuple(1, 2)));
317 // Tests using Invoke(f) as an action of a compatible type.
318 TEST(InvokeTest
, FunctionWithCompatibleType
) {
319 Action
<long(int, short, char, bool)> a
= Invoke(SumOf4
); // NOLINT
320 EXPECT_EQ(4321, a
.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
323 // Tests using Invoke() with an object pointer and a method pointer.
325 // Tests using Invoke() with a nullary method.
326 TEST(InvokeMethodTest
, Nullary
) {
328 Action
<int()> a
= Invoke(&foo
, &Foo::Nullary
); // NOLINT
329 EXPECT_EQ(123, a
.Perform(std::make_tuple()));
332 // Tests using Invoke() with a unary method.
333 TEST(InvokeMethodTest
, Unary
) {
335 Action
<short(long)> a
= Invoke(&foo
, &Foo::Unary
); // NOLINT
336 EXPECT_EQ(4123, a
.Perform(std::make_tuple(4000)));
339 // Tests using Invoke() with a binary method.
340 TEST(InvokeMethodTest
, Binary
) {
342 Action
<std::string(const std::string
&, char)> a
= Invoke(&foo
, &Foo::Binary
);
343 std::string
s("Hell");
344 std::tuple
<std::string
, char> dummy
= std::make_tuple(s
, 'o');
345 EXPECT_EQ("Hello", a
.Perform(dummy
));
348 // Tests using Invoke() with a ternary method.
349 TEST(InvokeMethodTest
, Ternary
) {
351 Action
<int(int, bool, char)> a
= Invoke(&foo
, &Foo::Ternary
); // NOLINT
352 EXPECT_EQ(1124, a
.Perform(std::make_tuple(1000, true, Char(1))));
355 // Tests using Invoke() with a 4-argument method.
356 TEST(InvokeMethodTest
, MethodThatTakes4Arguments
) {
358 Action
<int(int, int, int, int)> a
= Invoke(&foo
, &Foo::SumOf4
); // NOLINT
359 EXPECT_EQ(1357, a
.Perform(std::make_tuple(1000, 200, 30, 4)));
362 // Tests using Invoke() with a 5-argument method.
363 TEST(InvokeMethodTest
, MethodThatTakes5Arguments
) {
365 Action
<int(int, int, int, int, int)> a
= Invoke(&foo
, &Foo::SumOf5
); // NOLINT
366 EXPECT_EQ(12345, a
.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
369 // Tests using Invoke() with a 6-argument method.
370 TEST(InvokeMethodTest
, MethodThatTakes6Arguments
) {
372 Action
<int(int, int, int, int, int, int)> a
= // NOLINT
373 Invoke(&foo
, &Foo::SumOf6
);
375 a
.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
378 // Tests using Invoke() with a 7-argument method.
379 TEST(InvokeMethodTest
, MethodThatTakes7Arguments
) {
381 Action
<std::string(const char*, const char*, const char*, const char*,
382 const char*, const char*, const char*)>
383 a
= Invoke(&foo
, &Foo::Concat7
);
385 a
.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
386 CharPtr("4"), CharPtr("5"), CharPtr("6"),
390 // Tests using Invoke() with a 8-argument method.
391 TEST(InvokeMethodTest
, MethodThatTakes8Arguments
) {
393 Action
<std::string(const char*, const char*, const char*, const char*,
394 const char*, const char*, const char*, const char*)>
395 a
= Invoke(&foo
, &Foo::Concat8
);
396 EXPECT_EQ("12345678",
397 a
.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
398 CharPtr("4"), CharPtr("5"), CharPtr("6"),
399 CharPtr("7"), CharPtr("8"))));
402 // Tests using Invoke() with a 9-argument method.
403 TEST(InvokeMethodTest
, MethodThatTakes9Arguments
) {
405 Action
<std::string(const char*, const char*, const char*, const char*,
406 const char*, const char*, const char*, const char*,
408 a
= Invoke(&foo
, &Foo::Concat9
);
409 EXPECT_EQ("123456789", a
.Perform(std::make_tuple(
410 CharPtr("1"), CharPtr("2"), CharPtr("3"),
411 CharPtr("4"), CharPtr("5"), CharPtr("6"),
412 CharPtr("7"), CharPtr("8"), CharPtr("9"))));
415 // Tests using Invoke() with a 10-argument method.
416 TEST(InvokeMethodTest
, MethodThatTakes10Arguments
) {
418 Action
<std::string(const char*, const char*, const char*, const char*,
419 const char*, const char*, const char*, const char*,
420 const char*, const char*)>
421 a
= Invoke(&foo
, &Foo::Concat10
);
422 EXPECT_EQ("1234567890",
423 a
.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
424 CharPtr("4"), CharPtr("5"), CharPtr("6"),
425 CharPtr("7"), CharPtr("8"), CharPtr("9"),
429 // Tests using Invoke(f) as an action of a compatible type.
430 TEST(InvokeMethodTest
, MethodWithCompatibleType
) {
432 Action
<long(int, short, char, bool)> a
= // NOLINT
433 Invoke(&foo
, &Foo::SumOf4
);
434 EXPECT_EQ(4444, a
.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
437 // Tests using WithoutArgs with an action that takes no argument.
438 TEST(WithoutArgsTest
, NoArg
) {
439 Action
<int(int n
)> a
= WithoutArgs(Invoke(Nullary
)); // NOLINT
440 EXPECT_EQ(1, a
.Perform(std::make_tuple(2)));
443 // Tests using WithArg with an action that takes 1 argument.
444 TEST(WithArgTest
, OneArg
) {
445 Action
<bool(double x
, int n
)> b
= WithArg
<1>(Invoke(Unary
)); // NOLINT
446 EXPECT_TRUE(b
.Perform(std::make_tuple(1.5, -1)));
447 EXPECT_FALSE(b
.Perform(std::make_tuple(1.5, 1)));
450 TEST(ReturnArgActionTest
, WorksForOneArgIntArg0
) {
451 const Action
<int(int)> a
= ReturnArg
<0>();
452 EXPECT_EQ(5, a
.Perform(std::make_tuple(5)));
455 TEST(ReturnArgActionTest
, WorksForMultiArgBoolArg0
) {
456 const Action
<bool(bool, bool, bool)> a
= ReturnArg
<0>();
457 EXPECT_TRUE(a
.Perform(std::make_tuple(true, false, false)));
460 TEST(ReturnArgActionTest
, WorksForMultiArgStringArg2
) {
461 const Action
<std::string(int, int, std::string
, int)> a
= ReturnArg
<2>();
462 EXPECT_EQ("seven", a
.Perform(std::make_tuple(5, 6, std::string("seven"), 8)));
465 TEST(SaveArgActionTest
, WorksForSameType
) {
467 const Action
<void(int n
)> a1
= SaveArg
<0>(&result
);
468 a1
.Perform(std::make_tuple(5));
469 EXPECT_EQ(5, result
);
472 TEST(SaveArgActionTest
, WorksForCompatibleType
) {
474 const Action
<void(bool, char)> a1
= SaveArg
<1>(&result
);
475 a1
.Perform(std::make_tuple(true, 'a'));
476 EXPECT_EQ('a', result
);
479 TEST(SaveArgPointeeActionTest
, WorksForSameType
) {
482 const Action
<void(const int*)> a1
= SaveArgPointee
<0>(&result
);
483 a1
.Perform(std::make_tuple(&value
));
484 EXPECT_EQ(5, result
);
487 TEST(SaveArgPointeeActionTest
, WorksForCompatibleType
) {
490 const Action
<void(bool, char*)> a1
= SaveArgPointee
<1>(&result
);
491 a1
.Perform(std::make_tuple(true, &value
));
492 EXPECT_EQ('a', result
);
495 TEST(SetArgRefereeActionTest
, WorksForSameType
) {
497 const Action
<void(int&)> a1
= SetArgReferee
<0>(1);
498 a1
.Perform(std::tuple
<int&>(value
));
502 TEST(SetArgRefereeActionTest
, WorksForCompatibleType
) {
504 const Action
<void(int, int&)> a1
= SetArgReferee
<1>('a');
505 a1
.Perform(std::tuple
<int, int&>(0, value
));
506 EXPECT_EQ('a', value
);
509 TEST(SetArgRefereeActionTest
, WorksWithExtraArguments
) {
511 const Action
<void(bool, int, int&, const char*)> a1
= SetArgReferee
<2>('a');
512 a1
.Perform(std::tuple
<bool, int, int&, const char*>(true, 0, value
, "hi"));
513 EXPECT_EQ('a', value
);
516 // A class that can be used to verify that its destructor is called: it will set
517 // the bool provided to the constructor to true when destroyed.
518 class DeletionTester
{
520 explicit DeletionTester(bool* is_deleted
)
521 : is_deleted_(is_deleted
) {
522 // Make sure the bit is set to false.
523 *is_deleted_
= false;
534 TEST(DeleteArgActionTest
, OneArg
) {
535 bool is_deleted
= false;
536 DeletionTester
* t
= new DeletionTester(&is_deleted
);
537 const Action
<void(DeletionTester
*)> a1
= DeleteArg
<0>(); // NOLINT
538 EXPECT_FALSE(is_deleted
);
539 a1
.Perform(std::make_tuple(t
));
540 EXPECT_TRUE(is_deleted
);
543 TEST(DeleteArgActionTest
, TenArgs
) {
544 bool is_deleted
= false;
545 DeletionTester
* t
= new DeletionTester(&is_deleted
);
546 const Action
<void(bool, int, int, const char*, bool,
547 int, int, int, int, DeletionTester
*)> a1
= DeleteArg
<9>();
548 EXPECT_FALSE(is_deleted
);
549 a1
.Perform(std::make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t
));
550 EXPECT_TRUE(is_deleted
);
553 #if GTEST_HAS_EXCEPTIONS
555 TEST(ThrowActionTest
, ThrowsGivenExceptionInVoidFunction
) {
556 const Action
<void(int n
)> a
= Throw('a');
557 EXPECT_THROW(a
.Perform(std::make_tuple(0)), char);
560 class MyException
{};
562 TEST(ThrowActionTest
, ThrowsGivenExceptionInNonVoidFunction
) {
563 const Action
<double(char ch
)> a
= Throw(MyException());
564 EXPECT_THROW(a
.Perform(std::make_tuple('0')), MyException
);
567 TEST(ThrowActionTest
, ThrowsGivenExceptionInNullaryFunction
) {
568 const Action
<double()> a
= Throw(MyException());
569 EXPECT_THROW(a
.Perform(std::make_tuple()), MyException
);
575 virtual void Func() {}
578 class MockObject
: public Object
{
580 ~MockObject() override
{}
581 MOCK_METHOD(void, Func
, (), (override
));
584 TEST(ThrowActionTest
, Times0
) {
585 EXPECT_NONFATAL_FAILURE(
589 ON_CALL(m
, Func()).WillByDefault([] { throw "something"; });
590 EXPECT_CALL(m
, Func()).Times(0);
593 // Exception is caught but Times(0) still triggers a failure.
599 #endif // GTEST_HAS_EXCEPTIONS
601 // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
602 // pointed to by the N-th (0-based) argument to values in range [first, last).
603 TEST(SetArrayArgumentTest
, SetsTheNthArray
) {
604 using MyFunction
= void(bool, int*, char*);
605 int numbers
[] = { 1, 2, 3 };
606 Action
<MyFunction
> a
= SetArrayArgument
<1>(numbers
, numbers
+ 3);
612 a
.Perform(std::make_tuple(true, pn
, pch
));
617 EXPECT_EQ('\0', ch
[0]);
618 EXPECT_EQ('\0', ch
[1]);
619 EXPECT_EQ('\0', ch
[2]);
620 EXPECT_EQ('\0', ch
[3]);
622 // Tests first and last are iterators.
623 std::string letters
= "abc";
624 a
= SetArrayArgument
<2>(letters
.begin(), letters
.end());
625 std::fill_n(n
, 4, 0);
626 std::fill_n(ch
, 4, '\0');
627 a
.Perform(std::make_tuple(true, pn
, pch
));
632 EXPECT_EQ('a', ch
[0]);
633 EXPECT_EQ('b', ch
[1]);
634 EXPECT_EQ('c', ch
[2]);
635 EXPECT_EQ('\0', ch
[3]);
638 // Tests SetArrayArgument<N>(first, last) where first == last.
639 TEST(SetArrayArgumentTest
, SetsTheNthArrayWithEmptyRange
) {
640 using MyFunction
= void(bool, int*);
641 int numbers
[] = { 1, 2, 3 };
642 Action
<MyFunction
> a
= SetArrayArgument
<1>(numbers
, numbers
);
646 a
.Perform(std::make_tuple(true, pn
));
653 // Tests SetArrayArgument<N>(first, last) where *first is convertible
654 // (but not equal) to the argument type.
655 TEST(SetArrayArgumentTest
, SetsTheNthArrayWithConvertibleType
) {
656 using MyFunction
= void(bool, int*);
657 char chars
[] = { 97, 98, 99 };
658 Action
<MyFunction
> a
= SetArrayArgument
<1>(chars
, chars
+ 3);
660 int codes
[4] = { 111, 222, 333, 444 };
662 a
.Perform(std::make_tuple(true, pcodes
));
663 EXPECT_EQ(97, codes
[0]);
664 EXPECT_EQ(98, codes
[1]);
665 EXPECT_EQ(99, codes
[2]);
666 EXPECT_EQ(444, codes
[3]);
669 // Test SetArrayArgument<N>(first, last) with iterator as argument.
670 TEST(SetArrayArgumentTest
, SetsTheNthArrayWithIteratorArgument
) {
671 using MyFunction
= void(bool, std::back_insert_iterator
<std::string
>);
672 std::string letters
= "abc";
673 Action
<MyFunction
> a
= SetArrayArgument
<1>(letters
.begin(), letters
.end());
676 a
.Perform(std::make_tuple(true, back_inserter(s
)));
677 EXPECT_EQ(letters
, s
);
680 TEST(ReturnPointeeTest
, Works
) {
682 const Action
<int()> a
= ReturnPointee(&n
);
683 EXPECT_EQ(42, a
.Perform(std::make_tuple()));
686 EXPECT_EQ(43, a
.Perform(std::make_tuple()));
689 // Tests InvokeArgument<N>(...).
691 // Tests using InvokeArgument with a nullary function.
692 TEST(InvokeArgumentTest
, Function0
) {
693 Action
<int(int, int (*)())> a
= InvokeArgument
<1>(); // NOLINT
694 EXPECT_EQ(1, a
.Perform(std::make_tuple(2, &Nullary
)));
697 // Tests using InvokeArgument with a unary function.
698 TEST(InvokeArgumentTest
, Functor1
) {
699 Action
<int(UnaryFunctor
)> a
= InvokeArgument
<0>(true); // NOLINT
700 EXPECT_EQ(1, a
.Perform(std::make_tuple(UnaryFunctor())));
703 // Tests using InvokeArgument with a 5-ary function.
704 TEST(InvokeArgumentTest
, Function5
) {
705 Action
<int(int (*)(int, int, int, int, int))> a
= // NOLINT
706 InvokeArgument
<0>(10000, 2000, 300, 40, 5);
707 EXPECT_EQ(12345, a
.Perform(std::make_tuple(&SumOf5
)));
710 // Tests using InvokeArgument with a 5-ary functor.
711 TEST(InvokeArgumentTest
, Functor5
) {
712 Action
<int(SumOf5Functor
)> a
= // NOLINT
713 InvokeArgument
<0>(10000, 2000, 300, 40, 5);
714 EXPECT_EQ(12345, a
.Perform(std::make_tuple(SumOf5Functor())));
717 // Tests using InvokeArgument with a 6-ary function.
718 TEST(InvokeArgumentTest
, Function6
) {
719 Action
<int(int (*)(int, int, int, int, int, int))> a
= // NOLINT
720 InvokeArgument
<0>(100000, 20000, 3000, 400, 50, 6);
721 EXPECT_EQ(123456, a
.Perform(std::make_tuple(&SumOf6
)));
724 // Tests using InvokeArgument with a 6-ary functor.
725 TEST(InvokeArgumentTest
, Functor6
) {
726 Action
<int(SumOf6Functor
)> a
= // NOLINT
727 InvokeArgument
<0>(100000, 20000, 3000, 400, 50, 6);
728 EXPECT_EQ(123456, a
.Perform(std::make_tuple(SumOf6Functor())));
731 // Tests using InvokeArgument with a 7-ary function.
732 TEST(InvokeArgumentTest
, Function7
) {
733 Action
<std::string(std::string(*)(const char*, const char*, const char*,
734 const char*, const char*, const char*,
736 a
= InvokeArgument
<0>("1", "2", "3", "4", "5", "6", "7");
737 EXPECT_EQ("1234567", a
.Perform(std::make_tuple(&Concat7
)));
740 // Tests using InvokeArgument with a 8-ary function.
741 TEST(InvokeArgumentTest
, Function8
) {
742 Action
<std::string(std::string(*)(const char*, const char*, const char*,
743 const char*, const char*, const char*,
744 const char*, const char*))>
745 a
= InvokeArgument
<0>("1", "2", "3", "4", "5", "6", "7", "8");
746 EXPECT_EQ("12345678", a
.Perform(std::make_tuple(&Concat8
)));
749 // Tests using InvokeArgument with a 9-ary function.
750 TEST(InvokeArgumentTest
, Function9
) {
751 Action
<std::string(std::string(*)(const char*, const char*, const char*,
752 const char*, const char*, const char*,
753 const char*, const char*, const char*))>
754 a
= InvokeArgument
<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
755 EXPECT_EQ("123456789", a
.Perform(std::make_tuple(&Concat9
)));
758 // Tests using InvokeArgument with a 10-ary function.
759 TEST(InvokeArgumentTest
, Function10
) {
760 Action
<std::string(std::string(*)(
761 const char*, const char*, const char*, const char*, const char*,
762 const char*, const char*, const char*, const char*, const char*))>
763 a
= InvokeArgument
<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
764 EXPECT_EQ("1234567890", a
.Perform(std::make_tuple(&Concat10
)));
767 // Tests using InvokeArgument with a function that takes a pointer argument.
768 TEST(InvokeArgumentTest
, ByPointerFunction
) {
769 Action
<const char*(const char* (*)(const char* input
, short n
))> // NOLINT
770 a
= InvokeArgument
<0>(static_cast<const char*>("Hi"), Short(1));
771 EXPECT_STREQ("i", a
.Perform(std::make_tuple(&Binary
)));
774 // Tests using InvokeArgument with a function that takes a const char*
775 // by passing it a C-string literal.
776 TEST(InvokeArgumentTest
, FunctionWithCStringLiteral
) {
777 Action
<const char*(const char* (*)(const char* input
, short n
))> // NOLINT
778 a
= InvokeArgument
<0>("Hi", Short(1));
779 EXPECT_STREQ("i", a
.Perform(std::make_tuple(&Binary
)));
782 // Tests using InvokeArgument with a function that takes a const reference.
783 TEST(InvokeArgumentTest
, ByConstReferenceFunction
) {
784 Action
<bool(bool (*function
)(const std::string
& s
))> a
= // NOLINT
785 InvokeArgument
<0>(std::string("Hi"));
786 // When action 'a' is constructed, it makes a copy of the temporary
787 // string object passed to it, so it's OK to use 'a' later, when the
788 // temporary object has already died.
789 EXPECT_TRUE(a
.Perform(std::make_tuple(&ByConstRef
)));
792 // Tests using InvokeArgument with ByRef() and a function that takes a
794 TEST(InvokeArgumentTest
, ByExplicitConstReferenceFunction
) {
795 Action
<bool(bool (*)(const double& x
))> a
= // NOLINT
796 InvokeArgument
<0>(ByRef(g_double
));
797 // The above line calls ByRef() on a const value.
798 EXPECT_TRUE(a
.Perform(std::make_tuple(&ReferencesGlobalDouble
)));
801 a
= InvokeArgument
<0>(ByRef(x
)); // This calls ByRef() on a non-const.
802 EXPECT_FALSE(a
.Perform(std::make_tuple(&ReferencesGlobalDouble
)));
805 // Tests DoAll(a1, a2).
806 TEST(DoAllTest
, TwoActions
) {
808 Action
<int(int*)> a
= DoAll(SetArgPointee
<0>(1), // NOLINT
810 EXPECT_EQ(2, a
.Perform(std::make_tuple(&n
)));
814 // Tests DoAll(a1, a2, a3).
815 TEST(DoAllTest
, ThreeActions
) {
817 Action
<int(int*, int*)> a
= DoAll(SetArgPointee
<0>(1), // NOLINT
818 SetArgPointee
<1>(2), Return(3));
819 EXPECT_EQ(3, a
.Perform(std::make_tuple(&m
, &n
)));
824 // Tests DoAll(a1, a2, a3, a4).
825 TEST(DoAllTest
, FourActions
) {
828 Action
<int(int*, int*, char*)> a
= // NOLINT
829 DoAll(SetArgPointee
<0>(1), SetArgPointee
<1>(2), SetArgPointee
<2>('a'),
831 EXPECT_EQ(3, a
.Perform(std::make_tuple(&m
, &n
, &ch
)));
837 // Tests DoAll(a1, a2, a3, a4, a5).
838 TEST(DoAllTest
, FiveActions
) {
840 char a
= '\0', b
= '\0';
841 Action
<int(int*, int*, char*, char*)> action
= // NOLINT
842 DoAll(SetArgPointee
<0>(1), SetArgPointee
<1>(2), SetArgPointee
<2>('a'),
843 SetArgPointee
<3>('b'), Return(3));
844 EXPECT_EQ(3, action
.Perform(std::make_tuple(&m
, &n
, &a
, &b
)));
851 // Tests DoAll(a1, a2, ..., a6).
852 TEST(DoAllTest
, SixActions
) {
854 char a
= '\0', b
= '\0', c
= '\0';
855 Action
<int(int*, int*, char*, char*, char*)> action
= // NOLINT
856 DoAll(SetArgPointee
<0>(1), SetArgPointee
<1>(2), SetArgPointee
<2>('a'),
857 SetArgPointee
<3>('b'), SetArgPointee
<4>('c'), Return(3));
858 EXPECT_EQ(3, action
.Perform(std::make_tuple(&m
, &n
, &a
, &b
, &c
)));
866 // Tests DoAll(a1, a2, ..., a7).
867 TEST(DoAllTest
, SevenActions
) {
869 char a
= '\0', b
= '\0', c
= '\0', d
= '\0';
870 Action
<int(int*, int*, char*, char*, char*, char*)> action
= // NOLINT
871 DoAll(SetArgPointee
<0>(1), SetArgPointee
<1>(2), SetArgPointee
<2>('a'),
872 SetArgPointee
<3>('b'), SetArgPointee
<4>('c'), SetArgPointee
<5>('d'),
874 EXPECT_EQ(3, action
.Perform(std::make_tuple(&m
, &n
, &a
, &b
, &c
, &d
)));
883 // Tests DoAll(a1, a2, ..., a8).
884 TEST(DoAllTest
, EightActions
) {
886 char a
= '\0', b
= '\0', c
= '\0', d
= '\0', e
= '\0';
887 Action
<int(int*, int*, char*, char*, char*, char*, // NOLINT
890 DoAll(SetArgPointee
<0>(1), SetArgPointee
<1>(2), SetArgPointee
<2>('a'),
891 SetArgPointee
<3>('b'), SetArgPointee
<4>('c'),
892 SetArgPointee
<5>('d'), SetArgPointee
<6>('e'), Return(3));
893 EXPECT_EQ(3, action
.Perform(std::make_tuple(&m
, &n
, &a
, &b
, &c
, &d
, &e
)));
903 // Tests DoAll(a1, a2, ..., a9).
904 TEST(DoAllTest
, NineActions
) {
906 char a
= '\0', b
= '\0', c
= '\0', d
= '\0', e
= '\0', f
= '\0';
907 Action
<int(int*, int*, char*, char*, char*, char*, // NOLINT
909 action
= DoAll(SetArgPointee
<0>(1), SetArgPointee
<1>(2),
910 SetArgPointee
<2>('a'), SetArgPointee
<3>('b'),
911 SetArgPointee
<4>('c'), SetArgPointee
<5>('d'),
912 SetArgPointee
<6>('e'), SetArgPointee
<7>('f'), Return(3));
913 EXPECT_EQ(3, action
.Perform(std::make_tuple(&m
, &n
, &a
, &b
, &c
, &d
, &e
, &f
)));
924 // Tests DoAll(a1, a2, ..., a10).
925 TEST(DoAllTest
, TenActions
) {
927 char a
= '\0', b
= '\0', c
= '\0', d
= '\0';
928 char e
= '\0', f
= '\0', g
= '\0';
929 Action
<int(int*, int*, char*, char*, char*, char*, // NOLINT
930 char*, char*, char*)>
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'),
935 SetArgPointee
<7>('f'), SetArgPointee
<8>('g'), Return(3));
937 3, action
.Perform(std::make_tuple(&m
, &n
, &a
, &b
, &c
, &d
, &e
, &f
, &g
)));
949 TEST(DoAllTest
, NoArgs
) {
950 bool ran_first
= false;
952 DoAll([&] { ran_first
= true; }, [&] { return ran_first
; });
953 EXPECT_TRUE(a
.Perform({}));
956 TEST(DoAllTest
, MoveOnlyArgs
) {
957 bool ran_first
= false;
958 Action
<int(std::unique_ptr
<int>)> a
=
959 DoAll(InvokeWithoutArgs([&] { ran_first
= true; }),
960 [](std::unique_ptr
<int> p
) { return *p
; });
961 EXPECT_EQ(7, a
.Perform(std::make_tuple(std::unique_ptr
<int>(new int(7)))));
962 EXPECT_TRUE(ran_first
);
965 TEST(DoAllTest
, ImplicitlyConvertsActionArguments
) {
966 bool ran_first
= false;
967 // Action<void(std::vector<int>)> isn't an
968 // Action<void(const std::vector<int>&) but can be converted.
969 Action
<void(std::vector
<int>)> first
= [&] { ran_first
= true; };
970 Action
<int(std::vector
<int>)> a
=
971 DoAll(first
, [](std::vector
<int> arg
) { return arg
.front(); });
972 EXPECT_EQ(7, a
.Perform(std::make_tuple(std::vector
<int>{7})));
973 EXPECT_TRUE(ran_first
);
976 // The ACTION*() macros trigger warning C4100 (unreferenced formal
977 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
978 // the macro definition, as the warnings are generated when the macro
979 // is expanded and macro expansion cannot contain #pragma. Therefore
980 // we suppress them here.
981 // Also suppress C4503 decorated name length exceeded, name was truncated
983 #pragma warning(push)
984 #pragma warning(disable : 4100)
985 #pragma warning(disable : 4503)
987 // Tests the ACTION*() macro family.
989 // Tests that ACTION() can define an action that doesn't reference the
990 // mock function arguments.
991 ACTION(Return5
) { return 5; }
993 TEST(ActionMacroTest
, WorksWhenNotReferencingArguments
) {
994 Action
<double()> a1
= Return5();
995 EXPECT_DOUBLE_EQ(5, a1
.Perform(std::make_tuple()));
997 Action
<int(double, bool)> a2
= Return5();
998 EXPECT_EQ(5, a2
.Perform(std::make_tuple(1, true)));
1001 // Tests that ACTION() can define an action that returns void.
1002 ACTION(IncrementArg1
) { (*arg1
)++; }
1004 TEST(ActionMacroTest
, WorksWhenReturningVoid
) {
1005 Action
<void(int, int*)> a1
= IncrementArg1();
1007 a1
.Perform(std::make_tuple(5, &n
));
1011 // Tests that the body of ACTION() can reference the type of the
1013 ACTION(IncrementArg2
) {
1014 StaticAssertTypeEq
<int*, arg2_type
>();
1015 arg2_type temp
= arg2
;
1019 TEST(ActionMacroTest
, CanReferenceArgumentType
) {
1020 Action
<void(int, bool, int*)> a1
= IncrementArg2();
1022 a1
.Perform(std::make_tuple(5, false, &n
));
1026 // Tests that the body of ACTION() can reference the argument tuple
1027 // via args_type and args.
1029 StaticAssertTypeEq
<std::tuple
<int, char, int*>, args_type
>();
1030 args_type args_copy
= args
;
1031 return std::get
<0>(args_copy
) + std::get
<1>(args_copy
);
1034 TEST(ActionMacroTest
, CanReferenceArgumentTuple
) {
1035 Action
<int(int, char, int*)> a1
= Sum2();
1037 EXPECT_EQ(11, a1
.Perform(std::make_tuple(5, Char(6), &dummy
)));
1042 // Tests that the body of ACTION() can reference the mock function
1044 int Dummy(bool flag
) { return flag
? 1 : 0; }
1048 ACTION(InvokeDummy
) {
1049 StaticAssertTypeEq
<int(bool), function_type
>();
1050 function_type
* fp
= &Dummy
;
1054 TEST(ActionMacroTest
, CanReferenceMockFunctionType
) {
1055 Action
<int(bool)> a1
= InvokeDummy();
1056 EXPECT_EQ(1, a1
.Perform(std::make_tuple(true)));
1057 EXPECT_EQ(1, a1
.Perform(std::make_tuple(false)));
1060 // Tests that the body of ACTION() can reference the mock function's
1062 ACTION(InvokeDummy2
) {
1063 StaticAssertTypeEq
<int, return_type
>();
1064 return_type result
= Dummy(true);
1068 TEST(ActionMacroTest
, CanReferenceMockFunctionReturnType
) {
1069 Action
<int(bool)> a1
= InvokeDummy2();
1070 EXPECT_EQ(1, a1
.Perform(std::make_tuple(true)));
1071 EXPECT_EQ(1, a1
.Perform(std::make_tuple(false)));
1074 // Tests that ACTION() works for arguments passed by const reference.
1075 ACTION(ReturnAddrOfConstBoolReferenceArg
) {
1076 StaticAssertTypeEq
<const bool&, arg1_type
>();
1080 TEST(ActionMacroTest
, WorksForConstReferenceArg
) {
1081 Action
<const bool*(int, const bool&)> a
= ReturnAddrOfConstBoolReferenceArg();
1082 const bool b
= false;
1083 EXPECT_EQ(&b
, a
.Perform(std::tuple
<int, const bool&>(0, b
)));
1086 // Tests that ACTION() works for arguments passed by non-const reference.
1087 ACTION(ReturnAddrOfIntReferenceArg
) {
1088 StaticAssertTypeEq
<int&, arg0_type
>();
1092 TEST(ActionMacroTest
, WorksForNonConstReferenceArg
) {
1093 Action
<int*(int&, bool, int)> a
= ReturnAddrOfIntReferenceArg();
1095 EXPECT_EQ(&n
, a
.Perform(std::tuple
<int&, bool, int>(n
, true, 1)));
1098 // Tests that ACTION() can be used in a namespace.
1099 namespace action_test
{
1100 ACTION(Sum
) { return arg0
+ arg1
; }
1101 } // namespace action_test
1103 TEST(ActionMacroTest
, WorksInNamespace
) {
1104 Action
<int(int, int)> a1
= action_test::Sum();
1105 EXPECT_EQ(3, a1
.Perform(std::make_tuple(1, 2)));
1108 // Tests that the same ACTION definition works for mock functions with
1109 // different argument numbers.
1110 ACTION(PlusTwo
) { return arg0
+ 2; }
1112 TEST(ActionMacroTest
, WorksForDifferentArgumentNumbers
) {
1113 Action
<int(int)> a1
= PlusTwo();
1114 EXPECT_EQ(4, a1
.Perform(std::make_tuple(2)));
1116 Action
<double(float, void*)> a2
= PlusTwo();
1118 EXPECT_DOUBLE_EQ(6, a2
.Perform(std::make_tuple(4.0f
, &dummy
)));
1121 // Tests that ACTION_P can define a parameterized action.
1122 ACTION_P(Plus
, n
) { return arg0
+ n
; }
1124 TEST(ActionPMacroTest
, DefinesParameterizedAction
) {
1125 Action
<int(int m
, bool t
)> a1
= Plus(9);
1126 EXPECT_EQ(10, a1
.Perform(std::make_tuple(1, true)));
1129 // Tests that the body of ACTION_P can reference the argument types
1130 // and the parameter type.
1131 ACTION_P(TypedPlus
, n
) {
1132 arg0_type t1
= arg0
;
1137 TEST(ActionPMacroTest
, CanReferenceArgumentAndParameterTypes
) {
1138 Action
<int(char m
, bool t
)> a1
= TypedPlus(9);
1139 EXPECT_EQ(10, a1
.Perform(std::make_tuple(Char(1), true)));
1142 // Tests that a parameterized action can be used in any mock function
1143 // whose type is compatible.
1144 TEST(ActionPMacroTest
, WorksInCompatibleMockFunction
) {
1145 Action
<std::string(const std::string
& s
)> a1
= Plus("tail");
1146 const std::string re
= "re";
1147 std::tuple
<const std::string
> dummy
= std::make_tuple(re
);
1148 EXPECT_EQ("retail", a1
.Perform(dummy
));
1151 // Tests that we can use ACTION*() to define actions overloaded on the
1152 // number of parameters.
1154 ACTION(OverloadedAction
) { return arg0
? arg1
: "hello"; }
1156 ACTION_P(OverloadedAction
, default_value
) {
1157 return arg0
? arg1
: default_value
;
1160 ACTION_P2(OverloadedAction
, true_value
, false_value
) {
1161 return arg0
? true_value
: false_value
;
1164 TEST(ActionMacroTest
, CanDefineOverloadedActions
) {
1165 using MyAction
= Action
<const char*(bool, const char*)>;
1167 const MyAction a1
= OverloadedAction();
1168 EXPECT_STREQ("hello", a1
.Perform(std::make_tuple(false, CharPtr("world"))));
1169 EXPECT_STREQ("world", a1
.Perform(std::make_tuple(true, CharPtr("world"))));
1171 const MyAction a2
= OverloadedAction("hi");
1172 EXPECT_STREQ("hi", a2
.Perform(std::make_tuple(false, CharPtr("world"))));
1173 EXPECT_STREQ("world", a2
.Perform(std::make_tuple(true, CharPtr("world"))));
1175 const MyAction a3
= OverloadedAction("hi", "you");
1176 EXPECT_STREQ("hi", a3
.Perform(std::make_tuple(true, CharPtr("world"))));
1177 EXPECT_STREQ("you", a3
.Perform(std::make_tuple(false, CharPtr("world"))));
1180 // Tests ACTION_Pn where n >= 3.
1182 ACTION_P3(Plus
, m
, n
, k
) { return arg0
+ m
+ n
+ k
; }
1184 TEST(ActionPnMacroTest
, WorksFor3Parameters
) {
1185 Action
<double(int m
, bool t
)> a1
= Plus(100, 20, 3.4);
1186 EXPECT_DOUBLE_EQ(3123.4, a1
.Perform(std::make_tuple(3000, true)));
1188 Action
<std::string(const std::string
& s
)> a2
= Plus("tail", "-", ">");
1189 const std::string re
= "re";
1190 std::tuple
<const std::string
> dummy
= std::make_tuple(re
);
1191 EXPECT_EQ("retail->", a2
.Perform(dummy
));
1194 ACTION_P4(Plus
, p0
, p1
, p2
, p3
) { return arg0
+ p0
+ p1
+ p2
+ p3
; }
1196 TEST(ActionPnMacroTest
, WorksFor4Parameters
) {
1197 Action
<int(int)> a1
= Plus(1, 2, 3, 4);
1198 EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1
.Perform(std::make_tuple(10)));
1201 ACTION_P5(Plus
, p0
, p1
, p2
, p3
, p4
) { return arg0
+ p0
+ p1
+ p2
+ p3
+ p4
; }
1203 TEST(ActionPnMacroTest
, WorksFor5Parameters
) {
1204 Action
<int(int)> a1
= Plus(1, 2, 3, 4, 5);
1205 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1
.Perform(std::make_tuple(10)));
1208 ACTION_P6(Plus
, p0
, p1
, p2
, p3
, p4
, p5
) {
1209 return arg0
+ p0
+ p1
+ p2
+ p3
+ p4
+ p5
;
1212 TEST(ActionPnMacroTest
, WorksFor6Parameters
) {
1213 Action
<int(int)> a1
= Plus(1, 2, 3, 4, 5, 6);
1214 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1
.Perform(std::make_tuple(10)));
1217 ACTION_P7(Plus
, p0
, p1
, p2
, p3
, p4
, p5
, p6
) {
1218 return arg0
+ p0
+ p1
+ p2
+ p3
+ p4
+ p5
+ p6
;
1221 TEST(ActionPnMacroTest
, WorksFor7Parameters
) {
1222 Action
<int(int)> a1
= Plus(1, 2, 3, 4, 5, 6, 7);
1223 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1
.Perform(std::make_tuple(10)));
1226 ACTION_P8(Plus
, p0
, p1
, p2
, p3
, p4
, p5
, p6
, p7
) {
1227 return arg0
+ p0
+ p1
+ p2
+ p3
+ p4
+ p5
+ p6
+ p7
;
1230 TEST(ActionPnMacroTest
, WorksFor8Parameters
) {
1231 Action
<int(int)> a1
= Plus(1, 2, 3, 4, 5, 6, 7, 8);
1232 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
1233 a1
.Perform(std::make_tuple(10)));
1236 ACTION_P9(Plus
, p0
, p1
, p2
, p3
, p4
, p5
, p6
, p7
, p8
) {
1237 return arg0
+ p0
+ p1
+ p2
+ p3
+ p4
+ p5
+ p6
+ p7
+ p8
;
1240 TEST(ActionPnMacroTest
, WorksFor9Parameters
) {
1241 Action
<int(int)> a1
= Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
1242 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
1243 a1
.Perform(std::make_tuple(10)));
1246 ACTION_P10(Plus
, p0
, p1
, p2
, p3
, p4
, p5
, p6
, p7
, p8
, last_param
) {
1247 arg0_type t0
= arg0
;
1248 last_param_type t9
= last_param
;
1249 return t0
+ p0
+ p1
+ p2
+ p3
+ p4
+ p5
+ p6
+ p7
+ p8
+ t9
;
1252 TEST(ActionPnMacroTest
, WorksFor10Parameters
) {
1253 Action
<int(int)> a1
= Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1254 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
1255 a1
.Perform(std::make_tuple(10)));
1258 // Tests that the action body can promote the parameter types.
1260 ACTION_P2(PadArgument
, prefix
, suffix
) {
1261 // The following lines promote the two parameters to desired types.
1262 std::string
prefix_str(prefix
);
1263 char suffix_char
= static_cast<char>(suffix
);
1264 return prefix_str
+ arg0
+ suffix_char
;
1267 TEST(ActionPnMacroTest
, SimpleTypePromotion
) {
1268 Action
<std::string(const char*)> no_promo
=
1269 PadArgument(std::string("foo"), 'r');
1270 Action
<std::string(const char*)> promo
=
1271 PadArgument("foo", static_cast<int>('r'));
1272 EXPECT_EQ("foobar", no_promo
.Perform(std::make_tuple(CharPtr("ba"))));
1273 EXPECT_EQ("foobar", promo
.Perform(std::make_tuple(CharPtr("ba"))));
1276 // Tests that we can partially restrict parameter types using a
1277 // straight-forward pattern.
1279 // Defines a generic action that doesn't restrict the types of its
1281 ACTION_P3(ConcatImpl
, a
, b
, c
) {
1282 std::stringstream ss
;
1287 // Next, we try to restrict that either the first parameter is a
1288 // string, or the second parameter is an int.
1290 // Defines a partially specialized wrapper that restricts the first
1291 // parameter to std::string.
1292 template <typename T1
, typename T2
>
1293 // ConcatImplActionP3 is the class template ACTION_P3 uses to
1294 // implement ConcatImpl. We shouldn't change the name as this
1295 // pattern requires the user to use it directly.
1296 ConcatImplActionP3
<std::string
, T1
, T2
> Concat(const std::string
& a
, T1 b
,
1298 GTEST_INTENTIONAL_CONST_COND_PUSH_()
1300 GTEST_INTENTIONAL_CONST_COND_POP_()
1301 // This branch verifies that ConcatImpl() can be invoked without
1302 // explicit template arguments.
1303 return ConcatImpl(a
, b
, c
);
1305 // This branch verifies that ConcatImpl() can also be invoked with
1306 // explicit template arguments. It doesn't really need to be
1307 // executed as this is a compile-time verification.
1308 return ConcatImpl
<std::string
, T1
, T2
>(a
, b
, c
);
1312 // Defines another partially specialized wrapper that restricts the
1313 // second parameter to int.
1314 template <typename T1
, typename T2
>
1315 ConcatImplActionP3
<T1
, int, T2
> Concat(T1 a
, int b
, T2 c
) {
1316 return ConcatImpl(a
, b
, c
);
1319 TEST(ActionPnMacroTest
, CanPartiallyRestrictParameterTypes
) {
1320 Action
<const std::string()> a1
= Concat("Hello", "1", 2);
1321 EXPECT_EQ("Hello12", a1
.Perform(std::make_tuple()));
1323 a1
= Concat(1, 2, 3);
1324 EXPECT_EQ("123", a1
.Perform(std::make_tuple()));
1327 // Verifies the type of an ACTION*.
1330 ACTION_P(DoFoo
, p
) {}
1331 ACTION_P2(DoFoo
, p0
, p1
) {}
1333 TEST(ActionPnMacroTest
, TypesAreCorrect
) {
1334 // DoFoo() must be assignable to a DoFooAction variable.
1335 DoFooAction a0
= DoFoo();
1337 // DoFoo(1) must be assignable to a DoFooActionP variable.
1338 DoFooActionP
<int> a1
= DoFoo(1);
1340 // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
1341 // variable, and so on.
1342 DoFooActionP2
<int, char> a2
= DoFoo(1, '2');
1343 PlusActionP3
<int, int, char> a3
= Plus(1, 2, '3');
1344 PlusActionP4
<int, int, int, char> a4
= Plus(1, 2, 3, '4');
1345 PlusActionP5
<int, int, int, int, char> a5
= Plus(1, 2, 3, 4, '5');
1346 PlusActionP6
<int, int, int, int, int, char> a6
= Plus(1, 2, 3, 4, 5, '6');
1347 PlusActionP7
<int, int, int, int, int, int, char> a7
=
1348 Plus(1, 2, 3, 4, 5, 6, '7');
1349 PlusActionP8
<int, int, int, int, int, int, int, char> a8
=
1350 Plus(1, 2, 3, 4, 5, 6, 7, '8');
1351 PlusActionP9
<int, int, int, int, int, int, int, int, char> a9
=
1352 Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
1353 PlusActionP10
<int, int, int, int, int, int, int, int, int, char> a10
=
1354 Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1356 // Avoid "unused variable" warnings.
1370 // Tests that an ACTION_P*() action can be explicitly instantiated
1371 // with reference-typed parameters.
1373 ACTION_P(Plus1
, x
) { return x
; }
1374 ACTION_P2(Plus2
, x
, y
) { return x
+ y
; }
1375 ACTION_P3(Plus3
, x
, y
, z
) { return x
+ y
+ z
; }
1376 ACTION_P10(Plus10
, a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
) {
1377 return a0
+ a1
+ a2
+ a3
+ a4
+ a5
+ a6
+ a7
+ a8
+ a9
;
1380 TEST(ActionPnMacroTest
, CanExplicitlyInstantiateWithReferenceTypes
) {
1381 int x
= 1, y
= 2, z
= 3;
1382 const std::tuple
<> empty
= std::make_tuple();
1384 Action
<int()> a
= Plus1
<int&>(x
);
1385 EXPECT_EQ(1, a
.Perform(empty
));
1387 a
= Plus2
<const int&, int&>(x
, y
);
1388 EXPECT_EQ(3, a
.Perform(empty
));
1390 a
= Plus3
<int&, const int&, int&>(x
, y
, z
);
1391 EXPECT_EQ(6, a
.Perform(empty
));
1393 int n
[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
1394 a
= Plus10
<const int&, int&, const int&, int&, const int&, int&, const int&,
1395 int&, const int&, int&>(n
[0], n
[1], n
[2], n
[3], n
[4], n
[5], n
[6],
1397 EXPECT_EQ(55, a
.Perform(empty
));
1400 class TenArgConstructorClass
{
1402 TenArgConstructorClass(int a1
, int a2
, int a3
, int a4
, int a5
, int a6
, int a7
,
1403 int a8
, int a9
, int a10
)
1404 : value_(a1
+ a2
+ a3
+ a4
+ a5
+ a6
+ a7
+ a8
+ a9
+ a10
) {}
1408 // Tests that ACTION_TEMPLATE works when there is no value parameter.
1409 ACTION_TEMPLATE(CreateNew
, HAS_1_TEMPLATE_PARAMS(typename
, T
),
1410 AND_0_VALUE_PARAMS()) {
1414 TEST(ActionTemplateTest
, WorksWithoutValueParam
) {
1415 const Action
<int*()> a
= CreateNew
<int>();
1416 int* p
= a
.Perform(std::make_tuple());
1420 // Tests that ACTION_TEMPLATE works when there are value parameters.
1421 ACTION_TEMPLATE(CreateNew
, HAS_1_TEMPLATE_PARAMS(typename
, T
),
1422 AND_1_VALUE_PARAMS(a0
)) {
1426 TEST(ActionTemplateTest
, WorksWithValueParams
) {
1427 const Action
<int*()> a
= CreateNew
<int>(42);
1428 int* p
= a
.Perform(std::make_tuple());
1433 // Tests that ACTION_TEMPLATE works for integral template parameters.
1434 ACTION_TEMPLATE(MyDeleteArg
, HAS_1_TEMPLATE_PARAMS(int, k
),
1435 AND_0_VALUE_PARAMS()) {
1436 delete std::get
<k
>(args
);
1439 // Resets a bool variable in the destructor.
1440 class BoolResetter
{
1442 explicit BoolResetter(bool* value
) : value_(value
) {}
1443 ~BoolResetter() { *value_
= false; }
1449 TEST(ActionTemplateTest
, WorksForIntegralTemplateParams
) {
1450 const Action
<void(int*, BoolResetter
*)> a
= MyDeleteArg
<1>();
1453 auto* resetter
= new BoolResetter(&b
);
1454 a
.Perform(std::make_tuple(&n
, resetter
));
1455 EXPECT_FALSE(b
); // Verifies that resetter is deleted.
1458 // Tests that ACTION_TEMPLATES works for template template parameters.
1459 ACTION_TEMPLATE(ReturnSmartPointer
,
1460 HAS_1_TEMPLATE_PARAMS(template <typename Pointee
> class,
1462 AND_1_VALUE_PARAMS(pointee
)) {
1463 return Pointer
<pointee_type
>(new pointee_type(pointee
));
1466 TEST(ActionTemplateTest
, WorksForTemplateTemplateParameters
) {
1467 const Action
<std::shared_ptr
<int>()> a
=
1468 ReturnSmartPointer
<std::shared_ptr
>(42);
1469 std::shared_ptr
<int> p
= a
.Perform(std::make_tuple());
1473 // Tests that ACTION_TEMPLATE works for 10 template parameters.
1474 template <typename T1
, typename T2
, typename T3
, int k4
, bool k5
,
1475 unsigned int k6
, typename T7
, typename T8
, typename T9
>
1476 struct GiantTemplate
{
1478 explicit GiantTemplate(int a_value
) : value(a_value
) {}
1482 ACTION_TEMPLATE(ReturnGiant
,
1483 HAS_10_TEMPLATE_PARAMS(typename
, T1
, typename
, T2
, typename
, T3
,
1484 int, k4
, bool, k5
, unsigned int, k6
,
1485 class, T7
, class, T8
, class, T9
,
1486 template <typename T
> class, T10
),
1487 AND_1_VALUE_PARAMS(value
)) {
1488 return GiantTemplate
<T10
<T1
>, T2
, T3
, k4
, k5
, k6
, T7
, T8
, T9
>(value
);
1491 TEST(ActionTemplateTest
, WorksFor10TemplateParameters
) {
1492 using Giant
= GiantTemplate
<std::shared_ptr
<int>, bool, double, 5, true, 6,
1493 char, unsigned, int>;
1494 const Action
<Giant()> a
= ReturnGiant
<int, bool, double, 5, true, 6, char,
1495 unsigned, int, std::shared_ptr
>(42);
1496 Giant giant
= a
.Perform(std::make_tuple());
1497 EXPECT_EQ(42, giant
.value
);
1500 // Tests that ACTION_TEMPLATE works for 10 value parameters.
1501 ACTION_TEMPLATE(ReturnSum
, HAS_1_TEMPLATE_PARAMS(typename
, Number
),
1502 AND_10_VALUE_PARAMS(v1
, v2
, v3
, v4
, v5
, v6
, v7
, v8
, v9
, v10
)) {
1503 return static_cast<Number
>(v1
) + v2
+ v3
+ v4
+ v5
+ v6
+ v7
+ v8
+ v9
+ v10
;
1506 TEST(ActionTemplateTest
, WorksFor10ValueParameters
) {
1507 const Action
<int()> a
= ReturnSum
<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1508 EXPECT_EQ(55, a
.Perform(std::make_tuple()));
1511 // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
1512 // on the number of value parameters.
1514 ACTION(ReturnSum
) { return 0; }
1516 ACTION_P(ReturnSum
, x
) { return x
; }
1518 ACTION_TEMPLATE(ReturnSum
, HAS_1_TEMPLATE_PARAMS(typename
, Number
),
1519 AND_2_VALUE_PARAMS(v1
, v2
)) {
1520 return static_cast<Number
>(v1
) + v2
;
1523 ACTION_TEMPLATE(ReturnSum
, HAS_1_TEMPLATE_PARAMS(typename
, Number
),
1524 AND_3_VALUE_PARAMS(v1
, v2
, v3
)) {
1525 return static_cast<Number
>(v1
) + v2
+ v3
;
1528 ACTION_TEMPLATE(ReturnSum
, HAS_2_TEMPLATE_PARAMS(typename
, Number
, int, k
),
1529 AND_4_VALUE_PARAMS(v1
, v2
, v3
, v4
)) {
1530 return static_cast<Number
>(v1
) + v2
+ v3
+ v4
+ k
;
1533 TEST(ActionTemplateTest
, CanBeOverloadedOnNumberOfValueParameters
) {
1534 const Action
<int()> a0
= ReturnSum();
1535 const Action
<int()> a1
= ReturnSum(1);
1536 const Action
<int()> a2
= ReturnSum
<int>(1, 2);
1537 const Action
<int()> a3
= ReturnSum
<int>(1, 2, 3);
1538 const Action
<int()> a4
= ReturnSum
<int, 10000>(2000, 300, 40, 5);
1539 EXPECT_EQ(0, a0
.Perform(std::make_tuple()));
1540 EXPECT_EQ(1, a1
.Perform(std::make_tuple()));
1541 EXPECT_EQ(3, a2
.Perform(std::make_tuple()));
1542 EXPECT_EQ(6, a3
.Perform(std::make_tuple()));
1543 EXPECT_EQ(12345, a4
.Perform(std::make_tuple()));
1546 } // namespace gmock_more_actions_test
1547 } // namespace testing