39 # pragma warning(push)
40 # pragma warning(disable:4800)
44 #include "gmock/gmock-actions.h"
49 #include "gmock/gmock.h"
50 #include "gmock/internal/gmock-port.h"
51 #include "gtest/gtest.h"
52 #include "gtest/gtest-spi.h"
84 #if !GTEST_OS_WINDOWS_MOBILE
89 TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
96 TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
98 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
104 TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
108 #if GMOCK_WCHAR_T_IS_NATIVE_
109 #if !defined(__WCHAR_UNSIGNED__)
132 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
133 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
134 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
136 #if GMOCK_WCHAR_T_IS_NATIVE_
137 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
139 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists());
140 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
143 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
145 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists());
146 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());
148 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
151 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
155 TEST(BuiltInDefaultValueTest, IsFalseForBool) {
160 TEST(BuiltInDefaultValueTest, BoolExists) {
166 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
172 TEST(BuiltInDefaultValueTest, ExistsForString) {
173 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
178 TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
186 class MyDefaultConstructible {
188 MyDefaultConstructible() :
value_(42) {}
197 class MyNonDefaultConstructible {
200 explicit MyNonDefaultConstructible(
int a_value) :
value_(a_value) {}
209 TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
210 EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
213 TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
218 TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
219 EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
223 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
232 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
239 TEST(DefaultValueTest, IsInitiallyUnset) {
241 EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
242 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
246 TEST(DefaultValueTest, CanBeSetAndUnset) {
248 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
250 DefaultValue<int>::Set(1);
251 DefaultValue<const MyNonDefaultConstructible>::Set(
252 MyNonDefaultConstructible(42));
258 EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
264 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
267 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
273 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
276 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
277 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
286 TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
289 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
290 return std::unique_ptr<int>(
new int(42));
293 std::unique_ptr<int>
i = DefaultValue<std::unique_ptr<int>>
::Get();
298 TEST(DefaultValueTest, GetWorksForVoid) {
305 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
307 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
308 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
312 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
314 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
315 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
319 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
321 DefaultValue<const int&>::Set(
n);
322 MyNonDefaultConstructible
x(42);
323 DefaultValue<MyNonDefaultConstructible&>::Set(
x);
326 EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
335 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
338 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
344 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
346 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
359 typedef int MyGlobalFunction(
bool,
int);
361 class MyActionImpl :
public ActionInterface<MyGlobalFunction> {
363 int Perform(
const std::tuple<bool, int>&
args)
override {
364 return std::get<0>(
args) ? std::get<1>(
args) : 0;
368 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
369 MyActionImpl my_action_impl;
370 (void)my_action_impl;
386 TEST(ActionTest, CanBeConstructedFromActionInterface) {
387 Action<MyGlobalFunction>
action(
new MyActionImpl);
391 TEST(ActionTest, DelegatesWorkToActionInterface) {
392 const Action<MyGlobalFunction>
action(
new MyActionImpl);
399 TEST(ActionTest, IsCopyable) {
400 Action<MyGlobalFunction>
a1(
new MyActionImpl);
401 Action<MyGlobalFunction>
a2(
a1);
425 class IsNotZero :
public ActionInterface<bool(int)> {
427 bool Perform(
const std::tuple<int>&
arg)
override {
428 return std::get<0>(
arg) != 0;
432 TEST(ActionTest, CanBeConvertedToOtherActionType) {
433 const Action<
bool(
int)>
a1(
new IsNotZero);
434 const Action<
int(
char)>
a2 = Action<int(char)>(
a1);
443 class ReturnSecondArgumentAction {
448 template <
typename Result,
typename ArgumentTuple>
450 return std::get<1>(
args);
456 class ReturnZeroFromNullaryFunctionAction {
465 template <
typename Result>
466 Result Perform(
const std::tuple<>&)
const {
474 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
478 PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
479 ReturnZeroFromNullaryFunction() {
485 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
486 Action<
int(
bool,
int,
double)>
a1 = ReturnSecondArgument();
492 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
493 Action<
int()>
a1 = ReturnZeroFromNullaryFunction();
496 Action<
void*()>
a2 = ReturnZeroFromNullaryFunction();
502 TEST(ReturnTest, WorksForVoid) {
508 TEST(ReturnTest, ReturnsGivenValue) {
517 TEST(ReturnTest, AcceptsStringLiteral) {
518 Action<
const char*()>
a1 =
Return(
"Hello");
527 struct IntegerVectorWrapper {
528 std::vector<int> *
v;
529 IntegerVectorWrapper(std::vector<int>& _v) :
v(&_v) {}
533 TEST(ReturnTest, SupportsWrapperReturnType) {
536 for (
int i = 0;
i < 5; ++
i)
v.push_back(
i);
540 Action<IntegerVectorWrapper()>
a =
Return(
v);
551 struct Derived :
public Base {
552 bool operator==(
const Derived&) {
return true; }
555 TEST(ReturnTest, IsCovariant) {
571 explicit FromType(
bool* is_converted) : converted_(is_converted) {}
572 bool* converted()
const {
return converted_; }
575 bool*
const converted_;
583 ToType(
const FromType&
x) { *
x.converted() =
true; }
586 TEST(ReturnTest, ConvertsArgumentWhenConverted) {
587 bool converted =
false;
588 FromType
x(&converted);
590 EXPECT_TRUE(converted) <<
"Return must convert its argument in its own "
591 <<
"conversion operator.";
593 action.Perform(std::tuple<>());
594 EXPECT_FALSE(converted) <<
"Action must NOT convert its argument "
595 <<
"when performed.";
598 class DestinationType {};
603 operator DestinationType() {
return DestinationType(); }
606 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
612 TEST(ReturnNullTest, WorksInPointerReturningFunction) {
622 TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
623 const Action<std::unique_ptr<const int>()>
a1 =
ReturnNull();
631 TEST(ReturnRefTest, WorksForReference) {
639 TEST(ReturnRefTest, IsCovariant) {
650 TEST(ReturnRefOfCopyTest, WorksForReference) {
663 TEST(ReturnRefOfCopyTest, IsCovariant) {
683 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
686 int(
const std::unique_ptr<int>&, std::unique_ptr<int>));
694 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
703 TEST(DoDefaultDeathTest, DiesForUnknowType) {
707 #if GTEST_HAS_EXCEPTIONS
719 void VoidFunc(
bool ) {}
721 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
738 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
739 DefaultValue<int>::Set(1);
748 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
751 .WillByDefault(
Return(2));
758 TEST(DoDefaultTest, CannotBeUsedInOnCall) {
763 },
"DoDefault() cannot be used in ON_CALL()");
768 TEST(SetArgPointeeTest, SetsTheNthPointee) {
770 Action<MyFunction>
a = SetArgPointee<1>(2);
778 a = SetArgPointee<2>(
'a');
787 TEST(SetArgPointeeTest, AcceptsStringLiteral) {
789 Action<MyFunction>
a = SetArgPointee<0>(
"hi");
791 const char*
ptr =
nullptr;
796 a = SetArgPointee<1>(
"world");
803 TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
805 Action<MyFunction>
a = SetArgPointee<0>(
L"world");
806 const wchar_t*
ptr =
nullptr;
810 # if GTEST_HAS_STD_WSTRING
813 Action<MyStringFunction>
a2 = SetArgPointee<0>(
L"world");
822 TEST(SetArgPointeeTest, AcceptsCharPointer) {
824 const char*
const hi =
"hi";
825 Action<MyFunction>
a = SetArgPointee<1>(hi);
827 const char*
ptr =
nullptr;
832 char world_array[] =
"world";
833 char*
const world = world_array;
834 a = SetArgPointee<2>(world);
841 TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
842 typedef void MyFunction(
bool,
const wchar_t**);
843 const wchar_t*
const hi =
L"hi";
844 Action<MyFunction>
a = SetArgPointee<1>(hi);
845 const wchar_t*
ptr =
nullptr;
849 # if GTEST_HAS_STD_WSTRING
852 wchar_t world_array[] =
L"world";
853 wchar_t*
const world = world_array;
854 Action<MyStringFunction>
a2 = SetArgPointee<1>(world);
863 TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
865 Action<MyFunction>
a = SetArgumentPointee<1>(2);
873 a = SetArgumentPointee<2>(
'a');
884 class NullaryFunctor {
886 int operator()() {
return 2; }
892 class VoidNullaryFunctor {
894 void operator()() {
g_done =
true; }
897 short Short(
short n) {
return n; }
900 const char*
CharPtr(
const char* s) {
return s; }
902 bool Unary(
int x) {
return x < 0; }
940 TEST(InvokeWithoutArgsTest, Functor) {
946 Action<
int(
int,
double,
char)>
a2 =
960 Action<
int(
bool,
char)>
a =
966 TEST(IgnoreResultTest, PolymorphicAction) {
978 TEST(IgnoreResultTest, MonomorphicAction) {
987 MyNonDefaultConstructible ReturnMyNonDefaultConstructible(
double ) {
989 return MyNonDefaultConstructible(42);
992 TEST(IgnoreResultTest, ActionReturningClass) {
994 Action<void(
int)>
a =
1002 Action<void(
int)>
a =
Assign(&
x, 5);
1007 TEST(AssignTest, String) {
1009 Action<void(
void)>
a =
Assign(&
x,
"Hello, world");
1014 TEST(AssignTest, CompatibleTypes) {
1016 Action<void(
int)>
a =
Assign(&
x, 5);
1023 TEST(WithArgsTest, OneArg) {
1030 TEST(WithArgsTest, TwoArgs) {
1031 Action<
const char*(
const char*
s,
double x,
short n)>
a =
1033 const char s[] =
"Hello";
1039 template <
typename...
I>
1041 return a + ConcatAll()(
i...);
1046 TEST(WithArgsTest, TenArgs) {
1047 Action<
std::string(
const char*,
const char*,
const char*,
const char*)>
a =
1048 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(
Invoke(ConcatAll{}));
1055 class SubtractAction :
public ActionInterface<int(int, int)> {
1057 int Perform(
const std::tuple<int, int>&
args)
override {
1058 return std::get<0>(
args) - std::get<1>(
args);
1062 TEST(WithArgsTest, NonInvokeAction) {
1064 WithArgs<2, 1>(
MakeAction(
new SubtractAction));
1065 std::tuple<std::string, int, int>
dummy =
1072 Action<
int(
int x,
char y,
short z)>
a =
1078 TEST(WithArgsTest, RepeatedArguments) {
1079 Action<
int(
bool,
int m,
int n)>
a =
1085 TEST(WithArgsTest, ReversedArgumentOrder) {
1086 Action<
const char*(
short n,
const char*
input)>
a =
1088 const char s[] =
"Hello";
1093 TEST(WithArgsTest, ArgsOfCompatibleTypes) {
1094 Action<
long(
short x,
char y,
double z,
char c)>
a =
1101 TEST(WithArgsTest, VoidAction) {
1108 TEST(WithArgsTest, ReturnReference) {
1109 Action<
int&(
int&,
void*)> aa = WithArgs<0>([](
int&
a) ->
int& {
return a; });
1111 const int& res = aa.Perform(std::forward_as_tuple(
i,
nullptr));
1115 TEST(WithArgsTest, InnerActionWithConversion) {
1116 Action<Derived*()> inner = [] {
return nullptr; };
1121 #if !GTEST_OS_WINDOWS_MOBILE
1125 void SetUp()
override { errno = 0; }
1126 void TearDown()
override { errno = 0; }
1135 TEST_F(SetErrnoAndReturnTest, Ptr) {
1142 TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1148 #endif // !GTEST_OS_WINDOWS_MOBILE
1153 TEST(ByRefTest, IsCopyable) {
1157 auto ref_wrapper =
ByRef(s1);
1162 ref_wrapper =
ByRef(s2);
1166 auto ref_wrapper1 =
ByRef(s1);
1168 ref_wrapper = ref_wrapper1;
1174 TEST(ByRefTest, ConstValue) {
1178 const int& const_ref =
ByRef(
n);
1183 TEST(ByRefTest, NonConstValue) {
1191 const int& const_ref =
ByRef(
n);
1196 TEST(ByRefTest, ExplicitType) {
1198 const int& r1 = ByRef<const int>(
n);
1205 Derived& r2 = ByRef<Derived>(
d);
1208 const Derived& r3 = ByRef<const Derived>(
d);
1211 Base& r4 = ByRef<Base>(
d);
1214 const Base& r5 = ByRef<const Base>(
d);
1225 TEST(ByRefTest, PrintsCorrectly) {
1227 ::std::stringstream expected, actual;
1230 EXPECT_EQ(expected.str(), actual.str());
1234 std::unique_ptr<int> UniquePtrSource() {
1235 return std::unique_ptr<int>(
new int(19));
1238 std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1239 std::vector<std::unique_ptr<int>>
out;
1240 out.emplace_back(
new int(7));
1244 TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1246 std::unique_ptr<int>
i(
new int(19));
1250 Derived*
d =
new Derived;
1254 std::unique_ptr<int> result1 = mock.MakeUnique();
1257 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1262 std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1266 TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1269 std::unique_ptr<int>
i(
new int(19));
1275 std::unique_ptr<int> result1 = mock.MakeUnique();
1279 TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
1283 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1284 return std::unique_ptr<int>(
new int(42));
1290 .WillRepeatedly(
Invoke(VectorUniquePtrSource));
1291 std::unique_ptr<int> result1 = mock.MakeUnique();
1293 std::unique_ptr<int> result2 = mock.MakeUnique();
1297 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1303 TEST(MockMethodTest, CanTakeMoveOnlyValue) {
1305 auto make = [](
int i) {
return std::unique_ptr<int>(
new int(
i)); };
1307 EXPECT_CALL(mock, TakeUnique(
_)).WillRepeatedly([](std::unique_ptr<int>
i) {
1316 .RetiresOnSaturation();
1319 .RetiresOnSaturation();
1322 EXPECT_EQ(-7, mock.TakeUnique(make(7)));
1328 auto lvalue = make(6);
1330 .WillOnce([](
const std::unique_ptr<int>&
i, std::unique_ptr<int> j) {
1333 EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7)));
1336 std::unique_ptr<int> saved;
1337 EXPECT_CALL(mock, TakeUnique(
_)).WillOnce([&saved](std::unique_ptr<int>
i) {
1341 EXPECT_EQ(0, mock.TakeUnique(make(42)));
1355 int Deref(std::unique_ptr<int>
ptr) {
return *
ptr; }
1358 template <
typename T>
1359 T operator()(
T t) {
return 2 *
t; }
1362 std::unique_ptr<int> UniqueInt(
int i) {
1363 return std::unique_ptr<int>(
new int(
i));
1366 TEST(FunctorActionTest, ActionFromFunction) {
1367 Action<
int(
int,
int&,
int*)>
a = &
Add;
1368 int x = 1,
y = 2,
z = 3;
1373 Action<
int(std::unique_ptr<int>)>
a1 = &Deref;
1377 TEST(FunctorActionTest, ActionFromLambda) {
1378 Action<
int(
bool,
int)>
a1 = [](
bool b,
int i) {
return b ?
i : 0; };
1382 std::unique_ptr<int> saved;
1383 Action<void(std::unique_ptr<int>)>
a2 = [&saved](std::unique_ptr<int>
p) {
1390 TEST(FunctorActionTest, PolymorphicFunctor) {
1393 Action<double(
double)> ad =
Double();
1397 TEST(FunctorActionTest, TypeConversion) {
1399 const Action<
bool(
int)>
a1 = [](
int i) {
return i > 1; };
1400 const Action<
int(
bool)>
a2 = Action<int(bool)>(
a1);
1406 const Action<
int(
const char*)> s2 = Action<int(const char*)>(s1);
1415 TEST(FunctorActionTest, UnusedArguments) {
1417 Action<
int(
int,
double y,
double z)>
a =
1424 TEST(MoveOnlyArgumentsTest, ReturningActions) {
1425 Action<
int(std::unique_ptr<int>)>
a =
Return(1);
1431 Action<void(std::unique_ptr<int>,
int*)>
a2 = testing::SetArgPointee<1>(3);
1441 #if _MSC_VER == 1900
1442 # pragma warning(pop)