38 # pragma warning(push)
39 # pragma warning(disable:4244)
40 # pragma warning(disable:4100)
49 #include <forward_list>
60 #include <type_traits>
68 namespace gmock_matchers_test {
81 using std::stringstream;
83 using testing::internal::DummyMatchResultListener;
84 using testing::internal::ElementMatcherPair;
85 using testing::internal::ElementMatcherPairs;
86 using testing::internal::ExplainMatchFailureTupleTo;
87 using testing::internal::FloatingEqMatcher;
89 using testing::internal::IsReadableTypeName;
90 using testing::internal::MatchMatrix;
91 using testing::internal::PredicateFormatterFromMatcher;
93 using testing::internal::StreamMatchResultListener;
100 struct ContainerHelper {
101 MOCK_METHOD1(Call,
void(std::vector<std::unique_ptr<int>>));
104 std::vector<std::unique_ptr<int>> MakeUniquePtrs(
const std::vector<int>& ints) {
105 std::vector<std::unique_ptr<int>> pointers;
106 for (
int i : ints) pointers.emplace_back(
new int(
i));
111 class GreaterThanMatcher :
public MatcherInterface<int> {
113 explicit GreaterThanMatcher(
int rhs) :
rhs_(rhs) {}
115 void DescribeTo(ostream* os)
const override { *os <<
"is > " <<
rhs_; }
117 bool MatchAndExplain(
int lhs, MatchResultListener* listener)
const override {
118 const int diff = lhs -
rhs_;
120 *listener <<
"which is " << diff <<
" more than " <<
rhs_;
121 }
else if (diff == 0) {
122 *listener <<
"which is the same as " <<
rhs_;
124 *listener <<
"which is " << -diff <<
" less than " <<
rhs_;
134 Matcher<int> GreaterThan(
int n) {
135 return MakeMatcher(
new GreaterThanMatcher(
n));
140 return " (of type " + type_name +
")";
147 template <
typename T>
149 return DescribeMatcher<T>(
m);
153 template <
typename T>
155 return DescribeMatcher<T>(
m,
true);
159 template <
typename MatcherType,
typename Value>
161 StringMatchResultListener listener;
162 ExplainMatchResult(
m,
x, &listener);
163 return listener.str();
166 TEST(MonotonicMatcherTest, IsPrintable) {
168 ss << GreaterThan(5);
172 TEST(MatchResultListenerTest, StreamingWorks) {
173 StringMatchResultListener listener;
174 listener <<
"hi" << 5;
184 DummyMatchResultListener
dummy;
188 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
195 TEST(MatchResultListenerTest, IsInterestedWorks) {
196 EXPECT_TRUE(StringMatchResultListener().IsInterested());
197 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
199 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
200 EXPECT_FALSE(StreamMatchResultListener(
nullptr).IsInterested());
205 class EvenMatcherImpl :
public MatcherInterface<int> {
207 bool MatchAndExplain(
int x,
208 MatchResultListener* )
const override {
212 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
220 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
226 class NewEvenMatcherImpl :
public MatcherInterface<int> {
228 bool MatchAndExplain(
int x, MatchResultListener* listener)
const override {
229 const bool match =
x % 2 == 0;
231 *listener <<
"value % " << 2;
232 if (listener->stream() !=
nullptr) {
235 *listener->stream() <<
" == " << (
x % 2);
240 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
243 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
244 Matcher<int>
m = MakeMatcher(
new NewEvenMatcherImpl);
252 TEST(MatcherTest, CanBeDefaultConstructed) {
257 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
258 const MatcherInterface<int>* impl =
new EvenMatcherImpl;
259 Matcher<int>
m(impl);
265 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
272 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
273 Matcher<int*> m1 =
nullptr;
282 virtual ~Undefined() = 0;
286 TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
293 TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&>
m =
_; }
296 TEST(MatcherTest, IsCopyable) {
298 Matcher<bool> m1 = Eq(
false);
310 TEST(MatcherTest, CanDescribeItself) {
312 Describe(Matcher<int>(
new EvenMatcherImpl)));
316 TEST(MatcherTest, MatchAndExplain) {
317 Matcher<int>
m = GreaterThan(0);
318 StringMatchResultListener listener1;
320 EXPECT_EQ(
"which is 42 more than 0", listener1.str());
322 StringMatchResultListener listener2;
324 EXPECT_EQ(
"which is 9 less than 0", listener2.str());
329 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
330 Matcher<std::string> m1 =
"hi";
334 Matcher<const std::string&> m2 =
"hi";
341 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
346 Matcher<const std::string&> m2 =
std::string(
"hi");
351 #if GTEST_HAS_GLOBAL_STRING
354 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromGlobalString) {
355 Matcher<std::string> m1 =
::string(
"hi");
359 Matcher<const std::string&> m2 =
::string(
"hi");
363 #endif // GTEST_HAS_GLOBAL_STRING
365 #if GTEST_HAS_GLOBAL_STRING
368 TEST(GlobalStringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
369 Matcher< ::string> m1 =
"hi";
373 Matcher<const ::string&> m2 =
"hi";
380 TEST(GlobalStringMatcherTest, CanBeImplicitlyConstructedFromString) {
392 TEST(GlobalStringMatcherTest, CanBeImplicitlyConstructedFromGlobalString) {
393 Matcher< ::string> m1 =
::string(
"hi");
397 Matcher<const ::string&> m2 =
::string(
"hi");
401 #endif // GTEST_HAS_GLOBAL_STRING
406 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
407 Matcher<absl::string_view> m1 =
"cats";
411 Matcher<const absl::string_view&> m2 =
"cats";
418 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
419 Matcher<absl::string_view> m1 =
std::string(
"cats");
423 Matcher<const absl::string_view&> m2 =
std::string(
"cats");
428 #if GTEST_HAS_GLOBAL_STRING
431 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromGlobalString) {
432 Matcher<absl::string_view> m1 =
::string(
"cats");
436 Matcher<const absl::string_view&> m2 =
::string(
"cats");
440 #endif // GTEST_HAS_GLOBAL_STRING
444 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
445 Matcher<absl::string_view> m1 = absl::string_view(
"cats");
449 Matcher<const absl::string_view&> m2 = absl::string_view(
"cats");
453 #endif // GTEST_HAS_ABSL
458 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
459 const MatcherInterface<int>* dummy_impl =
nullptr;
460 Matcher<int>
m = MakeMatcher(dummy_impl);
466 class ReferencesBarOrIsZeroImpl {
468 template <
typename T>
469 bool MatchAndExplain(
const T&
x,
470 MatchResultListener* )
const {
472 return p == &g_bar ||
x == 0;
475 void DescribeTo(ostream* os)
const { *os <<
"g_bar or zero"; }
477 void DescribeNegationTo(ostream* os)
const {
478 *os <<
"doesn't reference g_bar and is not zero";
484 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
485 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
488 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
490 Matcher<const int&> m1 = ReferencesBarOrIsZero();
495 EXPECT_EQ(
"g_bar or zero", Describe(m1));
498 Matcher<double> m2 = ReferencesBarOrIsZero();
501 EXPECT_EQ(
"g_bar or zero", Describe(m2));
506 class PolymorphicIsEvenImpl {
508 void DescribeTo(ostream* os)
const { *os <<
"is even"; }
510 void DescribeNegationTo(ostream* os)
const {
514 template <
typename T>
515 bool MatchAndExplain(
const T&
x, MatchResultListener* listener)
const {
517 *listener <<
"% " << 2;
518 if (listener->stream() !=
nullptr) {
521 *listener->stream() <<
" == " << (
x % 2);
527 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
528 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
531 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
533 const Matcher<int> m1 = PolymorphicIsEven();
538 const Matcher<int> not_m1 = Not(m1);
544 const Matcher<char> m2 = PolymorphicIsEven();
549 const Matcher<char> not_m2 = Not(m2);
552 EXPECT_EQ(
"% 2 == 0", Explain(m2,
'\x42'));
556 TEST(MatcherCastTest, FromPolymorphicMatcher) {
557 Matcher<int>
m = MatcherCast<int>(Eq(5));
567 explicit IntValue(
int a_value) :
value_(a_value) {}
575 bool IsPositiveIntValue(
const IntValue&
foo) {
576 return foo.value() > 0;
581 TEST(MatcherCastTest, FromCompatibleType) {
582 Matcher<double> m1 = Eq(2.0);
583 Matcher<int> m2 = MatcherCast<int>(m1);
587 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
588 Matcher<int> m4 = MatcherCast<int>(m3);
597 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
598 Matcher<const int&> m1 = Eq(0);
599 Matcher<int> m2 = MatcherCast<int>(m1);
605 TEST(MatcherCastTest, FromReferenceToNonReference) {
606 Matcher<int&> m1 = Eq(0);
607 Matcher<int> m2 = MatcherCast<int>(m1);
613 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
614 Matcher<int> m1 = Eq(0);
615 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
621 TEST(MatcherCastTest, FromNonReferenceToReference) {
622 Matcher<int> m1 = Eq(0);
623 Matcher<int&> m2 = MatcherCast<int&>(m1);
631 TEST(MatcherCastTest, FromSameType) {
632 Matcher<int> m1 = Eq(0);
633 Matcher<int> m2 = MatcherCast<int>(m1);
640 TEST(MatcherCastTest, FromAValue) {
641 Matcher<int>
m = MatcherCast<int>(42);
648 TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
649 const int kExpected =
'c';
650 Matcher<int>
m = MatcherCast<int>(
'c');
655 struct NonImplicitlyConstructibleTypeWithOperatorEq {
657 const NonImplicitlyConstructibleTypeWithOperatorEq& ,
663 const NonImplicitlyConstructibleTypeWithOperatorEq& ) {
671 TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
672 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
673 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
674 EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
676 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
677 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
678 EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
683 MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
693 #if !defined _MSC_VER
702 namespace convertible_from_any {
704 struct ConvertibleFromAny {
705 ConvertibleFromAny(
int a_value) :
value(a_value) {}
706 template <
typename T>
707 ConvertibleFromAny(
const T& ) :
value(-1) {
713 bool operator==(
const ConvertibleFromAny&
a,
const ConvertibleFromAny&
b) {
714 return a.value ==
b.value;
717 ostream&
operator<<(ostream& os,
const ConvertibleFromAny&
a) {
718 return os <<
a.value;
721 TEST(MatcherCastTest, ConversionConstructorIsUsed) {
722 Matcher<ConvertibleFromAny>
m = MatcherCast<ConvertibleFromAny>(1);
727 TEST(MatcherCastTest, FromConvertibleFromAny) {
728 Matcher<ConvertibleFromAny>
m =
729 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
735 #endif // !defined _MSC_VER
737 struct IntReferenceWrapper {
738 IntReferenceWrapper(
const int& a_value) :
value(&a_value) {}
742 bool operator==(
const IntReferenceWrapper&
a,
const IntReferenceWrapper&
b) {
743 return a.value ==
b.value;
746 TEST(MatcherCastTest, ValueIsNotCopied) {
748 Matcher<IntReferenceWrapper>
m = MatcherCast<IntReferenceWrapper>(
n);
761 class Derived :
public Base {
763 Derived() : Base() {}
767 class OtherDerived :
public Base {};
770 TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
771 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
779 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
780 Matcher<double> m1 = DoubleEq(1.0);
781 Matcher<float> m2 = SafeMatcherCast<float>(m1);
785 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>(
'a'));
792 TEST(SafeMatcherCastTest, FromBaseClass) {
794 Matcher<Base*> m1 = Eq(&d);
795 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
799 Matcher<Base&> m3 = Ref(d);
800 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
806 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
808 Matcher<const int&> m1 = Ref(
n);
809 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
816 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
817 Matcher<int> m1 = Eq(0);
818 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
824 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
825 Matcher<int> m1 = Eq(0);
826 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
834 TEST(SafeMatcherCastTest, FromSameType) {
835 Matcher<int> m1 = Eq(0);
836 Matcher<int> m2 = SafeMatcherCast<int>(m1);
841 #if !defined _MSC_VER
843 namespace convertible_from_any {
844 TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
845 Matcher<ConvertibleFromAny>
m = SafeMatcherCast<ConvertibleFromAny>(1);
850 TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
851 Matcher<ConvertibleFromAny>
m =
852 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
858 #endif // !defined _MSC_VER
860 TEST(SafeMatcherCastTest, ValueIsNotCopied) {
862 Matcher<IntReferenceWrapper>
m = SafeMatcherCast<IntReferenceWrapper>(
n);
867 TEST(ExpectThat, TakesLiterals) {
873 TEST(ExpectThat, TakesFunctions) {
875 static void Func() {}
883 TEST(ATest, MatchesAnyValue) {
897 TEST(ATest, WorksForDerivedClass) {
907 TEST(ATest, CanDescribeSelf) {
912 TEST(AnTest, MatchesAnyValue) {
914 Matcher<int> m1 = An<int>();
921 Matcher<int&> m2 = An<int&>();
927 TEST(AnTest, CanDescribeSelf) {
928 EXPECT_EQ(
"is anything", Describe(An<int>()));
933 TEST(UnderscoreTest, MatchesAnyValue) {
942 Matcher<const bool&> m2 =
_;
948 TEST(UnderscoreTest, CanDescribeSelf) {
954 TEST(EqTest, MatchesEqualValue) {
956 const char a1[] =
"hi";
957 const char a2[] =
"hi";
959 Matcher<const char*> m1 = Eq(a1);
968 Unprintable() :
c_(
'a') {}
970 bool operator==(
const Unprintable& )
const {
return true; }
975 TEST(EqTest, CanDescribeSelf) {
976 Matcher<Unprintable>
m = Eq(Unprintable());
977 EXPECT_EQ(
"is equal to 1-byte object <61>", Describe(
m));
982 TEST(EqTest, IsPolymorphic) {
983 Matcher<int> m1 = Eq(1);
987 Matcher<char> m2 = Eq(1);
993 TEST(TypedEqTest, ChecksEqualityForGivenType) {
994 Matcher<char> m1 = TypedEq<char>(
'a');
998 Matcher<int> m2 = TypedEq<int>(6);
1004 TEST(TypedEqTest, CanDescribeSelf) {
1005 EXPECT_EQ(
"is equal to 2", Describe(TypedEq<int>(2)));
1014 template <
typename T>
1016 static bool IsTypeOf(
const T& ) {
return true; }
1018 template <
typename T2>
1019 static void IsTypeOf(T2
v);
1022 TEST(TypedEqTest, HasSpecifiedType) {
1029 TEST(GeTest, ImplementsGreaterThanOrEqual) {
1030 Matcher<int> m1 = Ge(0);
1037 TEST(GeTest, CanDescribeSelf) {
1038 Matcher<int>
m = Ge(5);
1043 TEST(GtTest, ImplementsGreaterThan) {
1044 Matcher<double> m1 = Gt(0);
1051 TEST(GtTest, CanDescribeSelf) {
1052 Matcher<int>
m = Gt(5);
1057 TEST(LeTest, ImplementsLessThanOrEqual) {
1058 Matcher<char> m1 = Le(
'b');
1065 TEST(LeTest, CanDescribeSelf) {
1066 Matcher<int>
m = Le(5);
1071 TEST(LtTest, ImplementsLessThan) {
1072 Matcher<const std::string&> m1 = Lt(
"Hello");
1079 TEST(LtTest, CanDescribeSelf) {
1080 Matcher<int>
m = Lt(5);
1085 TEST(NeTest, ImplementsNotEqual) {
1086 Matcher<int> m1 = Ne(0);
1093 TEST(NeTest, CanDescribeSelf) {
1094 Matcher<int>
m = Ne(5);
1100 explicit MoveOnly(
int i) :
i_(
i) {}
1101 MoveOnly(
const MoveOnly&) =
delete;
1102 MoveOnly(MoveOnly&&) =
default;
1103 MoveOnly& operator=(
const MoveOnly&) =
delete;
1104 MoveOnly& operator=(MoveOnly&&) =
default;
1106 bool operator==(
const MoveOnly& other)
const {
return i_ == other.i_; }
1107 bool operator!=(
const MoveOnly& other)
const {
return i_ != other.i_; }
1108 bool operator<(
const MoveOnly& other)
const {
return i_ < other.i_; }
1109 bool operator<=(
const MoveOnly& other)
const {
return i_ <= other.i_; }
1110 bool operator>(
const MoveOnly& other)
const {
return i_ > other.i_; }
1111 bool operator>=(
const MoveOnly& other)
const {
return i_ >= other.i_; }
1121 TEST(ComparisonBaseTest, WorksWithMoveOnly) {
1126 helper.Call(MoveOnly(0));
1128 helper.Call(MoveOnly(1));
1130 helper.Call(MoveOnly(0));
1132 helper.Call(MoveOnly(-1));
1134 helper.Call(MoveOnly(0));
1136 helper.Call(MoveOnly(1));
1140 TEST(IsNullTest, MatchesNullPointer) {
1141 Matcher<int*> m1 =
IsNull();
1147 Matcher<const char*> m2 =
IsNull();
1148 const char* p2 =
nullptr;
1152 Matcher<void*> m3 =
IsNull();
1155 EXPECT_FALSE(m3.Matches(
reinterpret_cast<void*
>(0xbeef)));
1158 TEST(IsNullTest, StdFunction) {
1159 const Matcher<std::function<
void()>>
m =
IsNull();
1166 TEST(IsNullTest, CanDescribeSelf) {
1169 EXPECT_EQ(
"isn't NULL", DescribeNegation(
m));
1173 TEST(NotNullTest, MatchesNonNullPointer) {
1174 Matcher<int*> m1 = NotNull();
1180 Matcher<const char*> m2 = NotNull();
1181 const char* p2 =
nullptr;
1186 TEST(NotNullTest, LinkedPtr) {
1187 const Matcher<std::shared_ptr<int>>
m = NotNull();
1188 const std::shared_ptr<int> null_p;
1189 const std::shared_ptr<int> non_null_p(
new int);
1195 TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1196 const Matcher<const std::shared_ptr<double>&>
m = NotNull();
1197 const std::shared_ptr<double> null_p;
1198 const std::shared_ptr<double> non_null_p(
new double);
1204 TEST(NotNullTest, StdFunction) {
1205 const Matcher<std::function<
void()>>
m = NotNull();
1212 TEST(NotNullTest, CanDescribeSelf) {
1213 Matcher<int*>
m = NotNull();
1219 TEST(RefTest, MatchesSameVariable) {
1222 Matcher<int&>
m = Ref(
a);
1228 TEST(RefTest, CanDescribeSelf) {
1230 Matcher<int&>
m = Ref(
n);
1232 ss <<
"references the variable @" << &
n <<
" 5";
1238 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1241 Matcher<const int&>
m = Ref(
a);
1250 TEST(RefTest, IsCovariant) {
1253 Matcher<const Base&> m1 = Ref(
base);
1264 TEST(RefTest, ExplainsResult) {
1276 TEST(StrEqTest, MatchesEqualString) {
1282 Matcher<const std::string&> m2 = StrEq(
"Hello");
1287 Matcher<const absl::string_view&> m3 = StrEq(
"Hello");
1288 EXPECT_TRUE(m3.Matches(absl::string_view(
"Hello")));
1292 Matcher<const absl::string_view&> m_empty = StrEq(
"");
1293 EXPECT_TRUE(m_empty.Matches(absl::string_view(
"")));
1294 EXPECT_TRUE(m_empty.Matches(absl::string_view()));
1295 EXPECT_FALSE(m_empty.Matches(absl::string_view(
"hello")));
1296 #endif // GTEST_HAS_ABSL
1299 TEST(StrEqTest, CanDescribeSelf) {
1300 Matcher<std::string>
m = StrEq(
"Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1301 EXPECT_EQ(
"is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1306 Matcher<std::string> m2 = StrEq(
str);
1307 EXPECT_EQ(
"is equal to \"012\\04500800\"", Describe(m2));
1309 Matcher<std::string> m3 = StrEq(
str);
1310 EXPECT_EQ(
"is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1313 TEST(StrNeTest, MatchesUnequalString) {
1314 Matcher<const char*>
m = StrNe(
"Hello");
1319 Matcher<std::string> m2 = StrNe(
std::string(
"Hello"));
1324 Matcher<const absl::string_view> m3 = StrNe(
"Hello");
1328 #endif // GTEST_HAS_ABSL
1331 TEST(StrNeTest, CanDescribeSelf) {
1332 Matcher<const char*>
m = StrNe(
"Hi");
1333 EXPECT_EQ(
"isn't equal to \"Hi\"", Describe(
m));
1336 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1337 Matcher<const char*>
m = StrCaseEq(
std::string(
"Hello"));
1343 Matcher<const std::string&> m2 = StrCaseEq(
"Hello");
1348 Matcher<const absl::string_view&> m3 = StrCaseEq(
std::string(
"Hello"));
1349 EXPECT_TRUE(m3.Matches(absl::string_view(
"Hello")));
1350 EXPECT_TRUE(m3.Matches(absl::string_view(
"hello")));
1353 #endif // GTEST_HAS_ABSL
1356 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1359 Matcher<const std::string&> m0 = StrCaseEq(str1);
1362 str1[3] = str2[3] =
'\0';
1363 Matcher<const std::string&> m1 = StrCaseEq(str1);
1366 str1[0] = str1[6] = str1[7] = str1[10] =
'\0';
1367 str2[0] = str2[6] = str2[7] = str2[10] =
'\0';
1368 Matcher<const std::string&> m2 = StrCaseEq(str1);
1369 str1[9] = str2[9] =
'\0';
1372 Matcher<const std::string&> m3 = StrCaseEq(str1);
1376 str2.append(1,
'\0');
1381 TEST(StrCaseEqTest, CanDescribeSelf) {
1382 Matcher<std::string>
m = StrCaseEq(
"Hi");
1383 EXPECT_EQ(
"is equal to (ignoring case) \"Hi\"", Describe(
m));
1386 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1387 Matcher<const char*>
m = StrCaseNe(
"Hello");
1393 Matcher<std::string> m2 = StrCaseNe(
std::string(
"Hello"));
1398 Matcher<const absl::string_view> m3 = StrCaseNe(
"Hello");
1403 #endif // GTEST_HAS_ABSL
1406 TEST(StrCaseNeTest, CanDescribeSelf) {
1407 Matcher<const char*>
m = StrCaseNe(
"Hi");
1408 EXPECT_EQ(
"isn't equal to (ignoring case) \"Hi\"", Describe(
m));
1412 TEST(HasSubstrTest, WorksForStringClasses) {
1413 const Matcher<std::string> m1 = HasSubstr(
"foo");
1417 const Matcher<const std::string&> m2 = HasSubstr(
"foo");
1421 const Matcher<std::string> m_empty = HasSubstr(
"");
1427 TEST(HasSubstrTest, WorksForCStrings) {
1428 const Matcher<char*> m1 = HasSubstr(
"foo");
1429 EXPECT_TRUE(m1.Matches(
const_cast<char*
>(
"I love food.")));
1433 const Matcher<const char*> m2 = HasSubstr(
"foo");
1438 const Matcher<const char*> m_empty = HasSubstr(
"");
1446 TEST(HasSubstrTest, WorksForStringViewClasses) {
1447 const Matcher<absl::string_view> m1 = HasSubstr(
"foo");
1448 EXPECT_TRUE(m1.Matches(absl::string_view(
"I love food.")));
1452 const Matcher<const absl::string_view&> m2 = HasSubstr(
"foo");
1453 EXPECT_TRUE(m2.Matches(absl::string_view(
"I love food.")));
1457 const Matcher<const absl::string_view&> m3 = HasSubstr(
"");
1458 EXPECT_TRUE(m3.Matches(absl::string_view(
"foo")));
1462 #endif // GTEST_HAS_ABSL
1465 TEST(HasSubstrTest, CanDescribeSelf) {
1466 Matcher<std::string>
m = HasSubstr(
"foo\n\"");
1467 EXPECT_EQ(
"has substring \"foo\\n\\\"\"", Describe(
m));
1470 TEST(KeyTest, CanDescribeSelf) {
1471 Matcher<const pair<std::string, int>&>
m = Key(
"foo");
1472 EXPECT_EQ(
"has a key that is equal to \"foo\"", Describe(
m));
1473 EXPECT_EQ(
"doesn't have a key that is equal to \"foo\"", DescribeNegation(
m));
1476 TEST(KeyTest, ExplainsResult) {
1477 Matcher<pair<int, bool> >
m = Key(GreaterThan(10));
1478 EXPECT_EQ(
"whose first field is a value which is 5 less than 10",
1479 Explain(
m, make_pair(5,
true)));
1480 EXPECT_EQ(
"whose first field is a value which is 5 more than 10",
1481 Explain(
m, make_pair(15,
true)));
1484 TEST(KeyTest, MatchesCorrectly) {
1485 pair<int, std::string>
p(25,
"foo");
1492 TEST(KeyTest, WorksWithMoveOnly) {
1493 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1500 struct PairWithGet {
1503 using first_type = int;
1504 using second_type =
string;
1506 const int& GetImpl(Tag<0>)
const {
return member_1; }
1507 const string& GetImpl(Tag<1>)
const {
return member_2; }
1510 auto get(
const PairWithGet&
value) -> decltype(
value.GetImpl(Tag<I>())) {
1511 return value.GetImpl(Tag<I>());
1513 TEST(PairTest, MatchesPairWithGetCorrectly) {
1514 PairWithGet
p{25,
"foo"};
1520 std::vector<PairWithGet>
v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1524 TEST(KeyTest, SafelyCastsInnerMatcher) {
1525 Matcher<int> is_positive = Gt(0);
1526 Matcher<int> is_negative = Lt(0);
1527 pair<char, bool>
p(
'a',
true);
1532 TEST(KeyTest, InsideContainsUsingMap) {
1533 map<int, char> container;
1534 container.insert(make_pair(1,
'a'));
1535 container.insert(make_pair(2,
'b'));
1536 container.insert(make_pair(4,
'c'));
1541 TEST(KeyTest, InsideContainsUsingMultimap) {
1542 multimap<int, char> container;
1543 container.insert(make_pair(1,
'a'));
1544 container.insert(make_pair(2,
'b'));
1545 container.insert(make_pair(4,
'c'));
1548 container.insert(make_pair(25,
'd'));
1550 container.insert(make_pair(25,
'e'));
1557 TEST(PairTest, Typing) {
1559 Matcher<const pair<const char*, int>&> m1 = Pair(
"foo", 42);
1560 Matcher<const pair<const char*, int> > m2 = Pair(
"foo", 42);
1561 Matcher<pair<const char*, int> > m3 = Pair(
"foo", 42);
1563 Matcher<pair<int, const std::string> > m4 = Pair(25,
"42");
1564 Matcher<pair<const std::string, int> > m5 = Pair(
"25", 42);
1567 TEST(PairTest, CanDescribeSelf) {
1568 Matcher<const pair<std::string, int>&> m1 = Pair(
"foo", 42);
1569 EXPECT_EQ(
"has a first field that is equal to \"foo\""
1570 ", and has a second field that is equal to 42",
1572 EXPECT_EQ(
"has a first field that isn't equal to \"foo\""
1573 ", or has a second field that isn't equal to 42",
1574 DescribeNegation(m1));
1576 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1577 EXPECT_EQ(
"has a first field that isn't equal to 13"
1578 ", and has a second field that is equal to 42",
1579 DescribeNegation(m2));
1582 TEST(PairTest, CanExplainMatchResultTo) {
1585 const Matcher<pair<int, int> >
m = Pair(GreaterThan(0), GreaterThan(0));
1586 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1587 Explain(
m, make_pair(-1, -2)));
1591 EXPECT_EQ(
"whose second field does not match, which is 2 less than 0",
1592 Explain(
m, make_pair(1, -2)));
1596 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1597 Explain(
m, make_pair(-1, 2)));
1600 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1601 "which is 1 more than 0, and the second field is a value "
1602 "which is 2 more than 0",
1603 Explain(
m, make_pair(1, 2)));
1607 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
1608 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1609 "which is 1 more than 0",
1610 Explain(explain_first, make_pair(1, 0)));
1614 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
1615 EXPECT_EQ(
"whose both fields match, where the second field is a value "
1616 "which is 1 more than 0",
1617 Explain(explain_second, make_pair(0, 1)));
1620 TEST(PairTest, MatchesCorrectly) {
1621 pair<int, std::string>
p(25,
"foo");
1640 TEST(PairTest, WorksWithMoveOnly) {
1641 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1642 p.second.reset(
new int(7));
1646 TEST(PairTest, SafelyCastsInnerMatchers) {
1647 Matcher<int> is_positive = Gt(0);
1648 Matcher<int> is_negative = Lt(0);
1649 pair<char, bool>
p(
'a',
true);
1656 TEST(PairTest, InsideContainsUsingMap) {
1657 map<int, char> container;
1658 container.insert(make_pair(1,
'a'));
1659 container.insert(make_pair(2,
'b'));
1660 container.insert(make_pair(4,
'c'));
1667 TEST(ContainsTest, WorksWithMoveOnly) {
1668 ContainerHelper helper;
1670 helper.Call(MakeUniquePtrs({1, 2}));
1673 TEST(PairTest, UseGetInsteadOfMembers) {
1674 PairWithGet pair{7,
"ABC"};
1679 std::vector<PairWithGet>
v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1680 EXPECT_THAT(
v, ElementsAre(Pair(11,
string(
"Foo")), Pair(Ge(10), Not(
""))));
1685 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1691 const Matcher<const std::string&> m2 =
StartsWith(
"Hi");
1699 const Matcher<absl::string_view> m_empty =
StartsWith(
"");
1700 EXPECT_TRUE(m_empty.Matches(absl::string_view()));
1701 EXPECT_TRUE(m_empty.Matches(absl::string_view(
"")));
1702 EXPECT_TRUE(m_empty.Matches(absl::string_view(
"not empty")));
1703 #endif // GTEST_HAS_ABSL
1706 TEST(StartsWithTest, CanDescribeSelf) {
1708 EXPECT_EQ(
"starts with \"Hi\"", Describe(
m));
1713 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1714 const Matcher<const char*> m1 =
EndsWith(
"");
1726 #if GTEST_HAS_GLOBAL_STRING
1727 const Matcher<const ::string&> m3 =
EndsWith(::
string(
"Hi"));
1733 #endif // GTEST_HAS_GLOBAL_STRING
1736 const Matcher<const absl::string_view&> m4 =
EndsWith(
"");
1741 #endif // GTEST_HAS_ABSL
1744 TEST(EndsWithTest, CanDescribeSelf) {
1745 Matcher<const std::string>
m =
EndsWith(
"Hi");
1751 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1752 const Matcher<const char*> m1 = MatchesRegex(
"a.*z");
1757 const Matcher<const std::string&> m2 = MatchesRegex(
new RE(
"a.*z"));
1763 const Matcher<const absl::string_view&> m3 = MatchesRegex(
"a.*z");
1765 EXPECT_TRUE(m3.Matches(absl::string_view(
"abcz")));
1768 const Matcher<const absl::string_view&> m4 = MatchesRegex(
"");
1771 #endif // GTEST_HAS_ABSL
1774 TEST(MatchesRegexTest, CanDescribeSelf) {
1775 Matcher<const std::string> m1 = MatchesRegex(
std::string(
"Hi.*"));
1776 EXPECT_EQ(
"matches regular expression \"Hi.*\"", Describe(m1));
1778 Matcher<const char*> m2 = MatchesRegex(
new RE(
"a.*"));
1779 EXPECT_EQ(
"matches regular expression \"a.*\"", Describe(m2));
1782 Matcher<const absl::string_view> m3 = MatchesRegex(
new RE(
"0.*"));
1783 EXPECT_EQ(
"matches regular expression \"0.*\"", Describe(m3));
1784 #endif // GTEST_HAS_ABSL
1789 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1790 const Matcher<const char*> m1 = ContainsRegex(
std::string(
"a.*z"));
1795 const Matcher<const std::string&> m2 = ContainsRegex(
new RE(
"a.*z"));
1801 const Matcher<const absl::string_view&> m3 = ContainsRegex(
new RE(
"a.*z"));
1802 EXPECT_TRUE(m3.Matches(absl::string_view(
"azbz")));
1803 EXPECT_TRUE(m3.Matches(absl::string_view(
"az1")));
1806 const Matcher<const absl::string_view&> m4 = ContainsRegex(
"");
1809 #endif // GTEST_HAS_ABSL
1812 TEST(ContainsRegexTest, CanDescribeSelf) {
1813 Matcher<const std::string> m1 = ContainsRegex(
"Hi.*");
1814 EXPECT_EQ(
"contains regular expression \"Hi.*\"", Describe(m1));
1816 Matcher<const char*> m2 = ContainsRegex(
new RE(
"a.*"));
1817 EXPECT_EQ(
"contains regular expression \"a.*\"", Describe(m2));
1820 Matcher<const absl::string_view> m3 = ContainsRegex(
new RE(
"0.*"));
1821 EXPECT_EQ(
"contains regular expression \"0.*\"", Describe(m3));
1822 #endif // GTEST_HAS_ABSL
1826 #if GTEST_HAS_STD_WSTRING
1827 TEST(StdWideStrEqTest, MatchesEqual) {
1828 Matcher<const wchar_t*>
m = StrEq(::
std::wstring(L
"Hello"));
1833 Matcher<const ::std::wstring&> m2 = StrEq(L
"Hello");
1837 Matcher<const ::std::wstring&> m3 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1843 Matcher<const ::std::wstring&> m4 = StrEq(
str);
1846 Matcher<const ::std::wstring&> m5 = StrEq(
str);
1850 TEST(StdWideStrEqTest, CanDescribeSelf) {
1851 Matcher< ::std::wstring>
m = StrEq(L
"Hi-\'\"?\\\a\b\f\n\r\t\v");
1852 EXPECT_EQ(
"is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1855 Matcher< ::std::wstring> m2 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1856 EXPECT_EQ(
"is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1861 Matcher<const ::std::wstring&> m4 = StrEq(
str);
1862 EXPECT_EQ(
"is equal to L\"012\\04500800\"", Describe(m4));
1864 Matcher<const ::std::wstring&> m5 = StrEq(
str);
1865 EXPECT_EQ(
"is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1868 TEST(StdWideStrNeTest, MatchesUnequalString) {
1869 Matcher<const wchar_t*>
m = StrNe(L
"Hello");
1874 Matcher< ::std::wstring> m2 = StrNe(::
std::wstring(L
"Hello"));
1879 TEST(StdWideStrNeTest, CanDescribeSelf) {
1880 Matcher<const wchar_t*>
m = StrNe(L
"Hi");
1881 EXPECT_EQ(
"isn't equal to L\"Hi\"", Describe(
m));
1884 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1885 Matcher<const wchar_t*>
m = StrCaseEq(::
std::wstring(L
"Hello"));
1891 Matcher<const ::std::wstring&> m2 = StrCaseEq(L
"Hello");
1896 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1899 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1902 str1[3] = str2[3] = L
'\0';
1903 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1906 str1[0] = str1[6] = str1[7] = str1[10] = L
'\0';
1907 str2[0] = str2[6] = str2[7] = str2[10] = L
'\0';
1908 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1909 str1[9] = str2[9] = L
'\0';
1912 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1916 str2.append(1, L
'\0');
1921 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1922 Matcher< ::std::wstring>
m = StrCaseEq(L
"Hi");
1923 EXPECT_EQ(
"is equal to (ignoring case) L\"Hi\"", Describe(
m));
1926 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1927 Matcher<const wchar_t*>
m = StrCaseNe(L
"Hello");
1933 Matcher< ::std::wstring> m2 = StrCaseNe(::
std::wstring(L
"Hello"));
1938 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1939 Matcher<const wchar_t*>
m = StrCaseNe(L
"Hi");
1940 EXPECT_EQ(
"isn't equal to (ignoring case) L\"Hi\"", Describe(
m));
1944 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1945 const Matcher< ::std::wstring> m1 = HasSubstr(L
"foo");
1949 const Matcher<const ::std::wstring&> m2 = HasSubstr(L
"foo");
1955 TEST(StdWideHasSubstrTest, WorksForCStrings) {
1956 const Matcher<wchar_t*> m1 = HasSubstr(L
"foo");
1957 EXPECT_TRUE(m1.Matches(
const_cast<wchar_t*
>(L
"I love food.")));
1958 EXPECT_FALSE(m1.Matches(
const_cast<wchar_t*
>(L
"tofo")));
1961 const Matcher<const wchar_t*> m2 = HasSubstr(L
"foo");
1968 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1969 Matcher< ::std::wstring>
m = HasSubstr(L
"foo\n\"");
1970 EXPECT_EQ(
"has substring L\"foo\\n\\\"\"", Describe(
m));
1975 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1981 const Matcher<const ::std::wstring&> m2 =
StartsWith(L
"Hi");
1989 TEST(StdWideStartsWithTest, CanDescribeSelf) {
1990 Matcher<const ::std::wstring>
m =
StartsWith(L
"Hi");
1991 EXPECT_EQ(
"starts with L\"Hi\"", Describe(
m));
1996 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1997 const Matcher<const wchar_t*> m1 =
EndsWith(L
"");
2010 TEST(StdWideEndsWithTest, CanDescribeSelf) {
2011 Matcher<const ::std::wstring>
m =
EndsWith(L
"Hi");
2015 #endif // GTEST_HAS_STD_WSTRING
2017 #if GTEST_HAS_GLOBAL_WSTRING
2018 TEST(GlobalWideStrEqTest, MatchesEqual) {
2019 Matcher<const wchar_t*>
m = StrEq(::
wstring(L
"Hello"));
2024 Matcher<const ::wstring&> m2 = StrEq(L
"Hello");
2028 Matcher<const ::wstring&> m3 = StrEq(L
"\xD3\x576\x8D3\xC74D");
2034 Matcher<const ::wstring&> m4 = StrEq(
str);
2037 Matcher<const ::wstring&> m5 = StrEq(
str);
2041 TEST(GlobalWideStrEqTest, CanDescribeSelf) {
2042 Matcher< ::wstring>
m = StrEq(L
"Hi-\'\"?\\\a\b\f\n\r\t\v");
2043 EXPECT_EQ(
"is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
2046 Matcher< ::wstring> m2 = StrEq(L
"\xD3\x576\x8D3\xC74D");
2047 EXPECT_EQ(
"is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
2052 Matcher<const ::wstring&> m4 = StrEq(
str);
2053 EXPECT_EQ(
"is equal to L\"012\\04500800\"", Describe(m4));
2055 Matcher<const ::wstring&> m5 = StrEq(
str);
2056 EXPECT_EQ(
"is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
2059 TEST(GlobalWideStrNeTest, MatchesUnequalString) {
2060 Matcher<const wchar_t*>
m = StrNe(L
"Hello");
2065 Matcher< ::wstring> m2 = StrNe(::
wstring(L
"Hello"));
2070 TEST(GlobalWideStrNeTest, CanDescribeSelf) {
2071 Matcher<const wchar_t*>
m = StrNe(L
"Hi");
2072 EXPECT_EQ(
"isn't equal to L\"Hi\"", Describe(
m));
2075 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
2076 Matcher<const wchar_t*>
m = StrCaseEq(::
wstring(L
"Hello"));
2082 Matcher<const ::wstring&> m2 = StrCaseEq(L
"Hello");
2087 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
2090 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
2093 str1[3] = str2[3] = L
'\0';
2094 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
2097 str1[0] = str1[6] = str1[7] = str1[10] = L
'\0';
2098 str2[0] = str2[6] = str2[7] = str2[10] = L
'\0';
2099 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
2100 str1[9] = str2[9] = L
'\0';
2103 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
2107 str2.append(1, L
'\0');
2112 TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
2113 Matcher< ::wstring>
m = StrCaseEq(L
"Hi");
2114 EXPECT_EQ(
"is equal to (ignoring case) L\"Hi\"", Describe(
m));
2117 TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
2118 Matcher<const wchar_t*>
m = StrCaseNe(L
"Hello");
2124 Matcher< ::wstring> m2 = StrCaseNe(::
wstring(L
"Hello"));
2129 TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
2130 Matcher<const wchar_t*>
m = StrCaseNe(L
"Hi");
2131 EXPECT_EQ(
"isn't equal to (ignoring case) L\"Hi\"", Describe(
m));
2135 TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
2136 const Matcher< ::wstring> m1 = HasSubstr(L
"foo");
2140 const Matcher<const ::wstring&> m2 = HasSubstr(L
"foo");
2146 TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
2147 const Matcher<wchar_t*> m1 = HasSubstr(L
"foo");
2148 EXPECT_TRUE(m1.Matches(
const_cast<wchar_t*
>(L
"I love food.")));
2149 EXPECT_FALSE(m1.Matches(
const_cast<wchar_t*
>(L
"tofo")));
2152 const Matcher<const wchar_t*> m2 = HasSubstr(L
"foo");
2159 TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
2160 Matcher< ::wstring>
m = HasSubstr(L
"foo\n\"");
2161 EXPECT_EQ(
"has substring L\"foo\\n\\\"\"", Describe(
m));
2166 TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
2172 const Matcher<const ::wstring&> m2 =
StartsWith(L
"Hi");
2180 TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
2182 EXPECT_EQ(
"starts with L\"Hi\"", Describe(
m));
2187 TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
2188 const Matcher<const wchar_t*> m1 =
EndsWith(L
"");
2201 TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
2202 Matcher<const ::wstring>
m =
EndsWith(L
"Hi");
2206 #endif // GTEST_HAS_GLOBAL_WSTRING
2208 typedef ::std::tuple<long, int> Tuple2;
2212 TEST(Eq2Test, MatchesEqualArguments) {
2213 Matcher<const Tuple2&>
m = Eq();
2219 TEST(Eq2Test, CanDescribeSelf) {
2220 Matcher<const Tuple2&>
m = Eq();
2226 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
2227 Matcher<const Tuple2&>
m = Ge();
2234 TEST(Ge2Test, CanDescribeSelf) {
2235 Matcher<const Tuple2&>
m = Ge();
2236 EXPECT_EQ(
"are a pair where the first >= the second", Describe(
m));
2241 TEST(Gt2Test, MatchesGreaterThanArguments) {
2242 Matcher<const Tuple2&>
m = Gt();
2249 TEST(Gt2Test, CanDescribeSelf) {
2250 Matcher<const Tuple2&>
m = Gt();
2251 EXPECT_EQ(
"are a pair where the first > the second", Describe(
m));
2256 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
2257 Matcher<const Tuple2&>
m = Le();
2264 TEST(Le2Test, CanDescribeSelf) {
2265 Matcher<const Tuple2&>
m = Le();
2266 EXPECT_EQ(
"are a pair where the first <= the second", Describe(
m));
2271 TEST(Lt2Test, MatchesLessThanArguments) {
2272 Matcher<const Tuple2&>
m = Lt();
2279 TEST(Lt2Test, CanDescribeSelf) {
2280 Matcher<const Tuple2&>
m = Lt();
2281 EXPECT_EQ(
"are a pair where the first < the second", Describe(
m));
2286 TEST(Ne2Test, MatchesUnequalArguments) {
2287 Matcher<const Tuple2&>
m = Ne();
2294 TEST(Ne2Test, CanDescribeSelf) {
2295 Matcher<const Tuple2&>
m = Ne();
2296 EXPECT_EQ(
"are an unequal pair", Describe(
m));
2299 TEST(PairMatchBaseTest, WorksWithMoveOnly) {
2300 using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
2301 Matcher<Pointers> matcher = Eq();
2310 TEST(FloatEq2Test, MatchesEqualArguments) {
2311 typedef ::std::tuple<float, float> Tpl;
2312 Matcher<const Tpl&>
m = FloatEq();
2319 TEST(FloatEq2Test, CanDescribeSelf) {
2320 Matcher<const ::std::tuple<float, float>&>
m = FloatEq();
2321 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2326 TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {
2327 typedef ::std::tuple<float, float> Tpl;
2328 Matcher<const Tpl&>
m = NanSensitiveFloatEq();
2330 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2331 std::numeric_limits<float>::quiet_NaN())));
2333 EXPECT_FALSE(
m.Matches(Tpl(1.0
f, std::numeric_limits<float>::quiet_NaN())));
2334 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0
f)));
2338 TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
2339 Matcher<const ::std::tuple<float, float>&>
m = NanSensitiveFloatEq();
2340 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2345 TEST(DoubleEq2Test, MatchesEqualArguments) {
2346 typedef ::std::tuple<double, double> Tpl;
2347 Matcher<const Tpl&>
m = DoubleEq();
2354 TEST(DoubleEq2Test, CanDescribeSelf) {
2355 Matcher<const ::std::tuple<double, double>&>
m = DoubleEq();
2356 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2361 TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {
2362 typedef ::std::tuple<double, double> Tpl;
2363 Matcher<const Tpl&>
m = NanSensitiveDoubleEq();
2365 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2366 std::numeric_limits<double>::quiet_NaN())));
2368 EXPECT_FALSE(
m.Matches(Tpl(1.0
f, std::numeric_limits<double>::quiet_NaN())));
2369 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0
f)));
2373 TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
2374 Matcher<const ::std::tuple<double, double>&>
m = NanSensitiveDoubleEq();
2375 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2380 TEST(FloatNear2Test, MatchesEqualArguments) {
2381 typedef ::std::tuple<float, float> Tpl;
2382 Matcher<const Tpl&>
m = FloatNear(0.5
f);
2389 TEST(FloatNear2Test, CanDescribeSelf) {
2390 Matcher<const ::std::tuple<float, float>&>
m = FloatNear(0.5
f);
2391 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2396 TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {
2397 typedef ::std::tuple<float, float> Tpl;
2398 Matcher<const Tpl&>
m = NanSensitiveFloatNear(0.5
f);
2401 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2402 std::numeric_limits<float>::quiet_NaN())));
2404 EXPECT_FALSE(
m.Matches(Tpl(1.0
f, std::numeric_limits<float>::quiet_NaN())));
2405 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0
f)));
2409 TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
2410 Matcher<const ::std::tuple<float, float>&>
m = NanSensitiveFloatNear(0.5
f);
2411 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2416 TEST(DoubleNear2Test, MatchesEqualArguments) {
2417 typedef ::std::tuple<double, double> Tpl;
2418 Matcher<const Tpl&>
m = DoubleNear(0.5);
2425 TEST(DoubleNear2Test, CanDescribeSelf) {
2426 Matcher<const ::std::tuple<double, double>&>
m = DoubleNear(0.5);
2427 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2432 TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {
2433 typedef ::std::tuple<double, double> Tpl;
2434 Matcher<const Tpl&>
m = NanSensitiveDoubleNear(0.5
f);
2437 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2438 std::numeric_limits<double>::quiet_NaN())));
2440 EXPECT_FALSE(
m.Matches(Tpl(1.0
f, std::numeric_limits<double>::quiet_NaN())));
2441 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0
f)));
2445 TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
2446 Matcher<const ::std::tuple<double, double>&>
m = NanSensitiveDoubleNear(0.5
f);
2447 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2451 TEST(NotTest, NegatesMatcher) {
2459 TEST(NotTest, CanDescribeSelf) {
2460 Matcher<int>
m = Not(Eq(5));
2465 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
2467 Matcher<int> greater_than_5 = Gt(5);
2469 Matcher<const int&>
m = Not(greater_than_5);
2470 Matcher<int&> m2 = Not(greater_than_5);
2471 Matcher<int&> m3 = Not(
m);
2475 void AllOfMatches(
int num,
const Matcher<int>&
m) {
2478 for (
int i = 1;
i <= num; ++
i) {
2486 TEST(AllOfTest, MatchesWhenAllMatch) {
2488 m = AllOf(Le(2), Ge(1));
2494 m = AllOf(Gt(0), Ne(1), Ne(2));
2500 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2507 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2516 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
2517 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
2518 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
2519 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
2520 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
2521 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
2522 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2524 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2526 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2529 50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
2530 Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),
2531 Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),
2532 Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),
2533 Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),
2534 Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
2540 TEST(AllOfTest, CanDescribeSelf) {
2542 m = AllOf(Le(2), Ge(1));
2543 EXPECT_EQ(
"(is <= 2) and (is >= 1)", Describe(
m));
2545 m = AllOf(Gt(0), Ne(1), Ne(2));
2547 "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
2550 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2552 "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
2556 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2558 "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
2559 "and (isn't equal to 7)";
2564 TEST(AllOfTest, CanDescribeNegation) {
2566 m = AllOf(Le(2), Ge(1));
2567 std::string expected_descr4 =
"(isn't <= 2) or (isn't >= 1)";
2568 EXPECT_EQ(expected_descr4, DescribeNegation(
m));
2570 m = AllOf(Gt(0), Ne(1), Ne(2));
2572 "(isn't > 0) or (is equal to 1) or (is equal to 2)";
2573 EXPECT_EQ(expected_descr5, DescribeNegation(
m));
2575 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2577 "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
2578 EXPECT_EQ(expected_descr6, DescribeNegation(
m));
2580 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2582 "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
2584 EXPECT_EQ(expected_desr7, DescribeNegation(
m));
2586 m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
2588 AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2590 AllOfMatches(11,
m);
2594 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2596 Matcher<int> greater_than_5 = Gt(5);
2597 Matcher<int> less_than_10 = Lt(10);
2599 Matcher<const int&>
m = AllOf(greater_than_5, less_than_10);
2600 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2601 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2604 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2605 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2608 TEST(AllOfTest, ExplainsResult) {
2614 m = AllOf(GreaterThan(10), Lt(30));
2615 EXPECT_EQ(
"which is 15 more than 10", Explain(
m, 25));
2618 m = AllOf(GreaterThan(10), GreaterThan(20));
2619 EXPECT_EQ(
"which is 20 more than 10, and which is 10 more than 20",
2624 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2625 EXPECT_EQ(
"which is 15 more than 10, and which is 5 more than 20",
2629 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2630 EXPECT_EQ(
"which is 30 more than 10, and which is 20 more than 20, "
2631 "and which is 10 more than 30",
2636 m = AllOf(GreaterThan(10), GreaterThan(20));
2637 EXPECT_EQ(
"which is 5 less than 10", Explain(
m, 5));
2642 m = AllOf(GreaterThan(10), Lt(30));
2647 m = AllOf(GreaterThan(10), GreaterThan(20));
2648 EXPECT_EQ(
"which is 5 less than 20", Explain(
m, 15));
2652 static void AnyOfMatches(
int num,
const Matcher<int>&
m) {
2655 for (
int i = 1;
i <= num; ++
i) {
2661 static void AnyOfStringMatches(
int num,
const Matcher<std::string>&
m) {
2665 for (
int i = 1;
i <= num; ++
i) {
2673 TEST(AnyOfTest, MatchesWhenAnyMatches) {
2675 m = AnyOf(Le(1), Ge(3));
2680 m = AnyOf(Lt(0), Eq(1), Eq(2));
2686 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2693 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2703 AnyOfMatches(2, AnyOf(1, 2));
2704 AnyOfMatches(3, AnyOf(1, 2, 3));
2705 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2706 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2707 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2708 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2709 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2710 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2711 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
2715 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
2718 Matcher<int>
m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2721 AnyOfMatches(11,
m);
2722 AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2723 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
2724 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
2725 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2726 41, 42, 43, 44, 45, 46, 47, 48, 49, 50));
2728 50, AnyOf(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
2729 "13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
2730 "23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"32",
2731 "33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
"41",
"42",
2732 "43",
"44",
"45",
"46",
"47",
"48",
"49",
"50"));
2736 TEST(ElementsAreTest, HugeMatcher) {
2737 vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
2740 ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7),
2741 Eq(8), Eq(9), Eq(10), Gt(1), Eq(12)));
2745 TEST(ElementsAreTest, HugeMatcherStr) {
2746 vector<string> test_vector{
2747 "literal_string",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
2749 EXPECT_THAT(test_vector, UnorderedElementsAre(
"literal_string",
_,
_,
_,
_,
_,
2754 TEST(ElementsAreTest, HugeMatcherUnordered) {
2755 vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
2758 Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7),
2759 Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));
2764 TEST(AnyOfTest, CanDescribeSelf) {
2766 m = AnyOf(Le(1), Ge(3));
2771 m = AnyOf(Lt(0), Eq(1), Eq(2));
2772 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2)", Describe(
m));
2774 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2775 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
2778 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2780 "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
2786 TEST(AnyOfTest, CanDescribeNegation) {
2788 m = AnyOf(Le(1), Ge(3));
2789 EXPECT_EQ(
"(isn't <= 1) and (isn't >= 3)",
2790 DescribeNegation(
m));
2792 m = AnyOf(Lt(0), Eq(1), Eq(2));
2793 EXPECT_EQ(
"(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
2794 DescribeNegation(
m));
2796 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2798 "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
2800 DescribeNegation(
m));
2802 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2804 "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
2805 "to 5) and (isn't equal to 7)",
2806 DescribeNegation(
m));
2810 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2812 Matcher<int> greater_than_5 = Gt(5);
2813 Matcher<int> less_than_10 = Lt(10);
2815 Matcher<const int&>
m = AnyOf(greater_than_5, less_than_10);
2816 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2817 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2820 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2821 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2824 TEST(AnyOfTest, ExplainsResult) {
2830 m = AnyOf(GreaterThan(10), Lt(0));
2831 EXPECT_EQ(
"which is 5 less than 10", Explain(
m, 5));
2834 m = AnyOf(GreaterThan(10), GreaterThan(20));
2835 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20",
2840 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2841 EXPECT_EQ(
"which is 5 less than 10, and which is 25 less than 30",
2845 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2846 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20, "
2847 "and which is 25 less than 30",
2852 m = AnyOf(GreaterThan(10), GreaterThan(20));
2853 EXPECT_EQ(
"which is 5 more than 10", Explain(
m, 15));
2858 m = AnyOf(GreaterThan(10), Lt(30));
2863 m = AnyOf(GreaterThan(30), GreaterThan(20));
2864 EXPECT_EQ(
"which is 5 more than 20", Explain(
m, 25));
2874 int IsPositive(
double x) {
2875 return x > 0 ? 1 : 0;
2880 class IsGreaterThan {
2882 explicit IsGreaterThan(
int threshold) :
threshold_(threshold) {}
2884 bool operator()(
int n)
const {
return n >
threshold_; }
2895 bool ReferencesFooAndIsZero(
const int&
n) {
2896 return (&
n == &
foo) && (
n == 0);
2901 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2902 Matcher<double>
m = Truly(IsPositive);
2908 TEST(TrulyTest, CanBeUsedWithFunctor) {
2909 Matcher<int>
m = Truly(IsGreaterThan(5));
2915 class ConvertibleToBool {
2918 operator bool()
const {
return number_ != 0; }
2924 ConvertibleToBool IsNotZero(
int number) {
2925 return ConvertibleToBool(
number);
2931 TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2932 Matcher<int>
m = Truly(IsNotZero);
2938 TEST(TrulyTest, CanDescribeSelf) {
2939 Matcher<double>
m = Truly(IsPositive);
2940 EXPECT_EQ(
"satisfies the given predicate",
2946 TEST(TrulyTest, WorksForByRefArguments) {
2947 Matcher<const int&>
m = Truly(ReferencesFooAndIsZero);
2955 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2962 TEST(MatchesTest, WorksOnByRefArguments) {
2970 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2971 Matcher<int> eq5 = Eq(5);
2979 TEST(ValueTest, WorksWithPolymorphicMatcher) {
2984 TEST(ValueTest, WorksWithMonomorphicMatcher) {
2985 const Matcher<int> is_zero = Eq(0);
2990 const Matcher<const int&> ref_n = Ref(
n);
2995 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
2996 StringMatchResultListener listener1;
2997 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
3000 StringMatchResultListener listener2;
3001 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
3005 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
3006 const Matcher<int> is_even = PolymorphicIsEven();
3007 StringMatchResultListener listener1;
3008 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
3011 const Matcher<const double&> is_zero = Eq(0);
3012 StringMatchResultListener listener2;
3013 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
3018 return ExplainMatchResult(inner_matcher, arg, result_listener);
3021 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
3025 TEST(DescribeMatcherTest, WorksWithValue) {
3026 EXPECT_EQ(
"is equal to 42", DescribeMatcher<int>(42));
3027 EXPECT_EQ(
"isn't equal to 42", DescribeMatcher<int>(42,
true));
3030 TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
3031 const Matcher<int> monomorphic = Le(0);
3032 EXPECT_EQ(
"is <= 0", DescribeMatcher<int>(monomorphic));
3033 EXPECT_EQ(
"isn't <= 0", DescribeMatcher<int>(monomorphic,
true));
3036 TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
3037 EXPECT_EQ(
"is even", DescribeMatcher<int>(PolymorphicIsEven()));
3038 EXPECT_EQ(
"is odd", DescribeMatcher<int>(PolymorphicIsEven(),
true));
3041 TEST(AllArgsTest, WorksForTuple) {
3042 EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));
3043 EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));
3046 TEST(AllArgsTest, WorksForNonTuple) {
3051 class AllArgsHelper {
3061 TEST(AllArgsTest, WorksInWithClause) {
3062 AllArgsHelper helper;
3064 .With(AllArgs(Lt()))
3065 .WillByDefault(
Return(1));
3068 .With(AllArgs(Gt()))
3075 class OptionalMatchersHelper {
3077 OptionalMatchersHelper() {}
3092 TEST(AllArgsTest, WorksWithoutMatchers) {
3093 OptionalMatchersHelper helper;
3115 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
3118 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) <<
"This should succeed too.";
3124 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
3127 static unsigned short n;
3137 "Expected: is > 10\n"
3138 " Actual: 5" + OfType(
"unsigned short"));
3141 EXPECT_THAT(
n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
3143 "Expected: (is <= 7) and (is >= 5)\n"
3144 " Actual: 0" + OfType(
"unsigned short"));
3149 TEST(MatcherAssertionTest, WorksForByRefArguments) {
3157 "Expected: does not reference the variable @");
3160 "Actual: 0" + OfType(
"int") +
", which is located @");
3165 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
3166 Matcher<const char*> starts_with_he =
StartsWith(
"he");
3169 Matcher<const std::string&> ends_with_ok =
EndsWith(
"ok");
3174 "Expected: ends with \"ok\"\n"
3175 " Actual: \"bad\"");
3176 Matcher<int> is_greater_than_5 = Gt(5);
3179 "Expected: is > 5\n"
3180 " Actual: 5" + OfType(
"int"));
3184 template <
typename RawType>
3208 max_(Floating::Max()),
3209 nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
3210 nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {
3214 EXPECT_EQ(
sizeof(RawType),
sizeof(Bits));
3220 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
3221 Matcher<RawType> m1 = matcher_maker(0.0);
3230 Matcher<RawType> m3 = matcher_maker(1.0);
3237 Matcher<RawType> m4 = matcher_maker(-
infinity_);
3240 Matcher<RawType> m5 = matcher_maker(
infinity_);
3249 Matcher<const RawType&> m6 = matcher_maker(0.0);
3256 Matcher<RawType&> m7 = matcher_maker(0.0);
3294 template <
typename RawType>
3295 class FloatingPointNearTest :
public FloatingPointTest<RawType> {
3297 typedef FloatingPointTest<RawType> ParentType;
3301 void TestNearMatches(
3302 testing::internal::FloatingEqMatcher<RawType>
3303 (*matcher_maker)(RawType, RawType)) {
3304 Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
3311 Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
3350 Matcher<RawType> m9 = matcher_maker(
3356 Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
3363 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
3378 typedef FloatingPointTest<float> FloatTest;
3380 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
3381 TestMatches(&FloatEq);
3384 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
3385 TestMatches(&NanSensitiveFloatEq);
3388 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
3390 Matcher<float>
m = FloatEq(
nan1_);
3396 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
3398 Matcher<float>
m = NanSensitiveFloatEq(
nan1_);
3404 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
3405 Matcher<float> m1 = FloatEq(2.0
f);
3406 EXPECT_EQ(
"is approximately 2", Describe(m1));
3407 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3409 Matcher<float> m2 = FloatEq(0.5
f);
3410 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3411 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3413 Matcher<float> m3 = FloatEq(
nan1_);
3414 EXPECT_EQ(
"never matches", Describe(m3));
3415 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3418 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
3419 Matcher<float> m1 = NanSensitiveFloatEq(2.0
f);
3420 EXPECT_EQ(
"is approximately 2", Describe(m1));
3421 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3423 Matcher<float> m2 = NanSensitiveFloatEq(0.5
f);
3424 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3425 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3427 Matcher<float> m3 = NanSensitiveFloatEq(
nan1_);
3429 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3434 typedef FloatingPointNearTest<float> FloatNearTest;
3436 TEST_F(FloatNearTest, FloatNearMatches) {
3437 TestNearMatches(&FloatNear);
3440 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
3441 TestNearMatches(&NanSensitiveFloatNear);
3444 TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
3445 Matcher<float> m1 = FloatNear(2.0
f, 0.5
f);
3446 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3448 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3450 Matcher<float> m2 = FloatNear(0.5
f, 0.5
f);
3451 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3453 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3455 Matcher<float> m3 = FloatNear(
nan1_, 0.0);
3456 EXPECT_EQ(
"never matches", Describe(m3));
3457 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3460 TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
3461 Matcher<float> m1 = NanSensitiveFloatNear(2.0
f, 0.5
f);
3462 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3464 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3466 Matcher<float> m2 = NanSensitiveFloatNear(0.5
f, 0.5
f);
3467 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3469 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3471 Matcher<float> m3 = NanSensitiveFloatNear(
nan1_, 0.1
f);
3473 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3476 TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
3484 TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
3486 Matcher<float>
m = NanSensitiveFloatNear(
nan1_, 0.1
f);
3493 typedef FloatingPointTest<double> DoubleTest;
3495 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
3496 TestMatches(&DoubleEq);
3499 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
3500 TestMatches(&NanSensitiveDoubleEq);
3503 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
3505 Matcher<double>
m = DoubleEq(
nan1_);
3511 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
3513 Matcher<double>
m = NanSensitiveDoubleEq(
nan1_);
3519 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
3520 Matcher<double> m1 = DoubleEq(2.0);
3521 EXPECT_EQ(
"is approximately 2", Describe(m1));
3522 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3524 Matcher<double> m2 = DoubleEq(0.5);
3525 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3526 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3528 Matcher<double> m3 = DoubleEq(
nan1_);
3529 EXPECT_EQ(
"never matches", Describe(m3));
3530 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3533 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
3534 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
3535 EXPECT_EQ(
"is approximately 2", Describe(m1));
3536 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3538 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
3539 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3540 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3542 Matcher<double> m3 = NanSensitiveDoubleEq(
nan1_);
3544 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3549 typedef FloatingPointNearTest<double> DoubleNearTest;
3551 TEST_F(DoubleNearTest, DoubleNearMatches) {
3552 TestNearMatches(&DoubleNear);
3555 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
3556 TestNearMatches(&NanSensitiveDoubleNear);
3559 TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
3560 Matcher<double> m1 = DoubleNear(2.0, 0.5);
3561 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3563 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3565 Matcher<double> m2 = DoubleNear(0.5, 0.5);
3566 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3568 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3570 Matcher<double> m3 = DoubleNear(
nan1_, 0.0);
3571 EXPECT_EQ(
"never matches", Describe(m3));
3572 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3575 TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {
3576 EXPECT_EQ(
"", Explain(DoubleNear(2.0, 0.1), 2.05));
3577 EXPECT_EQ(
"which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2));
3578 EXPECT_EQ(
"which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7));
3581 Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);
3584 EXPECT_TRUE(explanation ==
"which is 1.2e-10 from 2.1" ||
3585 explanation ==
"which is 1.2e-010 from 2.1")
3586 <<
" where explanation is \"" << explanation <<
"\".";
3589 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
3590 Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);
3591 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3593 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3595 Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);
3596 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3598 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3600 Matcher<double> m3 = NanSensitiveDoubleNear(
nan1_, 0.1);
3602 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3605 TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
3613 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
3615 Matcher<double>
m = NanSensitiveDoubleNear(
nan1_, 0.1);
3621 TEST(PointeeTest, RawPointer) {
3622 const Matcher<int*>
m = Pointee(Ge(0));
3631 TEST(PointeeTest, RawPointerToConst) {
3632 const Matcher<const double*>
m = Pointee(Ge(0));
3641 TEST(PointeeTest, ReferenceToConstRawPointer) {
3642 const Matcher<int* const &>
m = Pointee(Ge(0));
3651 TEST(PointeeTest, ReferenceToNonConstRawPointer) {
3652 const Matcher<double* &>
m = Pointee(Ge(0));
3663 MATCHER_P(FieldIIs, inner_matcher,
"") {
3664 return ExplainMatchResult(inner_matcher, arg.i, result_listener);
3668 TEST(WhenDynamicCastToTest, SameType) {
3673 Base* as_base_ptr = &derived;
3675 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));
3677 Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));
3680 TEST(WhenDynamicCastToTest, WrongTypes) {
3683 OtherDerived other_derived;
3688 Base* as_base_ptr = &derived;
3689 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(
_))));
3691 as_base_ptr = &other_derived;
3692 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(
_))));
3696 TEST(WhenDynamicCastToTest, AlreadyNull) {
3698 Base* as_base_ptr =
nullptr;
3702 struct AmbiguousCastTypes {
3703 class VirtualDerived :
public virtual Base {};
3704 class DerivedSub1 :
public VirtualDerived {};
3705 class DerivedSub2 :
public VirtualDerived {};
3706 class ManyDerivedInHierarchy :
public DerivedSub1,
public DerivedSub2 {};
3709 TEST(WhenDynamicCastToTest, AmbiguousCast) {
3710 AmbiguousCastTypes::DerivedSub1 sub1;
3711 AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
3714 static_cast<AmbiguousCastTypes::DerivedSub1*
>(&many_derived);
3716 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(
IsNull()));
3717 as_base_ptr = &sub1;
3720 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(
IsNull())));
3723 TEST(WhenDynamicCastToTest, Describe) {
3724 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(
_));
3726 "when dynamic_cast to " + internal::GetTypeName<Derived*>() +
", ";
3727 EXPECT_EQ(
prefix +
"points to a value that is anything", Describe(matcher));
3729 DescribeNegation(matcher));
3732 TEST(WhenDynamicCastToTest, Explain) {
3733 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(
_));
3734 Base*
null =
nullptr;
3735 EXPECT_THAT(Explain(matcher,
null), HasSubstr(
"NULL"));
3738 EXPECT_THAT(Explain(matcher, &derived), HasSubstr(
"which points to "));
3741 Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(
_);
3743 HasSubstr(
"which cannot be dynamic_cast"));
3746 TEST(WhenDynamicCastToTest, GoodReference) {
3749 Base& as_base_ref = derived;
3750 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
3751 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));
3754 TEST(WhenDynamicCastToTest, BadReference) {
3756 Base& as_base_ref = derived;
3757 EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(
_)));
3759 #endif // GTEST_HAS_RTTI
3762 template <
typename T>
3763 class ConstPropagatingPtr {
3765 typedef T element_type;
3767 ConstPropagatingPtr() :
val_() {}
3768 explicit ConstPropagatingPtr(
T* t) :
val_(t) {}
3769 ConstPropagatingPtr(
const ConstPropagatingPtr& other) :
val_(other.
val_) {}
3774 const T*
get()
const {
return val_; }
3781 TEST(PointeeTest, WorksWithConstPropagatingPointers) {
3782 const Matcher< ConstPropagatingPtr<int> >
m = Pointee(Lt(5));
3784 const ConstPropagatingPtr<int> co(&three);
3785 ConstPropagatingPtr<int> o(&three);
3793 TEST(PointeeTest, NeverMatchesNull) {
3794 const Matcher<const char*>
m = Pointee(
_);
3799 TEST(PointeeTest, MatchesAgainstAValue) {
3800 const Matcher<int*>
m = Pointee(5);
3809 TEST(PointeeTest, CanDescribeSelf) {
3810 const Matcher<int*>
m = Pointee(Gt(3));
3811 EXPECT_EQ(
"points to a value that is > 3", Describe(
m));
3812 EXPECT_EQ(
"does not point to a value that is > 3",
3813 DescribeNegation(
m));
3816 TEST(PointeeTest, CanExplainMatchResult) {
3817 const Matcher<const std::string*>
m = Pointee(
StartsWith(
"Hi"));
3821 const Matcher<long*> m2 = Pointee(GreaterThan(1));
3823 EXPECT_EQ(
"which points to 3" + OfType(
"long") +
", which is 2 more than 1",
3827 TEST(PointeeTest, AlwaysExplainsPointee) {
3828 const Matcher<int*>
m = Pointee(0);
3830 EXPECT_EQ(
"which points to 42" + OfType(
"int"), Explain(
m, &
n));
3836 Uncopyable() :
value_(-1) {}
3837 explicit Uncopyable(
int a_value) :
value_(a_value) {}
3840 void set_value(
int i) {
value_ =
i; }
3848 bool ValueIsPositive(
const Uncopyable&
x) {
return x.value() > 0; }
3850 MATCHER_P(UncopyableIs, inner_matcher,
"") {
3851 return ExplainMatchResult(inner_matcher, arg.value(), result_listener);
3856 AStruct() :
x(0),
y(1.0),
z(5),
p(nullptr) {}
3857 AStruct(
const AStruct& rhs)
3870 struct DerivedStruct :
public AStruct {
3878 TEST(FieldTest, WorksForNonConstField) {
3891 TEST(FieldTest, WorksForConstField) {
3905 TEST(FieldTest, WorksForUncopyableField) {
3915 TEST(FieldTest, WorksForPointerField) {
3917 Matcher<AStruct>
m =
Field(&
AStruct::p,
static_cast<const char*
>(
nullptr));
3932 TEST(FieldTest, WorksForByRefArgument) {
3943 TEST(FieldTest, WorksForArgumentOfSubType) {
3956 TEST(FieldTest, WorksForCompatibleMatcherType) {
3959 Matcher<signed char>(Ge(0)));
3968 TEST(FieldTest, CanDescribeSelf) {
3971 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(
m));
3972 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(
m));
3975 TEST(FieldTest, CanDescribeSelfWithFieldName) {
3978 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(
m));
3979 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
3980 DescribeNegation(
m));
3984 TEST(FieldTest, CanExplainMatchResult) {
3989 EXPECT_EQ(
"whose given field is 1" + OfType(
"int"), Explain(
m,
a));
3993 "whose given field is 1" + OfType(
"int") +
", which is 1 more than 0",
3997 TEST(FieldTest, CanExplainMatchResultWithFieldName) {
4002 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int"), Explain(
m,
a));
4005 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int") +
4006 ", which is 1 more than 0",
4011 TEST(FieldForPointerTest, WorksForPointerToConst) {
4021 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
4031 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
4041 TEST(FieldForPointerTest, DoesNotMatchNull) {
4048 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
4060 TEST(FieldForPointerTest, CanDescribeSelf) {
4063 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(
m));
4064 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(
m));
4067 TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
4070 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(
m));
4071 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
4072 DescribeNegation(
m));
4076 TEST(FieldForPointerTest, CanExplainMatchResult) {
4081 EXPECT_EQ(
"", Explain(
m,
static_cast<const AStruct*
>(
nullptr)));
4082 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int"),
4086 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int") +
4087 ", which is 1 more than 0", Explain(
m, &
a));
4090 TEST(FieldForPointerTest, CanExplainMatchResultWithFieldName) {
4095 EXPECT_EQ(
"", Explain(
m,
static_cast<const AStruct*
>(
nullptr)));
4097 "which points to an object whose field `field_name` is 1" + OfType(
"int"),
4101 EXPECT_EQ(
"which points to an object whose field `field_name` is 1" +
4102 OfType(
"int") +
", which is 1 more than 0",
4112 int n()
const {
return n_; }
4114 void set_n(
int new_n) {
n_ = new_n; }
4124 double&
x()
const {
return x_; }
4136 class DerivedClass :
public AClass {
4138 int k()
const {
return k_; }
4145 TEST(PropertyTest, WorksForNonReferenceProperty) {
4146 Matcher<const AClass&>
m = Property(&
AClass::n, Ge(0));
4147 Matcher<const AClass&> m_with_name = Property(
"n", &
AClass::n, Ge(0));
4161 TEST(PropertyTest, WorksForReferenceToConstProperty) {
4162 Matcher<const AClass&>
m = Property(&AClass::s,
StartsWith(
"hi"));
4163 Matcher<const AClass&> m_with_name =
4178 TEST(PropertyTest, WorksForRefQualifiedProperty) {
4179 Matcher<const AClass&>
m = Property(&AClass::s_ref,
StartsWith(
"hi"));
4180 Matcher<const AClass&> m_with_name =
4181 Property(
"s", &AClass::s_ref,
StartsWith(
"hi"));
4195 TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
4199 Matcher<const AClass&>
m = Property(&
AClass::x, Ref(
x));
4208 TEST(PropertyTest, WorksForByValueArgument) {
4209 Matcher<AClass>
m = Property(&AClass::s,
StartsWith(
"hi"));
4221 TEST(PropertyTest, WorksForArgumentOfSubType) {
4224 Matcher<const DerivedClass&>
m = Property(&
AClass::n, Ge(0));
4236 TEST(PropertyTest, WorksForCompatibleMatcherType) {
4238 Matcher<const AClass&>
m = Property(&
AClass::n,
4239 Matcher<signed char>(Ge(0)));
4241 Matcher<const AClass&> m_with_name =
4242 Property(
"n", &
AClass::n, Matcher<signed char>(Ge(0)));
4253 TEST(PropertyTest, CanDescribeSelf) {
4254 Matcher<const AClass&>
m = Property(&
AClass::n, Ge(0));
4256 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(
m));
4257 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4258 DescribeNegation(
m));
4261 TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
4262 Matcher<const AClass&>
m = Property(
"fancy_name", &
AClass::n, Ge(0));
4264 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(
m));
4265 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4266 DescribeNegation(
m));
4270 TEST(PropertyTest, CanExplainMatchResult) {
4271 Matcher<const AClass&>
m = Property(&
AClass::n, Ge(0));
4275 EXPECT_EQ(
"whose given property is 1" + OfType(
"int"), Explain(
m,
a));
4279 "whose given property is 1" + OfType(
"int") +
", which is 1 more than 0",
4283 TEST(PropertyTest, CanExplainMatchResultWithPropertyName) {
4284 Matcher<const AClass&>
m = Property(
"fancy_name", &
AClass::n, Ge(0));
4288 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int"), Explain(
m,
a));
4290 m = Property(
"fancy_name", &
AClass::n, GreaterThan(0));
4291 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int") +
4292 ", which is 1 more than 0",
4297 TEST(PropertyForPointerTest, WorksForPointerToConst) {
4298 Matcher<const AClass*>
m = Property(&
AClass::n, Ge(0));
4309 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
4310 Matcher<AClass*>
m = Property(&AClass::s,
StartsWith(
"hi"));
4322 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
4323 Matcher<AClass* const&>
m = Property(&AClass::s,
StartsWith(
"hi"));
4334 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
4335 Matcher<const AClass*>
m = Property(&
AClass::x,
_);
4341 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
4344 Matcher<const DerivedClass*>
m = Property(&
AClass::n, Ge(0));
4355 TEST(PropertyForPointerTest, CanDescribeSelf) {
4356 Matcher<const AClass*>
m = Property(&
AClass::n, Ge(0));
4358 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(
m));
4359 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4360 DescribeNegation(
m));
4363 TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
4364 Matcher<const AClass*>
m = Property(
"fancy_name", &
AClass::n, Ge(0));
4366 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(
m));
4367 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4368 DescribeNegation(
m));
4372 TEST(PropertyForPointerTest, CanExplainMatchResult) {
4373 Matcher<const AClass*>
m = Property(&
AClass::n, Ge(0));
4377 EXPECT_EQ(
"", Explain(
m,
static_cast<const AClass*
>(
nullptr)));
4379 "which points to an object whose given property is 1" + OfType(
"int"),
4383 EXPECT_EQ(
"which points to an object whose given property is 1" +
4384 OfType(
"int") +
", which is 1 more than 0",
4388 TEST(PropertyForPointerTest, CanExplainMatchResultWithPropertyName) {
4389 Matcher<const AClass*>
m = Property(
"fancy_name", &
AClass::n, Ge(0));
4393 EXPECT_EQ(
"", Explain(
m,
static_cast<const AClass*
>(
nullptr)));
4394 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4398 m = Property(
"fancy_name", &
AClass::n, GreaterThan(0));
4399 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4400 OfType(
"int") +
", which is 1 more than 0",
4409 return input == 1 ?
"foo" :
"bar";
4412 TEST(ResultOfTest, WorksForFunctionPointers) {
4413 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(
std::string(
"foo")));
4420 TEST(ResultOfTest, CanDescribeItself) {
4421 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq(
"foo"));
4423 EXPECT_EQ(
"is mapped by the given callable to a value that "
4424 "is equal to \"foo\"", Describe(matcher));
4425 EXPECT_EQ(
"is mapped by the given callable to a value that "
4426 "isn't equal to \"foo\"", DescribeNegation(matcher));
4430 int IntFunction(
int input) {
return input == 42 ? 80 : 90; }
4432 TEST(ResultOfTest, CanExplainMatchResult) {
4433 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
4434 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int"),
4435 Explain(matcher, 36));
4437 matcher = ResultOf(&IntFunction, GreaterThan(85));
4438 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int") +
4439 ", which is 5 more than 85", Explain(matcher, 36));
4444 TEST(ResultOfTest, WorksForNonReferenceResults) {
4445 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
4453 double& DoubleFunction(
double&
input) {
return input; }
4455 Uncopyable& RefUncopyableFunction(Uncopyable&
obj) {
4459 TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
4462 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(
x));
4470 Matcher<Uncopyable&> matcher2 =
4471 ResultOf(&RefUncopyableFunction, Ref(
obj));
4481 TEST(ResultOfTest, WorksForReferenceToConstResults) {
4484 Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s));
4492 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
4494 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
4502 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
4506 "NULL function pointer is passed into ResultOf\\(\\)\\.");
4511 TEST(ResultOfTest, WorksForFunctionReferences) {
4512 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq(
"foo"));
4519 struct Functor :
public ::std::unary_function<int, std::string> {
4521 return IntToStringFunction(
input);
4525 TEST(ResultOfTest, WorksForFunctors) {
4526 Matcher<int> matcher = ResultOf(Functor(), Eq(
std::string(
"foo")));
4535 struct PolymorphicFunctor {
4537 int operator()(
int n) {
return n; }
4538 int operator()(
const char* s) {
return static_cast<int>(strlen(s)); }
4539 std::string operator()(
int *
p) {
return p ?
"good ptr" :
"null"; }
4542 TEST(ResultOfTest, WorksForPolymorphicFunctors) {
4543 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
4548 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
4550 EXPECT_TRUE(matcher_string.Matches(
"long string"));
4554 TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
4555 Matcher<int*> matcher = ResultOf(PolymorphicFunctor(),
"good ptr");
4562 TEST(ResultOfTest, WorksForLambdas) {
4563 Matcher<int> matcher =
4564 ResultOf([](
int str_len) {
return std::string(str_len,
'x'); },
"xxx");
4569 const int* ReferencingFunction(
const int&
n) {
return &
n; }
4571 struct ReferencingFunctor {
4576 TEST(ResultOfTest, WorksForReferencingCallables) {
4579 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&
n));
4583 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&
n));
4588 class DivisibleByImpl {
4590 explicit DivisibleByImpl(
int a_divider) :
divider_(a_divider) {}
4593 template <
typename T>
4594 bool MatchAndExplain(
const T&
n, MatchResultListener* listener)
const {
4595 *listener <<
"which is " << (
n %
divider_) <<
" modulo "
4600 void DescribeTo(ostream* os)
const {
4601 *os <<
"is divisible by " <<
divider_;
4604 void DescribeNegationTo(ostream* os)
const {
4605 *os <<
"is not divisible by " <<
divider_;
4608 void set_divider(
int a_divider) {
divider_ = a_divider; }
4609 int divider()
const {
return divider_; }
4615 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(
int n) {
4616 return MakePolymorphicMatcher(DivisibleByImpl(
n));
4621 TEST(ExplainMatchResultTest, AllOf_False_False) {
4622 const Matcher<int>
m = AllOf(DivisibleBy(4), DivisibleBy(3));
4623 EXPECT_EQ(
"which is 1 modulo 4", Explain(
m, 5));
4628 TEST(ExplainMatchResultTest, AllOf_False_True) {
4629 const Matcher<int>
m = AllOf(DivisibleBy(4), DivisibleBy(3));
4630 EXPECT_EQ(
"which is 2 modulo 4", Explain(
m, 6));
4635 TEST(ExplainMatchResultTest, AllOf_True_False) {
4636 const Matcher<int>
m = AllOf(Ge(1), DivisibleBy(3));
4637 EXPECT_EQ(
"which is 2 modulo 3", Explain(
m, 5));
4642 TEST(ExplainMatchResultTest, AllOf_True_True) {
4643 const Matcher<int>
m = AllOf(DivisibleBy(2), DivisibleBy(3));
4644 EXPECT_EQ(
"which is 0 modulo 2, and which is 0 modulo 3", Explain(
m, 6));
4647 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
4648 const Matcher<int>
m = AllOf(Ge(2), Le(3));
4652 TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
4653 const Matcher<int>
m = GreaterThan(5);
4654 EXPECT_EQ(
"which is 1 more than 5", Explain(
m, 6));
4663 explicit NotCopyable(
int a_value) :
value_(a_value) {}
4667 bool operator==(
const NotCopyable& rhs)
const {
4668 return value() == rhs.value();
4671 bool operator>=(
const NotCopyable& rhs)
const {
4672 return value() >= rhs.value();
4680 TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
4681 const NotCopyable const_value1(1);
4682 const Matcher<const NotCopyable&>
m = Eq(
ByRef(const_value1));
4684 const NotCopyable n1(1), n2(2);
4689 TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
4690 NotCopyable value2(2);
4691 const Matcher<NotCopyable&>
m = Ge(
ByRef(value2));
4693 NotCopyable n1(1), n2(2);
4698 TEST(IsEmptyTest, ImplementsIsEmpty) {
4699 vector<int> container;
4701 container.push_back(0);
4703 container.push_back(1);
4707 TEST(IsEmptyTest, WorksWithString) {
4716 TEST(IsEmptyTest, CanDescribeSelf) {
4717 Matcher<vector<int> >
m = IsEmpty();
4719 EXPECT_EQ(
"isn't empty", DescribeNegation(
m));
4722 TEST(IsEmptyTest, ExplainsResult) {
4723 Matcher<vector<int> >
m = IsEmpty();
4724 vector<int> container;
4726 container.push_back(0);
4727 EXPECT_EQ(
"whose size is 1", Explain(
m, container));
4730 TEST(IsEmptyTest, WorksWithMoveOnly) {
4731 ContainerHelper helper;
4736 TEST(IsTrueTest, IsTrueIsFalse) {
4764 std::unique_ptr<int> null_unique;
4765 std::unique_ptr<int> nonnull_unique(
new int(0));
4772 TEST(SizeIsTest, ImplementsSizeIs) {
4773 vector<int> container;
4776 container.push_back(0);
4779 container.push_back(0);
4784 TEST(SizeIsTest, WorksWithMap) {
4785 map<std::string, int> container;
4788 container.insert(make_pair(
"foo", 1));
4791 container.insert(make_pair(
"bar", 2));
4796 TEST(SizeIsTest, WorksWithReferences) {
4797 vector<int> container;
4798 Matcher<const vector<int>&>
m = SizeIs(1);
4800 container.push_back(0);
4804 TEST(SizeIsTest, WorksWithMoveOnly) {
4805 ContainerHelper helper;
4807 helper.Call(MakeUniquePtrs({1, 2, 3}));
4812 struct MinimalistCustomType {
4813 int size()
const {
return 1; }
4815 TEST(SizeIsTest, WorksWithMinimalistCustomType) {
4816 MinimalistCustomType container;
4821 TEST(SizeIsTest, CanDescribeSelf) {
4822 Matcher<vector<int> >
m = SizeIs(2);
4823 EXPECT_EQ(
"size is equal to 2", Describe(
m));
4824 EXPECT_EQ(
"size isn't equal to 2", DescribeNegation(
m));
4827 TEST(SizeIsTest, ExplainsResult) {
4828 Matcher<vector<int> > m1 = SizeIs(2);
4829 Matcher<vector<int> > m2 = SizeIs(Lt(2u));
4830 Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
4831 Matcher<vector<int> > m4 = SizeIs(GreaterThan(1));
4832 vector<int> container;
4833 EXPECT_EQ(
"whose size 0 doesn't match", Explain(m1, container));
4834 EXPECT_EQ(
"whose size 0 matches", Explain(m2, container));
4835 EXPECT_EQ(
"whose size 0 matches", Explain(m3, container));
4836 EXPECT_EQ(
"whose size 0 doesn't match, which is 1 less than 1",
4837 Explain(m4, container));
4838 container.push_back(0);
4839 container.push_back(0);
4840 EXPECT_EQ(
"whose size 2 matches", Explain(m1, container));
4841 EXPECT_EQ(
"whose size 2 doesn't match", Explain(m2, container));
4842 EXPECT_EQ(
"whose size 2 doesn't match", Explain(m3, container));
4843 EXPECT_EQ(
"whose size 2 matches, which is 1 more than 1",
4844 Explain(m4, container));
4847 #if GTEST_HAS_TYPED_TEST
4851 template <
typename T>
4854 typedef testing::Types<
4859 ContainerEqTestTypes;
4861 TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
4865 static const int vals[] = {1, 1, 2, 3, 5, 8};
4866 TypeParam my_set(vals, vals + 6);
4867 const Matcher<TypeParam>
m = ContainerEq(my_set);
4874 static const int vals[] = {1, 1, 2, 3, 5, 8};
4875 static const int test_vals[] = {2, 1, 8, 5};
4876 TypeParam my_set(vals, vals + 6);
4877 TypeParam test_set(test_vals, test_vals + 4);
4878 const Matcher<TypeParam>
m = ContainerEq(my_set);
4880 EXPECT_EQ(
"which doesn't have these expected elements: 3",
4881 Explain(
m, test_set));
4886 static const int vals[] = {1, 1, 2, 3, 5, 8};
4887 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
4888 TypeParam my_set(vals, vals + 6);
4889 TypeParam test_set(test_vals, test_vals + 6);
4890 const Matcher<const TypeParam&>
m = ContainerEq(my_set);
4892 EXPECT_EQ(
"which has these unexpected elements: 46", Explain(
m, test_set));
4896 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
4897 static const int vals[] = {1, 1, 2, 3, 5, 8};
4898 static const int test_vals[] = {1, 2, 3, 8, 46};
4899 TypeParam my_set(vals, vals + 6);
4900 TypeParam test_set(test_vals, test_vals + 5);
4901 const Matcher<TypeParam>
m = ContainerEq(my_set);
4903 EXPECT_EQ(
"which has these unexpected elements: 46,\n"
4904 "and doesn't have these expected elements: 5",
4905 Explain(
m, test_set));
4909 TYPED_TEST(ContainerEqTest, DuplicateDifference) {
4910 static const int vals[] = {1, 1, 2, 3, 5, 8};
4911 static const int test_vals[] = {1, 2, 3, 5, 8};
4912 TypeParam my_set(vals, vals + 6);
4913 TypeParam test_set(test_vals, test_vals + 5);
4914 const Matcher<const TypeParam&>
m = ContainerEq(my_set);
4919 #endif // GTEST_HAS_TYPED_TEST
4923 TEST(ContainerEqExtraTest, MultipleValuesMissing) {
4924 static const int vals[] = {1, 1, 2, 3, 5, 8};
4925 static const int test_vals[] = {2, 1, 5};
4926 vector<int> my_set(vals, vals + 6);
4927 vector<int> test_set(test_vals, test_vals + 3);
4928 const Matcher<vector<int> >
m = ContainerEq(my_set);
4930 EXPECT_EQ(
"which doesn't have these expected elements: 3, 8",
4931 Explain(
m, test_set));
4936 TEST(ContainerEqExtraTest, MultipleValuesAdded) {
4937 static const int vals[] = {1, 1, 2, 3, 5, 8};
4938 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
4939 list<size_t> my_set(vals, vals + 6);
4940 list<size_t> test_set(test_vals, test_vals + 7);
4941 const Matcher<const list<size_t>&>
m = ContainerEq(my_set);
4943 EXPECT_EQ(
"which has these unexpected elements: 92, 46",
4944 Explain(
m, test_set));
4948 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
4949 static const int vals[] = {1, 1, 2, 3, 5, 8};
4950 static const int test_vals[] = {1, 2, 3, 92, 46};
4951 list<size_t> my_set(vals, vals + 6);
4952 list<size_t> test_set(test_vals, test_vals + 5);
4953 const Matcher<const list<size_t> >
m = ContainerEq(my_set);
4955 EXPECT_EQ(
"which has these unexpected elements: 92, 46,\n"
4956 "and doesn't have these expected elements: 5, 8",
4957 Explain(
m, test_set));
4962 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
4963 static const int vals[] = {1, 1, 2, 3, 5, 8};
4964 static const int test_vals[] = {1, 2, 3, 5, 8};
4965 vector<int> my_set(vals, vals + 6);
4966 vector<int> test_set(test_vals, test_vals + 5);
4967 const Matcher<vector<int> >
m = ContainerEq(my_set);
4976 TEST(ContainerEqExtraTest, WorksForMaps) {
4977 map<int, std::string> my_map;
4981 map<int, std::string> test_map;
4985 const Matcher<const map<int, std::string>&>
m = ContainerEq(my_map);
4989 EXPECT_EQ(
"which has these unexpected elements: (0, \"aa\"),\n"
4990 "and doesn't have these expected elements: (0, \"a\")",
4991 Explain(
m, test_map));
4994 TEST(ContainerEqExtraTest, WorksForNativeArray) {
4995 int a1[] = {1, 2, 3};
4996 int a2[] = {1, 2, 3};
4997 int b[] = {1, 2, 4};
5003 TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
5004 const char a1[][3] = {
"hi",
"lo"};
5005 const char a2[][3] = {
"hi",
"lo"};
5006 const char b[][3] = {
"lo",
"hi"};
5013 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
5014 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(
b[0])), ContainerEq(a2[1])));
5017 TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
5018 const int a1[] = {1, 2, 3};
5019 const int a2[] = {1, 2, 3};
5020 const int b[] = {1, 2, 3, 4};
5022 const int*
const p1 = a1;
5023 EXPECT_THAT(std::make_tuple(p1, 3), ContainerEq(a2));
5024 EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(
b)));
5026 const int c[] = {1, 3, 2};
5027 EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(c)));
5030 TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
5032 {
"hi",
"hello",
"ciao"},
5033 {
"bye",
"see you",
"ciao"}
5037 {
"hi",
"hello",
"ciao"},
5038 {
"bye",
"see you",
"ciao"}
5041 const Matcher<
const std::string(&)[2][3]>
m = ContainerEq(a2);
5048 TEST(WhenSortedByTest, WorksForEmptyContainer) {
5049 const vector<int> numbers;
5050 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
5051 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
5054 TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
5055 vector<unsigned> numbers;
5056 numbers.push_back(3);
5057 numbers.push_back(1);
5058 numbers.push_back(2);
5059 numbers.push_back(2);
5060 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
5061 ElementsAre(3, 2, 2, 1)));
5062 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
5063 ElementsAre(1, 2, 2, 3))));
5066 TEST(WhenSortedByTest, WorksForNonVectorContainer) {
5067 list<std::string>
words;
5068 words.push_back(
"say");
5069 words.push_back(
"hello");
5070 words.push_back(
"world");
5072 ElementsAre(
"hello",
"say",
"world")));
5074 ElementsAre(
"say",
"hello",
"world"))));
5077 TEST(WhenSortedByTest, WorksForNativeArray) {
5078 const int numbers[] = {1, 3, 2, 4};
5079 const int sorted_numbers[] = {1, 2, 3, 4};
5080 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
5082 ElementsAreArray(sorted_numbers)));
5083 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
5086 TEST(WhenSortedByTest, CanDescribeSelf) {
5087 const Matcher<vector<int> >
m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
5088 EXPECT_EQ(
"(when sorted) has 2 elements where\n"
5089 "element #0 is equal to 1,\n"
5090 "element #1 is equal to 2",
5092 EXPECT_EQ(
"(when sorted) doesn't have 2 elements, or\n"
5093 "element #0 isn't equal to 1, or\n"
5094 "element #1 isn't equal to 2",
5095 DescribeNegation(
m));
5098 TEST(WhenSortedByTest, ExplainsMatchResult) {
5099 const int a[] = {2, 1};
5100 EXPECT_EQ(
"which is { 1, 2 } when sorted, whose element #0 doesn't match",
5101 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)),
a));
5102 EXPECT_EQ(
"which is { 1, 2 } when sorted",
5103 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)),
a));
5109 TEST(WhenSortedTest, WorksForEmptyContainer) {
5110 const vector<int> numbers;
5112 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
5115 TEST(WhenSortedTest, WorksForNonEmptyContainer) {
5116 list<std::string>
words;
5117 words.push_back(
"3");
5118 words.push_back(
"1");
5119 words.push_back(
"2");
5120 words.push_back(
"2");
5125 TEST(WhenSortedTest, WorksForMapTypes) {
5126 map<std::string, int> word_counts;
5127 word_counts[
"and"] = 1;
5128 word_counts[
"the"] = 1;
5129 word_counts[
"buffalo"] = 2;
5131 WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"buffalo", 2),
5134 Not(WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"the", 1),
5135 Pair(
"buffalo", 2)))));
5138 TEST(WhenSortedTest, WorksForMultiMapTypes) {
5139 multimap<int, int> ifib;
5140 ifib.insert(make_pair(8, 6));
5141 ifib.insert(make_pair(2, 3));
5142 ifib.insert(make_pair(1, 1));
5143 ifib.insert(make_pair(3, 4));
5144 ifib.insert(make_pair(1, 2));
5145 ifib.insert(make_pair(5, 5));
5146 EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1),
5152 EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6),
5160 TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
5165 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
5168 TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
5172 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
5174 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
5175 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
5180 template <
typename T>
5185 typedef ConstIter const_iterator;
5188 template <
typename InIter>
5191 const_iterator
begin()
const {
5192 return const_iterator(
this,
remainder_.begin());
5194 const_iterator
end()
const {
5195 return const_iterator(
this,
remainder_.end());
5199 class ConstIter :
public std::iterator<std::input_iterator_tag,
5203 const value_type&> {
5205 ConstIter(
const Streamlike* s,
5206 typename std::list<value_type>::iterator pos)
5211 ConstIter& operator++() {
5212 s_->remainder_.erase(
pos_++);
5218 class PostIncrProxy {
5225 PostIncrProxy operator++(
int) {
5226 PostIncrProxy
proxy(**
this);
5231 friend bool operator==(
const ConstIter&
a,
const ConstIter&
b) {
5232 return a.s_ ==
b.s_ &&
a.pos_ ==
b.pos_;
5234 friend bool operator!=(
const ConstIter&
a,
const ConstIter&
b) {
5239 const Streamlike*
s_;
5240 typename std::list<value_type>::iterator
pos_;
5243 friend std::ostream&
operator<<(std::ostream& os,
const Streamlike& s) {
5245 typedef typename std::list<value_type>::const_iterator
Iter;
5246 const char* sep =
"";
5247 for (
Iter it =
s.remainder_.begin();
it !=
s.remainder_.end(); ++
it) {
5258 TEST(StreamlikeTest, Iteration) {
5259 const int a[5] = {2, 1, 4, 5, 3};
5260 Streamlike<int>
s(
a,
a + 5);
5261 Streamlike<int>::const_iterator
it =
s.begin();
5263 while (
it !=
s.end()) {
5269 TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
5270 std::forward_list<int> container;
5272 EXPECT_THAT(container, Not(BeginEndDistanceIs(1)));
5273 container.push_front(0);
5274 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
5276 container.push_front(0);
5277 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
5281 TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
5282 const int a[5] = {1, 2, 3, 4, 5};
5283 Streamlike<int>
s(
a,
a + 5);
5287 TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
5288 Matcher<vector<int> >
m = BeginEndDistanceIs(2);
5289 EXPECT_EQ(
"distance between begin() and end() is equal to 2", Describe(
m));
5290 EXPECT_EQ(
"distance between begin() and end() isn't equal to 2",
5291 DescribeNegation(
m));
5294 TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
5295 ContainerHelper helper;
5297 helper.Call(MakeUniquePtrs({1, 2}));
5300 TEST(BeginEndDistanceIsTest, ExplainsResult) {
5301 Matcher<vector<int> > m1 = BeginEndDistanceIs(2);
5302 Matcher<vector<int> > m2 = BeginEndDistanceIs(Lt(2));
5303 Matcher<vector<int> > m3 = BeginEndDistanceIs(AnyOf(0, 3));
5304 Matcher<vector<int> > m4 = BeginEndDistanceIs(GreaterThan(1));
5305 vector<int> container;
5306 EXPECT_EQ(
"whose distance between begin() and end() 0 doesn't match",
5307 Explain(m1, container));
5308 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5309 Explain(m2, container));
5310 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5311 Explain(m3, container));
5313 "whose distance between begin() and end() 0 doesn't match, which is 1 "
5315 Explain(m4, container));
5316 container.push_back(0);
5317 container.push_back(0);
5318 EXPECT_EQ(
"whose distance between begin() and end() 2 matches",
5319 Explain(m1, container));
5320 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5321 Explain(m2, container));
5322 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5323 Explain(m3, container));
5325 "whose distance between begin() and end() 2 matches, which is 1 more "
5327 Explain(m4, container));
5330 TEST(WhenSortedTest, WorksForStreamlike) {
5333 const int a[5] = {2, 1, 4, 5, 3};
5335 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
5336 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
5339 TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
5340 const int a[] = {2, 1, 4, 5, 3};
5342 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
5344 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
5347 TEST(IsSupersetOfTest, WorksForNativeArray) {
5348 const int subset[] = {1, 4};
5349 const int superset[] = {1, 2, 4};
5350 const int disjoint[] = {1, 0, 3};
5358 TEST(IsSupersetOfTest, WorksWithDuplicates) {
5359 const int not_enough[] = {1, 2};
5360 const int enough[] = {1, 1, 2};
5361 const int expected[] = {1, 1};
5362 EXPECT_THAT(not_enough, Not(IsSupersetOf(expected)));
5366 TEST(IsSupersetOfTest, WorksForEmpty) {
5367 vector<int> numbers;
5368 vector<int> expected;
5370 expected.push_back(1);
5371 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
5373 numbers.push_back(1);
5374 numbers.push_back(2);
5376 expected.push_back(1);
5378 expected.push_back(2);
5380 expected.push_back(3);
5381 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
5384 TEST(IsSupersetOfTest, WorksForStreamlike) {
5385 const int a[5] = {1, 2, 3, 4, 5};
5388 vector<int> expected;
5389 expected.push_back(1);
5390 expected.push_back(2);
5391 expected.push_back(5);
5394 expected.push_back(0);
5398 TEST(IsSupersetOfTest, TakesStlContainer) {
5399 const int actual[] = {3, 1, 2};
5401 ::std::list<int> expected;
5402 expected.push_back(1);
5403 expected.push_back(3);
5406 expected.push_back(4);
5410 TEST(IsSupersetOfTest, Describe) {
5411 typedef std::vector<int> IntVec;
5413 expected.push_back(111);
5414 expected.push_back(222);
5415 expected.push_back(333);
5417 Describe<IntVec>(IsSupersetOf(expected)),
5418 Eq(
"a surjection from elements to requirements exists such that:\n"
5419 " - an element is equal to 111\n"
5420 " - an element is equal to 222\n"
5421 " - an element is equal to 333"));
5424 TEST(IsSupersetOfTest, DescribeNegation) {
5425 typedef std::vector<int> IntVec;
5427 expected.push_back(111);
5428 expected.push_back(222);
5429 expected.push_back(333);
5431 DescribeNegation<IntVec>(IsSupersetOf(expected)),
5432 Eq(
"no surjection from elements to requirements exists such that:\n"
5433 " - an element is equal to 111\n"
5434 " - an element is equal to 222\n"
5435 " - an element is equal to 333"));
5438 TEST(IsSupersetOfTest, MatchAndExplain) {
5442 std::vector<int> expected;
5443 expected.push_back(1);
5444 expected.push_back(2);
5445 StringMatchResultListener listener;
5446 ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected),
v, &listener))
5449 Eq(
"where the following matchers don't match any elements:\n"
5450 "matcher #0: is equal to 1"));
5454 ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected),
v, &listener))
5457 " - element #0 is matched by matcher #1,\n"
5458 " - element #2 is matched by matcher #0"));
5461 TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
5462 const int numbers[] = {1, 3, 6, 2, 4, 5};
5467 TEST(IsSupersetOfTest, WorksWithMoveOnly) {
5468 ContainerHelper helper;
5469 EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));
5470 helper.Call(MakeUniquePtrs({1, 2}));
5471 EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));
5472 helper.Call(MakeUniquePtrs({2}));
5475 TEST(IsSubsetOfTest, WorksForNativeArray) {
5476 const int subset[] = {1, 4};
5477 const int superset[] = {1, 2, 4};
5478 const int disjoint[] = {1, 0, 3};
5486 TEST(IsSubsetOfTest, WorksWithDuplicates) {
5487 const int not_enough[] = {1, 2};
5488 const int enough[] = {1, 1, 2};
5489 const int actual[] = {1, 1};
5494 TEST(IsSubsetOfTest, WorksForEmpty) {
5495 vector<int> numbers;
5496 vector<int> expected;
5498 expected.push_back(1);
5501 numbers.push_back(1);
5502 numbers.push_back(2);
5504 expected.push_back(1);
5506 expected.push_back(2);
5508 expected.push_back(3);
5512 TEST(IsSubsetOfTest, WorksForStreamlike) {
5513 const int a[5] = {1, 2};
5516 vector<int> expected;
5517 expected.push_back(1);
5519 expected.push_back(2);
5520 expected.push_back(5);
5524 TEST(IsSubsetOfTest, TakesStlContainer) {
5525 const int actual[] = {3, 1, 2};
5527 ::std::list<int> expected;
5528 expected.push_back(1);
5529 expected.push_back(3);
5532 expected.push_back(2);
5533 expected.push_back(4);
5537 TEST(IsSubsetOfTest, Describe) {
5538 typedef std::vector<int> IntVec;
5540 expected.push_back(111);
5541 expected.push_back(222);
5542 expected.push_back(333);
5545 Describe<IntVec>(IsSubsetOf(expected)),
5546 Eq(
"an injection from elements to requirements exists such that:\n"
5547 " - an element is equal to 111\n"
5548 " - an element is equal to 222\n"
5549 " - an element is equal to 333"));
5552 TEST(IsSubsetOfTest, DescribeNegation) {
5553 typedef std::vector<int> IntVec;
5555 expected.push_back(111);
5556 expected.push_back(222);
5557 expected.push_back(333);
5559 DescribeNegation<IntVec>(IsSubsetOf(expected)),
5560 Eq(
"no injection from elements to requirements exists such that:\n"
5561 " - an element is equal to 111\n"
5562 " - an element is equal to 222\n"
5563 " - an element is equal to 333"));
5566 TEST(IsSubsetOfTest, MatchAndExplain) {
5570 std::vector<int> expected;
5571 expected.push_back(1);
5572 expected.push_back(2);
5573 StringMatchResultListener listener;
5574 ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected),
v, &listener))
5577 Eq(
"where the following elements don't match any matchers:\n"
5580 expected.push_back(3);
5582 ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected),
v, &listener))
5585 " - element #0 is matched by matcher #1,\n"
5586 " - element #1 is matched by matcher #2"));
5589 TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
5590 const int numbers[] = {1, 2, 3};
5595 TEST(IsSubsetOfTest, WorksWithMoveOnly) {
5596 ContainerHelper helper;
5597 EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));
5598 helper.Call(MakeUniquePtrs({1}));
5599 EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));
5600 helper.Call(MakeUniquePtrs({2}));
5606 TEST(ElemensAreStreamTest, WorksForStreamlike) {
5607 const int a[5] = {1, 2, 3, 4, 5};
5613 TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
5614 const int a[5] = {1, 2, 3, 4, 5};
5617 vector<int> expected;
5618 expected.push_back(1);
5619 expected.push_back(2);
5620 expected.push_back(3);
5621 expected.push_back(4);
5622 expected.push_back(5);
5629 TEST(ElementsAreTest, WorksWithUncopyable) {
5631 objs[0].set_value(-3);
5632 objs[1].set_value(1);
5633 EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));
5636 TEST(ElementsAreTest, WorksWithMoveOnly) {
5637 ContainerHelper helper;
5638 EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));
5639 helper.Call(MakeUniquePtrs({1, 2}));
5641 EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));
5642 helper.Call(MakeUniquePtrs({3, 4}));
5645 TEST(ElementsAreTest, TakesStlContainer) {
5646 const int actual[] = {3, 1, 2};
5648 ::std::list<int> expected;
5649 expected.push_back(3);
5650 expected.push_back(1);
5651 expected.push_back(2);
5654 expected.push_back(4);
5655 EXPECT_THAT(actual, Not(ElementsAreArray(expected)));
5660 TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
5661 const int a[] = {0, 1, 2, 3, 4};
5664 StringMatchResultListener listener;
5665 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(
a),
5666 s, &listener)) << listener.str();
5667 }
while (std::next_permutation(
s.begin(),
s.end()));
5670 TEST(UnorderedElementsAreArrayTest, VectorBool) {
5671 const bool a[] = {0, 1, 0, 1, 1};
5672 const bool b[] = {1, 0, 1, 1, 0};
5675 StringMatchResultListener listener;
5676 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected),
5677 actual, &listener)) << listener.str();
5680 TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
5684 const int a[5] = {2, 1, 4, 5, 3};
5687 ::std::vector<int> expected;
5688 expected.push_back(1);
5689 expected.push_back(2);
5690 expected.push_back(3);
5691 expected.push_back(4);
5692 expected.push_back(5);
5693 EXPECT_THAT(s, UnorderedElementsAreArray(expected));
5695 expected.push_back(6);
5696 EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));
5699 TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
5700 const int actual[] = {3, 1, 2};
5702 ::std::list<int> expected;
5703 expected.push_back(1);
5704 expected.push_back(2);
5705 expected.push_back(3);
5706 EXPECT_THAT(actual, UnorderedElementsAreArray(expected));
5708 expected.push_back(4);
5709 EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected)));
5713 TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
5714 const int a[5] = {2, 1, 4, 5, 3};
5715 EXPECT_THAT(
a, UnorderedElementsAreArray({1, 2, 3, 4, 5}));
5716 EXPECT_THAT(
a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6})));
5719 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
5721 EXPECT_THAT(
a, UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
5722 EXPECT_THAT(
a, Not(UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
5725 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
5726 const int a[5] = {2, 1, 4, 5, 3};
5728 {Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
5730 {Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
5733 TEST(UnorderedElementsAreArrayTest,
5734 TakesInitializerListOfDifferentTypedMatchers) {
5735 const int a[5] = {2, 1, 4, 5, 3};
5739 EXPECT_THAT(
a, UnorderedElementsAreArray<Matcher<int> >(
5740 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
5741 EXPECT_THAT(
a, Not(UnorderedElementsAreArray<Matcher<int> >(
5742 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
5746 TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
5747 ContainerHelper helper;
5749 Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)})));
5750 helper.Call(MakeUniquePtrs({2, 1}));
5755 typedef std::vector<int> IntVec;
5758 TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
5760 objs[0].set_value(-3);
5761 objs[1].set_value(1);
5763 UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3)));
5766 TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
5767 const int a[] = {1, 2, 3};
5770 StringMatchResultListener listener;
5771 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
5772 s, &listener)) << listener.str();
5773 }
while (std::next_permutation(
s.begin(),
s.end()));
5776 TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
5777 const int a[] = {1, 2, 3};
5779 std::vector<Matcher<int> > mv;
5784 StringMatchResultListener listener;
5785 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv),
5786 s, &listener)) << listener.str();
5789 TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
5793 const int a[5] = {2, 1, 4, 5, 3};
5796 EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
5797 EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
5800 TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
5801 ContainerHelper helper;
5802 EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2))));
5803 helper.Call(MakeUniquePtrs({2, 1}));
5812 TEST_F(UnorderedElementsAreTest, Performance) {
5814 std::vector<Matcher<int> > mv;
5815 for (
int i = 0;
i < 100; ++
i) {
5820 StringMatchResultListener listener;
5821 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv),
5822 s, &listener)) << listener.str();
5828 TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
5830 std::vector<Matcher<int> > mv;
5831 for (
int i = 0;
i < 100; ++
i) {
5839 StringMatchResultListener listener;
5840 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv),
5841 s, &listener)) << listener.str();
5844 TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
5847 StringMatchResultListener listener;
5848 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
5849 v, &listener)) << listener.str();
5850 EXPECT_THAT(listener.str(), Eq(
"which has 1 element"));
5853 TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
5855 StringMatchResultListener listener;
5856 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
5857 v, &listener)) << listener.str();
5861 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
5865 StringMatchResultListener listener;
5866 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2),
5867 v, &listener)) << listener.str();
5870 Eq(
"where the following matchers don't match any elements:\n"
5871 "matcher #1: is equal to 2"));
5874 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
5878 StringMatchResultListener listener;
5879 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1),
5880 v, &listener)) << listener.str();
5883 Eq(
"where the following elements don't match any matchers:\n"
5887 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
5891 StringMatchResultListener listener;
5892 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2),
5893 v, &listener)) << listener.str();
5897 " the following matchers don't match any elements:\n"
5898 "matcher #0: is equal to 1\n"
5901 " the following elements don't match any matchers:\n"
5906 static std::string EMString(
int element,
int matcher) {
5908 ss <<
"(element #" << element <<
", matcher #" << matcher <<
")";
5912 TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
5915 std::vector<std::string>
v;
5919 StringMatchResultListener listener;
5921 UnorderedElementsAre(
"a",
"a", AnyOf(
"b",
"c")),
v, &listener))
5925 "where no permutation of the elements can satisfy all matchers, "
5926 "and the closest match is 2 of 3 matchers with the "
5932 AnyOf(
prefix +
"{\n " + EMString(0, 0) +
5933 ",\n " + EMString(1, 2) +
"\n}",
5934 prefix +
"{\n " + EMString(0, 1) +
5935 ",\n " + EMString(1, 2) +
"\n}",
5936 prefix +
"{\n " + EMString(0, 0) +
5937 ",\n " + EMString(2, 2) +
"\n}",
5938 prefix +
"{\n " + EMString(0, 1) +
5939 ",\n " + EMString(2, 2) +
"\n}"));
5942 TEST_F(UnorderedElementsAreTest, Describe) {
5943 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()),
5946 Describe<IntVec>(UnorderedElementsAre(345)),
5947 Eq(
"has 1 element and that element is equal to 345"));
5949 Describe<IntVec>(UnorderedElementsAre(111, 222, 333)),
5950 Eq(
"has 3 elements and there exists some permutation "
5951 "of elements such that:\n"
5952 " - element #0 is equal to 111, and\n"
5953 " - element #1 is equal to 222, and\n"
5954 " - element #2 is equal to 333"));
5957 TEST_F(UnorderedElementsAreTest, DescribeNegation) {
5958 EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()),
5961 DescribeNegation<IntVec>(UnorderedElementsAre(345)),
5962 Eq(
"doesn't have 1 element, or has 1 element that isn't equal to 345"));
5964 DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)),
5965 Eq(
"doesn't have 3 elements, or there exists no permutation "
5966 "of elements such that:\n"
5967 " - element #0 is equal to 123, and\n"
5968 " - element #1 is equal to 234, and\n"
5969 " - element #2 is equal to 345"));
5978 template <
typename Graph>
5979 class BacktrackingMaxBPMState {
5982 explicit BacktrackingMaxBPMState(
const Graph*
g) :
graph_(
g) { }
5984 ElementMatcherPairs Compute() {
5985 if (
graph_->LhsSize() == 0 ||
graph_->RhsSize() == 0) {
5990 for (
size_t irhs = 0; irhs <
graph_->RhsSize(); ++irhs) {
6000 static const size_t kUnused =
static_cast<size_t>(-1);
6002 void PushMatch(
size_t lhs,
size_t rhs) {
6003 matches_.push_back(ElementMatcherPair(lhs, rhs));
6012 const ElementMatcherPair& back =
matches_.back();
6018 bool RecurseInto(
size_t irhs) {
6022 for (
size_t ilhs = 0; ilhs <
graph_->LhsSize(); ++ilhs) {
6026 if (!
graph_->HasEdge(ilhs, irhs)) {
6029 PushMatch(ilhs, irhs);
6033 for (
size_t mi = irhs + 1; mi <
graph_->RhsSize(); ++mi) {
6034 if (!RecurseInto(mi))
return false;
6048 template <
typename Graph>
6055 template <
typename Graph>
6057 FindBacktrackingMaxBPM(
const Graph&
g) {
6058 return BacktrackingMaxBPMState<Graph>(&
g).Compute();
6068 TEST_P(BipartiteTest, Exhaustive) {
6069 int nodes = GetParam();
6070 MatchMatrix graph(nodes, nodes);
6072 ElementMatcherPairs matches =
6074 EXPECT_EQ(FindBacktrackingMaxBPM(graph).
size(), matches.size())
6075 <<
"graph: " << graph.DebugString();
6078 std::vector<bool> seen_element(graph.LhsSize());
6079 std::vector<bool> seen_matcher(graph.RhsSize());
6081 for (
size_t i = 0;
i < matches.size(); ++
i) {
6082 size_t ilhs = matches[
i].first;
6083 size_t irhs = matches[
i].second;
6087 seen_element[ilhs] =
true;
6088 seen_matcher[irhs] =
true;
6090 }
while (graph.NextGraph());
6097 class BipartiteNonSquareTest
6101 TEST_F(BipartiteNonSquareTest, SimpleBacktracking) {
6109 MatchMatrix
g(4, 3);
6110 static const int kEdges[][2] = {{0, 2}, {1, 1}, {2, 1}, {3, 0}};
6112 g.SetEdge(kEdges[
i][0], kEdges[
i][1],
true);
6115 ElementsAre(Pair(3, 0),
6116 Pair(AnyOf(1, 2), 1),
6117 Pair(0, 2))) <<
g.DebugString();
6121 TEST_P(BipartiteNonSquareTest, Exhaustive) {
6122 size_t nlhs = GetParam().first;
6123 size_t nrhs = GetParam().second;
6124 MatchMatrix graph(nlhs, nrhs);
6128 <<
"graph: " << graph.DebugString()
6129 <<
"\nbacktracking: "
6133 }
while (graph.NextGraph());
6138 std::make_pair(1, 2),
6139 std::make_pair(2, 1),
6140 std::make_pair(3, 2),
6141 std::make_pair(2, 3),
6142 std::make_pair(4, 1),
6143 std::make_pair(1, 4),
6144 std::make_pair(4, 3),
6145 std::make_pair(3, 4)));
6147 class BipartiteRandomTest
6152 TEST_P(BipartiteRandomTest, LargerNets) {
6153 int nodes = GetParam().first;
6154 int iters = GetParam().second;
6155 MatchMatrix graph(nodes, nodes);
6162 for (; iters > 0; --iters, ++seed) {
6163 srand(
static_cast<int>(seed));
6167 <<
" graph: " << graph.DebugString()
6168 <<
"\nTo reproduce the failure, rerun the test with the flag"
6176 std::make_pair(5, 10000),
6177 std::make_pair(6, 5000),
6178 std::make_pair(7, 2000),
6179 std::make_pair(8, 500),
6180 std::make_pair(9, 100)));
6184 TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
6186 EXPECT_TRUE(IsReadableTypeName(
"const unsigned char*"));
6187 EXPECT_TRUE(IsReadableTypeName(
"MyMap<int, void*>"));
6188 EXPECT_TRUE(IsReadableTypeName(
"void (*)(int, bool)"));
6191 TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
6192 EXPECT_TRUE(IsReadableTypeName(
"my_long_namespace::MyClassName"));
6193 EXPECT_TRUE(IsReadableTypeName(
"int [5][6][7][8][9][10][11]"));
6194 EXPECT_TRUE(IsReadableTypeName(
"my_namespace::MyOuterClass::MyInnerClass"));
6197 TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
6199 IsReadableTypeName(
"basic_string<char, std::char_traits<char> >"));
6200 EXPECT_FALSE(IsReadableTypeName(
"std::vector<int, std::alloc_traits<int> >"));
6203 TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
6204 EXPECT_FALSE(IsReadableTypeName(
"void (&)(int, bool, char, float)"));
6209 TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
6215 const char*
params[] = {
"5"};
6220 const char* params2[] = {
"5",
"8"};
6223 Strings(params2, params2 + 2)));
6227 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
6228 PolymorphicMatcher<DivisibleByImpl>
m(DivisibleByImpl(42));
6229 DivisibleByImpl& impl =
m.mutable_impl();
6232 impl.set_divider(0);
6237 TEST(PolymorphicMatcherTest, CanAccessImpl) {
6238 const PolymorphicMatcher<DivisibleByImpl>
m(DivisibleByImpl(42));
6239 const DivisibleByImpl& impl =
m.impl();
6243 TEST(MatcherTupleTest, ExplainsMatchFailure) {
6245 ExplainMatchFailureTupleTo(
6246 std::make_tuple(Matcher<char>(Eq(
'a')), GreaterThan(5)),
6247 std::make_tuple(
'a', 10), &ss1);
6251 ExplainMatchFailureTupleTo(
6252 std::make_tuple(GreaterThan(5), Matcher<char>(Eq(
'a'))),
6253 std::make_tuple(2,
'b'), &ss2);
6255 " Actual: 2, which is 3 less than 5\n"
6256 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
6257 " Actual: 'b' (98, 0x62)\n",
6261 ExplainMatchFailureTupleTo(
6262 std::make_tuple(GreaterThan(5), Matcher<char>(Eq(
'a'))),
6263 std::make_tuple(2,
'a'), &ss3);
6265 " Actual: 2, which is 3 less than 5\n",
6272 TEST(EachTest, ExplainsMatchResultCorrectly) {
6275 Matcher<set<int> >
m = Each(2);
6278 Matcher<
const int(&)[1]>
n = Each(1);
6280 const int b[1] = {1};
6284 EXPECT_EQ(
"whose element #0 doesn't match", Explain(
n,
b));
6289 m = Each(GreaterThan(0));
6292 m = Each(GreaterThan(10));
6293 EXPECT_EQ(
"whose element #0 doesn't match, which is 9 less than 10",
6297 TEST(EachTest, DescribesItselfCorrectly) {
6298 Matcher<vector<int> >
m = Each(1);
6299 EXPECT_EQ(
"only contains elements that is equal to 1", Describe(
m));
6301 Matcher<vector<int> > m2 = Not(
m);
6302 EXPECT_EQ(
"contains some element that isn't equal to 1", Describe(m2));
6305 TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
6306 vector<int> some_vector;
6308 some_vector.push_back(3);
6311 some_vector.push_back(1);
6312 some_vector.push_back(2);
6316 vector<std::string> another_vector;
6317 another_vector.push_back(
"fee");
6319 another_vector.push_back(
"fie");
6320 another_vector.push_back(
"foe");
6321 another_vector.push_back(
"fum");
6325 TEST(EachTest, MatchesMapWhenAllElementsMatch) {
6326 map<const char*, int> my_map;
6327 const char*
bar =
"a string";
6331 map<std::string, int> another_map;
6333 another_map[
"fee"] = 1;
6335 another_map[
"fie"] = 2;
6336 another_map[
"foe"] = 3;
6337 another_map[
"fum"] = 4;
6343 TEST(EachTest, AcceptsMatcher) {
6344 const int a[] = {1, 2, 3};
6349 TEST(EachTest, WorksForNativeArrayAsTuple) {
6350 const int a[] = {1, 2};
6356 TEST(EachTest, WorksWithMoveOnly) {
6357 ContainerHelper helper;
6359 helper.Call(MakeUniquePtrs({1, 2}));
6363 class IsHalfOfMatcher {
6365 template <
typename T1,
typename T2>
6366 bool MatchAndExplain(
const std::tuple<T1, T2>& a_pair,
6367 MatchResultListener* listener)
const {
6368 if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
6369 *listener <<
"where the second is " << std::get<1>(a_pair);
6372 *listener <<
"where the second/2 is " << std::get<1>(a_pair) / 2;
6377 void DescribeTo(ostream* os)
const {
6378 *os <<
"are a pair where the first is half of the second";
6381 void DescribeNegationTo(ostream* os)
const {
6382 *os <<
"are a pair where the first isn't half of the second";
6386 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
6387 return MakePolymorphicMatcher(IsHalfOfMatcher());
6390 TEST(PointwiseTest, DescribesSelf) {
6395 const Matcher<const vector<int>&>
m = Pointwise(IsHalfOf(), rhs);
6396 EXPECT_EQ(
"contains 3 values, where each value and its corresponding value "
6397 "in { 1, 2, 3 } are a pair where the first is half of the second",
6399 EXPECT_EQ(
"doesn't contain exactly 3 values, or contains a value x at some "
6400 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
6401 "where the first isn't half of the second",
6402 DescribeNegation(
m));
6405 TEST(PointwiseTest, MakesCopyOfRhs) {
6406 list<signed char> rhs;
6411 const Matcher<
const int (&)[2]>
m = Pointwise(IsHalfOf(), rhs);
6419 TEST(PointwiseTest, WorksForLhsNativeArray) {
6420 const int lhs[] = {1, 2, 3};
6429 TEST(PointwiseTest, WorksForRhsNativeArray) {
6430 const int rhs[] = {1, 2, 3};
6440 TEST(PointwiseTest, WorksForVectorOfBool) {
6441 vector<bool> rhs(3,
false);
6443 vector<bool> lhs = rhs;
6450 TEST(PointwiseTest, WorksForRhsInitializerList) {
6451 const vector<int> lhs{2, 4, 6};
6453 EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));
6457 TEST(PointwiseTest, RejectsWrongSize) {
6458 const double lhs[2] = {1, 2};
6459 const int rhs[1] = {0};
6462 Explain(Pointwise(Gt(), rhs), lhs));
6464 const int rhs2[3] = {0, 1, 2};
6468 TEST(PointwiseTest, RejectsWrongContent) {
6469 const double lhs[3] = {1, 2, 3};
6470 const int rhs[3] = {2, 6, 4};
6471 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
6472 EXPECT_EQ(
"where the value pair (2, 6) at index #1 don't match, "
6473 "where the second/2 is 3",
6474 Explain(Pointwise(IsHalfOf(), rhs), lhs));
6477 TEST(PointwiseTest, AcceptsCorrectContent) {
6478 const double lhs[3] = {1, 2, 3};
6479 const int rhs[3] = {2, 4, 6};
6481 EXPECT_EQ(
"", Explain(Pointwise(IsHalfOf(), rhs), lhs));
6484 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
6485 const double lhs[3] = {1, 2, 3};
6486 const int rhs[3] = {2, 4, 6};
6487 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6489 EXPECT_EQ(
"", Explain(Pointwise(m1, rhs), lhs));
6493 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6495 EXPECT_EQ(
"", Explain(Pointwise(m2, rhs), lhs));
6498 MATCHER(PointeeEquals,
"Points to an equal value") {
6499 return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),
6500 ::testing::get<0>(arg), result_listener);
6503 TEST(PointwiseTest, WorksWithMoveOnly) {
6504 ContainerHelper helper;
6505 EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));
6506 helper.Call(MakeUniquePtrs({1, 2}));
6509 TEST(UnorderedPointwiseTest, DescribesSelf) {
6514 const Matcher<const vector<int>&>
m = UnorderedPointwise(IsHalfOf(), rhs);
6516 "has 3 elements and there exists some permutation of elements such "
6518 " - element #0 and 1 are a pair where the first is half of the second, "
6520 " - element #1 and 2 are a pair where the first is half of the second, "
6522 " - element #2 and 3 are a pair where the first is half of the second",
6525 "doesn't have 3 elements, or there exists no permutation of elements "
6527 " - element #0 and 1 are a pair where the first is half of the second, "
6529 " - element #1 and 2 are a pair where the first is half of the second, "
6531 " - element #2 and 3 are a pair where the first is half of the second",
6532 DescribeNegation(
m));
6535 TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
6536 list<signed char> rhs;
6541 const Matcher<
const int (&)[2]>
m = UnorderedPointwise(IsHalfOf(), rhs);
6549 TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
6550 const int lhs[] = {1, 2, 3};
6556 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
6559 TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
6560 const int rhs[] = {1, 2, 3};
6566 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));
6570 TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
6571 const vector<int> lhs{2, 4, 6};
6572 EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3}));
6573 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));
6577 TEST(UnorderedPointwiseTest, RejectsWrongSize) {
6578 const double lhs[2] = {1, 2};
6579 const int rhs[1] = {0};
6580 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
6582 Explain(UnorderedPointwise(Gt(), rhs), lhs));
6584 const int rhs2[3] = {0, 1, 2};
6585 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2)));
6588 TEST(UnorderedPointwiseTest, RejectsWrongContent) {
6589 const double lhs[3] = {1, 2, 3};
6590 const int rhs[3] = {2, 6, 6};
6591 EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs)));
6592 EXPECT_EQ(
"where the following elements don't match any matchers:\n"
6594 Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs));
6597 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
6598 const double lhs[3] = {1, 2, 3};
6599 const int rhs[3] = {2, 4, 6};
6600 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
6603 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
6604 const double lhs[3] = {1, 2, 3};
6605 const int rhs[3] = {6, 4, 2};
6606 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
6609 TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
6610 const double lhs[3] = {1, 2, 3};
6611 const int rhs[3] = {4, 6, 2};
6612 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6617 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6621 TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
6622 ContainerHelper helper;
6623 EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),
6624 std::vector<int>{1, 2})));
6625 helper.Call(MakeUniquePtrs({2, 1}));
6630 template <
typename T>
6631 class SampleOptional {
6634 explicit SampleOptional(
T value)
6645 TEST(OptionalTest, DescribesSelf) {
6646 const Matcher<SampleOptional<int>>
m =
Optional(Eq(1));
6647 EXPECT_EQ(
"value is equal to 1", Describe(
m));
6650 TEST(OptionalTest, ExplainsSelf) {
6651 const Matcher<SampleOptional<int>>
m =
Optional(Eq(1));
6652 EXPECT_EQ(
"whose value 1 matches", Explain(
m, SampleOptional<int>(1)));
6653 EXPECT_EQ(
"whose value 2 doesn't match", Explain(
m, SampleOptional<int>(2)));
6656 TEST(OptionalTest, MatchesNonEmptyOptional) {
6657 const Matcher<SampleOptional<int>> m1 =
Optional(1);
6658 const Matcher<SampleOptional<int>> m2 =
Optional(Eq(2));
6659 const Matcher<SampleOptional<int>> m3 =
Optional(Lt(3));
6660 SampleOptional<int> opt(1);
6666 TEST(OptionalTest, DoesNotMatchNullopt) {
6667 const Matcher<SampleOptional<int>>
m =
Optional(1);
6668 SampleOptional<int> empty;
6672 TEST(OptionalTest, WorksWithMoveOnly) {
6673 Matcher<SampleOptional<std::unique_ptr<int>>>
m =
Optional(Eq(
nullptr));
6674 EXPECT_TRUE(
m.Matches(SampleOptional<std::unique_ptr<int>>(
nullptr)));
6677 class SampleVariantIntString {
6682 template <
typename T>
6683 friend bool holds_alternative(
const SampleVariantIntString&
value) {
6687 template <
typename T>
6688 friend const T&
get(
const SampleVariantIntString&
value) {
6689 return value.get_impl(
static_cast<T*
>(
nullptr));
6693 const int& get_impl(
int*)
const {
return i_; }
6701 TEST(VariantTest, DescribesSelf) {
6702 const Matcher<SampleVariantIntString>
m = VariantWith<int>(Eq(1));
6703 EXPECT_THAT(Describe(
m), ContainsRegex(
"is a variant<> with value of type "
6704 "'.*' and the value is equal to 1"));
6707 TEST(VariantTest, ExplainsSelf) {
6708 const Matcher<SampleVariantIntString>
m = VariantWith<int>(Eq(1));
6710 ContainsRegex(
"whose value 1"));
6712 HasSubstr(
"whose value is not of type '"));
6714 "whose value 2 doesn't match");
6717 TEST(VariantTest, FullMatch) {
6718 Matcher<SampleVariantIntString>
m = VariantWith<int>(Eq(1));
6721 m = VariantWith<std::string>(Eq(
"1"));
6725 TEST(VariantTest, TypeDoesNotMatch) {
6726 Matcher<SampleVariantIntString>
m = VariantWith<int>(Eq(1));
6729 m = VariantWith<std::string>(Eq(
"1"));
6733 TEST(VariantTest, InnerDoesNotMatch) {
6734 Matcher<SampleVariantIntString>
m = VariantWith<int>(Eq(1));
6737 m = VariantWith<std::string>(Eq(
"1"));
6741 class SampleAnyType {
6743 explicit SampleAnyType(
int i) :
index_(0),
i_(
i) {}
6746 template <
typename T>
6747 friend const T* any_cast(
const SampleAnyType* any) {
6748 return any->get_impl(
static_cast<T*
>(
nullptr));
6756 const int* get_impl(
int*)
const {
return index_ == 0 ? &
i_ :
nullptr; }
6758 return index_ == 1 ? &
s_ :
nullptr;
6762 TEST(AnyWithTest, FullMatch) {
6763 Matcher<SampleAnyType>
m = AnyWith<int>(Eq(1));
6767 TEST(AnyWithTest, TestBadCastType) {
6768 Matcher<SampleAnyType>
m = AnyWith<std::string>(Eq(
"fail"));
6772 TEST(AnyWithTest, TestUseInContainers) {
6773 std::vector<SampleAnyType>
a;
6778 a, ElementsAreArray({AnyWith<int>(1), AnyWith<int>(2), AnyWith<int>(3)}));
6780 std::vector<SampleAnyType>
b;
6781 b.emplace_back(
"hello");
6782 b.emplace_back(
"merhaba");
6783 b.emplace_back(
"salut");
6784 EXPECT_THAT(
b, ElementsAreArray({AnyWith<std::string>(
"hello"),
6785 AnyWith<std::string>(
"merhaba"),
6786 AnyWith<std::string>(
"salut")}));
6788 TEST(AnyWithTest, TestCompare) {
6789 EXPECT_THAT(SampleAnyType(1), AnyWith<int>(Gt(0)));
6792 TEST(AnyWithTest, DescribesSelf) {
6793 const Matcher<const SampleAnyType&>
m = AnyWith<int>(Eq(1));
6794 EXPECT_THAT(Describe(
m), ContainsRegex(
"is an 'any' type with value of type "
6795 "'.*' and the value is equal to 1"));
6798 TEST(AnyWithTest, ExplainsSelf) {
6799 const Matcher<const SampleAnyType&>
m = AnyWith<int>(Eq(1));
6801 EXPECT_THAT(Explain(
m, SampleAnyType(1)), ContainsRegex(
"whose value 1"));
6803 HasSubstr(
"whose value is not of type '"));
6804 EXPECT_THAT(Explain(
m, SampleAnyType(2)),
"whose value 2 doesn't match");
6807 TEST(PointeeTest, WorksOnMoveOnlyType) {
6808 std::unique_ptr<int>
p(
new int(3));
6813 TEST(NotTest, WorksOnMoveOnlyType) {
6814 std::unique_ptr<int>
p(
new int(3));
6821 TEST(ArgsTest, AcceptsZeroTemplateArg) {
6822 const std::tuple<int, bool> t(5,
true);
6827 TEST(ArgsTest, AcceptsOneTemplateArg) {
6828 const std::tuple<int, bool> t(5,
true);
6830 EXPECT_THAT(t, Args<1>(Eq(std::make_tuple(
true))));
6831 EXPECT_THAT(t, Not(Args<1>(Eq(std::make_tuple(
false)))));
6834 TEST(ArgsTest, AcceptsTwoTemplateArgs) {
6835 const std::tuple<short, int, long> t(4, 5, 6L);
6842 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
6843 const std::tuple<short, int, long> t(4, 5, 6L);
6848 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
6849 const std::tuple<short, int, long> t(4, 5, 6L);
6855 return std::get<0>(arg) + std::get<1>(arg) + std::get<2>(arg) == 0;
6858 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
6859 EXPECT_THAT(std::make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
6860 EXPECT_THAT(std::make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
6863 TEST(ArgsTest, CanBeNested) {
6864 const std::tuple<short, int, long, int> t(4, 5, 6L, 6);
6865 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
6866 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
6869 TEST(ArgsTest, CanMatchTupleByValue) {
6870 typedef std::tuple<char, int, int> Tuple3;
6871 const Matcher<Tuple3>
m = Args<1, 2>(Lt());
6876 TEST(ArgsTest, CanMatchTupleByReference) {
6877 typedef std::tuple<char, char, int> Tuple3;
6878 const Matcher<const Tuple3&>
m = Args<0, 1>(Lt());
6888 TEST(ArgsTest, AcceptsTenTemplateArgs) {
6889 EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
6890 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
6891 PrintsAs(
"(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
6892 EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
6893 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
6894 PrintsAs(
"(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
6897 TEST(ArgsTest, DescirbesSelfCorrectly) {
6898 const Matcher<std::tuple<int, bool, char> >
m = Args<2, 0>(Lt());
6899 EXPECT_EQ(
"are a tuple whose fields (#2, #0) are a pair where "
6900 "the first < the second",
6904 TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
6905 const Matcher<const std::tuple<int, bool, char, int>&>
m =
6906 Args<0, 2, 3>(Args<2, 0>(Lt()));
6907 EXPECT_EQ(
"are a tuple whose fields (#0, #2, #3) are a tuple "
6908 "whose fields (#2, #0) are a pair where the first < the second",
6912 TEST(ArgsTest, DescribesNegationCorrectly) {
6913 const Matcher<std::tuple<int, char> >
m = Args<1, 0>(Gt());
6914 EXPECT_EQ(
"are a tuple whose fields (#1, #0) aren't a pair "
6915 "where the first > the second",
6916 DescribeNegation(
m));
6919 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
6920 const Matcher<std::tuple<bool, int, int> >
m = Args<1, 2>(Eq());
6921 EXPECT_EQ(
"whose fields (#1, #2) are (42, 42)",
6922 Explain(
m, std::make_tuple(
false, 42, 42)));
6923 EXPECT_EQ(
"whose fields (#1, #2) are (42, 43)",
6924 Explain(
m, std::make_tuple(
false, 42, 43)));
6928 class LessThanMatcher :
public MatcherInterface<std::tuple<char, int> > {
6930 virtual void DescribeTo(::std::ostream* os)
const {}
6932 virtual bool MatchAndExplain(std::tuple<char, int>
value,
6933 MatchResultListener* listener)
const {
6934 const int diff = std::get<0>(
value) - std::get<1>(
value);
6936 *listener <<
"where the first value is " << diff
6937 <<
" more than the second";
6943 Matcher<std::tuple<char, int> > LessThan() {
6944 return MakeMatcher(
new LessThanMatcher);
6947 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
6948 const Matcher<std::tuple<char, int, int> >
m = Args<0, 2>(LessThan());
6950 "whose fields (#0, #2) are ('a' (97, 0x61), 42), "
6951 "where the first value is 55 more than the second",
6952 Explain(
m, std::make_tuple(
'a', 42, 42)));
6953 EXPECT_EQ(
"whose fields (#0, #2) are ('\\0', 43)",
6954 Explain(
m, std::make_tuple(
'\0', 42, 43)));
6959 enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
6964 class MockMatcher :
public MatcherInterface<Behavior> {
6966 bool MatchAndExplain(Behavior behavior,
6967 MatchResultListener* listener)
const override {
6968 *listener <<
"[MatchAndExplain]";
6970 case kInitialSuccess:
6974 return !listener->IsInterested();
6984 return listener->IsInterested();
6991 void DescribeTo(ostream* os)
const override { *os <<
"[DescribeTo]"; }
6993 void DescribeNegationTo(ostream* os)
const override {
6994 *os <<
"[DescribeNegationTo]";
6998 AssertionResult RunPredicateFormatter(Behavior behavior) {
6999 auto matcher = MakeMatcher(
new MockMatcher);
7000 PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
7002 return predicate_formatter(
"dummy-name", behavior);
7006 TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
7007 AssertionResult result = RunPredicateFormatter(kInitialSuccess);
7013 TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
7014 AssertionResult result = RunPredicateFormatter(kAlwaysFail);
7017 "Value of: dummy-name\nExpected: [DescribeTo]\n"
7018 " Actual: 1, [MatchAndExplain]";
7022 TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
7023 AssertionResult result = RunPredicateFormatter(kFlaky);
7026 "Value of: dummy-name\nExpected: [DescribeTo]\n"
7027 " The matcher failed on the initial attempt; but passed when rerun to "
7028 "generate the explanation.\n"
7029 " Actual: 2, [MatchAndExplain]";
7038 # pragma warning(pop)