48namespace gmock_matchers_test {
53TEST_P(MonotonicMatcherTestP, IsPrintable) {
59TEST(MatchResultListenerTest, StreamingWorks) {
60 StringMatchResultListener listener;
61 listener <<
"hi" << 5;
71 DummyMatchResultListener dummy;
75TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
76 EXPECT_TRUE(DummyMatchResultListener().stream() ==
nullptr);
77 EXPECT_TRUE(StreamMatchResultListener(
nullptr).stream() ==
nullptr);
79 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
82TEST(MatchResultListenerTest, IsInterestedWorks) {
83 EXPECT_TRUE(StringMatchResultListener().IsInterested());
84 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
87 EXPECT_FALSE(StreamMatchResultListener(
nullptr).IsInterested());
92class EvenMatcherImpl :
public MatcherInterface<int> {
94 bool MatchAndExplain(
int x,
95 MatchResultListener* )
const override {
99 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
107TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
113class NewEvenMatcherImpl :
public MatcherInterface<int> {
115 bool MatchAndExplain(
int x, MatchResultListener* listener)
const override {
116 const bool match =
x % 2 == 0;
118 *listener <<
"value % " << 2;
119 if (listener->stream() !=
nullptr) {
122 *listener->stream() <<
" == " << (
x % 2);
127 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
130TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
131 Matcher<int> m = MakeMatcher(
new NewEvenMatcherImpl);
141TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; }
144TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
145 const MatcherInterface<int>* impl =
new EvenMatcherImpl;
146 Matcher<int> m(impl);
152TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
159TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
160 Matcher<int*> m1 =
nullptr;
169 virtual ~Undefined() = 0;
173TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
174 Matcher<int> m1 = Undefined::kInt;
180TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m = _; }
183TEST(MatcherTest, IsCopyable) {
185 Matcher<bool> m1 = Eq(
false);
197TEST(MatcherTest, CanDescribeItself) {
202TEST_P(MatcherTestP, MatchAndExplain) {
203 Matcher<int> m = GreaterThan(0);
204 StringMatchResultListener listener1;
206 EXPECT_EQ(
"which is 42 more than 0", listener1.str());
208 StringMatchResultListener listener2;
210 EXPECT_EQ(
"which is 9 less than 0", listener2.str());
215TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
216 Matcher<std::string> m1 =
"hi";
220 Matcher<const std::string&> m2 =
"hi";
227TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
228 Matcher<std::string> m1 = std::string(
"hi");
232 Matcher<const std::string&> m2 = std::string(
"hi");
237#if GTEST_INTERNAL_HAS_STRING_VIEW
240TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
241 Matcher<internal::StringView> m1 =
"cats";
245 Matcher<const internal::StringView&> m2 =
"cats";
252TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
253 Matcher<internal::StringView> m1 = std::string(
"cats");
257 Matcher<const internal::StringView&> m2 = std::string(
"cats");
264TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
265 Matcher<internal::StringView> m1 = internal::StringView(
"cats");
269 Matcher<const internal::StringView&> m2 = internal::StringView(
"cats");
277TEST(StringMatcherTest,
278 CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
279 std::string
value =
"cats";
280 Matcher<std::string> m1 = Eq(std::ref(
value));
284 Matcher<const std::string&> m2 = Eq(std::ref(
value));
292TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
293 const MatcherInterface<int>* dummy_impl =
new EvenMatcherImpl;
294 Matcher<int> m = MakeMatcher(dummy_impl);
300class ReferencesBarOrIsZeroImpl {
302 template <
typename T>
303 bool MatchAndExplain(
const T&
x, MatchResultListener* )
const {
305 return p == &g_bar ||
x == 0;
308 void DescribeTo(ostream* os)
const { *os <<
"g_bar or zero"; }
310 void DescribeNegationTo(ostream* os)
const {
311 *os <<
"doesn't reference g_bar and is not zero";
317PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
318 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
321TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
323 Matcher<const int&> m1 = ReferencesBarOrIsZero();
331 Matcher<double> m2 = ReferencesBarOrIsZero();
339class PolymorphicIsEvenImpl {
341 void DescribeTo(ostream* os)
const { *os <<
"is even"; }
343 void DescribeNegationTo(ostream* os)
const { *os <<
"is odd"; }
345 template <
typename T>
346 bool MatchAndExplain(
const T&
x, MatchResultListener* listener)
const {
348 *listener <<
"% " << 2;
349 if (listener->stream() !=
nullptr) {
352 *listener->stream() <<
" == " << (
x % 2);
358PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
359 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
362TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
364 const Matcher<int> m1 = PolymorphicIsEven();
369 const Matcher<int> not_m1 = Not(m1);
375 const Matcher<char> m2 = PolymorphicIsEven();
380 const Matcher<char> not_m2 = Not(m2);
389TEST_P(MatcherCastTestP, FromPolymorphicMatcher) {
391 if (use_gtest_matcher_) {
394 m = MatcherCast<int16_t>(Gt(int64_t{5}));
405 explicit IntValue(
int a_value) : value_(a_value) {}
407 int value()
const {
return value_; }
414bool IsPositiveIntValue(
const IntValue&
foo) {
return foo.value() > 0; }
418TEST(MatcherCastTest, FromCompatibleType) {
419 Matcher<double> m1 = Eq(2.0);
420 Matcher<int> m2 = MatcherCast<int>(m1);
424 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
425 Matcher<int> m4 = MatcherCast<int>(m3);
434TEST(MatcherCastTest, FromConstReferenceToNonReference) {
435 Matcher<const int&> m1 = Eq(0);
436 Matcher<int> m2 = MatcherCast<int>(m1);
442TEST(MatcherCastTest, FromReferenceToNonReference) {
443 Matcher<int&> m1 = Eq(0);
444 Matcher<int> m2 = MatcherCast<int>(m1);
450TEST(MatcherCastTest, FromNonReferenceToConstReference) {
451 Matcher<int> m1 = Eq(0);
452 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
458TEST(MatcherCastTest, FromNonReferenceToReference) {
459 Matcher<int> m1 = Eq(0);
460 Matcher<int&> m2 = MatcherCast<int&>(m1);
468TEST(MatcherCastTest, FromSameType) {
469 Matcher<int> m1 = Eq(0);
470 Matcher<int> m2 = MatcherCast<int>(m1);
477TEST(MatcherCastTest, FromAValue) {
478 Matcher<int> m = MatcherCast<int>(42);
485TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
486 const int kExpected =
'c';
487 Matcher<int> m = MatcherCast<int>(
'c');
492struct NonImplicitlyConstructibleTypeWithOperatorEq {
494 const NonImplicitlyConstructibleTypeWithOperatorEq& ,
500 const NonImplicitlyConstructibleTypeWithOperatorEq& ) {
508TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
509 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
510 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
511 EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
513 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
514 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
515 EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
520 MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
539namespace convertible_from_any {
541struct ConvertibleFromAny {
542 ConvertibleFromAny(
int a_value) :
value(a_value) {}
543 template <
typename T>
544 ConvertibleFromAny(
const T& ) :
value(-1) {
550bool operator==(
const ConvertibleFromAny& a,
const ConvertibleFromAny& b) {
551 return a.value == b.value;
554ostream&
operator<<(ostream& os,
const ConvertibleFromAny& a) {
555 return os << a.value;
558TEST(MatcherCastTest, ConversionConstructorIsUsed) {
559 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
564TEST(MatcherCastTest, FromConvertibleFromAny) {
565 Matcher<ConvertibleFromAny> m =
566 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
574struct IntReferenceWrapper {
575 IntReferenceWrapper(
const int& a_value) :
value(&a_value) {}
579bool operator==(
const IntReferenceWrapper& a,
const IntReferenceWrapper& b) {
580 return a.value == b.value;
583TEST(MatcherCastTest, ValueIsNotCopied) {
585 Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
592 virtual ~Base() =
default;
597 Base& operator=(
const Base&) =
delete;
600class Derived :
public Base {
602 Derived() :
Base() {}
606class OtherDerived :
public Base {};
611TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) {
613 if (use_gtest_matcher_) {
616 m2 = SafeMatcherCast<char>(Gt(32));
625TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
626 Matcher<double> m1 = DoubleEq(1.0);
627 Matcher<float> m2 = SafeMatcherCast<float>(m1);
631 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>(
'a'));
638TEST(SafeMatcherCastTest, FromBaseClass) {
640 Matcher<Base*> m1 = Eq(&d);
641 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
645 Matcher<Base&> m3 = Ref(d);
646 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
652TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
654 Matcher<const int&> m1 = Ref(n);
655 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
662TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
663 Matcher<std::unique_ptr<int>> m1 = IsNull();
664 Matcher<const std::unique_ptr<int>&> m2 =
665 SafeMatcherCast<const std::unique_ptr<int>&>(m1);
667 EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(
new int)));
671TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
672 Matcher<int> m1 = Eq(0);
673 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
681TEST(SafeMatcherCastTest, FromSameType) {
682 Matcher<int> m1 = Eq(0);
683 Matcher<int> m2 = SafeMatcherCast<int>(m1);
690namespace convertible_from_any {
691TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
692 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
697TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
698 Matcher<ConvertibleFromAny> m =
699 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
707TEST(SafeMatcherCastTest, ValueIsNotCopied) {
709 Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);
714TEST(ExpectThat, TakesLiterals) {
720TEST(ExpectThat, TakesFunctions) {
722 static void Func() {}
724 void (*func)() = Helper::Func;
730TEST(ATest, MatchesAnyValue) {
732 Matcher<double> m1 = A<double>();
739 Matcher<int&> m2 = A<int&>();
744TEST(ATest, WorksForDerivedClass) {
757TEST(AnTest, MatchesAnyValue) {
759 Matcher<int> m1 = An<int>();
766 Matcher<int&> m2 = An<int&>();
776TEST(UnderscoreTest, MatchesAnyValue) {
785 Matcher<const bool&> m2 = _;
791TEST(UnderscoreTest, CanDescribeSelf) {
797TEST(EqTest, MatchesEqualValue) {
799 const char a1[] =
"hi";
800 const char a2[] =
"hi";
802 Matcher<const char*> m1 = Eq(a1);
811 Unprintable() : c_(
'a') {}
813 bool operator==(
const Unprintable& )
const {
return true; }
815 char dummy_c() {
return c_; }
821TEST(EqTest, CanDescribeSelf) {
822 Matcher<Unprintable> m = Eq(Unprintable());
828TEST(EqTest, IsPolymorphic) {
829 Matcher<int> m1 = Eq(1);
833 Matcher<char> m2 = Eq(1);
839TEST(TypedEqTest, ChecksEqualityForGivenType) {
840 Matcher<char> m1 = TypedEq<char>(
'a');
844 Matcher<int> m2 = TypedEq<int>(6);
850TEST(TypedEqTest, CanDescribeSelf) {
861 static bool IsTypeOf(
const T& ) {
return true; }
863 template <
typename T2>
864 static void IsTypeOf(T2 v);
867TEST(TypedEqTest, HasSpecifiedType) {
869 Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5));
870 Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5));
874TEST(GeTest, ImplementsGreaterThanOrEqual) {
875 Matcher<int> m1 = Ge(0);
882TEST(GeTest, CanDescribeSelf) {
883 Matcher<int> m = Ge(5);
888TEST(GtTest, ImplementsGreaterThan) {
889 Matcher<double> m1 = Gt(0);
896TEST(GtTest, CanDescribeSelf) {
897 Matcher<int> m = Gt(5);
902TEST(LeTest, ImplementsLessThanOrEqual) {
903 Matcher<char> m1 = Le(
'b');
910TEST(LeTest, CanDescribeSelf) {
911 Matcher<int> m = Le(5);
916TEST(LtTest, ImplementsLessThan) {
917 Matcher<const std::string&> m1 = Lt(
"Hello");
924TEST(LtTest, CanDescribeSelf) {
925 Matcher<int> m = Lt(5);
930TEST(NeTest, ImplementsNotEqual) {
931 Matcher<int> m1 = Ne(0);
938TEST(NeTest, CanDescribeSelf) {
939 Matcher<int> m = Ne(5);
945 explicit MoveOnly(
int i) : i_(
i) {}
946 MoveOnly(
const MoveOnly&) =
delete;
947 MoveOnly(MoveOnly&&) =
default;
948 MoveOnly& operator=(
const MoveOnly&) =
delete;
949 MoveOnly& operator=(MoveOnly&&) =
default;
951 bool operator==(
const MoveOnly& other)
const {
return i_ == other.i_; }
952 bool operator!=(
const MoveOnly& other)
const {
return i_ != other.i_; }
953 bool operator<(
const MoveOnly& other)
const {
return i_ < other.i_; }
954 bool operator<=(
const MoveOnly& other)
const {
return i_ <= other.i_; }
955 bool operator>(
const MoveOnly& other)
const {
return i_ > other.i_; }
956 bool operator>=(
const MoveOnly& other)
const {
return i_ >= other.i_; }
967#if defined(_MSC_VER) && (_MSC_VER < 1910)
968TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) {
970TEST(ComparisonBaseTest, WorksWithMoveOnly) {
976 helper.Call(MoveOnly(0));
978 helper.Call(MoveOnly(1));
980 helper.Call(MoveOnly(0));
982 helper.Call(MoveOnly(-1));
984 helper.Call(MoveOnly(0));
986 helper.Call(MoveOnly(1));
989TEST(IsEmptyTest, MatchesContainer) {
990 const Matcher<std::vector<int>> m =
IsEmpty();
991 std::vector<int> a = {};
992 std::vector<int> b = {1};
997TEST(IsEmptyTest, MatchesStdString) {
998 const Matcher<std::string> m =
IsEmpty();
1005TEST(IsEmptyTest, MatchesCString) {
1006 const Matcher<const char*> m =
IsEmpty();
1007 const char a[] =
"";
1008 const char b[] =
"x";
1014TEST(IsNullTest, MatchesNullPointer) {
1015 Matcher<int*> m1 = IsNull();
1021 Matcher<const char*> m2 = IsNull();
1022 const char* p2 =
nullptr;
1026 Matcher<void*> m3 = IsNull();
1029 EXPECT_FALSE(m3.Matches(
reinterpret_cast<void*
>(0xbeef)));
1032TEST(IsNullTest, StdFunction) {
1033 const Matcher<std::function<void()>> m = IsNull();
1040TEST(IsNullTest, CanDescribeSelf) {
1041 Matcher<int*> m = IsNull();
1047TEST(NotNullTest, MatchesNonNullPointer) {
1048 Matcher<int*> m1 = NotNull();
1054 Matcher<const char*> m2 = NotNull();
1055 const char* p2 =
nullptr;
1060TEST(NotNullTest, LinkedPtr) {
1061 const Matcher<std::shared_ptr<int>> m = NotNull();
1062 const std::shared_ptr<int> null_p;
1063 const std::shared_ptr<int> non_null_p(
new int);
1069TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1070 const Matcher<const std::shared_ptr<double>&> m = NotNull();
1071 const std::shared_ptr<double> null_p;
1072 const std::shared_ptr<double> non_null_p(
new double);
1078TEST(NotNullTest, StdFunction) {
1079 const Matcher<std::function<void()>> m = NotNull();
1086TEST(NotNullTest, CanDescribeSelf) {
1087 Matcher<int*> m = NotNull();
1093TEST(RefTest, MatchesSameVariable) {
1096 Matcher<int&> m = Ref(a);
1102TEST(RefTest, CanDescribeSelf) {
1104 Matcher<int&> m = Ref(n);
1106 ss <<
"references the variable @" << &n <<
" 5";
1112TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1115 Matcher<const int&> m = Ref(a);
1124TEST(RefTest, IsCovariant) {
1127 Matcher<const Base&> m1 = Ref(base);
1138TEST(RefTest, ExplainsResult) {
1141 StartsWith(
"which is located @"));
1145 StartsWith(
"which is located @"));
1150template <
typename T = std::
string>
1151std::string FromStringLike(internal::StringLike<T> str) {
1152 return std::string(str);
1155TEST(StringLike, TestConversions) {
1156 EXPECT_EQ(
"foo", FromStringLike(
"foo"));
1157 EXPECT_EQ(
"foo", FromStringLike(std::string(
"foo")));
1158#if GTEST_INTERNAL_HAS_STRING_VIEW
1159 EXPECT_EQ(
"foo", FromStringLike(internal::StringView(
"foo")));
1164 EXPECT_EQ(
"foo", FromStringLike({
'f',
'o',
'o'}));
1165 const char buf[] =
"foo";
1166 EXPECT_EQ(
"foo", FromStringLike({buf, buf + 3}));
1169TEST(StrEqTest, MatchesEqualString) {
1170 Matcher<const char*> m = StrEq(std::string(
"Hello"));
1175 Matcher<const std::string&> m2 = StrEq(
"Hello");
1179#if GTEST_INTERNAL_HAS_STRING_VIEW
1180 Matcher<const internal::StringView&> m3 =
1181 StrEq(internal::StringView(
"Hello"));
1182 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1183 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1186 Matcher<const internal::StringView&> m_empty = StrEq(
"");
1187 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1188 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1189 EXPECT_FALSE(m_empty.Matches(internal::StringView(
"hello")));
1193TEST(StrEqTest, CanDescribeSelf) {
1194 Matcher<std::string> m = StrEq(
"Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1195 EXPECT_EQ(
"is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1198 std::string str(
"01204500800");
1200 Matcher<std::string> m2 = StrEq(str);
1202 str[0] = str[6] = str[7] = str[9] = str[10] =
'\0';
1203 Matcher<std::string> m3 = StrEq(str);
1207TEST(StrNeTest, MatchesUnequalString) {
1208 Matcher<const char*> m = StrNe(
"Hello");
1213 Matcher<std::string> m2 = StrNe(std::string(
"Hello"));
1217#if GTEST_INTERNAL_HAS_STRING_VIEW
1218 Matcher<const internal::StringView> m3 = StrNe(internal::StringView(
"Hello"));
1219 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1221 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1225TEST(StrNeTest, CanDescribeSelf) {
1226 Matcher<const char*> m = StrNe(
"Hi");
1230TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1231 Matcher<const char*> m = StrCaseEq(std::string(
"Hello"));
1237 Matcher<const std::string&> m2 = StrCaseEq(
"Hello");
1241#if GTEST_INTERNAL_HAS_STRING_VIEW
1242 Matcher<const internal::StringView&> m3 =
1243 StrCaseEq(internal::StringView(
"Hello"));
1244 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1245 EXPECT_TRUE(m3.Matches(internal::StringView(
"hello")));
1251TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1252 std::string str1(
"oabocdooeoo");
1253 std::string str2(
"OABOCDOOEOO");
1254 Matcher<const std::string&> m0 = StrCaseEq(str1);
1257 str1[3] = str2[3] =
'\0';
1258 Matcher<const std::string&> m1 = StrCaseEq(str1);
1261 str1[0] = str1[6] = str1[7] = str1[10] =
'\0';
1262 str2[0] = str2[6] = str2[7] = str2[10] =
'\0';
1263 Matcher<const std::string&> m2 = StrCaseEq(str1);
1264 str1[9] = str2[9] =
'\0';
1267 Matcher<const std::string&> m3 = StrCaseEq(str1);
1271 str2.append(1,
'\0');
1276TEST(StrCaseEqTest, CanDescribeSelf) {
1277 Matcher<std::string> m = StrCaseEq(
"Hi");
1281TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1282 Matcher<const char*> m = StrCaseNe(
"Hello");
1288 Matcher<std::string> m2 = StrCaseNe(std::string(
"Hello"));
1292#if GTEST_INTERNAL_HAS_STRING_VIEW
1293 Matcher<const internal::StringView> m3 =
1294 StrCaseNe(internal::StringView(
"Hello"));
1295 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hi")));
1297 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1298 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1302TEST(StrCaseNeTest, CanDescribeSelf) {
1303 Matcher<const char*> m = StrCaseNe(
"Hi");
1308TEST(HasSubstrTest, WorksForStringClasses) {
1309 const Matcher<std::string> m1 = HasSubstr(
"foo");
1310 EXPECT_TRUE(m1.Matches(std::string(
"I love food.")));
1313 const Matcher<const std::string&> m2 = HasSubstr(
"foo");
1314 EXPECT_TRUE(m2.Matches(std::string(
"I love food.")));
1317 const Matcher<std::string> m_empty = HasSubstr(
"");
1319 EXPECT_TRUE(m_empty.Matches(std::string(
"not empty")));
1323TEST(HasSubstrTest, WorksForCStrings) {
1324 const Matcher<char*> m1 = HasSubstr(
"foo");
1325 EXPECT_TRUE(m1.Matches(
const_cast<char*
>(
"I love food.")));
1329 const Matcher<const char*> m2 = HasSubstr(
"foo");
1334 const Matcher<const char*> m_empty = HasSubstr(
"");
1340#if GTEST_INTERNAL_HAS_STRING_VIEW
1342TEST(HasSubstrTest, WorksForStringViewClasses) {
1343 const Matcher<internal::StringView> m1 =
1344 HasSubstr(internal::StringView(
"foo"));
1345 EXPECT_TRUE(m1.Matches(internal::StringView(
"I love food.")));
1346 EXPECT_FALSE(m1.Matches(internal::StringView(
"tofo")));
1349 const Matcher<const internal::StringView&> m2 = HasSubstr(
"foo");
1350 EXPECT_TRUE(m2.Matches(internal::StringView(
"I love food.")));
1351 EXPECT_FALSE(m2.Matches(internal::StringView(
"tofo")));
1354 const Matcher<const internal::StringView&> m3 = HasSubstr(
"");
1355 EXPECT_TRUE(m3.Matches(internal::StringView(
"foo")));
1356 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1362TEST(HasSubstrTest, CanDescribeSelf) {
1363 Matcher<std::string> m = HasSubstr(
"foo\n\"");
1369TEST(KeyTest, CanDescribeSelf) {
1370 Matcher<const pair<std::string, int>&> m = Key(
"foo");
1375TEST_P(KeyTestP, ExplainsResult) {
1376 Matcher<pair<int, bool>> m = Key(GreaterThan(10));
1377 EXPECT_EQ(
"whose first field is a value which is 5 less than 10",
1378 Explain(m, make_pair(5,
true)));
1379 EXPECT_EQ(
"whose first field is a value which is 5 more than 10",
1380 Explain(m, make_pair(15,
true)));
1383TEST(KeyTest, MatchesCorrectly) {
1384 pair<int, std::string>
p(25,
"foo");
1391TEST(KeyTest, WorksWithMoveOnly) {
1392 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1404 using first_type = int;
1405 using second_type = std::string;
1407 const int& GetImpl(Tag<0>)
const {
return member_1; }
1408 const std::string& GetImpl(Tag<1>)
const {
return member_2; }
1411auto get(
const PairWithGet&
value) ->
decltype(
value.GetImpl(Tag<I>())) {
1412 return value.GetImpl(Tag<I>());
1414TEST(PairTest, MatchesPairWithGetCorrectly) {
1415 PairWithGet
p{25,
"foo"};
1421 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1425TEST(KeyTest, SafelyCastsInnerMatcher) {
1426 Matcher<int> is_positive = Gt(0);
1427 Matcher<int> is_negative = Lt(0);
1428 pair<char, bool>
p(
'a',
true);
1433TEST(KeyTest, InsideContainsUsingMap) {
1434 map<int, char> container;
1435 container.insert(make_pair(1,
'a'));
1436 container.insert(make_pair(2,
'b'));
1437 container.insert(make_pair(4,
'c'));
1442TEST(KeyTest, InsideContainsUsingMultimap) {
1443 multimap<int, char> container;
1444 container.insert(make_pair(1,
'a'));
1445 container.insert(make_pair(2,
'b'));
1446 container.insert(make_pair(4,
'c'));
1449 container.insert(make_pair(25,
'd'));
1451 container.insert(make_pair(25,
'e'));
1458TEST(PairTest, Typing) {
1460 Matcher<const pair<const char*, int>&> m1 = Pair(
"foo", 42);
1461 Matcher<const pair<const char*, int>> m2 = Pair(
"foo", 42);
1462 Matcher<pair<const char*, int>> m3 = Pair(
"foo", 42);
1464 Matcher<pair<int, const std::string>> m4 = Pair(25,
"42");
1465 Matcher<pair<const std::string, int>> m5 = Pair(
"25", 42);
1468TEST(PairTest, CanDescribeSelf) {
1469 Matcher<const pair<std::string, int>&> m1 = Pair(
"foo", 42);
1471 "has a first field that is equal to \"foo\""
1472 ", and has a second field that is equal to 42",
1475 "has a first field that isn't equal to \"foo\""
1476 ", or has a second field that isn't equal to 42",
1479 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1481 "has a first field that isn't equal to 13"
1482 ", and has a second field that is equal to 42",
1486TEST_P(PairTestP, CanExplainMatchResultTo) {
1489 const Matcher<pair<int, int>> m = Pair(GreaterThan(0), GreaterThan(0));
1490 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1491 Explain(m, make_pair(-1, -2)));
1495 EXPECT_EQ(
"whose second field does not match, which is 2 less than 0",
1496 Explain(m, make_pair(1, -2)));
1500 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1501 Explain(m, make_pair(-1, 2)));
1505 "whose both fields match, where the first field is a value "
1506 "which is 1 more than 0, and the second field is a value "
1507 "which is 2 more than 0",
1512 const Matcher<pair<int, int>> explain_first = Pair(GreaterThan(0), 0);
1514 "whose both fields match, where the first field is a value "
1515 "which is 1 more than 0",
1516 Explain(explain_first, make_pair(1, 0)));
1520 const Matcher<pair<int, int>> explain_second = Pair(0, GreaterThan(0));
1522 "whose both fields match, where the second field is a value "
1523 "which is 1 more than 0",
1524 Explain(explain_second, make_pair(0, 1)));
1527TEST(PairTest, MatchesCorrectly) {
1528 pair<int, std::string>
p(25,
"foo");
1547TEST(PairTest, WorksWithMoveOnly) {
1548 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1549 p.second = std::make_unique<int>(7);
1553TEST(PairTest, SafelyCastsInnerMatchers) {
1554 Matcher<int> is_positive = Gt(0);
1555 Matcher<int> is_negative = Lt(0);
1556 pair<char, bool>
p(
'a',
true);
1563TEST(PairTest, InsideContainsUsingMap) {
1564 map<int, char> container;
1565 container.insert(make_pair(1,
'a'));
1566 container.insert(make_pair(2,
'b'));
1567 container.insert(make_pair(4,
'c'));
1571 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1576TEST(FieldsAreTest, MatchesCorrectly) {
1577 std::tuple<int, std::string, double>
p(25,
"foo", .5);
1581 EXPECT_THAT(
p, FieldsAre(Ge(20), HasSubstr(
"o"), DoubleEq(.5)));
1589TEST(FieldsAreTest, CanDescribeSelf) {
1590 Matcher<const pair<std::string, int>&> m1 = FieldsAre(
"foo", 42);
1592 "has field #0 that is equal to \"foo\""
1593 ", and has field #1 that is equal to 42",
1596 "has field #0 that isn't equal to \"foo\""
1597 ", or has field #1 that isn't equal to 42",
1601TEST_P(FieldsAreTestP, CanExplainMatchResultTo) {
1603 Matcher<std::tuple<int, int, int>> m =
1604 FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));
1606 EXPECT_EQ(
"whose field #0 does not match, which is 1 less than 0",
1607 Explain(m, std::make_tuple(-1, -2, -3)));
1608 EXPECT_EQ(
"whose field #1 does not match, which is 2 less than 0",
1609 Explain(m, std::make_tuple(1, -2, -3)));
1610 EXPECT_EQ(
"whose field #2 does not match, which is 3 less than 0",
1611 Explain(m, std::make_tuple(1, 2, -3)));
1615 "whose all elements match, "
1616 "where field #0 is a value which is 1 more than 0"
1617 ", and field #1 is a value which is 2 more than 0"
1618 ", and field #2 is a value which is 3 more than 0",
1619 Explain(m, std::make_tuple(1, 2, 3)));
1622 m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));
1624 "whose all elements match, "
1625 "where field #0 is a value which is 1 more than 0"
1626 ", and field #2 is a value which is 3 more than 0",
1627 Explain(m, std::make_tuple(1, 0, 3)));
1630 m = FieldsAre(0, GreaterThan(0), 0);
1632 "whose all elements match, "
1633 "where field #1 is a value which is 1 more than 0",
1634 Explain(m, std::make_tuple(0, 1, 0)));
1637#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
1638TEST(FieldsAreTest, StructuredBindings) {
1666 EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));
1668 int a, b, c, d, e, f;
1670 EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));
1672 int a, b, c, d, e, f, g;
1674 EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));
1676 int a, b, c, d, e, f, g, h;
1678 EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));
1680 int a, b, c, d, e, f, g, h,
i;
1682 EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));
1683 struct MyVarType10 {
1684 int a, b, c, d, e, f, g, h,
i, j;
1686 EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1687 struct MyVarType11 {
1688 int a, b, c, d, e, f, g, h,
i, j, k;
1690 EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1691 struct MyVarType12 {
1692 int a, b, c, d, e, f, g, h,
i, j, k, l;
1694 EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1695 struct MyVarType13 {
1696 int a, b, c, d, e, f, g, h,
i, j, k, l, m;
1698 EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1699 struct MyVarType14 {
1700 int a, b, c, d, e, f, g, h,
i, j, k, l, m, n;
1703 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1704 struct MyVarType15 {
1705 int a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o;
1708 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1709 struct MyVarType16 {
1710 int a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p;
1713 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1714 struct MyVarType17 {
1715 int a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p, q;
1718 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1719 struct MyVarType18 {
1720 int a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p, q, r;
1723 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1724 struct MyVarType19 {
1725 int a, b, c, d, e, f, g, h,
i, j, k, l, m, n, o,
p, q, r, s;
1727 EXPECT_THAT(MyVarType19{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1732TEST(PairTest, UseGetInsteadOfMembers) {
1733 PairWithGet pair{7,
"ABC"};
1738 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1740 ElementsAre(Pair(11, std::string(
"Foo")), Pair(Ge(10), Not(
""))));
1745TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1746 const Matcher<const char*> m1 = StartsWith(std::string(
""));
1751 const Matcher<const std::string&> m2 = StartsWith(
"Hi");
1758#if GTEST_INTERNAL_HAS_STRING_VIEW
1759 const Matcher<internal::StringView> m_empty =
1760 StartsWith(internal::StringView(
""));
1761 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1762 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1763 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"not empty")));
1767TEST(StartsWithTest, CanDescribeSelf) {
1768 Matcher<const std::string> m = StartsWith(
"Hi");
1774TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1775 const Matcher<const char*> m1 = EndsWith(
"");
1780 const Matcher<const std::string&> m2 = EndsWith(std::string(
"Hi"));
1787#if GTEST_INTERNAL_HAS_STRING_VIEW
1788 const Matcher<const internal::StringView&> m4 =
1789 EndsWith(internal::StringView(
""));
1793 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1797TEST(EndsWithTest, CanDescribeSelf) {
1798 Matcher<const std::string> m = EndsWith(
"Hi");
1804TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
1805 const Matcher<const char*> m1 = WhenBase64Unescaped(EndsWith(
"!"));
1811 const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith(
"!"));
1817#if GTEST_INTERNAL_HAS_STRING_VIEW
1818 const Matcher<const internal::StringView&> m3 =
1819 WhenBase64Unescaped(EndsWith(
"!"));
1827TEST(WhenBase64UnescapedTest, CanDescribeSelf) {
1828 const Matcher<const char*> m = WhenBase64Unescaped(EndsWith(
"!"));
1834TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1835 const Matcher<const char*> m1 = MatchesRegex(
"a.*z");
1840 const Matcher<const std::string&> m2 = MatchesRegex(
new RE(
"a.*z"));
1845#if GTEST_INTERNAL_HAS_STRING_VIEW
1846 const Matcher<const internal::StringView&> m3 = MatchesRegex(
"a.*z");
1847 EXPECT_TRUE(m3.Matches(internal::StringView(
"az")));
1848 EXPECT_TRUE(m3.Matches(internal::StringView(
"abcz")));
1851 const Matcher<const internal::StringView&> m4 =
1852 MatchesRegex(internal::StringView(
""));
1853 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1858TEST(MatchesRegexTest, CanDescribeSelf) {
1859 Matcher<const std::string> m1 = MatchesRegex(std::string(
"Hi.*"));
1862 Matcher<const char*> m2 = MatchesRegex(
new RE(
"a.*"));
1865#if GTEST_INTERNAL_HAS_STRING_VIEW
1866 Matcher<const internal::StringView> m3 = MatchesRegex(
new RE(
"0.*"));
1873TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1874 const Matcher<const char*> m1 = ContainsRegex(std::string(
"a.*z"));
1879 const Matcher<const std::string&> m2 = ContainsRegex(
new RE(
"a.*z"));
1884#if GTEST_INTERNAL_HAS_STRING_VIEW
1885 const Matcher<const internal::StringView&> m3 = ContainsRegex(
new RE(
"a.*z"));
1886 EXPECT_TRUE(m3.Matches(internal::StringView(
"azbz")));
1887 EXPECT_TRUE(m3.Matches(internal::StringView(
"az1")));
1890 const Matcher<const internal::StringView&> m4 =
1891 ContainsRegex(internal::StringView(
""));
1892 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1897TEST(ContainsRegexTest, CanDescribeSelf) {
1898 Matcher<const std::string> m1 = ContainsRegex(
"Hi.*");
1901 Matcher<const char*> m2 = ContainsRegex(
new RE(
"a.*"));
1904#if GTEST_INTERNAL_HAS_STRING_VIEW
1905 Matcher<const internal::StringView> m3 = ContainsRegex(
new RE(
"0.*"));
1911#if GTEST_HAS_STD_WSTRING
1912TEST(StdWideStrEqTest, MatchesEqual) {
1913 Matcher<const wchar_t*> m = StrEq(::std::wstring(L
"Hello"));
1918 Matcher<const ::std::wstring&> m2 = StrEq(L
"Hello");
1922 Matcher<const ::std::wstring&> m3 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1926 ::std::wstring str(L
"01204500800");
1928 Matcher<const ::std::wstring&> m4 = StrEq(str);
1930 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
1931 Matcher<const ::std::wstring&> m5 = StrEq(str);
1935TEST(StdWideStrEqTest, CanDescribeSelf) {
1936 Matcher<::std::wstring> m = StrEq(L
"Hi-\'\"?\\\a\b\f\n\r\t\v");
1937 EXPECT_EQ(
"is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1940 Matcher<::std::wstring> m2 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1943 ::std::wstring str(L
"01204500800");
1945 Matcher<const ::std::wstring&> m4 = StrEq(str);
1947 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
1948 Matcher<const ::std::wstring&> m5 = StrEq(str);
1952TEST(StdWideStrNeTest, MatchesUnequalString) {
1953 Matcher<const wchar_t*> m = StrNe(L
"Hello");
1958 Matcher<::std::wstring> m2 = StrNe(::std::wstring(L
"Hello"));
1963TEST(StdWideStrNeTest, CanDescribeSelf) {
1964 Matcher<const wchar_t*> m = StrNe(L
"Hi");
1968TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1969 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L
"Hello"));
1975 Matcher<const ::std::wstring&> m2 = StrCaseEq(L
"Hello");
1980TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1981 ::std::wstring str1(L
"oabocdooeoo");
1982 ::std::wstring str2(L
"OABOCDOOEOO");
1983 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1984 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L
'\0')));
1986 str1[3] = str2[3] = L
'\0';
1987 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1990 str1[0] = str1[6] = str1[7] = str1[10] = L
'\0';
1991 str2[0] = str2[6] = str2[7] = str2[10] = L
'\0';
1992 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1993 str1[9] = str2[9] = L
'\0';
1996 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
2000 str2.append(1, L
'\0');
2005TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
2006 Matcher<::std::wstring> m = StrCaseEq(L
"Hi");
2010TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
2011 Matcher<const wchar_t*> m = StrCaseNe(L
"Hello");
2017 Matcher<::std::wstring> m2 = StrCaseNe(::std::wstring(L
"Hello"));
2022TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
2023 Matcher<const wchar_t*> m = StrCaseNe(L
"Hi");
2028TEST(StdWideHasSubstrTest, WorksForStringClasses) {
2029 const Matcher<::std::wstring> m1 = HasSubstr(L
"foo");
2030 EXPECT_TRUE(m1.Matches(::std::wstring(L
"I love food.")));
2033 const Matcher<const ::std::wstring&> m2 = HasSubstr(L
"foo");
2034 EXPECT_TRUE(m2.Matches(::std::wstring(L
"I love food.")));
2039TEST(StdWideHasSubstrTest, WorksForCStrings) {
2040 const Matcher<wchar_t*> m1 = HasSubstr(L
"foo");
2041 EXPECT_TRUE(m1.Matches(
const_cast<wchar_t*
>(L
"I love food.")));
2042 EXPECT_FALSE(m1.Matches(
const_cast<wchar_t*
>(L
"tofo")));
2045 const Matcher<const wchar_t*> m2 = HasSubstr(L
"foo");
2052TEST(StdWideHasSubstrTest, CanDescribeSelf) {
2053 Matcher<::std::wstring> m = HasSubstr(L
"foo\n\"");
2059TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
2060 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L
""));
2065 const Matcher<const ::std::wstring&> m2 = StartsWith(L
"Hi");
2073TEST(StdWideStartsWithTest, CanDescribeSelf) {
2074 Matcher<const ::std::wstring> m = StartsWith(L
"Hi");
2080TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
2081 const Matcher<const wchar_t*> m1 = EndsWith(L
"");
2086 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L
"Hi"));
2094TEST(StdWideEndsWithTest, CanDescribeSelf) {
2095 Matcher<const ::std::wstring> m = EndsWith(L
"Hi");
2101TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
2102 StringMatchResultListener listener1;
2103 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
2106 StringMatchResultListener listener2;
2107 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
2111TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
2112 const Matcher<int> is_even = PolymorphicIsEven();
2113 StringMatchResultListener listener1;
2114 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
2117 const Matcher<const double&> is_zero = Eq(0);
2118 StringMatchResultListener listener2;
2119 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
2123MATCHER(ConstructNoArg,
"") {
return true; }
2124MATCHER_P(Construct1Arg, arg1,
"") {
return true; }
2125MATCHER_P2(Construct2Args, arg1, arg2,
"") {
return true; }
2127TEST(MatcherConstruct, ExplicitVsImplicit) {
2130 ConstructNoArgMatcher m = {};
2133 ConstructNoArgMatcher m2;
2139 using M = Construct1ArgMatcherP<int>;
2140 EXPECT_TRUE((std::is_constructible<M, int>::value));
2145 Construct2ArgsMatcherP2<int, double> m = {1, 2.2};
2151 return ExplainMatchResult(inner_matcher, arg, result_listener);
2154TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2158TEST(DescribeMatcherTest, WorksWithValue) {
2159 EXPECT_EQ(
"is equal to 42", DescribeMatcher<int>(42));
2160 EXPECT_EQ(
"isn't equal to 42", DescribeMatcher<int>(42,
true));
2163TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
2164 const Matcher<int> monomorphic = Le(0);
2165 EXPECT_EQ(
"is <= 0", DescribeMatcher<int>(monomorphic));
2166 EXPECT_EQ(
"isn't <= 0", DescribeMatcher<int>(monomorphic,
true));
2169TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
2170 EXPECT_EQ(
"is even", DescribeMatcher<int>(PolymorphicIsEven()));
2171 EXPECT_EQ(
"is odd", DescribeMatcher<int>(PolymorphicIsEven(),
true));
2175 return ExplainMatchResult(inner_matcher, arg.i, result_listener);
2179TEST(WhenDynamicCastToTest, SameType) {
2184 Base* as_base_ptr = &derived;
2185 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull())));
2186 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));
2188 Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));
2191TEST(WhenDynamicCastToTest, WrongTypes) {
2194 OtherDerived other_derived;
2197 EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
2198 EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull()));
2199 Base* as_base_ptr = &derived;
2200 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_))));
2201 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull()));
2202 as_base_ptr = &other_derived;
2203 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
2204 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
2207TEST(WhenDynamicCastToTest, AlreadyNull) {
2209 Base* as_base_ptr =
nullptr;
2210 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
2213struct AmbiguousCastTypes {
2214 class VirtualDerived :
public virtual Base {};
2215 class DerivedSub1 :
public VirtualDerived {};
2216 class DerivedSub2 :
public VirtualDerived {};
2217 class ManyDerivedInHierarchy :
public DerivedSub1,
public DerivedSub2 {};
2220TEST(WhenDynamicCastToTest, AmbiguousCast) {
2221 AmbiguousCastTypes::DerivedSub1 sub1;
2222 AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
2225 static_cast<AmbiguousCastTypes::DerivedSub1*
>(&many_derived);
2227 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull()));
2228 as_base_ptr = &sub1;
2231 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull())));
2234TEST(WhenDynamicCastToTest, Describe) {
2235 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
2236 const std::string prefix =
2237 "when dynamic_cast to " + internal::GetTypeName<Derived*>() +
", ";
2239 EXPECT_EQ(prefix +
"does not point to a value that is anything",
2243TEST(WhenDynamicCastToTest, Explain) {
2244 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
2245 Base* null =
nullptr;
2252 Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_);
2254 HasSubstr(
"which cannot be dynamic_cast"));
2257TEST(WhenDynamicCastToTest, GoodReference) {
2260 Base& as_base_ref = derived;
2261 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
2262 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));
2265TEST(WhenDynamicCastToTest, BadReference) {
2267 Base& as_base_ref = derived;
2268 EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_)));
2272class DivisibleByImpl {
2274 explicit DivisibleByImpl(
int a_divider) : divider_(a_divider) {}
2277 template <
typename T>
2278 bool MatchAndExplain(
const T& n, MatchResultListener* listener)
const {
2279 *listener <<
"which is " << (n % divider_) <<
" modulo " << divider_;
2280 return (n % divider_) == 0;
2283 void DescribeTo(ostream* os)
const { *os <<
"is divisible by " << divider_; }
2285 void DescribeNegationTo(ostream* os)
const {
2286 *os <<
"is not divisible by " << divider_;
2289 void set_divider(
int a_divider) { divider_ = a_divider; }
2290 int divider()
const {
return divider_; }
2296PolymorphicMatcher<DivisibleByImpl> DivisibleBy(
int n) {
2297 return MakePolymorphicMatcher(DivisibleByImpl(n));
2302TEST(ExplainMatchResultTest, AllOf_False_False) {
2303 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2309TEST(ExplainMatchResultTest, AllOf_False_True) {
2310 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2316TEST(ExplainMatchResultTest, AllOf_True_False) {
2317 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
2323TEST(ExplainMatchResultTest, AllOf_True_True) {
2324 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
2328TEST(ExplainMatchResultTest, AllOf_True_True_2) {
2329 const Matcher<int> m = AllOf(Ge(2), Le(3));
2335TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {
2336 const Matcher<int> m = GreaterThan(5);
2341TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
2342 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
2343 DivisibleByImpl& impl = m.mutable_impl();
2346 impl.set_divider(0);
2347 EXPECT_EQ(0, m.mutable_impl().divider());
2351TEST(PolymorphicMatcherTest, CanAccessImpl) {
2352 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
2353 const DivisibleByImpl& impl = m.impl();
Definition gtest_unittest.cc:5120
Definition googletest-list-tests-unittest_.cc:69
int value
Definition gmock-actions_test.cc:1714
#define MOCK_METHOD1(m,...)
Definition gmock-function-mocker.h:357
int member_1
Definition gmock-matchers-comparisons_test.cc:1402
std::string member_2
Definition gmock-matchers-comparisons_test.cc:1403
static const int kInt
Definition gmock-matchers-comparisons_test.cc:170
int i
Definition gmock-matchers-comparisons_test.cc:603
int x
Definition gmock-matchers-containers_test.cc:376
const char * p
Definition gmock-matchers-containers_test.cc:379
#define MATCHER_P2(name, p0, p1, description)
#define EXPECT_THAT(value, matcher)
#define MATCHER_P(name, p0, description)
#define MATCHER(name, description)
#define INSTANTIATE_GTEST_MATCHER_TEST_P(TestSuite)
Definition gmock-matchers_test.h:151
#define EXPECT_CALL(obj, call)
Definition gmock-spec-builders.h:2145
std::ostream & operator<<(std::ostream &stream, const CustomStruct &val)
Definition googletest-param-test-test.cc:999
#define TEST_P(test_suite_name, test_name)
Definition gtest-param-test.h:450
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition gtest-port.h:360
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition gtest-port.h:361
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1868
#define TEST(test_suite_name, test_name)
Definition gtest.h:2176
#define EXPECT_TRUE(condition)
Definition gtest.h:1807
#define ADD_FAILURE()
Definition gtest.h:1735
#define EXPECT_FALSE(condition)
Definition gtest.h:1811
Definition googletest-output-test_.cc:471
constexpr auto operator!=(GaussianDistribution< T > const &lhs, GaussianDistribution< T > const &rhs)
Definition gaussian.hpp:114
constexpr auto operator==(GaussianDistribution< T > const &lhs, GaussianDistribution< T > const &rhs)
Definition gaussian.hpp:96
GtestGreaterThanMatcher< typename std::decay< T >::type > GtestGreaterThan(T &&rhs)
Definition gmock-matchers_test.h:126
std::string Explain(const MatcherType &m, const Value &x)
Definition gmock-matchers_test.h:183
std::string Describe(const Matcher< T > &m)
Definition gmock-matchers_test.h:171
std::string DescribeNegation(const Matcher< T > &m)
Definition gmock-matchers_test.h:177
Definition gmock-actions.h:151
inline ::std::reference_wrapper< T > ByRef(T &l_value)
Definition gmock-actions.h:1994
PolymorphicMatcher< internal::IsEmptyMatcher > IsEmpty()
Definition gmock-more-matchers.h:93