36 #include "gmock/gmock-generated-actions.h" 41 #include "gmock/gmock.h" 42 #include "gtest/gtest.h" 45 namespace gmock_generated_actions_test {
52 using testing::tuple_element;
67 inline short Short(
short n) {
return n; }
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;
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;
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) {
391 TEST(WithArgsTest, RepeatedArguments) {
398 TEST(WithArgsTest, ReversedArgumentOrder) {
401 const char s[] =
"Hello";
406 TEST(WithArgsTest, ArgsOfCompatibleTypes) {
413 TEST(WithArgsTest, VoidAction) {
430 TEST(DoAllTest, ThreeActions) {
445 DoAll(SetArgPointee<0>(1),
447 SetArgPointee<2>(
'a'),
458 char a =
'\0',
b =
'\0';
460 DoAll(SetArgPointee<0>(1),
462 SetArgPointee<2>(
'a'),
463 SetArgPointee<3>(
'b'),
475 char a =
'\0',
b =
'\0',
c =
'\0';
477 DoAll(SetArgPointee<0>(1),
479 SetArgPointee<2>(
'a'),
480 SetArgPointee<3>(
'b'),
481 SetArgPointee<4>(
'c'),
492 TEST(DoAllTest, SevenActions) {
494 char a =
'\0',
b =
'\0',
c =
'\0', d =
'\0';
496 DoAll(SetArgPointee<0>(1),
498 SetArgPointee<2>(
'a'),
499 SetArgPointee<3>(
'b'),
500 SetArgPointee<4>(
'c'),
501 SetArgPointee<5>(
'd'),
513 TEST(DoAllTest, EightActions) {
515 char a =
'\0',
b =
'\0',
c =
'\0', d =
'\0', e =
'\0';
516 Action<int(
int*,
int*,
char*,
char*,
char*,
char*,
518 DoAll(SetArgPointee<0>(1),
520 SetArgPointee<2>(
'a'),
521 SetArgPointee<3>(
'b'),
522 SetArgPointee<4>(
'c'),
523 SetArgPointee<5>(
'd'),
524 SetArgPointee<6>(
'e'),
539 char a =
'\0',
b =
'\0',
c =
'\0', d =
'\0', e =
'\0',
f =
'\0';
540 Action<int(
int*,
int*,
char*,
char*,
char*,
char*,
542 DoAll(SetArgPointee<0>(1),
544 SetArgPointee<2>(
'a'),
545 SetArgPointee<3>(
'b'),
546 SetArgPointee<4>(
'c'),
547 SetArgPointee<5>(
'd'),
548 SetArgPointee<6>(
'e'),
549 SetArgPointee<7>(
'f'),
565 char a =
'\0',
b =
'\0',
c =
'\0', d =
'\0';
566 char e =
'\0',
f =
'\0',
g =
'\0';
567 Action<int(
int*,
int*,
char*,
char*,
char*,
char*,
568 char*,
char*,
char*)>
action =
569 DoAll(SetArgPointee<0>(1),
571 SetArgPointee<2>(
'a'),
572 SetArgPointee<3>(
'b'),
573 SetArgPointee<4>(
'c'),
574 SetArgPointee<5>(
'd'),
575 SetArgPointee<6>(
'e'),
576 SetArgPointee<7>(
'f'),
577 SetArgPointee<8>(
'g'),
597 # pragma warning(push) 598 # pragma warning(disable:4100) 607 TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
618 TEST(ActionMacroTest, WorksWhenReturningVoid) {
628 StaticAssertTypeEq<int*, arg2_type>();
629 arg2_type
temp = arg2;
633 TEST(ActionMacroTest, CanReferenceArgumentType) {
643 StaticAssertTypeEq<tuple<int, char, int*>, args_type>();
644 args_type args_copy = args;
645 return get<0>(args_copy) + get<1>(args_copy);
648 TEST(ActionMacroTest, CanReferenceArgumentTuple) {
659 StaticAssertTypeEq<int(bool), function_type>();
660 function_type* fp = &
Dummy;
664 TEST(ActionMacroTest, CanReferenceMockFunctionType) {
673 StaticAssertTypeEq<int, return_type>();
678 TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
685 ACTION(ReturnAddrOfConstBoolReferenceArg) {
686 StaticAssertTypeEq<const bool&, arg1_type>();
690 TEST(ActionMacroTest, WorksForConstReferenceArg) {
692 const bool b =
false;
698 StaticAssertTypeEq<int&, arg0_type>();
702 TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
709 namespace action_test {
713 TEST(ActionMacroTest, WorksInNamespace) {
722 TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
734 TEST(ActionPMacroTest, DefinesParameterizedAction) {
747 TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
754 TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
763 ACTION(OverloadedAction) {
return arg0 ? arg1 :
"hello"; }
766 return arg0 ? arg1 : default_value;
770 return arg0 ? true_value : false_value;
773 TEST(ActionMacroTest, CanDefineOverloadedActions) {
776 const MyAction a1 = OverloadedAction();
780 const MyAction a2 = OverloadedAction(
"hi");
784 const MyAction a3 = OverloadedAction(
"hi",
"you");
793 TEST(ActionPnMacroTest, WorksFor3Parameters) {
799 EXPECT_EQ(
"retail->", a2.Perform(tuple<const std::string&>(re)));
802 ACTION_P4(Plus, p0, p1, p2, p3) {
return arg0 + p0 + p1 + p2 + p3; }
804 TEST(ActionPnMacroTest, WorksFor4Parameters) {
809 ACTION_P5(Plus, p0, p1, p2, p3, p4) {
return arg0 + p0 + p1 + p2 + p3 + p4; }
811 TEST(ActionPnMacroTest, WorksFor5Parameters) {
817 return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
820 TEST(ActionPnMacroTest, WorksFor6Parameters) {
826 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
829 TEST(ActionPnMacroTest, WorksFor7Parameters) {
835 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
838 TEST(ActionPnMacroTest, WorksFor8Parameters) {
844 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
847 TEST(ActionPnMacroTest, WorksFor9Parameters) {
852 ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
854 last_param_type t9 = last_param;
855 return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
858 TEST(ActionPnMacroTest, WorksFor10Parameters) {
860 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
869 char suffix_char =
static_cast<char>(
suffix);
870 return prefix_str + arg0 + suffix_char;
873 TEST(ActionPnMacroTest, SimpleTypePromotion) {
877 PadArgument(
"foo", static_cast<int>(
'r'));
888 std::stringstream ss;
898 template <
typename T1,
typename T2>
902 ConcatImplActionP3<std::string, T1, T2>
909 return ConcatImpl(a, b, c);
914 return ConcatImpl<std::string, T1, T2>(
a,
b,
c);
920 template <
typename T1,
typename T2>
921 ConcatImplActionP3<T1, int, T2>
923 return ConcatImpl(a, b, c);
926 TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
940 TEST(ActionPnMacroTest, TypesAreCorrect) {
942 DoFooAction a0 = DoFoo();
945 DoFooActionP<int> a1 = DoFoo(1);
949 DoFooActionP2<int, char> a2 = DoFoo(1,
'2');
950 PlusActionP3<int, int, char> a3 = Plus(1, 2,
'3');
951 PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3,
'4');
952 PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4,
'5');
953 PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5,
'6');
954 PlusActionP7<int, int, int, int, int, int, char> a7 =
955 Plus(1, 2, 3, 4, 5, 6,
'7');
956 PlusActionP8<int, int, int, int, int, int, int, char> a8 =
957 Plus(1, 2, 3, 4, 5, 6, 7,
'8');
958 PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
959 Plus(1, 2, 3, 4, 5, 6, 7, 8,
'9');
960 PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
961 Plus(1, 2, 3, 4, 5, 6, 7, 8, 9,
'0');
983 ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
984 return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
987 TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
988 int x = 1,
y = 2,
z = 3;
994 a = Plus2<const int&, int&>(
x,
y);
997 a = Plus3<int&, const int&, int&>(
x,
y,
z);
1000 int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
1001 a = Plus10<
const int&,
int&,
const int&,
int&,
const int&,
int&,
const int&,
1002 int&,
const int&,
int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
1035 TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
1037 ReturnNew<UnaryConstructorClass>(4000);
1043 TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
1045 ReturnNew<UnaryConstructorClass>(4000);
1054 int a6,
int a7,
int a8,
int a9,
int a10)
1055 : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
1061 TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
1063 ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
1064 4000000, 500000, 60000,
1073 HAS_1_TEMPLATE_PARAMS(
typename,
T),
1074 AND_0_VALUE_PARAMS()) {
1078 TEST(ActionTemplateTest, WorksWithoutValueParam) {
1086 HAS_1_TEMPLATE_PARAMS(
typename,
T),
1087 AND_1_VALUE_PARAMS(a0)) {
1091 TEST(ActionTemplateTest, WorksWithValueParams) {
1100 HAS_1_TEMPLATE_PARAMS(
int,
k),
1101 AND_0_VALUE_PARAMS()) {
1102 delete get<k>(args);
1114 TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
1125 HAS_1_TEMPLATE_PARAMS(template <typename Pointee>
class,
1127 AND_1_VALUE_PARAMS(pointee)) {
1128 return Pointer<pointee_type>(
new pointee_type(pointee));
1131 TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
1132 using ::testing::internal::linked_ptr;
1139 template <
typename T1,
typename T2,
typename T3,
int k4,
bool k5,
1140 unsigned int k6,
typename T7,
typename T8,
typename T9>
1148 HAS_10_TEMPLATE_PARAMS(
1158 template <typename T>
class, T10),
1159 AND_1_VALUE_PARAMS(
value)) {
1163 TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1164 using ::testing::internal::linked_ptr;
1166 true, 6, char, unsigned,
int> Giant;
1168 int, bool, double, 5,
true, 6, char, unsigned, int, linked_ptr>(42);
1175 HAS_1_TEMPLATE_PARAMS(
typename, Number),
1176 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1177 return static_cast<Number
>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1180 TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1181 const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1193 HAS_1_TEMPLATE_PARAMS(
typename, Number),
1194 AND_2_VALUE_PARAMS(v1, v2)) {
1195 return static_cast<Number
>(v1) + v2;
1199 HAS_1_TEMPLATE_PARAMS(
typename, Number),
1200 AND_3_VALUE_PARAMS(v1, v2, v3)) {
1201 return static_cast<Number
>(v1) + v2 + v3;
1205 HAS_2_TEMPLATE_PARAMS(
typename, Number,
int,
k),
1206 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1207 return static_cast<Number
>(v1) + v2 + v3 + v4 +
k;
1210 TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1215 const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1224 # 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)
#define EXPECT_EQ(val1, val2)
#define GTEST_INTENTIONAL_CONST_COND_POP_()
const char * Plus1(const char *s)
bool ByConstRef(const string &s)
p0_type ReturnNew(p0_type p0)
const char * Binary(const char *input, short n)
int Ternary(int x, char y, short z)
bool prefix(Cursor &c, uint32_t expected)
string Concat4(const char *s1, const char *s2, const char *s3, const char *s4)
int operator()(int a, int b, int c, int d, int e)
boost::variant< DeliverAppData, WriteToSocket, ReportHandshakeSuccess, ReportEarlyHandshakeSuccess, ReportEarlyWriteFailed, ReportError, MutateState, WaitForData, NewCachedPsk > Action
ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param)
Action< F > MakeAction(ActionInterface< F > *impl)
#define EXPECT_STREQ(s1, s2)
ConcatImplActionP3< std::string, T1, T2 > Concat(const std::string &a, T1 b, T2 c)
NullaryConstructorClass()
std::array< typename return_type_helper< D, TList... >::type, sizeof...(TList)> return_type
constexpr auto empty(C const &c) -> decltype(c.empty())
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgPointee(const T &x)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
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)
static map< string, int > m
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)
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)
static const char *const value
#define EXPECT_TRUE(condition)
void VoidTernary(int, char, bool)
ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7)
Result Perform(const ArgumentTuple &args) const
#define EXPECT_DOUBLE_EQ(val1, val2)
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)
const char * CharPtr(const char *s)
bool StaticAssertTypeEq()
UnaryConstructorClass(int value)
ACTION_P4(Plus, p0, p1, p2, p3)
const internal::AnythingMatcher _
#define EXPECT_FALSE(condition)
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
string Concat6(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6)
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
internal::ReturnAction< R > Return(R value)