49 namespace gmock_matchers_test {
54 TEST_P(MonotonicMatcherTestP, IsPrintable) {
60 TEST(MatchResultListenerTest, StreamingWorks) {
61 StringMatchResultListener listener;
62 listener <<
"hi" << 5;
72 DummyMatchResultListener dummy;
76 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
77 EXPECT_TRUE(DummyMatchResultListener().stream() ==
nullptr);
78 EXPECT_TRUE(StreamMatchResultListener(
nullptr).stream() ==
nullptr);
80 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
83 TEST(MatchResultListenerTest, IsInterestedWorks) {
84 EXPECT_TRUE(StringMatchResultListener().IsInterested());
85 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
88 EXPECT_FALSE(StreamMatchResultListener(
nullptr).IsInterested());
93 class EvenMatcherImpl :
public MatcherInterface<int> {
95 bool MatchAndExplain(
int x,
96 MatchResultListener* )
const override {
100 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
108 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
114 class NewEvenMatcherImpl :
public MatcherInterface<int> {
116 bool MatchAndExplain(
int x, MatchResultListener* listener)
const override {
117 const bool match = x % 2 == 0;
119 *listener <<
"value % " << 2;
120 if (listener->stream() !=
nullptr) {
123 *listener->stream() <<
" == " << (x % 2);
128 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
131 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
132 Matcher<int> m = MakeMatcher(
new NewEvenMatcherImpl);
142 TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; }
145 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
146 const MatcherInterface<int>* impl =
new EvenMatcherImpl;
147 Matcher<int> m(impl);
153 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
160 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
161 Matcher<int*> m1 =
nullptr;
170 virtual ~Undefined() = 0;
174 TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
181 TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m =
_; }
184 TEST(MatcherTest, IsCopyable) {
186 Matcher<bool> m1 = Eq(
false);
198 TEST(MatcherTest, CanDescribeItself) {
203 TEST_P(MatcherTestP, MatchAndExplain) {
204 Matcher<int> m = GreaterThan(0);
205 StringMatchResultListener listener1;
207 EXPECT_EQ(
"which is 42 more than 0", listener1.str());
209 StringMatchResultListener listener2;
211 EXPECT_EQ(
"which is 9 less than 0", listener2.str());
216 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
217 Matcher<std::string> m1 =
"hi";
221 Matcher<const std::string&> m2 =
"hi";
228 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
229 Matcher<std::string> m1 = std::string(
"hi");
233 Matcher<const std::string&> m2 = std::string(
"hi");
238 #if GTEST_INTERNAL_HAS_STRING_VIEW
241 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
242 Matcher<internal::StringView> m1 =
"cats";
246 Matcher<const internal::StringView&> m2 =
"cats";
253 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
254 Matcher<internal::StringView> m1 = std::string(
"cats");
258 Matcher<const internal::StringView&> m2 = std::string(
"cats");
265 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
266 Matcher<internal::StringView> m1 = internal::StringView(
"cats");
270 Matcher<const internal::StringView&> m2 = internal::StringView(
"cats");
274 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
278 TEST(StringMatcherTest,
279 CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
280 std::string
value =
"cats";
281 Matcher<std::string> m1 = Eq(std::ref(value));
285 Matcher<const std::string&> m2 = Eq(std::ref(value));
293 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
294 const MatcherInterface<int>* dummy_impl =
new EvenMatcherImpl;
295 Matcher<int> m = MakeMatcher(dummy_impl);
301 class ReferencesBarOrIsZeroImpl {
303 template <
typename T>
304 bool MatchAndExplain(
const T& x, MatchResultListener* )
const {
306 return p == &g_bar || x == 0;
309 void DescribeTo(ostream* os)
const { *os <<
"g_bar or zero"; }
311 void DescribeNegationTo(ostream* os)
const {
312 *os <<
"doesn't reference g_bar and is not zero";
318 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
319 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
322 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
324 Matcher<const int&> m1 = ReferencesBarOrIsZero();
332 Matcher<double> m2 = ReferencesBarOrIsZero();
340 class PolymorphicIsEvenImpl {
342 void DescribeTo(ostream* os)
const { *os <<
"is even"; }
344 void DescribeNegationTo(ostream* os)
const { *os <<
"is odd"; }
346 template <
typename T>
347 bool MatchAndExplain(
const T& x, MatchResultListener* listener)
const {
349 *listener <<
"% " << 2;
350 if (listener->stream() !=
nullptr) {
353 *listener->stream() <<
" == " << (x % 2);
359 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
360 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
363 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
365 const Matcher<int> m1 = PolymorphicIsEven();
370 const Matcher<int> not_m1 = Not(m1);
376 const Matcher<char> m2 = PolymorphicIsEven();
381 const Matcher<char> not_m2 = Not(m2);
390 TEST_P(MatcherCastTestP, FromPolymorphicMatcher) {
392 if (use_gtest_matcher_) {
395 m = MatcherCast<int16_t>(Gt(int64_t{5}));
406 explicit IntValue(
int a_value) :
value_(a_value) {}
417 class MutableIntView {
421 explicit MutableIntView(
int& a_value) :
value_(a_value) {}
430 bool IsPositiveIntValue(
const IntValue&
foo) {
return foo.value() > 0; }
433 bool IsPositiveMutableIntView(MutableIntView foo) {
return foo.value() > 0; }
437 TEST(MatcherCastTest, FromCompatibleType) {
438 Matcher<double> m1 = Eq(2.0);
439 Matcher<int> m2 = MatcherCast<int>(m1);
443 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
444 Matcher<int> m4 = MatcherCast<int>(m3);
451 Matcher<MutableIntView> m5 = Truly(IsPositiveMutableIntView);
452 Matcher<int> m6 = MatcherCast<int>(m5);
461 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
463 Matcher<const int&> m1 = Ref(n);
464 Matcher<int> m2 = MatcherCast<int>(m1);
471 TEST(MatcherCastTest, FromConstReferenceToReference) {
473 Matcher<const int&> m1 = Ref(n);
474 Matcher<int&> m2 = MatcherCast<int&>(m1);
481 TEST(MatcherCastTest, FromReferenceToNonReference) {
482 Matcher<int&> m1 = Eq(0);
483 Matcher<int> m2 = MatcherCast<int>(m1);
489 Matcher<int&> m3 = Ref(n);
490 Matcher<int> m4 = MatcherCast<int>(m3);
495 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
496 Matcher<int> m1 = Eq(0);
497 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
503 TEST(MatcherCastTest, FromNonReferenceToReference) {
504 Matcher<int> m1 = Eq(0);
505 Matcher<int&> m2 = MatcherCast<int&>(m1);
513 TEST(MatcherCastTest, FromSameType) {
514 Matcher<int> m1 = Eq(0);
515 Matcher<int> m2 = MatcherCast<int>(m1);
522 TEST(MatcherCastTest, FromAValue) {
523 Matcher<int> m = MatcherCast<int>(42);
530 TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
531 const int kExpected =
'c';
532 Matcher<int> m = MatcherCast<int>(
'c');
537 struct NonImplicitlyConstructibleTypeWithOperatorEq {
539 const NonImplicitlyConstructibleTypeWithOperatorEq& ,
545 const NonImplicitlyConstructibleTypeWithOperatorEq& ) {
553 TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
554 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
555 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
556 EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
558 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
559 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
560 EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
565 MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
575 #if !defined _MSC_VER
584 namespace convertible_from_any {
586 struct ConvertibleFromAny {
587 ConvertibleFromAny(
int a_value) :
value(a_value) {}
588 template <
typename T>
589 ConvertibleFromAny(
const T& ) :
value(-1) {
595 bool operator==(
const ConvertibleFromAny&
a,
const ConvertibleFromAny& b) {
596 return a.value == b.value;
599 ostream&
operator<<(ostream& os,
const ConvertibleFromAny&
a) {
600 return os << a.value;
603 TEST(MatcherCastTest, ConversionConstructorIsUsed) {
604 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
609 TEST(MatcherCastTest, FromConvertibleFromAny) {
610 Matcher<ConvertibleFromAny> m =
611 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
617 #endif // !defined _MSC_VER
619 struct IntReferenceWrapper {
620 IntReferenceWrapper(
const int& a_value) : value(&a_value) {}
624 bool operator==(
const IntReferenceWrapper&
a,
const IntReferenceWrapper& b) {
625 return a.value == b.value;
628 TEST(MatcherCastTest, ValueIsNotCopied) {
630 Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
637 virtual ~
Base() =
default;
642 Base& operator=(
const Base&) =
delete;
645 class Derived :
public Base {
647 Derived() :
Base() {}
651 class OtherDerived :
public Base {};
656 TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) {
658 if (use_gtest_matcher_) {
661 m2 = SafeMatcherCast<char>(Gt(32));
670 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
671 Matcher<double> m1 = DoubleEq(1.0);
672 Matcher<float> m2 = SafeMatcherCast<float>(m1);
676 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>(
'a'));
683 TEST(SafeMatcherCastTest, FromBaseClass) {
685 Matcher<Base*> m1 = Eq(&d);
686 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
690 Matcher<Base&> m3 = Ref(d);
691 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
697 TEST(SafeMatcherCastTest, FromConstReferenceToNonReference) {
699 Matcher<const int&> m1 = Ref(n);
700 Matcher<int> m2 = SafeMatcherCast<int>(m1);
707 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
709 Matcher<const int&> m1 = Ref(n);
710 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
717 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
718 Matcher<std::unique_ptr<int>> m1 =
IsNull();
719 Matcher<const std::unique_ptr<int>&> m2 =
720 SafeMatcherCast<const std::unique_ptr<int>&>(m1);
722 EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(
new int)));
726 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
727 Matcher<int> m1 = Eq(0);
728 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
736 TEST(SafeMatcherCastTest, FromSameType) {
737 Matcher<int> m1 = Eq(0);
738 Matcher<int> m2 = SafeMatcherCast<int>(m1);
743 #if !defined _MSC_VER
745 namespace convertible_from_any {
746 TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
747 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
752 TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
753 Matcher<ConvertibleFromAny> m =
754 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
760 #endif // !defined _MSC_VER
762 TEST(SafeMatcherCastTest, ValueIsNotCopied) {
764 Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);
769 TEST(ExpectThat, TakesLiterals) {
775 TEST(ExpectThat, TakesFunctions) {
777 static void Func() {}
785 TEST(ATest, MatchesAnyValue) {
799 TEST(ATest, WorksForDerivedClass) {
812 TEST(AnTest, MatchesAnyValue) {
814 Matcher<int> m1 = An<int>();
821 Matcher<int&> m2 = An<int&>();
831 TEST(UnderscoreTest, MatchesAnyValue) {
840 Matcher<const bool&> m2 =
_;
846 TEST(UnderscoreTest, CanDescribeSelf) {
852 TEST(EqTest, MatchesEqualValue) {
854 const char a1[] =
"hi";
855 const char a2[] =
"hi";
857 Matcher<const char*> m1 = Eq(a1);
866 Unprintable() :
c_(
'a') {}
868 bool operator==(
const Unprintable& )
const {
return true; }
870 char dummy_c() {
return c_; }
876 TEST(EqTest, CanDescribeSelf) {
877 Matcher<Unprintable> m = Eq(Unprintable());
883 TEST(EqTest, IsPolymorphic) {
884 Matcher<int> m1 = Eq(1);
888 Matcher<char> m2 = Eq(1);
894 TEST(TypedEqTest, ChecksEqualityForGivenType) {
895 Matcher<char> m1 = TypedEq<char>(
'a');
899 Matcher<int> m2 = TypedEq<int>(6);
905 TEST(TypedEqTest, CanDescribeSelf) {
914 template <
typename T>
916 static bool IsTypeOf(
const T& ) {
return true; }
918 template <
typename T2>
919 static void IsTypeOf(
T2 v);
922 TEST(TypedEqTest, HasSpecifiedType) {
924 Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5));
925 Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5));
929 TEST(GeTest, ImplementsGreaterThanOrEqual) {
930 Matcher<int> m1 = Ge(0);
937 TEST(GeTest, CanDescribeSelf) {
938 Matcher<int> m = Ge(5);
943 TEST(GtTest, ImplementsGreaterThan) {
944 Matcher<double> m1 = Gt(0);
951 TEST(GtTest, CanDescribeSelf) {
952 Matcher<int> m = Gt(5);
957 TEST(LeTest, ImplementsLessThanOrEqual) {
958 Matcher<char> m1 = Le(
'b');
965 TEST(LeTest, CanDescribeSelf) {
966 Matcher<int> m = Le(5);
971 TEST(LtTest, ImplementsLessThan) {
972 Matcher<const std::string&> m1 = Lt(
"Hello");
979 TEST(LtTest, CanDescribeSelf) {
980 Matcher<int> m = Lt(5);
985 TEST(NeTest, ImplementsNotEqual) {
986 Matcher<int> m1 = Ne(0);
993 TEST(NeTest, CanDescribeSelf) {
994 Matcher<int> m = Ne(5);
1000 explicit MoveOnly(
int i) :
i_(i) {}
1001 MoveOnly(
const MoveOnly&) =
delete;
1002 MoveOnly(MoveOnly&&) =
default;
1003 MoveOnly& operator=(
const MoveOnly&) =
delete;
1004 MoveOnly& operator=(MoveOnly&&) =
default;
1006 bool operator==(
const MoveOnly& other)
const {
return i_ == other.i_; }
1007 bool operator!=(
const MoveOnly& other)
const {
return i_ != other.i_; }
1008 bool operator<(
const MoveOnly& other)
const {
return i_ < other.i_; }
1009 bool operator<=(
const MoveOnly& other)
const {
return i_ <= other.i_; }
1010 bool operator>(
const MoveOnly& other)
const {
return i_ > other.i_; }
1011 bool operator>=(
const MoveOnly& other)
const {
return i_ >= other.i_; }
1022 #if defined(_MSC_VER) && (_MSC_VER < 1910)
1023 TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) {
1025 TEST(ComparisonBaseTest, WorksWithMoveOnly) {
1031 helper.Call(MoveOnly(0));
1033 helper.Call(MoveOnly(1));
1035 helper.Call(MoveOnly(0));
1037 helper.Call(MoveOnly(-1));
1039 helper.Call(MoveOnly(0));
1041 helper.Call(MoveOnly(1));
1044 TEST(IsEmptyTest, MatchesContainer) {
1045 const Matcher<std::vector<int>> m =
IsEmpty();
1046 std::vector<int> a = {};
1047 std::vector<int> b = {1};
1052 TEST(IsEmptyTest, MatchesStdString) {
1053 const Matcher<std::string> m =
IsEmpty();
1054 std::string a =
"z";
1060 TEST(IsEmptyTest, MatchesCString) {
1061 const Matcher<const char*> m =
IsEmpty();
1062 const char a[] =
"";
1063 const char b[] =
"x";
1069 TEST(IsNullTest, MatchesNullPointer) {
1070 Matcher<int*> m1 =
IsNull();
1076 Matcher<const char*> m2 =
IsNull();
1077 const char* p2 =
nullptr;
1081 Matcher<void*> m3 =
IsNull();
1084 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
1087 TEST(IsNullTest, StdFunction) {
1088 const Matcher<std::function<void()>> m =
IsNull();
1095 TEST(IsNullTest, CanDescribeSelf) {
1096 Matcher<int*> m =
IsNull();
1102 TEST(NotNullTest, MatchesNonNullPointer) {
1103 Matcher<int*> m1 = NotNull();
1109 Matcher<const char*> m2 = NotNull();
1110 const char* p2 =
nullptr;
1115 TEST(NotNullTest, LinkedPtr) {
1116 const Matcher<std::shared_ptr<int>> m = NotNull();
1117 const std::shared_ptr<int> null_p;
1118 const std::shared_ptr<int> non_null_p(
new int);
1124 TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1125 const Matcher<const std::shared_ptr<double>&> m = NotNull();
1126 const std::shared_ptr<double> null_p;
1127 const std::shared_ptr<double> non_null_p(
new double);
1133 TEST(NotNullTest, StdFunction) {
1134 const Matcher<std::function<void()>> m = NotNull();
1141 TEST(NotNullTest, CanDescribeSelf) {
1142 Matcher<int*> m = NotNull();
1148 TEST(RefTest, MatchesSameVariable) {
1151 Matcher<int&> m = Ref(a);
1157 TEST(RefTest, CanDescribeSelf) {
1159 Matcher<int&> m = Ref(n);
1161 ss <<
"references the variable @" << &n <<
" 5";
1167 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1170 Matcher<const int&> m = Ref(a);
1179 TEST(RefTest, IsCovariant) {
1182 Matcher<const Base&> m1 = Ref(base);
1193 TEST(RefTest, ExplainsResult) {
1205 template <
typename T = std::
string>
1206 std::string FromStringLike(internal::StringLike<T> str) {
1207 return std::string(str);
1210 TEST(StringLike, TestConversions) {
1211 EXPECT_EQ(
"foo", FromStringLike(
"foo"));
1212 EXPECT_EQ(
"foo", FromStringLike(std::string(
"foo")));
1213 #if GTEST_INTERNAL_HAS_STRING_VIEW
1214 EXPECT_EQ(
"foo", FromStringLike(internal::StringView(
"foo")));
1215 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1219 EXPECT_EQ(
"foo", FromStringLike({
'f',
'o',
'o'}));
1220 const char buf[] =
"foo";
1221 EXPECT_EQ(
"foo", FromStringLike({buf, buf + 3}));
1224 TEST(StrEqTest, MatchesEqualString) {
1225 Matcher<const char*> m = StrEq(std::string(
"Hello"));
1230 Matcher<const std::string&> m2 = StrEq(
"Hello");
1234 #if GTEST_INTERNAL_HAS_STRING_VIEW
1235 Matcher<const internal::StringView&> m3 =
1236 StrEq(internal::StringView(
"Hello"));
1237 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1238 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1241 Matcher<const internal::StringView&> m_empty = StrEq(
"");
1242 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1243 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1244 EXPECT_FALSE(m_empty.Matches(internal::StringView(
"hello")));
1245 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1248 TEST(StrEqTest, CanDescribeSelf) {
1249 Matcher<std::string> m = StrEq(
"Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1250 EXPECT_EQ(
"is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1253 std::string str(
"01204500800");
1255 Matcher<std::string> m2 = StrEq(str);
1257 str[0] = str[6] = str[7] = str[9] = str[10] =
'\0';
1258 Matcher<std::string> m3 = StrEq(str);
1262 TEST(StrNeTest, MatchesUnequalString) {
1263 Matcher<const char*> m = StrNe(
"Hello");
1268 Matcher<std::string> m2 = StrNe(std::string(
"Hello"));
1272 #if GTEST_INTERNAL_HAS_STRING_VIEW
1273 Matcher<const internal::StringView> m3 = StrNe(internal::StringView(
"Hello"));
1274 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1276 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1277 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1280 TEST(StrNeTest, CanDescribeSelf) {
1281 Matcher<const char*> m = StrNe(
"Hi");
1285 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1286 Matcher<const char*> m = StrCaseEq(std::string(
"Hello"));
1292 Matcher<const std::string&> m2 = StrCaseEq(
"Hello");
1296 #if GTEST_INTERNAL_HAS_STRING_VIEW
1297 Matcher<const internal::StringView&> m3 =
1298 StrCaseEq(internal::StringView(
"Hello"));
1299 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1300 EXPECT_TRUE(m3.Matches(internal::StringView(
"hello")));
1303 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1306 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1307 std::string str1(
"oabocdooeoo");
1308 std::string str2(
"OABOCDOOEOO");
1309 Matcher<const std::string&> m0 = StrCaseEq(str1);
1312 str1[3] = str2[3] =
'\0';
1313 Matcher<const std::string&> m1 = StrCaseEq(str1);
1316 str1[0] = str1[6] = str1[7] = str1[10] =
'\0';
1317 str2[0] = str2[6] = str2[7] = str2[10] =
'\0';
1318 Matcher<const std::string&> m2 = StrCaseEq(str1);
1319 str1[9] = str2[9] =
'\0';
1322 Matcher<const std::string&> m3 = StrCaseEq(str1);
1326 str2.append(1,
'\0');
1331 TEST(StrCaseEqTest, CanDescribeSelf) {
1332 Matcher<std::string> m = StrCaseEq(
"Hi");
1336 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1337 Matcher<const char*> m = StrCaseNe(
"Hello");
1343 Matcher<std::string> m2 = StrCaseNe(std::string(
"Hello"));
1347 #if GTEST_INTERNAL_HAS_STRING_VIEW
1348 Matcher<const internal::StringView> m3 =
1349 StrCaseNe(internal::StringView(
"Hello"));
1350 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hi")));
1352 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1353 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1354 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1357 TEST(StrCaseNeTest, CanDescribeSelf) {
1358 Matcher<const char*> m = StrCaseNe(
"Hi");
1363 TEST(HasSubstrTest, WorksForStringClasses) {
1364 const Matcher<std::string> m1 = HasSubstr(
"foo");
1365 EXPECT_TRUE(m1.Matches(std::string(
"I love food.")));
1368 const Matcher<const std::string&> m2 = HasSubstr(
"foo");
1369 EXPECT_TRUE(m2.Matches(std::string(
"I love food.")));
1372 const Matcher<std::string> m_empty = HasSubstr(
"");
1374 EXPECT_TRUE(m_empty.Matches(std::string(
"not empty")));
1378 TEST(HasSubstrTest, WorksForCStrings) {
1379 const Matcher<char*> m1 = HasSubstr(
"foo");
1380 EXPECT_TRUE(m1.Matches(const_cast<char*>(
"I love food.")));
1384 const Matcher<const char*> m2 = HasSubstr(
"foo");
1389 const Matcher<const char*> m_empty = HasSubstr(
"");
1395 #if GTEST_INTERNAL_HAS_STRING_VIEW
1397 TEST(HasSubstrTest, WorksForStringViewClasses) {
1398 const Matcher<internal::StringView> m1 =
1399 HasSubstr(internal::StringView(
"foo"));
1400 EXPECT_TRUE(m1.Matches(internal::StringView(
"I love food.")));
1401 EXPECT_FALSE(m1.Matches(internal::StringView(
"tofo")));
1404 const Matcher<const internal::StringView&> m2 = HasSubstr(
"foo");
1405 EXPECT_TRUE(m2.Matches(internal::StringView(
"I love food.")));
1406 EXPECT_FALSE(m2.Matches(internal::StringView(
"tofo")));
1409 const Matcher<const internal::StringView&> m3 = HasSubstr(
"");
1410 EXPECT_TRUE(m3.Matches(internal::StringView(
"foo")));
1411 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1414 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1417 TEST(HasSubstrTest, CanDescribeSelf) {
1418 Matcher<std::string> m = HasSubstr(
"foo\n\"");
1424 TEST(KeyTest, CanDescribeSelf) {
1425 Matcher<const pair<std::string, int>&> m = Key(
"foo");
1430 TEST_P(KeyTestP, ExplainsResult) {
1431 Matcher<pair<int, bool>> m = Key(GreaterThan(10));
1432 EXPECT_EQ(
"whose first field is a value which is 5 less than 10",
1433 Explain(m, make_pair(5,
true)));
1434 EXPECT_EQ(
"whose first field is a value which is 5 more than 10",
1435 Explain(m, make_pair(15,
true)));
1438 TEST(KeyTest, MatchesCorrectly) {
1439 pair<int, std::string>
p(25,
"foo");
1446 TEST(KeyTest, WorksWithMoveOnly) {
1447 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1456 struct PairWithGet {
1459 using first_type = int;
1460 using second_type = std::string;
1462 const int& GetImpl(Tag<0>)
const {
return member_1; }
1463 const std::string& GetImpl(Tag<1>)
const {
return member_2; }
1466 auto get(
const PairWithGet&
value) -> decltype(value.GetImpl(Tag<I>())) {
1467 return value.GetImpl(Tag<I>());
1469 TEST(PairTest, MatchesPairWithGetCorrectly) {
1470 PairWithGet p{25,
"foo"};
1476 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1480 TEST(KeyTest, SafelyCastsInnerMatcher) {
1481 Matcher<int> is_positive = Gt(0);
1482 Matcher<int> is_negative = Lt(0);
1483 pair<char, bool>
p(
'a',
true);
1488 TEST(KeyTest, InsideContainsUsingMap) {
1490 container.insert(make_pair(1,
'a'));
1491 container.insert(make_pair(2,
'b'));
1492 container.insert(make_pair(4,
'c'));
1497 TEST(KeyTest, InsideContainsUsingMultimap) {
1498 multimap<int, char> container;
1499 container.insert(make_pair(1,
'a'));
1500 container.insert(make_pair(2,
'b'));
1501 container.insert(make_pair(4,
'c'));
1504 container.insert(make_pair(25,
'd'));
1506 container.insert(make_pair(25,
'e'));
1513 TEST(PairTest, Typing) {
1515 Matcher<const pair<const char*, int>&> m1 = Pair(
"foo", 42);
1516 Matcher<const pair<const char*, int>> m2 = Pair(
"foo", 42);
1517 Matcher<pair<const char*, int>> m3 = Pair(
"foo", 42);
1519 Matcher<pair<int, const std::string>> m4 = Pair(25,
"42");
1520 Matcher<pair<const std::string, int>> m5 = Pair(
"25", 42);
1523 TEST(PairTest, CanDescribeSelf) {
1524 Matcher<const pair<std::string, int>&> m1 = Pair(
"foo", 42);
1526 "has a first field that is equal to \"foo\""
1527 ", and has a second field that is equal to 42",
1530 "has a first field that isn't equal to \"foo\""
1531 ", or has a second field that isn't equal to 42",
1534 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1536 "has a first field that isn't equal to 13"
1537 ", and has a second field that is equal to 42",
1541 TEST_P(PairTestP, CanExplainMatchResultTo) {
1544 const Matcher<pair<int, int>> m = Pair(GreaterThan(0), GreaterThan(0));
1545 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1546 Explain(m, make_pair(-1, -2)));
1550 EXPECT_EQ(
"whose second field does not match, which is 2 less than 0",
1551 Explain(m, make_pair(1, -2)));
1555 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1556 Explain(m, make_pair(-1, 2)));
1560 "whose both fields match, where the first field is a value "
1561 "which is 1 more than 0, and the second field is a value "
1562 "which is 2 more than 0",
1567 const Matcher<pair<int, int>> explain_first = Pair(GreaterThan(0), 0);
1569 "whose both fields match, where the first field is a value "
1570 "which is 1 more than 0",
1571 Explain(explain_first, make_pair(1, 0)));
1575 const Matcher<pair<int, int>> explain_second = Pair(0, GreaterThan(0));
1577 "whose both fields match, where the second field is a value "
1578 "which is 1 more than 0",
1579 Explain(explain_second, make_pair(0, 1)));
1582 TEST(PairTest, MatchesCorrectly) {
1583 pair<int, std::string>
p(25,
"foo");
1599 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr(
"a"))));
1602 TEST(PairTest, WorksWithMoveOnly) {
1603 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1604 p.second = std::make_unique<int>(7);
1608 TEST(PairTest, SafelyCastsInnerMatchers) {
1609 Matcher<int> is_positive = Gt(0);
1610 Matcher<int> is_negative = Lt(0);
1611 pair<char, bool>
p(
'a',
true);
1618 TEST(PairTest, InsideContainsUsingMap) {
1619 map<int, char> container;
1620 container.insert(make_pair(1,
'a'));
1621 container.insert(make_pair(2,
'b'));
1622 container.insert(make_pair(4,
'c'));
1631 TEST(FieldsAreTest, MatchesCorrectly) {
1632 std::tuple<int, std::string, double>
p(25,
"foo", .5);
1636 EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr(
"o"), DoubleEq(.5)));
1644 TEST(FieldsAreTest, CanDescribeSelf) {
1645 Matcher<const pair<std::string, int>&> m1 = FieldsAre(
"foo", 42);
1647 "has field #0 that is equal to \"foo\""
1648 ", and has field #1 that is equal to 42",
1651 "has field #0 that isn't equal to \"foo\""
1652 ", or has field #1 that isn't equal to 42",
1656 TEST_P(FieldsAreTestP, CanExplainMatchResultTo) {
1658 Matcher<std::tuple<int, int, int>> m =
1659 FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));
1661 EXPECT_EQ(
"whose field #0 does not match, which is 1 less than 0",
1662 Explain(m, std::make_tuple(-1, -2, -3)));
1663 EXPECT_EQ(
"whose field #1 does not match, which is 2 less than 0",
1664 Explain(m, std::make_tuple(1, -2, -3)));
1665 EXPECT_EQ(
"whose field #2 does not match, which is 3 less than 0",
1666 Explain(m, std::make_tuple(1, 2, -3)));
1670 "whose all elements match, "
1671 "where field #0 is a value which is 1 more than 0"
1672 ", and field #1 is a value which is 2 more than 0"
1673 ", and field #2 is a value which is 3 more than 0",
1674 Explain(m, std::make_tuple(1, 2, 3)));
1677 m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));
1679 "whose all elements match, "
1680 "where field #0 is a value which is 1 more than 0"
1681 ", and field #2 is a value which is 3 more than 0",
1682 Explain(m, std::make_tuple(1, 0, 3)));
1685 m = FieldsAre(0, GreaterThan(0), 0);
1687 "whose all elements match, "
1688 "where field #1 is a value which is 1 more than 0",
1689 Explain(m, std::make_tuple(0, 1, 0)));
1692 #if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
1693 TEST(FieldsAreTest, StructuredBindings) {
1721 EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));
1723 int a, b,
c, d, e,
f;
1725 EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));
1727 int a, b,
c, d, e,
f,
g;
1729 EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));
1731 int a, b,
c, d, e,
f,
g, h;
1733 EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));
1735 int a, b,
c, d, e,
f,
g, h,
i;
1737 EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));
1738 struct MyVarType10 {
1739 int a, b,
c, d, e,
f,
g, h,
i, j;
1741 EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1742 struct MyVarType11 {
1743 int a, b,
c, d, e,
f,
g, h,
i, j, k;
1745 EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1746 struct MyVarType12 {
1747 int a, b,
c, d, e,
f,
g, h,
i, j, k, l;
1749 EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1750 struct MyVarType13 {
1751 int a, b,
c, d, e,
f,
g, h,
i, j, k, l, m;
1753 EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1754 struct MyVarType14 {
1755 int a, b,
c, d, e,
f,
g, h,
i, j, k, l, m, n;
1758 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1759 struct MyVarType15 {
1760 int a, b,
c, d, e,
f,
g, h,
i, j, k, l, m, n, o;
1763 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1764 struct MyVarType16 {
1765 int a, b,
c, d, e,
f,
g, h,
i, j, k, l, m, n, o,
p;
1768 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1769 struct MyVarType17 {
1770 int a, b,
c, d, e,
f,
g, h,
i, j, k, l, m, n, o,
p, q;
1773 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1774 struct MyVarType18 {
1775 int a, b,
c, d, e,
f,
g, h,
i, j, k, l, m, n, o,
p, q, r;
1778 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1779 struct MyVarType19 {
1780 int a, b,
c, d, e,
f,
g, h,
i, j, k, l, m, n, o,
p, q, r, s;
1782 EXPECT_THAT(MyVarType19{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1787 TEST(PairTest, UseGetInsteadOfMembers) {
1788 PairWithGet pair{7,
"ABC"};
1793 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1795 ElementsAre(Pair(11, std::string(
"Foo")), Pair(Ge(10), Not(
""))));
1800 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1801 const Matcher<const char*> m1 =
StartsWith(std::string(
""));
1806 const Matcher<const std::string&> m2 =
StartsWith(
"Hi");
1813 #if GTEST_INTERNAL_HAS_STRING_VIEW
1814 const Matcher<internal::StringView> m_empty =
1816 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1817 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1818 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"not empty")));
1819 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1822 TEST(StartsWithTest, CanDescribeSelf) {
1823 Matcher<const std::string> m =
StartsWith(
"Hi");
1827 TEST(StartsWithTest, WorksWithStringMatcherOnStringViewMatchee) {
1828 #if GTEST_INTERNAL_HAS_STRING_VIEW
1829 EXPECT_THAT(internal::StringView(
"talk to me goose"),
1832 GTEST_SKIP() <<
"Not applicable without internal::StringView.";
1833 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1838 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1839 const Matcher<const char*> m1 =
EndsWith(
"");
1844 const Matcher<const std::string&> m2 =
EndsWith(std::string(
"Hi"));
1851 #if GTEST_INTERNAL_HAS_STRING_VIEW
1852 const Matcher<const internal::StringView&> m4 =
1853 EndsWith(internal::StringView(
""));
1857 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1858 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1861 TEST(EndsWithTest, CanDescribeSelf) {
1862 Matcher<const std::string> m =
EndsWith(
"Hi");
1868 TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
1869 const Matcher<const char*> m1 = WhenBase64Unescaped(
EndsWith(
"!"));
1875 const Matcher<const std::string&> m2 = WhenBase64Unescaped(
EndsWith(
"!"));
1881 #if GTEST_INTERNAL_HAS_STRING_VIEW
1882 const Matcher<const internal::StringView&> m3 =
1883 WhenBase64Unescaped(
EndsWith(
"!"));
1888 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1891 TEST(WhenBase64UnescapedTest, CanDescribeSelf) {
1892 const Matcher<const char*> m = WhenBase64Unescaped(
EndsWith(
"!"));
1898 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1899 const Matcher<const char*> m1 = MatchesRegex(
"a.*z");
1904 const Matcher<const std::string&> m2 = MatchesRegex(
new RE(
"a.*z"));
1909 #if GTEST_INTERNAL_HAS_STRING_VIEW
1910 const Matcher<const internal::StringView&> m3 = MatchesRegex(
"a.*z");
1911 EXPECT_TRUE(m3.Matches(internal::StringView(
"az")));
1912 EXPECT_TRUE(m3.Matches(internal::StringView(
"abcz")));
1915 const Matcher<const internal::StringView&> m4 =
1916 MatchesRegex(internal::StringView(
""));
1917 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1919 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1922 TEST(MatchesRegexTest, CanDescribeSelf) {
1923 Matcher<const std::string> m1 = MatchesRegex(std::string(
"Hi.*"));
1926 Matcher<const char*> m2 = MatchesRegex(
new RE(
"a.*"));
1929 #if GTEST_INTERNAL_HAS_STRING_VIEW
1930 Matcher<const internal::StringView> m3 = MatchesRegex(
new RE(
"0.*"));
1932 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1937 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1938 const Matcher<const char*> m1 = ContainsRegex(std::string(
"a.*z"));
1943 const Matcher<const std::string&> m2 = ContainsRegex(
new RE(
"a.*z"));
1948 #if GTEST_INTERNAL_HAS_STRING_VIEW
1949 const Matcher<const internal::StringView&> m3 = ContainsRegex(
new RE(
"a.*z"));
1950 EXPECT_TRUE(m3.Matches(internal::StringView(
"azbz")));
1951 EXPECT_TRUE(m3.Matches(internal::StringView(
"az1")));
1954 const Matcher<const internal::StringView&> m4 =
1955 ContainsRegex(internal::StringView(
""));
1956 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1958 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1961 TEST(ContainsRegexTest, CanDescribeSelf) {
1962 Matcher<const std::string> m1 = ContainsRegex(
"Hi.*");
1965 Matcher<const char*> m2 = ContainsRegex(
new RE(
"a.*"));
1968 #if GTEST_INTERNAL_HAS_STRING_VIEW
1969 Matcher<const internal::StringView> m3 = ContainsRegex(
new RE(
"0.*"));
1971 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1975 #if GTEST_HAS_STD_WSTRING
1976 TEST(StdWideStrEqTest, MatchesEqual) {
1977 Matcher<const wchar_t*> m = StrEq(::std::wstring(L
"Hello"));
1982 Matcher<const ::std::wstring&> m2 = StrEq(L
"Hello");
1986 Matcher<const ::std::wstring&> m3 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1990 ::std::wstring str(L
"01204500800");
1992 Matcher<const ::std::wstring&> m4 = StrEq(str);
1994 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
1995 Matcher<const ::std::wstring&> m5 = StrEq(str);
1999 TEST(StdWideStrEqTest, CanDescribeSelf) {
2000 Matcher<::std::wstring> m = StrEq(L
"Hi-\'\"?\\\a\b\f\n\r\t\v");
2001 EXPECT_EQ(
"is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
2004 Matcher<::std::wstring> m2 = StrEq(L
"\xD3\x576\x8D3\xC74D");
2007 ::std::wstring str(L
"01204500800");
2009 Matcher<const ::std::wstring&> m4 = StrEq(str);
2011 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
2012 Matcher<const ::std::wstring&> m5 = StrEq(str);
2016 TEST(StdWideStrNeTest, MatchesUnequalString) {
2017 Matcher<const wchar_t*> m = StrNe(L
"Hello");
2022 Matcher<::std::wstring> m2 = StrNe(::std::wstring(L
"Hello"));
2027 TEST(StdWideStrNeTest, CanDescribeSelf) {
2028 Matcher<const wchar_t*> m = StrNe(L
"Hi");
2032 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
2033 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L
"Hello"));
2039 Matcher<const ::std::wstring&> m2 = StrCaseEq(L
"Hello");
2044 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
2045 ::std::wstring str1(L
"oabocdooeoo");
2046 ::std::wstring str2(L
"OABOCDOOEOO");
2047 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
2048 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L
'\0')));
2050 str1[3] = str2[3] = L
'\0';
2051 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
2054 str1[0] = str1[6] = str1[7] = str1[10] = L
'\0';
2055 str2[0] = str2[6] = str2[7] = str2[10] = L
'\0';
2056 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
2057 str1[9] = str2[9] = L
'\0';
2060 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
2064 str2.append(1, L
'\0');
2069 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
2070 Matcher<::std::wstring> m = StrCaseEq(L
"Hi");
2074 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
2075 Matcher<const wchar_t*> m = StrCaseNe(L
"Hello");
2081 Matcher<::std::wstring> m2 = StrCaseNe(::std::wstring(L
"Hello"));
2086 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
2087 Matcher<const wchar_t*> m = StrCaseNe(L
"Hi");
2092 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
2093 const Matcher<::std::wstring> m1 = HasSubstr(L
"foo");
2094 EXPECT_TRUE(m1.Matches(::std::wstring(L
"I love food.")));
2097 const Matcher<const ::std::wstring&> m2 = HasSubstr(L
"foo");
2098 EXPECT_TRUE(m2.Matches(::std::wstring(L
"I love food.")));
2103 TEST(StdWideHasSubstrTest, WorksForCStrings) {
2104 const Matcher<wchar_t*> m1 = HasSubstr(L
"foo");
2105 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L
"I love food.")));
2106 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L
"tofo")));
2109 const Matcher<const wchar_t*> m2 = HasSubstr(L
"foo");
2116 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
2117 Matcher<::std::wstring> m = HasSubstr(L
"foo\n\"");
2123 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
2124 const Matcher<const wchar_t*> m1 =
StartsWith(::std::wstring(L
""));
2129 const Matcher<const ::std::wstring&> m2 =
StartsWith(L
"Hi");
2137 TEST(StdWideStartsWithTest, CanDescribeSelf) {
2138 Matcher<const ::std::wstring> m =
StartsWith(L
"Hi");
2144 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
2145 const Matcher<const wchar_t*> m1 =
EndsWith(L
"");
2150 const Matcher<const ::std::wstring&> m2 =
EndsWith(::std::wstring(L
"Hi"));
2158 TEST(StdWideEndsWithTest, CanDescribeSelf) {
2159 Matcher<const ::std::wstring> m =
EndsWith(L
"Hi");
2163 #endif // GTEST_HAS_STD_WSTRING
2165 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
2166 StringMatchResultListener listener1;
2167 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
2170 StringMatchResultListener listener2;
2171 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
2175 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
2176 const Matcher<int> is_even = PolymorphicIsEven();
2177 StringMatchResultListener listener1;
2178 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
2181 const Matcher<const double&> is_zero = Eq(0);
2182 StringMatchResultListener listener2;
2183 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
2187 MATCHER(ConstructNoArg,
"") {
return true; }
2188 MATCHER_P(Construct1Arg, arg1,
"") {
return true; }
2189 MATCHER_P2(Construct2Args, arg1, arg2,
"") {
return true; }
2191 TEST(MatcherConstruct, ExplicitVsImplicit) {
2194 ConstructNoArgMatcher m = {};
2197 ConstructNoArgMatcher m2;
2203 using M = Construct1ArgMatcherP<int>;
2209 Construct2ArgsMatcherP2<int, double> m = {1, 2.2};
2215 return ExplainMatchResult(inner_matcher, arg, result_listener);
2218 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2222 TEST(DescribeMatcherTest, WorksWithValue) {
2223 EXPECT_EQ(
"is equal to 42", DescribeMatcher<int>(42));
2224 EXPECT_EQ(
"isn't equal to 42", DescribeMatcher<int>(42,
true));
2227 TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
2228 const Matcher<int> monomorphic = Le(0);
2229 EXPECT_EQ(
"is <= 0", DescribeMatcher<int>(monomorphic));
2230 EXPECT_EQ(
"isn't <= 0", DescribeMatcher<int>(monomorphic,
true));
2233 TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
2234 EXPECT_EQ(
"is even", DescribeMatcher<int>(PolymorphicIsEven()));
2235 EXPECT_EQ(
"is odd", DescribeMatcher<int>(PolymorphicIsEven(),
true));
2238 MATCHER_P(FieldIIs, inner_matcher,
"") {
2239 return ExplainMatchResult(inner_matcher, arg.i, result_listener);
2243 TEST(WhenDynamicCastToTest, SameType) {
2248 Base* as_base_ptr = &derived;
2250 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));
2252 Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));
2255 TEST(WhenDynamicCastToTest, WrongTypes) {
2258 OtherDerived other_derived;
2261 EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(
_))));
2263 Base* as_base_ptr = &derived;
2264 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(
_))));
2266 as_base_ptr = &other_derived;
2267 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(
_))));
2271 TEST(WhenDynamicCastToTest, AlreadyNull) {
2273 Base* as_base_ptr =
nullptr;
2277 struct AmbiguousCastTypes {
2278 class VirtualDerived :
public virtual Base {};
2279 class DerivedSub1 :
public VirtualDerived {};
2280 class DerivedSub2 :
public VirtualDerived {};
2281 class ManyDerivedInHierarchy :
public DerivedSub1,
public DerivedSub2 {};
2284 TEST(WhenDynamicCastToTest, AmbiguousCast) {
2285 AmbiguousCastTypes::DerivedSub1 sub1;
2286 AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
2289 static_cast<AmbiguousCastTypes::DerivedSub1*
>(&many_derived);
2291 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(
IsNull()));
2292 as_base_ptr = &sub1;
2295 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(
IsNull())));
2299 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(
_));
2300 const std::string prefix =
2301 "when dynamic_cast to " + internal::GetTypeName<Derived*>() +
", ";
2303 EXPECT_EQ(prefix +
"does not point to a value that is anything",
2308 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(
_));
2316 Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(
_);
2318 HasSubstr(
"which cannot be dynamic_cast"));
2321 TEST(WhenDynamicCastToTest, GoodReference) {
2324 Base& as_base_ref = derived;
2325 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
2326 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));
2329 TEST(WhenDynamicCastToTest, BadReference) {
2331 Base& as_base_ref = derived;
2332 EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(
_)));
2334 #endif // GTEST_HAS_RTTI
2336 class DivisibleByImpl {
2338 explicit DivisibleByImpl(
int a_divider) :
divider_(a_divider) {}
2341 template <
typename T>
2342 bool MatchAndExplain(
const T& n, MatchResultListener* listener)
const {
2347 void DescribeTo(ostream* os)
const { *os <<
"is divisible by " <<
divider_; }
2349 void DescribeNegationTo(ostream* os)
const {
2350 *os <<
"is not divisible by " <<
divider_;
2353 void set_divider(
int a_divider) {
divider_ = a_divider; }
2354 int divider()
const {
return divider_; }
2360 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(
int n) {
2361 return MakePolymorphicMatcher(DivisibleByImpl(n));
2366 TEST(ExplainMatchResultTest, AllOf_False_False) {
2367 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2373 TEST(ExplainMatchResultTest, AllOf_False_True) {
2374 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2380 TEST(ExplainMatchResultTest, AllOf_True_False) {
2381 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
2387 TEST(ExplainMatchResultTest, AllOf_True_True) {
2388 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
2394 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
2395 const Matcher<int> m = AllOf(Ge(2), Le(3));
2401 TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {
2402 const Matcher<int> m = GreaterThan(5);
2407 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
2408 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
2409 DivisibleByImpl& impl = m.mutable_impl();
2412 impl.set_divider(0);
2413 EXPECT_EQ(0, m.mutable_impl().divider());
2417 TEST(PolymorphicMatcherTest, CanAccessImpl) {
2418 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
2419 const DivisibleByImpl& impl = m.impl();
constexpr bool StartsWith(const char(&prefix)[N], const char(&str)[M])
GTEST_DISABLE_MSC_WARNINGS_POP_() TEST(LinkTest
std::string Explain(const MatcherType &m, const Value &x)
constexpr bool EndsWith(const char(&suffix)[N], const char(&str)[M])
#define TEST(test_suite_name, test_name)
MATCHER_P2(IsPair, first, second,"")
#define INSTANTIATE_GTEST_MATCHER_TEST_P(TestSuite)
#define MATCHER(name, description)
std::string Describe(const Matcher< T > &m)
inline::std::reference_wrapper< T > ByRef(T &l_value)
bool operator!=(const Allocator< T > &a_t, const Allocator< U > &a_u)
std::ostream & operator<<(std::ostream &os, const Expr< T > &xx)
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
bool operator>(BigUInt< n > const &a, BigUInt< n > const &b)
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
bool operator>=(BigUInt< n > const &a, BigUInt< n > const &b)
ADVar foo(double d, ADVar x, ADVar y)
GtestGreaterThanMatcher< typename std::decay< T >::type > GtestGreaterThan(T &&rhs)
PolymorphicMatcher< internal::IsEmptyMatcher > IsEmpty()
#define MOCK_METHOD1(m,...)
#define EXPECT_THAT(value, matcher)
#define MATCHER_P(name, p0, description)
#define EXPECT_CALL(obj, call)
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.
std::string DescribeNegation(const Matcher< T > &m)
#define EXPECT_EQ(val1, val2)
#define TEST_P(test_suite_name, test_name)
const T func(int n, T *x)
#define EXPECT_TRUE(condition)
int operator<(const ADvari &L, const ADvari &R)
#define EXPECT_FALSE(condition)
AssertionResult IsNull(const char *str)
bool operator<=(BigUInt< n > const &a, BigUInt< n > const &b)