41# pragma warning(disable:4800)
83using testing::make_tuple;
85using testing::tuple_element;
87#if !GTEST_OS_WINDOWS_MOBILE
91#if GTEST_HAS_PROTOBUF_
92using testing::internal::TestMessage;
96TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
97 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
98 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
99 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
103TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
105 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
111TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
112 EXPECT_EQ(0
U, BuiltInDefaultValue<unsigned char>::Get());
113 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
114 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
115#if GMOCK_HAS_SIGNED_WCHAR_T_
116 EXPECT_EQ(0
U, BuiltInDefaultValue<unsigned wchar_t>::Get());
117 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
119#if GMOCK_WCHAR_T_IS_NATIVE_
120#if !defined(__WCHAR_UNSIGNED__)
121 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
123 EXPECT_EQ(0
U, BuiltInDefaultValue<wchar_t>::Get());
126 EXPECT_EQ(0
U, BuiltInDefaultValue<unsigned short>::Get());
127 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get());
128 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get());
129 EXPECT_EQ(0
U, BuiltInDefaultValue<unsigned int>::Get());
130 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
131 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
132 EXPECT_EQ(0
U, BuiltInDefaultValue<unsigned long>::Get());
133 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get());
134 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get());
135 EXPECT_EQ(0
U, BuiltInDefaultValue<UInt64>::Get());
136 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
137 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
138 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
143TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
144 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
145 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
147#if GMOCK_HAS_SIGNED_WCHAR_T_
148 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
149 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
151#if GMOCK_WCHAR_T_IS_NATIVE_
152 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
154 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists());
155 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());
157 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
158 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
160 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists());
161 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());
163 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
166 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
170TEST(BuiltInDefaultValueTest, IsFalseForBool) {
175TEST(BuiltInDefaultValueTest, BoolExists) {
181TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
182#if GTEST_HAS_GLOBAL_STRING
183 EXPECT_EQ(
"", BuiltInDefaultValue< ::string>::Get());
186 EXPECT_EQ(
"", BuiltInDefaultValue< ::std::string>::Get());
191TEST(BuiltInDefaultValueTest, ExistsForString) {
192#if GTEST_HAS_GLOBAL_STRING
193 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
196 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
201TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
202 EXPECT_EQ(
"", BuiltInDefaultValue<const std::string>::Get());
203 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
204 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
209class MyDefaultConstructible {
211 MyDefaultConstructible() : value_(42) {}
213 int value()
const {
return value_; }
220class MyNonDefaultConstructible {
223 explicit MyNonDefaultConstructible(
int a_value) : value_(a_value) {}
225 int value()
const {
return value_; }
233TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
234 EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
237TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
238 EXPECT_EQ(42, BuiltInDefaultValue<MyDefaultConstructible>::Get().
value());
243TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
244 EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
248TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
250 BuiltInDefaultValue<int&>::Get();
253 BuiltInDefaultValue<const char&>::Get();
257TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
259 BuiltInDefaultValue<MyNonDefaultConstructible>::Get();
264TEST(DefaultValueTest, IsInitiallyUnset) {
266 EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
267 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
271TEST(DefaultValueTest, CanBeSetAndUnset) {
273 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
275 DefaultValue<int>::Set(1);
276 DefaultValue<const MyNonDefaultConstructible>::Set(
277 MyNonDefaultConstructible(42));
280 EXPECT_EQ(42, DefaultValue<const MyNonDefaultConstructible>::Get().
value());
283 EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
285 DefaultValue<int>::Clear();
286 DefaultValue<const MyNonDefaultConstructible>::Clear();
289 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
292 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
298TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
301 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
302 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
307 DefaultValue<MyNonDefaultConstructible>::Get();
311#if GTEST_HAS_STD_UNIQUE_PTR_
312TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
313 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
314 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == NULL);
315 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
316 return std::unique_ptr<int>(
new int(42));
318 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
319 std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
325TEST(DefaultValueTest, GetWorksForVoid) {
326 return DefaultValue<void>::Get();
332TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
334 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
335 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
339TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
341 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
342 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
346TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
348 DefaultValue<const int&>::Set(n);
349 MyNonDefaultConstructible x(42);
350 DefaultValue<MyNonDefaultConstructible&>::Set(x);
353 EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
355 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
356 EXPECT_EQ(&x, &(DefaultValue<MyNonDefaultConstructible&>::Get()));
358 DefaultValue<const int&>::Clear();
359 DefaultValue<MyNonDefaultConstructible&>::Clear();
362 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
365 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
371TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
373 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
376 DefaultValue<int&>::Get();
379 DefaultValue<MyNonDefaultConstructible>::Get();
386typedef int MyGlobalFunction(
bool,
int);
388class MyActionImpl :
public ActionInterface<MyGlobalFunction> {
390 virtual int Perform(
const tuple<bool, int>& args) {
391 return get<0>(args) ? get<1>(args) : 0;
395TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
396 MyActionImpl my_action_impl;
397 (void)my_action_impl;
400TEST(ActionInterfaceTest, MakeAction) {
413TEST(ActionTest, CanBeConstructedFromActionInterface) {
414 Action<MyGlobalFunction>
action(
new MyActionImpl);
418TEST(ActionTest, DelegatesWorkToActionInterface) {
419 const Action<MyGlobalFunction>
action(
new MyActionImpl);
426TEST(ActionTest, IsCopyable) {
427 Action<MyGlobalFunction> a1(
new MyActionImpl);
428 Action<MyGlobalFunction> a2(a1);
431 EXPECT_EQ(5, a1.Perform(make_tuple(
true, 5)));
432 EXPECT_EQ(0, a1.Perform(make_tuple(
false, 1)));
435 EXPECT_EQ(5, a2.Perform(make_tuple(
true, 5)));
436 EXPECT_EQ(0, a2.Perform(make_tuple(
false, 1)));
441 EXPECT_EQ(5, a1.Perform(make_tuple(
true, 5)));
442 EXPECT_EQ(0, a1.Perform(make_tuple(
false, 1)));
445 EXPECT_EQ(5, a2.Perform(make_tuple(
true, 5)));
446 EXPECT_EQ(0, a2.Perform(make_tuple(
false, 1)));
452class IsNotZero :
public ActionInterface<bool(int)> {
454 virtual bool Perform(
const tuple<int>& arg) {
455 return get<0>(arg) != 0;
465TEST(ActionTest, CanBeConvertedToOtherActionType) {
466 const Action<bool(
int)> a1(
new IsNotZero);
467 const Action<int(
char)> a2 = Action<int(char)>(a1);
468 EXPECT_EQ(1, a2.Perform(make_tuple(
'a')));
469 EXPECT_EQ(0, a2.Perform(make_tuple(
'\0')));
477class ReturnSecondArgumentAction {
482 template <
typename Result,
typename ArgumentTuple>
483 Result Perform(
const ArgumentTuple& args) {
return get<1>(args); }
488class ReturnZeroFromNullaryFunctionAction {
497 template <
typename Result>
498 Result Perform(
const tuple<>&)
const {
return 0; }
504PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
505 return MakePolymorphicAction(ReturnSecondArgumentAction());
508PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
509ReturnZeroFromNullaryFunction() {
510 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
515TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
516 Action<int(
bool,
int,
double)> a1 = ReturnSecondArgument();
517 EXPECT_EQ(5, a1.Perform(make_tuple(
false, 5, 2.0)));
522TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
523 Action<int()> a1 = ReturnZeroFromNullaryFunction();
526 Action<
void*()> a2 = ReturnZeroFromNullaryFunction();
532TEST(ReturnTest, WorksForVoid) {
533 const Action<void(
int)>
ret = Return();
534 return ret.Perform(make_tuple(1));
538TEST(ReturnTest, ReturnsGivenValue) {
539 Action<int()>
ret = Return(1);
547TEST(ReturnTest, AcceptsStringLiteral) {
548 Action<
const char*()> a1 = Return(
"Hello");
551 Action<std::string()> a2 = Return(
"world");
552 EXPECT_EQ(
"world", a2.Perform(make_tuple()));
557struct IntegerVectorWrapper {
558 std::vector<int> * v;
559 IntegerVectorWrapper(std::vector<int>& _v) : v(&_v) {}
563TEST(ReturnTest, SupportsWrapperReturnType) {
566 for (
int i = 0; i < 5; ++i) v.push_back(i);
570 Action<IntegerVectorWrapper()>
a = Return(v);
571 const std::vector<int>& result = *(
a.Perform(make_tuple()).v);
581struct Derived :
public Base {
582 bool operator==(
const Derived&) {
return true; }
585TEST(ReturnTest, IsCovariant) {
588 Action<Base*()>
ret = Return(&base);
591 ret = Return(&derived);
601 explicit FromType(
bool* is_converted) : converted_(is_converted) {}
602 bool* converted()
const {
return converted_; }
605 bool*
const converted_;
613 ToType(
const FromType& x) { *x.converted() =
true; }
616TEST(ReturnTest, ConvertsArgumentWhenConverted) {
617 bool converted =
false;
618 FromType x(&converted);
619 Action<ToType()>
action(Return(x));
620 EXPECT_TRUE(converted) <<
"Return must convert its argument in its own "
621 <<
"conversion operator.";
623 action.Perform(tuple<>());
624 EXPECT_FALSE(converted) <<
"Action must NOT convert its argument "
625 <<
"when performed.";
628class DestinationType {};
633 operator DestinationType() {
return DestinationType(); }
636TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
638 Action<DestinationType()>
action(Return(
s));
642TEST(ReturnNullTest, WorksInPointerReturningFunction) {
643 const Action<
int*()> a1 = ReturnNull();
646 const Action<
const char*(bool)> a2 = ReturnNull();
650#if GTEST_HAS_STD_UNIQUE_PTR_
653TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
654 const Action<std::unique_ptr<const int>()> a1 = ReturnNull();
657 const Action<std::shared_ptr<int>(std::string)> a2 = ReturnNull();
658 EXPECT_TRUE(a2.Perform(make_tuple(
"foo")) ==
nullptr);
663TEST(ReturnRefTest, WorksForReference) {
665 const Action<
const int&(bool)>
ret = ReturnRef(n);
671TEST(ReturnRefTest, IsCovariant) {
674 Action<Base&()>
a = ReturnRef(base);
677 a = ReturnRef(derived);
678 EXPECT_EQ(&derived, &
a.Perform(make_tuple()));
682TEST(ReturnRefOfCopyTest, WorksForReference) {
684 const Action<
const int&()>
ret = ReturnRefOfCopy(n);
695TEST(ReturnRefOfCopyTest, IsCovariant) {
698 Action<Base&()>
a = ReturnRefOfCopy(base);
701 a = ReturnRefOfCopy(derived);
702 EXPECT_NE(&derived, &
a.Perform(make_tuple()));
713#if GTEST_HAS_STD_UNIQUE_PTR_
716 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
719 int(
const std::unique_ptr<int>&, std::unique_ptr<int>));
728TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
731 .WillOnce(DoDefault());
737TEST(DoDefaultDeathTest, DiesForUnknowType) {
740 .WillRepeatedly(DoDefault());
741#if GTEST_HAS_EXCEPTIONS
753void VoidFunc(
bool ) {}
755TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
758 .WillRepeatedly(DoAll(Invoke(VoidFunc),
772TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
773 DefaultValue<int>::Set(1);
776 .WillOnce(DoDefault());
778 DefaultValue<int>::Clear();
782TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
785 .WillByDefault(Return(2));
787 .WillOnce(DoDefault());
792TEST(DoDefaultTest, CannotBeUsedInOnCall) {
796 .WillByDefault(DoDefault());
797 },
"DoDefault() cannot be used in ON_CALL()");
802TEST(SetArgPointeeTest, SetsTheNthPointee) {
804 Action<MyFunction>
a = SetArgPointee<1>(2);
808 a.Perform(make_tuple(
true, &n, &ch));
812 a = SetArgPointee<2>(
'a');
815 a.Perform(make_tuple(
true, &n, &ch));
820#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
823TEST(SetArgPointeeTest, AcceptsStringLiteral) {
824 typedef void MyFunction(std::string*,
const char**);
825 Action<MyFunction>
a = SetArgPointee<0>(
"hi");
827 const char* ptr = NULL;
828 a.Perform(make_tuple(&str, &ptr));
832 a = SetArgPointee<1>(
"world");
834 a.Perform(make_tuple(&str, &ptr));
839TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
841 Action<MyFunction>
a = SetArgPointee<0>(L
"world");
842 const wchar_t* ptr = NULL;
843 a.Perform(make_tuple(&ptr));
846# if GTEST_HAS_STD_WSTRING
848 typedef void MyStringFunction(std::wstring*);
849 Action<MyStringFunction> a2 = SetArgPointee<0>(L
"world");
850 std::wstring
str = L
"";
851 a2.Perform(make_tuple(&str));
859TEST(SetArgPointeeTest, AcceptsCharPointer) {
860 typedef void MyFunction(
bool, std::string*,
const char**);
861 const char*
const hi =
"hi";
862 Action<MyFunction>
a = SetArgPointee<1>(hi);
864 const char* ptr = NULL;
865 a.Perform(make_tuple(
true, &str, &ptr));
869 char world_array[] =
"world";
870 char*
const world = world_array;
871 a = SetArgPointee<2>(world);
873 a.Perform(make_tuple(
true, &str, &ptr));
878TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
879 typedef void MyFunction(
bool,
const wchar_t**);
880 const wchar_t*
const hi = L
"hi";
881 Action<MyFunction>
a = SetArgPointee<1>(hi);
882 const wchar_t* ptr = NULL;
883 a.Perform(make_tuple(
true, &ptr));
886# if GTEST_HAS_STD_WSTRING
888 typedef void MyStringFunction(
bool, std::wstring*);
889 wchar_t world_array[] = L
"world";
890 wchar_t*
const world = world_array;
891 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
893 a2.Perform(make_tuple(
true, &str));
898#if GTEST_HAS_PROTOBUF_
902TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
903 TestMessage*
const msg =
new TestMessage;
904 msg->set_member(
"yes");
905 TestMessage orig_msg;
906 orig_msg.CopyFrom(*msg);
908 Action<void(
bool, TestMessage*)>
a = SetArgPointee<1>(*msg);
917 a.Perform(make_tuple(
true, &dest));
924TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
925 TestMessage*
const msg =
new TestMessage;
926 msg->set_member(
"yes");
927 TestMessage orig_msg;
928 orig_msg.CopyFrom(*msg);
930 Action<void(
bool, ::ProtocolMessage*)>
a = SetArgPointee<1>(*msg);
938 ::ProtocolMessage*
const dest_base = &dest;
940 a.Perform(make_tuple(
true, dest_base));
947TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
948 using testing::internal::FooMessage;
949 FooMessage*
const msg =
new FooMessage;
950 msg->set_int_field(2);
951 msg->set_string_field(
"hi");
953 orig_msg.CopyFrom(*msg);
955 Action<void(
bool, FooMessage*)>
a = SetArgPointee<1>(*msg);
963 dest.set_int_field(0);
964 a.Perform(make_tuple(
true, &dest));
972TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
973 using testing::internal::FooMessage;
974 FooMessage*
const msg =
new FooMessage;
975 msg->set_int_field(2);
976 msg->set_string_field(
"hi");
978 orig_msg.CopyFrom(*msg);
980 Action<void(
bool, ::proto2::Message*)>
a = SetArgPointee<1>(*msg);
988 dest.set_int_field(0);
989 ::proto2::Message*
const dest_base = &dest;
990 a.Perform(make_tuple(
true, dest_base));
999TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
1001 Action<MyFunction>
a = SetArgumentPointee<1>(2);
1005 a.Perform(make_tuple(
true, &n, &ch));
1009 a = SetArgumentPointee<2>(
'a');
1012 a.Perform(make_tuple(
true, &n, &ch));
1017#if GTEST_HAS_PROTOBUF_
1021TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
1022 TestMessage*
const msg =
new TestMessage;
1023 msg->set_member(
"yes");
1024 TestMessage orig_msg;
1025 orig_msg.CopyFrom(*msg);
1027 Action<void(
bool, TestMessage*)>
a = SetArgumentPointee<1>(*msg);
1036 a.Perform(make_tuple(
true, &dest));
1043TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
1044 TestMessage*
const msg =
new TestMessage;
1045 msg->set_member(
"yes");
1046 TestMessage orig_msg;
1047 orig_msg.CopyFrom(*msg);
1049 Action<void(
bool, ::ProtocolMessage*)>
a = SetArgumentPointee<1>(*msg);
1057 ::ProtocolMessage*
const dest_base = &dest;
1059 a.Perform(make_tuple(
true, dest_base));
1066TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
1067 using testing::internal::FooMessage;
1068 FooMessage*
const msg =
new FooMessage;
1069 msg->set_int_field(2);
1070 msg->set_string_field(
"hi");
1071 FooMessage orig_msg;
1072 orig_msg.CopyFrom(*msg);
1074 Action<void(
bool, FooMessage*)>
a = SetArgumentPointee<1>(*msg);
1082 dest.set_int_field(0);
1083 a.Perform(make_tuple(
true, &dest));
1091TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
1092 using testing::internal::FooMessage;
1093 FooMessage*
const msg =
new FooMessage;
1094 msg->set_int_field(2);
1095 msg->set_string_field(
"hi");
1096 FooMessage orig_msg;
1097 orig_msg.CopyFrom(*msg);
1099 Action<void(
bool, ::proto2::Message*)>
a = SetArgumentPointee<1>(*msg);
1107 dest.set_int_field(0);
1108 ::proto2::Message*
const dest_base = &dest;
1109 a.Perform(make_tuple(
true, dest_base));
1119class NullaryFunctor {
1121 int operator()() {
return 2; }
1127class VoidNullaryFunctor {
1129 void operator()() {
g_done =
true; }
1134 Foo() : value_(123) {}
1136 int Nullary()
const {
return value_; }
1143TEST(InvokeWithoutArgsTest, Function) {
1145 Action<int(
int)>
a = InvokeWithoutArgs(Nullary);
1149 Action<int(
int,
double)> a2 = InvokeWithoutArgs(Nullary);
1150 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
1153 Action<void(
int)> a3 = InvokeWithoutArgs(VoidNullary);
1155 a3.Perform(make_tuple(1));
1160TEST(InvokeWithoutArgsTest, Functor) {
1162 Action<int()>
a = InvokeWithoutArgs(NullaryFunctor());
1166 Action<int(
int,
double,
char)> a2 =
1167 InvokeWithoutArgs(NullaryFunctor());
1168 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5,
'a')));
1171 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1173 a3.Perform(make_tuple());
1180 Action<int(
bool,
char)>
a =
1181 InvokeWithoutArgs(&
foo, &Foo::Nullary);
1182 EXPECT_EQ(123,
a.Perform(make_tuple(
true,
'a')));
1186TEST(IgnoreResultTest, PolymorphicAction) {
1187 Action<void(
int)>
a = IgnoreResult(Return(5));
1188 a.Perform(make_tuple(1));
1198TEST(IgnoreResultTest, MonomorphicAction) {
1200 Action<void()>
a = IgnoreResult(Invoke(ReturnOne));
1201 a.Perform(make_tuple());
1207MyNonDefaultConstructible ReturnMyNonDefaultConstructible(
double ) {
1209 return MyNonDefaultConstructible(42);
1212TEST(IgnoreResultTest, ActionReturningClass) {
1214 Action<void(
int)>
a =
1215 IgnoreResult(Invoke(ReturnMyNonDefaultConstructible));
1216 a.Perform(make_tuple(2));
1220TEST(AssignTest, Int) {
1222 Action<void(
int)>
a = Assign(&x, 5);
1223 a.Perform(make_tuple(0));
1227TEST(AssignTest, String) {
1229 Action<void(
void)>
a = Assign(&x,
"Hello, world");
1230 a.Perform(make_tuple());
1234TEST(AssignTest, CompatibleTypes) {
1236 Action<void(
int)>
a = Assign(&x, 5);
1237 a.Perform(make_tuple(0));
1241#if !GTEST_OS_WINDOWS_MOBILE
1245 virtual void SetUp() { errno = 0; }
1246 virtual void TearDown() { errno = 0; }
1249TEST_F(SetErrnoAndReturnTest, Int) {
1250 Action<int(
void)>
a = SetErrnoAndReturn(ENOTTY, -5);
1255TEST_F(SetErrnoAndReturnTest, Ptr) {
1257 Action<
int*(void)>
a = SetErrnoAndReturn(ENOTTY, &x);
1262TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1263 Action<double()>
a = SetErrnoAndReturn(EINVAL, 5);
1273TEST(ByRefTest, IsCopyable) {
1274 const std::string s1 =
"Hi";
1275 const std::string s2 =
"Hello";
1279 const std::string& r1 = ref_wrapper;
1283 ref_wrapper = ByRef(s2);
1284 const std::string& r2 = ref_wrapper;
1290 ref_wrapper = ref_wrapper1;
1291 const std::string& r3 = ref_wrapper;
1296TEST(ByRefTest, ConstValue) {
1300 const int& const_ref = ByRef(n);
1305TEST(ByRefTest, NonConstValue) {
1309 int& ref = ByRef(n);
1313 const int& const_ref = ByRef(n);
1318TEST(ByRefTest, ExplicitType) {
1320 const int& r1 = ByRef<const int>(n);
1327 Derived& r2 = ByRef<Derived>(d);
1330 const Derived& r3 = ByRef<const Derived>(d);
1333 Base& r4 = ByRef<Base>(d);
1336 const Base& r5 = ByRef<const Base>(d);
1347TEST(ByRefTest, PrintsCorrectly) {
1349 ::std::stringstream expected, actual;
1352 EXPECT_EQ(expected.str(), actual.str());
1355#if GTEST_HAS_STD_UNIQUE_PTR_
1357std::unique_ptr<int> UniquePtrSource() {
1358 return std::unique_ptr<int>(
new int(19));
1361std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1362 std::vector<std::unique_ptr<int>> out;
1363 out.emplace_back(
new int(7));
1367TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1369 std::unique_ptr<int> i(
new int(19));
1370 EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
1372 .WillOnce(Return(ByMove(VectorUniquePtrSource())));
1373 Derived*
d =
new Derived;
1375 .WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));
1377 std::unique_ptr<int> result1 = mock.MakeUnique();
1380 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1385 std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1389TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1392 std::unique_ptr<int> i(
new int(19));
1396 Return(ByMove(std::move(i)))));
1398 std::unique_ptr<int> result1 = mock.MakeUnique();
1402TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
1406 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1407 return std::unique_ptr<int>(
new int(42));
1411 EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
1413 .WillRepeatedly(Invoke(VectorUniquePtrSource));
1414 std::unique_ptr<int> result1 = mock.MakeUnique();
1416 std::unique_ptr<int> result2 = mock.MakeUnique();
1420 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1426TEST(MockMethodTest, CanTakeMoveOnlyValue) {
1428 auto make = [](
int i) {
return std::unique_ptr<int>(
new int(i)); };
1430 EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr<int> i) {
1438 .WillOnce(Return(-7))
1439 .RetiresOnSaturation();
1441 .WillOnce(Return(-1))
1442 .RetiresOnSaturation();
1445 EXPECT_EQ(-7, mock.TakeUnique(make(7)));
1451 auto lvalue = make(6);
1453 .WillOnce([](
const std::unique_ptr<int>& i, std::unique_ptr<int>
j) {
1456 EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7)));
1459 std::unique_ptr<int> saved;
1460 EXPECT_CALL(mock, TakeUnique(_)).WillOnce([&saved](std::unique_ptr<int> i) {
1461 saved = std::move(i);
1464 EXPECT_EQ(0, mock.TakeUnique(make(42)));
1473int Add(
int val,
int& ref,
int* ptr) {
1474 int result = val + ref + *ptr;
1480int Deref(std::unique_ptr<int> ptr) {
return *ptr; }
1483 template <
typename T>
1484 T operator()(
T t) {
return 2 * t; }
1487std::unique_ptr<int> UniqueInt(
int i) {
1488 return std::unique_ptr<int>(
new int(i));
1491TEST(FunctorActionTest, ActionFromFunction) {
1492 Action<int(
int,
int&,
int*)>
a = &Add;
1493 int x = 1,
y = 2, z = 3;
1494 EXPECT_EQ(6,
a.Perform(std::forward_as_tuple(x, y, &z)));
1498 Action<int(std::unique_ptr<int>)> a1 = &Deref;
1499 EXPECT_EQ(7, a1.Perform(std::make_tuple(UniqueInt(7))));
1502TEST(FunctorActionTest, ActionFromLambda) {
1503 Action<int(
bool,
int)> a1 = [](
bool b,
int i) {
return b ? i : 0; };
1504 EXPECT_EQ(5, a1.Perform(make_tuple(
true, 5)));
1505 EXPECT_EQ(0, a1.Perform(make_tuple(
false, 5)));
1507 std::unique_ptr<int> saved;
1508 Action<void(std::unique_ptr<int>)> a2 = [&saved](std::unique_ptr<int>
p) {
1509 saved = std::move(
p);
1511 a2.Perform(make_tuple(UniqueInt(5)));
1515TEST(FunctorActionTest, PolymorphicFunctor) {
1516 Action<int(
int)> ai =
Double();
1517 EXPECT_EQ(2, ai.Perform(make_tuple(1)));
1518 Action<double(
double)> ad =
Double();
1519 EXPECT_EQ(3.0, ad.Perform(make_tuple(1.5)));
1522TEST(FunctorActionTest, TypeConversion) {
1524 const Action<bool(
int)> a1 = [](
int i) {
return i > 1; };
1525 const Action<int(
bool)> a2 = Action<int(bool)>(a1);
1526 EXPECT_EQ(1, a1.Perform(make_tuple(42)));
1527 EXPECT_EQ(0, a2.Perform(make_tuple(42)));
1530 const Action<bool(std::string)> s1 = [](std::string
s) {
return !
s.empty(); };
1531 const Action<int(
const char*)> s2 = Action<int(const char*)>(s1);
1532 EXPECT_EQ(0, s2.Perform(make_tuple(
"")));
1533 EXPECT_EQ(1, s2.Perform(make_tuple(
"hello")));
1536 const Action<bool(std::string)> x = [](
Unused) {
return 42; };
1540TEST(FunctorActionTest, UnusedArguments) {
1542 Action<int(
int,
double y,
double z)>
a =
1544 tuple<int, double, double>
dummy = make_tuple(3, 7.3, 9.44);
1553TEST(MoveOnlyArgumentsTest, ReturningActions) {
1554 Action<int(std::unique_ptr<int>)>
a = Return(1);
1555 EXPECT_EQ(1,
a.Perform(make_tuple(
nullptr)));
1558 EXPECT_EQ(7,
a.Perform(make_tuple(
nullptr)));
1562 a2.Perform(make_tuple(
nullptr, &x));
1572# pragma warning(pop)
static void Print(const T &value, ::std::ostream *os)
bool operator==(const environment &other)
#define MOCK_METHOD0(m,...)
#define MOCK_METHOD2(m,...)
#define MOCK_METHOD1(m,...)
#define EXPECT_THAT(value, matcher)
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define GTEST_DISALLOW_ASSIGN_(type)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define TEST_F(test_fixture, test_name)
#define EXPECT_EQ(val1, val2)
#define EXPECT_NE(val1, val2)
#define EXPECT_ANY_THROW(statement)
#define EXPECT_DOUBLE_EQ(val1, val2)
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(s1, s2)
#define TEST(test_case_name, test_name)
#define EXPECT_FALSE(condition)
constexpr enabler dummy
An instance to use in EnableIf.
static const Reg8 ch(Operand::CH)
TypeWithSize< 8 >::Int Int64
void UniversalPrint(const T &value, ::std::ostream *os)
FloatingPoint< double > Double
TypeWithSize< 8 >::UInt UInt64
internal::Ne2Matcher Ne()
internal::IgnoreResultAction< A > IgnoreResult(const A &an_action)
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
PolymorphicMatcher< internal::IsNullMatcher > IsNull()
internal::ByMoveWrapper< R > ByMove(R x)
PolymorphicAction< Impl > MakePolymorphicAction(const Impl &impl)
PolymorphicAction< internal::ReturnVoidAction > Return()
const internal::AnythingMatcher _
internal::IgnoredValue Unused
PolymorphicAction< internal::AssignAction< T1, T2 > > Assign(T1 *ptr, T2 val)
PolymorphicAction< internal::SetErrnoAndReturnAction< T > > SetErrnoAndReturn(int errval, T result)
internal::ElementsAreMatcher< ::testing::tuple<> > ElementsAre()
Action< F > MakeAction(ActionInterface< F > *impl)
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgumentPointee(const T &x)
internal::ReturnRefOfCopyAction< R > ReturnRefOfCopy(const R &x)
internal::ReferenceWrapper< T > ByRef(T &l_value)
internal::WithArgsAction< InnerAction > WithoutArgs(const InnerAction &action)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
internal::ReturnRefAction< R > ReturnRef(R &x)
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgPointee(const T &x)
internal::DoDefaultAction DoDefault()
PolymorphicAction< internal::ReturnNullAction > ReturnNull()
internal::PointeeMatcher< InnerMatcher > Pointee(const InnerMatcher &inner_matcher)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
#define T(meth, val, expected)