38 # pragma warning(push)
39 # pragma warning(disable:4244)
40 # pragma warning(disable:4100)
51 #include <forward_list>
62 #include <type_traits>
63 #include <unordered_map>
64 #include <unordered_set>
74 namespace gmock_matchers_test {
87 using std::stringstream;
89 using testing::internal::DummyMatchResultListener;
90 using testing::internal::ElementMatcherPair;
91 using testing::internal::ElementMatcherPairs;
92 using testing::internal::ElementsAreArrayMatcher;
93 using testing::internal::ExplainMatchFailureTupleTo;
94 using testing::internal::FloatingEqMatcher;
96 using testing::internal::IsReadableTypeName;
97 using testing::internal::MatchMatrix;
98 using testing::internal::PredicateFormatterFromMatcher;
100 using testing::internal::StreamMatchResultListener;
106 struct ContainerHelper {
107 MOCK_METHOD1(Call,
void(std::vector<std::unique_ptr<int>>));
110 std::vector<std::unique_ptr<int>> MakeUniquePtrs(
const std::vector<int>& ints) {
111 std::vector<std::unique_ptr<int>> pointers;
112 for (
int i : ints) pointers.emplace_back(
new int(
i));
117 class GreaterThanMatcher :
public MatcherInterface<int> {
119 explicit GreaterThanMatcher(
int rhs) :
rhs_(rhs) {}
121 void DescribeTo(ostream* os)
const override { *os <<
"is > " <<
rhs_; }
123 bool MatchAndExplain(
int lhs, MatchResultListener* listener)
const override {
126 *listener <<
"which is " << diff <<
" more than " <<
rhs_;
127 }
else if (diff == 0) {
128 *listener <<
"which is the same as " <<
rhs_;
130 *listener <<
"which is " << -diff <<
" less than " <<
rhs_;
140 Matcher<int> GreaterThan(
int n) {
141 return MakeMatcher(
new GreaterThanMatcher(n));
144 std::string OfType(
const std::string& type_name) {
146 return IsReadableTypeName(type_name) ?
" (of type " + type_name +
")" :
"";
153 template <
typename T>
154 std::string Describe(
const Matcher<T>& m) {
155 return DescribeMatcher<T>(m);
159 template <
typename T>
160 std::string DescribeNegation(
const Matcher<T>& m) {
161 return DescribeMatcher<T>(m,
true);
165 template <
typename MatcherType,
typename Value>
166 std::string Explain(
const MatcherType& m,
const Value&
x) {
167 StringMatchResultListener listener;
168 ExplainMatchResult(m, x, &listener);
169 return listener.str();
172 TEST(MonotonicMatcherTest, IsPrintable) {
174 ss << GreaterThan(5);
178 TEST(MatchResultListenerTest, StreamingWorks) {
179 StringMatchResultListener listener;
180 listener <<
"hi" << 5;
190 DummyMatchResultListener dummy;
194 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
195 EXPECT_TRUE(DummyMatchResultListener().stream() ==
nullptr);
196 EXPECT_TRUE(StreamMatchResultListener(
nullptr).stream() ==
nullptr);
198 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
201 TEST(MatchResultListenerTest, IsInterestedWorks) {
202 EXPECT_TRUE(StringMatchResultListener().IsInterested());
203 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
205 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
206 EXPECT_FALSE(StreamMatchResultListener(
nullptr).IsInterested());
211 class EvenMatcherImpl :
public MatcherInterface<int> {
213 bool MatchAndExplain(
int x,
214 MatchResultListener* )
const override {
218 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
226 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
232 class NewEvenMatcherImpl :
public MatcherInterface<int> {
234 bool MatchAndExplain(
int x, MatchResultListener* listener)
const override {
235 const bool match = x % 2 == 0;
237 *listener <<
"value % " << 2;
238 if (listener->stream() !=
nullptr) {
241 *listener->stream() <<
" == " << (x % 2);
246 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
249 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
250 Matcher<int> m = MakeMatcher(
new NewEvenMatcherImpl);
253 EXPECT_EQ(
"value % 2 == 0", Explain(m, 2));
254 EXPECT_EQ(
"value % 2 == 1", Explain(m, 3));
258 TEST(MatcherTest, CanBeDefaultConstructed) {
263 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
264 const MatcherInterface<int>* impl =
new EvenMatcherImpl;
265 Matcher<int> m(impl);
271 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
278 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
279 Matcher<int*> m1 =
nullptr;
288 virtual ~Undefined() = 0;
292 TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
299 TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m =
_; }
302 TEST(MatcherTest, IsCopyable) {
304 Matcher<bool> m1 = Eq(
false);
316 TEST(MatcherTest, CanDescribeItself) {
318 Describe(Matcher<int>(
new EvenMatcherImpl)));
322 TEST(MatcherTest, MatchAndExplain) {
323 Matcher<int> m = GreaterThan(0);
324 StringMatchResultListener listener1;
326 EXPECT_EQ(
"which is 42 more than 0", listener1.str());
328 StringMatchResultListener listener2;
330 EXPECT_EQ(
"which is 9 less than 0", listener2.str());
335 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
336 Matcher<std::string> m1 =
"hi";
340 Matcher<const std::string&> m2 =
"hi";
347 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
348 Matcher<std::string> m1 = std::string(
"hi");
352 Matcher<const std::string&> m2 = std::string(
"hi");
357 #if GTEST_INTERNAL_HAS_STRING_VIEW
360 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
361 Matcher<internal::StringView> m1 =
"cats";
365 Matcher<const internal::StringView&> m2 =
"cats";
372 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
373 Matcher<internal::StringView> m1 = std::string(
"cats");
377 Matcher<const internal::StringView&> m2 = std::string(
"cats");
384 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
385 Matcher<internal::StringView> m1 = internal::StringView(
"cats");
389 Matcher<const internal::StringView&> m2 = internal::StringView(
"cats");
393 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
397 TEST(StringMatcherTest,
398 CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
399 std::string
value =
"cats";
400 Matcher<std::string> m1 = Eq(std::ref(value));
404 Matcher<const std::string&> m2 = Eq(std::ref(value));
412 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
413 const MatcherInterface<int>* dummy_impl =
nullptr;
414 Matcher<int> m = MakeMatcher(dummy_impl);
420 class ReferencesBarOrIsZeroImpl {
422 template <
typename T>
423 bool MatchAndExplain(
const T& x,
424 MatchResultListener* )
const {
426 return p == &g_bar || x == 0;
429 void DescribeTo(ostream* os)
const { *os <<
"g_bar or zero"; }
431 void DescribeNegationTo(ostream* os)
const {
432 *os <<
"doesn't reference g_bar and is not zero";
438 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
439 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
442 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
444 Matcher<const int&> m1 = ReferencesBarOrIsZero();
449 EXPECT_EQ(
"g_bar or zero", Describe(m1));
452 Matcher<double> m2 = ReferencesBarOrIsZero();
455 EXPECT_EQ(
"g_bar or zero", Describe(m2));
460 class PolymorphicIsEvenImpl {
462 void DescribeTo(ostream* os)
const { *os <<
"is even"; }
464 void DescribeNegationTo(ostream* os)
const {
468 template <
typename T>
469 bool MatchAndExplain(
const T& x, MatchResultListener* listener)
const {
471 *listener <<
"% " << 2;
472 if (listener->stream() !=
nullptr) {
475 *listener->stream() <<
" == " << (x % 2);
481 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
482 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
485 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
487 const Matcher<int> m1 = PolymorphicIsEven();
492 const Matcher<int> not_m1 = Not(m1);
498 const Matcher<char> m2 = PolymorphicIsEven();
503 const Matcher<char> not_m2 = Not(m2);
506 EXPECT_EQ(
"% 2 == 0", Explain(m2,
'\x42'));
510 TEST(MatcherCastTest, FromPolymorphicMatcher) {
511 Matcher<int> m = MatcherCast<int>(Eq(5));
521 explicit IntValue(
int a_value) :
value_(a_value) {}
529 bool IsPositiveIntValue(
const IntValue&
foo) {
530 return foo.value() > 0;
535 TEST(MatcherCastTest, FromCompatibleType) {
536 Matcher<double> m1 = Eq(2.0);
537 Matcher<int> m2 = MatcherCast<int>(m1);
541 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
542 Matcher<int> m4 = MatcherCast<int>(m3);
551 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
552 Matcher<const int&> m1 = Eq(0);
553 Matcher<int> m2 = MatcherCast<int>(m1);
559 TEST(MatcherCastTest, FromReferenceToNonReference) {
560 Matcher<int&> m1 = Eq(0);
561 Matcher<int> m2 = MatcherCast<int>(m1);
567 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
568 Matcher<int> m1 = Eq(0);
569 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
575 TEST(MatcherCastTest, FromNonReferenceToReference) {
576 Matcher<int> m1 = Eq(0);
577 Matcher<int&> m2 = MatcherCast<int&>(m1);
585 TEST(MatcherCastTest, FromSameType) {
586 Matcher<int> m1 = Eq(0);
587 Matcher<int> m2 = MatcherCast<int>(m1);
594 TEST(MatcherCastTest, FromAValue) {
595 Matcher<int> m = MatcherCast<int>(42);
602 TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
603 const int kExpected =
'c';
604 Matcher<int> m = MatcherCast<int>(
'c');
609 struct NonImplicitlyConstructibleTypeWithOperatorEq {
611 const NonImplicitlyConstructibleTypeWithOperatorEq& ,
617 const NonImplicitlyConstructibleTypeWithOperatorEq& ) {
625 TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
626 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
627 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
628 EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
630 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
631 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
632 EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
637 MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
647 #if !defined _MSC_VER
656 namespace convertible_from_any {
658 struct ConvertibleFromAny {
659 ConvertibleFromAny(
int a_value) :
value(a_value) {}
660 template <
typename T>
661 ConvertibleFromAny(
const T& ) :
value(-1) {
667 bool operator==(
const ConvertibleFromAny&
a,
const ConvertibleFromAny& b) {
668 return a.value == b.value;
671 ostream&
operator<<(ostream& os,
const ConvertibleFromAny&
a) {
672 return os << a.value;
675 TEST(MatcherCastTest, ConversionConstructorIsUsed) {
676 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
681 TEST(MatcherCastTest, FromConvertibleFromAny) {
682 Matcher<ConvertibleFromAny> m =
683 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
689 #endif // !defined _MSC_VER
691 struct IntReferenceWrapper {
692 IntReferenceWrapper(
const int& a_value) : value(&a_value) {}
696 bool operator==(
const IntReferenceWrapper&
a,
const IntReferenceWrapper& b) {
697 return a.value == b.value;
700 TEST(MatcherCastTest, ValueIsNotCopied) {
702 Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
715 class Derived :
public Base {
717 Derived() : Base() {}
721 class OtherDerived :
public Base {};
724 TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
725 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
733 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
734 Matcher<double> m1 = DoubleEq(1.0);
735 Matcher<float> m2 = SafeMatcherCast<float>(m1);
739 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>(
'a'));
746 TEST(SafeMatcherCastTest, FromBaseClass) {
748 Matcher<Base*> m1 = Eq(&d);
749 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
753 Matcher<Base&> m3 = Ref(d);
754 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
760 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
762 Matcher<const int&> m1 = Ref(n);
763 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
770 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
771 Matcher<std::unique_ptr<int>> m1 =
IsNull();
772 Matcher<const std::unique_ptr<int>&> m2 =
773 SafeMatcherCast<const std::unique_ptr<int>&>(m1);
775 EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(
new int)));
779 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
780 Matcher<int> m1 = Eq(0);
781 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
789 TEST(SafeMatcherCastTest, FromSameType) {
790 Matcher<int> m1 = Eq(0);
791 Matcher<int> m2 = SafeMatcherCast<int>(m1);
796 #if !defined _MSC_VER
798 namespace convertible_from_any {
799 TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
800 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
805 TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
806 Matcher<ConvertibleFromAny> m =
807 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
813 #endif // !defined _MSC_VER
815 TEST(SafeMatcherCastTest, ValueIsNotCopied) {
817 Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);
822 TEST(ExpectThat, TakesLiterals) {
828 TEST(ExpectThat, TakesFunctions) {
830 static void Func() {}
838 TEST(ATest, MatchesAnyValue) {
852 TEST(ATest, WorksForDerivedClass) {
862 TEST(ATest, CanDescribeSelf) {
867 TEST(AnTest, MatchesAnyValue) {
869 Matcher<int> m1 = An<int>();
876 Matcher<int&> m2 = An<int&>();
882 TEST(AnTest, CanDescribeSelf) {
883 EXPECT_EQ(
"is anything", Describe(An<int>()));
888 TEST(UnderscoreTest, MatchesAnyValue) {
897 Matcher<const bool&> m2 =
_;
903 TEST(UnderscoreTest, CanDescribeSelf) {
909 TEST(EqTest, MatchesEqualValue) {
911 const char a1[] =
"hi";
912 const char a2[] =
"hi";
914 Matcher<const char*> m1 = Eq(a1);
923 Unprintable() :
c_(
'a') {}
925 bool operator==(
const Unprintable& )
const {
return true; }
927 char dummy_c() {
return c_; }
932 TEST(EqTest, CanDescribeSelf) {
933 Matcher<Unprintable> m = Eq(Unprintable());
934 EXPECT_EQ(
"is equal to 1-byte object <61>", Describe(m));
939 TEST(EqTest, IsPolymorphic) {
940 Matcher<int> m1 = Eq(1);
944 Matcher<char> m2 = Eq(1);
950 TEST(TypedEqTest, ChecksEqualityForGivenType) {
951 Matcher<char> m1 = TypedEq<char>(
'a');
955 Matcher<int> m2 = TypedEq<int>(6);
961 TEST(TypedEqTest, CanDescribeSelf) {
962 EXPECT_EQ(
"is equal to 2", Describe(TypedEq<int>(2)));
970 template <
typename T>
972 static bool IsTypeOf(
const T& ) {
return true; }
974 template <
typename T2>
975 static void IsTypeOf(
T2 v);
978 TEST(TypedEqTest, HasSpecifiedType) {
980 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
981 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
985 TEST(GeTest, ImplementsGreaterThanOrEqual) {
986 Matcher<int> m1 = Ge(0);
993 TEST(GeTest, CanDescribeSelf) {
994 Matcher<int> m = Ge(5);
999 TEST(GtTest, ImplementsGreaterThan) {
1000 Matcher<double> m1 = Gt(0);
1007 TEST(GtTest, CanDescribeSelf) {
1008 Matcher<int> m = Gt(5);
1013 TEST(LeTest, ImplementsLessThanOrEqual) {
1014 Matcher<char> m1 = Le(
'b');
1021 TEST(LeTest, CanDescribeSelf) {
1022 Matcher<int> m = Le(5);
1027 TEST(LtTest, ImplementsLessThan) {
1028 Matcher<const std::string&> m1 = Lt(
"Hello");
1035 TEST(LtTest, CanDescribeSelf) {
1036 Matcher<int> m = Lt(5);
1041 TEST(NeTest, ImplementsNotEqual) {
1042 Matcher<int> m1 = Ne(0);
1049 TEST(NeTest, CanDescribeSelf) {
1050 Matcher<int> m = Ne(5);
1051 EXPECT_EQ(
"isn't equal to 5", Describe(m));
1056 explicit MoveOnly(
int i) :
i_(i) {}
1057 MoveOnly(
const MoveOnly&) =
delete;
1058 MoveOnly(MoveOnly&&) =
default;
1059 MoveOnly& operator=(
const MoveOnly&) =
delete;
1060 MoveOnly& operator=(MoveOnly&&) =
default;
1062 bool operator==(
const MoveOnly& other)
const {
return i_ == other.i_; }
1063 bool operator!=(
const MoveOnly& other)
const {
return i_ != other.i_; }
1064 bool operator<(
const MoveOnly& other)
const {
return i_ < other.i_; }
1065 bool operator<=(
const MoveOnly& other)
const {
return i_ <= other.i_; }
1066 bool operator>(
const MoveOnly& other)
const {
return i_ > other.i_; }
1067 bool operator>=(
const MoveOnly& other)
const {
return i_ >= other.i_; }
1077 TEST(ComparisonBaseTest, WorksWithMoveOnly) {
1082 helper.Call(MoveOnly(0));
1084 helper.Call(MoveOnly(1));
1086 helper.Call(MoveOnly(0));
1088 helper.Call(MoveOnly(-1));
1090 helper.Call(MoveOnly(0));
1092 helper.Call(MoveOnly(1));
1096 TEST(IsNullTest, MatchesNullPointer) {
1097 Matcher<int*> m1 =
IsNull();
1103 Matcher<const char*> m2 =
IsNull();
1104 const char* p2 =
nullptr;
1108 Matcher<void*> m3 =
IsNull();
1111 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
1114 TEST(IsNullTest, StdFunction) {
1115 const Matcher<std::function<void()>> m =
IsNull();
1122 TEST(IsNullTest, CanDescribeSelf) {
1123 Matcher<int*> m =
IsNull();
1125 EXPECT_EQ(
"isn't NULL", DescribeNegation(m));
1129 TEST(NotNullTest, MatchesNonNullPointer) {
1130 Matcher<int*> m1 = NotNull();
1136 Matcher<const char*> m2 = NotNull();
1137 const char* p2 =
nullptr;
1142 TEST(NotNullTest, LinkedPtr) {
1143 const Matcher<std::shared_ptr<int>> m = NotNull();
1144 const std::shared_ptr<int> null_p;
1145 const std::shared_ptr<int> non_null_p(
new int);
1151 TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1152 const Matcher<const std::shared_ptr<double>&> m = NotNull();
1153 const std::shared_ptr<double> null_p;
1154 const std::shared_ptr<double> non_null_p(
new double);
1160 TEST(NotNullTest, StdFunction) {
1161 const Matcher<std::function<void()>> m = NotNull();
1168 TEST(NotNullTest, CanDescribeSelf) {
1169 Matcher<int*> m = NotNull();
1175 TEST(RefTest, MatchesSameVariable) {
1178 Matcher<int&> m = Ref(a);
1184 TEST(RefTest, CanDescribeSelf) {
1186 Matcher<int&> m = Ref(n);
1188 ss <<
"references the variable @" << &n <<
" 5";
1194 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1197 Matcher<const int&> m = Ref(a);
1206 TEST(RefTest, IsCovariant) {
1209 Matcher<const Base&> m1 = Ref(base);
1220 TEST(RefTest, ExplainsResult) {
1222 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1223 StartsWith(
"which is located @"));
1226 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1227 StartsWith(
"which is located @"));
1232 template <
typename T = std::
string>
1233 std::string FromStringLike(internal::StringLike<T> str) {
1234 return std::string(str);
1237 TEST(StringLike, TestConversions) {
1238 EXPECT_EQ(
"foo", FromStringLike(
"foo"));
1239 EXPECT_EQ(
"foo", FromStringLike(std::string(
"foo")));
1240 #if GTEST_INTERNAL_HAS_STRING_VIEW
1241 EXPECT_EQ(
"foo", FromStringLike(internal::StringView(
"foo")));
1242 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1246 EXPECT_EQ(
"foo", FromStringLike({
'f',
'o',
'o'}));
1247 const char buf[] =
"foo";
1248 EXPECT_EQ(
"foo", FromStringLike({buf, buf + 3}));
1251 TEST(StrEqTest, MatchesEqualString) {
1252 Matcher<const char*> m = StrEq(std::string(
"Hello"));
1257 Matcher<const std::string&> m2 = StrEq(
"Hello");
1261 #if GTEST_INTERNAL_HAS_STRING_VIEW
1262 Matcher<const internal::StringView&> m3 =
1263 StrEq(internal::StringView(
"Hello"));
1264 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1265 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1268 Matcher<const internal::StringView&> m_empty = StrEq(
"");
1269 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1270 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1271 EXPECT_FALSE(m_empty.Matches(internal::StringView(
"hello")));
1272 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1275 TEST(StrEqTest, CanDescribeSelf) {
1276 Matcher<std::string> m = StrEq(
"Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1277 EXPECT_EQ(
"is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1280 std::string str(
"01204500800");
1282 Matcher<std::string> m2 = StrEq(str);
1283 EXPECT_EQ(
"is equal to \"012\\04500800\"", Describe(m2));
1284 str[0] = str[6] = str[7] = str[9] = str[10] =
'\0';
1285 Matcher<std::string> m3 = StrEq(str);
1286 EXPECT_EQ(
"is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1289 TEST(StrNeTest, MatchesUnequalString) {
1290 Matcher<const char*> m = StrNe(
"Hello");
1295 Matcher<std::string> m2 = StrNe(std::string(
"Hello"));
1299 #if GTEST_INTERNAL_HAS_STRING_VIEW
1300 Matcher<const internal::StringView> m3 = StrNe(internal::StringView(
"Hello"));
1301 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1303 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1304 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1307 TEST(StrNeTest, CanDescribeSelf) {
1308 Matcher<const char*> m = StrNe(
"Hi");
1309 EXPECT_EQ(
"isn't equal to \"Hi\"", Describe(m));
1312 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1313 Matcher<const char*> m = StrCaseEq(std::string(
"Hello"));
1319 Matcher<const std::string&> m2 = StrCaseEq(
"Hello");
1323 #if GTEST_INTERNAL_HAS_STRING_VIEW
1324 Matcher<const internal::StringView&> m3 =
1325 StrCaseEq(internal::StringView(
"Hello"));
1326 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1327 EXPECT_TRUE(m3.Matches(internal::StringView(
"hello")));
1330 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1333 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1334 std::string str1(
"oabocdooeoo");
1335 std::string str2(
"OABOCDOOEOO");
1336 Matcher<const std::string&> m0 = StrCaseEq(str1);
1339 str1[3] = str2[3] =
'\0';
1340 Matcher<const std::string&> m1 = StrCaseEq(str1);
1343 str1[0] = str1[6] = str1[7] = str1[10] =
'\0';
1344 str2[0] = str2[6] = str2[7] = str2[10] =
'\0';
1345 Matcher<const std::string&> m2 = StrCaseEq(str1);
1346 str1[9] = str2[9] =
'\0';
1349 Matcher<const std::string&> m3 = StrCaseEq(str1);
1353 str2.append(1,
'\0');
1358 TEST(StrCaseEqTest, CanDescribeSelf) {
1359 Matcher<std::string> m = StrCaseEq(
"Hi");
1360 EXPECT_EQ(
"is equal to (ignoring case) \"Hi\"", Describe(m));
1363 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1364 Matcher<const char*> m = StrCaseNe(
"Hello");
1370 Matcher<std::string> m2 = StrCaseNe(std::string(
"Hello"));
1374 #if GTEST_INTERNAL_HAS_STRING_VIEW
1375 Matcher<const internal::StringView> m3 =
1376 StrCaseNe(internal::StringView(
"Hello"));
1377 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hi")));
1379 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1380 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1381 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1384 TEST(StrCaseNeTest, CanDescribeSelf) {
1385 Matcher<const char*> m = StrCaseNe(
"Hi");
1386 EXPECT_EQ(
"isn't equal to (ignoring case) \"Hi\"", Describe(m));
1390 TEST(HasSubstrTest, WorksForStringClasses) {
1391 const Matcher<std::string> m1 = HasSubstr(
"foo");
1392 EXPECT_TRUE(m1.Matches(std::string(
"I love food.")));
1395 const Matcher<const std::string&> m2 = HasSubstr(
"foo");
1396 EXPECT_TRUE(m2.Matches(std::string(
"I love food.")));
1399 const Matcher<std::string> m_empty = HasSubstr(
"");
1401 EXPECT_TRUE(m_empty.Matches(std::string(
"not empty")));
1405 TEST(HasSubstrTest, WorksForCStrings) {
1406 const Matcher<char*> m1 = HasSubstr(
"foo");
1407 EXPECT_TRUE(m1.Matches(const_cast<char*>(
"I love food.")));
1411 const Matcher<const char*> m2 = HasSubstr(
"foo");
1416 const Matcher<const char*> m_empty = HasSubstr(
"");
1422 #if GTEST_INTERNAL_HAS_STRING_VIEW
1424 TEST(HasSubstrTest, WorksForStringViewClasses) {
1425 const Matcher<internal::StringView> m1 =
1426 HasSubstr(internal::StringView(
"foo"));
1427 EXPECT_TRUE(m1.Matches(internal::StringView(
"I love food.")));
1428 EXPECT_FALSE(m1.Matches(internal::StringView(
"tofo")));
1431 const Matcher<const internal::StringView&> m2 = HasSubstr(
"foo");
1432 EXPECT_TRUE(m2.Matches(internal::StringView(
"I love food.")));
1433 EXPECT_FALSE(m2.Matches(internal::StringView(
"tofo")));
1436 const Matcher<const internal::StringView&> m3 = HasSubstr(
"");
1437 EXPECT_TRUE(m3.Matches(internal::StringView(
"foo")));
1438 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1441 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1444 TEST(HasSubstrTest, CanDescribeSelf) {
1445 Matcher<std::string> m = HasSubstr(
"foo\n\"");
1446 EXPECT_EQ(
"has substring \"foo\\n\\\"\"", Describe(m));
1449 TEST(KeyTest, CanDescribeSelf) {
1450 Matcher<const pair<std::string, int>&> m = Key(
"foo");
1451 EXPECT_EQ(
"has a key that is equal to \"foo\"", Describe(m));
1452 EXPECT_EQ(
"doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1455 TEST(KeyTest, ExplainsResult) {
1456 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1457 EXPECT_EQ(
"whose first field is a value which is 5 less than 10",
1458 Explain(m, make_pair(5,
true)));
1459 EXPECT_EQ(
"whose first field is a value which is 5 more than 10",
1460 Explain(m, make_pair(15,
true)));
1463 TEST(KeyTest, MatchesCorrectly) {
1464 pair<int, std::string>
p(25,
"foo");
1471 TEST(KeyTest, WorksWithMoveOnly) {
1472 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1479 struct PairWithGet {
1482 using first_type = int;
1483 using second_type = std::string;
1485 const int& GetImpl(Tag<0>)
const {
return member_1; }
1486 const std::string& GetImpl(Tag<1>)
const {
return member_2; }
1489 auto get(
const PairWithGet&
value) -> decltype(value.GetImpl(Tag<I>())) {
1490 return value.GetImpl(Tag<I>());
1492 TEST(PairTest, MatchesPairWithGetCorrectly) {
1493 PairWithGet p{25,
"foo"};
1499 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1503 TEST(KeyTest, SafelyCastsInnerMatcher) {
1504 Matcher<int> is_positive = Gt(0);
1505 Matcher<int> is_negative = Lt(0);
1506 pair<char, bool>
p(
'a',
true);
1511 TEST(KeyTest, InsideContainsUsingMap) {
1513 container.insert(make_pair(1,
'a'));
1514 container.insert(make_pair(2,
'b'));
1515 container.insert(make_pair(4,
'c'));
1520 TEST(KeyTest, InsideContainsUsingMultimap) {
1521 multimap<int, char> container;
1522 container.insert(make_pair(1,
'a'));
1523 container.insert(make_pair(2,
'b'));
1524 container.insert(make_pair(4,
'c'));
1527 container.insert(make_pair(25,
'd'));
1529 container.insert(make_pair(25,
'e'));
1536 TEST(PairTest, Typing) {
1538 Matcher<const pair<const char*, int>&> m1 = Pair(
"foo", 42);
1539 Matcher<const pair<const char*, int> > m2 = Pair(
"foo", 42);
1540 Matcher<pair<const char*, int> > m3 = Pair(
"foo", 42);
1542 Matcher<pair<int, const std::string> > m4 = Pair(25,
"42");
1543 Matcher<pair<const std::string, int> > m5 = Pair(
"25", 42);
1546 TEST(PairTest, CanDescribeSelf) {
1547 Matcher<const pair<std::string, int>&> m1 = Pair(
"foo", 42);
1548 EXPECT_EQ(
"has a first field that is equal to \"foo\""
1549 ", and has a second field that is equal to 42",
1551 EXPECT_EQ(
"has a first field that isn't equal to \"foo\""
1552 ", or has a second field that isn't equal to 42",
1553 DescribeNegation(m1));
1555 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1556 EXPECT_EQ(
"has a first field that isn't equal to 13"
1557 ", and has a second field that is equal to 42",
1558 DescribeNegation(m2));
1561 TEST(PairTest, CanExplainMatchResultTo) {
1564 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
1565 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1566 Explain(m, make_pair(-1, -2)));
1570 EXPECT_EQ(
"whose second field does not match, which is 2 less than 0",
1571 Explain(m, make_pair(1, -2)));
1575 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1576 Explain(m, make_pair(-1, 2)));
1579 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1580 "which is 1 more than 0, and the second field is a value "
1581 "which is 2 more than 0",
1582 Explain(m, make_pair(1, 2)));
1586 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
1587 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1588 "which is 1 more than 0",
1589 Explain(explain_first, make_pair(1, 0)));
1593 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
1594 EXPECT_EQ(
"whose both fields match, where the second field is a value "
1595 "which is 1 more than 0",
1596 Explain(explain_second, make_pair(0, 1)));
1599 TEST(PairTest, MatchesCorrectly) {
1600 pair<int, std::string>
p(25,
"foo");
1616 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr(
"a"))));
1619 TEST(PairTest, WorksWithMoveOnly) {
1620 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1621 p.second.reset(
new int(7));
1625 TEST(PairTest, SafelyCastsInnerMatchers) {
1626 Matcher<int> is_positive = Gt(0);
1627 Matcher<int> is_negative = Lt(0);
1628 pair<char, bool>
p(
'a',
true);
1635 TEST(PairTest, InsideContainsUsingMap) {
1636 map<int, char> container;
1637 container.insert(make_pair(1,
'a'));
1638 container.insert(make_pair(2,
'b'));
1639 container.insert(make_pair(4,
'c'));
1646 TEST(ContainsTest, WorksWithMoveOnly) {
1647 ContainerHelper helper;
1649 helper.Call(MakeUniquePtrs({1, 2}));
1652 TEST(PairTest, UseGetInsteadOfMembers) {
1653 PairWithGet pair{7,
"ABC"};
1658 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1660 ElementsAre(Pair(11, std::string(
"Foo")), Pair(Ge(10), Not(
""))));
1665 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1666 const Matcher<const char*> m1 = StartsWith(std::string(
""));
1671 const Matcher<const std::string&> m2 = StartsWith(
"Hi");
1678 #if GTEST_INTERNAL_HAS_STRING_VIEW
1679 const Matcher<internal::StringView> m_empty =
1680 StartsWith(internal::StringView(
""));
1681 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1682 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1683 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"not empty")));
1684 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1687 TEST(StartsWithTest, CanDescribeSelf) {
1688 Matcher<const std::string> m = StartsWith(
"Hi");
1689 EXPECT_EQ(
"starts with \"Hi\"", Describe(m));
1694 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1695 const Matcher<const char*> m1 = EndsWith(
"");
1700 const Matcher<const std::string&> m2 = EndsWith(std::string(
"Hi"));
1707 #if GTEST_INTERNAL_HAS_STRING_VIEW
1708 const Matcher<const internal::StringView&> m4 =
1709 EndsWith(internal::StringView(
""));
1713 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1714 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1717 TEST(EndsWithTest, CanDescribeSelf) {
1718 Matcher<const std::string> m = EndsWith(
"Hi");
1719 EXPECT_EQ(
"ends with \"Hi\"", Describe(m));
1724 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1725 const Matcher<const char*> m1 = MatchesRegex(
"a.*z");
1730 const Matcher<const std::string&> m2 = MatchesRegex(
new RE(
"a.*z"));
1735 #if GTEST_INTERNAL_HAS_STRING_VIEW
1736 const Matcher<const internal::StringView&> m3 = MatchesRegex(
"a.*z");
1737 EXPECT_TRUE(m3.Matches(internal::StringView(
"az")));
1738 EXPECT_TRUE(m3.Matches(internal::StringView(
"abcz")));
1741 const Matcher<const internal::StringView&> m4 =
1742 MatchesRegex(internal::StringView(
""));
1743 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1745 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1748 TEST(MatchesRegexTest, CanDescribeSelf) {
1749 Matcher<const std::string> m1 = MatchesRegex(std::string(
"Hi.*"));
1750 EXPECT_EQ(
"matches regular expression \"Hi.*\"", Describe(m1));
1752 Matcher<const char*> m2 = MatchesRegex(
new RE(
"a.*"));
1753 EXPECT_EQ(
"matches regular expression \"a.*\"", Describe(m2));
1755 #if GTEST_INTERNAL_HAS_STRING_VIEW
1756 Matcher<const internal::StringView> m3 = MatchesRegex(
new RE(
"0.*"));
1757 EXPECT_EQ(
"matches regular expression \"0.*\"", Describe(m3));
1758 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1763 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1764 const Matcher<const char*> m1 = ContainsRegex(std::string(
"a.*z"));
1769 const Matcher<const std::string&> m2 = ContainsRegex(
new RE(
"a.*z"));
1774 #if GTEST_INTERNAL_HAS_STRING_VIEW
1775 const Matcher<const internal::StringView&> m3 =
1776 ContainsRegex(
new RE(
"a.*z"));
1777 EXPECT_TRUE(m3.Matches(internal::StringView(
"azbz")));
1778 EXPECT_TRUE(m3.Matches(internal::StringView(
"az1")));
1781 const Matcher<const internal::StringView&> m4 =
1782 ContainsRegex(internal::StringView(
""));
1783 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1785 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1788 TEST(ContainsRegexTest, CanDescribeSelf) {
1789 Matcher<const std::string> m1 = ContainsRegex(
"Hi.*");
1790 EXPECT_EQ(
"contains regular expression \"Hi.*\"", Describe(m1));
1792 Matcher<const char*> m2 = ContainsRegex(
new RE(
"a.*"));
1793 EXPECT_EQ(
"contains regular expression \"a.*\"", Describe(m2));
1795 #if GTEST_INTERNAL_HAS_STRING_VIEW
1796 Matcher<const internal::StringView> m3 = ContainsRegex(
new RE(
"0.*"));
1797 EXPECT_EQ(
"contains regular expression \"0.*\"", Describe(m3));
1798 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1802 #if GTEST_HAS_STD_WSTRING
1803 TEST(StdWideStrEqTest, MatchesEqual) {
1804 Matcher<const wchar_t*> m = StrEq(::std::wstring(L
"Hello"));
1809 Matcher<const ::std::wstring&> m2 = StrEq(L
"Hello");
1813 Matcher<const ::std::wstring&> m3 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1817 ::std::wstring str(L
"01204500800");
1819 Matcher<const ::std::wstring&> m4 = StrEq(str);
1821 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
1822 Matcher<const ::std::wstring&> m5 = StrEq(str);
1826 TEST(StdWideStrEqTest, CanDescribeSelf) {
1827 Matcher< ::std::wstring> m = StrEq(L
"Hi-\'\"?\\\a\b\f\n\r\t\v");
1828 EXPECT_EQ(
"is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1831 Matcher< ::std::wstring> m2 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1832 EXPECT_EQ(
"is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1835 ::std::wstring str(L
"01204500800");
1837 Matcher<const ::std::wstring&> m4 = StrEq(str);
1838 EXPECT_EQ(
"is equal to L\"012\\04500800\"", Describe(m4));
1839 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
1840 Matcher<const ::std::wstring&> m5 = StrEq(str);
1841 EXPECT_EQ(
"is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1844 TEST(StdWideStrNeTest, MatchesUnequalString) {
1845 Matcher<const wchar_t*> m = StrNe(L
"Hello");
1850 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L
"Hello"));
1855 TEST(StdWideStrNeTest, CanDescribeSelf) {
1856 Matcher<const wchar_t*> m = StrNe(L
"Hi");
1857 EXPECT_EQ(
"isn't equal to L\"Hi\"", Describe(m));
1860 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1861 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L
"Hello"));
1867 Matcher<const ::std::wstring&> m2 = StrCaseEq(L
"Hello");
1872 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1873 ::std::wstring str1(L
"oabocdooeoo");
1874 ::std::wstring str2(L
"OABOCDOOEOO");
1875 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1876 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L
'\0')));
1878 str1[3] = str2[3] = L
'\0';
1879 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1882 str1[0] = str1[6] = str1[7] = str1[10] = L
'\0';
1883 str2[0] = str2[6] = str2[7] = str2[10] = L
'\0';
1884 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1885 str1[9] = str2[9] = L
'\0';
1888 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1892 str2.append(1, L
'\0');
1897 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1898 Matcher< ::std::wstring> m = StrCaseEq(L
"Hi");
1899 EXPECT_EQ(
"is equal to (ignoring case) L\"Hi\"", Describe(m));
1902 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1903 Matcher<const wchar_t*> m = StrCaseNe(L
"Hello");
1909 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L
"Hello"));
1914 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1915 Matcher<const wchar_t*> m = StrCaseNe(L
"Hi");
1916 EXPECT_EQ(
"isn't equal to (ignoring case) L\"Hi\"", Describe(m));
1920 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1921 const Matcher< ::std::wstring> m1 = HasSubstr(L
"foo");
1922 EXPECT_TRUE(m1.Matches(::std::wstring(L
"I love food.")));
1925 const Matcher<const ::std::wstring&> m2 = HasSubstr(L
"foo");
1926 EXPECT_TRUE(m2.Matches(::std::wstring(L
"I love food.")));
1931 TEST(StdWideHasSubstrTest, WorksForCStrings) {
1932 const Matcher<wchar_t*> m1 = HasSubstr(L
"foo");
1933 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L
"I love food.")));
1934 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L
"tofo")));
1937 const Matcher<const wchar_t*> m2 = HasSubstr(L
"foo");
1944 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1945 Matcher< ::std::wstring> m = HasSubstr(L
"foo\n\"");
1946 EXPECT_EQ(
"has substring L\"foo\\n\\\"\"", Describe(m));
1951 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1952 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L
""));
1957 const Matcher<const ::std::wstring&> m2 = StartsWith(L
"Hi");
1965 TEST(StdWideStartsWithTest, CanDescribeSelf) {
1966 Matcher<const ::std::wstring> m = StartsWith(L
"Hi");
1967 EXPECT_EQ(
"starts with L\"Hi\"", Describe(m));
1972 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1973 const Matcher<const wchar_t*> m1 = EndsWith(L
"");
1978 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L
"Hi"));
1986 TEST(StdWideEndsWithTest, CanDescribeSelf) {
1987 Matcher<const ::std::wstring> m = EndsWith(L
"Hi");
1988 EXPECT_EQ(
"ends with L\"Hi\"", Describe(m));
1991 #endif // GTEST_HAS_STD_WSTRING
1993 typedef ::std::tuple<long, int> Tuple2;
1997 TEST(Eq2Test, MatchesEqualArguments) {
1998 Matcher<const Tuple2&> m = Eq();
2004 TEST(Eq2Test, CanDescribeSelf) {
2005 Matcher<const Tuple2&> m = Eq();
2006 EXPECT_EQ(
"are an equal pair", Describe(m));
2011 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
2012 Matcher<const Tuple2&> m = Ge();
2019 TEST(Ge2Test, CanDescribeSelf) {
2020 Matcher<const Tuple2&> m = Ge();
2021 EXPECT_EQ(
"are a pair where the first >= the second", Describe(m));
2026 TEST(Gt2Test, MatchesGreaterThanArguments) {
2027 Matcher<const Tuple2&> m = Gt();
2034 TEST(Gt2Test, CanDescribeSelf) {
2035 Matcher<const Tuple2&> m = Gt();
2036 EXPECT_EQ(
"are a pair where the first > the second", Describe(m));
2041 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
2042 Matcher<const Tuple2&> m = Le();
2049 TEST(Le2Test, CanDescribeSelf) {
2050 Matcher<const Tuple2&> m = Le();
2051 EXPECT_EQ(
"are a pair where the first <= the second", Describe(m));
2056 TEST(Lt2Test, MatchesLessThanArguments) {
2057 Matcher<const Tuple2&> m = Lt();
2064 TEST(Lt2Test, CanDescribeSelf) {
2065 Matcher<const Tuple2&> m = Lt();
2066 EXPECT_EQ(
"are a pair where the first < the second", Describe(m));
2071 TEST(Ne2Test, MatchesUnequalArguments) {
2072 Matcher<const Tuple2&> m = Ne();
2079 TEST(Ne2Test, CanDescribeSelf) {
2080 Matcher<const Tuple2&> m = Ne();
2081 EXPECT_EQ(
"are an unequal pair", Describe(m));
2084 TEST(PairMatchBaseTest, WorksWithMoveOnly) {
2085 using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
2086 Matcher<Pointers> matcher = Eq();
2094 TEST(IsNan, FloatMatchesNan) {
2095 float quiet_nan = std::numeric_limits<float>::quiet_NaN();
2096 float other_nan = std::nanf(
"1");
2097 float real_value = 1.0f;
2099 Matcher<float> m = IsNan();
2104 Matcher<float&> m_ref = IsNan();
2109 Matcher<const float&> m_cref = IsNan();
2116 TEST(IsNan, DoubleMatchesNan) {
2117 double quiet_nan = std::numeric_limits<double>::quiet_NaN();
2118 double other_nan = std::nan(
"1");
2119 double real_value = 1.0;
2121 Matcher<double> m = IsNan();
2126 Matcher<double&> m_ref = IsNan();
2131 Matcher<const double&> m_cref = IsNan();
2138 TEST(IsNan, LongDoubleMatchesNan) {
2139 long double quiet_nan = std::numeric_limits<long double>::quiet_NaN();
2140 long double other_nan = std::nan(
"1");
2141 long double real_value = 1.0;
2143 Matcher<long double> m = IsNan();
2148 Matcher<long double&> m_ref = IsNan();
2153 Matcher<const long double&> m_cref = IsNan();
2160 TEST(IsNan, NotMatchesNan) {
2161 Matcher<float> mf = Not(IsNan());
2162 EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN()));
2166 Matcher<double> md = Not(IsNan());
2167 EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN()));
2171 Matcher<long double> mld = Not(IsNan());
2172 EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN()));
2178 TEST(IsNan, CanDescribeSelf) {
2179 Matcher<float> mf = IsNan();
2182 Matcher<double> md = IsNan();
2185 Matcher<long double> mld = IsNan();
2190 TEST(IsNan, CanDescribeSelfWithNot) {
2191 Matcher<float> mf = Not(IsNan());
2194 Matcher<double> md = Not(IsNan());
2197 Matcher<long double> mld = Not(IsNan());
2203 TEST(FloatEq2Test, MatchesEqualArguments) {
2204 typedef ::std::tuple<float, float> Tpl;
2205 Matcher<const Tpl&> m = FloatEq();
2207 EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f)));
2212 TEST(FloatEq2Test, CanDescribeSelf) {
2213 Matcher<const ::std::tuple<float, float>&> m = FloatEq();
2214 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2219 TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {
2220 typedef ::std::tuple<float, float> Tpl;
2221 Matcher<const Tpl&> m = NanSensitiveFloatEq();
2223 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2224 std::numeric_limits<float>::quiet_NaN())));
2226 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2227 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2231 TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
2232 Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();
2233 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2238 TEST(DoubleEq2Test, MatchesEqualArguments) {
2239 typedef ::std::tuple<double, double> Tpl;
2240 Matcher<const Tpl&> m = DoubleEq();
2242 EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1)));
2247 TEST(DoubleEq2Test, CanDescribeSelf) {
2248 Matcher<const ::std::tuple<double, double>&> m = DoubleEq();
2249 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2254 TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {
2255 typedef ::std::tuple<double, double> Tpl;
2256 Matcher<const Tpl&> m = NanSensitiveDoubleEq();
2258 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2259 std::numeric_limits<double>::quiet_NaN())));
2261 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2262 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2266 TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
2267 Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();
2268 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2273 TEST(FloatNear2Test, MatchesEqualArguments) {
2274 typedef ::std::tuple<float, float> Tpl;
2275 Matcher<const Tpl&> m = FloatNear(0.5f);
2282 TEST(FloatNear2Test, CanDescribeSelf) {
2283 Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f);
2284 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2289 TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {
2290 typedef ::std::tuple<float, float> Tpl;
2291 Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f);
2294 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2295 std::numeric_limits<float>::quiet_NaN())));
2297 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2298 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2302 TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
2303 Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f);
2304 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2309 TEST(DoubleNear2Test, MatchesEqualArguments) {
2310 typedef ::std::tuple<double, double> Tpl;
2311 Matcher<const Tpl&> m = DoubleNear(0.5);
2318 TEST(DoubleNear2Test, CanDescribeSelf) {
2319 Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5);
2320 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2325 TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {
2326 typedef ::std::tuple<double, double> Tpl;
2327 Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f);
2330 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2331 std::numeric_limits<double>::quiet_NaN())));
2333 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2334 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2338 TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
2339 Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f);
2340 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2344 TEST(NotTest, NegatesMatcher) {
2352 TEST(NotTest, CanDescribeSelf) {
2353 Matcher<int> m = Not(Eq(5));
2354 EXPECT_EQ(
"isn't equal to 5", Describe(m));
2358 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
2360 Matcher<int> greater_than_5 = Gt(5);
2362 Matcher<const int&> m = Not(greater_than_5);
2363 Matcher<int&> m2 = Not(greater_than_5);
2364 Matcher<int&> m3 = Not(m);
2368 void AllOfMatches(
int num,
const Matcher<int>& m) {
2371 for (
int i = 1;
i <= num; ++
i) {
2379 TEST(AllOfTest, MatchesWhenAllMatch) {
2381 m = AllOf(Le(2), Ge(1));
2387 m = AllOf(Gt(0), Ne(1), Ne(2));
2393 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2400 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2409 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
2410 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
2411 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
2412 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
2413 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
2414 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
2415 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2417 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2419 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2422 50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
2423 Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),
2424 Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),
2425 Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),
2426 Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),
2427 Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
2433 TEST(AllOfTest, CanDescribeSelf) {
2435 m = AllOf(Le(2), Ge(1));
2436 EXPECT_EQ(
"(is <= 2) and (is >= 1)", Describe(m));
2438 m = AllOf(Gt(0), Ne(1), Ne(2));
2439 std::string expected_descr1 =
2440 "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
2441 EXPECT_EQ(expected_descr1, Describe(m));
2443 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2444 std::string expected_descr2 =
2445 "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
2447 EXPECT_EQ(expected_descr2, Describe(m));
2449 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2450 std::string expected_descr3 =
2451 "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
2452 "and (isn't equal to 7)";
2453 EXPECT_EQ(expected_descr3, Describe(m));
2457 TEST(AllOfTest, CanDescribeNegation) {
2459 m = AllOf(Le(2), Ge(1));
2460 std::string expected_descr4 =
"(isn't <= 2) or (isn't >= 1)";
2461 EXPECT_EQ(expected_descr4, DescribeNegation(m));
2463 m = AllOf(Gt(0), Ne(1), Ne(2));
2464 std::string expected_descr5 =
2465 "(isn't > 0) or (is equal to 1) or (is equal to 2)";
2466 EXPECT_EQ(expected_descr5, DescribeNegation(m));
2468 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2469 std::string expected_descr6 =
2470 "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
2471 EXPECT_EQ(expected_descr6, DescribeNegation(m));
2473 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2474 std::string expected_desr7 =
2475 "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
2477 EXPECT_EQ(expected_desr7, DescribeNegation(m));
2479 m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
2481 AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2482 EXPECT_THAT(Describe(m), EndsWith(
"and (isn't equal to 11)"));
2483 AllOfMatches(11, m);
2487 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2489 Matcher<int> greater_than_5 = Gt(5);
2490 Matcher<int> less_than_10 = Lt(10);
2492 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2493 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2494 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2497 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2498 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2501 TEST(AllOfTest, ExplainsResult) {
2507 m = AllOf(GreaterThan(10), Lt(30));
2508 EXPECT_EQ(
"which is 15 more than 10", Explain(m, 25));
2511 m = AllOf(GreaterThan(10), GreaterThan(20));
2512 EXPECT_EQ(
"which is 20 more than 10, and which is 10 more than 20",
2517 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2518 EXPECT_EQ(
"which is 15 more than 10, and which is 5 more than 20",
2522 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2523 EXPECT_EQ(
"which is 30 more than 10, and which is 20 more than 20, "
2524 "and which is 10 more than 30",
2529 m = AllOf(GreaterThan(10), GreaterThan(20));
2530 EXPECT_EQ(
"which is 5 less than 10", Explain(m, 5));
2535 m = AllOf(GreaterThan(10), Lt(30));
2540 m = AllOf(GreaterThan(10), GreaterThan(20));
2541 EXPECT_EQ(
"which is 5 less than 20", Explain(m, 15));
2545 static void AnyOfMatches(
int num,
const Matcher<int>& m) {
2548 for (
int i = 1;
i <= num; ++
i) {
2554 static void AnyOfStringMatches(
int num,
const Matcher<std::string>& m) {
2558 for (
int i = 1;
i <= num; ++
i) {
2566 TEST(AnyOfTest, MatchesWhenAnyMatches) {
2568 m = AnyOf(Le(1), Ge(3));
2573 m = AnyOf(Lt(0), Eq(1), Eq(2));
2579 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2586 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2596 AnyOfMatches(2, AnyOf(1, 2));
2597 AnyOfMatches(3, AnyOf(1, 2, 3));
2598 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2599 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2600 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2601 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2602 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2603 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2604 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
2608 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
2611 Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2613 EXPECT_THAT(Describe(m), EndsWith(
"or (is equal to 11)"));
2614 AnyOfMatches(11, m);
2615 AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2616 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
2617 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
2618 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2619 41, 42, 43, 44, 45, 46, 47, 48, 49, 50));
2621 50, AnyOf(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
2622 "13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
2623 "23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"32",
2624 "33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
"41",
"42",
2625 "43",
"44",
"45",
"46",
"47",
"48",
"49",
"50"));
2629 TEST(ElementsAreTest, HugeMatcher) {
2630 vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
2633 ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7),
2634 Eq(8), Eq(9), Eq(10), Gt(1), Eq(12)));
2638 TEST(ElementsAreTest, HugeMatcherStr) {
2639 vector<std::string> test_vector{
2640 "literal_string",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
2642 EXPECT_THAT(test_vector, UnorderedElementsAre(
"literal_string",
_,
_,
_,
_,
_,
2647 TEST(ElementsAreTest, HugeMatcherUnordered) {
2648 vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
2651 Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7),
2652 Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));
2657 TEST(AnyOfTest, CanDescribeSelf) {
2659 m = AnyOf(Le(1), Ge(3));
2664 m = AnyOf(Lt(0), Eq(1), Eq(2));
2665 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m));
2667 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2668 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
2671 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2673 "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
2679 TEST(AnyOfTest, CanDescribeNegation) {
2681 m = AnyOf(Le(1), Ge(3));
2682 EXPECT_EQ(
"(isn't <= 1) and (isn't >= 3)",
2683 DescribeNegation(m));
2685 m = AnyOf(Lt(0), Eq(1), Eq(2));
2686 EXPECT_EQ(
"(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
2687 DescribeNegation(m));
2689 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2691 "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
2693 DescribeNegation(m));
2695 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2697 "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
2698 "to 5) and (isn't equal to 7)",
2699 DescribeNegation(m));
2703 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2705 Matcher<int> greater_than_5 = Gt(5);
2706 Matcher<int> less_than_10 = Lt(10);
2708 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2709 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2710 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2713 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2714 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2717 TEST(AnyOfTest, ExplainsResult) {
2723 m = AnyOf(GreaterThan(10), Lt(0));
2724 EXPECT_EQ(
"which is 5 less than 10", Explain(m, 5));
2727 m = AnyOf(GreaterThan(10), GreaterThan(20));
2728 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20",
2733 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2734 EXPECT_EQ(
"which is 5 less than 10, and which is 25 less than 30",
2738 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2739 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20, "
2740 "and which is 25 less than 30",
2745 m = AnyOf(GreaterThan(10), GreaterThan(20));
2746 EXPECT_EQ(
"which is 5 more than 10", Explain(m, 15));
2751 m = AnyOf(GreaterThan(10), Lt(30));
2756 m = AnyOf(GreaterThan(30), GreaterThan(20));
2757 EXPECT_EQ(
"which is 5 more than 20", Explain(m, 25));
2767 int IsPositive(
double x) {
2768 return x > 0 ? 1 : 0;
2773 class IsGreaterThan {
2775 explicit IsGreaterThan(
int threshold) :
threshold_(threshold) {}
2777 bool operator()(
int n)
const {
return n >
threshold_; }
2788 bool ReferencesFooAndIsZero(
const int& n) {
2789 return (&n == &
foo) && (n == 0);
2794 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2795 Matcher<double> m = Truly(IsPositive);
2801 TEST(TrulyTest, CanBeUsedWithFunctor) {
2802 Matcher<int> m = Truly(IsGreaterThan(5));
2808 class ConvertibleToBool {
2810 explicit ConvertibleToBool(
int number) :
number_(number) {}
2811 operator bool()
const {
return number_ != 0; }
2817 ConvertibleToBool IsNotZero(
int number) {
2818 return ConvertibleToBool(number);
2824 TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2825 Matcher<int> m = Truly(IsNotZero);
2831 TEST(TrulyTest, CanDescribeSelf) {
2832 Matcher<double> m = Truly(IsPositive);
2833 EXPECT_EQ(
"satisfies the given predicate",
2839 TEST(TrulyTest, WorksForByRefArguments) {
2840 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2848 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2855 TEST(MatchesTest, WorksOnByRefArguments) {
2863 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2864 Matcher<int> eq5 = Eq(5);
2872 TEST(ValueTest, WorksWithPolymorphicMatcher) {
2877 TEST(ValueTest, WorksWithMonomorphicMatcher) {
2878 const Matcher<int> is_zero = Eq(0);
2883 const Matcher<const int&> ref_n = Ref(n);
2888 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
2889 StringMatchResultListener listener1;
2890 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
2893 StringMatchResultListener listener2;
2894 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
2898 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
2899 const Matcher<int> is_even = PolymorphicIsEven();
2900 StringMatchResultListener listener1;
2901 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
2904 const Matcher<const double&> is_zero = Eq(0);
2905 StringMatchResultListener listener2;
2906 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
2910 MATCHER(ConstructNoArg,
"") {
return true; }
2911 MATCHER_P(Construct1Arg, arg1,
"") {
return true; }
2912 MATCHER_P2(Construct2Args, arg1, arg2,
"") {
return true; }
2914 TEST(MatcherConstruct, ExplicitVsImplicit) {
2917 ConstructNoArgMatcher m = {};
2920 ConstructNoArgMatcher m2;
2926 using M = Construct1ArgMatcherP<int>;
2932 Construct2ArgsMatcherP2<int, double> m = {1, 2.2};
2938 return ExplainMatchResult(inner_matcher, arg, result_listener);
2941 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2945 TEST(DescribeMatcherTest, WorksWithValue) {
2946 EXPECT_EQ(
"is equal to 42", DescribeMatcher<int>(42));
2947 EXPECT_EQ(
"isn't equal to 42", DescribeMatcher<int>(42,
true));
2950 TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
2951 const Matcher<int> monomorphic = Le(0);
2952 EXPECT_EQ(
"is <= 0", DescribeMatcher<int>(monomorphic));
2953 EXPECT_EQ(
"isn't <= 0", DescribeMatcher<int>(monomorphic,
true));
2956 TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
2957 EXPECT_EQ(
"is even", DescribeMatcher<int>(PolymorphicIsEven()));
2958 EXPECT_EQ(
"is odd", DescribeMatcher<int>(PolymorphicIsEven(),
true));
2961 TEST(AllArgsTest, WorksForTuple) {
2962 EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));
2963 EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));
2966 TEST(AllArgsTest, WorksForNonTuple) {
2971 class AllArgsHelper {
2981 TEST(AllArgsTest, WorksInWithClause) {
2982 AllArgsHelper helper;
2984 .With(AllArgs(Lt()))
2985 .WillByDefault(
Return(1));
2988 .With(AllArgs(Gt()))
2995 class OptionalMatchersHelper {
2997 OptionalMatchersHelper() {}
3012 TEST(AllArgsTest, WorksWithoutMatchers) {
3013 OptionalMatchersHelper helper;
3035 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
3038 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) <<
"This should succeed too.";
3044 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
3047 static unsigned short n;
3052 "Expected: is > 10\n"
3053 " Actual: 5" + OfType(
"unsigned short"));
3058 "Expected: (is <= 7) and (is >= 5)\n"
3059 " Actual: 0" + OfType(
"unsigned short"));
3064 TEST(MatcherAssertionTest, WorksForByRefArguments) {
3072 "Expected: does not reference the variable @");
3075 "Actual: 0" + OfType(
"int") +
", which is located @");
3080 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
3081 Matcher<const char*> starts_with_he = StartsWith(
"he");
3084 Matcher<const std::string&> ends_with_ok = EndsWith(
"ok");
3086 const std::string bad =
"bad";
3089 "Expected: ends with \"ok\"\n"
3090 " Actual: \"bad\"");
3091 Matcher<int> is_greater_than_5 = Gt(5);
3094 "Expected: is > 5\n"
3095 " Actual: 5" + OfType(
"int"));
3099 template <
typename RawType>
3103 typedef typename Floating::Bits Bits;
3123 max_(Floating::Max()),
3124 nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
3125 nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {
3129 EXPECT_EQ(
sizeof(RawType),
sizeof(Bits));
3135 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
3136 Matcher<RawType> m1 = matcher_maker(0.0);
3145 Matcher<RawType> m3 = matcher_maker(1.0);
3152 Matcher<RawType> m4 = matcher_maker(-
infinity_);
3155 Matcher<RawType> m5 = matcher_maker(
infinity_);
3164 Matcher<const RawType&> m6 = matcher_maker(0.0);
3171 Matcher<RawType&> m7 = matcher_maker(0.0);
3209 template <
typename RawType>
3210 class FloatingPointNearTest :
public FloatingPointTest<RawType> {
3212 typedef FloatingPointTest<RawType> ParentType;
3216 void TestNearMatches(
3217 testing::internal::FloatingEqMatcher<RawType>
3218 (*matcher_maker)(RawType, RawType)) {
3219 Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
3226 Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
3265 Matcher<RawType> m9 = matcher_maker(
3271 Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
3278 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
3293 typedef FloatingPointTest<float> FloatTest;
3295 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
3296 TestMatches(&FloatEq);
3299 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
3300 TestMatches(&NanSensitiveFloatEq);
3303 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
3305 Matcher<float> m = FloatEq(
nan1_);
3311 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
3313 Matcher<float> m = NanSensitiveFloatEq(
nan1_);
3319 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
3320 Matcher<float> m1 = FloatEq(2.0f);
3321 EXPECT_EQ(
"is approximately 2", Describe(m1));
3322 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3324 Matcher<float> m2 = FloatEq(0.5f);
3325 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3326 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3328 Matcher<float> m3 = FloatEq(
nan1_);
3329 EXPECT_EQ(
"never matches", Describe(m3));
3330 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3333 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
3334 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
3335 EXPECT_EQ(
"is approximately 2", Describe(m1));
3336 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3338 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
3339 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3340 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3342 Matcher<float> m3 = NanSensitiveFloatEq(
nan1_);
3344 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3349 typedef FloatingPointNearTest<float> FloatNearTest;
3351 TEST_F(FloatNearTest, FloatNearMatches) {
3352 TestNearMatches(&FloatNear);
3355 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
3356 TestNearMatches(&NanSensitiveFloatNear);
3359 TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
3360 Matcher<float> m1 = FloatNear(2.0f, 0.5f);
3361 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3363 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3365 Matcher<float> m2 = FloatNear(0.5f, 0.5f);
3366 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3368 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3370 Matcher<float> m3 = FloatNear(
nan1_, 0.0);
3371 EXPECT_EQ(
"never matches", Describe(m3));
3372 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3375 TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
3376 Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);
3377 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3379 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3381 Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);
3382 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3384 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3386 Matcher<float> m3 = NanSensitiveFloatNear(
nan1_, 0.1f);
3388 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3391 TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
3399 TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
3401 Matcher<float> m = NanSensitiveFloatNear(
nan1_, 0.1f);
3408 typedef FloatingPointTest<double> DoubleTest;
3410 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
3411 TestMatches(&DoubleEq);
3414 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
3415 TestMatches(&NanSensitiveDoubleEq);
3418 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
3420 Matcher<double> m = DoubleEq(
nan1_);
3426 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
3428 Matcher<double> m = NanSensitiveDoubleEq(
nan1_);
3434 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
3435 Matcher<double> m1 = DoubleEq(2.0);
3436 EXPECT_EQ(
"is approximately 2", Describe(m1));
3437 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3439 Matcher<double> m2 = DoubleEq(0.5);
3440 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3441 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3443 Matcher<double> m3 = DoubleEq(
nan1_);
3444 EXPECT_EQ(
"never matches", Describe(m3));
3445 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3448 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
3449 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
3450 EXPECT_EQ(
"is approximately 2", Describe(m1));
3451 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3453 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
3454 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3455 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3457 Matcher<double> m3 = NanSensitiveDoubleEq(
nan1_);
3459 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3464 typedef FloatingPointNearTest<double> DoubleNearTest;
3466 TEST_F(DoubleNearTest, DoubleNearMatches) {
3467 TestNearMatches(&DoubleNear);
3470 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
3471 TestNearMatches(&NanSensitiveDoubleNear);
3474 TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
3475 Matcher<double> m1 = DoubleNear(2.0, 0.5);
3476 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3478 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3480 Matcher<double> m2 = DoubleNear(0.5, 0.5);
3481 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3483 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3485 Matcher<double> m3 = DoubleNear(
nan1_, 0.0);
3486 EXPECT_EQ(
"never matches", Describe(m3));
3487 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3490 TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {
3491 EXPECT_EQ(
"", Explain(DoubleNear(2.0, 0.1), 2.05));
3492 EXPECT_EQ(
"which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2));
3493 EXPECT_EQ(
"which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7));
3495 const std::string explanation =
3496 Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);
3499 EXPECT_TRUE(explanation ==
"which is 1.2e-10 from 2.1" ||
3500 explanation ==
"which is 1.2e-010 from 2.1")
3501 <<
" where explanation is \"" << explanation <<
"\".";
3504 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
3505 Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);
3506 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3508 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3510 Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);
3511 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3513 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3515 Matcher<double> m3 = NanSensitiveDoubleNear(
nan1_, 0.1);
3517 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3520 TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
3528 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
3530 Matcher<double> m = NanSensitiveDoubleNear(
nan1_, 0.1);
3536 TEST(PointeeTest, RawPointer) {
3537 const Matcher<int*> m = Pointee(Ge(0));
3546 TEST(PointeeTest, RawPointerToConst) {
3547 const Matcher<const double*> m = Pointee(Ge(0));
3556 TEST(PointeeTest, ReferenceToConstRawPointer) {
3557 const Matcher<int* const &> m = Pointee(Ge(0));
3566 TEST(PointeeTest, ReferenceToNonConstRawPointer) {
3567 const Matcher<double* &> m = Pointee(Ge(0));
3578 MATCHER_P(FieldIIs, inner_matcher,
"") {
3579 return ExplainMatchResult(inner_matcher, arg.i, result_listener);
3583 TEST(WhenDynamicCastToTest, SameType) {
3588 Base* as_base_ptr = &derived;
3590 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));
3592 Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));
3595 TEST(WhenDynamicCastToTest, WrongTypes) {
3598 OtherDerived other_derived;
3601 EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(
_))));
3603 Base* as_base_ptr = &derived;
3604 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(
_))));
3606 as_base_ptr = &other_derived;
3607 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(
_))));
3611 TEST(WhenDynamicCastToTest, AlreadyNull) {
3613 Base* as_base_ptr =
nullptr;
3617 struct AmbiguousCastTypes {
3618 class VirtualDerived :
public virtual Base {};
3619 class DerivedSub1 :
public VirtualDerived {};
3620 class DerivedSub2 :
public VirtualDerived {};
3621 class ManyDerivedInHierarchy :
public DerivedSub1,
public DerivedSub2 {};
3624 TEST(WhenDynamicCastToTest, AmbiguousCast) {
3625 AmbiguousCastTypes::DerivedSub1 sub1;
3626 AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
3629 static_cast<AmbiguousCastTypes::DerivedSub1*
>(&many_derived);
3631 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(
IsNull()));
3632 as_base_ptr = &sub1;
3635 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(
IsNull())));
3638 TEST(WhenDynamicCastToTest, Describe) {
3639 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(
_));
3640 const std::string prefix =
3641 "when dynamic_cast to " + internal::GetTypeName<Derived*>() +
", ";
3642 EXPECT_EQ(prefix +
"points to a value that is anything", Describe(matcher));
3643 EXPECT_EQ(prefix +
"does not point to a value that is anything",
3644 DescribeNegation(matcher));
3647 TEST(WhenDynamicCastToTest, Explain) {
3648 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(
_));
3649 Base*
null =
nullptr;
3650 EXPECT_THAT(Explain(matcher, null), HasSubstr(
"NULL"));
3653 EXPECT_THAT(Explain(matcher, &derived), HasSubstr(
"which points to "));
3656 Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(
_);
3658 HasSubstr(
"which cannot be dynamic_cast"));
3661 TEST(WhenDynamicCastToTest, GoodReference) {
3664 Base& as_base_ref = derived;
3665 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
3666 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));
3669 TEST(WhenDynamicCastToTest, BadReference) {
3671 Base& as_base_ref = derived;
3672 EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(
_)));
3674 #endif // GTEST_HAS_RTTI
3677 template <
typename T>
3678 class ConstPropagatingPtr {
3680 typedef T element_type;
3682 ConstPropagatingPtr() :
val_() {}
3683 explicit ConstPropagatingPtr(
T* t) :
val_(t) {}
3684 ConstPropagatingPtr(
const ConstPropagatingPtr& other) :
val_(other.
val_) {}
3686 T*
get() {
return val_; }
3689 const T*
get()
const {
return val_; }
3696 TEST(PointeeTest, WorksWithConstPropagatingPointers) {
3697 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
3699 const ConstPropagatingPtr<int> co(&three);
3700 ConstPropagatingPtr<int> o(&three);
3708 TEST(PointeeTest, NeverMatchesNull) {
3709 const Matcher<const char*> m = Pointee(
_);
3714 TEST(PointeeTest, MatchesAgainstAValue) {
3715 const Matcher<int*> m = Pointee(5);
3724 TEST(PointeeTest, CanDescribeSelf) {
3725 const Matcher<int*> m = Pointee(Gt(3));
3726 EXPECT_EQ(
"points to a value that is > 3", Describe(m));
3727 EXPECT_EQ(
"does not point to a value that is > 3",
3728 DescribeNegation(m));
3731 TEST(PointeeTest, CanExplainMatchResult) {
3732 const Matcher<const std::string*> m = Pointee(StartsWith(
"Hi"));
3734 EXPECT_EQ(
"", Explain(m, static_cast<const std::string*>(
nullptr)));
3736 const Matcher<long*> m2 = Pointee(GreaterThan(1));
3738 EXPECT_EQ(
"which points to 3" + OfType(
"long") +
", which is 2 more than 1",
3742 TEST(PointeeTest, AlwaysExplainsPointee) {
3743 const Matcher<int*> m = Pointee(0);
3745 EXPECT_EQ(
"which points to 42" + OfType(
"int"), Explain(m, &n));
3751 Uncopyable() :
value_(-1) {}
3752 explicit Uncopyable(
int a_value) :
value_(a_value) {}
3755 void set_value(
int i) {
value_ =
i; }
3763 bool ValueIsPositive(
const Uncopyable& x) {
return x.value() > 0; }
3765 MATCHER_P(UncopyableIs, inner_matcher,
"") {
3766 return ExplainMatchResult(inner_matcher, arg.value(), result_listener);
3771 AStruct() : x(0),
y(1.0),
z(5), p(nullptr) {}
3772 AStruct(
const AStruct& rhs)
3773 : x(rhs.x),
y(rhs.
y),
z(rhs.
z.value()), p(rhs.p) {}
3782 struct DerivedStruct :
public AStruct {
3787 TEST(FieldTest, WorksForNonConstField) {
3788 Matcher<AStruct> m = Field(&
AStruct::x, Ge(0));
3789 Matcher<AStruct> m_with_name = Field(
"x", &
AStruct::x, Ge(0));
3800 TEST(FieldTest, WorksForConstField) {
3803 Matcher<AStruct> m = Field(&
AStruct::y, Ge(0.0));
3804 Matcher<AStruct> m_with_name = Field(
"y", &
AStruct::y, Ge(0.0));
3808 m_with_name = Field(
"y", &
AStruct::y, Le(0.0));
3814 TEST(FieldTest, WorksForUncopyableField) {
3817 Matcher<AStruct> m = Field(&
AStruct::z, Truly(ValueIsPositive));
3819 m = Field(&
AStruct::z, Not(Truly(ValueIsPositive)));
3824 TEST(FieldTest, WorksForPointerField) {
3826 Matcher<AStruct> m = Field(&
AStruct::p, static_cast<const char*>(
nullptr));
3841 TEST(FieldTest, WorksForByRefArgument) {
3842 Matcher<const AStruct&> m = Field(&
AStruct::x, Ge(0));
3852 TEST(FieldTest, WorksForArgumentOfSubType) {
3855 Matcher<const DerivedStruct&> m = Field(&
AStruct::x, Ge(0));
3865 TEST(FieldTest, WorksForCompatibleMatcherType) {
3867 Matcher<const AStruct&> m = Field(&
AStruct::x,
3868 Matcher<signed char>(Ge(0)));
3877 TEST(FieldTest, CanDescribeSelf) {
3878 Matcher<const AStruct&> m = Field(&
AStruct::x, Ge(0));
3880 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(m));
3881 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(m));
3884 TEST(FieldTest, CanDescribeSelfWithFieldName) {
3885 Matcher<const AStruct&> m = Field(
"field_name", &
AStruct::x, Ge(0));
3887 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(m));
3888 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
3889 DescribeNegation(m));
3893 TEST(FieldTest, CanExplainMatchResult) {
3894 Matcher<const AStruct&> m = Field(&
AStruct::x, Ge(0));
3898 EXPECT_EQ(
"whose given field is 1" + OfType(
"int"), Explain(m, a));
3902 "whose given field is 1" + OfType(
"int") +
", which is 1 more than 0",
3906 TEST(FieldTest, CanExplainMatchResultWithFieldName) {
3907 Matcher<const AStruct&> m = Field(
"field_name", &
AStruct::x, Ge(0));
3911 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int"), Explain(m, a));
3913 m = Field(
"field_name", &
AStruct::x, GreaterThan(0));
3914 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int") +
3915 ", which is 1 more than 0",
3920 TEST(FieldForPointerTest, WorksForPointerToConst) {
3921 Matcher<const AStruct*> m = Field(&
AStruct::x, Ge(0));
3930 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3931 Matcher<AStruct*> m = Field(&
AStruct::x, Ge(0));
3940 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3941 Matcher<AStruct* const&> m = Field(&
AStruct::x, Ge(0));
3950 TEST(FieldForPointerTest, DoesNotMatchNull) {
3951 Matcher<const AStruct*> m = Field(&
AStruct::x,
_);
3957 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3960 Matcher<DerivedStruct*> m = Field(&
AStruct::x, Ge(0));
3969 TEST(FieldForPointerTest, CanDescribeSelf) {
3970 Matcher<const AStruct*> m = Field(&
AStruct::x, Ge(0));
3972 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(m));
3973 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(m));
3976 TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
3977 Matcher<const AStruct*> m = Field(
"field_name", &
AStruct::x, Ge(0));
3979 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(m));
3980 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
3981 DescribeNegation(m));
3985 TEST(FieldForPointerTest, CanExplainMatchResult) {
3986 Matcher<const AStruct*> m = Field(&
AStruct::x, Ge(0));
3990 EXPECT_EQ(
"", Explain(m, static_cast<const AStruct*>(
nullptr)));
3991 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int"),
3995 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int") +
3996 ", which is 1 more than 0", Explain(m, &a));
3999 TEST(FieldForPointerTest, CanExplainMatchResultWithFieldName) {
4000 Matcher<const AStruct*> m = Field(
"field_name", &
AStruct::x, Ge(0));
4004 EXPECT_EQ(
"", Explain(m, static_cast<const AStruct*>(
nullptr)));
4006 "which points to an object whose field `field_name` is 1" + OfType(
"int"),
4009 m = Field(
"field_name", &
AStruct::x, GreaterThan(0));
4010 EXPECT_EQ(
"which points to an object whose field `field_name` is 1" +
4011 OfType(
"int") +
", which is 1 more than 0",
4021 int n()
const {
return n_; }
4023 void set_n(
int new_n) {
n_ = new_n; }
4026 const std::string& s()
const {
return s_; }
4028 const std::string& s_ref() const & {
return s_; }
4030 void set_s(
const std::string& new_s) {
s_ = new_s; }
4033 double&
x()
const {
return x_; }
4045 class DerivedClass :
public AClass {
4047 int k()
const {
return k_; }
4054 TEST(PropertyTest, WorksForNonReferenceProperty) {
4055 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
4056 Matcher<const AClass&> m_with_name = Property(
"n", &AClass::n, Ge(0));
4070 TEST(PropertyTest, WorksForReferenceToConstProperty) {
4071 Matcher<const AClass&> m = Property(&AClass::s, StartsWith(
"hi"));
4072 Matcher<const AClass&> m_with_name =
4073 Property(
"s", &AClass::s, StartsWith(
"hi"));
4087 TEST(PropertyTest, WorksForRefQualifiedProperty) {
4088 Matcher<const AClass&> m = Property(&AClass::s_ref, StartsWith(
"hi"));
4089 Matcher<const AClass&> m_with_name =
4090 Property(
"s", &AClass::s_ref, StartsWith(
"hi"));
4104 TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
4108 Matcher<const AClass&> m = Property(&
AClass::x, Ref(x));
4117 TEST(PropertyTest, WorksForByValueArgument) {
4118 Matcher<AClass> m = Property(&AClass::s, StartsWith(
"hi"));
4130 TEST(PropertyTest, WorksForArgumentOfSubType) {
4133 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
4145 TEST(PropertyTest, WorksForCompatibleMatcherType) {
4147 Matcher<const AClass&> m = Property(&AClass::n,
4148 Matcher<signed char>(Ge(0)));
4150 Matcher<const AClass&> m_with_name =
4151 Property(
"n", &AClass::n, Matcher<signed char>(Ge(0)));
4162 TEST(PropertyTest, CanDescribeSelf) {
4163 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
4165 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(m));
4166 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4167 DescribeNegation(m));
4170 TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
4171 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
4173 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(m));
4174 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4175 DescribeNegation(m));
4179 TEST(PropertyTest, CanExplainMatchResult) {
4180 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
4184 EXPECT_EQ(
"whose given property is 1" + OfType(
"int"), Explain(m, a));
4186 m = Property(&AClass::n, GreaterThan(0));
4188 "whose given property is 1" + OfType(
"int") +
", which is 1 more than 0",
4192 TEST(PropertyTest, CanExplainMatchResultWithPropertyName) {
4193 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
4197 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int"), Explain(m, a));
4199 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
4200 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int") +
4201 ", which is 1 more than 0",
4206 TEST(PropertyForPointerTest, WorksForPointerToConst) {
4207 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
4218 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
4219 Matcher<AClass*> m = Property(&AClass::s, StartsWith(
"hi"));
4231 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
4232 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith(
"hi"));
4243 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
4244 Matcher<const AClass*> m = Property(&
AClass::x,
_);
4250 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
4253 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
4264 TEST(PropertyForPointerTest, CanDescribeSelf) {
4265 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
4267 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(m));
4268 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4269 DescribeNegation(m));
4272 TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
4273 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
4275 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(m));
4276 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4277 DescribeNegation(m));
4281 TEST(PropertyForPointerTest, CanExplainMatchResult) {
4282 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
4286 EXPECT_EQ(
"", Explain(m, static_cast<const AClass*>(
nullptr)));
4288 "which points to an object whose given property is 1" + OfType(
"int"),
4291 m = Property(&AClass::n, GreaterThan(0));
4292 EXPECT_EQ(
"which points to an object whose given property is 1" +
4293 OfType(
"int") +
", which is 1 more than 0",
4297 TEST(PropertyForPointerTest, CanExplainMatchResultWithPropertyName) {
4298 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
4302 EXPECT_EQ(
"", Explain(m, static_cast<const AClass*>(
nullptr)));
4303 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4307 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
4308 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4309 OfType(
"int") +
", which is 1 more than 0",
4317 std::string IntToStringFunction(
int input) {
4318 return input == 1 ?
"foo" :
"bar";
4321 TEST(ResultOfTest, WorksForFunctionPointers) {
4322 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string(
"foo")));
4329 TEST(ResultOfTest, CanDescribeItself) {
4330 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq(
"foo"));
4332 EXPECT_EQ(
"is mapped by the given callable to a value that "
4333 "is equal to \"foo\"", Describe(matcher));
4334 EXPECT_EQ(
"is mapped by the given callable to a value that "
4335 "isn't equal to \"foo\"", DescribeNegation(matcher));
4339 int IntFunction(
int input) {
return input == 42 ? 80 : 90; }
4341 TEST(ResultOfTest, CanExplainMatchResult) {
4342 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
4343 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int"),
4344 Explain(matcher, 36));
4346 matcher = ResultOf(&IntFunction, GreaterThan(85));
4347 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int") +
4348 ", which is 5 more than 85", Explain(matcher, 36));
4353 TEST(ResultOfTest, WorksForNonReferenceResults) {
4354 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
4362 double& DoubleFunction(
double& input) {
return input; }
4364 Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
4368 TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
4371 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
4379 Matcher<Uncopyable&> matcher2 =
4380 ResultOf(&RefUncopyableFunction, Ref(obj));
4388 const std::string& StringFunction(
const std::string& input) {
return input; }
4390 TEST(ResultOfTest, WorksForReferenceToConstResults) {
4391 std::string s =
"foo";
4393 Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s));
4401 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
4403 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
4411 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
4413 ResultOf(
static_cast<std::string (*)(
int dummy)
>(
nullptr),
4414 Eq(std::string(
"foo"))),
4415 "NULL function pointer is passed into ResultOf\\(\\)\\.");
4420 TEST(ResultOfTest, WorksForFunctionReferences) {
4421 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq(
"foo"));
4429 std::string operator()(
int input)
const {
4430 return IntToStringFunction(input);
4434 TEST(ResultOfTest, WorksForFunctors) {
4435 Matcher<int> matcher = ResultOf(Functor(), Eq(std::string(
"foo")));
4444 struct PolymorphicFunctor {
4445 typedef int result_type;
4446 int operator()(
int n) {
return n; }
4447 int operator()(
const char* s) {
return static_cast<int>(strlen(s)); }
4448 std::string operator()(
int *p) {
return p ?
"good ptr" :
"null"; }
4451 TEST(ResultOfTest, WorksForPolymorphicFunctors) {
4452 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
4457 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
4459 EXPECT_TRUE(matcher_string.Matches(
"long string"));
4463 TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
4464 Matcher<int*> matcher = ResultOf(PolymorphicFunctor(),
"good ptr");
4471 TEST(ResultOfTest, WorksForLambdas) {
4472 Matcher<int> matcher = ResultOf(
4474 return std::string(static_cast<size_t>(str_len),
'x');
4481 TEST(ResultOfTest, WorksForNonCopyableArguments) {
4482 Matcher<std::unique_ptr<int>> matcher = ResultOf(
4483 [](
const std::unique_ptr<int>& str_len) {
4484 return std::string(static_cast<size_t>(*str_len),
'x');
4487 EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(
new int(3))));
4488 EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(
new int(1))));
4491 const int* ReferencingFunction(
const int& n) {
return &n; }
4493 struct ReferencingFunctor {
4494 typedef const int* result_type;
4495 result_type operator()(
const int& n) {
return &n; }
4498 TEST(ResultOfTest, WorksForReferencingCallables) {
4501 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
4505 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
4510 class DivisibleByImpl {
4512 explicit DivisibleByImpl(
int a_divider) :
divider_(a_divider) {}
4515 template <
typename T>
4516 bool MatchAndExplain(
const T& n, MatchResultListener* listener)
const {
4517 *listener <<
"which is " << (n %
divider_) <<
" modulo "
4522 void DescribeTo(ostream* os)
const {
4523 *os <<
"is divisible by " <<
divider_;
4526 void DescribeNegationTo(ostream* os)
const {
4527 *os <<
"is not divisible by " <<
divider_;
4530 void set_divider(
int a_divider) {
divider_ = a_divider; }
4531 int divider()
const {
return divider_; }
4537 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(
int n) {
4538 return MakePolymorphicMatcher(DivisibleByImpl(n));
4543 TEST(ExplainMatchResultTest, AllOf_False_False) {
4544 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
4545 EXPECT_EQ(
"which is 1 modulo 4", Explain(m, 5));
4550 TEST(ExplainMatchResultTest, AllOf_False_True) {
4551 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
4552 EXPECT_EQ(
"which is 2 modulo 4", Explain(m, 6));
4557 TEST(ExplainMatchResultTest, AllOf_True_False) {
4558 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
4559 EXPECT_EQ(
"which is 2 modulo 3", Explain(m, 5));
4564 TEST(ExplainMatchResultTest, AllOf_True_True) {
4565 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
4566 EXPECT_EQ(
"which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
4569 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
4570 const Matcher<int> m = AllOf(Ge(2), Le(3));
4574 TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
4575 const Matcher<int> m = GreaterThan(5);
4576 EXPECT_EQ(
"which is 1 more than 5", Explain(m, 6));
4585 explicit NotCopyable(
int a_value) :
value_(a_value) {}
4589 bool operator==(
const NotCopyable& rhs)
const {
4590 return value() == rhs.value();
4593 bool operator>=(
const NotCopyable& rhs)
const {
4594 return value() >= rhs.value();
4602 TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
4603 const NotCopyable const_value1(1);
4604 const Matcher<const NotCopyable&> m = Eq(
ByRef(const_value1));
4606 const NotCopyable n1(1), n2(2);
4611 TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
4612 NotCopyable value2(2);
4613 const Matcher<NotCopyable&> m = Ge(
ByRef(value2));
4615 NotCopyable n1(1), n2(2);
4620 TEST(IsEmptyTest, ImplementsIsEmpty) {
4621 vector<int> container;
4623 container.push_back(0);
4625 container.push_back(1);
4629 TEST(IsEmptyTest, WorksWithString) {
4634 text = std::string(
"\0", 1);
4638 TEST(IsEmptyTest, CanDescribeSelf) {
4639 Matcher<vector<int> > m = IsEmpty();
4641 EXPECT_EQ(
"isn't empty", DescribeNegation(m));
4644 TEST(IsEmptyTest, ExplainsResult) {
4645 Matcher<vector<int> > m = IsEmpty();
4646 vector<int> container;
4648 container.push_back(0);
4649 EXPECT_EQ(
"whose size is 1", Explain(m, container));
4652 TEST(IsEmptyTest, WorksWithMoveOnly) {
4653 ContainerHelper helper;
4658 TEST(IsTrueTest, IsTrueIsFalse) {
4686 std::unique_ptr<int> null_unique;
4687 std::unique_ptr<int> nonnull_unique(
new int(0));
4694 TEST(SizeIsTest, ImplementsSizeIs) {
4695 vector<int> container;
4698 container.push_back(0);
4701 container.push_back(0);
4706 TEST(SizeIsTest, WorksWithMap) {
4707 map<std::string, int> container;
4710 container.insert(make_pair(
"foo", 1));
4713 container.insert(make_pair(
"bar", 2));
4718 TEST(SizeIsTest, WorksWithReferences) {
4719 vector<int> container;
4720 Matcher<const vector<int>&> m = SizeIs(1);
4722 container.push_back(0);
4726 TEST(SizeIsTest, WorksWithMoveOnly) {
4727 ContainerHelper helper;
4729 helper.Call(MakeUniquePtrs({1, 2, 3}));
4734 struct MinimalistCustomType {
4735 int size()
const {
return 1; }
4737 TEST(SizeIsTest, WorksWithMinimalistCustomType) {
4738 MinimalistCustomType container;
4743 TEST(SizeIsTest, CanDescribeSelf) {
4744 Matcher<vector<int> > m = SizeIs(2);
4745 EXPECT_EQ(
"size is equal to 2", Describe(m));
4746 EXPECT_EQ(
"size isn't equal to 2", DescribeNegation(m));
4749 TEST(SizeIsTest, ExplainsResult) {
4750 Matcher<vector<int> > m1 = SizeIs(2);
4751 Matcher<vector<int> > m2 = SizeIs(Lt(2u));
4752 Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
4753 Matcher<vector<int> > m4 = SizeIs(Gt(1u));
4754 vector<int> container;
4755 EXPECT_EQ(
"whose size 0 doesn't match", Explain(m1, container));
4756 EXPECT_EQ(
"whose size 0 matches", Explain(m2, container));
4757 EXPECT_EQ(
"whose size 0 matches", Explain(m3, container));
4758 EXPECT_EQ(
"whose size 0 doesn't match", Explain(m4, container));
4759 container.push_back(0);
4760 container.push_back(0);
4761 EXPECT_EQ(
"whose size 2 matches", Explain(m1, container));
4762 EXPECT_EQ(
"whose size 2 doesn't match", Explain(m2, container));
4763 EXPECT_EQ(
"whose size 2 doesn't match", Explain(m3, container));
4764 EXPECT_EQ(
"whose size 2 matches", Explain(m4, container));
4767 #if GTEST_HAS_TYPED_TEST
4771 template <
typename T>
4779 ContainerEqTestTypes;
4785 static const int vals[] = {1, 1, 2, 3, 5, 8};
4786 TypeParam my_set(vals, vals + 6);
4787 const Matcher<TypeParam> m = ContainerEq(my_set);
4794 static const int vals[] = {1, 1, 2, 3, 5, 8};
4795 static const int test_vals[] = {2, 1, 8, 5};
4796 TypeParam my_set(vals, vals + 6);
4797 TypeParam test_set(test_vals, test_vals + 4);
4798 const Matcher<TypeParam> m = ContainerEq(my_set);
4800 EXPECT_EQ(
"which doesn't have these expected elements: 3",
4801 Explain(m, test_set));
4806 static const int vals[] = {1, 1, 2, 3, 5, 8};
4807 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
4808 TypeParam my_set(vals, vals + 6);
4809 TypeParam test_set(test_vals, test_vals + 6);
4810 const Matcher<const TypeParam&> m = ContainerEq(my_set);
4812 EXPECT_EQ(
"which has these unexpected elements: 46", Explain(m, test_set));
4816 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
4817 static const int vals[] = {1, 1, 2, 3, 5, 8};
4818 static const int test_vals[] = {1, 2, 3, 8, 46};
4819 TypeParam my_set(vals, vals + 6);
4820 TypeParam test_set(test_vals, test_vals + 5);
4821 const Matcher<TypeParam> m = ContainerEq(my_set);
4823 EXPECT_EQ(
"which has these unexpected elements: 46,\n"
4824 "and doesn't have these expected elements: 5",
4825 Explain(m, test_set));
4829 TYPED_TEST(ContainerEqTest, DuplicateDifference) {
4830 static const int vals[] = {1, 1, 2, 3, 5, 8};
4831 static const int test_vals[] = {1, 2, 3, 5, 8};
4832 TypeParam my_set(vals, vals + 6);
4833 TypeParam test_set(test_vals, test_vals + 5);
4834 const Matcher<const TypeParam&> m = ContainerEq(my_set);
4839 #endif // GTEST_HAS_TYPED_TEST
4843 TEST(ContainerEqExtraTest, MultipleValuesMissing) {
4844 static const int vals[] = {1, 1, 2, 3, 5, 8};
4845 static const int test_vals[] = {2, 1, 5};
4846 vector<int> my_set(vals, vals + 6);
4847 vector<int> test_set(test_vals, test_vals + 3);
4848 const Matcher<vector<int> > m = ContainerEq(my_set);
4850 EXPECT_EQ(
"which doesn't have these expected elements: 3, 8",
4851 Explain(m, test_set));
4856 TEST(ContainerEqExtraTest, MultipleValuesAdded) {
4857 static const int vals[] = {1, 1, 2, 3, 5, 8};
4858 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
4859 list<size_t> my_set(vals, vals + 6);
4860 list<size_t> test_set(test_vals, test_vals + 7);
4861 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
4863 EXPECT_EQ(
"which has these unexpected elements: 92, 46",
4864 Explain(m, test_set));
4868 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
4869 static const int vals[] = {1, 1, 2, 3, 5, 8};
4870 static const int test_vals[] = {1, 2, 3, 92, 46};
4871 list<size_t> my_set(vals, vals + 6);
4872 list<size_t> test_set(test_vals, test_vals + 5);
4873 const Matcher<const list<size_t> > m = ContainerEq(my_set);
4875 EXPECT_EQ(
"which has these unexpected elements: 92, 46,\n"
4876 "and doesn't have these expected elements: 5, 8",
4877 Explain(m, test_set));
4882 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
4883 static const int vals[] = {1, 1, 2, 3, 5, 8};
4884 static const int test_vals[] = {1, 2, 3, 5, 8};
4885 vector<int> my_set(vals, vals + 6);
4886 vector<int> test_set(test_vals, test_vals + 5);
4887 const Matcher<vector<int> > m = ContainerEq(my_set);
4896 TEST(ContainerEqExtraTest, WorksForMaps) {
4897 map<int, std::string> my_map;
4901 map<int, std::string> test_map;
4905 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
4909 EXPECT_EQ(
"which has these unexpected elements: (0, \"aa\"),\n"
4910 "and doesn't have these expected elements: (0, \"a\")",
4911 Explain(m, test_map));
4914 TEST(ContainerEqExtraTest, WorksForNativeArray) {
4915 int a1[] = {1, 2, 3};
4916 int a2[] = {1, 2, 3};
4917 int b[] = {1, 2, 4};
4923 TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
4924 const char a1[][3] = {
"hi",
"lo"};
4925 const char a2[][3] = {
"hi",
"lo"};
4926 const char b[][3] = {
"lo",
"hi"};
4933 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
4934 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
4937 TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
4938 const int a1[] = {1, 2, 3};
4939 const int a2[] = {1, 2, 3};
4940 const int b[] = {1, 2, 3, 4};
4942 const int*
const p1 = a1;
4943 EXPECT_THAT(std::make_tuple(p1, 3), ContainerEq(a2));
4944 EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(b)));
4946 const int c[] = {1, 3, 2};
4947 EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(c)));
4950 TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
4951 std::string a1[][3] = {
4952 {
"hi",
"hello",
"ciao"},
4953 {
"bye",
"see you",
"ciao"}
4956 std::string a2[][3] = {
4957 {
"hi",
"hello",
"ciao"},
4958 {
"bye",
"see you",
"ciao"}
4961 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
4968 TEST(WhenSortedByTest, WorksForEmptyContainer) {
4969 const vector<int> numbers;
4970 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
4971 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
4974 TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
4975 vector<unsigned> numbers;
4976 numbers.push_back(3);
4977 numbers.push_back(1);
4978 numbers.push_back(2);
4979 numbers.push_back(2);
4980 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
4981 ElementsAre(3, 2, 2, 1)));
4982 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
4983 ElementsAre(1, 2, 2, 3))));
4986 TEST(WhenSortedByTest, WorksForNonVectorContainer) {
4987 list<std::string> words;
4988 words.push_back(
"say");
4989 words.push_back(
"hello");
4990 words.push_back(
"world");
4991 EXPECT_THAT(words, WhenSortedBy(less<std::string>(),
4992 ElementsAre(
"hello",
"say",
"world")));
4993 EXPECT_THAT(words, Not(WhenSortedBy(less<std::string>(),
4994 ElementsAre(
"say",
"hello",
"world"))));
4997 TEST(WhenSortedByTest, WorksForNativeArray) {
4998 const int numbers[] = {1, 3, 2, 4};
4999 const int sorted_numbers[] = {1, 2, 3, 4};
5000 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
5002 ElementsAreArray(sorted_numbers)));
5003 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
5006 TEST(WhenSortedByTest, CanDescribeSelf) {
5007 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
5008 EXPECT_EQ(
"(when sorted) has 2 elements where\n"
5009 "element #0 is equal to 1,\n"
5010 "element #1 is equal to 2",
5012 EXPECT_EQ(
"(when sorted) doesn't have 2 elements, or\n"
5013 "element #0 isn't equal to 1, or\n"
5014 "element #1 isn't equal to 2",
5015 DescribeNegation(m));
5018 TEST(WhenSortedByTest, ExplainsMatchResult) {
5019 const int a[] = {2, 1};
5020 EXPECT_EQ(
"which is { 1, 2 } when sorted, whose element #0 doesn't match",
5021 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
5022 EXPECT_EQ(
"which is { 1, 2 } when sorted",
5023 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
5029 TEST(WhenSortedTest, WorksForEmptyContainer) {
5030 const vector<int> numbers;
5032 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
5035 TEST(WhenSortedTest, WorksForNonEmptyContainer) {
5036 list<std::string> words;
5037 words.push_back(
"3");
5038 words.push_back(
"1");
5039 words.push_back(
"2");
5040 words.push_back(
"2");
5041 EXPECT_THAT(words, WhenSorted(ElementsAre(
"1",
"2",
"2",
"3")));
5042 EXPECT_THAT(words, Not(WhenSorted(ElementsAre(
"3",
"1",
"2",
"2"))));
5045 TEST(WhenSortedTest, WorksForMapTypes) {
5046 map<std::string, int> word_counts;
5047 word_counts[
"and"] = 1;
5048 word_counts[
"the"] = 1;
5049 word_counts[
"buffalo"] = 2;
5051 WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"buffalo", 2),
5054 Not(WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"the", 1),
5055 Pair(
"buffalo", 2)))));
5058 TEST(WhenSortedTest, WorksForMultiMapTypes) {
5059 multimap<int, int> ifib;
5060 ifib.insert(make_pair(8, 6));
5061 ifib.insert(make_pair(2, 3));
5062 ifib.insert(make_pair(1, 1));
5063 ifib.insert(make_pair(3, 4));
5064 ifib.insert(make_pair(1, 2));
5065 ifib.insert(make_pair(5, 5));
5066 EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1),
5072 EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6),
5080 TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
5085 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
5088 TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
5092 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
5094 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
5095 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
5100 template <
typename T>
5105 typedef ConstIter const_iterator;
5106 typedef T value_type;
5108 template <
typename InIter>
5109 Streamlike(InIter first, InIter last) :
remainder_(first, last) {}
5111 const_iterator begin()
const {
5112 return const_iterator(
this,
remainder_.begin());
5114 const_iterator end()
const {
5115 return const_iterator(
this,
remainder_.end());
5119 class ConstIter :
public std::iterator<std::input_iterator_tag,
5123 const value_type&> {
5125 ConstIter(
const Streamlike* s,
5126 typename std::list<value_type>::iterator pos)
5130 const value_type* operator->()
const {
return &*
pos_; }
5131 ConstIter& operator++() {
5132 s_->remainder_.erase(
pos_++);
5138 class PostIncrProxy {
5140 explicit PostIncrProxy(
const value_type& value) :
value_(value) {}
5145 PostIncrProxy operator++(
int) {
5146 PostIncrProxy proxy(**
this);
5151 friend bool operator==(
const ConstIter& a,
const ConstIter& b) {
5152 return a.s_ == b.s_ && a.pos_ == b.pos_;
5154 friend bool operator!=(
const ConstIter& a,
const ConstIter& b) {
5159 const Streamlike*
s_;
5160 typename std::list<value_type>::iterator
pos_;
5163 friend std::ostream&
operator<<(std::ostream& os,
const Streamlike& s) {
5165 typedef typename std::list<value_type>::const_iterator Iter;
5166 const char* sep =
"";
5167 for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
5178 TEST(StreamlikeTest, Iteration) {
5179 const int a[5] = {2, 1, 4, 5, 3};
5180 Streamlike<int> s(a, a + 5);
5181 Streamlike<int>::const_iterator it = s.begin();
5183 while (it != s.end()) {
5189 TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
5190 std::forward_list<int> container;
5192 EXPECT_THAT(container, Not(BeginEndDistanceIs(1)));
5193 container.push_front(0);
5194 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
5196 container.push_front(0);
5197 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
5201 TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
5202 const int a[5] = {1, 2, 3, 4, 5};
5203 Streamlike<int> s(a, a + 5);
5207 TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
5208 Matcher<vector<int> > m = BeginEndDistanceIs(2);
5209 EXPECT_EQ(
"distance between begin() and end() is equal to 2", Describe(m));
5210 EXPECT_EQ(
"distance between begin() and end() isn't equal to 2",
5211 DescribeNegation(m));
5214 TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
5215 ContainerHelper helper;
5217 helper.Call(MakeUniquePtrs({1, 2}));
5220 TEST(BeginEndDistanceIsTest, ExplainsResult) {
5221 Matcher<vector<int> > m1 = BeginEndDistanceIs(2);
5222 Matcher<vector<int> > m2 = BeginEndDistanceIs(Lt(2));
5223 Matcher<vector<int> > m3 = BeginEndDistanceIs(AnyOf(0, 3));
5224 Matcher<vector<int> > m4 = BeginEndDistanceIs(GreaterThan(1));
5225 vector<int> container;
5226 EXPECT_EQ(
"whose distance between begin() and end() 0 doesn't match",
5227 Explain(m1, container));
5228 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5229 Explain(m2, container));
5230 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5231 Explain(m3, container));
5233 "whose distance between begin() and end() 0 doesn't match, which is 1 "
5235 Explain(m4, container));
5236 container.push_back(0);
5237 container.push_back(0);
5238 EXPECT_EQ(
"whose distance between begin() and end() 2 matches",
5239 Explain(m1, container));
5240 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5241 Explain(m2, container));
5242 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5243 Explain(m3, container));
5245 "whose distance between begin() and end() 2 matches, which is 1 more "
5247 Explain(m4, container));
5250 TEST(WhenSortedTest, WorksForStreamlike) {
5253 const int a[5] = {2, 1, 4, 5, 3};
5254 Streamlike<int> s(std::begin(a), std::end(a));
5255 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
5256 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
5259 TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
5260 const int a[] = {2, 1, 4, 5, 3};
5261 Streamlike<int> s(std::begin(a), std::end(a));
5262 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
5264 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
5267 TEST(IsSupersetOfTest, WorksForNativeArray) {
5268 const int subset[] = {1, 4};
5269 const int superset[] = {1, 2, 4};
5270 const int disjoint[] = {1, 0, 3};
5278 TEST(IsSupersetOfTest, WorksWithDuplicates) {
5279 const int not_enough[] = {1, 2};
5280 const int enough[] = {1, 1, 2};
5282 EXPECT_THAT(not_enough, Not(IsSupersetOf(expected)));
5286 TEST(IsSupersetOfTest, WorksForEmpty) {
5287 vector<int> numbers;
5290 expected.push_back(1);
5291 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
5293 numbers.push_back(1);
5294 numbers.push_back(2);
5296 expected.push_back(1);
5298 expected.push_back(2);
5300 expected.push_back(3);
5301 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
5304 TEST(IsSupersetOfTest, WorksForStreamlike) {
5305 const int a[5] = {1, 2, 3, 4, 5};
5306 Streamlike<int> s(std::begin(a), std::end(a));
5309 expected.push_back(1);
5310 expected.push_back(2);
5311 expected.push_back(5);
5314 expected.push_back(0);
5318 TEST(IsSupersetOfTest, TakesStlContainer) {
5319 const int actual[] = {3, 1, 2};
5322 expected.push_back(1);
5323 expected.push_back(3);
5326 expected.push_back(4);
5330 TEST(IsSupersetOfTest, Describe) {
5331 typedef std::vector<int> IntVec;
5333 expected.push_back(111);
5334 expected.push_back(222);
5335 expected.push_back(333);
5337 Describe<IntVec>(IsSupersetOf(expected)),
5338 Eq(
"a surjection from elements to requirements exists such that:\n"
5339 " - an element is equal to 111\n"
5340 " - an element is equal to 222\n"
5341 " - an element is equal to 333"));
5344 TEST(IsSupersetOfTest, DescribeNegation) {
5345 typedef std::vector<int> IntVec;
5347 expected.push_back(111);
5348 expected.push_back(222);
5349 expected.push_back(333);
5351 DescribeNegation<IntVec>(IsSupersetOf(expected)),
5352 Eq(
"no surjection from elements to requirements exists such that:\n"
5353 " - an element is equal to 111\n"
5354 " - an element is equal to 222\n"
5355 " - an element is equal to 333"));
5358 TEST(IsSupersetOfTest, MatchAndExplain) {
5363 expected.push_back(1);
5364 expected.push_back(2);
5365 StringMatchResultListener listener;
5366 ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
5369 Eq(
"where the following matchers don't match any elements:\n"
5370 "matcher #0: is equal to 1"));
5374 ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
5377 " - element #0 is matched by matcher #1,\n"
5378 " - element #2 is matched by matcher #0"));
5381 TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
5382 const int numbers[] = {1, 3, 6, 2, 4, 5};
5387 TEST(IsSupersetOfTest, WorksWithMoveOnly) {
5388 ContainerHelper helper;
5389 EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));
5390 helper.Call(MakeUniquePtrs({1, 2}));
5391 EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));
5392 helper.Call(MakeUniquePtrs({2}));
5395 TEST(IsSubsetOfTest, WorksForNativeArray) {
5396 const int subset[] = {1, 4};
5397 const int superset[] = {1, 2, 4};
5398 const int disjoint[] = {1, 0, 3};
5406 TEST(IsSubsetOfTest, WorksWithDuplicates) {
5407 const int not_enough[] = {1, 2};
5408 const int enough[] = {1, 1, 2};
5409 const int actual[] = {1, 1};
5414 TEST(IsSubsetOfTest, WorksForEmpty) {
5415 vector<int> numbers;
5418 expected.push_back(1);
5421 numbers.push_back(1);
5422 numbers.push_back(2);
5424 expected.push_back(1);
5426 expected.push_back(2);
5428 expected.push_back(3);
5432 TEST(IsSubsetOfTest, WorksForStreamlike) {
5433 const int a[5] = {1, 2};
5434 Streamlike<int> s(std::begin(a), std::end(a));
5437 expected.push_back(1);
5439 expected.push_back(2);
5440 expected.push_back(5);
5444 TEST(IsSubsetOfTest, TakesStlContainer) {
5445 const int actual[] = {3, 1, 2};
5448 expected.push_back(1);
5449 expected.push_back(3);
5452 expected.push_back(2);
5453 expected.push_back(4);
5457 TEST(IsSubsetOfTest, Describe) {
5458 typedef std::vector<int> IntVec;
5460 expected.push_back(111);
5461 expected.push_back(222);
5462 expected.push_back(333);
5465 Describe<IntVec>(IsSubsetOf(expected)),
5466 Eq(
"an injection from elements to requirements exists such that:\n"
5467 " - an element is equal to 111\n"
5468 " - an element is equal to 222\n"
5469 " - an element is equal to 333"));
5472 TEST(IsSubsetOfTest, DescribeNegation) {
5473 typedef std::vector<int> IntVec;
5475 expected.push_back(111);
5476 expected.push_back(222);
5477 expected.push_back(333);
5479 DescribeNegation<IntVec>(IsSubsetOf(expected)),
5480 Eq(
"no injection from elements to requirements exists such that:\n"
5481 " - an element is equal to 111\n"
5482 " - an element is equal to 222\n"
5483 " - an element is equal to 333"));
5486 TEST(IsSubsetOfTest, MatchAndExplain) {
5491 expected.push_back(1);
5492 expected.push_back(2);
5493 StringMatchResultListener listener;
5494 ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
5497 Eq(
"where the following elements don't match any matchers:\n"
5500 expected.push_back(3);
5502 ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
5505 " - element #0 is matched by matcher #1,\n"
5506 " - element #1 is matched by matcher #2"));
5509 TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
5510 const int numbers[] = {1, 2, 3};
5515 TEST(IsSubsetOfTest, WorksWithMoveOnly) {
5516 ContainerHelper helper;
5517 EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));
5518 helper.Call(MakeUniquePtrs({1}));
5519 EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));
5520 helper.Call(MakeUniquePtrs({2}));
5526 TEST(ElemensAreStreamTest, WorksForStreamlike) {
5527 const int a[5] = {1, 2, 3, 4, 5};
5528 Streamlike<int> s(std::begin(a), std::end(a));
5533 TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
5534 const int a[5] = {1, 2, 3, 4, 5};
5535 Streamlike<int> s(std::begin(a), std::end(a));
5538 expected.push_back(1);
5539 expected.push_back(2);
5540 expected.push_back(3);
5541 expected.push_back(4);
5542 expected.push_back(5);
5549 TEST(ElementsAreTest, WorksWithUncopyable) {
5551 objs[0].set_value(-3);
5552 objs[1].set_value(1);
5553 EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));
5556 TEST(ElementsAreTest, WorksWithMoveOnly) {
5557 ContainerHelper helper;
5558 EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));
5559 helper.Call(MakeUniquePtrs({1, 2}));
5561 EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));
5562 helper.Call(MakeUniquePtrs({3, 4}));
5565 TEST(ElementsAreTest, TakesStlContainer) {
5566 const int actual[] = {3, 1, 2};
5569 expected.push_back(3);
5570 expected.push_back(1);
5571 expected.push_back(2);
5574 expected.push_back(4);
5575 EXPECT_THAT(actual, Not(ElementsAreArray(expected)));
5580 TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
5581 const int a[] = {0, 1, 2, 3, 4};
5582 std::vector<int> s(std::begin(a), std::end(a));
5584 StringMatchResultListener listener;
5585 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a),
5586 s, &listener)) << listener.str();
5587 }
while (std::next_permutation(s.begin(), s.end()));
5590 TEST(UnorderedElementsAreArrayTest, VectorBool) {
5591 const bool a[] = {0, 1, 0, 1, 1};
5592 const bool b[] = {1, 0, 1, 1, 0};
5593 std::vector<bool>
expected(std::begin(a), std::end(a));
5594 std::vector<bool> actual(std::begin(b), std::end(b));
5595 StringMatchResultListener listener;
5596 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected),
5597 actual, &listener)) << listener.str();
5600 TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
5604 const int a[5] = {2, 1, 4, 5, 3};
5605 Streamlike<int> s(std::begin(a), std::end(a));
5608 expected.push_back(1);
5609 expected.push_back(2);
5610 expected.push_back(3);
5611 expected.push_back(4);
5612 expected.push_back(5);
5613 EXPECT_THAT(s, UnorderedElementsAreArray(expected));
5615 expected.push_back(6);
5616 EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));
5619 TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
5620 const int actual[] = {3, 1, 2};
5623 expected.push_back(1);
5624 expected.push_back(2);
5625 expected.push_back(3);
5626 EXPECT_THAT(actual, UnorderedElementsAreArray(expected));
5628 expected.push_back(4);
5629 EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected)));
5633 TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
5634 const int a[5] = {2, 1, 4, 5, 3};
5635 EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5}));
5636 EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6})));
5639 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
5640 const std::string a[5] = {
"a",
"b",
"c",
"d",
"e"};
5641 EXPECT_THAT(a, UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
5642 EXPECT_THAT(a, Not(UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
5645 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
5646 const int a[5] = {2, 1, 4, 5, 3};
5648 {Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
5650 {Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
5653 TEST(UnorderedElementsAreArrayTest,
5654 TakesInitializerListOfDifferentTypedMatchers) {
5655 const int a[5] = {2, 1, 4, 5, 3};
5659 EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int> >(
5660 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
5661 EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int> >(
5662 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
5666 TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
5667 ContainerHelper helper;
5669 Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)})));
5670 helper.Call(MakeUniquePtrs({2, 1}));
5675 typedef std::vector<int> IntVec;
5678 TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
5680 objs[0].set_value(-3);
5681 objs[1].set_value(1);
5683 UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3)));
5686 TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
5687 const int a[] = {1, 2, 3};
5688 std::vector<int> s(std::begin(a), std::end(a));
5690 StringMatchResultListener listener;
5691 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
5692 s, &listener)) << listener.str();
5693 }
while (std::next_permutation(s.begin(), s.end()));
5696 TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
5697 const int a[] = {1, 2, 3};
5698 std::vector<int> s(std::begin(a), std::end(a));
5699 std::vector<Matcher<int> > mv;
5704 StringMatchResultListener listener;
5705 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv),
5706 s, &listener)) << listener.str();
5709 TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
5713 const int a[5] = {2, 1, 4, 5, 3};
5714 Streamlike<int> s(std::begin(a), std::end(a));
5716 EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
5717 EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
5720 TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
5721 ContainerHelper helper;
5722 EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2))));
5723 helper.Call(MakeUniquePtrs({2, 1}));
5732 TEST_F(UnorderedElementsAreTest, Performance) {
5734 std::vector<Matcher<int> > mv;
5735 for (
int i = 0;
i < 100; ++
i) {
5740 StringMatchResultListener listener;
5741 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv),
5742 s, &listener)) << listener.str();
5748 TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
5750 std::vector<Matcher<int> > mv;
5751 for (
int i = 0;
i < 100; ++
i) {
5759 StringMatchResultListener listener;
5760 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv),
5761 s, &listener)) << listener.str();
5764 TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
5767 StringMatchResultListener listener;
5768 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
5769 v, &listener)) << listener.str();
5770 EXPECT_THAT(listener.str(), Eq(
"which has 1 element"));
5773 TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
5775 StringMatchResultListener listener;
5776 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
5777 v, &listener)) << listener.str();
5781 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
5785 StringMatchResultListener listener;
5786 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2),
5787 v, &listener)) << listener.str();
5790 Eq(
"where the following matchers don't match any elements:\n"
5791 "matcher #1: is equal to 2"));
5794 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
5798 StringMatchResultListener listener;
5799 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1),
5800 v, &listener)) << listener.str();
5803 Eq(
"where the following elements don't match any matchers:\n"
5807 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
5811 StringMatchResultListener listener;
5812 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2),
5813 v, &listener)) << listener.str();
5817 " the following matchers don't match any elements:\n"
5818 "matcher #0: is equal to 1\n"
5821 " the following elements don't match any matchers:\n"
5826 static std::string EMString(
int element,
int matcher) {
5828 ss <<
"(element #" << element <<
", matcher #" << matcher <<
")";
5832 TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
5835 std::vector<std::string> v;
5839 StringMatchResultListener listener;
5841 UnorderedElementsAre(
"a",
"a", AnyOf(
"b",
"c")), v, &listener))
5844 std::string prefix =
5845 "where no permutation of the elements can satisfy all matchers, "
5846 "and the closest match is 2 of 3 matchers with the "
5852 AnyOf(prefix +
"{\n " + EMString(0, 0) +
5853 ",\n " + EMString(1, 2) +
"\n}",
5854 prefix +
"{\n " + EMString(0, 1) +
5855 ",\n " + EMString(1, 2) +
"\n}",
5856 prefix +
"{\n " + EMString(0, 0) +
5857 ",\n " + EMString(2, 2) +
"\n}",
5858 prefix +
"{\n " + EMString(0, 1) +
5859 ",\n " + EMString(2, 2) +
"\n}"));
5862 TEST_F(UnorderedElementsAreTest, Describe) {
5863 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()),
5866 Describe<IntVec>(UnorderedElementsAre(345)),
5867 Eq(
"has 1 element and that element is equal to 345"));
5869 Describe<IntVec>(UnorderedElementsAre(111, 222, 333)),
5870 Eq(
"has 3 elements and there exists some permutation "
5871 "of elements such that:\n"
5872 " - element #0 is equal to 111, and\n"
5873 " - element #1 is equal to 222, and\n"
5874 " - element #2 is equal to 333"));
5877 TEST_F(UnorderedElementsAreTest, DescribeNegation) {
5878 EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()),
5881 DescribeNegation<IntVec>(UnorderedElementsAre(345)),
5882 Eq(
"doesn't have 1 element, or has 1 element that isn't equal to 345"));
5884 DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)),
5885 Eq(
"doesn't have 3 elements, or there exists no permutation "
5886 "of elements such that:\n"
5887 " - element #0 is equal to 123, and\n"
5888 " - element #1 is equal to 234, and\n"
5889 " - element #2 is equal to 345"));
5898 template <
typename Graph>
5899 class BacktrackingMaxBPMState {
5902 explicit BacktrackingMaxBPMState(
const Graph* g) :
graph_(g) { }
5904 ElementMatcherPairs Compute() {
5905 if (
graph_->LhsSize() == 0 ||
graph_->RhsSize() == 0) {
5910 for (
size_t irhs = 0; irhs <
graph_->RhsSize(); ++irhs) {
5920 static const size_t kUnused =
static_cast<size_t>(-1);
5922 void PushMatch(
size_t lhs,
size_t rhs) {
5923 matches_.push_back(ElementMatcherPair(lhs, rhs));
5932 const ElementMatcherPair& back =
matches_.back();
5938 bool RecurseInto(
size_t irhs) {
5942 for (
size_t ilhs = 0; ilhs <
graph_->LhsSize(); ++ilhs) {
5946 if (!
graph_->HasEdge(ilhs, irhs)) {
5949 PushMatch(ilhs, irhs);
5953 for (
size_t mi = irhs + 1; mi <
graph_->RhsSize(); ++mi) {
5954 if (!RecurseInto(mi))
return false;
5968 template <
typename Graph>
5975 template <
typename Graph>
5977 FindBacktrackingMaxBPM(
const Graph& g) {
5978 return BacktrackingMaxBPMState<Graph>(&g).Compute();
5988 TEST_P(BipartiteTest, Exhaustive) {
5989 size_t nodes = GetParam();
5990 MatchMatrix graph(nodes, nodes);
5992 ElementMatcherPairs matches =
5994 EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), matches.size())
5995 <<
"graph: " << graph.DebugString();
5998 std::vector<bool> seen_element(graph.LhsSize());
5999 std::vector<bool> seen_matcher(graph.RhsSize());
6001 for (
size_t i = 0;
i < matches.size(); ++
i) {
6002 size_t ilhs = matches[
i].first;
6003 size_t irhs = matches[
i].second;
6007 seen_element[ilhs] =
true;
6008 seen_matcher[irhs] =
true;
6010 }
while (graph.NextGraph());
6017 class BipartiteNonSquareTest
6021 TEST_F(BipartiteNonSquareTest, SimpleBacktracking) {
6029 MatchMatrix
g(4, 3);
6030 constexpr std::array<std::array<size_t, 2>, 4> kEdges = {
6031 {{{0, 2}}, {{1, 1}}, {{2, 1}}, {{3, 0}}}};
6032 for (
size_t i = 0;
i < kEdges.size(); ++
i) {
6033 g.SetEdge(kEdges[
i][0], kEdges[i][1],
true);
6036 ElementsAre(Pair(3, 0),
6037 Pair(AnyOf(1, 2), 1),
6038 Pair(0, 2))) <<
g.DebugString();
6042 TEST_P(BipartiteNonSquareTest, Exhaustive) {
6043 size_t nlhs = GetParam().first;
6044 size_t nrhs = GetParam().second;
6045 MatchMatrix graph(nlhs, nrhs);
6047 EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
6049 <<
"graph: " << graph.DebugString()
6050 <<
"\nbacktracking: "
6054 }
while (graph.NextGraph());
6059 std::make_pair(1, 2),
6060 std::make_pair(2, 1),
6061 std::make_pair(3, 2),
6062 std::make_pair(2, 3),
6063 std::make_pair(4, 1),
6064 std::make_pair(1, 4),
6065 std::make_pair(4, 3),
6066 std::make_pair(3, 4)));
6068 class BipartiteRandomTest
6073 TEST_P(BipartiteRandomTest, LargerNets) {
6074 int nodes = GetParam().first;
6075 int iters = GetParam().second;
6076 MatchMatrix graph(static_cast<size_t>(nodes), static_cast<size_t>(nodes));
6078 auto seed =
static_cast<uint32_t
>(
GTEST_FLAG(random_seed));
6080 seed =
static_cast<uint32_t
>(time(
nullptr));
6083 for (; iters > 0; --iters, ++seed) {
6084 srand(static_cast<unsigned int>(seed));
6086 EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
6088 <<
" graph: " << graph.DebugString()
6089 <<
"\nTo reproduce the failure, rerun the test with the flag"
6097 std::make_pair(5, 10000),
6098 std::make_pair(6, 5000),
6099 std::make_pair(7, 2000),
6100 std::make_pair(8, 500),
6101 std::make_pair(9, 100)));
6105 TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
6107 EXPECT_TRUE(IsReadableTypeName(
"const unsigned char*"));
6108 EXPECT_TRUE(IsReadableTypeName(
"MyMap<int, void*>"));
6109 EXPECT_TRUE(IsReadableTypeName(
"void (*)(int, bool)"));
6112 TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
6113 EXPECT_TRUE(IsReadableTypeName(
"my_long_namespace::MyClassName"));
6114 EXPECT_TRUE(IsReadableTypeName(
"int [5][6][7][8][9][10][11]"));
6115 EXPECT_TRUE(IsReadableTypeName(
"my_namespace::MyOuterClass::MyInnerClass"));
6118 TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
6120 IsReadableTypeName(
"basic_string<char, std::char_traits<char> >"));
6121 EXPECT_FALSE(IsReadableTypeName(
"std::vector<int, std::alloc_traits<int> >"));
6124 TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
6125 EXPECT_FALSE(IsReadableTypeName(
"void (&)(int, bool, char, float)"));
6130 TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
6136 const char* params[] = {
"5"};
6139 Strings(params, params + 1)));
6141 const char* params2[] = {
"5",
"8"};
6144 Strings(params2, params2 + 2)));
6148 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
6149 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
6150 DivisibleByImpl& impl = m.mutable_impl();
6153 impl.set_divider(0);
6154 EXPECT_EQ(0, m.mutable_impl().divider());
6158 TEST(PolymorphicMatcherTest, CanAccessImpl) {
6159 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
6160 const DivisibleByImpl& impl = m.impl();
6164 TEST(MatcherTupleTest, ExplainsMatchFailure) {
6166 ExplainMatchFailureTupleTo(
6167 std::make_tuple(Matcher<char>(Eq(
'a')), GreaterThan(5)),
6168 std::make_tuple(
'a', 10), &ss1);
6172 ExplainMatchFailureTupleTo(
6173 std::make_tuple(GreaterThan(5), Matcher<char>(Eq(
'a'))),
6174 std::make_tuple(2,
'b'), &ss2);
6176 " Actual: 2, which is 3 less than 5\n"
6177 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
6178 " Actual: 'b' (98, 0x62)\n",
6182 ExplainMatchFailureTupleTo(
6183 std::make_tuple(GreaterThan(5), Matcher<char>(Eq(
'a'))),
6184 std::make_tuple(2,
'a'), &ss3);
6186 " Actual: 2, which is 3 less than 5\n",
6193 TEST(EachTest, ExplainsMatchResultCorrectly) {
6196 Matcher<set<int> > m = Each(2);
6199 Matcher<const int(&)[1]> n = Each(1);
6201 const int b[1] = {1};
6205 EXPECT_EQ(
"whose element #0 doesn't match", Explain(n, b));
6210 m = Each(GreaterThan(0));
6213 m = Each(GreaterThan(10));
6214 EXPECT_EQ(
"whose element #0 doesn't match, which is 9 less than 10",
6218 TEST(EachTest, DescribesItselfCorrectly) {
6219 Matcher<vector<int> > m = Each(1);
6220 EXPECT_EQ(
"only contains elements that is equal to 1", Describe(m));
6222 Matcher<vector<int> > m2 = Not(m);
6223 EXPECT_EQ(
"contains some element that isn't equal to 1", Describe(m2));
6226 TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
6227 vector<int> some_vector;
6229 some_vector.push_back(3);
6232 some_vector.push_back(1);
6233 some_vector.push_back(2);
6237 vector<std::string> another_vector;
6238 another_vector.push_back(
"fee");
6239 EXPECT_THAT(another_vector, Each(std::string(
"fee")));
6240 another_vector.push_back(
"fie");
6241 another_vector.push_back(
"foe");
6242 another_vector.push_back(
"fum");
6243 EXPECT_THAT(another_vector, Not(Each(std::string(
"fee"))));
6246 TEST(EachTest, MatchesMapWhenAllElementsMatch) {
6247 map<const char*, int> my_map;
6248 const char*
bar =
"a string";
6252 map<std::string, int> another_map;
6253 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
6254 another_map[
"fee"] = 1;
6255 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
6256 another_map[
"fie"] = 2;
6257 another_map[
"foe"] = 3;
6258 another_map[
"fum"] = 4;
6259 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fee"), 1))));
6260 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fum"), 1))));
6264 TEST(EachTest, AcceptsMatcher) {
6265 const int a[] = {1, 2, 3};
6270 TEST(EachTest, WorksForNativeArrayAsTuple) {
6271 const int a[] = {1, 2};
6272 const int*
const pointer =
a;
6273 EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0)));
6274 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1))));
6277 TEST(EachTest, WorksWithMoveOnly) {
6278 ContainerHelper helper;
6280 helper.Call(MakeUniquePtrs({1, 2}));
6284 class IsHalfOfMatcher {
6286 template <
typename T1,
typename T2>
6287 bool MatchAndExplain(
const std::tuple<T1, T2>& a_pair,
6288 MatchResultListener* listener)
const {
6289 if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
6290 *listener <<
"where the second is " << std::get<1>(a_pair);
6293 *listener <<
"where the second/2 is " << std::get<1>(a_pair) / 2;
6298 void DescribeTo(ostream* os)
const {
6299 *os <<
"are a pair where the first is half of the second";
6302 void DescribeNegationTo(ostream* os)
const {
6303 *os <<
"are a pair where the first isn't half of the second";
6307 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
6308 return MakePolymorphicMatcher(IsHalfOfMatcher());
6311 TEST(PointwiseTest, DescribesSelf) {
6316 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
6317 EXPECT_EQ(
"contains 3 values, where each value and its corresponding value "
6318 "in { 1, 2, 3 } are a pair where the first is half of the second",
6320 EXPECT_EQ(
"doesn't contain exactly 3 values, or contains a value x at some "
6321 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
6322 "where the first isn't half of the second",
6323 DescribeNegation(m));
6326 TEST(PointwiseTest, MakesCopyOfRhs) {
6327 list<signed char> rhs;
6332 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
6340 TEST(PointwiseTest, WorksForLhsNativeArray) {
6341 const int lhs[] = {1, 2, 3};
6350 TEST(PointwiseTest, WorksForRhsNativeArray) {
6351 const int rhs[] = {1, 2, 3};
6361 TEST(PointwiseTest, WorksForVectorOfBool) {
6362 vector<bool> rhs(3,
false);
6364 vector<bool> lhs = rhs;
6371 TEST(PointwiseTest, WorksForRhsInitializerList) {
6372 const vector<int> lhs{2, 4, 6};
6374 EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));
6378 TEST(PointwiseTest, RejectsWrongSize) {
6379 const double lhs[2] = {1, 2};
6380 const int rhs[1] = {0};
6383 Explain(Pointwise(Gt(), rhs), lhs));
6385 const int rhs2[3] = {0, 1, 2};
6389 TEST(PointwiseTest, RejectsWrongContent) {
6390 const double lhs[3] = {1, 2, 3};
6391 const int rhs[3] = {2, 6, 4};
6392 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
6393 EXPECT_EQ(
"where the value pair (2, 6) at index #1 don't match, "
6394 "where the second/2 is 3",
6395 Explain(Pointwise(IsHalfOf(), rhs), lhs));
6398 TEST(PointwiseTest, AcceptsCorrectContent) {
6399 const double lhs[3] = {1, 2, 3};
6400 const int rhs[3] = {2, 4, 6};
6402 EXPECT_EQ(
"", Explain(Pointwise(IsHalfOf(), rhs), lhs));
6405 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
6406 const double lhs[3] = {1, 2, 3};
6407 const int rhs[3] = {2, 4, 6};
6408 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6410 EXPECT_EQ(
"", Explain(Pointwise(m1, rhs), lhs));
6414 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6416 EXPECT_EQ(
"", Explain(Pointwise(m2, rhs), lhs));
6419 MATCHER(PointeeEquals,
"Points to an equal value") {
6420 return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),
6421 ::testing::get<0>(arg), result_listener);
6424 TEST(PointwiseTest, WorksWithMoveOnly) {
6425 ContainerHelper helper;
6426 EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));
6427 helper.Call(MakeUniquePtrs({1, 2}));
6430 TEST(UnorderedPointwiseTest, DescribesSelf) {
6435 const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs);
6437 "has 3 elements and there exists some permutation of elements such "
6439 " - element #0 and 1 are a pair where the first is half of the second, "
6441 " - element #1 and 2 are a pair where the first is half of the second, "
6443 " - element #2 and 3 are a pair where the first is half of the second",
6446 "doesn't have 3 elements, or there exists no permutation of elements "
6448 " - element #0 and 1 are a pair where the first is half of the second, "
6450 " - element #1 and 2 are a pair where the first is half of the second, "
6452 " - element #2 and 3 are a pair where the first is half of the second",
6453 DescribeNegation(m));
6456 TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
6457 list<signed char> rhs;
6462 const Matcher<const int (&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs);
6470 TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
6471 const int lhs[] = {1, 2, 3};
6477 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
6480 TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
6481 const int rhs[] = {1, 2, 3};
6487 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));
6491 TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
6492 const vector<int> lhs{2, 4, 6};
6493 EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3}));
6494 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));
6498 TEST(UnorderedPointwiseTest, RejectsWrongSize) {
6499 const double lhs[2] = {1, 2};
6500 const int rhs[1] = {0};
6501 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
6503 Explain(UnorderedPointwise(Gt(), rhs), lhs));
6505 const int rhs2[3] = {0, 1, 2};
6506 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2)));
6509 TEST(UnorderedPointwiseTest, RejectsWrongContent) {
6510 const double lhs[3] = {1, 2, 3};
6511 const int rhs[3] = {2, 6, 6};
6512 EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs)));
6513 EXPECT_EQ(
"where the following elements don't match any matchers:\n"
6515 Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs));
6518 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
6519 const double lhs[3] = {1, 2, 3};
6520 const int rhs[3] = {2, 4, 6};
6521 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
6524 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
6525 const double lhs[3] = {1, 2, 3};
6526 const int rhs[3] = {6, 4, 2};
6527 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
6530 TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
6531 const double lhs[3] = {1, 2, 3};
6532 const int rhs[3] = {4, 6, 2};
6533 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6538 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6542 TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
6543 ContainerHelper helper;
6544 EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),
6545 std::vector<int>{1, 2})));
6546 helper.Call(MakeUniquePtrs({2, 1}));
6551 template <
typename T>
6552 class SampleOptional {
6554 using value_type =
T;
6555 explicit SampleOptional(
T value)
6566 TEST(OptionalTest, DescribesSelf) {
6567 const Matcher<SampleOptional<int>> m = Optional(Eq(1));
6568 EXPECT_EQ(
"value is equal to 1", Describe(m));
6571 TEST(OptionalTest, ExplainsSelf) {
6572 const Matcher<SampleOptional<int>> m = Optional(Eq(1));
6573 EXPECT_EQ(
"whose value 1 matches", Explain(m, SampleOptional<int>(1)));
6574 EXPECT_EQ(
"whose value 2 doesn't match", Explain(m, SampleOptional<int>(2)));
6577 TEST(OptionalTest, MatchesNonEmptyOptional) {
6578 const Matcher<SampleOptional<int>> m1 = Optional(1);
6579 const Matcher<SampleOptional<int>> m2 = Optional(Eq(2));
6580 const Matcher<SampleOptional<int>> m3 = Optional(Lt(3));
6581 SampleOptional<int> opt(1);
6587 TEST(OptionalTest, DoesNotMatchNullopt) {
6588 const Matcher<SampleOptional<int>> m = Optional(1);
6589 SampleOptional<int> empty;
6593 TEST(OptionalTest, WorksWithMoveOnly) {
6594 Matcher<SampleOptional<std::unique_ptr<int>>> m = Optional(Eq(
nullptr));
6595 EXPECT_TRUE(m.Matches(SampleOptional<std::unique_ptr<int>>(
nullptr)));
6598 class SampleVariantIntString {
6601 SampleVariantIntString(
const std::string& s) :
s_(s),
has_int_(
false) {}
6603 template <
typename T>
6604 friend bool holds_alternative(
const SampleVariantIntString& value) {
6608 template <
typename T>
6609 friend const T&
get(
const SampleVariantIntString&
value) {
6610 return value.get_impl(static_cast<T*>(
nullptr));
6614 const int& get_impl(
int*)
const {
return i_; }
6615 const std::string& get_impl(std::string*)
const {
return s_; }
6622 TEST(VariantTest, DescribesSelf) {
6623 const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6624 EXPECT_THAT(Describe(m), ContainsRegex(
"is a variant<> with value of type "
6625 "'.*' and the value is equal to 1"));
6628 TEST(VariantTest, ExplainsSelf) {
6629 const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6630 EXPECT_THAT(Explain(m, SampleVariantIntString(1)),
6631 ContainsRegex(
"whose value 1"));
6632 EXPECT_THAT(Explain(m, SampleVariantIntString(
"A")),
6633 HasSubstr(
"whose value is not of type '"));
6634 EXPECT_THAT(Explain(m, SampleVariantIntString(2)),
6635 "whose value 2 doesn't match");
6638 TEST(VariantTest, FullMatch) {
6639 Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6640 EXPECT_TRUE(m.Matches(SampleVariantIntString(1)));
6642 m = VariantWith<std::string>(Eq(
"1"));
6643 EXPECT_TRUE(m.Matches(SampleVariantIntString(
"1")));
6646 TEST(VariantTest, TypeDoesNotMatch) {
6647 Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6650 m = VariantWith<std::string>(Eq(
"1"));
6654 TEST(VariantTest, InnerDoesNotMatch) {
6655 Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6658 m = VariantWith<std::string>(Eq(
"1"));
6662 class SampleAnyType {
6664 explicit SampleAnyType(
int i) :
index_(0),
i_(i) {}
6665 explicit SampleAnyType(
const std::string& s) :
index_(1),
s_(s) {}
6667 template <
typename T>
6668 friend const T* any_cast(
const SampleAnyType* any) {
6669 return any->get_impl(static_cast<T*>(
nullptr));
6677 const int* get_impl(
int*)
const {
return index_ == 0 ? &
i_ :
nullptr; }
6678 const std::string* get_impl(std::string*)
const {
6679 return index_ == 1 ? &
s_ :
nullptr;
6683 TEST(AnyWithTest, FullMatch) {
6684 Matcher<SampleAnyType> m = AnyWith<int>(Eq(1));
6688 TEST(AnyWithTest, TestBadCastType) {
6689 Matcher<SampleAnyType> m = AnyWith<std::string>(Eq(
"fail"));
6693 TEST(AnyWithTest, TestUseInContainers) {
6694 std::vector<SampleAnyType>
a;
6699 a, ElementsAreArray({AnyWith<int>(1), AnyWith<int>(2), AnyWith<int>(3)}));
6701 std::vector<SampleAnyType> b;
6702 b.emplace_back(
"hello");
6703 b.emplace_back(
"merhaba");
6704 b.emplace_back(
"salut");
6705 EXPECT_THAT(b, ElementsAreArray({AnyWith<std::string>(
"hello"),
6706 AnyWith<std::string>(
"merhaba"),
6707 AnyWith<std::string>(
"salut")}));
6709 TEST(AnyWithTest, TestCompare) {
6710 EXPECT_THAT(SampleAnyType(1), AnyWith<int>(Gt(0)));
6713 TEST(AnyWithTest, DescribesSelf) {
6714 const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1));
6715 EXPECT_THAT(Describe(m), ContainsRegex(
"is an 'any' type with value of type "
6716 "'.*' and the value is equal to 1"));
6719 TEST(AnyWithTest, ExplainsSelf) {
6720 const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1));
6722 EXPECT_THAT(Explain(m, SampleAnyType(1)), ContainsRegex(
"whose value 1"));
6724 HasSubstr(
"whose value is not of type '"));
6725 EXPECT_THAT(Explain(m, SampleAnyType(2)),
"whose value 2 doesn't match");
6728 TEST(PointeeTest, WorksOnMoveOnlyType) {
6729 std::unique_ptr<int>
p(
new int(3));
6734 TEST(NotTest, WorksOnMoveOnlyType) {
6735 std::unique_ptr<int>
p(
new int(3));
6742 TEST(ArgsTest, AcceptsZeroTemplateArg) {
6743 const std::tuple<int, bool> t(5,
true);
6748 TEST(ArgsTest, AcceptsOneTemplateArg) {
6749 const std::tuple<int, bool> t(5,
true);
6751 EXPECT_THAT(t, Args<1>(Eq(std::make_tuple(
true))));
6752 EXPECT_THAT(t, Not(Args<1>(Eq(std::make_tuple(
false)))));
6755 TEST(ArgsTest, AcceptsTwoTemplateArgs) {
6756 const std::tuple<short, int, long> t(4, 5, 6L);
6763 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
6764 const std::tuple<short, int, long> t(4, 5, 6L);
6769 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
6770 const std::tuple<short, int, long> t(4, 5, 6L);
6776 return std::get<0>(arg) + std::get<1>(arg) + std::get<2>(arg) == 0;
6779 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
6780 EXPECT_THAT(std::make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
6781 EXPECT_THAT(std::make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
6784 TEST(ArgsTest, CanBeNested) {
6785 const std::tuple<short, int, long, int> t(4, 5, 6L, 6);
6786 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
6787 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
6790 TEST(ArgsTest, CanMatchTupleByValue) {
6791 typedef std::tuple<char, int, int> Tuple3;
6792 const Matcher<Tuple3> m = Args<1, 2>(Lt());
6797 TEST(ArgsTest, CanMatchTupleByReference) {
6798 typedef std::tuple<char, char, int> Tuple3;
6799 const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
6809 TEST(ArgsTest, AcceptsTenTemplateArgs) {
6810 EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
6811 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
6812 PrintsAs(
"(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
6813 EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
6814 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
6815 PrintsAs(
"(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
6818 TEST(ArgsTest, DescirbesSelfCorrectly) {
6819 const Matcher<std::tuple<int, bool, char> > m = Args<2, 0>(Lt());
6820 EXPECT_EQ(
"are a tuple whose fields (#2, #0) are a pair where "
6821 "the first < the second",
6825 TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
6826 const Matcher<const std::tuple<int, bool, char, int>&> m =
6827 Args<0, 2, 3>(Args<2, 0>(Lt()));
6828 EXPECT_EQ(
"are a tuple whose fields (#0, #2, #3) are a tuple "
6829 "whose fields (#2, #0) are a pair where the first < the second",
6833 TEST(ArgsTest, DescribesNegationCorrectly) {
6834 const Matcher<std::tuple<int, char> > m = Args<1, 0>(Gt());
6835 EXPECT_EQ(
"are a tuple whose fields (#1, #0) aren't a pair "
6836 "where the first > the second",
6837 DescribeNegation(m));
6840 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
6841 const Matcher<std::tuple<bool, int, int> > m = Args<1, 2>(Eq());
6842 EXPECT_EQ(
"whose fields (#1, #2) are (42, 42)",
6843 Explain(m, std::make_tuple(
false, 42, 42)));
6844 EXPECT_EQ(
"whose fields (#1, #2) are (42, 43)",
6845 Explain(m, std::make_tuple(
false, 42, 43)));
6849 class LessThanMatcher :
public MatcherInterface<std::tuple<char, int> > {
6851 void DescribeTo(::std::ostream* )
const override {}
6853 bool MatchAndExplain(std::tuple<char, int> value,
6854 MatchResultListener* listener)
const override {
6855 const int diff = std::get<0>(
value) - std::get<1>(value);
6857 *listener <<
"where the first value is " << diff
6858 <<
" more than the second";
6864 Matcher<std::tuple<char, int> > LessThan() {
6865 return MakeMatcher(
new LessThanMatcher);
6868 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
6869 const Matcher<std::tuple<char, int, int> > m = Args<0, 2>(LessThan());
6871 "whose fields (#0, #2) are ('a' (97, 0x61), 42), "
6872 "where the first value is 55 more than the second",
6873 Explain(m, std::make_tuple(
'a', 42, 42)));
6874 EXPECT_EQ(
"whose fields (#0, #2) are ('\\0', 43)",
6875 Explain(m, std::make_tuple(
'\0', 42, 43)));
6880 enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
6885 class MockMatcher :
public MatcherInterface<Behavior> {
6887 bool MatchAndExplain(Behavior behavior,
6888 MatchResultListener* listener)
const override {
6889 *listener <<
"[MatchAndExplain]";
6891 case kInitialSuccess:
6895 return !listener->IsInterested();
6905 return listener->IsInterested();
6908 GTEST_LOG_(FATAL) <<
"This should never be reached";
6912 void DescribeTo(ostream* os)
const override { *os <<
"[DescribeTo]"; }
6914 void DescribeNegationTo(ostream* os)
const override {
6915 *os <<
"[DescribeNegationTo]";
6919 AssertionResult RunPredicateFormatter(Behavior behavior) {
6920 auto matcher = MakeMatcher(
new MockMatcher);
6921 PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
6923 return predicate_formatter(
"dummy-name", behavior);
6927 TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
6928 AssertionResult result = RunPredicateFormatter(kInitialSuccess);
6934 TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
6935 AssertionResult result = RunPredicateFormatter(kAlwaysFail);
6937 std::string expect =
6938 "Value of: dummy-name\nExpected: [DescribeTo]\n"
6940 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
6944 TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
6945 AssertionResult result = RunPredicateFormatter(kFlaky);
6947 std::string expect =
6948 "Value of: dummy-name\nExpected: [DescribeTo]\n"
6949 " The matcher failed on the initial attempt; but passed when rerun to "
6950 "generate the explanation.\n"
6952 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
6958 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
6959 Matcher<const vector<int>&> m = ElementsAre();
6963 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
6964 Matcher<vector<int>> m = ElementsAre(Gt(5));
6965 EXPECT_EQ(
"has 1 element that is > 5", Describe(m));
6968 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
6969 Matcher<list<std::string>> m = ElementsAre(StrEq(
"one"),
"two");
6971 "has 2 elements where\n"
6972 "element #0 is equal to \"one\",\n"
6973 "element #1 is equal to \"two\"",
6977 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
6978 Matcher<vector<int>> m = ElementsAre();
6979 EXPECT_EQ(
"isn't empty", DescribeNegation(m));
6982 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
6983 Matcher<const list<int>&> m = ElementsAre(Gt(5));
6985 "doesn't have 1 element, or\n"
6986 "element #0 isn't > 5",
6987 DescribeNegation(m));
6990 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
6991 Matcher<const list<std::string>&> m = ElementsAre(
"one",
"two");
6993 "doesn't have 2 elements, or\n"
6994 "element #0 isn't equal to \"one\", or\n"
6995 "element #1 isn't equal to \"two\"",
6996 DescribeNegation(m));
6999 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
7000 Matcher<const list<int>&> m = ElementsAre(1, Ne(2));
7002 list<int> test_list;
7003 test_list.push_back(1);
7004 test_list.push_back(3);
7008 TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
7009 Matcher<const vector<int>&> m =
7010 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
7012 const int a[] = {10, 0, 100};
7013 vector<int> test_vector(std::begin(a), std::end(a));
7015 "whose element #0 matches, which is 9 more than 1,\n"
7016 "and whose element #2 matches, which is 98 more than 2",
7017 Explain(m, test_vector));
7020 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
7021 Matcher<const list<int>&> m = ElementsAre(1, 3);
7023 list<int> test_list;
7027 test_list.push_back(1);
7028 EXPECT_EQ(
"which has 1 element", Explain(m, test_list));
7031 TEST(ElementsAreTest, CanExplainMismatchRightSize) {
7032 Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));
7037 EXPECT_EQ(
"whose element #0 doesn't match", Explain(m, v));
7040 EXPECT_EQ(
"whose element #1 doesn't match, which is 4 less than 5",
7044 TEST(ElementsAreTest, MatchesOneElementVector) {
7045 vector<std::string> test_vector;
7046 test_vector.push_back(
"test string");
7048 EXPECT_THAT(test_vector, ElementsAre(StrEq(
"test string")));
7051 TEST(ElementsAreTest, MatchesOneElementList) {
7052 list<std::string> test_list;
7053 test_list.push_back(
"test string");
7055 EXPECT_THAT(test_list, ElementsAre(
"test string"));
7058 TEST(ElementsAreTest, MatchesThreeElementVector) {
7059 vector<std::string> test_vector;
7060 test_vector.push_back(
"one");
7061 test_vector.push_back(
"two");
7062 test_vector.push_back(
"three");
7064 EXPECT_THAT(test_vector, ElementsAre(
"one", StrEq(
"two"),
_));
7067 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
7068 vector<int> test_vector;
7069 test_vector.push_back(4);
7074 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
7075 vector<int> test_vector;
7076 test_vector.push_back(4);
7081 TEST(ElementsAreTest, MatchesOneElementValue) {
7082 vector<int> test_vector;
7083 test_vector.push_back(4);
7088 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
7089 vector<int> test_vector;
7090 test_vector.push_back(1);
7091 test_vector.push_back(2);
7092 test_vector.push_back(3);
7097 TEST(ElementsAreTest, MatchesTenElementVector) {
7098 const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
7099 vector<int> test_vector(std::begin(a), std::end(a));
7104 ElementsAre(0, Ge(0),
_, 3, 4, Ne(2), Eq(6), 7, 8,
_));
7107 TEST(ElementsAreTest, DoesNotMatchWrongSize) {
7108 vector<std::string> test_vector;
7109 test_vector.push_back(
"test string");
7110 test_vector.push_back(
"test string");
7112 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
7116 TEST(ElementsAreTest, DoesNotMatchWrongValue) {
7117 vector<std::string> test_vector;
7118 test_vector.push_back(
"other string");
7120 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
7124 TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
7125 vector<std::string> test_vector;
7126 test_vector.push_back(
"one");
7127 test_vector.push_back(
"three");
7128 test_vector.push_back(
"two");
7130 Matcher<vector<std::string>> m =
7131 ElementsAre(StrEq(
"one"), StrEq(
"two"), StrEq(
"three"));
7135 TEST(ElementsAreTest, WorksForNestedContainer) {
7136 constexpr std::array<const char*, 2> strings = {{
"Hi",
"world"}};
7138 vector<list<char>> nested;
7139 for (
const auto& s : strings) {
7140 nested.emplace_back(s, s + strlen(s));
7143 EXPECT_THAT(nested, ElementsAre(ElementsAre(
'H', Ne(
'e')),
7144 ElementsAre(
'w',
'o',
_,
_,
'd')));
7145 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre(
'H',
'e'),
7146 ElementsAre(
'w',
'o',
_,
_,
'd'))));
7149 TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
7150 int a[] = {0, 1, 2};
7151 vector<int> v(std::begin(a), std::end(a));
7153 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
7154 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
7157 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
7158 int a[] = {0, 1, 2};
7159 vector<int> v(std::begin(a), std::end(a));
7165 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
7166 int array[] = {0, 1, 2};
7172 class NativeArrayPassedAsPointerAndSize {
7174 NativeArrayPassedAsPointerAndSize() {}
7176 MOCK_METHOD(
void, Helper, (
int* array,
int size));
7182 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
7183 int array[] = {0, 1};
7184 ::std::tuple<int*, size_t> array_as_tuple(array, 2);
7188 NativeArrayPassedAsPointerAndSize helper;
7190 helper.Helper(array, 2);
7193 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
7194 const char a2[][3] = {
"hi",
"lo"};
7195 EXPECT_THAT(a2, ElementsAre(ElementsAre(
'h',
'i',
'\0'),
7196 ElementsAre(
'l',
'o',
'\0')));
7197 EXPECT_THAT(a2, ElementsAre(StrEq(
"hi"), StrEq(
"lo")));
7198 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre(
'h',
'o',
'\0')),
7199 ElementsAre(
'l',
'o',
'\0')));
7202 TEST(ElementsAreTest, AcceptsStringLiteral) {
7203 std::string array[] = {
"hi",
"one",
"two"};
7204 EXPECT_THAT(array, ElementsAre(
"hi",
"one",
"two"));
7205 EXPECT_THAT(array, Not(ElementsAre(
"hi",
"one",
"too")));
7209 extern const char kHi[];
7211 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
7215 std::string array1[] = {
"hi"};
7218 std::string array2[] = {
"ho"};
7222 const char kHi[] =
"hi";
7224 TEST(ElementsAreTest, MakesCopyOfArguments) {
7228 ::testing::internal::ElementsAreMatcher<std::tuple<int, int>>
7229 polymorphic_matcher = ElementsAre(x, y);
7232 const int array1[] = {1, 2};
7234 const int array2[] = {0, 0};
7242 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
7243 const int a[] = {1, 2, 3};
7245 vector<int> test_vector(std::begin(a), std::end(a));
7249 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
7252 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
7253 std::array<const char*, 3> a = {{
"one",
"two",
"three"}};
7255 vector<std::string> test_vector(std::begin(a), std::end(a));
7256 EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size()));
7258 const char** p = a.data();
7259 test_vector[0] =
"1";
7260 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, a.size())));
7263 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
7264 const char* a[] = {
"one",
"two",
"three"};
7266 vector<std::string> test_vector(std::begin(a), std::end(a));
7269 test_vector[0] =
"1";
7270 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
7273 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
7274 const Matcher<std::string> kMatcherArray[] = {StrEq(
"one"), StrEq(
"two"),
7277 vector<std::string> test_vector;
7278 test_vector.push_back(
"one");
7279 test_vector.push_back(
"two");
7280 test_vector.push_back(
"three");
7281 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
7283 test_vector.push_back(
"three");
7284 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
7287 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
7288 const int a[] = {1, 2, 3};
7289 vector<int> test_vector(std::begin(a), std::end(a));
7290 const vector<int>
expected(std::begin(a), std::end(a));
7291 EXPECT_THAT(test_vector, ElementsAreArray(expected));
7292 test_vector.push_back(4);
7293 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
7296 TEST(ElementsAreArrayTest, TakesInitializerList) {
7297 const int a[5] = {1, 2, 3, 4, 5};
7298 EXPECT_THAT(a, ElementsAreArray({1, 2, 3, 4, 5}));
7299 EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 5, 4})));
7300 EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 4, 6})));
7303 TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
7304 const std::string a[5] = {
"a",
"b",
"c",
"d",
"e"};
7305 EXPECT_THAT(a, ElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
7306 EXPECT_THAT(a, Not(ElementsAreArray({
"a",
"b",
"c",
"e",
"d"})));
7307 EXPECT_THAT(a, Not(ElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
7310 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
7311 const int a[5] = {1, 2, 3, 4, 5};
7312 EXPECT_THAT(a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
7313 EXPECT_THAT(a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
7316 TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) {
7317 const int a[5] = {1, 2, 3, 4, 5};
7322 a, ElementsAreArray<Matcher<int>>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
7323 EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int>>(
7324 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
7327 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
7328 const int a[] = {1, 2, 3};
7329 const Matcher<int> kMatchers[] = {Eq(1), Eq(2), Eq(3)};
7330 vector<int> test_vector(std::begin(a), std::end(a));
7331 const vector<Matcher<int>>
expected(std::begin(kMatchers),
7332 std::end(kMatchers));
7333 EXPECT_THAT(test_vector, ElementsAreArray(expected));
7334 test_vector.push_back(4);
7335 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
7338 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
7339 const int a[] = {1, 2, 3};
7340 const vector<int> test_vector(std::begin(a), std::end(a));
7341 const vector<int>
expected(std::begin(a), std::end(a));
7342 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
7344 EXPECT_THAT(test_vector, ElementsAreArray(std::begin(a), std::end(a)));
7346 int*
const null_int =
nullptr;
7347 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
7348 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
7353 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
7354 ::std::string a[] = {
"hi",
"ho"};
7355 ::std::string b[] = {
"hi",
"ho"};
7362 TEST(ElementsAreArrayTest, SourceLifeSpan) {
7363 const int a[] = {1, 2, 3};
7364 vector<int> test_vector(std::begin(a), std::end(a));
7365 vector<int> expect(std::begin(a), std::end(a));
7366 ElementsAreArrayMatcher<int> matcher_maker =
7367 ElementsAreArray(expect.begin(), expect.end());
7371 for (
int&
i : expect) {
7375 test_vector.push_back(3);
7383 MATCHER(IsEven,
"") {
return (arg % 2) == 0; }
7385 TEST(MatcherMacroTest, Works) {
7386 const Matcher<int> m = IsEven();
7391 EXPECT_EQ(
"not (is even)", DescribeNegation(m));
7397 MATCHER(IsEven2, negation ?
"is odd" :
"is even") {
7398 if ((arg % 2) == 0) {
7401 *result_listener <<
"OK";
7404 *result_listener <<
"% 2 == " << (arg % 2);
7412 std::string(negation ?
"doesn't equal" :
"equals") +
" the sum of " +
7414 if (arg == (x + y)) {
7415 *result_listener <<
"OK";
7420 if (result_listener->stream() !=
nullptr) {
7421 *result_listener->stream() <<
"diff == " << (x + y - arg);
7429 TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
7430 const Matcher<int> m1 = IsEven2();
7432 EXPECT_EQ(
"is odd", DescribeNegation(m1));
7434 const Matcher<int> m2 = EqSumOf(5, 9);
7435 EXPECT_EQ(
"equals the sum of 5 and 9", Describe(m2));
7436 EXPECT_EQ(
"doesn't equal the sum of 5 and 9", DescribeNegation(m2));
7440 TEST(MatcherMacroTest, CanExplainMatchResult) {
7441 const Matcher<int> m1 = IsEven2();
7445 const Matcher<int> m2 = EqSumOf(1, 2);
7447 EXPECT_EQ(
"diff == -1", Explain(m2, 4));
7454 StaticAssertTypeEq<::std::string, arg_type>();
7458 MATCHER(IsEmptyStringByRef,
"") {
7459 StaticAssertTypeEq<const ::std::string&, arg_type>();
7463 TEST(MatcherMacroTest, CanReferenceArgType) {
7464 const Matcher<::std::string> m1 = IsEmptyString();
7467 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
7473 namespace matcher_test {
7474 MATCHER(IsOdd,
"") {
return (arg % 2) != 0; }
7477 TEST(MatcherMacroTest, WorksInNamespace) {
7485 return Value(arg, matcher_test::IsOdd()) && arg > 0;
7488 TEST(MatcherMacroTest, CanBeComposedUsingValue) {
7496 MATCHER_P(IsGreaterThan32And, n,
"") {
return arg > 32 && arg > n; }
7498 TEST(MatcherPMacroTest, Works) {
7499 const Matcher<int> m = IsGreaterThan32And(5);
7503 EXPECT_EQ(
"is greater than 32 and 5", Describe(m));
7504 EXPECT_EQ(
"not (is greater than 32 and 5)", DescribeNegation(m));
7510 MATCHER_P(_is_Greater_Than32and_, n,
"") {
return arg > 32 && arg > n; }
7512 TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
7513 const Matcher<int> m = _is_Greater_Than32and_(5);
7515 EXPECT_EQ(
"is greater than 32 and 5", Describe(m));
7516 EXPECT_EQ(
"not (is greater than 32 and 5)", DescribeNegation(m));
7524 class UncopyableFoo {
7528 UncopyableFoo(
const UncopyableFoo&) =
delete;
7529 void operator=(
const UncopyableFoo&) =
delete;
7535 MATCHER_P(ReferencesUncopyable, variable,
"") {
return &arg == &variable; }
7537 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
7538 UncopyableFoo foo1(
'1'), foo2(
'2');
7539 const Matcher<const UncopyableFoo&> m =
7540 ReferencesUncopyable<const UncopyableFoo&>(foo1);
7549 EXPECT_EQ(
"references uncopyable 1-byte object <31>", Describe(m));
7555 MATCHER_P3(ParamTypesAreIntLongAndChar,
foo, bar, baz,
"") {
7556 StaticAssertTypeEq<int, foo_type>();
7557 StaticAssertTypeEq<long, bar_type>();
7558 StaticAssertTypeEq<char, baz_type>();
7562 TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
7563 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L,
'a'));
7569 MATCHER_P2(ReferencesAnyOf, variable1, variable2,
"") {
7570 return &arg == &variable1 || &arg == &variable2;
7573 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
7574 UncopyableFoo foo1(
'1'), foo2(
'2'), foo3(
'3');
7575 const Matcher<const UncopyableFoo&> const_m =
7576 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
7582 const Matcher<UncopyableFoo&> m =
7583 ReferencesAnyOf<UncopyableFoo&, UncopyableFoo&>(foo1, foo2);
7590 TEST(MatcherPnMacroTest,
7591 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
7592 UncopyableFoo foo1(
'1'), foo2(
'2');
7593 const Matcher<const UncopyableFoo&> m =
7594 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
7600 EXPECT_EQ(
"references any of (1-byte object <31>, 1-byte object <32>)",
7606 MATCHER_P2(IsNotInClosedRange, low, hi,
"") {
return arg < low || arg > hi; }
7608 TEST(MatcherPnMacroTest, Works) {
7609 const Matcher<const long&> m = IsNotInClosedRange(10, 20);
7613 EXPECT_EQ(
"is not in closed range (10, 20)", Describe(m));
7614 EXPECT_EQ(
"not (is not in closed range (10, 20))", DescribeNegation(m));
7622 MATCHER(EqualsSumOf,
"") {
return arg == 0; }
7623 MATCHER_P(EqualsSumOf, a,
"") {
return arg ==
a; }
7624 MATCHER_P2(EqualsSumOf, a, b,
"") {
return arg == a + b; }
7625 MATCHER_P3(EqualsSumOf, a, b, c,
"") {
return arg == a + b +
c; }
7626 MATCHER_P4(EqualsSumOf, a, b, c, d,
"") {
return arg == a + b + c + d; }
7627 MATCHER_P5(EqualsSumOf, a, b, c, d, e,
"") {
return arg == a + b + c + d + e; }
7628 MATCHER_P6(EqualsSumOf, a, b, c, d, e, f,
"") {
7629 return arg == a + b + c + d + e +
f;
7631 MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g,
"") {
7632 return arg == a + b + c + d + e +
f +
g;
7634 MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h,
"") {
7635 return arg == a + b + c + d + e +
f +
g + h;
7637 MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h,
i,
"") {
7638 return arg == a + b + c + d + e +
f +
g + h +
i;
7640 MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h,
i, j,
"") {
7641 return arg == a + b + c + d + e +
f +
g + h +
i + j;
7644 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
7650 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
7652 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f'));
7654 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g'));
7655 EXPECT_THAT(
"abcdefgh", EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
7657 EXPECT_THAT(
"abcdefghi", EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
7658 'f',
'g',
"h",
'i'));
7660 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
"h",
7661 'i', ::std::string(
"j")));
7667 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
7668 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
7670 Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f')));
7671 EXPECT_THAT(
"abcdefg ", Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
7673 EXPECT_THAT(
"abcdefgh ", Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
7674 "e",
'f',
'g',
"h")));
7675 EXPECT_THAT(
"abcdefghi ", Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
7676 "e",
'f',
'g',
"h",
'i')));
7678 Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
7679 "h",
'i', ::std::string(
"j"))));
7684 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
7685 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
7686 EXPECT_THAT(
"abcd", EqualsSumOf(::std::string(
"a"),
"b",
'c',
"d"));
7688 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
7689 EXPECT_THAT(
"abcde", Not(EqualsSumOf(::std::string(
"a"),
"b",
'c',
"d")));
7696 std::string prefix_str(prefix);
7697 char suffix_char =
static_cast<char>(suffix);
7698 return arg == prefix_str + suffix_char;
7701 TEST(MatcherPnMacroTest, SimpleTypePromotion) {
7702 Matcher<std::string> no_promo = EqConcat(std::string(
"foo"),
't');
7703 Matcher<const std::string&> promo = EqConcat(
"foo", static_cast<int>(
't'));
7712 TEST(MatcherPnMacroTest, TypesAreCorrect) {
7714 EqualsSumOfMatcher a0 = EqualsSumOf();
7717 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
7721 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1,
'2');
7722 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2,
'3');
7723 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3,
'4');
7724 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
7725 EqualsSumOf(1, 2, 3, 4,
'5');
7726 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
7727 EqualsSumOf(1, 2, 3, 4, 5,
'6');
7728 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
7729 EqualsSumOf(1, 2, 3, 4, 5, 6,
'7');
7730 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
7731 EqualsSumOf(1, 2, 3, 4, 5, 6, 7,
'8');
7732 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
7733 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8,
'9');
7734 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
7735 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9,
'0');
7756 const int count =
static_cast<int>(Value(arg, m1)) +
7757 static_cast<int>(Value(arg, m2)) +
7758 static_cast<int>(Value(arg, m3));
7762 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
7769 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
7770 list<int> some_list;
7771 some_list.push_back(3);
7772 some_list.push_back(1);
7773 some_list.push_back(2);
7778 list<std::string> another_list;
7779 another_list.push_back(
"fee");
7780 another_list.push_back(
"fie");
7781 another_list.push_back(
"foe");
7782 another_list.push_back(
"fum");
7783 EXPECT_THAT(another_list, Contains(std::string(
"fee")));
7786 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
7787 list<int> some_list;
7788 some_list.push_back(3);
7789 some_list.push_back(1);
7793 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
7802 set<std::string> another_set;
7803 another_set.insert(
"fee");
7804 another_set.insert(
"fie");
7805 another_set.insert(
"foe");
7806 another_set.insert(
"fum");
7807 EXPECT_THAT(another_set, Contains(Eq(std::string(
"fum"))));
7810 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
7816 set<std::string> c_string_set;
7817 c_string_set.insert(
"hello");
7818 EXPECT_THAT(c_string_set, Not(Contains(std::string(
"goodbye"))));
7821 TEST(ContainsTest, ExplainsMatchResultCorrectly) {
7822 const int a[2] = {1, 2};
7823 Matcher<const int(&)[2]> m = Contains(2);
7824 EXPECT_EQ(
"whose element #1 matches", Explain(m, a));
7829 m = Contains(GreaterThan(0));
7830 EXPECT_EQ(
"whose element #0 matches, which is 1 more than 0", Explain(m, a));
7832 m = Contains(GreaterThan(10));
7836 TEST(ContainsTest, DescribesItselfCorrectly) {
7837 Matcher<vector<int>> m = Contains(1);
7838 EXPECT_EQ(
"contains at least one element that is equal to 1", Describe(m));
7840 Matcher<vector<int>> m2 = Not(m);
7841 EXPECT_EQ(
"doesn't contain any element that is equal to 1", Describe(m2));
7844 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
7845 map<std::string, int> my_map;
7846 const char* bar =
"a string";
7848 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
7850 map<std::string, int> another_map;
7851 another_map[
"fee"] = 1;
7852 another_map[
"fie"] = 2;
7853 another_map[
"foe"] = 3;
7854 another_map[
"fum"] = 4;
7856 Contains(pair<const std::string, int>(std::string(
"fee"), 1)));
7857 EXPECT_THAT(another_map, Contains(pair<const std::string, int>(
"fie", 2)));
7860 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
7861 map<int, int> some_map;
7864 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
7867 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
7868 const char* string_array[] = {
"fee",
"fie",
"foe",
"fum"};
7869 EXPECT_THAT(string_array, Contains(Eq(std::string(
"fum"))));
7872 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
7873 int int_array[] = {1, 2, 3, 4};
7877 TEST(ContainsTest, AcceptsMatcher) {
7878 const int a[] = {1, 2, 3};
7883 TEST(ContainsTest, WorksForNativeArrayAsTuple) {
7884 const int a[] = {1, 2};
7885 const int*
const pointer =
a;
7886 EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1));
7887 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3))));
7890 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
7891 int a[][3] = {{1, 2, 3}, {4, 5, 6}};
7894 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
7898 TEST(AllOfArrayTest, BasicForms) {
7900 std::vector<int> v0{};
7901 std::vector<int> v1{1};
7902 std::vector<int> v2{2, 3};
7903 std::vector<int> v3{4, 4, 4};
7906 EXPECT_THAT(2, Not(AllOfArray(v1.begin(), v1.end())));
7907 EXPECT_THAT(3, Not(AllOfArray(v2.begin(), v2.end())));
7910 int ar[6] = {1, 2, 3, 4, 4, 4};
7919 int ar2[2] = {2, 3};
7920 int ar3[3] = {4, 4, 4};
7940 TEST(AllOfArrayTest, Matchers) {
7942 std::vector<Matcher<int>> matchers{Ge(1), Lt(2)};
7951 TEST(AnyOfArrayTest, BasicForms) {
7953 std::vector<int> v0{};
7954 std::vector<int> v1{1};
7955 std::vector<int> v2{2, 3};
7956 EXPECT_THAT(0, Not(AnyOfArray(v0.begin(), v0.end())));
7958 EXPECT_THAT(2, Not(AnyOfArray(v1.begin(), v1.end())));
7960 EXPECT_THAT(4, Not(AnyOfArray(v2.begin(), v2.end())));
7962 int ar[3] = {1, 2, 3};
7971 int ar2[2] = {2, 3};
7991 TEST(AnyOfArrayTest, Matchers) {
7994 std::vector<Matcher<int>> matchers{Lt(1), Ge(2)};
8003 TEST(AnyOfArrayTest, ExplainsMatchResultCorrectly) {
8006 const std::vector<int> v0{};
8007 const std::vector<int> v1{1};
8008 const std::vector<int> v2{2, 3};
8009 const Matcher<int> m0 = AnyOfArray(v0);
8010 const Matcher<int> m1 = AnyOfArray(v1);
8011 const Matcher<int> m2 = AnyOfArray(v2);
8018 EXPECT_EQ(
"(is equal to 1)", Describe(m1));
8019 EXPECT_EQ(
"(is equal to 2) or (is equal to 3)", Describe(m2));
8021 EXPECT_EQ(
"(isn't equal to 1)", DescribeNegation(m1));
8022 EXPECT_EQ(
"(isn't equal to 2) and (isn't equal to 3)", DescribeNegation(m2));
8024 const Matcher<int> g1 = AnyOfArray({GreaterThan(1)});
8025 const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)});
8027 EXPECT_EQ(
"which is 1 less than 1", Explain(g1, 0));
8028 EXPECT_EQ(
"which is the same as 1", Explain(g1, 1));
8029 EXPECT_EQ(
"which is 1 more than 1", Explain(g1, 2));
8030 EXPECT_EQ(
"which is 1 less than 1, and which is 2 less than 2",
8032 EXPECT_EQ(
"which is the same as 1, and which is 1 less than 2",
8038 TEST(AllOfTest, HugeMatcher) {
8041 EXPECT_THAT(0, testing::AllOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
8042 testing::AllOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
_)));
8045 TEST(AnyOfTest, HugeMatcher) {
8048 EXPECT_THAT(0, testing::AnyOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
8049 testing::AnyOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
_)));
8052 namespace adl_test {
8066 template <
typename T1,
typename T2>
8067 bool AllOf(
const T1& ,
const T2& ) {
8071 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
8073 testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
8076 template <
typename T1,
typename T2>
8077 bool AnyOf(
const T1&,
const T2&) {
8081 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
8083 testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
8088 TEST(AllOfTest, WorksOnMoveOnlyType) {
8089 std::unique_ptr<int>
p(
new int(3));
8090 EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
8091 EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
8094 TEST(AnyOfTest, WorksOnMoveOnlyType) {
8095 std::unique_ptr<int>
p(
new int(3));
8096 EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
8097 EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
8100 MATCHER(IsNotNull,
"") {
return arg !=
nullptr; }
8104 TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
8105 std::unique_ptr<int>
p(
new int(3));
8107 EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
8110 MATCHER_P(UniquePointee, pointee,
"") {
return *arg == pointee; }
8114 TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
8115 std::unique_ptr<int>
p(
new int(3));
8125 # pragma warning(pop)
GTEST_API_ bool IsTrue(bool condition)
TYPED_TEST(CodeLocationForTYPEDTEST, Verify)
internal::ValueArray< T...> Values(T...v)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
const RawType further_from_negative_zero_
#define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description)
::std::string PrintToString(const T &value)
TEST_F(TestInfoTest, Names)
const RawType further_from_one_
#define GTEST_LOG_(severity)
#define ASSERT_THAT(value, matcher)
#define MOCK_METHOD0(m,...)
::std::vector< ::std::string > Strings
#define ON_CALL(obj, call)
MATCHER_P2(IsPair, first, second,"")
const RawType close_to_negative_zero_
#define MATCHER_P5(name, p0, p1, p2, p3, p4, description)
internal::ParamGenerator< T > Range(T start, T end, IncrementT step)
const Bits infinity_bits_
#define MATCHER_P3(name, p0, p1, p2, description)
internal::ProxyTypeList< Ts...> Types
BigUInt< n > operator*(BigUInt< n > const &a, BigUInt< n > const &b)
std::ostream & operator<<(std::ostream &os, const Message &sb)
#define EXPECT_FATAL_FAILURE(statement, substr)
#define MOCK_METHOD2(m,...)
TEST_P(CodeLocationForTESTP, Verify)
inline::std::reference_wrapper< T > ByRef(T &l_value)
bool operator!=(const Allocator< T > &a_t, const Allocator< U > &a_u)
const RawType close_to_one_
CacheTaylor< T > diff(const CacheTaylor< T > &x, int n=1)
Compute Taylor series of n-th derivative of x.
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
#define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description)
#define ASSERT_TRUE(condition)
bool operator>(BigUInt< n > const &a, BigUInt< n > const &b)
std::list< value_type >::iterator pos_
bool operator>=(BigUInt< n > const &a, BigUInt< n > const &b)
std::vector< size_t > rhs_used_
const RawType further_from_infinity_
TEST(GTestEnvVarTest, Dummy)
ADVar foo(double d, ADVar x, ADVar y)
const RawType close_to_positive_zero_
#define MOCK_METHOD1(m,...)
#define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description)
INSTANTIATE_TEST_SUITE_P(, CodeLocationForTESTP, Values(0))
GTEST_API_ std::string FormatMatcherDescription(bool negation, const char *matcher_name, const Strings ¶m_values)
#define SCOPED_TRACE(message)
#define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description)
#define EXPECT_THAT(value, matcher)
#define MATCHER_P(name, p0, description)
ElementMatcherPairs best_so_far_
internal::ReturnAction< R > Return(R value)
#define EXPECT_CALL(obj, call)
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.
#define EXPECT_EQ(val1, val2)
GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix &g)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define MATCHER_P4(name, p0, p1, p2, p3, description)
const T func(int n, T *x)
const RawType close_to_infinity_
TYPED_TEST_SUITE(CodeLocationForTYPEDTEST, int)
#define EXPECT_TRUE(condition)
#define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description)
ElementMatcherPairs matches_
int operator<(const ADvari &L, const ADvari &R)
#define EXPECT_FALSE(condition)
AssertionResult IsNull(const char *str)
#define ASSERT_FALSE(condition)
static const size_t kUnused
std::vector< size_t > lhs_used_
MATCHER(IsEmpty, negation?"isn't empty":"is empty")
bool operator<=(BigUInt< n > const &a, BigUInt< n > const &b)
#define GTEST_FLAG_PREFIX_
std::list< value_type > remainder_
static ExpectedAnswer expected[4]