41 #include "gmock/gmock.h" 42 #include "gtest/gtest.h" 45 namespace gmock_generated_actions_test {
51 using ::std::tr1::tuple;
52 using ::std::tr1::tuple_element;
60 using testing::ReturnNew;
67 inline short Short(
short n) {
return n; }
68 inline char Char(
char ch) {
return ch; }
80 bool Unary(
int x) {
return x < 0; }
82 const char*
Plus1(
const char* s) {
return s + 1; }
95 const char*
Binary(
const char* input,
short n) {
return input + n; }
99 int Ternary(
int x,
char y,
short z) {
return x + y + z; }
103 int SumOf4(
int a,
int b,
int c,
int d) {
return a + b + c + d; }
105 string Concat4(
const char* s1,
const char* s2,
const char* s3,
107 return string(s1) + s2 + s3 + s4;
110 int SumOf5(
int a,
int b,
int c,
int d,
int e) {
return a + b + c + d + e; }
114 return a + b + c + d + e;
118 string Concat5(
const char* s1,
const char* s2,
const char* s3,
119 const char* s4,
const char* s5) {
120 return string(s1) + s2 + s3 + s4 + s5;
123 int SumOf6(
int a,
int b,
int c,
int d,
int e,
int f) {
124 return a + b + c + d + e + f;
129 return a + b + c + d + e + f;
133 string Concat6(
const char* s1,
const char* s2,
const char* s3,
134 const char* s4,
const char* s5,
const char* s6) {
135 return string(s1) + s2 + s3 + s4 + s5 + s6;
138 string Concat7(
const char* s1,
const char* s2,
const char* s3,
139 const char* s4,
const char* s5,
const char* s6,
141 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
144 string Concat8(
const char* s1,
const char* s2,
const char* s3,
145 const char* s4,
const char* s5,
const char* s6,
146 const char* s7,
const char* s8) {
147 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
150 string Concat9(
const char* s1,
const char* s2,
const char* s3,
151 const char* s4,
const char* s5,
const char* s6,
152 const char* s7,
const char* s8,
const char* s9) {
153 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
156 string Concat10(
const char* s1,
const char* s2,
const char* s3,
157 const char* s4,
const char* s5,
const char* s6,
158 const char* s7,
const char* s8,
const char* s9,
160 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
165 inline const char*
CharPtr(
const char* s) {
return s; }
170 TEST(InvokeArgumentTest, Function0) {
176 TEST(InvokeArgumentTest, Functor1) {
182 TEST(InvokeArgumentTest, Function5) {
184 InvokeArgument<0>(10000, 2000, 300, 40, 5);
189 TEST(InvokeArgumentTest, Functor5) {
191 InvokeArgument<0>(10000, 2000, 300, 40, 5);
196 TEST(InvokeArgumentTest, Function6) {
198 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
203 TEST(InvokeArgumentTest, Functor6) {
205 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
210 TEST(InvokeArgumentTest, Function7) {
211 Action<
string(
string(*)(
const char*,
const char*,
const char*,
212 const char*,
const char*,
const char*,
214 InvokeArgument<0>(
"1",
"2",
"3",
"4",
"5",
"6",
"7");
219 TEST(InvokeArgumentTest, Function8) {
220 Action<
string(
string(*)(
const char*,
const char*,
const char*,
221 const char*,
const char*,
const char*,
222 const char*,
const char*))> a =
223 InvokeArgument<0>(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8");
228 TEST(InvokeArgumentTest, Function9) {
229 Action<
string(
string(*)(
const char*,
const char*,
const char*,
230 const char*,
const char*,
const char*,
231 const char*,
const char*,
const char*))> a =
232 InvokeArgument<0>(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9");
237 TEST(InvokeArgumentTest, Function10) {
238 Action<
string(
string(*)(
const char*,
const char*,
const char*,
239 const char*,
const char*,
const char*,
240 const char*,
const char*,
const char*,
242 InvokeArgument<0>(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0");
247 TEST(InvokeArgumentTest, ByPointerFunction) {
249 InvokeArgument<0>(
static_cast<const char*
>(
"Hi"),
Short(1));
255 TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
257 InvokeArgument<0>(
"Hi",
Short(1));
262 TEST(InvokeArgumentTest, ByConstReferenceFunction) {
264 InvokeArgument<0>(
string(
"Hi"));
273 TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
275 InvokeArgument<0>(
ByRef(g_double));
280 a = InvokeArgument<0>(
ByRef(x));
295 const char s[] =
"Hello";
300 TEST(WithArgsTest, ThreeArgs) {
317 const char*,
const char*)> a =
333 TEST(WithArgsTest, SevenArgs) {
342 TEST(WithArgsTest, EightArgs) {
371 virtual int Perform(
const tuple<int, int>& args) {
372 return get<0>(args) - get<1>(args);
376 TEST(WithArgsTest, NonInvokeAction) {
390 TEST(WithArgsTest, RepeatedArguments) {
397 TEST(WithArgsTest, ReversedArgumentOrder) {
400 const char s[] =
"Hello";
405 TEST(WithArgsTest, ArgsOfCompatibleTypes) {
412 TEST(WithArgsTest, VoidAction) {
429 TEST(DoAllTest, ThreeActions) {
444 DoAll(SetArgPointee<0>(1),
446 SetArgPointee<2>(
'a'),
457 char a =
'\0', b =
'\0';
459 DoAll(SetArgPointee<0>(1),
461 SetArgPointee<2>(
'a'),
462 SetArgPointee<3>(
'b'),
474 char a =
'\0', b =
'\0',
c =
'\0';
476 DoAll(SetArgPointee<0>(1),
478 SetArgPointee<2>(
'a'),
479 SetArgPointee<3>(
'b'),
480 SetArgPointee<4>(
'c'),
491 TEST(DoAllTest, SevenActions) {
493 char a =
'\0', b =
'\0',
c =
'\0',
d =
'\0';
495 DoAll(SetArgPointee<0>(1),
497 SetArgPointee<2>(
'a'),
498 SetArgPointee<3>(
'b'),
499 SetArgPointee<4>(
'c'),
500 SetArgPointee<5>(
'd'),
512 TEST(DoAllTest, EightActions) {
514 char a =
'\0', b =
'\0',
c =
'\0',
d =
'\0', e =
'\0';
515 Action<int(
int*,
int*,
char*,
char*,
char*,
char*,
517 DoAll(SetArgPointee<0>(1),
519 SetArgPointee<2>(
'a'),
520 SetArgPointee<3>(
'b'),
521 SetArgPointee<4>(
'c'),
522 SetArgPointee<5>(
'd'),
523 SetArgPointee<6>(
'e'),
538 char a =
'\0', b =
'\0',
c =
'\0',
d =
'\0', e =
'\0',
f =
'\0';
539 Action<int(
int*,
int*,
char*,
char*,
char*,
char*,
540 char*,
char*)> action =
541 DoAll(SetArgPointee<0>(1),
543 SetArgPointee<2>(
'a'),
544 SetArgPointee<3>(
'b'),
545 SetArgPointee<4>(
'c'),
546 SetArgPointee<5>(
'd'),
547 SetArgPointee<6>(
'e'),
548 SetArgPointee<7>(
'f'),
564 char a =
'\0', b =
'\0',
c =
'\0',
d =
'\0';
565 char e =
'\0',
f =
'\0', g =
'\0';
566 Action<int(
int*,
int*,
char*,
char*,
char*,
char*,
567 char*,
char*,
char*)> action =
568 DoAll(SetArgPointee<0>(1),
570 SetArgPointee<2>(
'a'),
571 SetArgPointee<3>(
'b'),
572 SetArgPointee<4>(
'c'),
573 SetArgPointee<5>(
'd'),
574 SetArgPointee<6>(
'e'),
575 SetArgPointee<7>(
'f'),
576 SetArgPointee<8>(
'g'),
596 # pragma warning(push) 597 # pragma warning(disable:4100) 606 TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
617 TEST(ActionMacroTest, WorksWhenReturningVoid) {
627 StaticAssertTypeEq<int*, arg2_type>();
628 arg2_type temp = arg2;
632 TEST(ActionMacroTest, CanReferenceArgumentType) {
642 StaticAssertTypeEq< ::std::tr1::tuple<int, char, int*>, args_type>();
643 args_type args_copy = args;
644 return get<0>(args_copy) + get<1>(args_copy);
647 TEST(ActionMacroTest, CanReferenceArgumentTuple) {
655 int Dummy(
bool flag) {
return flag? 1 : 0; }
658 StaticAssertTypeEq<int(bool), function_type>();
659 function_type* fp = &
Dummy;
663 TEST(ActionMacroTest, CanReferenceMockFunctionType) {
672 StaticAssertTypeEq<int, return_type>();
673 return_type result =
Dummy(
true);
677 TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
684 ACTION(ReturnAddrOfConstBoolReferenceArg) {
685 StaticAssertTypeEq<const bool&, arg1_type>();
689 TEST(ActionMacroTest, WorksForConstReferenceArg) {
691 const bool b =
false;
697 StaticAssertTypeEq<int&, arg0_type>();
701 TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
708 namespace action_test {
712 TEST(ActionMacroTest, WorksInNamespace) {
721 TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
733 TEST(ActionPMacroTest, DefinesParameterizedAction) {
746 TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
753 TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
762 ACTION(OverloadedAction) {
return arg0 ? arg1 :
"hello"; }
769 return arg0 ? true_value : false_value;
772 TEST(ActionMacroTest, CanDefineOverloadedActions) {
775 const MyAction a1 = OverloadedAction();
779 const MyAction a2 = OverloadedAction(
"hi");
783 const MyAction a3 = OverloadedAction(
"hi",
"you");
792 TEST(ActionPnMacroTest, WorksFor3Parameters) {
801 ACTION_P4(Plus, p0, p1, p2, p3) {
return arg0 + p0 + p1 + p2 + p3; }
803 TEST(ActionPnMacroTest, WorksFor4Parameters) {
808 ACTION_P5(Plus, p0, p1, p2, p3, p4) {
return arg0 + p0 + p1 + p2 + p3 + p4; }
810 TEST(ActionPnMacroTest, WorksFor5Parameters) {
816 return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
819 TEST(ActionPnMacroTest, WorksFor6Parameters) {
825 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
828 TEST(ActionPnMacroTest, WorksFor7Parameters) {
834 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
837 TEST(ActionPnMacroTest, WorksFor8Parameters) {
843 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
846 TEST(ActionPnMacroTest, WorksFor9Parameters) {
851 ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
853 last_param_type t9 = last_param;
854 return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
857 TEST(ActionPnMacroTest, WorksFor10Parameters) {
859 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
868 char suffix_char =
static_cast<char>(suffix);
869 return prefix_str + arg0 + suffix_char;
872 TEST(ActionPnMacroTest, SimpleTypePromotion) {
876 PadArgument(
"foo", static_cast<int>(
'r'));
887 std::stringstream ss;
897 template <
typename T1,
typename T2>
901 ConcatImplActionP3<std::string, T1, T2>
906 return ConcatImpl(a, b, c);
911 return ConcatImpl<std::string, T1, T2>(a, b,
c);
917 template <
typename T1,
typename T2>
918 ConcatImplActionP3<T1, int, T2>
920 return ConcatImpl(a, b, c);
923 TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
937 TEST(ActionPnMacroTest, TypesAreCorrect) {
939 DoFooAction a0 = DoFoo();
942 DoFooActionP<int> a1 = DoFoo(1);
946 DoFooActionP2<int, char> a2 = DoFoo(1,
'2');
947 PlusActionP3<int, int, char> a3 = Plus(1, 2,
'3');
948 PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3,
'4');
949 PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4,
'5');
950 PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5,
'6');
951 PlusActionP7<int, int, int, int, int, int, char> a7 =
952 Plus(1, 2, 3, 4, 5, 6,
'7');
953 PlusActionP8<int, int, int, int, int, int, int, char> a8 =
954 Plus(1, 2, 3, 4, 5, 6, 7,
'8');
955 PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
956 Plus(1, 2, 3, 4, 5, 6, 7, 8,
'9');
957 PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
958 Plus(1, 2, 3, 4, 5, 6, 7, 8, 9,
'0');
980 ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
981 return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
984 TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
985 int x = 1, y = 2, z = 3;
991 a = Plus2<const int&, int&>(x, y);
994 a = Plus3<int&, const int&, int&>(x, y, z);
997 int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
998 a = Plus10<
const int&,
int&,
const int&,
int&,
const int&,
int&,
const int&,
999 int&,
const int&,
int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
1032 TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
1034 ReturnNew<UnaryConstructorClass>(4000);
1040 TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
1042 ReturnNew<UnaryConstructorClass>(4000);
1051 int a6,
int a7,
int a8,
int a9,
int a10)
1052 : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
1058 TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
1060 ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
1061 4000000, 500000, 60000,
1070 HAS_1_TEMPLATE_PARAMS(
typename, T),
1071 AND_0_VALUE_PARAMS()) {
1075 TEST(ActionTemplateTest, WorksWithoutValueParam) {
1083 HAS_1_TEMPLATE_PARAMS(
typename, T),
1084 AND_1_VALUE_PARAMS(a0)) {
1088 TEST(ActionTemplateTest, WorksWithValueParams) {
1097 HAS_1_TEMPLATE_PARAMS(
int, k),
1098 AND_0_VALUE_PARAMS()) {
1099 delete std::tr1::get<k>(args);
1111 TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
1122 HAS_1_TEMPLATE_PARAMS(template <typename Pointee>
class,
1124 AND_1_VALUE_PARAMS(pointee)) {
1125 return Pointer<pointee_type>(
new pointee_type(pointee));
1128 TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
1129 using ::testing::internal::linked_ptr;
1136 template <
typename T1,
typename T2,
typename T3,
int k4,
bool k5,
1137 unsigned int k6,
typename T7,
typename T8,
typename T9>
1145 HAS_10_TEMPLATE_PARAMS(
1155 template <typename T>
class, T10),
1156 AND_1_VALUE_PARAMS(value)) {
1160 TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1161 using ::testing::internal::linked_ptr;
1163 true, 6, char, unsigned,
int> Giant;
1165 int, bool, double, 5,
true, 6, char, unsigned, int, linked_ptr>(42);
1172 HAS_1_TEMPLATE_PARAMS(
typename, Number),
1173 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1174 return static_cast<Number
>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1177 TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1178 const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1190 HAS_1_TEMPLATE_PARAMS(
typename, Number),
1191 AND_2_VALUE_PARAMS(v1, v2)) {
1192 return static_cast<Number
>(v1) + v2;
1196 HAS_1_TEMPLATE_PARAMS(
typename, Number),
1197 AND_3_VALUE_PARAMS(v1, v2, v3)) {
1198 return static_cast<Number
>(v1) + v2 + v3;
1202 HAS_2_TEMPLATE_PARAMS(
typename, Number,
int, k),
1203 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1204 return static_cast<Number
>(v1) + v2 + v3 + v4 + k;
1207 TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1212 const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1221 # pragma warning(pop)
string Concat7(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7)
int SumOf5(int a, int b, int c, int d, int e)
GiantTemplate(int a_value)
int operator()(int a, int b, int c, int d, int e, int f)
TEST(InvokeArgumentTest, Function0)
internal::IgnoredValue Unused
string Concat9(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, const char *s8, const char *s9)
string ByNonConstRef(string &s)
ACTION_P2(OverloadedAction, true_value, false_value)
BoolResetter(bool *value)
bool ReferencesGlobalDouble(const double &x)
ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T), AND_0_VALUE_PARAMS())
ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6)
bool ByConstRef(const string &s)
const char * Plus1(const char *s)
int Ternary(int x, char y, short z)
#define EXPECT_TRUE(condition)
string Concat4(const char *s1, const char *s2, const char *s3, const char *s4)
#define EXPECT_STREQ(expected, actual)
int operator()(int a, int b, int c, int d, int e)
ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param)
Action< F > MakeAction(ActionInterface< F > *impl)
NullaryConstructorClass()
#define EXPECT_DOUBLE_EQ(expected, actual)
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgPointee(const T &x)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define EXPECT_FALSE(condition)
internal::WithArgsAction< InnerAction, k1 > WithArgs(const InnerAction &action)
ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8)
void VoidBinary(int, char)
TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10)
ACTION_P5(Plus, p0, p1, p2, p3, p4)
int SumOf6(int a, int b, int c, int d, int e, int f)
ACTION_P6(Plus, p0, p1, p2, p3, p4, p5)
string Concat10(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, const char *s8, const char *s9, const char *s10)
const char * Binary(const char *input, short n)
virtual int Perform(const tuple< int, int > &args)
string Concat5(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5)
const char * CharPtr(const char *s)
void VoidTernary(int, char, bool)
ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7)
Result Perform(const ArgumentTuple &args) const
string Concat8(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, const char *s8)
int SumOf4(int a, int b, int c, int d)
internal::ReferenceWrapper< T > ByRef(T &l_value)
internal::DoBothAction< Action1, Action2 > DoAll(Action1 a1, Action2 a2)
bool StaticAssertTypeEq()
UnaryConstructorClass(int value)
ACTION_P4(Plus, p0, p1, p2, p3)
const internal::AnythingMatcher _
ConcatImplActionP3< std::string, T1, T2 > Concat(const std::string &a, T1 b, T2 c)
string Concat6(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6)
#define EXPECT_EQ(expected, actual)
internal::ReturnAction< R > Return(R value)