35 #include "gmock/gmock-generated-actions.h"
41 #include "gmock/gmock.h"
42 #include "gtest/gtest.h"
45 namespace gmock_generated_actions_test {
62 inline short Short(
short n) {
return n; }
63 inline char Char(
char ch) {
return ch; }
81 int SumOf5(
int a,
int b,
int c,
int d,
int e) {
return a +
b + c + d + e; }
83 struct SumOf5Functor {
85 return a +
b + c + d + e;
90 const char* s4,
const char* s5) {
94 int SumOf6(
int a,
int b,
int c,
int d,
int e,
int f) {
95 return a +
b + c + d + e + f;
98 struct SumOf6Functor {
100 return a +
b + c + d + e + f;
105 const char* s4,
const char* s5,
const char* s6) {
110 const char* s4,
const char* s5,
const char* s6,
112 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
116 const char* s4,
const char* s5,
const char* s6,
117 const char* s7,
const char*
s8) {
122 const char* s4,
const char* s5,
const char* s6,
123 const char* s7,
const char*
s8,
const char* s9) {
124 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 +
s8 + s9;
128 const char* s4,
const char* s5,
const char* s6,
129 const char* s7,
const char*
s8,
const char* s9,
131 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 +
s8 + s9 + s10;
136 inline const char*
CharPtr(
const char* s) {
return s; }
141 TEST(InvokeArgumentTest, Function0) {
142 Action<
int(
int,
int(*)())>
a = InvokeArgument<1>();
147 TEST(InvokeArgumentTest, Functor1) {
148 Action<
int(UnaryFunctor)>
a = InvokeArgument<0>(
true);
153 TEST(InvokeArgumentTest, Function5) {
154 Action<
int(
int(*)(
int,
int,
int,
int,
int))>
a =
155 InvokeArgument<0>(10000, 2000, 300, 40, 5);
160 TEST(InvokeArgumentTest, Functor5) {
161 Action<
int(SumOf5Functor)>
a =
162 InvokeArgument<0>(10000, 2000, 300, 40, 5);
167 TEST(InvokeArgumentTest, Function6) {
168 Action<
int(
int(*)(
int,
int,
int,
int,
int,
int))>
a =
169 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
174 TEST(InvokeArgumentTest, Functor6) {
175 Action<
int(SumOf6Functor)>
a =
176 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
181 TEST(InvokeArgumentTest, Function7) {
183 const char*,
const char*,
const char*,
185 a = InvokeArgument<0>(
"1",
"2",
"3",
"4",
"5",
"6",
"7");
190 TEST(InvokeArgumentTest, Function8) {
192 const char*,
const char*,
const char*,
193 const char*,
const char*))>
194 a = InvokeArgument<0>(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8");
199 TEST(InvokeArgumentTest, Function9) {
201 const char*,
const char*,
const char*,
202 const char*,
const char*,
const char*))>
203 a = InvokeArgument<0>(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9");
208 TEST(InvokeArgumentTest, Function10) {
210 const char*,
const char*,
const char*,
const char*,
const char*,
211 const char*,
const char*,
const char*,
const char*,
const char*))>
212 a = InvokeArgument<0>(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0");
217 TEST(InvokeArgumentTest, ByPointerFunction) {
218 Action<
const char*(
const char*(*)(
const char*
input,
short n))>
a =
219 InvokeArgument<0>(
static_cast<const char*
>(
"Hi"),
Short(1));
225 TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
226 Action<
const char*(
const char*(*)(
const char*
input,
short n))>
a =
227 InvokeArgument<0>(
"Hi",
Short(1));
232 TEST(InvokeArgumentTest, ByConstReferenceFunction) {
243 TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
244 Action<
bool(
bool(*)(
const double& x))>
a =
250 a = InvokeArgument<0>(
ByRef(x));
255 TEST(DoAllTest, TwoActions) {
257 Action<
int(
int*)>
a =
DoAll(SetArgPointee<0>(1),
264 TEST(DoAllTest, ThreeActions) {
266 Action<
int(
int*,
int*)>
a =
DoAll(SetArgPointee<0>(1),
275 TEST(DoAllTest, FourActions) {
278 Action<
int(
int*,
int*,
char*)>
a =
279 DoAll(SetArgPointee<0>(1),
281 SetArgPointee<2>(
'a'),
290 TEST(DoAllTest, FiveActions) {
292 char a =
'\0',
b =
'\0';
293 Action<
int(
int*,
int*,
char*,
char*)>
action =
294 DoAll(SetArgPointee<0>(1),
296 SetArgPointee<2>(
'a'),
297 SetArgPointee<3>(
'b'),
307 TEST(DoAllTest, SixActions) {
309 char a =
'\0',
b =
'\0',
c =
'\0';
310 Action<
int(
int*,
int*,
char*,
char*,
char*)>
action =
311 DoAll(SetArgPointee<0>(1),
313 SetArgPointee<2>(
'a'),
314 SetArgPointee<3>(
'b'),
315 SetArgPointee<4>(
'c'),
326 TEST(DoAllTest, SevenActions) {
328 char a =
'\0',
b =
'\0',
c =
'\0',
d =
'\0';
329 Action<
int(
int*,
int*,
char*,
char*,
char*,
char*)>
action =
330 DoAll(SetArgPointee<0>(1),
332 SetArgPointee<2>(
'a'),
333 SetArgPointee<3>(
'b'),
334 SetArgPointee<4>(
'c'),
335 SetArgPointee<5>(
'd'),
347 TEST(DoAllTest, EightActions) {
349 char a =
'\0',
b =
'\0',
c =
'\0',
d =
'\0',
e =
'\0';
350 Action<
int(
int*,
int*,
char*,
char*,
char*,
char*,
352 DoAll(SetArgPointee<0>(1),
354 SetArgPointee<2>(
'a'),
355 SetArgPointee<3>(
'b'),
356 SetArgPointee<4>(
'c'),
357 SetArgPointee<5>(
'd'),
358 SetArgPointee<6>(
'e'),
371 TEST(DoAllTest, NineActions) {
373 char a =
'\0',
b =
'\0',
c =
'\0',
d =
'\0',
e =
'\0',
f =
'\0';
374 Action<
int(
int*,
int*,
char*,
char*,
char*,
char*,
376 DoAll(SetArgPointee<0>(1),
378 SetArgPointee<2>(
'a'),
379 SetArgPointee<3>(
'b'),
380 SetArgPointee<4>(
'c'),
381 SetArgPointee<5>(
'd'),
382 SetArgPointee<6>(
'e'),
383 SetArgPointee<7>(
'f'),
397 TEST(DoAllTest, TenActions) {
399 char a =
'\0',
b =
'\0',
c =
'\0',
d =
'\0';
400 char e =
'\0',
f =
'\0',
g =
'\0';
401 Action<
int(
int*,
int*,
char*,
char*,
char*,
char*,
402 char*,
char*,
char*)>
action =
403 DoAll(SetArgPointee<0>(1),
405 SetArgPointee<2>(
'a'),
406 SetArgPointee<3>(
'b'),
407 SetArgPointee<4>(
'c'),
408 SetArgPointee<5>(
'd'),
409 SetArgPointee<6>(
'e'),
410 SetArgPointee<7>(
'f'),
411 SetArgPointee<8>(
'g'),
433 # pragma warning(push)
434 # pragma warning(disable:4100)
435 # pragma warning(disable:4503)
441 ACTION(Return5) {
return 5; }
443 TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
444 Action<double()>
a1 = Return5();
447 Action<
int(
double,
bool)>
a2 = Return5();
452 ACTION(IncrementArg1) { (*arg1)++; }
454 TEST(ActionMacroTest, WorksWhenReturningVoid) {
455 Action<void(
int,
int*)>
a1 = IncrementArg1();
464 StaticAssertTypeEq<int*, arg2_type>();
465 arg2_type
temp = arg2;
469 TEST(ActionMacroTest, CanReferenceArgumentType) {
470 Action<void(
int,
bool,
int*)>
a1 = IncrementArg2();
479 StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
480 args_type args_copy =
args;
481 return std::get<0>(args_copy) + std::get<1>(args_copy);
484 TEST(ActionMacroTest, CanReferenceArgumentTuple) {
485 Action<
int(
int,
char,
int*)>
a1 = Sum2();
495 StaticAssertTypeEq<int(bool), function_type>();
496 function_type* fp = &
Dummy;
500 TEST(ActionMacroTest, CanReferenceMockFunctionType) {
501 Action<
int(
bool)>
a1 = InvokeDummy();
509 StaticAssertTypeEq<int, return_type>();
514 TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
515 Action<
int(
bool)>
a1 = InvokeDummy2();
521 ACTION(ReturnAddrOfConstBoolReferenceArg) {
522 StaticAssertTypeEq<const bool&, arg1_type>();
526 TEST(ActionMacroTest, WorksForConstReferenceArg) {
527 Action<
const bool*(
int,
const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
528 const bool b =
false;
529 EXPECT_EQ(&
b,
a.Perform(std::tuple<int, const bool&>(0,
b)));
533 ACTION(ReturnAddrOfIntReferenceArg) {
534 StaticAssertTypeEq<int&, arg0_type>();
538 TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
539 Action<
int*(
int&,
bool,
int)> a = ReturnAddrOfIntReferenceArg();
541 EXPECT_EQ(&n,
a.Perform(std::tuple<int&, bool, int>(n,
true, 1)));
545 namespace action_test {
546 ACTION(Sum) {
return arg0 + arg1; }
549 TEST(ActionMacroTest, WorksInNamespace) {
550 Action<
int(
int,
int)>
a1 = action_test::Sum();
556 ACTION(PlusTwo) {
return arg0 + 2; }
558 TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
559 Action<
int(
int)>
a1 = PlusTwo();
562 Action<double(
float,
void*)>
a2 = PlusTwo();
570 TEST(ActionPMacroTest, DefinesParameterizedAction) {
571 Action<
int(
int m,
bool t)>
a1 = Plus(9);
583 TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
584 Action<
int(
char m,
bool t)>
a1 = TypedPlus(9);
590 TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
600 ACTION(OverloadedAction) {
return arg0 ? arg1 :
"hello"; }
602 ACTION_P(OverloadedAction, default_value) {
603 return arg0 ? arg1 : default_value;
606 ACTION_P2(OverloadedAction, true_value, false_value) {
607 return arg0 ? true_value : false_value;
610 TEST(ActionMacroTest, CanDefineOverloadedActions) {
611 typedef Action<
const char*(
bool,
const char*)> MyAction;
613 const MyAction
a1 = OverloadedAction();
617 const MyAction
a2 = OverloadedAction(
"hi");
621 const MyAction a3 = OverloadedAction(
"hi",
"you");
630 TEST(ActionPnMacroTest, WorksFor3Parameters) {
631 Action<double(
int m,
bool t)>
a1 = Plus(100, 20, 3.4);
640 ACTION_P4(Plus, p0, p1, p2, p3) {
return arg0 + p0 + p1 + p2 + p3; }
642 TEST(ActionPnMacroTest, WorksFor4Parameters) {
643 Action<
int(
int)>
a1 = Plus(1, 2, 3, 4);
647 ACTION_P5(Plus, p0, p1, p2, p3, p4) {
return arg0 + p0 + p1 + p2 + p3 + p4; }
649 TEST(ActionPnMacroTest, WorksFor5Parameters) {
650 Action<
int(
int)>
a1 = Plus(1, 2, 3, 4, 5);
654 ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
655 return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
658 TEST(ActionPnMacroTest, WorksFor6Parameters) {
659 Action<
int(
int)>
a1 = Plus(1, 2, 3, 4, 5, 6);
663 ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
664 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
667 TEST(ActionPnMacroTest, WorksFor7Parameters) {
668 Action<
int(
int)>
a1 = Plus(1, 2, 3, 4, 5, 6, 7);
672 ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
673 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
676 TEST(ActionPnMacroTest, WorksFor8Parameters) {
677 Action<
int(
int)>
a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
678 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
682 ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
683 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
686 TEST(ActionPnMacroTest, WorksFor9Parameters) {
687 Action<
int(
int)>
a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
688 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
692 ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
694 last_param_type t9 = last_param;
695 return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
698 TEST(ActionPnMacroTest, WorksFor10Parameters) {
699 Action<
int(
int)>
a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
700 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
709 char suffix_char =
static_cast<char>(
suffix);
710 return prefix_str + arg0 + suffix_char;
713 TEST(ActionPnMacroTest, SimpleTypePromotion) {
717 PadArgument(
"foo",
static_cast<int>(
'r'));
728 std::stringstream ss;
738 template <
typename T1,
typename T2>
742 ConcatImplActionP3<std::string, T1, T2>
749 return ConcatImpl(a,
b, c);
754 return ConcatImpl<std::string, T1, T2>(a,
b, c);
760 template <
typename T1,
typename T2>
761 ConcatImplActionP3<T1, int, T2>
763 return ConcatImpl(a,
b, c);
766 TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
780 TEST(ActionPnMacroTest, TypesAreCorrect) {
782 DoFooAction a0 = DoFoo();
785 DoFooActionP<int>
a1 = DoFoo(1);
789 DoFooActionP2<int, char>
a2 = DoFoo(1,
'2');
790 PlusActionP3<int, int, char> a3 = Plus(1, 2,
'3');
791 PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3,
'4');
792 PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4,
'5');
793 PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5,
'6');
794 PlusActionP7<int, int, int, int, int, int, char> a7 =
795 Plus(1, 2, 3, 4, 5, 6,
'7');
796 PlusActionP8<int, int, int, int, int, int, int, char> a8 =
797 Plus(1, 2, 3, 4, 5, 6, 7,
'8');
798 PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
799 Plus(1, 2, 3, 4, 5, 6, 7, 8,
'9');
800 PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
801 Plus(1, 2, 3, 4, 5, 6, 7, 8, 9,
'0');
823 ACTION_P10(Plus10, a0,
a1,
a2, a3, a4, a5, a6, a7, a8, a9) {
824 return a0 +
a1 +
a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
827 TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
828 int x = 1,
y = 2,
z = 3;
831 Action<
int()>
a = Plus1<int&>(x);
834 a = Plus2<const int&, int&>(x,
y);
837 a = Plus3<int&, const int&, int&>(x,
y, z);
840 int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
841 a = Plus10<
const int&,
int&,
const int&,
int&,
const int&,
int&,
const int&,
842 int&,
const int&,
int&>(
n[0],
n[1],
n[2],
n[3],
n[4],
n[5],
n[6],
n[7],
847 class NullaryConstructorClass {
854 TEST(ReturnNewTest, NoArgs) {
855 Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>();
861 class UnaryConstructorClass {
869 Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
875 TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
876 Action<UnaryConstructorClass*(
bool,
int)> a =
877 ReturnNew<UnaryConstructorClass>(4000);
883 TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
884 Action<
const UnaryConstructorClass*()> a =
885 ReturnNew<UnaryConstructorClass>(4000);
891 class TenArgConstructorClass {
894 int a6,
int a7,
int a8,
int a9,
int a10)
895 :
value_(
a1 +
a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
901 TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
902 Action<TenArgConstructorClass*()> a =
903 ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
904 4000000, 500000, 60000,
913 HAS_1_TEMPLATE_PARAMS(
typename,
T),
914 AND_0_VALUE_PARAMS()) {
918 TEST(ActionTemplateTest, WorksWithoutValueParam) {
919 const Action<
int*()> a = CreateNew<int>();
926 HAS_1_TEMPLATE_PARAMS(
typename,
T),
927 AND_1_VALUE_PARAMS(a0)) {
931 TEST(ActionTemplateTest, WorksWithValueParams) {
932 const Action<
int*()> a = CreateNew<int>(42);
940 HAS_1_TEMPLATE_PARAMS(
int,
k),
941 AND_0_VALUE_PARAMS()) {
942 delete std::get<k>(
args);
954 TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
955 const Action<void(
int*, BoolResetter*)>
a = MyDeleteArg<1>();
958 BoolResetter* resetter =
new BoolResetter(&
b);
965 HAS_1_TEMPLATE_PARAMS(template <typename Pointee>
class,
967 AND_1_VALUE_PARAMS(pointee)) {
968 return Pointer<pointee_type>(
new pointee_type(pointee));
971 TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
972 const Action<std::shared_ptr<int>()>
a =
973 ReturnSmartPointer<std::shared_ptr>(42);
979 template <
typename T1,
typename T2,
typename T3,
int k4,
bool k5,
980 unsigned int k6,
typename T7,
typename T8,
typename T9>
981 struct GiantTemplate {
988 HAS_10_TEMPLATE_PARAMS(
998 template <typename T>
class, T10),
999 AND_1_VALUE_PARAMS(
value)) {
1000 return GiantTemplate<T10<T1>,
T2,
T3, k4, k5, k6, T7, T8, T9>(
value);
1003 TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1004 using Giant = GiantTemplate<std::shared_ptr<int>,
bool, double, 5,
true, 6,
1005 char, unsigned,
int>;
1006 const Action<Giant()>
a = ReturnGiant<
int,
bool, double, 5,
true, 6, char,
1007 unsigned,
int, std::shared_ptr>(42);
1014 HAS_1_TEMPLATE_PARAMS(
typename, Number),
1015 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1016 return static_cast<Number
>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1019 TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1020 const Action<
int()>
a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1027 ACTION(ReturnSum) {
return 0; }
1032 HAS_1_TEMPLATE_PARAMS(
typename, Number),
1033 AND_2_VALUE_PARAMS(v1, v2)) {
1034 return static_cast<Number
>(v1) + v2;
1038 HAS_1_TEMPLATE_PARAMS(
typename, Number),
1039 AND_3_VALUE_PARAMS(v1, v2, v3)) {
1040 return static_cast<Number
>(v1) + v2 + v3;
1044 HAS_2_TEMPLATE_PARAMS(
typename, Number,
int,
k),
1045 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1046 return static_cast<Number
>(v1) + v2 + v3 + v4 +
k;
1049 TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1050 const Action<
int()> a0 = ReturnSum();
1051 const Action<
int()>
a1 = ReturnSum(1);
1052 const Action<
int()>
a2 = ReturnSum<int>(1, 2);
1053 const Action<
int()> a3 = ReturnSum<int>(1, 2, 3);
1054 const Action<
int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);