37 # pragma warning(push)
38 # pragma warning(disable:4100)
42 # pragma warning(disable:4800)
51 #include <type_traits>
60 using ::testing::Action;
61 using ::testing::ActionInterface;
65 using ::testing::DefaultValue;
72 using ::testing::PolymorphicAction;
83 using ::testing::internal::BuiltInDefaultValue;
85 #if !GTEST_OS_WINDOWS_MOBILE
90 TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
91 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() ==
nullptr);
92 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() ==
nullptr);
93 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() ==
nullptr);
97 TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
99 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
105 TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
106 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
107 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
108 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
109 #if GMOCK_WCHAR_T_IS_NATIVE_
110 #if !defined(__WCHAR_UNSIGNED__)
111 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
113 EXPECT_EQ(0U, BuiltInDefaultValue<wchar_t>::Get());
116 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get());
117 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get());
118 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get());
119 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
120 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
121 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
122 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get());
123 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get());
124 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get());
125 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long long>::Get());
126 EXPECT_EQ(0, BuiltInDefaultValue<signed long long>::Get());
127 EXPECT_EQ(0, BuiltInDefaultValue<long long>::Get());
128 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
129 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
134 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
135 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
136 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
138 #if GMOCK_WCHAR_T_IS_NATIVE_
139 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
141 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());
144 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
145 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
147 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists());
148 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());
150 EXPECT_TRUE(BuiltInDefaultValue<unsigned long long>::Exists());
151 EXPECT_TRUE(BuiltInDefaultValue<signed long long>::Exists());
152 EXPECT_TRUE(BuiltInDefaultValue<long long>::Exists());
154 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
158 TEST(BuiltInDefaultValueTest, IsFalseForBool) {
163 TEST(BuiltInDefaultValueTest, BoolExists) {
169 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
170 EXPECT_EQ(
"", BuiltInDefaultValue< ::std::string>::Get());
175 TEST(BuiltInDefaultValueTest, ExistsForString) {
176 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
181 TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
182 EXPECT_EQ(
"", BuiltInDefaultValue<const std::string>::Get());
183 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
184 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() ==
nullptr);
189 class MyDefaultConstructible {
191 MyDefaultConstructible() :
value_(42) {}
200 class MyNonDefaultConstructible {
203 explicit MyNonDefaultConstructible(
int a_value) :
value_(a_value) {}
212 TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
213 EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
216 TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
217 EXPECT_EQ(42, BuiltInDefaultValue<MyDefaultConstructible>::Get().
value());
221 TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
222 EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
226 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
228 BuiltInDefaultValue<int&>::Get();
231 BuiltInDefaultValue<const char&>::Get();
235 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
237 BuiltInDefaultValue<MyNonDefaultConstructible>::Get();
242 TEST(DefaultValueTest, IsInitiallyUnset) {
244 EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
245 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
249 TEST(DefaultValueTest, CanBeSetAndUnset) {
251 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
253 DefaultValue<int>::Set(1);
254 DefaultValue<const MyNonDefaultConstructible>::Set(
255 MyNonDefaultConstructible(42));
258 EXPECT_EQ(42, DefaultValue<const MyNonDefaultConstructible>::Get().
value());
261 EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
263 DefaultValue<int>::Clear();
264 DefaultValue<const MyNonDefaultConstructible>::Clear();
267 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
270 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
276 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
279 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
280 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
285 DefaultValue<MyNonDefaultConstructible>::Get();
289 TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
290 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
291 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() ==
nullptr);
292 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
293 return std::unique_ptr<int>(
new int(42));
295 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
296 std::unique_ptr<int>
i = DefaultValue<std::unique_ptr<int>>::Get();
301 TEST(DefaultValueTest, GetWorksForVoid) {
302 return DefaultValue<void>::Get();
308 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
310 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
311 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
315 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
317 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
318 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
322 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
324 DefaultValue<const int&>::Set(n);
325 MyNonDefaultConstructible
x(42);
326 DefaultValue<MyNonDefaultConstructible&>::Set(
x);
329 EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
331 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
332 EXPECT_EQ(&
x, &(DefaultValue<MyNonDefaultConstructible&>::Get()));
334 DefaultValue<const int&>::Clear();
335 DefaultValue<MyNonDefaultConstructible&>::Clear();
338 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
341 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
347 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
349 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
352 DefaultValue<int&>::Get();
355 DefaultValue<MyNonDefaultConstructible>::Get();
362 typedef int MyGlobalFunction(
bool,
int);
364 class MyActionImpl :
public ActionInterface<MyGlobalFunction> {
366 int Perform(
const std::tuple<bool, int>& args)
override {
367 return std::get<0>(args) ? std::get<1>(args) : 0;
371 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
372 MyActionImpl my_action_impl;
373 (
void)my_action_impl;
377 Action<MyGlobalFunction> action =
MakeAction(
new MyActionImpl);
384 EXPECT_EQ(5, action.Perform(std::make_tuple(
true, 5)));
389 TEST(ActionTest, CanBeConstructedFromActionInterface) {
390 Action<MyGlobalFunction> action(
new MyActionImpl);
394 TEST(ActionTest, DelegatesWorkToActionInterface) {
395 const Action<MyGlobalFunction> action(
new MyActionImpl);
397 EXPECT_EQ(5, action.Perform(std::make_tuple(
true, 5)));
398 EXPECT_EQ(0, action.Perform(std::make_tuple(
false, 1)));
402 TEST(ActionTest, IsCopyable) {
403 Action<MyGlobalFunction> a1(
new MyActionImpl);
404 Action<MyGlobalFunction> a2(a1);
407 EXPECT_EQ(5, a1.Perform(std::make_tuple(
true, 5)));
408 EXPECT_EQ(0, a1.Perform(std::make_tuple(
false, 1)));
411 EXPECT_EQ(5, a2.Perform(std::make_tuple(
true, 5)));
412 EXPECT_EQ(0, a2.Perform(std::make_tuple(
false, 1)));
417 EXPECT_EQ(5, a1.Perform(std::make_tuple(
true, 5)));
418 EXPECT_EQ(0, a1.Perform(std::make_tuple(
false, 1)));
421 EXPECT_EQ(5, a2.Perform(std::make_tuple(
true, 5)));
422 EXPECT_EQ(0, a2.Perform(std::make_tuple(
false, 1)));
428 class IsNotZero :
public ActionInterface<bool(int)> {
430 bool Perform(
const std::tuple<int>& arg)
override {
431 return std::get<0>(arg) != 0;
435 TEST(ActionTest, CanBeConvertedToOtherActionType) {
436 const Action<bool(int)> a1(
new IsNotZero);
437 const Action<int(char)> a2 = Action<int(char)>(a1);
438 EXPECT_EQ(1, a2.Perform(std::make_tuple(
'a')));
439 EXPECT_EQ(0, a2.Perform(std::make_tuple(
'\0')));
446 class ReturnSecondArgumentAction {
451 template <
typename Result,
typename ArgumentTuple>
452 Result Perform(
const ArgumentTuple& args) {
453 return std::get<1>(args);
459 class ReturnZeroFromNullaryFunctionAction {
468 template <
typename Result>
469 Result Perform(
const std::tuple<>&)
const {
477 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
481 PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
482 ReturnZeroFromNullaryFunction() {
488 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
489 Action<int(bool, int, double)> a1 = ReturnSecondArgument();
490 EXPECT_EQ(5, a1.Perform(std::make_tuple(
false, 5, 2.0)));
495 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
496 Action<int()> a1 = ReturnZeroFromNullaryFunction();
497 EXPECT_EQ(0, a1.Perform(std::make_tuple()));
499 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
500 EXPECT_TRUE(a2.Perform(std::make_tuple()) ==
nullptr);
505 TEST(ReturnTest, WorksForVoid) {
506 const Action<void(int)> ret =
Return();
507 return ret.Perform(std::make_tuple(1));
511 TEST(ReturnTest, ReturnsGivenValue) {
512 Action<int()> ret =
Return(1);
513 EXPECT_EQ(1, ret.Perform(std::make_tuple()));
516 EXPECT_EQ(-5, ret.Perform(std::make_tuple()));
520 TEST(ReturnTest, AcceptsStringLiteral) {
521 Action<const char*()> a1 =
Return(
"Hello");
524 Action<std::string()> a2 =
Return(
"world");
525 EXPECT_EQ(
"world", a2.Perform(std::make_tuple()));
530 struct IntegerVectorWrapper {
531 std::vector<int> * v;
532 IntegerVectorWrapper(std::vector<int>& _v) : v(&_v) {}
536 TEST(ReturnTest, SupportsWrapperReturnType) {
539 for (
int i = 0;
i < 5; ++
i) v.push_back(
i);
543 Action<IntegerVectorWrapper()>
a =
Return(v);
544 const std::vector<int>& result = *(a.Perform(std::make_tuple()).v);
545 EXPECT_THAT(result, ::testing::ElementsAre(0, 1, 2, 3, 4));
554 struct Derived :
public Base {
555 bool operator==(
const Derived&) {
return true; }
558 TEST(ReturnTest, IsCovariant) {
561 Action<Base*()> ret =
Return(&base);
562 EXPECT_EQ(&base, ret.Perform(std::make_tuple()));
565 EXPECT_EQ(&derived, ret.Perform(std::make_tuple()));
574 explicit FromType(
bool* is_converted) : converted_(is_converted) {}
575 bool* converted()
const {
return converted_; }
578 bool*
const converted_;
584 ToType(
const FromType&
x) { *x.converted() =
true; }
587 TEST(ReturnTest, ConvertsArgumentWhenConverted) {
588 bool converted =
false;
589 FromType
x(&converted);
590 Action<ToType()> action(
Return(
x));
591 EXPECT_TRUE(converted) <<
"Return must convert its argument in its own "
592 <<
"conversion operator.";
594 action.Perform(std::tuple<>());
595 EXPECT_FALSE(converted) <<
"Action must NOT convert its argument "
596 <<
"when performed.";
599 class DestinationType {};
604 operator DestinationType() {
return DestinationType(); }
607 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
609 Action<DestinationType()> action(
Return(s));
613 TEST(ReturnNullTest, WorksInPointerReturningFunction) {
615 EXPECT_TRUE(a1.Perform(std::make_tuple()) ==
nullptr);
617 const Action<const char*(bool)> a2 =
ReturnNull();
618 EXPECT_TRUE(a2.Perform(std::make_tuple(
true)) ==
nullptr);
623 TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
624 const Action<std::unique_ptr<const int>()> a1 =
ReturnNull();
625 EXPECT_TRUE(a1.Perform(std::make_tuple()) ==
nullptr);
627 const Action<std::shared_ptr<int>(std::string)> a2 =
ReturnNull();
628 EXPECT_TRUE(a2.Perform(std::make_tuple(
"foo")) ==
nullptr);
632 TEST(ReturnRefTest, WorksForReference) {
634 const Action<const int&(bool)> ret =
ReturnRef(n);
636 EXPECT_EQ(&n, &ret.Perform(std::make_tuple(
true)));
640 TEST(ReturnRefTest, IsCovariant) {
644 EXPECT_EQ(&base, &a.Perform(std::make_tuple()));
647 EXPECT_EQ(&derived, &a.Perform(std::make_tuple()));
650 template <
typename T,
typename = decltype(ReturnRef(std::declval<T&&>()))>
651 bool CanCallReturnRef(
T&&) {
return true; }
652 bool CanCallReturnRef(
Unused) {
return false; }
655 TEST(ReturnRefTest, WorksForNonTemporary) {
656 int scalar_value = 123;
659 std::string non_scalar_value(
"ABC");
662 const int const_scalar_value{321};
665 const std::string const_non_scalar_value(
"CBA");
666 EXPECT_TRUE(CanCallReturnRef(const_non_scalar_value));
670 TEST(ReturnRefTest, DoesNotWorkForTemporary) {
671 auto scalar_value = []() ->
int {
return 123; };
674 auto non_scalar_value = []() -> std::string {
return "ABC"; };
679 EXPECT_FALSE(CanCallReturnRef(static_cast<const int>(321)));
681 auto const_non_scalar_value = []() ->
const std::string {
return "CBA"; };
682 EXPECT_FALSE(CanCallReturnRef(const_non_scalar_value()));
686 TEST(ReturnRefOfCopyTest, WorksForReference) {
690 EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
691 EXPECT_EQ(42, ret.Perform(std::make_tuple()));
694 EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
695 EXPECT_EQ(42, ret.Perform(std::make_tuple()));
699 TEST(ReturnRefOfCopyTest, IsCovariant) {
703 EXPECT_NE(&base, &a.Perform(std::make_tuple()));
706 EXPECT_NE(&derived, &a.Perform(std::make_tuple()));
710 TEST(ReturnRoundRobinTest, WorksForInitList) {
713 EXPECT_EQ(1, ret.Perform(std::make_tuple()));
714 EXPECT_EQ(2, ret.Perform(std::make_tuple()));
715 EXPECT_EQ(3, ret.Perform(std::make_tuple()));
716 EXPECT_EQ(1, ret.Perform(std::make_tuple()));
717 EXPECT_EQ(2, ret.Perform(std::make_tuple()));
718 EXPECT_EQ(3, ret.Perform(std::make_tuple()));
722 TEST(ReturnRoundRobinTest, WorksForVector) {
723 std::vector<double> v = {4.4, 5.5, 6.6};
726 EXPECT_EQ(4.4, ret.Perform(std::make_tuple()));
727 EXPECT_EQ(5.5, ret.Perform(std::make_tuple()));
728 EXPECT_EQ(6.6, ret.Perform(std::make_tuple()));
729 EXPECT_EQ(4.4, ret.Perform(std::make_tuple()));
730 EXPECT_EQ(5.5, ret.Perform(std::make_tuple()));
731 EXPECT_EQ(6.6, ret.Perform(std::make_tuple()));
744 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
747 int(
const std::unique_ptr<int>&, std::unique_ptr<int>));
755 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
764 TEST(DoDefaultDeathTest, DiesForUnknowType) {
768 #if GTEST_HAS_EXCEPTIONS
780 void VoidFunc(
bool ) {}
782 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
799 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
800 DefaultValue<int>::Set(1);
805 DefaultValue<int>::Clear();
809 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
812 .WillByDefault(
Return(2));
819 TEST(DoDefaultTest, CannotBeUsedInOnCall) {
824 },
"DoDefault() cannot be used in ON_CALL()");
829 TEST(SetArgPointeeTest, SetsTheNthPointee) {
831 Action<MyFunction> a = SetArgPointee<1>(2);
835 a.Perform(std::make_tuple(
true, &n, &ch));
839 a = SetArgPointee<2>(
'a');
842 a.Perform(std::make_tuple(
true, &n, &ch));
848 TEST(SetArgPointeeTest, AcceptsStringLiteral) {
849 typedef void MyFunction(std::string*,
const char**);
850 Action<MyFunction> a = SetArgPointee<0>(
"hi");
852 const char* ptr =
nullptr;
853 a.Perform(std::make_tuple(&str, &ptr));
857 a = SetArgPointee<1>(
"world");
859 a.Perform(std::make_tuple(&str, &ptr));
864 TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
866 Action<MyFunction> a = SetArgPointee<0>(L
"world");
867 const wchar_t* ptr =
nullptr;
868 a.Perform(std::make_tuple(&ptr));
871 # if GTEST_HAS_STD_WSTRING
873 typedef void MyStringFunction(std::wstring*);
874 Action<MyStringFunction> a2 = SetArgPointee<0>(L
"world");
875 std::wstring str = L
"";
876 a2.Perform(std::make_tuple(&str));
883 TEST(SetArgPointeeTest, AcceptsCharPointer) {
884 typedef void MyFunction(
bool, std::string*,
const char**);
885 const char*
const hi =
"hi";
886 Action<MyFunction> a = SetArgPointee<1>(hi);
888 const char* ptr =
nullptr;
889 a.Perform(std::make_tuple(
true, &str, &ptr));
893 char world_array[] =
"world";
894 char*
const world = world_array;
895 a = SetArgPointee<2>(world);
897 a.Perform(std::make_tuple(
true, &str, &ptr));
902 TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
903 typedef void MyFunction(
bool,
const wchar_t**);
904 const wchar_t*
const hi = L
"hi";
905 Action<MyFunction> a = SetArgPointee<1>(hi);
906 const wchar_t* ptr =
nullptr;
907 a.Perform(std::make_tuple(
true, &ptr));
910 # if GTEST_HAS_STD_WSTRING
912 typedef void MyStringFunction(
bool, std::wstring*);
913 wchar_t world_array[] = L
"world";
914 wchar_t*
const world = world_array;
915 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
917 a2.Perform(std::make_tuple(
true, &str));
924 TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
926 Action<MyFunction> a = SetArgumentPointee<1>(2);
930 a.Perform(std::make_tuple(
true, &n, &ch));
934 a = SetArgumentPointee<2>(
'a');
937 a.Perform(std::make_tuple(
true, &n, &ch));
945 class NullaryFunctor {
947 int operator()() {
return 2; }
953 class VoidNullaryFunctor {
955 void operator()() { g_done =
true; }
958 short Short(
short n) {
return n; }
959 char Char(
char ch) {
return ch; }
961 const char*
CharPtr(
const char* s) {
return s; }
963 bool Unary(
int x) {
return x < 0; }
965 const char*
Binary(
const char* input,
short n) {
return input + n; }
969 int Ternary(
int x,
char y,
short z) {
return x + y +
z; }
971 int SumOf4(
int a,
int b,
int c,
int d) {
return a + b + c + d; }
984 TEST(InvokeWithoutArgsTest, Function) {
987 EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
991 EXPECT_EQ(1, a2.Perform(std::make_tuple(2, 3.5)));
996 a3.Perform(std::make_tuple(1));
1001 TEST(InvokeWithoutArgsTest, Functor) {
1004 EXPECT_EQ(2, a.Perform(std::make_tuple()));
1007 Action<int(int, double, char)> a2 =
1009 EXPECT_EQ(2, a2.Perform(std::make_tuple(3, 3.5,
'a')));
1014 a3.Perform(std::make_tuple());
1021 Action<int(bool, char)> a =
1023 EXPECT_EQ(123, a.Perform(std::make_tuple(
true,
'a')));
1027 TEST(IgnoreResultTest, PolymorphicAction) {
1029 a.Perform(std::make_tuple(1));
1039 TEST(IgnoreResultTest, MonomorphicAction) {
1042 a.Perform(std::make_tuple());
1048 MyNonDefaultConstructible ReturnMyNonDefaultConstructible(
double ) {
1050 return MyNonDefaultConstructible(42);
1053 TEST(IgnoreResultTest, ActionReturningClass) {
1055 Action<void(int)> a =
1057 a.Perform(std::make_tuple(2));
1061 TEST(AssignTest, Int) {
1063 Action<void(int)> a =
Assign(&x, 5);
1064 a.Perform(std::make_tuple(0));
1068 TEST(AssignTest, String) {
1070 Action<void(void)> a =
Assign(&x,
"Hello, world");
1071 a.Perform(std::make_tuple());
1075 TEST(AssignTest, CompatibleTypes) {
1077 Action<void(int)> a =
Assign(&x, 5);
1078 a.Perform(std::make_tuple(0));
1084 TEST(WithArgsTest, OneArg) {
1085 Action<bool(double x, int n)> a = WithArgs<1>(
Invoke(
Unary));
1091 TEST(WithArgsTest, TwoArgs) {
1092 Action<const char*(const char* s, double x, short n)> a =
1094 const char s[] =
"Hello";
1099 std::string operator()()
const {
return {}; }
1100 template <
typename... I>
1101 std::string operator()(
const char* a, I...
i)
const {
1102 return a + ConcatAll()(
i...);
1107 TEST(WithArgsTest, TenArgs) {
1108 Action<std::string(const char*, const char*, const char*, const char*)> a =
1109 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(
Invoke(ConcatAll{}));
1116 class SubtractAction :
public ActionInterface<int(int, int)> {
1118 int Perform(
const std::tuple<int, int>& args)
override {
1119 return std::get<0>(args) - std::get<1>(args);
1123 TEST(WithArgsTest, NonInvokeAction) {
1124 Action<int(const std::string&, int, int)> a =
1125 WithArgs<2, 1>(
MakeAction(
new SubtractAction));
1126 std::tuple<std::string, int, int> dummy =
1127 std::make_tuple(std::string(
"hi"), 2, 10);
1132 TEST(WithArgsTest, Identity) {
1133 Action<int(int x, char y, short z)> a =
1139 TEST(WithArgsTest, RepeatedArguments) {
1140 Action<int(bool, int m, int n)> a =
1142 EXPECT_EQ(4, a.Perform(std::make_tuple(
false, 1, 10)));
1146 TEST(WithArgsTest, ReversedArgumentOrder) {
1147 Action<const char*(short n, const char* input)> a =
1149 const char s[] =
"Hello";
1154 TEST(WithArgsTest, ArgsOfCompatibleTypes) {
1155 Action<long(short x, char y, double z, char c)> a =
1158 a.Perform(std::make_tuple(
Short(100),
Char(20), 5.6,
Char(3))));
1162 TEST(WithArgsTest, VoidAction) {
1163 Action<void(double x, char c, int n)> a = WithArgs<2, 1>(
Invoke(
VoidBinary));
1165 a.Perform(std::make_tuple(1.5,
'a', 3));
1169 TEST(WithArgsTest, ReturnReference) {
1170 Action<int&(int&, void*)> aa = WithArgs<0>([](
int&
a) ->
int& {
return a; });
1172 const int& res = aa.Perform(std::forward_as_tuple(i,
nullptr));
1176 TEST(WithArgsTest, InnerActionWithConversion) {
1177 Action<Derived*()> inner = [] {
return nullptr; };
1179 EXPECT_EQ(
nullptr, a.Perform(std::make_tuple(1.1)));
1182 #if !GTEST_OS_WINDOWS_MOBILE
1186 void SetUp()
override { errno = 0; }
1187 void TearDown()
override { errno = 0; }
1190 TEST_F(SetErrnoAndReturnTest, Int) {
1192 EXPECT_EQ(-5, a.Perform(std::make_tuple()));
1196 TEST_F(SetErrnoAndReturnTest, Ptr) {
1199 EXPECT_EQ(&x, a.Perform(std::make_tuple()));
1203 TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1209 #endif // !GTEST_OS_WINDOWS_MOBILE
1214 TEST(ByRefTest, IsCopyable) {
1215 const std::string s1 =
"Hi";
1216 const std::string s2 =
"Hello";
1218 auto ref_wrapper =
ByRef(s1);
1219 const std::string& r1 = ref_wrapper;
1223 ref_wrapper =
ByRef(s2);
1224 const std::string& r2 = ref_wrapper;
1227 auto ref_wrapper1 =
ByRef(s1);
1229 ref_wrapper = ref_wrapper1;
1230 const std::string& r3 = ref_wrapper;
1235 TEST(ByRefTest, ConstValue) {
1239 const int& const_ref =
ByRef(n);
1244 TEST(ByRefTest, NonConstValue) {
1248 int& ref =
ByRef(n);
1252 const int& const_ref =
ByRef(n);
1257 TEST(ByRefTest, ExplicitType) {
1259 const int& r1 = ByRef<const int>(n);
1266 Derived& r2 = ByRef<Derived>(d);
1269 const Derived& r3 = ByRef<const Derived>(d);
1272 Base& r4 = ByRef<Base>(d);
1275 const Base& r5 = ByRef<const Base>(d);
1286 TEST(ByRefTest, PrintsCorrectly) {
1288 ::std::stringstream
expected, actual;
1291 EXPECT_EQ(expected.str(), actual.str());
1294 struct UnaryConstructorClass {
1295 explicit UnaryConstructorClass(
int v) :
value(v) {}
1301 Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
1302 UnaryConstructorClass*
c = a.Perform(std::make_tuple());
1307 TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
1308 Action<UnaryConstructorClass*(bool, int)> a =
1309 ReturnNew<UnaryConstructorClass>(4000);
1310 UnaryConstructorClass* c = a.Perform(std::make_tuple(
false, 5));
1315 TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
1316 Action<const UnaryConstructorClass*()> a =
1317 ReturnNew<UnaryConstructorClass>(4000);
1318 const UnaryConstructorClass* c = a.Perform(std::make_tuple());
1323 class TenArgConstructorClass {
1325 TenArgConstructorClass(
int a1,
int a2,
int a3,
int a4,
int a5,
int a6,
int a7,
1326 int a8,
int a9,
int a10)
1327 :
value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {}
1332 TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
1333 Action<TenArgConstructorClass*()> a = ReturnNew<TenArgConstructorClass>(
1334 1000000000, 200000000, 30000000, 4000000, 500000, 60000, 7000, 800, 90,
1336 TenArgConstructorClass* c = a.Perform(std::make_tuple());
1341 std::unique_ptr<int> UniquePtrSource() {
1342 return std::unique_ptr<int>(
new int(19));
1345 std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1346 std::vector<std::unique_ptr<int>> out;
1347 out.emplace_back(
new int(7));
1351 TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1353 std::unique_ptr<int>
i(
new int(19));
1357 Derived* d =
new Derived;
1359 .WillOnce(
Return(
ByMove(std::unique_ptr<Derived>(d))));
1361 std::unique_ptr<int> result1 = mock.MakeUnique();
1364 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1369 std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1373 TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1374 testing::MockFunction<void()> mock_function;
1376 std::unique_ptr<int>
i(
new int(19));
1382 std::unique_ptr<int> result1 = mock.MakeUnique();
1386 TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
1390 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1391 return std::unique_ptr<int>(
new int(42));
1397 .WillRepeatedly(
Invoke(VectorUniquePtrSource));
1398 std::unique_ptr<int> result1 = mock.MakeUnique();
1400 std::unique_ptr<int> result2 = mock.MakeUnique();
1404 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1410 TEST(MockMethodTest, CanTakeMoveOnlyValue) {
1412 auto make = [](
int i) {
return std::unique_ptr<int>(
new int(i)); };
1414 EXPECT_CALL(mock, TakeUnique(
_)).WillRepeatedly([](std::unique_ptr<int> i) {
1421 EXPECT_CALL(mock, TakeUnique(testing::Pointee(7)))
1423 .RetiresOnSaturation();
1426 .RetiresOnSaturation();
1429 EXPECT_EQ(-7, mock.TakeUnique(make(7)));
1435 auto lvalue = make(6);
1437 .WillOnce([](
const std::unique_ptr<int>& i, std::unique_ptr<int> j) {
1440 EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7)));
1443 std::unique_ptr<int> saved;
1444 EXPECT_CALL(mock, TakeUnique(
_)).WillOnce([&saved](std::unique_ptr<int> i) {
1445 saved = std::move(i);
1448 EXPECT_EQ(0, mock.TakeUnique(make(42)));
1455 int Add(
int val,
int& ref,
int* ptr) {
1456 int result = val + ref + *ptr;
1462 int Deref(std::unique_ptr<int> ptr) {
return *ptr; }
1465 template <
typename T>
1466 T operator()(
T t) {
return 2 * t; }
1469 std::unique_ptr<int> UniqueInt(
int i) {
1470 return std::unique_ptr<int>(
new int(i));
1473 TEST(FunctorActionTest, ActionFromFunction) {
1474 Action<int(int, int&, int*)> a = &
Add;
1475 int x = 1,
y = 2,
z = 3;
1476 EXPECT_EQ(6, a.Perform(std::forward_as_tuple(x,
y, &
z)));
1480 Action<int(std::unique_ptr<int>)> a1 = &Deref;
1481 EXPECT_EQ(7, a1.Perform(std::make_tuple(UniqueInt(7))));
1484 TEST(FunctorActionTest, ActionFromLambda) {
1485 Action<int(bool, int)> a1 = [](
bool b,
int i) {
return b ? i : 0; };
1486 EXPECT_EQ(5, a1.Perform(std::make_tuple(
true, 5)));
1487 EXPECT_EQ(0, a1.Perform(std::make_tuple(
false, 5)));
1489 std::unique_ptr<int> saved;
1490 Action<void(std::unique_ptr<int>)> a2 = [&saved](std::unique_ptr<int>
p) {
1491 saved = std::move(p);
1493 a2.Perform(std::make_tuple(UniqueInt(5)));
1497 TEST(FunctorActionTest, PolymorphicFunctor) {
1498 Action<int(int)> ai =
Double();
1499 EXPECT_EQ(2, ai.Perform(std::make_tuple(1)));
1500 Action<double(double)> ad =
Double();
1501 EXPECT_EQ(3.0, ad.Perform(std::make_tuple(1.5)));
1504 TEST(FunctorActionTest, TypeConversion) {
1506 const Action<bool(int)> a1 = [](
int i) {
return i > 1; };
1507 const Action<int(bool)> a2 = Action<int(bool)>(a1);
1508 EXPECT_EQ(1, a1.Perform(std::make_tuple(42)));
1509 EXPECT_EQ(0, a2.Perform(std::make_tuple(42)));
1512 const Action<bool(std::string)> s1 = [](std::string s) {
return !s.empty(); };
1513 const Action<int(const char*)> s2 = Action<int(const char*)>(s1);
1514 EXPECT_EQ(0, s2.Perform(std::make_tuple(
"")));
1515 EXPECT_EQ(1, s2.Perform(std::make_tuple(
"hello")));
1518 const Action<bool(std::string)> x1 = [](
Unused) {
return 42; };
1519 const Action<bool(std::string)> x2 = [] {
return 42; };
1520 EXPECT_TRUE(x1.Perform(std::make_tuple(
"hello")));
1521 EXPECT_TRUE(x2.Perform(std::make_tuple(
"hello")));
1524 std::function<int()>
f = [] {
return 7; };
1525 Action<int(int)> d = f;
1527 EXPECT_EQ(7, d.Perform(std::make_tuple(1)));
1530 Action<void(int)>(
nullptr);
1533 TEST(FunctorActionTest, UnusedArguments) {
1535 Action<int(int, double y, double z)> a =
1537 std::tuple<int, double, double> dummy = std::make_tuple(3, 7.3, 9.44);
1542 TEST(MoveOnlyArgumentsTest, ReturningActions) {
1543 Action<int(std::unique_ptr<int>)> a =
Return(1);
1544 EXPECT_EQ(1, a.Perform(std::make_tuple(
nullptr)));
1547 EXPECT_EQ(7, a.Perform(std::make_tuple(
nullptr)));
1549 Action<void(std::unique_ptr<int>,
int*)> a2 = testing::SetArgPointee<1>(3);
1551 a2.Perform(std::make_tuple(
nullptr, &x));
1559 TEST(ActionMacro, LargeArity) {
1561 1,
testing::Action<
int(
int)>(ReturnArity()).Perform(std::make_tuple(0)));
1564 testing::Action<
int(
int,
int,
int,
int,
int,
int,
int,
int,
int,
int)>(
1566 .Perform(std::make_tuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
1569 testing::Action<
int(
int,
int,
int,
int,
int,
int,
int,
int,
int,
int,
int,
1570 int,
int,
int,
int,
int,
int,
int,
int,
int)>(
1572 .Perform(std::make_tuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1573 14, 15, 16, 17, 18, 19)));
1579 #if _MSC_VER == 1900
1580 # pragma warning(pop)
#define EXPECT_DOUBLE_EQ(val1, val2)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
PolymorphicAction< internal::ReturnNullAction > ReturnNull()
internal::IgnoredValue Unused
#define EXPECT_NONFATAL_FAILURE(statement, substr)
internal::ReturnRoundRobinAction< T > ReturnRoundRobin(std::vector< T > vals)
int SumOf4(int a, int b, int c, int d)
PolymorphicAction< internal::AssignAction< T1, T2 > > Assign(T1 *ptr, T2 val)
#define EXPECT_NE(val1, val2)
#define TEST_F(test_fixture, test_name)
#define MOCK_METHOD0(m,...)
internal::DoAllAction< typename std::decay< Action >::type...> DoAll(Action &&...action)
#define TEST(test_suite_name, test_name)
#define ON_CALL(obj, call)
#define EXPECT_ANY_THROW(statement)
internal::InvokeWithoutArgsAction< typename std::decay< FunctionImpl >::type > InvokeWithoutArgs(FunctionImpl function_impl)
internal::WithArgsAction< typename std::decay< InnerAction >::type, k, ks...> WithArgs(InnerAction &&action)
#define MOCK_METHOD2(m,...)
PolymorphicAction< Impl > MakePolymorphicAction(const Impl &impl)
void VoidBinary(int, char)
inline::std::reference_wrapper< T > ByRef(T &l_value)
PolymorphicAction< internal::SetErrnoAndReturnAction< T > > SetErrnoAndReturn(int errval, T result)
internal::ReturnRefAction< R > ReturnRef(R &x)
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
FloatingPoint< double > Double
internal::WithArgsAction< typename std::decay< InnerAction >::type > WithoutArgs(InnerAction &&action)
internal::ReturnRefOfCopyAction< R > ReturnRefOfCopy(const R &x)
void UniversalPrint(const T &value,::std::ostream *os)
ADVar foo(double d, ADVar x, ADVar y)
#define EXPECT_STREQ(s1, s2)
const char * Binary(const char *input, short n)
#define MOCK_METHOD1(m,...)
internal::SetArgumentPointeeAction< N, T > SetArgPointee(T value)
#define EXPECT_THAT(value, matcher)
const char * CharPtr(const char *s)
internal::DoDefaultAction DoDefault()
internal::ReturnAction< R > Return(R value)
int Ternary(int x, char y, short z)
#define EXPECT_CALL(obj, call)
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.
#define EXPECT_EQ(val1, val2)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
internal::SetArgumentPointeeAction< N, T > SetArgumentPointee(T value)
internal::ByMoveWrapper< R > ByMove(R x)
internal::ReturnNewAction< T, typename std::decay< Params >::type...> ReturnNew(Params &&...params)
#define EXPECT_TRUE(condition)
static void Print(const T &value,::std::ostream *os)
internal::IgnoreResultAction< A > IgnoreResult(const A &an_action)
#define EXPECT_FALSE(condition)
AssertionResult IsNull(const char *str)
Action< F > MakeAction(ActionInterface< F > *impl)
static ExpectedAnswer expected[4]