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_HAS_SIGNED_WCHAR_T_
112 #if GMOCK_WCHAR_T_IS_NATIVE_
113 #if !defined(__WCHAR_UNSIGNED__)
136 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
137 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
138 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
140 #if GMOCK_HAS_SIGNED_WCHAR_T_
141 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
144 #if GMOCK_WCHAR_T_IS_NATIVE_
145 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
147 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists());
148 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());
150 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
151 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
153 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists());
154 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());
156 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
159 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
163 TEST(BuiltInDefaultValueTest, IsFalseForBool) {
168 TEST(BuiltInDefaultValueTest, BoolExists) {
174 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
175 #if GTEST_HAS_GLOBAL_STRING
177 #endif // GTEST_HAS_GLOBAL_STRING
184 TEST(BuiltInDefaultValueTest, ExistsForString) {
185 #if GTEST_HAS_GLOBAL_STRING
186 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
187 #endif // GTEST_HAS_GLOBAL_STRING
189 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
194 TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
202 class MyDefaultConstructible {
204 MyDefaultConstructible() :
value_(42) {}
213 class MyNonDefaultConstructible {
216 explicit MyNonDefaultConstructible(
int a_value) :
value_(a_value) {}
225 TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
226 EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
229 TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
234 TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
235 EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
239 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
248 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
255 TEST(DefaultValueTest, IsInitiallyUnset) {
257 EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
258 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
262 TEST(DefaultValueTest, CanBeSetAndUnset) {
264 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
266 DefaultValue<int>::Set(1);
267 DefaultValue<const MyNonDefaultConstructible>::Set(
268 MyNonDefaultConstructible(42));
274 EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
280 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
283 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
289 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
292 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
293 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
302 TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
305 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
306 return std::unique_ptr<int>(
new int(42));
309 std::unique_ptr<int>
i = DefaultValue<std::unique_ptr<int>>
::Get();
314 TEST(DefaultValueTest, GetWorksForVoid) {
321 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
323 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
324 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
328 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
330 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
331 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
335 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
337 DefaultValue<const int&>::Set(
n);
338 MyNonDefaultConstructible
x(42);
339 DefaultValue<MyNonDefaultConstructible&>::Set(
x);
342 EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
351 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
354 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
360 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
362 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
375 typedef int MyGlobalFunction(
bool,
int);
377 class MyActionImpl :
public ActionInterface<MyGlobalFunction> {
379 int Perform(
const std::tuple<bool, int>&
args)
override {
380 return std::get<0>(
args) ? std::get<1>(
args) : 0;
384 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
385 MyActionImpl my_action_impl;
386 (void)my_action_impl;
402 TEST(ActionTest, CanBeConstructedFromActionInterface) {
403 Action<MyGlobalFunction>
action(
new MyActionImpl);
407 TEST(ActionTest, DelegatesWorkToActionInterface) {
408 const Action<MyGlobalFunction>
action(
new MyActionImpl);
415 TEST(ActionTest, IsCopyable) {
416 Action<MyGlobalFunction>
a1(
new MyActionImpl);
417 Action<MyGlobalFunction>
a2(
a1);
441 class IsNotZero :
public ActionInterface<bool(int)> {
443 bool Perform(
const std::tuple<int>&
arg)
override {
444 return std::get<0>(
arg) != 0;
448 TEST(ActionTest, CanBeConvertedToOtherActionType) {
449 const Action<
bool(
int)>
a1(
new IsNotZero);
450 const Action<
int(
char)>
a2 = Action<int(char)>(
a1);
459 class ReturnSecondArgumentAction {
464 template <
typename Result,
typename ArgumentTuple>
466 return std::get<1>(
args);
472 class ReturnZeroFromNullaryFunctionAction {
481 template <
typename Result>
482 Result Perform(
const std::tuple<>&)
const {
490 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
494 PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
495 ReturnZeroFromNullaryFunction() {
501 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
502 Action<
int(
bool,
int,
double)>
a1 = ReturnSecondArgument();
508 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
509 Action<
int()>
a1 = ReturnZeroFromNullaryFunction();
512 Action<
void*()>
a2 = ReturnZeroFromNullaryFunction();
518 TEST(ReturnTest, WorksForVoid) {
524 TEST(ReturnTest, ReturnsGivenValue) {
533 TEST(ReturnTest, AcceptsStringLiteral) {
534 Action<
const char*()>
a1 =
Return(
"Hello");
543 struct IntegerVectorWrapper {
544 std::vector<int> *
v;
545 IntegerVectorWrapper(std::vector<int>& _v) :
v(&_v) {}
549 TEST(ReturnTest, SupportsWrapperReturnType) {
552 for (
int i = 0;
i < 5; ++
i)
v.push_back(
i);
556 Action<IntegerVectorWrapper()>
a =
Return(
v);
567 struct Derived :
public Base {
568 bool operator==(
const Derived&) {
return true; }
571 TEST(ReturnTest, IsCovariant) {
587 explicit FromType(
bool* is_converted) : converted_(is_converted) {}
588 bool* converted()
const {
return converted_; }
591 bool*
const converted_;
599 ToType(
const FromType&
x) { *
x.converted() =
true; }
602 TEST(ReturnTest, ConvertsArgumentWhenConverted) {
603 bool converted =
false;
604 FromType
x(&converted);
606 EXPECT_TRUE(converted) <<
"Return must convert its argument in its own "
607 <<
"conversion operator.";
609 action.Perform(std::tuple<>());
610 EXPECT_FALSE(converted) <<
"Action must NOT convert its argument "
611 <<
"when performed.";
614 class DestinationType {};
619 operator DestinationType() {
return DestinationType(); }
622 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
628 TEST(ReturnNullTest, WorksInPointerReturningFunction) {
638 TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
639 const Action<std::unique_ptr<const int>()>
a1 =
ReturnNull();
647 TEST(ReturnRefTest, WorksForReference) {
655 TEST(ReturnRefTest, IsCovariant) {
666 TEST(ReturnRefOfCopyTest, WorksForReference) {
679 TEST(ReturnRefOfCopyTest, IsCovariant) {
699 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
702 int(
const std::unique_ptr<int>&, std::unique_ptr<int>));
710 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
719 TEST(DoDefaultDeathTest, DiesForUnknowType) {
723 #if GTEST_HAS_EXCEPTIONS
735 void VoidFunc(
bool ) {}
737 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
754 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
755 DefaultValue<int>::Set(1);
764 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
767 .WillByDefault(
Return(2));
774 TEST(DoDefaultTest, CannotBeUsedInOnCall) {
779 },
"DoDefault() cannot be used in ON_CALL()");
784 TEST(SetArgPointeeTest, SetsTheNthPointee) {
786 Action<MyFunction>
a = SetArgPointee<1>(2);
794 a = SetArgPointee<2>(
'a');
803 TEST(SetArgPointeeTest, AcceptsStringLiteral) {
805 Action<MyFunction>
a = SetArgPointee<0>(
"hi");
807 const char*
ptr =
nullptr;
812 a = SetArgPointee<1>(
"world");
819 TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
821 Action<MyFunction>
a = SetArgPointee<0>(
L"world");
822 const wchar_t*
ptr =
nullptr;
826 # if GTEST_HAS_STD_WSTRING
829 Action<MyStringFunction>
a2 = SetArgPointee<0>(
L"world");
838 TEST(SetArgPointeeTest, AcceptsCharPointer) {
840 const char*
const hi =
"hi";
841 Action<MyFunction>
a = SetArgPointee<1>(hi);
843 const char*
ptr =
nullptr;
848 char world_array[] =
"world";
849 char*
const world = world_array;
850 a = SetArgPointee<2>(world);
857 TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
858 typedef void MyFunction(
bool,
const wchar_t**);
859 const wchar_t*
const hi =
L"hi";
860 Action<MyFunction>
a = SetArgPointee<1>(hi);
861 const wchar_t*
ptr =
nullptr;
865 # if GTEST_HAS_STD_WSTRING
868 wchar_t world_array[] =
L"world";
869 wchar_t*
const world = world_array;
870 Action<MyStringFunction>
a2 = SetArgPointee<1>(world);
879 TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
881 Action<MyFunction>
a = SetArgumentPointee<1>(2);
889 a = SetArgumentPointee<2>(
'a');
900 class NullaryFunctor {
902 int operator()() {
return 2; }
908 class VoidNullaryFunctor {
910 void operator()() {
g_done =
true; }
913 short Short(
short n) {
return n; }
916 const char*
CharPtr(
const char* s) {
return s; }
918 bool Unary(
int x) {
return x < 0; }
956 TEST(InvokeWithoutArgsTest, Functor) {
962 Action<
int(
int,
double,
char)>
a2 =
976 Action<
int(
bool,
char)>
a =
982 TEST(IgnoreResultTest, PolymorphicAction) {
994 TEST(IgnoreResultTest, MonomorphicAction) {
1003 MyNonDefaultConstructible ReturnMyNonDefaultConstructible(
double ) {
1005 return MyNonDefaultConstructible(42);
1008 TEST(IgnoreResultTest, ActionReturningClass) {
1010 Action<void(
int)>
a =
1018 Action<void(
int)>
a =
Assign(&
x, 5);
1023 TEST(AssignTest, String) {
1025 Action<void(
void)>
a =
Assign(&
x,
"Hello, world");
1030 TEST(AssignTest, CompatibleTypes) {
1032 Action<void(
int)>
a =
Assign(&
x, 5);
1039 TEST(WithArgsTest, OneArg) {
1046 TEST(WithArgsTest, TwoArgs) {
1047 Action<
const char*(
const char*
s,
double x,
short n)>
a =
1049 const char s[] =
"Hello";
1055 template <
typename...
I>
1057 return a + ConcatAll()(
i...);
1062 TEST(WithArgsTest, TenArgs) {
1063 Action<
std::string(
const char*,
const char*,
const char*,
const char*)>
a =
1064 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(
Invoke(ConcatAll{}));
1071 class SubtractAction :
public ActionInterface<int(int, int)> {
1073 int Perform(
const std::tuple<int, int>&
args)
override {
1074 return std::get<0>(
args) - std::get<1>(
args);
1078 TEST(WithArgsTest, NonInvokeAction) {
1080 WithArgs<2, 1>(
MakeAction(
new SubtractAction));
1081 std::tuple<std::string, int, int>
dummy =
1088 Action<
int(
int x,
char y,
short z)>
a =
1094 TEST(WithArgsTest, RepeatedArguments) {
1095 Action<
int(
bool,
int m,
int n)>
a =
1101 TEST(WithArgsTest, ReversedArgumentOrder) {
1102 Action<
const char*(
short n,
const char*
input)>
a =
1104 const char s[] =
"Hello";
1109 TEST(WithArgsTest, ArgsOfCompatibleTypes) {
1110 Action<
long(
short x,
char y,
double z,
char c)>
a =
1117 TEST(WithArgsTest, VoidAction) {
1124 TEST(WithArgsTest, ReturnReference) {
1125 Action<
int&(
int&,
void*)> aa = WithArgs<0>([](
int&
a) ->
int& {
return a; });
1127 const int& res = aa.Perform(std::forward_as_tuple(
i,
nullptr));
1131 TEST(WithArgsTest, InnerActionWithConversion) {
1132 Action<Derived*()> inner = [] {
return nullptr; };
1137 #if !GTEST_OS_WINDOWS_MOBILE
1141 void SetUp()
override { errno = 0; }
1142 void TearDown()
override { errno = 0; }
1151 TEST_F(SetErrnoAndReturnTest, Ptr) {
1158 TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1164 #endif // !GTEST_OS_WINDOWS_MOBILE
1169 TEST(ByRefTest, IsCopyable) {
1173 auto ref_wrapper =
ByRef(s1);
1178 ref_wrapper =
ByRef(s2);
1182 auto ref_wrapper1 =
ByRef(s1);
1184 ref_wrapper = ref_wrapper1;
1190 TEST(ByRefTest, ConstValue) {
1194 const int& const_ref =
ByRef(
n);
1199 TEST(ByRefTest, NonConstValue) {
1207 const int& const_ref =
ByRef(
n);
1212 TEST(ByRefTest, ExplicitType) {
1214 const int& r1 = ByRef<const int>(
n);
1221 Derived& r2 = ByRef<Derived>(
d);
1224 const Derived& r3 = ByRef<const Derived>(
d);
1227 Base& r4 = ByRef<Base>(
d);
1230 const Base& r5 = ByRef<const Base>(
d);
1241 TEST(ByRefTest, PrintsCorrectly) {
1243 ::std::stringstream expected, actual;
1246 EXPECT_EQ(expected.str(), actual.str());
1250 std::unique_ptr<int> UniquePtrSource() {
1251 return std::unique_ptr<int>(
new int(19));
1254 std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1255 std::vector<std::unique_ptr<int>>
out;
1256 out.emplace_back(
new int(7));
1260 TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1262 std::unique_ptr<int>
i(
new int(19));
1266 Derived*
d =
new Derived;
1270 std::unique_ptr<int> result1 = mock.MakeUnique();
1273 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1278 std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1282 TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1285 std::unique_ptr<int>
i(
new int(19));
1291 std::unique_ptr<int> result1 = mock.MakeUnique();
1295 TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
1299 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1300 return std::unique_ptr<int>(
new int(42));
1306 .WillRepeatedly(
Invoke(VectorUniquePtrSource));
1307 std::unique_ptr<int> result1 = mock.MakeUnique();
1309 std::unique_ptr<int> result2 = mock.MakeUnique();
1313 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1319 TEST(MockMethodTest, CanTakeMoveOnlyValue) {
1321 auto make = [](
int i) {
return std::unique_ptr<int>(
new int(
i)); };
1323 EXPECT_CALL(mock, TakeUnique(
_)).WillRepeatedly([](std::unique_ptr<int>
i) {
1332 .RetiresOnSaturation();
1335 .RetiresOnSaturation();
1338 EXPECT_EQ(-7, mock.TakeUnique(make(7)));
1344 auto lvalue = make(6);
1346 .WillOnce([](
const std::unique_ptr<int>&
i, std::unique_ptr<int> j) {
1349 EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7)));
1352 std::unique_ptr<int> saved;
1353 EXPECT_CALL(mock, TakeUnique(
_)).WillOnce([&saved](std::unique_ptr<int>
i) {
1357 EXPECT_EQ(0, mock.TakeUnique(make(42)));
1371 int Deref(std::unique_ptr<int>
ptr) {
return *
ptr; }
1374 template <
typename T>
1375 T operator()(
T t) {
return 2 *
t; }
1378 std::unique_ptr<int> UniqueInt(
int i) {
1379 return std::unique_ptr<int>(
new int(
i));
1382 TEST(FunctorActionTest, ActionFromFunction) {
1383 Action<
int(
int,
int&,
int*)>
a = &
Add;
1384 int x = 1,
y = 2,
z = 3;
1389 Action<
int(std::unique_ptr<int>)>
a1 = &Deref;
1393 TEST(FunctorActionTest, ActionFromLambda) {
1394 Action<
int(
bool,
int)>
a1 = [](
bool b,
int i) {
return b ?
i : 0; };
1398 std::unique_ptr<int> saved;
1399 Action<void(std::unique_ptr<int>)>
a2 = [&saved](std::unique_ptr<int>
p) {
1406 TEST(FunctorActionTest, PolymorphicFunctor) {
1409 Action<double(
double)> ad =
Double();
1413 TEST(FunctorActionTest, TypeConversion) {
1415 const Action<
bool(
int)>
a1 = [](
int i) {
return i > 1; };
1416 const Action<
int(
bool)>
a2 = Action<int(bool)>(
a1);
1422 const Action<
int(
const char*)> s2 = Action<int(const char*)>(s1);
1431 TEST(FunctorActionTest, UnusedArguments) {
1433 Action<
int(
int,
double y,
double z)>
a =
1440 TEST(MoveOnlyArgumentsTest, ReturningActions) {
1441 Action<
int(std::unique_ptr<int>)>
a =
Return(1);
1447 Action<void(std::unique_ptr<int>,
int*)>
a2 = testing::SetArgPointee<1>(3);
1457 #if _MSC_VER == 1900
1458 # pragma warning(pop)