40 #include "gmock/gmock.h" 42 #include "gtest/gtest.h" 43 #include "gtest/gtest-spi.h" 49 using ::std::tr1::tuple;
50 using ::std::tr1::tuple_element;
75 #if !GTEST_OS_WINDOWS_MOBILE 79 #if GTEST_HAS_PROTOBUF_ 80 using testing::internal::TestMessage;
81 #endif // GTEST_HAS_PROTOBUF_ 84 TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
85 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
86 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
87 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
91 TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
93 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
99 TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
100 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
101 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
102 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
103 #if GMOCK_HAS_SIGNED_WCHAR_T_ 104 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
105 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
107 #if GMOCK_WCHAR_T_IS_NATIVE_ 108 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
110 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get());
111 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get());
112 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get());
113 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
114 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
115 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
116 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get());
117 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get());
118 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get());
119 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
120 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
121 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
122 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
127 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
128 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
129 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
131 #if GMOCK_HAS_SIGNED_WCHAR_T_ 132 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
133 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
135 #if GMOCK_WCHAR_T_IS_NATIVE_ 136 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
138 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists());
139 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());
141 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
144 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists());
145 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());
147 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
150 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
154 TEST(BuiltInDefaultValueTest, IsFalseForBool) {
159 TEST(BuiltInDefaultValueTest, BoolExists) {
165 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
166 #if GTEST_HAS_GLOBAL_STRING 167 EXPECT_EQ(
"", BuiltInDefaultValue< ::string>::Get());
168 #endif // GTEST_HAS_GLOBAL_STRING 170 EXPECT_EQ(
"", BuiltInDefaultValue< ::std::string>::Get());
175 TEST(BuiltInDefaultValueTest, ExistsForString) {
176 #if GTEST_HAS_GLOBAL_STRING 177 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
178 #endif // GTEST_HAS_GLOBAL_STRING 180 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
185 TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
186 EXPECT_EQ(
"", BuiltInDefaultValue<const std::string>::Get());
187 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
188 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
195 UserType() : value(0) {}
200 TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
205 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
207 BuiltInDefaultValue<int&>::Get();
210 BuiltInDefaultValue<const char&>::Get();
214 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
216 BuiltInDefaultValue<UserType>::Get();
221 TEST(DefaultValueTest, IsInitiallyUnset) {
227 TEST(DefaultValueTest, CanBeSetAndUnset) {
231 DefaultValue<int>::Set(1);
232 DefaultValue<const UserType>::Set(UserType());
235 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
238 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
240 DefaultValue<int>::Clear();
241 DefaultValue<const UserType>::Clear();
253 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
262 DefaultValue<UserType>::Get();
267 TEST(DefaultValueTest, GetWorksForVoid) {
268 return DefaultValue<void>::Get();
274 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
280 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
286 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
288 DefaultValue<const int&>::Set(n);
290 DefaultValue<UserType&>::Set(u);
295 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
296 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
298 DefaultValue<const int&>::Clear();
299 DefaultValue<UserType&>::Clear();
311 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
316 DefaultValue<int&>::Get();
319 DefaultValue<UserType>::Get();
326 typedef int MyGlobalFunction(
bool,
int);
328 class MyActionImpl :
public ActionInterface<MyGlobalFunction> {
330 virtual int Perform(
const tuple<bool, int>& args) {
331 return get<0>(args) ? get<1>(args) : 0;
335 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
336 MyActionImpl my_action_impl;
337 (void)my_action_impl;
341 Action<MyGlobalFunction> action =
MakeAction(
new MyActionImpl);
353 TEST(ActionTest, CanBeConstructedFromActionInterface) {
354 Action<MyGlobalFunction> action(
new MyActionImpl);
358 TEST(ActionTest, DelegatesWorkToActionInterface) {
359 const Action<MyGlobalFunction> action(
new MyActionImpl);
366 TEST(ActionTest, IsCopyable) {
367 Action<MyGlobalFunction> a1(
new MyActionImpl);
368 Action<MyGlobalFunction> a2(a1);
392 class IsNotZero :
public ActionInterface<bool(int)> {
394 virtual bool Perform(
const tuple<int>&
arg) {
395 return get<0>(
arg) != 0;
399 #if !GTEST_OS_SYMBIAN 405 TEST(ActionTest, CanBeConvertedToOtherActionType) {
406 const Action<bool(int)> a1(
new IsNotZero);
407 const Action<int(char)> a2 = Action<int(char)>(a1);
411 #endif // !GTEST_OS_SYMBIAN 417 class ReturnSecondArgumentAction {
422 template <
typename Result,
typename ArgumentTuple>
423 Result Perform(
const ArgumentTuple& args) {
return get<1>(args); }
428 class ReturnZeroFromNullaryFunctionAction {
437 template <
typename Result>
438 Result Perform(
const tuple<>&)
const {
return 0; }
444 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
448 PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
449 ReturnZeroFromNullaryFunction() {
455 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
456 Action<int(bool, int, double)> a1 = ReturnSecondArgument();
462 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
463 Action<int()> a1 = ReturnZeroFromNullaryFunction();
466 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
472 TEST(ReturnTest, WorksForVoid) {
473 const Action<void(int)> ret =
Return();
478 TEST(ReturnTest, ReturnsGivenValue) {
479 Action<int()> ret =
Return(1);
487 TEST(ReturnTest, AcceptsStringLiteral) {
488 Action<const char*()> a1 =
Return(
"Hello");
491 Action<std::string()> a2 =
Return(
"world");
501 struct Derived :
public Base {
502 bool operator==(
const Derived&) {
return true; }
505 TEST(ReturnTest, IsCovariant) {
508 Action<Base*()> ret =
Return(&base);
521 explicit FromType(
bool* is_converted) : converted_(is_converted) {}
522 bool* converted()
const {
return converted_; }
525 bool*
const converted_;
533 ToType(
const FromType& x) { *x.converted() =
true; }
536 TEST(ReturnTest, ConvertsArgumentWhenConverted) {
537 bool converted =
false;
538 FromType x(&converted);
539 Action<ToType()> action(
Return(x));
540 EXPECT_TRUE(converted) <<
"Return must convert its argument in its own " 541 <<
"conversion operator.";
543 action.Perform(tuple<>());
544 EXPECT_FALSE(converted) <<
"Action must NOT convert its argument " 545 <<
"when performed.";
548 class DestinationType {};
553 operator DestinationType() {
return DestinationType(); }
556 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
558 Action<DestinationType()> action(
Return(s));
562 TEST(ReturnNullTest, WorksInPointerReturningFunction) {
566 const Action<const char*(bool)> a2 =
ReturnNull();
571 TEST(ReturnRefTest, WorksForReference) {
573 const Action<const int&(bool)> ret =
ReturnRef(n);
579 TEST(ReturnRefTest, IsCovariant) {
590 TEST(ReturnRefOfCopyTest, WorksForReference) {
603 TEST(ReturnRefOfCopyTest, IsCovariant) {
630 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
639 TEST(DoDefaultDeathTest, DiesForUnknowType) {
643 #if GTEST_HAS_EXCEPTIONS 657 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
674 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
675 DefaultValue<int>::Set(1);
680 DefaultValue<int>::Clear();
684 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
687 .WillByDefault(
Return(2));
694 TEST(DoDefaultTest, CannotBeUsedInOnCall) {
699 },
"DoDefault() cannot be used in ON_CALL()");
704 TEST(SetArgPointeeTest, SetsTheNthPointee) {
706 Action<MyFunction> a = SetArgPointee<1>(2);
714 a = SetArgPointee<2>(
'a');
722 #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN) 725 TEST(SetArgPointeeTest, AcceptsStringLiteral) {
727 Action<MyFunction> a = SetArgPointee<0>(
"hi");
729 const char* ptr = NULL;
734 a = SetArgPointee<1>(
"world");
741 TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
743 Action<MyFunction> a = SetArgPointee<0>(L
"world");
744 const wchar_t* ptr = NULL;
748 # if GTEST_HAS_STD_WSTRING 751 Action<MyStringFunction> a2 = SetArgPointee<0>(L
"world");
761 TEST(SetArgPointeeTest, AcceptsCharPointer) {
763 const char*
const hi =
"hi";
764 Action<MyFunction> a = SetArgPointee<1>(hi);
766 const char* ptr = NULL;
771 char world_array[] =
"world";
772 char*
const world = world_array;
773 a = SetArgPointee<2>(world);
780 TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
781 typedef void MyFunction(
bool,
const wchar_t**);
782 const wchar_t*
const hi = L
"hi";
783 Action<MyFunction> a = SetArgPointee<1>(hi);
784 const wchar_t* ptr = NULL;
788 # if GTEST_HAS_STD_WSTRING 791 wchar_t world_array[] = L
"world";
792 wchar_t*
const world = world_array;
793 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
800 #if GTEST_HAS_PROTOBUF_ 804 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
805 TestMessage*
const msg =
new TestMessage;
806 msg->set_member(
"yes");
807 TestMessage orig_msg;
808 orig_msg.CopyFrom(*msg);
810 Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
826 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
827 TestMessage*
const msg =
new TestMessage;
828 msg->set_member(
"yes");
829 TestMessage orig_msg;
830 orig_msg.CopyFrom(*msg);
832 Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg);
840 ::ProtocolMessage*
const dest_base = &dest;
849 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
850 using testing::internal::FooMessage;
851 FooMessage*
const msg =
new FooMessage;
852 msg->set_int_field(2);
853 msg->set_string_field(
"hi");
855 orig_msg.CopyFrom(*msg);
857 Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
865 dest.set_int_field(0);
874 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
875 using testing::internal::FooMessage;
876 FooMessage*
const msg =
new FooMessage;
877 msg->set_int_field(2);
878 msg->set_string_field(
"hi");
880 orig_msg.CopyFrom(*msg);
882 Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
890 dest.set_int_field(0);
897 #endif // GTEST_HAS_PROTOBUF_ 901 TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
903 Action<MyFunction> a = SetArgumentPointee<1>(2);
911 a = SetArgumentPointee<2>(
'a');
919 #if GTEST_HAS_PROTOBUF_ 923 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
924 TestMessage*
const msg =
new TestMessage;
925 msg->set_member(
"yes");
926 TestMessage orig_msg;
927 orig_msg.CopyFrom(*msg);
929 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
945 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
946 TestMessage*
const msg =
new TestMessage;
947 msg->set_member(
"yes");
948 TestMessage orig_msg;
949 orig_msg.CopyFrom(*msg);
951 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
959 ::ProtocolMessage*
const dest_base = &dest;
968 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
969 using testing::internal::FooMessage;
970 FooMessage*
const msg =
new FooMessage;
971 msg->set_int_field(2);
972 msg->set_string_field(
"hi");
974 orig_msg.CopyFrom(*msg);
976 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
984 dest.set_int_field(0);
993 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
994 using testing::internal::FooMessage;
995 FooMessage*
const msg =
new FooMessage;
996 msg->set_int_field(2);
997 msg->set_string_field(
"hi");
999 orig_msg.CopyFrom(*msg);
1001 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
1009 dest.set_int_field(0);
1016 #endif // GTEST_HAS_PROTOBUF_ 1021 class NullaryFunctor {
1023 int operator()() {
return 2; }
1029 class VoidNullaryFunctor {
1031 void operator()() { g_done =
true; }
1036 Foo() : value_(123) {}
1038 int Nullary()
const {
return value_; }
1045 TEST(InvokeWithoutArgsTest, Function) {
1062 TEST(InvokeWithoutArgsTest, Functor) {
1068 Action<int(int, double, char)> a2 =
1082 Action<int(bool, char)> a =
1088 TEST(IgnoreResultTest, PolymorphicAction) {
1100 TEST(IgnoreResultTest, MonomorphicAction) {
1109 MyClass ReturnMyClass(
double ) {
1114 TEST(IgnoreResultTest, ActionReturningClass) {
1121 TEST(AssignTest, Int) {
1123 Action<void(int)> a =
Assign(&x, 5);
1130 Action<void(void)> a =
Assign(&x,
"Hello, world");
1135 TEST(AssignTest, CompatibleTypes) {
1137 Action<void(int)> a =
Assign(&x, 5);
1142 #if !GTEST_OS_WINDOWS_MOBILE 1146 virtual void SetUp() { errno = 0; }
1147 virtual void TearDown() { errno = 0; }
1150 TEST_F(SetErrnoAndReturnTest, Int) {
1156 TEST_F(SetErrnoAndReturnTest, Ptr) {
1163 TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1169 #endif // !GTEST_OS_WINDOWS_MOBILE 1174 TEST(ByRefTest, IsCopyable) {
1184 ref_wrapper =
ByRef(s2);
1191 ref_wrapper = ref_wrapper1;
1197 TEST(ByRefTest, ConstValue) {
1201 const int& const_ref =
ByRef(n);
1206 TEST(ByRefTest, NonConstValue) {
1210 int& ref =
ByRef(n);
1214 const int& const_ref =
ByRef(n);
1219 TEST(ByRefTest, ExplicitType) {
1221 const int& r1 = ByRef<const int>(n);
1228 Derived& r2 = ByRef<Derived>(d);
1231 const Derived& r3 = ByRef<const Derived>(d);
1234 Base& r4 = ByRef<Base>(d);
1237 const Base& r5 = ByRef<const Base>(d);
1248 TEST(ByRefTest, PrintsCorrectly) {
1250 ::std::stringstream expected, actual;
1253 EXPECT_EQ(expected.str(), actual.str());
TypeWithSize< 8 >::Int Int64
#define MOCK_METHOD0(m,...)
internal::NeMatcher< Rhs > Ne(Rhs x)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
boost::function< void(void)> VoidFunc
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgumentPointee(const T &x)
PolymorphicAction< Impl > MakePolymorphicAction(const Impl &impl)
TypeWithSize< 8 >::UInt UInt64
bool operator==(const OpcUa::BrowseDescription &lhs, const OpcUa::BrowseDescription &rhs)
#define EXPECT_TRUE(condition)
#define ON_CALL(obj, call)
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
internal::ReturnRefAction< R > ReturnRef(R &x)
#define EXPECT_STREQ(expected, actual)
#define TEST_F(test_fixture, test_name)
Action< F > MakeAction(ActionInterface< F > *impl)
#define EXPECT_DOUBLE_EQ(expected, actual)
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgPointee(const T &x)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define EXPECT_FALSE(condition)
ConvertibleToBool IsNotZero(int number)
void UniversalPrint(const T &value,::std::ostream *os)
#define GTEST_DISALLOW_ASSIGN_(type)
#define EXPECT_NE(expected, actual)
#define TEST(test_case_name, test_name)
#define EXPECT_ANY_THROW(statement)
PolymorphicAction< internal::ReturnNullAction > ReturnNull()
#define MOCK_METHOD1(m,...)
internal::NamedArg< char > arg(StringRef name, const T &arg)
internal::DoDefaultAction DoDefault()
#define EXPECT_CALL(obj, call)
internal::ReferenceWrapper< T > ByRef(T &l_value)
PolymorphicAction< internal::AssignAction< T1, T2 > > Assign(T1 *ptr, T2 val)
internal::DoBothAction< Action1, Action2 > DoAll(Action1 a1, Action2 a2)
const internal::AnythingMatcher _
internal::IgnoreResultAction< A > IgnoreResult(const A &an_action)
PolymorphicAction< internal::SetErrnoAndReturnAction< T > > SetErrnoAndReturn(int errval, T result)
static void Print(const T &value,::std::ostream *os)
#define EXPECT_EQ(expected, actual)
internal::ReturnRefOfCopyAction< R > ReturnRefOfCopy(const R &x)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
internal::ReturnAction< R > Return(R value)