38#pragma warning(disable : 4244)
39#pragma warning(disable : 4100)
44#include "test/gmock-matchers_test.h"
47namespace gmock_matchers_test {
50INSTANTIATE_GTEST_MATCHER_TEST_P(MonotonicMatcherTest);
52TEST_P(MonotonicMatcherTestP, IsPrintable) {
55 EXPECT_EQ(
"is > 5", ss.str());
58TEST(MatchResultListenerTest, StreamingWorks) {
59 StringMatchResultListener listener;
60 listener <<
"hi" << 5;
61 EXPECT_EQ(
"hi5", listener.str());
64 EXPECT_EQ(
"", listener.str());
67 EXPECT_EQ(
"42", listener.str());
70 DummyMatchResultListener dummy;
74TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
75 EXPECT_TRUE(DummyMatchResultListener().stream() ==
nullptr);
76 EXPECT_TRUE(StreamMatchResultListener(
nullptr).stream() ==
nullptr);
78 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
81TEST(MatchResultListenerTest, IsInterestedWorks) {
82 EXPECT_TRUE(StringMatchResultListener().IsInterested());
83 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
85 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
86 EXPECT_FALSE(StreamMatchResultListener(
nullptr).IsInterested());
91class EvenMatcherImpl :
public MatcherInterface<int> {
93 bool MatchAndExplain(
int x,
94 MatchResultListener* )
const override {
98 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
106TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
112class NewEvenMatcherImpl :
public MatcherInterface<int> {
114 bool MatchAndExplain(
int x, MatchResultListener* listener)
const override {
115 const bool match = x % 2 == 0;
117 *listener <<
"value % " << 2;
118 if (listener->stream() !=
nullptr) {
121 *listener->stream() <<
" == " << (x % 2);
126 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
129TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
130 Matcher<int> m = MakeMatcher(
new NewEvenMatcherImpl);
131 EXPECT_TRUE(m.Matches(2));
132 EXPECT_FALSE(m.Matches(3));
133 EXPECT_EQ(
"value % 2 == 0", Explain(m, 2));
134 EXPECT_EQ(
"value % 2 == 1", Explain(m, 3));
137INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherTest);
140TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; }
143TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
144 const MatcherInterface<int>* impl =
new EvenMatcherImpl;
145 Matcher<int> m(impl);
146 EXPECT_TRUE(m.Matches(4));
147 EXPECT_FALSE(m.Matches(5));
151TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
153 EXPECT_TRUE(m1.Matches(5));
154 EXPECT_FALSE(m1.Matches(6));
158TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
159 Matcher<int*> m1 =
nullptr;
160 EXPECT_TRUE(m1.Matches(
nullptr));
162 EXPECT_FALSE(m1.Matches(&n));
168 virtual ~Undefined() = 0;
169 static const int kInt = 1;
172TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
173 Matcher<int> m1 = Undefined::kInt;
174 EXPECT_TRUE(m1.Matches(1));
175 EXPECT_FALSE(m1.Matches(2));
179TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m = _; }
182TEST(MatcherTest, IsCopyable) {
184 Matcher<bool> m1 = Eq(
false);
185 EXPECT_TRUE(m1.Matches(
false));
186 EXPECT_FALSE(m1.Matches(
true));
190 EXPECT_TRUE(m1.Matches(
true));
191 EXPECT_FALSE(m1.Matches(
false));
196TEST(MatcherTest, CanDescribeItself) {
197 EXPECT_EQ(
"is an even number", Describe(Matcher<int>(
new EvenMatcherImpl)));
201TEST_P(MatcherTestP, MatchAndExplain) {
202 Matcher<int> m = GreaterThan(0);
203 StringMatchResultListener listener1;
204 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
205 EXPECT_EQ(
"which is 42 more than 0", listener1.str());
207 StringMatchResultListener listener2;
208 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
209 EXPECT_EQ(
"which is 9 less than 0", listener2.str());
214TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
215 Matcher<std::string> m1 =
"hi";
216 EXPECT_TRUE(m1.Matches(
"hi"));
217 EXPECT_FALSE(m1.Matches(
"hello"));
219 Matcher<const std::string&> m2 =
"hi";
220 EXPECT_TRUE(m2.Matches(
"hi"));
221 EXPECT_FALSE(m2.Matches(
"hello"));
226TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
227 Matcher<std::string> m1 = std::string(
"hi");
228 EXPECT_TRUE(m1.Matches(
"hi"));
229 EXPECT_FALSE(m1.Matches(
"hello"));
231 Matcher<const std::string&> m2 = std::string(
"hi");
232 EXPECT_TRUE(m2.Matches(
"hi"));
233 EXPECT_FALSE(m2.Matches(
"hello"));
236#if GTEST_INTERNAL_HAS_STRING_VIEW
239TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
240 Matcher<internal::StringView> m1 =
"cats";
241 EXPECT_TRUE(m1.Matches(
"cats"));
242 EXPECT_FALSE(m1.Matches(
"dogs"));
244 Matcher<const internal::StringView&> m2 =
"cats";
245 EXPECT_TRUE(m2.Matches(
"cats"));
246 EXPECT_FALSE(m2.Matches(
"dogs"));
251TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
252 Matcher<internal::StringView> m1 = std::string(
"cats");
253 EXPECT_TRUE(m1.Matches(
"cats"));
254 EXPECT_FALSE(m1.Matches(
"dogs"));
256 Matcher<const internal::StringView&> m2 = std::string(
"cats");
257 EXPECT_TRUE(m2.Matches(
"cats"));
258 EXPECT_FALSE(m2.Matches(
"dogs"));
263TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
264 Matcher<internal::StringView> m1 = internal::StringView(
"cats");
265 EXPECT_TRUE(m1.Matches(
"cats"));
266 EXPECT_FALSE(m1.Matches(
"dogs"));
268 Matcher<const internal::StringView&> m2 = internal::StringView(
"cats");
269 EXPECT_TRUE(m2.Matches(
"cats"));
270 EXPECT_FALSE(m2.Matches(
"dogs"));
276TEST(StringMatcherTest,
277 CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
278 std::string value =
"cats";
279 Matcher<std::string> m1 = Eq(std::ref(value));
280 EXPECT_TRUE(m1.Matches(
"cats"));
281 EXPECT_FALSE(m1.Matches(
"dogs"));
283 Matcher<const std::string&> m2 = Eq(std::ref(value));
284 EXPECT_TRUE(m2.Matches(
"cats"));
285 EXPECT_FALSE(m2.Matches(
"dogs"));
291TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
292 const MatcherInterface<int>* dummy_impl =
new EvenMatcherImpl;
293 Matcher<int> m = MakeMatcher(dummy_impl);
299class ReferencesBarOrIsZeroImpl {
301 template <
typename T>
302 bool MatchAndExplain(
const T& x, MatchResultListener* )
const {
304 return p == &g_bar || x == 0;
307 void DescribeTo(ostream* os)
const { *os <<
"g_bar or zero"; }
309 void DescribeNegationTo(ostream* os)
const {
310 *os <<
"doesn't reference g_bar and is not zero";
316PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
317 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
320TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
322 Matcher<const int&> m1 = ReferencesBarOrIsZero();
323 EXPECT_TRUE(m1.Matches(0));
325 EXPECT_TRUE(m1.Matches(g_bar));
326 EXPECT_FALSE(m1.Matches(1));
327 EXPECT_EQ(
"g_bar or zero", Describe(m1));
330 Matcher<double> m2 = ReferencesBarOrIsZero();
331 EXPECT_TRUE(m2.Matches(0.0));
332 EXPECT_FALSE(m2.Matches(0.1));
333 EXPECT_EQ(
"g_bar or zero", Describe(m2));
338class PolymorphicIsEvenImpl {
340 void DescribeTo(ostream* os)
const { *os <<
"is even"; }
342 void DescribeNegationTo(ostream* os)
const { *os <<
"is odd"; }
344 template <
typename T>
345 bool MatchAndExplain(
const T& x, MatchResultListener* listener)
const {
347 *listener <<
"% " << 2;
348 if (listener->stream() !=
nullptr) {
351 *listener->stream() <<
" == " << (x % 2);
357PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
358 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
361TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
363 const Matcher<int> m1 = PolymorphicIsEven();
364 EXPECT_TRUE(m1.Matches(42));
365 EXPECT_FALSE(m1.Matches(43));
366 EXPECT_EQ(
"is even", Describe(m1));
368 const Matcher<int> not_m1 = Not(m1);
369 EXPECT_EQ(
"is odd", Describe(not_m1));
371 EXPECT_EQ(
"% 2 == 0", Explain(m1, 42));
374 const Matcher<char> m2 = PolymorphicIsEven();
375 EXPECT_TRUE(m2.Matches(
'\x42'));
376 EXPECT_FALSE(m2.Matches(
'\x43'));
377 EXPECT_EQ(
"is even", Describe(m2));
379 const Matcher<char> not_m2 = Not(m2);
380 EXPECT_EQ(
"is odd", Describe(not_m2));
382 EXPECT_EQ(
"% 2 == 0", Explain(m2,
'\x42'));
385INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherCastTest);
388TEST_P(MatcherCastTestP, FromPolymorphicMatcher) {
390 if (use_gtest_matcher_) {
391 m = MatcherCast<int16_t>(GtestGreaterThan(int64_t{5}));
393 m = MatcherCast<int16_t>(Gt(int64_t{5}));
395 EXPECT_TRUE(m.Matches(6));
396 EXPECT_FALSE(m.Matches(4));
404 explicit IntValue(
int a_value) : value_(a_value) {}
406 int value()
const {
return value_; }
413bool IsPositiveIntValue(
const IntValue& foo) {
return foo.value() > 0; }
417TEST(MatcherCastTest, FromCompatibleType) {
418 Matcher<double> m1 = Eq(2.0);
419 Matcher<int> m2 = MatcherCast<int>(m1);
420 EXPECT_TRUE(m2.Matches(2));
421 EXPECT_FALSE(m2.Matches(3));
423 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
424 Matcher<int> m4 = MatcherCast<int>(m3);
428 EXPECT_TRUE(m4.Matches(1));
429 EXPECT_FALSE(m4.Matches(0));
433TEST(MatcherCastTest, FromConstReferenceToNonReference) {
434 Matcher<const int&> m1 = Eq(0);
435 Matcher<int> m2 = MatcherCast<int>(m1);
436 EXPECT_TRUE(m2.Matches(0));
437 EXPECT_FALSE(m2.Matches(1));
441TEST(MatcherCastTest, FromReferenceToNonReference) {
442 Matcher<int&> m1 = Eq(0);
443 Matcher<int> m2 = MatcherCast<int>(m1);
444 EXPECT_TRUE(m2.Matches(0));
445 EXPECT_FALSE(m2.Matches(1));
449TEST(MatcherCastTest, FromNonReferenceToConstReference) {
450 Matcher<int> m1 = Eq(0);
451 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
452 EXPECT_TRUE(m2.Matches(0));
453 EXPECT_FALSE(m2.Matches(1));
457TEST(MatcherCastTest, FromNonReferenceToReference) {
458 Matcher<int> m1 = Eq(0);
459 Matcher<int&> m2 = MatcherCast<int&>(m1);
461 EXPECT_TRUE(m2.Matches(n));
463 EXPECT_FALSE(m2.Matches(n));
467TEST(MatcherCastTest, FromSameType) {
468 Matcher<int> m1 = Eq(0);
469 Matcher<int> m2 = MatcherCast<int>(m1);
470 EXPECT_TRUE(m2.Matches(0));
471 EXPECT_FALSE(m2.Matches(1));
476TEST(MatcherCastTest, FromAValue) {
477 Matcher<int> m = MatcherCast<int>(42);
478 EXPECT_TRUE(m.Matches(42));
479 EXPECT_FALSE(m.Matches(239));
484TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
485 const int kExpected =
'c';
486 Matcher<int> m = MatcherCast<int>(
'c');
487 EXPECT_TRUE(m.Matches(kExpected));
488 EXPECT_FALSE(m.Matches(kExpected + 1));
491struct NonImplicitlyConstructibleTypeWithOperatorEq {
492 friend bool operator==(
493 const NonImplicitlyConstructibleTypeWithOperatorEq& ,
497 friend bool operator==(
499 const NonImplicitlyConstructibleTypeWithOperatorEq& ) {
507TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
508 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
509 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
510 EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
512 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
513 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
514 EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
519 MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
520 EXPECT_TRUE(m3.Matches(42));
521 EXPECT_FALSE(m3.Matches(239));
538namespace convertible_from_any {
540struct ConvertibleFromAny {
541 ConvertibleFromAny(
int a_value) : value(a_value) {}
542 template <
typename T>
543 ConvertibleFromAny(
const T& ) : value(-1) {
544 ADD_FAILURE() <<
"Conversion constructor called";
549bool operator==(
const ConvertibleFromAny& a,
const ConvertibleFromAny& b) {
550 return a.value == b.value;
553ostream& operator<<(ostream& os,
const ConvertibleFromAny& a) {
554 return os << a.value;
557TEST(MatcherCastTest, ConversionConstructorIsUsed) {
558 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
559 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
560 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
563TEST(MatcherCastTest, FromConvertibleFromAny) {
564 Matcher<ConvertibleFromAny> m =
565 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
566 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
567 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
573struct IntReferenceWrapper {
574 IntReferenceWrapper(
const int& a_value) : value(&a_value) {}
578bool operator==(
const IntReferenceWrapper& a,
const IntReferenceWrapper& b) {
579 return a.value == b.value;
582TEST(MatcherCastTest, ValueIsNotCopied) {
584 Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
586 EXPECT_TRUE(m.Matches(n));
596 Base& operator=(
const Base&) =
delete;
599class Derived :
public Base {
601 Derived() :
Base() {}
605class OtherDerived :
public Base {};
607INSTANTIATE_GTEST_MATCHER_TEST_P(SafeMatcherCastTest);
610TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) {
612 if (use_gtest_matcher_) {
613 m2 = SafeMatcherCast<char>(GtestGreaterThan(32));
615 m2 = SafeMatcherCast<char>(Gt(32));
617 EXPECT_TRUE(m2.Matches(
'A'));
618 EXPECT_FALSE(m2.Matches(
'\n'));
624TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
625 Matcher<double> m1 = DoubleEq(1.0);
626 Matcher<float> m2 = SafeMatcherCast<float>(m1);
627 EXPECT_TRUE(m2.Matches(1.0f));
628 EXPECT_FALSE(m2.Matches(2.0f));
630 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>(
'a'));
631 EXPECT_TRUE(m3.Matches(
'a'));
632 EXPECT_FALSE(m3.Matches(
'b'));
637TEST(SafeMatcherCastTest, FromBaseClass) {
639 Matcher<Base*> m1 = Eq(&d);
640 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
641 EXPECT_TRUE(m2.Matches(&d));
642 EXPECT_FALSE(m2.Matches(&d2));
644 Matcher<Base&> m3 = Ref(d);
645 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
646 EXPECT_TRUE(m4.Matches(d));
647 EXPECT_FALSE(m4.Matches(d2));
651TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
653 Matcher<const int&> m1 = Ref(n);
654 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
656 EXPECT_TRUE(m2.Matches(n));
657 EXPECT_FALSE(m2.Matches(n1));
661TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
662 Matcher<std::unique_ptr<int>> m1 = IsNull();
663 Matcher<const std::unique_ptr<int>&> m2 =
664 SafeMatcherCast<const std::unique_ptr<int>&>(m1);
665 EXPECT_TRUE(m2.Matches(std::unique_ptr<int>()));
666 EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(
new int)));
670TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
671 Matcher<int> m1 = Eq(0);
672 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
674 EXPECT_TRUE(m2.Matches(n));
676 EXPECT_FALSE(m2.Matches(n));
680TEST(SafeMatcherCastTest, FromSameType) {
681 Matcher<int> m1 = Eq(0);
682 Matcher<int> m2 = SafeMatcherCast<int>(m1);
683 EXPECT_TRUE(m2.Matches(0));
684 EXPECT_FALSE(m2.Matches(1));
689namespace convertible_from_any {
690TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
691 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
692 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
693 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
696TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
697 Matcher<ConvertibleFromAny> m =
698 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
699 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
700 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
706TEST(SafeMatcherCastTest, ValueIsNotCopied) {
708 Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);
710 EXPECT_TRUE(m.Matches(n));
713TEST(ExpectThat, TakesLiterals) {
715 EXPECT_THAT(1.0, 1.0);
716 EXPECT_THAT(std::string(),
"");
719TEST(ExpectThat, TakesFunctions) {
721 static void Func() {}
723 void (*func)() = Helper::Func;
724 EXPECT_THAT(func, Helper::Func);
725 EXPECT_THAT(func, &Helper::Func);
729TEST(ATest, MatchesAnyValue) {
731 Matcher<double> m1 = A<double>();
732 EXPECT_TRUE(m1.Matches(91.43));
733 EXPECT_TRUE(m1.Matches(-15.32));
738 Matcher<int&> m2 = A<int&>();
739 EXPECT_TRUE(m2.Matches(a));
740 EXPECT_TRUE(m2.Matches(b));
743TEST(ATest, WorksForDerivedClass) {
746 EXPECT_THAT(&base, A<Base*>());
748 EXPECT_THAT(&derived, A<Base*>());
749 EXPECT_THAT(&derived, A<Derived*>());
753TEST(ATest, CanDescribeSelf) { EXPECT_EQ(
"is anything", Describe(A<bool>())); }
756TEST(AnTest, MatchesAnyValue) {
758 Matcher<int> m1 = An<int>();
759 EXPECT_TRUE(m1.Matches(9143));
760 EXPECT_TRUE(m1.Matches(-1532));
765 Matcher<int&> m2 = An<int&>();
766 EXPECT_TRUE(m2.Matches(a));
767 EXPECT_TRUE(m2.Matches(b));
771TEST(AnTest, CanDescribeSelf) { EXPECT_EQ(
"is anything", Describe(An<int>())); }
775TEST(UnderscoreTest, MatchesAnyValue) {
778 EXPECT_TRUE(m1.Matches(123));
779 EXPECT_TRUE(m1.Matches(-242));
784 Matcher<const bool&> m2 = _;
785 EXPECT_TRUE(m2.Matches(a));
786 EXPECT_TRUE(m2.Matches(b));
790TEST(UnderscoreTest, CanDescribeSelf) {
792 EXPECT_EQ(
"is anything", Describe(m));
796TEST(EqTest, MatchesEqualValue) {
798 const char a1[] =
"hi";
799 const char a2[] =
"hi";
801 Matcher<const char*> m1 = Eq(a1);
802 EXPECT_TRUE(m1.Matches(a1));
803 EXPECT_FALSE(m1.Matches(a2));
810 Unprintable() : c_(
'a') {}
812 bool operator==(
const Unprintable& )
const {
return true; }
814 char dummy_c() {
return c_; }
820TEST(EqTest, CanDescribeSelf) {
821 Matcher<Unprintable> m = Eq(Unprintable());
822 EXPECT_EQ(
"is equal to 1-byte object <61>", Describe(m));
827TEST(EqTest, IsPolymorphic) {
828 Matcher<int> m1 = Eq(1);
829 EXPECT_TRUE(m1.Matches(1));
830 EXPECT_FALSE(m1.Matches(2));
832 Matcher<char> m2 = Eq(1);
833 EXPECT_TRUE(m2.Matches(
'\1'));
834 EXPECT_FALSE(m2.Matches(
'a'));
838TEST(TypedEqTest, ChecksEqualityForGivenType) {
839 Matcher<char> m1 = TypedEq<char>(
'a');
840 EXPECT_TRUE(m1.Matches(
'a'));
841 EXPECT_FALSE(m1.Matches(
'b'));
843 Matcher<int> m2 = TypedEq<int>(6);
844 EXPECT_TRUE(m2.Matches(6));
845 EXPECT_FALSE(m2.Matches(7));
849TEST(TypedEqTest, CanDescribeSelf) {
850 EXPECT_EQ(
"is equal to 2", Describe(TypedEq<int>(2)));
860 static bool IsTypeOf(
const T& ) {
return true; }
862 template <
typename T2>
863 static void IsTypeOf(T2 v);
866TEST(TypedEqTest, HasSpecifiedType) {
868 Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5));
869 Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5));
873TEST(GeTest, ImplementsGreaterThanOrEqual) {
874 Matcher<int> m1 = Ge(0);
875 EXPECT_TRUE(m1.Matches(1));
876 EXPECT_TRUE(m1.Matches(0));
877 EXPECT_FALSE(m1.Matches(-1));
881TEST(GeTest, CanDescribeSelf) {
882 Matcher<int> m = Ge(5);
883 EXPECT_EQ(
"is >= 5", Describe(m));
887TEST(GtTest, ImplementsGreaterThan) {
888 Matcher<double> m1 = Gt(0);
889 EXPECT_TRUE(m1.Matches(1.0));
890 EXPECT_FALSE(m1.Matches(0.0));
891 EXPECT_FALSE(m1.Matches(-1.0));
895TEST(GtTest, CanDescribeSelf) {
896 Matcher<int> m = Gt(5);
897 EXPECT_EQ(
"is > 5", Describe(m));
901TEST(LeTest, ImplementsLessThanOrEqual) {
902 Matcher<char> m1 = Le(
'b');
903 EXPECT_TRUE(m1.Matches(
'a'));
904 EXPECT_TRUE(m1.Matches(
'b'));
905 EXPECT_FALSE(m1.Matches(
'c'));
909TEST(LeTest, CanDescribeSelf) {
910 Matcher<int> m = Le(5);
911 EXPECT_EQ(
"is <= 5", Describe(m));
915TEST(LtTest, ImplementsLessThan) {
916 Matcher<const std::string&> m1 = Lt(
"Hello");
917 EXPECT_TRUE(m1.Matches(
"Abc"));
918 EXPECT_FALSE(m1.Matches(
"Hello"));
919 EXPECT_FALSE(m1.Matches(
"Hello, world!"));
923TEST(LtTest, CanDescribeSelf) {
924 Matcher<int> m = Lt(5);
925 EXPECT_EQ(
"is < 5", Describe(m));
929TEST(NeTest, ImplementsNotEqual) {
930 Matcher<int> m1 = Ne(0);
931 EXPECT_TRUE(m1.Matches(1));
932 EXPECT_TRUE(m1.Matches(-1));
933 EXPECT_FALSE(m1.Matches(0));
937TEST(NeTest, CanDescribeSelf) {
938 Matcher<int> m = Ne(5);
939 EXPECT_EQ(
"isn't equal to 5", Describe(m));
944 explicit MoveOnly(
int i) : i_(i) {}
945 MoveOnly(
const MoveOnly&) =
delete;
946 MoveOnly(MoveOnly&&) =
default;
947 MoveOnly& operator=(
const MoveOnly&) =
delete;
948 MoveOnly& operator=(MoveOnly&&) =
default;
950 bool operator==(
const MoveOnly& other)
const {
return i_ == other.i_; }
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_; }
962 MOCK_METHOD1(Call,
void(MoveOnly));
966#if defined(_MSC_VER) && (_MSC_VER < 1910)
967TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) {
969TEST(ComparisonBaseTest, WorksWithMoveOnly) {
974 EXPECT_CALL(helper, Call(Eq(ByRef(m))));
975 helper.Call(MoveOnly(0));
976 EXPECT_CALL(helper, Call(Ne(ByRef(m))));
977 helper.Call(MoveOnly(1));
978 EXPECT_CALL(helper, Call(Le(ByRef(m))));
979 helper.Call(MoveOnly(0));
980 EXPECT_CALL(helper, Call(Lt(ByRef(m))));
981 helper.Call(MoveOnly(-1));
982 EXPECT_CALL(helper, Call(Ge(ByRef(m))));
983 helper.Call(MoveOnly(0));
984 EXPECT_CALL(helper, Call(Gt(ByRef(m))));
985 helper.Call(MoveOnly(1));
988TEST(IsEmptyTest, MatchesContainer) {
989 const Matcher<std::vector<int>> m = IsEmpty();
990 std::vector<int> a = {};
991 std::vector<int> b = {1};
992 EXPECT_TRUE(m.Matches(a));
993 EXPECT_FALSE(m.Matches(b));
996TEST(IsEmptyTest, MatchesStdString) {
997 const Matcher<std::string> m = IsEmpty();
1000 EXPECT_FALSE(m.Matches(a));
1001 EXPECT_TRUE(m.Matches(b));
1004TEST(IsEmptyTest, MatchesCString) {
1005 const Matcher<const char*> m = IsEmpty();
1006 const char a[] =
"";
1007 const char b[] =
"x";
1008 EXPECT_TRUE(m.Matches(a));
1009 EXPECT_FALSE(m.Matches(b));
1013TEST(IsNullTest, MatchesNullPointer) {
1014 Matcher<int*> m1 = IsNull();
1017 EXPECT_TRUE(m1.Matches(p1));
1018 EXPECT_FALSE(m1.Matches(&n));
1020 Matcher<const char*> m2 = IsNull();
1021 const char* p2 =
nullptr;
1022 EXPECT_TRUE(m2.Matches(p2));
1023 EXPECT_FALSE(m2.Matches(
"hi"));
1025 Matcher<void*> m3 = IsNull();
1027 EXPECT_TRUE(m3.Matches(p3));
1028 EXPECT_FALSE(m3.Matches(
reinterpret_cast<void*
>(0xbeef)));
1031TEST(IsNullTest, StdFunction) {
1032 const Matcher<std::function<void()>> m = IsNull();
1034 EXPECT_TRUE(m.Matches(std::function<
void()>()));
1035 EXPECT_FALSE(m.Matches([] {}));
1039TEST(IsNullTest, CanDescribeSelf) {
1040 Matcher<int*> m = IsNull();
1041 EXPECT_EQ(
"is NULL", Describe(m));
1042 EXPECT_EQ(
"isn't NULL", DescribeNegation(m));
1046TEST(NotNullTest, MatchesNonNullPointer) {
1047 Matcher<int*> m1 = NotNull();
1050 EXPECT_FALSE(m1.Matches(p1));
1051 EXPECT_TRUE(m1.Matches(&n));
1053 Matcher<const char*> m2 = NotNull();
1054 const char* p2 =
nullptr;
1055 EXPECT_FALSE(m2.Matches(p2));
1056 EXPECT_TRUE(m2.Matches(
"hi"));
1059TEST(NotNullTest, LinkedPtr) {
1060 const Matcher<std::shared_ptr<int>> m = NotNull();
1061 const std::shared_ptr<int> null_p;
1062 const std::shared_ptr<int> non_null_p(
new int);
1064 EXPECT_FALSE(m.Matches(null_p));
1065 EXPECT_TRUE(m.Matches(non_null_p));
1068TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1069 const Matcher<const std::shared_ptr<double>&> m = NotNull();
1070 const std::shared_ptr<double> null_p;
1071 const std::shared_ptr<double> non_null_p(
new double);
1073 EXPECT_FALSE(m.Matches(null_p));
1074 EXPECT_TRUE(m.Matches(non_null_p));
1077TEST(NotNullTest, StdFunction) {
1078 const Matcher<std::function<void()>> m = NotNull();
1080 EXPECT_TRUE(m.Matches([] {}));
1081 EXPECT_FALSE(m.Matches(std::function<
void()>()));
1085TEST(NotNullTest, CanDescribeSelf) {
1086 Matcher<int*> m = NotNull();
1087 EXPECT_EQ(
"isn't NULL", Describe(m));
1092TEST(RefTest, MatchesSameVariable) {
1095 Matcher<int&> m = Ref(a);
1096 EXPECT_TRUE(m.Matches(a));
1097 EXPECT_FALSE(m.Matches(b));
1101TEST(RefTest, CanDescribeSelf) {
1103 Matcher<int&> m = Ref(n);
1105 ss <<
"references the variable @" << &n <<
" 5";
1106 EXPECT_EQ(ss.str(), Describe(m));
1111TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1114 Matcher<const int&> m = Ref(a);
1115 EXPECT_TRUE(m.Matches(a));
1116 EXPECT_FALSE(m.Matches(b));
1123TEST(RefTest, IsCovariant) {
1126 Matcher<const Base&> m1 = Ref(base);
1127 EXPECT_TRUE(m1.Matches(base));
1128 EXPECT_FALSE(m1.Matches(base2));
1129 EXPECT_FALSE(m1.Matches(derived));
1132 EXPECT_TRUE(m1.Matches(derived));
1133 EXPECT_FALSE(m1.Matches(base));
1134 EXPECT_FALSE(m1.Matches(base2));
1137TEST(RefTest, ExplainsResult) {
1139 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1140 StartsWith(
"which is located @"));
1143 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1144 StartsWith(
"which is located @"));
1149template <
typename T = std::
string>
1150std::string FromStringLike(internal::StringLike<T> str) {
1151 return std::string(str);
1154TEST(StringLike, TestConversions) {
1155 EXPECT_EQ(
"foo", FromStringLike(
"foo"));
1156 EXPECT_EQ(
"foo", FromStringLike(std::string(
"foo")));
1157#if GTEST_INTERNAL_HAS_STRING_VIEW
1158 EXPECT_EQ(
"foo", FromStringLike(internal::StringView(
"foo")));
1162 EXPECT_EQ(
"", FromStringLike({}));
1163 EXPECT_EQ(
"foo", FromStringLike({
'f',
'o',
'o'}));
1164 const char buf[] =
"foo";
1165 EXPECT_EQ(
"foo", FromStringLike({buf, buf + 3}));
1168TEST(StrEqTest, MatchesEqualString) {
1169 Matcher<const char*> m = StrEq(std::string(
"Hello"));
1170 EXPECT_TRUE(m.Matches(
"Hello"));
1171 EXPECT_FALSE(m.Matches(
"hello"));
1172 EXPECT_FALSE(m.Matches(
nullptr));
1174 Matcher<const std::string&> m2 = StrEq(
"Hello");
1175 EXPECT_TRUE(m2.Matches(
"Hello"));
1176 EXPECT_FALSE(m2.Matches(
"Hi"));
1178#if GTEST_INTERNAL_HAS_STRING_VIEW
1179 Matcher<const internal::StringView&> m3 =
1180 StrEq(internal::StringView(
"Hello"));
1181 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1182 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1183 EXPECT_FALSE(m3.Matches(internal::StringView()));
1185 Matcher<const internal::StringView&> m_empty = StrEq(
"");
1186 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1187 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1188 EXPECT_FALSE(m_empty.Matches(internal::StringView(
"hello")));
1192TEST(StrEqTest, CanDescribeSelf) {
1193 Matcher<std::string> m = StrEq(
"Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1194 EXPECT_EQ(
"is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1197 std::string str(
"01204500800");
1199 Matcher<std::string> m2 = StrEq(str);
1200 EXPECT_EQ(
"is equal to \"012\\04500800\"", Describe(m2));
1201 str[0] = str[6] = str[7] = str[9] = str[10] =
'\0';
1202 Matcher<std::string> m3 = StrEq(str);
1203 EXPECT_EQ(
"is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1206TEST(StrNeTest, MatchesUnequalString) {
1207 Matcher<const char*> m = StrNe(
"Hello");
1208 EXPECT_TRUE(m.Matches(
""));
1209 EXPECT_TRUE(m.Matches(
nullptr));
1210 EXPECT_FALSE(m.Matches(
"Hello"));
1212 Matcher<std::string> m2 = StrNe(std::string(
"Hello"));
1213 EXPECT_TRUE(m2.Matches(
"hello"));
1214 EXPECT_FALSE(m2.Matches(
"Hello"));
1216#if GTEST_INTERNAL_HAS_STRING_VIEW
1217 Matcher<const internal::StringView> m3 = StrNe(internal::StringView(
"Hello"));
1218 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1219 EXPECT_TRUE(m3.Matches(internal::StringView()));
1220 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1224TEST(StrNeTest, CanDescribeSelf) {
1225 Matcher<const char*> m = StrNe(
"Hi");
1226 EXPECT_EQ(
"isn't equal to \"Hi\"", Describe(m));
1229TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1230 Matcher<const char*> m = StrCaseEq(std::string(
"Hello"));
1231 EXPECT_TRUE(m.Matches(
"Hello"));
1232 EXPECT_TRUE(m.Matches(
"hello"));
1233 EXPECT_FALSE(m.Matches(
"Hi"));
1234 EXPECT_FALSE(m.Matches(
nullptr));
1236 Matcher<const std::string&> m2 = StrCaseEq(
"Hello");
1237 EXPECT_TRUE(m2.Matches(
"hello"));
1238 EXPECT_FALSE(m2.Matches(
"Hi"));
1240#if GTEST_INTERNAL_HAS_STRING_VIEW
1241 Matcher<const internal::StringView&> m3 =
1242 StrCaseEq(internal::StringView(
"Hello"));
1243 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1244 EXPECT_TRUE(m3.Matches(internal::StringView(
"hello")));
1245 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hi")));
1246 EXPECT_FALSE(m3.Matches(internal::StringView()));
1250TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1251 std::string str1(
"oabocdooeoo");
1252 std::string str2(
"OABOCDOOEOO");
1253 Matcher<const std::string&> m0 = StrCaseEq(str1);
1254 EXPECT_FALSE(m0.Matches(str2 + std::string(1,
'\0')));
1256 str1[3] = str2[3] =
'\0';
1257 Matcher<const std::string&> m1 = StrCaseEq(str1);
1258 EXPECT_TRUE(m1.Matches(str2));
1260 str1[0] = str1[6] = str1[7] = str1[10] =
'\0';
1261 str2[0] = str2[6] = str2[7] = str2[10] =
'\0';
1262 Matcher<const std::string&> m2 = StrCaseEq(str1);
1263 str1[9] = str2[9] =
'\0';
1264 EXPECT_FALSE(m2.Matches(str2));
1266 Matcher<const std::string&> m3 = StrCaseEq(str1);
1267 EXPECT_TRUE(m3.Matches(str2));
1269 EXPECT_FALSE(m3.Matches(str2 +
"x"));
1270 str2.append(1,
'\0');
1271 EXPECT_FALSE(m3.Matches(str2));
1272 EXPECT_FALSE(m3.Matches(std::string(str2, 0, 9)));
1275TEST(StrCaseEqTest, CanDescribeSelf) {
1276 Matcher<std::string> m = StrCaseEq(
"Hi");
1277 EXPECT_EQ(
"is equal to (ignoring case) \"Hi\"", Describe(m));
1280TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1281 Matcher<const char*> m = StrCaseNe(
"Hello");
1282 EXPECT_TRUE(m.Matches(
"Hi"));
1283 EXPECT_TRUE(m.Matches(
nullptr));
1284 EXPECT_FALSE(m.Matches(
"Hello"));
1285 EXPECT_FALSE(m.Matches(
"hello"));
1287 Matcher<std::string> m2 = StrCaseNe(std::string(
"Hello"));
1288 EXPECT_TRUE(m2.Matches(
""));
1289 EXPECT_FALSE(m2.Matches(
"Hello"));
1291#if GTEST_INTERNAL_HAS_STRING_VIEW
1292 Matcher<const internal::StringView> m3 =
1293 StrCaseNe(internal::StringView(
"Hello"));
1294 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hi")));
1295 EXPECT_TRUE(m3.Matches(internal::StringView()));
1296 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1297 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1301TEST(StrCaseNeTest, CanDescribeSelf) {
1302 Matcher<const char*> m = StrCaseNe(
"Hi");
1303 EXPECT_EQ(
"isn't equal to (ignoring case) \"Hi\"", Describe(m));
1307TEST(HasSubstrTest, WorksForStringClasses) {
1308 const Matcher<std::string> m1 = HasSubstr(
"foo");
1309 EXPECT_TRUE(m1.Matches(std::string(
"I love food.")));
1310 EXPECT_FALSE(m1.Matches(std::string(
"tofo")));
1312 const Matcher<const std::string&> m2 = HasSubstr(
"foo");
1313 EXPECT_TRUE(m2.Matches(std::string(
"I love food.")));
1314 EXPECT_FALSE(m2.Matches(std::string(
"tofo")));
1316 const Matcher<std::string> m_empty = HasSubstr(
"");
1317 EXPECT_TRUE(m_empty.Matches(std::string()));
1318 EXPECT_TRUE(m_empty.Matches(std::string(
"not empty")));
1322TEST(HasSubstrTest, WorksForCStrings) {
1323 const Matcher<char*> m1 = HasSubstr(
"foo");
1324 EXPECT_TRUE(m1.Matches(
const_cast<char*
>(
"I love food.")));
1325 EXPECT_FALSE(m1.Matches(
const_cast<char*
>(
"tofo")));
1326 EXPECT_FALSE(m1.Matches(
nullptr));
1328 const Matcher<const char*> m2 = HasSubstr(
"foo");
1329 EXPECT_TRUE(m2.Matches(
"I love food."));
1330 EXPECT_FALSE(m2.Matches(
"tofo"));
1331 EXPECT_FALSE(m2.Matches(
nullptr));
1333 const Matcher<const char*> m_empty = HasSubstr(
"");
1334 EXPECT_TRUE(m_empty.Matches(
"not empty"));
1335 EXPECT_TRUE(m_empty.Matches(
""));
1336 EXPECT_FALSE(m_empty.Matches(
nullptr));
1339#if GTEST_INTERNAL_HAS_STRING_VIEW
1341TEST(HasSubstrTest, WorksForStringViewClasses) {
1342 const Matcher<internal::StringView> m1 =
1343 HasSubstr(internal::StringView(
"foo"));
1344 EXPECT_TRUE(m1.Matches(internal::StringView(
"I love food.")));
1345 EXPECT_FALSE(m1.Matches(internal::StringView(
"tofo")));
1346 EXPECT_FALSE(m1.Matches(internal::StringView()));
1348 const Matcher<const internal::StringView&> m2 = HasSubstr(
"foo");
1349 EXPECT_TRUE(m2.Matches(internal::StringView(
"I love food.")));
1350 EXPECT_FALSE(m2.Matches(internal::StringView(
"tofo")));
1351 EXPECT_FALSE(m2.Matches(internal::StringView()));
1353 const Matcher<const internal::StringView&> m3 = HasSubstr(
"");
1354 EXPECT_TRUE(m3.Matches(internal::StringView(
"foo")));
1355 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1356 EXPECT_TRUE(m3.Matches(internal::StringView()));
1361TEST(HasSubstrTest, CanDescribeSelf) {
1362 Matcher<std::string> m = HasSubstr(
"foo\n\"");
1363 EXPECT_EQ(
"has substring \"foo\\n\\\"\"", Describe(m));
1366INSTANTIATE_GTEST_MATCHER_TEST_P(KeyTest);
1368TEST(KeyTest, CanDescribeSelf) {
1369 Matcher<const pair<std::string, int>&> m = Key(
"foo");
1370 EXPECT_EQ(
"has a key that is equal to \"foo\"", Describe(m));
1371 EXPECT_EQ(
"doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1374TEST_P(KeyTestP, ExplainsResult) {
1375 Matcher<pair<int, bool>> m = Key(GreaterThan(10));
1376 EXPECT_EQ(
"whose first field is a value which is 5 less than 10",
1377 Explain(m, make_pair(5,
true)));
1378 EXPECT_EQ(
"whose first field is a value which is 5 more than 10",
1379 Explain(m, make_pair(15,
true)));
1382TEST(KeyTest, MatchesCorrectly) {
1383 pair<int, std::string> p(25,
"foo");
1384 EXPECT_THAT(p, Key(25));
1385 EXPECT_THAT(p, Not(Key(42)));
1386 EXPECT_THAT(p, Key(Ge(20)));
1387 EXPECT_THAT(p, Not(Key(Lt(25))));
1390TEST(KeyTest, WorksWithMoveOnly) {
1391 pair<std::unique_ptr<int>, std::unique_ptr<int>> p;
1392 EXPECT_THAT(p, Key(Eq(
nullptr)));
1395INSTANTIATE_GTEST_MATCHER_TEST_P(PairTest);
1402 std::string member_2;
1403 using first_type = int;
1404 using second_type = std::string;
1406 const int& GetImpl(Tag<0>)
const {
return member_1; }
1407 const std::string& GetImpl(Tag<1>)
const {
return member_2; }
1410auto get(
const PairWithGet& value) ->
decltype(value.GetImpl(Tag<I>())) {
1411 return value.GetImpl(Tag<I>());
1413TEST(PairTest, MatchesPairWithGetCorrectly) {
1414 PairWithGet p{25,
"foo"};
1415 EXPECT_THAT(p, Key(25));
1416 EXPECT_THAT(p, Not(Key(42)));
1417 EXPECT_THAT(p, Key(Ge(20)));
1418 EXPECT_THAT(p, Not(Key(Lt(25))));
1420 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1421 EXPECT_THAT(v, Contains(Key(29)));
1424TEST(KeyTest, SafelyCastsInnerMatcher) {
1425 Matcher<int> is_positive = Gt(0);
1426 Matcher<int> is_negative = Lt(0);
1427 pair<char, bool> p(
'a',
true);
1428 EXPECT_THAT(p, Key(is_positive));
1429 EXPECT_THAT(p, Not(Key(is_negative)));
1432TEST(KeyTest, InsideContainsUsingMap) {
1433 map<int, char> container;
1434 container.insert(make_pair(1,
'a'));
1435 container.insert(make_pair(2,
'b'));
1436 container.insert(make_pair(4,
'c'));
1437 EXPECT_THAT(container, Contains(Key(1)));
1438 EXPECT_THAT(container, Not(Contains(Key(3))));
1441TEST(KeyTest, InsideContainsUsingMultimap) {
1442 multimap<int, char> container;
1443 container.insert(make_pair(1,
'a'));
1444 container.insert(make_pair(2,
'b'));
1445 container.insert(make_pair(4,
'c'));
1447 EXPECT_THAT(container, Not(Contains(Key(25))));
1448 container.insert(make_pair(25,
'd'));
1449 EXPECT_THAT(container, Contains(Key(25)));
1450 container.insert(make_pair(25,
'e'));
1451 EXPECT_THAT(container, Contains(Key(25)));
1453 EXPECT_THAT(container, Contains(Key(1)));
1454 EXPECT_THAT(container, Not(Contains(Key(3))));
1457TEST(PairTest, Typing) {
1459 Matcher<const pair<const char*, int>&> m1 = Pair(
"foo", 42);
1460 Matcher<const pair<const char*, int>> m2 = Pair(
"foo", 42);
1461 Matcher<pair<const char*, int>> m3 = Pair(
"foo", 42);
1463 Matcher<pair<int, const std::string>> m4 = Pair(25,
"42");
1464 Matcher<pair<const std::string, int>> m5 = Pair(
"25", 42);
1467TEST(PairTest, CanDescribeSelf) {
1468 Matcher<const pair<std::string, int>&> m1 = Pair(
"foo", 42);
1470 "has a first field that is equal to \"foo\""
1471 ", and has a second field that is equal to 42",
1474 "has a first field that isn't equal to \"foo\""
1475 ", or has a second field that isn't equal to 42",
1476 DescribeNegation(m1));
1478 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1480 "has a first field that isn't equal to 13"
1481 ", and has a second field that is equal to 42",
1482 DescribeNegation(m2));
1485TEST_P(PairTestP, CanExplainMatchResultTo) {
1488 const Matcher<pair<int, int>> m = Pair(GreaterThan(0), GreaterThan(0));
1489 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1490 Explain(m, make_pair(-1, -2)));
1494 EXPECT_EQ(
"whose second field does not match, which is 2 less than 0",
1495 Explain(m, make_pair(1, -2)));
1499 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1500 Explain(m, make_pair(-1, 2)));
1504 "whose both fields match, where the first field is a value "
1505 "which is 1 more than 0, and the second field is a value "
1506 "which is 2 more than 0",
1507 Explain(m, make_pair(1, 2)));
1511 const Matcher<pair<int, int>> explain_first = Pair(GreaterThan(0), 0);
1513 "whose both fields match, where the first field is a value "
1514 "which is 1 more than 0",
1515 Explain(explain_first, make_pair(1, 0)));
1519 const Matcher<pair<int, int>> explain_second = Pair(0, GreaterThan(0));
1521 "whose both fields match, where the second field is a value "
1522 "which is 1 more than 0",
1523 Explain(explain_second, make_pair(0, 1)));
1526TEST(PairTest, MatchesCorrectly) {
1527 pair<int, std::string> p(25,
"foo");
1530 EXPECT_THAT(p, Pair(25,
"foo"));
1531 EXPECT_THAT(p, Pair(Ge(20), HasSubstr(
"o")));
1534 EXPECT_THAT(p, Not(Pair(42,
"foo")));
1535 EXPECT_THAT(p, Not(Pair(Lt(25),
"foo")));
1538 EXPECT_THAT(p, Not(Pair(25,
"bar")));
1539 EXPECT_THAT(p, Not(Pair(25, Not(
"foo"))));
1542 EXPECT_THAT(p, Not(Pair(13,
"bar")));
1543 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr(
"a"))));
1546TEST(PairTest, WorksWithMoveOnly) {
1547 pair<std::unique_ptr<int>, std::unique_ptr<int>> p;
1548 p.second.reset(
new int(7));
1549 EXPECT_THAT(p, Pair(Eq(
nullptr), Ne(
nullptr)));
1552TEST(PairTest, SafelyCastsInnerMatchers) {
1553 Matcher<int> is_positive = Gt(0);
1554 Matcher<int> is_negative = Lt(0);
1555 pair<char, bool> p(
'a',
true);
1556 EXPECT_THAT(p, Pair(is_positive, _));
1557 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1558 EXPECT_THAT(p, Pair(_, is_positive));
1559 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1562TEST(PairTest, InsideContainsUsingMap) {
1563 map<int, char> container;
1564 container.insert(make_pair(1,
'a'));
1565 container.insert(make_pair(2,
'b'));
1566 container.insert(make_pair(4,
'c'));
1567 EXPECT_THAT(container, Contains(Pair(1,
'a')));
1568 EXPECT_THAT(container, Contains(Pair(1, _)));
1569 EXPECT_THAT(container, Contains(Pair(_,
'a')));
1570 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1573INSTANTIATE_GTEST_MATCHER_TEST_P(FieldsAreTest);
1575TEST(FieldsAreTest, MatchesCorrectly) {
1576 std::tuple<int, std::string, double> p(25,
"foo", .5);
1579 EXPECT_THAT(p, FieldsAre(25,
"foo", .5));
1580 EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr(
"o"), DoubleEq(.5)));
1583 EXPECT_THAT(p, Not(FieldsAre(26,
"foo", .5)));
1584 EXPECT_THAT(p, Not(FieldsAre(25,
"fo", .5)));
1585 EXPECT_THAT(p, Not(FieldsAre(25,
"foo", .6)));
1588TEST(FieldsAreTest, CanDescribeSelf) {
1589 Matcher<const pair<std::string, int>&> m1 = FieldsAre(
"foo", 42);
1591 "has field #0 that is equal to \"foo\""
1592 ", and has field #1 that is equal to 42",
1595 "has field #0 that isn't equal to \"foo\""
1596 ", or has field #1 that isn't equal to 42",
1597 DescribeNegation(m1));
1600TEST_P(FieldsAreTestP, CanExplainMatchResultTo) {
1602 Matcher<std::tuple<int, int, int>> m =
1603 FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));
1605 EXPECT_EQ(
"whose field #0 does not match, which is 1 less than 0",
1606 Explain(m, std::make_tuple(-1, -2, -3)));
1607 EXPECT_EQ(
"whose field #1 does not match, which is 2 less than 0",
1608 Explain(m, std::make_tuple(1, -2, -3)));
1609 EXPECT_EQ(
"whose field #2 does not match, which is 3 less than 0",
1610 Explain(m, std::make_tuple(1, 2, -3)));
1614 "whose all elements match, "
1615 "where field #0 is a value which is 1 more than 0"
1616 ", and field #1 is a value which is 2 more than 0"
1617 ", and field #2 is a value which is 3 more than 0",
1618 Explain(m, std::make_tuple(1, 2, 3)));
1621 m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));
1623 "whose all elements match, "
1624 "where field #0 is a value which is 1 more than 0"
1625 ", and field #2 is a value which is 3 more than 0",
1626 Explain(m, std::make_tuple(1, 0, 3)));
1629 m = FieldsAre(0, GreaterThan(0), 0);
1631 "whose all elements match, "
1632 "where field #1 is a value which is 1 more than 0",
1633 Explain(m, std::make_tuple(0, 1, 0)));
1636#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
1637TEST(FieldsAreTest, StructuredBindings) {
1643 EXPECT_THAT((
MyType{17,
"foo"}), FieldsAre(Eq(17), HasSubstr(
"oo")));
1649 EXPECT_THAT(MyVarType1{}, FieldsAre(0));
1653 EXPECT_THAT(MyVarType2{}, FieldsAre(0, 0));
1657 EXPECT_THAT(MyVarType3{}, FieldsAre(0, 0, 0));
1661 EXPECT_THAT(MyVarType4{}, FieldsAre(0, 0, 0, 0));
1665 EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));
1667 int a, b, c, d, e, f;
1669 EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));
1671 int a, b, c, d, e, f, g;
1673 EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));
1675 int a, b, c, d, e, f, g, h;
1677 EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));
1679 int a, b, c, d, e, f, g, h, i;
1681 EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));
1682 struct MyVarType10 {
1683 int a, b, c, d, e, f, g, h, i, j;
1685 EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1686 struct MyVarType11 {
1687 int a, b, c, d, e, f, g, h, i, j, k;
1689 EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1690 struct MyVarType12 {
1691 int a, b, c, d, e, f, g, h, i, j, k, l;
1693 EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1694 struct MyVarType13 {
1695 int a, b, c, d, e, f, g, h, i, j, k, l, m;
1697 EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1698 struct MyVarType14 {
1699 int a, b, c, d, e, f, g, h, i, j, k, l, m, n;
1701 EXPECT_THAT(MyVarType14{},
1702 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1703 struct MyVarType15 {
1704 int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;
1706 EXPECT_THAT(MyVarType15{},
1707 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1708 struct MyVarType16 {
1709 int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
1711 EXPECT_THAT(MyVarType16{},
1712 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1713 struct MyVarType17 {
1714 int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
1716 EXPECT_THAT(MyVarType17{},
1717 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1718 struct MyVarType18 {
1719 int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r;
1721 EXPECT_THAT(MyVarType18{},
1722 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1723 struct MyVarType19 {
1724 int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s;
1726 EXPECT_THAT(MyVarType19{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1731TEST(PairTest, UseGetInsteadOfMembers) {
1732 PairWithGet pair{7,
"ABC"};
1733 EXPECT_THAT(pair, Pair(7,
"ABC"));
1734 EXPECT_THAT(pair, Pair(Ge(7), HasSubstr(
"AB")));
1735 EXPECT_THAT(pair, Not(Pair(Lt(7),
"ABC")));
1737 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1739 ElementsAre(Pair(11, std::string(
"Foo")), Pair(Ge(10), Not(
""))));
1744TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1745 const Matcher<const char*> m1 = StartsWith(std::string(
""));
1746 EXPECT_TRUE(m1.Matches(
"Hi"));
1747 EXPECT_TRUE(m1.Matches(
""));
1748 EXPECT_FALSE(m1.Matches(
nullptr));
1750 const Matcher<const std::string&> m2 = StartsWith(
"Hi");
1751 EXPECT_TRUE(m2.Matches(
"Hi"));
1752 EXPECT_TRUE(m2.Matches(
"Hi Hi!"));
1753 EXPECT_TRUE(m2.Matches(
"High"));
1754 EXPECT_FALSE(m2.Matches(
"H"));
1755 EXPECT_FALSE(m2.Matches(
" Hi"));
1757#if GTEST_INTERNAL_HAS_STRING_VIEW
1758 const Matcher<internal::StringView> m_empty =
1759 StartsWith(internal::StringView(
""));
1760 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1761 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1762 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"not empty")));
1766TEST(StartsWithTest, CanDescribeSelf) {
1767 Matcher<const std::string> m = StartsWith(
"Hi");
1768 EXPECT_EQ(
"starts with \"Hi\"", Describe(m));
1773TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1774 const Matcher<const char*> m1 = EndsWith(
"");
1775 EXPECT_TRUE(m1.Matches(
"Hi"));
1776 EXPECT_TRUE(m1.Matches(
""));
1777 EXPECT_FALSE(m1.Matches(
nullptr));
1779 const Matcher<const std::string&> m2 = EndsWith(std::string(
"Hi"));
1780 EXPECT_TRUE(m2.Matches(
"Hi"));
1781 EXPECT_TRUE(m2.Matches(
"Wow Hi Hi"));
1782 EXPECT_TRUE(m2.Matches(
"Super Hi"));
1783 EXPECT_FALSE(m2.Matches(
"i"));
1784 EXPECT_FALSE(m2.Matches(
"Hi "));
1786#if GTEST_INTERNAL_HAS_STRING_VIEW
1787 const Matcher<const internal::StringView&> m4 =
1788 EndsWith(internal::StringView(
""));
1789 EXPECT_TRUE(m4.Matches(
"Hi"));
1790 EXPECT_TRUE(m4.Matches(
""));
1791 EXPECT_TRUE(m4.Matches(internal::StringView()));
1792 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1796TEST(EndsWithTest, CanDescribeSelf) {
1797 Matcher<const std::string> m = EndsWith(
"Hi");
1798 EXPECT_EQ(
"ends with \"Hi\"", Describe(m));
1803TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
1804 const Matcher<const char*> m1 = WhenBase64Unescaped(EndsWith(
"!"));
1805 EXPECT_FALSE(m1.Matches(
"invalid base64"));
1806 EXPECT_FALSE(m1.Matches(
"aGVsbG8gd29ybGQ="));
1807 EXPECT_TRUE(m1.Matches(
"aGVsbG8gd29ybGQh"));
1809 const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith(
"!"));
1810 EXPECT_FALSE(m2.Matches(
"invalid base64"));
1811 EXPECT_FALSE(m2.Matches(
"aGVsbG8gd29ybGQ="));
1812 EXPECT_TRUE(m2.Matches(
"aGVsbG8gd29ybGQh"));
1814#if GTEST_INTERNAL_HAS_STRING_VIEW
1815 const Matcher<const internal::StringView&> m3 =
1816 WhenBase64Unescaped(EndsWith(
"!"));
1817 EXPECT_FALSE(m3.Matches(
"invalid base64"));
1818 EXPECT_FALSE(m3.Matches(
"aGVsbG8gd29ybGQ="));
1819 EXPECT_TRUE(m3.Matches(
"aGVsbG8gd29ybGQh"));
1823TEST(WhenBase64UnescapedTest, CanDescribeSelf) {
1824 const Matcher<const char*> m = WhenBase64Unescaped(EndsWith(
"!"));
1825 EXPECT_EQ(
"matches after Base64Unescape ends with \"!\"", Describe(m));
1830TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1831 const Matcher<const char*> m1 = MatchesRegex(
"a.*z");
1832 EXPECT_TRUE(m1.Matches(
"az"));
1833 EXPECT_TRUE(m1.Matches(
"abcz"));
1834 EXPECT_FALSE(m1.Matches(
nullptr));
1836 const Matcher<const std::string&> m2 = MatchesRegex(
new RE(
"a.*z"));
1837 EXPECT_TRUE(m2.Matches(
"azbz"));
1838 EXPECT_FALSE(m2.Matches(
"az1"));
1839 EXPECT_FALSE(m2.Matches(
"1az"));
1841#if GTEST_INTERNAL_HAS_STRING_VIEW
1842 const Matcher<const internal::StringView&> m3 = MatchesRegex(
"a.*z");
1843 EXPECT_TRUE(m3.Matches(internal::StringView(
"az")));
1844 EXPECT_TRUE(m3.Matches(internal::StringView(
"abcz")));
1845 EXPECT_FALSE(m3.Matches(internal::StringView(
"1az")));
1846 EXPECT_FALSE(m3.Matches(internal::StringView()));
1847 const Matcher<const internal::StringView&> m4 =
1848 MatchesRegex(internal::StringView(
""));
1849 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1850 EXPECT_TRUE(m4.Matches(internal::StringView()));
1854TEST(MatchesRegexTest, CanDescribeSelf) {
1855 Matcher<const std::string> m1 = MatchesRegex(std::string(
"Hi.*"));
1856 EXPECT_EQ(
"matches regular expression \"Hi.*\"", Describe(m1));
1858 Matcher<const char*> m2 = MatchesRegex(
new RE(
"a.*"));
1859 EXPECT_EQ(
"matches regular expression \"a.*\"", Describe(m2));
1861#if GTEST_INTERNAL_HAS_STRING_VIEW
1862 Matcher<const internal::StringView> m3 = MatchesRegex(
new RE(
"0.*"));
1863 EXPECT_EQ(
"matches regular expression \"0.*\"", Describe(m3));
1869TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1870 const Matcher<const char*> m1 = ContainsRegex(std::string(
"a.*z"));
1871 EXPECT_TRUE(m1.Matches(
"az"));
1872 EXPECT_TRUE(m1.Matches(
"0abcz1"));
1873 EXPECT_FALSE(m1.Matches(
nullptr));
1875 const Matcher<const std::string&> m2 = ContainsRegex(
new RE(
"a.*z"));
1876 EXPECT_TRUE(m2.Matches(
"azbz"));
1877 EXPECT_TRUE(m2.Matches(
"az1"));
1878 EXPECT_FALSE(m2.Matches(
"1a"));
1880#if GTEST_INTERNAL_HAS_STRING_VIEW
1881 const Matcher<const internal::StringView&> m3 = ContainsRegex(
new RE(
"a.*z"));
1882 EXPECT_TRUE(m3.Matches(internal::StringView(
"azbz")));
1883 EXPECT_TRUE(m3.Matches(internal::StringView(
"az1")));
1884 EXPECT_FALSE(m3.Matches(internal::StringView(
"1a")));
1885 EXPECT_FALSE(m3.Matches(internal::StringView()));
1886 const Matcher<const internal::StringView&> m4 =
1887 ContainsRegex(internal::StringView(
""));
1888 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1889 EXPECT_TRUE(m4.Matches(internal::StringView()));
1893TEST(ContainsRegexTest, CanDescribeSelf) {
1894 Matcher<const std::string> m1 = ContainsRegex(
"Hi.*");
1895 EXPECT_EQ(
"contains regular expression \"Hi.*\"", Describe(m1));
1897 Matcher<const char*> m2 = ContainsRegex(
new RE(
"a.*"));
1898 EXPECT_EQ(
"contains regular expression \"a.*\"", Describe(m2));
1900#if GTEST_INTERNAL_HAS_STRING_VIEW
1901 Matcher<const internal::StringView> m3 = ContainsRegex(
new RE(
"0.*"));
1902 EXPECT_EQ(
"contains regular expression \"0.*\"", Describe(m3));
1907#if GTEST_HAS_STD_WSTRING
1908TEST(StdWideStrEqTest, MatchesEqual) {
1909 Matcher<const wchar_t*> m = StrEq(::std::wstring(L
"Hello"));
1910 EXPECT_TRUE(m.Matches(L
"Hello"));
1911 EXPECT_FALSE(m.Matches(L
"hello"));
1912 EXPECT_FALSE(m.Matches(
nullptr));
1914 Matcher<const ::std::wstring&> m2 = StrEq(L
"Hello");
1915 EXPECT_TRUE(m2.Matches(L
"Hello"));
1916 EXPECT_FALSE(m2.Matches(L
"Hi"));
1918 Matcher<const ::std::wstring&> m3 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1919 EXPECT_TRUE(m3.Matches(L
"\xD3\x576\x8D3\xC74D"));
1920 EXPECT_FALSE(m3.Matches(L
"\xD3\x576\x8D3\xC74E"));
1922 ::std::wstring str(L
"01204500800");
1924 Matcher<const ::std::wstring&> m4 = StrEq(str);
1925 EXPECT_TRUE(m4.Matches(str));
1926 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
1927 Matcher<const ::std::wstring&> m5 = StrEq(str);
1928 EXPECT_TRUE(m5.Matches(str));
1931TEST(StdWideStrEqTest, CanDescribeSelf) {
1932 Matcher<::std::wstring> m = StrEq(L
"Hi-\'\"?\\\a\b\f\n\r\t\v");
1933 EXPECT_EQ(
"is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1936 Matcher<::std::wstring> m2 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1937 EXPECT_EQ(
"is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", Describe(m2));
1939 ::std::wstring str(L
"01204500800");
1941 Matcher<const ::std::wstring&> m4 = StrEq(str);
1942 EXPECT_EQ(
"is equal to L\"012\\04500800\"", Describe(m4));
1943 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
1944 Matcher<const ::std::wstring&> m5 = StrEq(str);
1945 EXPECT_EQ(
"is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1948TEST(StdWideStrNeTest, MatchesUnequalString) {
1949 Matcher<const wchar_t*> m = StrNe(L
"Hello");
1950 EXPECT_TRUE(m.Matches(L
""));
1951 EXPECT_TRUE(m.Matches(
nullptr));
1952 EXPECT_FALSE(m.Matches(L
"Hello"));
1954 Matcher<::std::wstring> m2 = StrNe(::std::wstring(L
"Hello"));
1955 EXPECT_TRUE(m2.Matches(L
"hello"));
1956 EXPECT_FALSE(m2.Matches(L
"Hello"));
1959TEST(StdWideStrNeTest, CanDescribeSelf) {
1960 Matcher<const wchar_t*> m = StrNe(L
"Hi");
1961 EXPECT_EQ(
"isn't equal to L\"Hi\"", Describe(m));
1964TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1965 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L
"Hello"));
1966 EXPECT_TRUE(m.Matches(L
"Hello"));
1967 EXPECT_TRUE(m.Matches(L
"hello"));
1968 EXPECT_FALSE(m.Matches(L
"Hi"));
1969 EXPECT_FALSE(m.Matches(
nullptr));
1971 Matcher<const ::std::wstring&> m2 = StrCaseEq(L
"Hello");
1972 EXPECT_TRUE(m2.Matches(L
"hello"));
1973 EXPECT_FALSE(m2.Matches(L
"Hi"));
1976TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1977 ::std::wstring str1(L
"oabocdooeoo");
1978 ::std::wstring str2(L
"OABOCDOOEOO");
1979 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1980 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L
'\0')));
1982 str1[3] = str2[3] = L
'\0';
1983 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1984 EXPECT_TRUE(m1.Matches(str2));
1986 str1[0] = str1[6] = str1[7] = str1[10] = L
'\0';
1987 str2[0] = str2[6] = str2[7] = str2[10] = L
'\0';
1988 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1989 str1[9] = str2[9] = L
'\0';
1990 EXPECT_FALSE(m2.Matches(str2));
1992 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1993 EXPECT_TRUE(m3.Matches(str2));
1995 EXPECT_FALSE(m3.Matches(str2 + L
"x"));
1996 str2.append(1, L
'\0');
1997 EXPECT_FALSE(m3.Matches(str2));
1998 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
2001TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
2002 Matcher<::std::wstring> m = StrCaseEq(L
"Hi");
2003 EXPECT_EQ(
"is equal to (ignoring case) L\"Hi\"", Describe(m));
2006TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
2007 Matcher<const wchar_t*> m = StrCaseNe(L
"Hello");
2008 EXPECT_TRUE(m.Matches(L
"Hi"));
2009 EXPECT_TRUE(m.Matches(
nullptr));
2010 EXPECT_FALSE(m.Matches(L
"Hello"));
2011 EXPECT_FALSE(m.Matches(L
"hello"));
2013 Matcher<::std::wstring> m2 = StrCaseNe(::std::wstring(L
"Hello"));
2014 EXPECT_TRUE(m2.Matches(L
""));
2015 EXPECT_FALSE(m2.Matches(L
"Hello"));
2018TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
2019 Matcher<const wchar_t*> m = StrCaseNe(L
"Hi");
2020 EXPECT_EQ(
"isn't equal to (ignoring case) L\"Hi\"", Describe(m));
2024TEST(StdWideHasSubstrTest, WorksForStringClasses) {
2025 const Matcher<::std::wstring> m1 = HasSubstr(L
"foo");
2026 EXPECT_TRUE(m1.Matches(::std::wstring(L
"I love food.")));
2027 EXPECT_FALSE(m1.Matches(::std::wstring(L
"tofo")));
2029 const Matcher<const ::std::wstring&> m2 = HasSubstr(L
"foo");
2030 EXPECT_TRUE(m2.Matches(::std::wstring(L
"I love food.")));
2031 EXPECT_FALSE(m2.Matches(::std::wstring(L
"tofo")));
2035TEST(StdWideHasSubstrTest, WorksForCStrings) {
2036 const Matcher<wchar_t*> m1 = HasSubstr(L
"foo");
2037 EXPECT_TRUE(m1.Matches(
const_cast<wchar_t*
>(L
"I love food.")));
2038 EXPECT_FALSE(m1.Matches(
const_cast<wchar_t*
>(L
"tofo")));
2039 EXPECT_FALSE(m1.Matches(
nullptr));
2041 const Matcher<const wchar_t*> m2 = HasSubstr(L
"foo");
2042 EXPECT_TRUE(m2.Matches(L
"I love food."));
2043 EXPECT_FALSE(m2.Matches(L
"tofo"));
2044 EXPECT_FALSE(m2.Matches(
nullptr));
2048TEST(StdWideHasSubstrTest, CanDescribeSelf) {
2049 Matcher<::std::wstring> m = HasSubstr(L
"foo\n\"");
2050 EXPECT_EQ(
"has substring L\"foo\\n\\\"\"", Describe(m));
2055TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
2056 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L
""));
2057 EXPECT_TRUE(m1.Matches(L
"Hi"));
2058 EXPECT_TRUE(m1.Matches(L
""));
2059 EXPECT_FALSE(m1.Matches(
nullptr));
2061 const Matcher<const ::std::wstring&> m2 = StartsWith(L
"Hi");
2062 EXPECT_TRUE(m2.Matches(L
"Hi"));
2063 EXPECT_TRUE(m2.Matches(L
"Hi Hi!"));
2064 EXPECT_TRUE(m2.Matches(L
"High"));
2065 EXPECT_FALSE(m2.Matches(L
"H"));
2066 EXPECT_FALSE(m2.Matches(L
" Hi"));
2069TEST(StdWideStartsWithTest, CanDescribeSelf) {
2070 Matcher<const ::std::wstring> m = StartsWith(L
"Hi");
2071 EXPECT_EQ(
"starts with L\"Hi\"", Describe(m));
2076TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
2077 const Matcher<const wchar_t*> m1 = EndsWith(L
"");
2078 EXPECT_TRUE(m1.Matches(L
"Hi"));
2079 EXPECT_TRUE(m1.Matches(L
""));
2080 EXPECT_FALSE(m1.Matches(
nullptr));
2082 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L
"Hi"));
2083 EXPECT_TRUE(m2.Matches(L
"Hi"));
2084 EXPECT_TRUE(m2.Matches(L
"Wow Hi Hi"));
2085 EXPECT_TRUE(m2.Matches(L
"Super Hi"));
2086 EXPECT_FALSE(m2.Matches(L
"i"));
2087 EXPECT_FALSE(m2.Matches(L
"Hi "));
2090TEST(StdWideEndsWithTest, CanDescribeSelf) {
2091 Matcher<const ::std::wstring> m = EndsWith(L
"Hi");
2092 EXPECT_EQ(
"ends with L\"Hi\"", Describe(m));
2097TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
2098 StringMatchResultListener listener1;
2099 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
2100 EXPECT_EQ(
"% 2 == 0", listener1.str());
2102 StringMatchResultListener listener2;
2103 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
2104 EXPECT_EQ(
"", listener2.str());
2107TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
2108 const Matcher<int> is_even = PolymorphicIsEven();
2109 StringMatchResultListener listener1;
2110 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
2111 EXPECT_EQ(
"% 2 == 0", listener1.str());
2113 const Matcher<const double&> is_zero = Eq(0);
2114 StringMatchResultListener listener2;
2115 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
2116 EXPECT_EQ(
"", listener2.str());
2119MATCHER(ConstructNoArg,
"") {
return true; }
2120MATCHER_P(Construct1Arg, arg1,
"") {
return true; }
2121MATCHER_P2(Construct2Args, arg1, arg2,
"") {
return true; }
2123TEST(MatcherConstruct, ExplicitVsImplicit) {
2126 ConstructNoArgMatcher m = {};
2129 ConstructNoArgMatcher m2;
2135 using M = Construct1ArgMatcherP<int>;
2136 EXPECT_TRUE((std::is_constructible<M, int>::value));
2137 EXPECT_FALSE((std::is_convertible<int, M>::value));
2141 Construct2ArgsMatcherP2<int, double> m = {1, 2.2};
2146MATCHER_P(Really, inner_matcher,
"") {
2147 return ExplainMatchResult(inner_matcher, arg, result_listener);
2150TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2151 EXPECT_THAT(0, Really(Eq(0)));
2154TEST(DescribeMatcherTest, WorksWithValue) {
2155 EXPECT_EQ(
"is equal to 42", DescribeMatcher<int>(42));
2156 EXPECT_EQ(
"isn't equal to 42", DescribeMatcher<int>(42,
true));
2159TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
2160 const Matcher<int> monomorphic = Le(0);
2161 EXPECT_EQ(
"is <= 0", DescribeMatcher<int>(monomorphic));
2162 EXPECT_EQ(
"isn't <= 0", DescribeMatcher<int>(monomorphic,
true));
2165TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
2166 EXPECT_EQ(
"is even", DescribeMatcher<int>(PolymorphicIsEven()));
2167 EXPECT_EQ(
"is odd", DescribeMatcher<int>(PolymorphicIsEven(),
true));
2170MATCHER_P(FieldIIs, inner_matcher,
"") {
2171 return ExplainMatchResult(inner_matcher, arg.i, result_listener);
2175TEST(WhenDynamicCastToTest, SameType) {
2180 Base* as_base_ptr = &derived;
2181 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull())));
2182 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));
2183 EXPECT_THAT(as_base_ptr,
2184 Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));
2187TEST(WhenDynamicCastToTest, WrongTypes) {
2190 OtherDerived other_derived;
2193 EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
2194 EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull()));
2195 Base* as_base_ptr = &derived;
2196 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_))));
2197 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull()));
2198 as_base_ptr = &other_derived;
2199 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
2200 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
2203TEST(WhenDynamicCastToTest, AlreadyNull) {
2205 Base* as_base_ptr =
nullptr;
2206 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
2209struct AmbiguousCastTypes {
2210 class VirtualDerived :
public virtual Base {};
2211 class DerivedSub1 :
public VirtualDerived {};
2212 class DerivedSub2 :
public VirtualDerived {};
2213 class ManyDerivedInHierarchy :
public DerivedSub1,
public DerivedSub2 {};
2216TEST(WhenDynamicCastToTest, AmbiguousCast) {
2217 AmbiguousCastTypes::DerivedSub1 sub1;
2218 AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
2221 static_cast<AmbiguousCastTypes::DerivedSub1*
>(&many_derived);
2222 EXPECT_THAT(as_base_ptr,
2223 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull()));
2224 as_base_ptr = &sub1;
2227 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull())));
2230TEST(WhenDynamicCastToTest, Describe) {
2231 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
2232 const std::string prefix =
2233 "when dynamic_cast to " + internal::GetTypeName<Derived*>() +
", ";
2234 EXPECT_EQ(prefix +
"points to a value that is anything", Describe(matcher));
2235 EXPECT_EQ(prefix +
"does not point to a value that is anything",
2236 DescribeNegation(matcher));
2239TEST(WhenDynamicCastToTest, Explain) {
2240 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
2241 Base* null =
nullptr;
2242 EXPECT_THAT(Explain(matcher, null), HasSubstr(
"NULL"));
2244 EXPECT_TRUE(matcher.Matches(&derived));
2245 EXPECT_THAT(Explain(matcher, &derived), HasSubstr(
"which points to "));
2248 Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_);
2249 EXPECT_THAT(Explain(ref_matcher, derived),
2250 HasSubstr(
"which cannot be dynamic_cast"));
2253TEST(WhenDynamicCastToTest, GoodReference) {
2256 Base& as_base_ref = derived;
2257 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
2258 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));
2261TEST(WhenDynamicCastToTest, BadReference) {
2263 Base& as_base_ref = derived;
2264 EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_)));
2268class DivisibleByImpl {
2270 explicit DivisibleByImpl(
int a_divider) : divider_(a_divider) {}
2273 template <
typename T>
2274 bool MatchAndExplain(
const T& n, MatchResultListener* listener)
const {
2275 *listener <<
"which is " << (n % divider_) <<
" modulo " << divider_;
2276 return (n % divider_) == 0;
2279 void DescribeTo(ostream* os)
const { *os <<
"is divisible by " << divider_; }
2281 void DescribeNegationTo(ostream* os)
const {
2282 *os <<
"is not divisible by " << divider_;
2285 void set_divider(
int a_divider) { divider_ = a_divider; }
2286 int divider()
const {
return divider_; }
2292PolymorphicMatcher<DivisibleByImpl> DivisibleBy(
int n) {
2293 return MakePolymorphicMatcher(DivisibleByImpl(n));
2298TEST(ExplainMatchResultTest, AllOf_False_False) {
2299 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2300 EXPECT_EQ(
"which is 1 modulo 4", Explain(m, 5));
2305TEST(ExplainMatchResultTest, AllOf_False_True) {
2306 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2307 EXPECT_EQ(
"which is 2 modulo 4", Explain(m, 6));
2312TEST(ExplainMatchResultTest, AllOf_True_False) {
2313 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
2314 EXPECT_EQ(
"which is 2 modulo 3", Explain(m, 5));
2319TEST(ExplainMatchResultTest, AllOf_True_True) {
2320 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
2321 EXPECT_EQ(
"which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
2324TEST(ExplainMatchResultTest, AllOf_True_True_2) {
2325 const Matcher<int> m = AllOf(Ge(2), Le(3));
2326 EXPECT_EQ(
"", Explain(m, 2));
2329INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest);
2331TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {
2332 const Matcher<int> m = GreaterThan(5);
2333 EXPECT_EQ(
"which is 1 more than 5", Explain(m, 6));
2337TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
2338 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
2339 DivisibleByImpl& impl = m.mutable_impl();
2340 EXPECT_EQ(42, impl.divider());
2342 impl.set_divider(0);
2343 EXPECT_EQ(0, m.mutable_impl().divider());
2347TEST(PolymorphicMatcherTest, CanAccessImpl) {
2348 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
2349 const DivisibleByImpl& impl = m.impl();
2350 EXPECT_EQ(42, impl.divider());
Definition gtest_unittest.cc:5101
Definition googletest-list-tests-unittest_.cc:66