38 # pragma warning(push)
39 # pragma warning(disable:4244)
40 # pragma warning(disable:4100)
43 #include "gmock/gmock-matchers.h"
51 #include <forward_list>
62 #include <type_traits>
63 #include <unordered_map>
64 #include <unordered_set>
68 #include "gmock/gmock-more-matchers.h"
69 #include "gmock/gmock.h"
70 #include "gtest/gtest-spi.h"
71 #include "gtest/gtest.h"
74 namespace gmock_matchers_test {
87 using std::stringstream;
106 struct ContainerHelper {
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 template <
typename T =
int>
118 class GreaterThanMatcher :
public MatcherInterface<T> {
120 explicit GreaterThanMatcher(
T rhs) :
rhs_(rhs) {}
122 void DescribeTo(ostream* os)
const override { *os <<
"is > " <<
rhs_; }
124 bool MatchAndExplain(
T lhs, MatchResultListener* listener)
const override {
126 *listener <<
"which is " << (lhs -
rhs_) <<
" more than " <<
rhs_;
127 }
else if (lhs ==
rhs_) {
128 *listener <<
"which is the same as " <<
rhs_;
130 *listener <<
"which is " << (
rhs_ - lhs) <<
" less than " <<
rhs_;
140 template <
typename T>
141 Matcher<T> GreaterThan(
T n) {
154 template <
typename T>
156 return DescribeMatcher<T>(
m);
160 template <
typename T>
162 return DescribeMatcher<T>(
m,
true);
166 template <
typename MatcherType,
typename Value>
168 StringMatchResultListener listener;
170 return listener.str();
173 TEST(MonotonicMatcherTest, IsPrintable) {
175 ss << GreaterThan(5);
179 TEST(MatchResultListenerTest, StreamingWorks) {
180 StringMatchResultListener listener;
181 listener <<
"hi" << 5;
191 DummyMatchResultListener
dummy;
195 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
202 TEST(MatchResultListenerTest, IsInterestedWorks) {
203 EXPECT_TRUE(StringMatchResultListener().IsInterested());
204 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
206 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
207 EXPECT_FALSE(StreamMatchResultListener(
nullptr).IsInterested());
212 class EvenMatcherImpl :
public MatcherInterface<int> {
214 bool MatchAndExplain(
int x,
215 MatchResultListener* )
const override {
219 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
227 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
233 class NewEvenMatcherImpl :
public MatcherInterface<int> {
235 bool MatchAndExplain(
int x, MatchResultListener* listener)
const override {
236 const bool match =
x % 2 == 0;
238 *listener <<
"value % " << 2;
239 if (listener->stream() !=
nullptr) {
242 *listener->stream() <<
" == " << (
x % 2);
247 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
250 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
259 TEST(MatcherTest, CanBeDefaultConstructed) {
264 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
265 const MatcherInterface<int>* impl =
new EvenMatcherImpl;
266 Matcher<int>
m(impl);
272 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
279 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
280 Matcher<int*> m1 =
nullptr;
289 virtual ~Undefined() = 0;
293 TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
300 TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&>
m =
_; }
303 TEST(MatcherTest, IsCopyable) {
305 Matcher<bool> m1 =
Eq(
false);
317 TEST(MatcherTest, CanDescribeItself) {
319 Describe(Matcher<int>(
new EvenMatcherImpl)));
323 TEST(MatcherTest, MatchAndExplain) {
324 Matcher<int>
m = GreaterThan(0);
325 StringMatchResultListener listener1;
327 EXPECT_EQ(
"which is 42 more than 0", listener1.str());
329 StringMatchResultListener listener2;
331 EXPECT_EQ(
"which is 9 less than 0", listener2.str());
336 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
337 Matcher<std::string> m1 =
"hi";
341 Matcher<const std::string&> m2 =
"hi";
348 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
353 Matcher<const std::string&> m2 =
std::string(
"hi");
358 #if GTEST_INTERNAL_HAS_STRING_VIEW
361 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
362 Matcher<internal::StringView> m1 =
"cats";
366 Matcher<const internal::StringView&> m2 =
"cats";
373 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
374 Matcher<internal::StringView> m1 =
std::string(
"cats");
378 Matcher<const internal::StringView&> m2 =
std::string(
"cats");
385 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
386 Matcher<internal::StringView> m1 = internal::StringView(
"cats");
390 Matcher<const internal::StringView&> m2 = internal::StringView(
"cats");
394 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
398 TEST(StringMatcherTest,
399 CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
413 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
414 const MatcherInterface<int>* dummy_impl =
new EvenMatcherImpl;
421 class ReferencesBarOrIsZeroImpl {
423 template <
typename T>
424 bool MatchAndExplain(
const T& x,
425 MatchResultListener* )
const {
427 return p == &g_bar ||
x == 0;
430 void DescribeTo(ostream* os)
const { *os <<
"g_bar or zero"; }
432 void DescribeNegationTo(ostream* os)
const {
433 *os <<
"doesn't reference g_bar and is not zero";
439 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
443 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
445 Matcher<const int&> m1 = ReferencesBarOrIsZero();
450 EXPECT_EQ(
"g_bar or zero", Describe(m1));
453 Matcher<double> m2 = ReferencesBarOrIsZero();
456 EXPECT_EQ(
"g_bar or zero", Describe(m2));
461 class PolymorphicIsEvenImpl {
463 void DescribeTo(ostream* os)
const { *os <<
"is even"; }
465 void DescribeNegationTo(ostream* os)
const {
469 template <
typename T>
470 bool MatchAndExplain(
const T& x, MatchResultListener* listener)
const {
472 *listener <<
"% " << 2;
473 if (listener->stream() !=
nullptr) {
476 *listener->stream() <<
" == " << (
x % 2);
482 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
486 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
488 const Matcher<int> m1 = PolymorphicIsEven();
493 const Matcher<int> not_m1 =
Not(m1);
499 const Matcher<char> m2 = PolymorphicIsEven();
504 const Matcher<char> not_m2 =
Not(m2);
507 EXPECT_EQ(
"% 2 == 0", Explain(m2,
'\x42'));
511 TEST(MatcherCastTest, FromPolymorphicMatcher) {
512 Matcher<int>
m = MatcherCast<int>(
Eq(5));
522 explicit IntValue(
int a_value) :
value_(a_value) {}
530 bool IsPositiveIntValue(
const IntValue&
foo) {
531 return foo.value() > 0;
536 TEST(MatcherCastTest, FromCompatibleType) {
537 Matcher<double> m1 =
Eq(2.0);
538 Matcher<int> m2 = MatcherCast<int>(m1);
542 Matcher<IntValue> m3 =
Truly(IsPositiveIntValue);
543 Matcher<int> m4 = MatcherCast<int>(m3);
552 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
553 Matcher<const int&> m1 =
Eq(0);
554 Matcher<int> m2 = MatcherCast<int>(m1);
560 TEST(MatcherCastTest, FromReferenceToNonReference) {
561 Matcher<int&> m1 =
Eq(0);
562 Matcher<int> m2 = MatcherCast<int>(m1);
568 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
569 Matcher<int> m1 =
Eq(0);
570 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
576 TEST(MatcherCastTest, FromNonReferenceToReference) {
577 Matcher<int> m1 =
Eq(0);
578 Matcher<int&> m2 = MatcherCast<int&>(m1);
586 TEST(MatcherCastTest, FromSameType) {
587 Matcher<int> m1 =
Eq(0);
588 Matcher<int> m2 = MatcherCast<int>(m1);
595 TEST(MatcherCastTest, FromAValue) {
596 Matcher<int>
m = MatcherCast<int>(42);
603 TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
604 const int kExpected =
'c';
605 Matcher<int>
m = MatcherCast<int>(
'c');
610 struct NonImplicitlyConstructibleTypeWithOperatorEq {
612 const NonImplicitlyConstructibleTypeWithOperatorEq& ,
618 const NonImplicitlyConstructibleTypeWithOperatorEq& ) {
626 TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
627 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
628 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
629 EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
631 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
632 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
633 EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
638 MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
648 #if !defined _MSC_VER
657 namespace convertible_from_any {
659 struct ConvertibleFromAny {
660 ConvertibleFromAny(
int a_value) :
value(a_value) {}
661 template <
typename T>
662 ConvertibleFromAny(
const T& ) :
value(-1) {
668 bool operator==(
const ConvertibleFromAny& a,
const ConvertibleFromAny&
b) {
669 return a.value ==
b.value;
672 ostream&
operator<<(ostream& os,
const ConvertibleFromAny& a) {
673 return os <<
a.value;
676 TEST(MatcherCastTest, ConversionConstructorIsUsed) {
677 Matcher<ConvertibleFromAny>
m = MatcherCast<ConvertibleFromAny>(1);
682 TEST(MatcherCastTest, FromConvertibleFromAny) {
683 Matcher<ConvertibleFromAny>
m =
684 MatcherCast<ConvertibleFromAny>(
Eq(ConvertibleFromAny(1)));
690 #endif // !defined _MSC_VER
692 struct IntReferenceWrapper {
693 IntReferenceWrapper(
const int& a_value) :
value(&a_value) {}
697 bool operator==(
const IntReferenceWrapper& a,
const IntReferenceWrapper&
b) {
698 return a.value ==
b.value;
701 TEST(MatcherCastTest, ValueIsNotCopied) {
703 Matcher<IntReferenceWrapper>
m = MatcherCast<IntReferenceWrapper>(n);
716 class Derived :
public Base {
718 Derived() : Base() {}
722 class OtherDerived :
public Base {};
725 TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
726 Matcher<char> m2 = SafeMatcherCast<char>(
Eq(32));
734 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
736 Matcher<float> m2 = SafeMatcherCast<float>(m1);
740 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>(
'a'));
747 TEST(SafeMatcherCastTest, FromBaseClass) {
749 Matcher<Base*> m1 =
Eq(&d);
750 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
754 Matcher<Base&> m3 =
Ref(d);
755 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
761 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
763 Matcher<const int&> m1 =
Ref(n);
764 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
771 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
772 Matcher<std::unique_ptr<int>> m1 =
IsNull();
773 Matcher<const std::unique_ptr<int>&> m2 =
774 SafeMatcherCast<const std::unique_ptr<int>&>(m1);
776 EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(
new int)));
780 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
781 Matcher<int> m1 =
Eq(0);
782 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
790 TEST(SafeMatcherCastTest, FromSameType) {
791 Matcher<int> m1 =
Eq(0);
792 Matcher<int> m2 = SafeMatcherCast<int>(m1);
797 #if !defined _MSC_VER
799 namespace convertible_from_any {
800 TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
801 Matcher<ConvertibleFromAny>
m = SafeMatcherCast<ConvertibleFromAny>(1);
806 TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
807 Matcher<ConvertibleFromAny>
m =
808 SafeMatcherCast<ConvertibleFromAny>(
Eq(ConvertibleFromAny(1)));
814 #endif // !defined _MSC_VER
816 TEST(SafeMatcherCastTest, ValueIsNotCopied) {
818 Matcher<IntReferenceWrapper>
m = SafeMatcherCast<IntReferenceWrapper>(n);
823 TEST(ExpectThat, TakesLiterals) {
829 TEST(ExpectThat, TakesFunctions) {
831 static void Func() {}
839 TEST(ATest, MatchesAnyValue) {
853 TEST(ATest, WorksForDerivedClass) {
863 TEST(ATest, CanDescribeSelf) {
868 TEST(AnTest, MatchesAnyValue) {
870 Matcher<int> m1 = An<int>();
877 Matcher<int&> m2 = An<int&>();
883 TEST(AnTest, CanDescribeSelf) {
884 EXPECT_EQ(
"is anything", Describe(An<int>()));
889 TEST(UnderscoreTest, MatchesAnyValue) {
898 Matcher<const bool&> m2 =
_;
904 TEST(UnderscoreTest, CanDescribeSelf) {
910 TEST(EqTest, MatchesEqualValue) {
912 const char a1[] =
"hi";
913 const char a2[] =
"hi";
915 Matcher<const char*> m1 =
Eq(
a1);
924 Unprintable() :
c_(
'a') {}
926 bool operator==(
const Unprintable& )
const {
return true; }
928 char dummy_c() {
return c_; }
933 TEST(EqTest, CanDescribeSelf) {
934 Matcher<Unprintable>
m =
Eq(Unprintable());
935 EXPECT_EQ(
"is equal to 1-byte object <61>", Describe(
m));
940 TEST(EqTest, IsPolymorphic) {
941 Matcher<int> m1 =
Eq(1);
945 Matcher<char> m2 =
Eq(1);
951 TEST(TypedEqTest, ChecksEqualityForGivenType) {
952 Matcher<char> m1 = TypedEq<char>(
'a');
956 Matcher<int> m2 = TypedEq<int>(6);
962 TEST(TypedEqTest, CanDescribeSelf) {
963 EXPECT_EQ(
"is equal to 2", Describe(TypedEq<int>(2)));
971 template <
typename T>
973 static bool IsTypeOf(
const T& ) {
return true; }
975 template <
typename T2>
976 static void IsTypeOf(
T2 v);
979 TEST(TypedEqTest, HasSpecifiedType) {
986 TEST(GeTest, ImplementsGreaterThanOrEqual) {
987 Matcher<int> m1 =
Ge(0);
994 TEST(GeTest, CanDescribeSelf) {
995 Matcher<int>
m =
Ge(5);
1000 TEST(GtTest, ImplementsGreaterThan) {
1001 Matcher<double> m1 =
Gt(0);
1008 TEST(GtTest, CanDescribeSelf) {
1009 Matcher<int>
m =
Gt(5);
1014 TEST(LeTest, ImplementsLessThanOrEqual) {
1015 Matcher<char> m1 =
Le(
'b');
1022 TEST(LeTest, CanDescribeSelf) {
1023 Matcher<int>
m =
Le(5);
1028 TEST(LtTest, ImplementsLessThan) {
1029 Matcher<const std::string&> m1 =
Lt(
"Hello");
1036 TEST(LtTest, CanDescribeSelf) {
1037 Matcher<int>
m =
Lt(5);
1042 TEST(NeTest, ImplementsNotEqual) {
1043 Matcher<int> m1 =
Ne(0);
1050 TEST(NeTest, CanDescribeSelf) {
1051 Matcher<int>
m =
Ne(5);
1057 explicit MoveOnly(
int i) :
i_(
i) {}
1058 MoveOnly(
const MoveOnly&) =
delete;
1059 MoveOnly(MoveOnly&&) =
default;
1060 MoveOnly& operator=(
const MoveOnly&) =
delete;
1061 MoveOnly& operator=(MoveOnly&&) =
default;
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_; }
1068 bool operator>=(
const MoveOnly& other)
const {
return i_ >= other.i_; }
1079 #if defined(_MSC_VER) && (_MSC_VER < 1910)
1080 TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) {
1082 TEST(ComparisonBaseTest, WorksWithMoveOnly) {
1088 helper.Call(MoveOnly(0));
1090 helper.Call(MoveOnly(1));
1092 helper.Call(MoveOnly(0));
1094 helper.Call(MoveOnly(-1));
1096 helper.Call(MoveOnly(0));
1098 helper.Call(MoveOnly(1));
1102 TEST(IsNullTest, MatchesNullPointer) {
1103 Matcher<int*> m1 =
IsNull();
1109 Matcher<const char*> m2 =
IsNull();
1110 const char* p2 =
nullptr;
1114 Matcher<void*> m3 =
IsNull();
1117 EXPECT_FALSE(m3.Matches(
reinterpret_cast<void*
>(0xbeef)));
1120 TEST(IsNullTest, StdFunction) {
1128 TEST(IsNullTest, CanDescribeSelf) {
1131 EXPECT_EQ(
"isn't NULL", DescribeNegation(
m));
1135 TEST(NotNullTest, MatchesNonNullPointer) {
1142 Matcher<const char*> m2 =
NotNull();
1143 const char* p2 =
nullptr;
1148 TEST(NotNullTest, LinkedPtr) {
1149 const Matcher<std::shared_ptr<int>>
m =
NotNull();
1150 const std::shared_ptr<int> null_p;
1151 const std::shared_ptr<int> non_null_p(
new int);
1157 TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1158 const Matcher<const std::shared_ptr<double>&>
m =
NotNull();
1159 const std::shared_ptr<double> null_p;
1160 const std::shared_ptr<double> non_null_p(
new double);
1166 TEST(NotNullTest, StdFunction) {
1174 TEST(NotNullTest, CanDescribeSelf) {
1181 TEST(RefTest, MatchesSameVariable) {
1184 Matcher<int&>
m =
Ref(a);
1190 TEST(RefTest, CanDescribeSelf) {
1192 Matcher<int&>
m =
Ref(n);
1194 ss <<
"references the variable @" << &
n <<
" 5";
1200 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1203 Matcher<const int&>
m =
Ref(a);
1212 TEST(RefTest, IsCovariant) {
1215 Matcher<const Base&> m1 =
Ref(
base);
1226 TEST(RefTest, ExplainsResult) {
1238 template <
typename T = std::
string>
1243 TEST(StringLike, TestConversions) {
1244 EXPECT_EQ(
"foo", FromStringLike(
"foo"));
1246 #if GTEST_INTERNAL_HAS_STRING_VIEW
1247 EXPECT_EQ(
"foo", FromStringLike(internal::StringView(
"foo")));
1248 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1252 EXPECT_EQ(
"foo", FromStringLike({
'f',
'o',
'o'}));
1253 const char buf[] =
"foo";
1257 TEST(StrEqTest, MatchesEqualString) {
1263 Matcher<const std::string&> m2 =
StrEq(
"Hello");
1267 #if GTEST_INTERNAL_HAS_STRING_VIEW
1268 Matcher<const internal::StringView&> m3 =
1269 StrEq(internal::StringView(
"Hello"));
1270 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1271 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1274 Matcher<const internal::StringView&> m_empty =
StrEq(
"");
1275 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1276 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1277 EXPECT_FALSE(m_empty.Matches(internal::StringView(
"hello")));
1278 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1281 TEST(StrEqTest, CanDescribeSelf) {
1282 Matcher<std::string>
m =
StrEq(
"Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1283 EXPECT_EQ(
"is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1288 Matcher<std::string> m2 =
StrEq(
str);
1289 EXPECT_EQ(
"is equal to \"012\\04500800\"", Describe(m2));
1291 Matcher<std::string> m3 =
StrEq(
str);
1292 EXPECT_EQ(
"is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1295 TEST(StrNeTest, MatchesUnequalString) {
1296 Matcher<const char*>
m =
StrNe(
"Hello");
1305 #if GTEST_INTERNAL_HAS_STRING_VIEW
1306 Matcher<const internal::StringView> m3 =
StrNe(internal::StringView(
"Hello"));
1307 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1309 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1310 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1313 TEST(StrNeTest, CanDescribeSelf) {
1314 Matcher<const char*>
m =
StrNe(
"Hi");
1315 EXPECT_EQ(
"isn't equal to \"Hi\"", Describe(
m));
1318 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1325 Matcher<const std::string&> m2 =
StrCaseEq(
"Hello");
1329 #if GTEST_INTERNAL_HAS_STRING_VIEW
1330 Matcher<const internal::StringView&> m3 =
1331 StrCaseEq(internal::StringView(
"Hello"));
1332 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1333 EXPECT_TRUE(m3.Matches(internal::StringView(
"hello")));
1336 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1339 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1342 Matcher<const std::string&> m0 =
StrCaseEq(str1);
1345 str1[3] = str2[3] =
'\0';
1346 Matcher<const std::string&> m1 =
StrCaseEq(str1);
1349 str1[0] = str1[6] = str1[7] = str1[10] =
'\0';
1350 str2[0] = str2[6] = str2[7] = str2[10] =
'\0';
1351 Matcher<const std::string&> m2 =
StrCaseEq(str1);
1352 str1[9] = str2[9] =
'\0';
1355 Matcher<const std::string&> m3 =
StrCaseEq(str1);
1359 str2.append(1,
'\0');
1364 TEST(StrCaseEqTest, CanDescribeSelf) {
1366 EXPECT_EQ(
"is equal to (ignoring case) \"Hi\"", Describe(
m));
1369 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1380 #if GTEST_INTERNAL_HAS_STRING_VIEW
1381 Matcher<const internal::StringView> m3 =
1382 StrCaseNe(internal::StringView(
"Hello"));
1383 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hi")));
1385 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1386 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1387 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1390 TEST(StrCaseNeTest, CanDescribeSelf) {
1392 EXPECT_EQ(
"isn't equal to (ignoring case) \"Hi\"", Describe(
m));
1396 TEST(HasSubstrTest, WorksForStringClasses) {
1397 const Matcher<std::string> m1 =
HasSubstr(
"foo");
1401 const Matcher<const std::string&> m2 =
HasSubstr(
"foo");
1405 const Matcher<std::string> m_empty =
HasSubstr(
"");
1411 TEST(HasSubstrTest, WorksForCStrings) {
1412 const Matcher<char*> m1 =
HasSubstr(
"foo");
1413 EXPECT_TRUE(m1.Matches(
const_cast<char*
>(
"I love food.")));
1417 const Matcher<const char*> m2 =
HasSubstr(
"foo");
1422 const Matcher<const char*> m_empty =
HasSubstr(
"");
1428 #if GTEST_INTERNAL_HAS_STRING_VIEW
1430 TEST(HasSubstrTest, WorksForStringViewClasses) {
1431 const Matcher<internal::StringView> m1 =
1433 EXPECT_TRUE(m1.Matches(internal::StringView(
"I love food.")));
1434 EXPECT_FALSE(m1.Matches(internal::StringView(
"tofo")));
1437 const Matcher<const internal::StringView&> m2 =
HasSubstr(
"foo");
1438 EXPECT_TRUE(m2.Matches(internal::StringView(
"I love food.")));
1439 EXPECT_FALSE(m2.Matches(internal::StringView(
"tofo")));
1442 const Matcher<const internal::StringView&> m3 =
HasSubstr(
"");
1443 EXPECT_TRUE(m3.Matches(internal::StringView(
"foo")));
1444 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1447 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1450 TEST(HasSubstrTest, CanDescribeSelf) {
1451 Matcher<std::string>
m =
HasSubstr(
"foo\n\"");
1452 EXPECT_EQ(
"has substring \"foo\\n\\\"\"", Describe(
m));
1455 TEST(KeyTest, CanDescribeSelf) {
1456 Matcher<const pair<std::string, int>&>
m =
Key(
"foo");
1457 EXPECT_EQ(
"has a key that is equal to \"foo\"", Describe(
m));
1458 EXPECT_EQ(
"doesn't have a key that is equal to \"foo\"", DescribeNegation(
m));
1461 TEST(KeyTest, ExplainsResult) {
1462 Matcher<pair<int, bool> >
m =
Key(GreaterThan(10));
1463 EXPECT_EQ(
"whose first field is a value which is 5 less than 10",
1464 Explain(
m, make_pair(5,
true)));
1465 EXPECT_EQ(
"whose first field is a value which is 5 more than 10",
1466 Explain(
m, make_pair(15,
true)));
1469 TEST(KeyTest, MatchesCorrectly) {
1470 pair<int, std::string>
p(25,
"foo");
1477 TEST(KeyTest, WorksWithMoveOnly) {
1478 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1485 struct PairWithGet {
1488 using first_type =
int;
1491 const int& GetImpl(Tag<0>)
const {
return member_1; }
1495 auto get(
const PairWithGet&
value) -> decltype(
value.GetImpl(Tag<I>())) {
1496 return value.GetImpl(Tag<I>());
1498 TEST(PairTest, MatchesPairWithGetCorrectly) {
1499 PairWithGet
p{25,
"foo"};
1505 std::vector<PairWithGet>
v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1509 TEST(KeyTest, SafelyCastsInnerMatcher) {
1510 Matcher<int> is_positive =
Gt(0);
1511 Matcher<int> is_negative =
Lt(0);
1512 pair<char, bool>
p(
'a',
true);
1517 TEST(KeyTest, InsideContainsUsingMap) {
1526 TEST(KeyTest, InsideContainsUsingMultimap) {
1542 TEST(PairTest, Typing) {
1544 Matcher<const pair<const char*, int>&> m1 =
Pair(
"foo", 42);
1545 Matcher<const pair<const char*, int> > m2 =
Pair(
"foo", 42);
1546 Matcher<pair<const char*, int> > m3 =
Pair(
"foo", 42);
1548 Matcher<pair<int, const std::string> > m4 =
Pair(25,
"42");
1549 Matcher<pair<const std::string, int> > m5 =
Pair(
"25", 42);
1552 TEST(PairTest, CanDescribeSelf) {
1553 Matcher<const pair<std::string, int>&> m1 =
Pair(
"foo", 42);
1554 EXPECT_EQ(
"has a first field that is equal to \"foo\""
1555 ", and has a second field that is equal to 42",
1557 EXPECT_EQ(
"has a first field that isn't equal to \"foo\""
1558 ", or has a second field that isn't equal to 42",
1559 DescribeNegation(m1));
1561 Matcher<const pair<int, int>&> m2 =
Not(
Pair(
Not(13), 42));
1562 EXPECT_EQ(
"has a first field that isn't equal to 13"
1563 ", and has a second field that is equal to 42",
1564 DescribeNegation(m2));
1567 TEST(PairTest, CanExplainMatchResultTo) {
1570 const Matcher<pair<int, int> >
m =
Pair(GreaterThan(0), GreaterThan(0));
1571 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1572 Explain(
m, make_pair(-1, -2)));
1576 EXPECT_EQ(
"whose second field does not match, which is 2 less than 0",
1577 Explain(
m, make_pair(1, -2)));
1581 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1582 Explain(
m, make_pair(-1, 2)));
1585 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1586 "which is 1 more than 0, and the second field is a value "
1587 "which is 2 more than 0",
1588 Explain(
m, make_pair(1, 2)));
1592 const Matcher<pair<int, int> > explain_first =
Pair(GreaterThan(0), 0);
1593 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1594 "which is 1 more than 0",
1595 Explain(explain_first, make_pair(1, 0)));
1599 const Matcher<pair<int, int> > explain_second =
Pair(0, GreaterThan(0));
1600 EXPECT_EQ(
"whose both fields match, where the second field is a value "
1601 "which is 1 more than 0",
1602 Explain(explain_second, make_pair(0, 1)));
1605 TEST(PairTest, MatchesCorrectly) {
1606 pair<int, std::string>
p(25,
"foo");
1625 TEST(PairTest, WorksWithMoveOnly) {
1626 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1627 p.second.reset(
new int(7));
1631 TEST(PairTest, SafelyCastsInnerMatchers) {
1632 Matcher<int> is_positive =
Gt(0);
1633 Matcher<int> is_negative =
Lt(0);
1634 pair<char, bool>
p(
'a',
true);
1641 TEST(PairTest, InsideContainsUsingMap) {
1652 TEST(FieldsAreTest, MatchesCorrectly) {
1653 std::tuple<int, std::string, double>
p(25,
"foo", .5);
1665 TEST(FieldsAreTest, CanDescribeSelf) {
1666 Matcher<const pair<std::string, int>&> m1 = FieldsAre(
"foo", 42);
1668 "has field #0 that is equal to \"foo\""
1669 ", and has field #1 that is equal to 42",
1672 "has field #0 that isn't equal to \"foo\""
1673 ", or has field #1 that isn't equal to 42",
1674 DescribeNegation(m1));
1677 TEST(FieldsAreTest, CanExplainMatchResultTo) {
1679 Matcher<std::tuple<int, int, int>>
m =
1680 FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));
1682 EXPECT_EQ(
"whose field #0 does not match, which is 1 less than 0",
1684 EXPECT_EQ(
"whose field #1 does not match, which is 2 less than 0",
1686 EXPECT_EQ(
"whose field #2 does not match, which is 3 less than 0",
1691 "whose all elements match, "
1692 "where field #0 is a value which is 1 more than 0"
1693 ", and field #1 is a value which is 2 more than 0"
1694 ", and field #2 is a value which is 3 more than 0",
1698 m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));
1700 "whose all elements match, "
1701 "where field #0 is a value which is 1 more than 0"
1702 ", and field #2 is a value which is 3 more than 0",
1706 m = FieldsAre(0, GreaterThan(0), 0);
1708 "whose all elements match, "
1709 "where field #1 is a value which is 1 more than 0",
1713 #if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
1714 TEST(FieldsAreTest, StructuredBindings) {
1742 EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));
1746 EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));
1750 EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));
1754 EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));
1758 EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));
1759 struct MyVarType10 {
1762 EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1763 struct MyVarType11 {
1764 int a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k;
1766 EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1767 struct MyVarType12 {
1768 int a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l;
1770 EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1771 struct MyVarType13 {
1772 int a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m;
1774 EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1775 struct MyVarType14 {
1776 int a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m,
n;
1779 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1780 struct MyVarType15 {
1781 int a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m,
n,
o;
1784 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1785 struct MyVarType16 {
1786 int a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m,
n,
o,
p;
1789 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1793 TEST(ContainsTest, WorksWithMoveOnly) {
1794 ContainerHelper helper;
1796 helper.Call(MakeUniquePtrs({1, 2}));
1799 TEST(PairTest, UseGetInsteadOfMembers) {
1800 PairWithGet
pair{7,
"ABC"};
1805 std::vector<PairWithGet>
v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1812 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1818 const Matcher<const std::string&> m2 =
StartsWith(
"Hi");
1825 #if GTEST_INTERNAL_HAS_STRING_VIEW
1826 const Matcher<internal::StringView> m_empty =
1828 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1829 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1830 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"not empty")));
1831 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1834 TEST(StartsWithTest, CanDescribeSelf) {
1836 EXPECT_EQ(
"starts with \"Hi\"", Describe(
m));
1841 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1842 const Matcher<const char*> m1 =
EndsWith(
"");
1854 #if GTEST_INTERNAL_HAS_STRING_VIEW
1855 const Matcher<const internal::StringView&> m4 =
1856 EndsWith(internal::StringView(
""));
1860 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1861 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1864 TEST(EndsWithTest, CanDescribeSelf) {
1865 Matcher<const std::string>
m =
EndsWith(
"Hi");
1871 TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
1872 const Matcher<const char*> m1 = WhenBase64Unescaped(
EndsWith(
"!"));
1877 const Matcher<const std::string&> m2 = WhenBase64Unescaped(
EndsWith(
"!"));
1882 #if GTEST_INTERNAL_HAS_STRING_VIEW
1883 const Matcher<const internal::StringView&> m3 =
1884 WhenBase64Unescaped(
EndsWith(
"!"));
1888 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1891 TEST(WhenBase64UnescapedTest, CanDescribeSelf) {
1892 const Matcher<const char*>
m = WhenBase64Unescaped(
EndsWith(
"!"));
1893 EXPECT_EQ(
"matches after Base64Unescape ends with \"!\"", Describe(
m));
1898 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1904 const Matcher<const std::string&> m2 =
MatchesRegex(
new RE(
"a.*z"));
1909 #if GTEST_INTERNAL_HAS_STRING_VIEW
1910 const Matcher<const internal::StringView&> m3 =
MatchesRegex(
"a.*z");
1911 EXPECT_TRUE(m3.Matches(internal::StringView(
"az")));
1912 EXPECT_TRUE(m3.Matches(internal::StringView(
"abcz")));
1915 const Matcher<const internal::StringView&> m4 =
1917 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1919 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1922 TEST(MatchesRegexTest, CanDescribeSelf) {
1924 EXPECT_EQ(
"matches regular expression \"Hi.*\"", Describe(m1));
1927 EXPECT_EQ(
"matches regular expression \"a.*\"", Describe(m2));
1929 #if GTEST_INTERNAL_HAS_STRING_VIEW
1930 Matcher<const internal::StringView> m3 =
MatchesRegex(
new RE(
"0.*"));
1931 EXPECT_EQ(
"matches regular expression \"0.*\"", Describe(m3));
1932 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1937 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1943 const Matcher<const std::string&> m2 =
ContainsRegex(
new RE(
"a.*z"));
1948 #if GTEST_INTERNAL_HAS_STRING_VIEW
1949 const Matcher<const internal::StringView&> m3 =
1951 EXPECT_TRUE(m3.Matches(internal::StringView(
"azbz")));
1952 EXPECT_TRUE(m3.Matches(internal::StringView(
"az1")));
1955 const Matcher<const internal::StringView&> m4 =
1957 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1959 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1962 TEST(ContainsRegexTest, CanDescribeSelf) {
1964 EXPECT_EQ(
"contains regular expression \"Hi.*\"", Describe(m1));
1967 EXPECT_EQ(
"contains regular expression \"a.*\"", Describe(m2));
1969 #if GTEST_INTERNAL_HAS_STRING_VIEW
1970 Matcher<const internal::StringView> m3 =
ContainsRegex(
new RE(
"0.*"));
1971 EXPECT_EQ(
"contains regular expression \"0.*\"", Describe(m3));
1972 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1976 #if GTEST_HAS_STD_WSTRING
1977 TEST(StdWideStrEqTest, MatchesEqual) {
1983 Matcher<const ::std::wstring&> m2 =
StrEq(L
"Hello");
1987 Matcher<const ::std::wstring&> m3 =
StrEq(L
"\xD3\x576\x8D3\xC74D");
1993 Matcher<const ::std::wstring&> m4 =
StrEq(
str);
1996 Matcher<const ::std::wstring&> m5 =
StrEq(
str);
2000 TEST(StdWideStrEqTest, CanDescribeSelf) {
2001 Matcher< ::std::wstring>
m =
StrEq(L
"Hi-\'\"?\\\a\b\f\n\r\t\v");
2002 EXPECT_EQ(
"is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
2005 Matcher< ::std::wstring> m2 =
StrEq(L
"\xD3\x576\x8D3\xC74D");
2006 EXPECT_EQ(
"is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
2011 Matcher<const ::std::wstring&> m4 =
StrEq(
str);
2012 EXPECT_EQ(
"is equal to L\"012\\04500800\"", Describe(m4));
2014 Matcher<const ::std::wstring&> m5 =
StrEq(
str);
2015 EXPECT_EQ(
"is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
2018 TEST(StdWideStrNeTest, MatchesUnequalString) {
2019 Matcher<const wchar_t*>
m =
StrNe(L
"Hello");
2029 TEST(StdWideStrNeTest, CanDescribeSelf) {
2030 Matcher<const wchar_t*>
m =
StrNe(L
"Hi");
2031 EXPECT_EQ(
"isn't equal to L\"Hi\"", Describe(
m));
2034 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
2041 Matcher<const ::std::wstring&> m2 =
StrCaseEq(L
"Hello");
2046 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
2049 Matcher<const ::std::wstring&> m0 =
StrCaseEq(str1);
2052 str1[3] = str2[3] =
L'\0';
2053 Matcher<const ::std::wstring&> m1 =
StrCaseEq(str1);
2056 str1[0] = str1[6] = str1[7] = str1[10] =
L'\0';
2057 str2[0] = str2[6] = str2[7] = str2[10] =
L'\0';
2058 Matcher<const ::std::wstring&> m2 =
StrCaseEq(str1);
2059 str1[9] = str2[9] =
L'\0';
2062 Matcher<const ::std::wstring&> m3 =
StrCaseEq(str1);
2066 str2.append(1, L
'\0');
2071 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
2072 Matcher< ::std::wstring>
m =
StrCaseEq(L
"Hi");
2073 EXPECT_EQ(
"is equal to (ignoring case) L\"Hi\"", Describe(
m));
2076 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
2077 Matcher<const wchar_t*>
m =
StrCaseNe(L
"Hello");
2088 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
2089 Matcher<const wchar_t*>
m =
StrCaseNe(L
"Hi");
2090 EXPECT_EQ(
"isn't equal to (ignoring case) L\"Hi\"", Describe(
m));
2094 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
2095 const Matcher< ::std::wstring> m1 =
HasSubstr(L
"foo");
2099 const Matcher<const ::std::wstring&> m2 =
HasSubstr(L
"foo");
2105 TEST(StdWideHasSubstrTest, WorksForCStrings) {
2106 const Matcher<wchar_t*> m1 =
HasSubstr(L
"foo");
2107 EXPECT_TRUE(m1.Matches(
const_cast<wchar_t*
>(L
"I love food.")));
2108 EXPECT_FALSE(m1.Matches(
const_cast<wchar_t*
>(L
"tofo")));
2111 const Matcher<const wchar_t*> m2 =
HasSubstr(L
"foo");
2118 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
2119 Matcher< ::std::wstring>
m =
HasSubstr(L
"foo\n\"");
2120 EXPECT_EQ(
"has substring L\"foo\\n\\\"\"", Describe(
m));
2125 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
2131 const Matcher<const ::std::wstring&> m2 =
StartsWith(L
"Hi");
2139 TEST(StdWideStartsWithTest, CanDescribeSelf) {
2140 Matcher<const ::std::wstring>
m =
StartsWith(L
"Hi");
2141 EXPECT_EQ(
"starts with L\"Hi\"", Describe(
m));
2146 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
2147 const Matcher<const wchar_t*> m1 =
EndsWith(L
"");
2160 TEST(StdWideEndsWithTest, CanDescribeSelf) {
2161 Matcher<const ::std::wstring>
m =
EndsWith(L
"Hi");
2165 #endif // GTEST_HAS_STD_WSTRING
2167 typedef ::std::tuple<long, int> Tuple2;
2171 TEST(Eq2Test, MatchesEqualArguments) {
2172 Matcher<const Tuple2&>
m =
Eq();
2178 TEST(Eq2Test, CanDescribeSelf) {
2179 Matcher<const Tuple2&>
m =
Eq();
2185 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
2186 Matcher<const Tuple2&>
m =
Ge();
2193 TEST(Ge2Test, CanDescribeSelf) {
2194 Matcher<const Tuple2&>
m =
Ge();
2195 EXPECT_EQ(
"are a pair where the first >= the second", Describe(
m));
2200 TEST(Gt2Test, MatchesGreaterThanArguments) {
2201 Matcher<const Tuple2&>
m =
Gt();
2208 TEST(Gt2Test, CanDescribeSelf) {
2209 Matcher<const Tuple2&>
m =
Gt();
2210 EXPECT_EQ(
"are a pair where the first > the second", Describe(
m));
2215 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
2216 Matcher<const Tuple2&>
m =
Le();
2223 TEST(Le2Test, CanDescribeSelf) {
2224 Matcher<const Tuple2&>
m =
Le();
2225 EXPECT_EQ(
"are a pair where the first <= the second", Describe(
m));
2230 TEST(Lt2Test, MatchesLessThanArguments) {
2231 Matcher<const Tuple2&>
m =
Lt();
2238 TEST(Lt2Test, CanDescribeSelf) {
2239 Matcher<const Tuple2&>
m =
Lt();
2240 EXPECT_EQ(
"are a pair where the first < the second", Describe(
m));
2245 TEST(Ne2Test, MatchesUnequalArguments) {
2246 Matcher<const Tuple2&>
m =
Ne();
2253 TEST(Ne2Test, CanDescribeSelf) {
2254 Matcher<const Tuple2&>
m =
Ne();
2255 EXPECT_EQ(
"are an unequal pair", Describe(
m));
2258 TEST(PairMatchBaseTest, WorksWithMoveOnly) {
2259 using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
2260 Matcher<Pointers> matcher =
Eq();
2269 float quiet_nan = std::numeric_limits<float>::quiet_NaN();
2270 float other_nan = std::nanf(
"1");
2271 float real_value = 1.0f;
2273 Matcher<float>
m =
IsNan();
2278 Matcher<float&> m_ref =
IsNan();
2283 Matcher<const float&> m_cref =
IsNan();
2291 double quiet_nan = std::numeric_limits<double>::quiet_NaN();
2292 double other_nan = std::nan(
"1");
2293 double real_value = 1.0;
2295 Matcher<double>
m =
IsNan();
2300 Matcher<double&> m_ref =
IsNan();
2305 Matcher<const double&> m_cref =
IsNan();
2313 long double quiet_nan = std::numeric_limits<long double>::quiet_NaN();
2314 long double other_nan = std::nan(
"1");
2315 long double real_value = 1.0;
2317 Matcher<long double>
m =
IsNan();
2322 Matcher<long double&> m_ref =
IsNan();
2327 Matcher<const long double&> m_cref =
IsNan();
2336 EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN()));
2341 EXPECT_FALSE(
md.Matches(std::numeric_limits<double>::quiet_NaN()));
2345 Matcher<long double> mld =
Not(
IsNan());
2346 EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN()));
2353 Matcher<float> mf =
IsNan();
2359 Matcher<long double> mld =
IsNan();
2371 Matcher<long double> mld =
Not(
IsNan());
2377 TEST(FloatEq2Test, MatchesEqualArguments) {
2378 typedef ::std::tuple<float, float> Tpl;
2386 TEST(FloatEq2Test, CanDescribeSelf) {
2387 Matcher<const ::std::tuple<float, float>&>
m =
FloatEq();
2388 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2393 TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {
2394 typedef ::std::tuple<float, float> Tpl;
2397 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2398 std::numeric_limits<float>::quiet_NaN())));
2400 EXPECT_FALSE(
m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2401 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2405 TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
2407 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2412 TEST(DoubleEq2Test, MatchesEqualArguments) {
2413 typedef ::std::tuple<double, double> Tpl;
2421 TEST(DoubleEq2Test, CanDescribeSelf) {
2422 Matcher<const ::std::tuple<double, double>&>
m =
DoubleEq();
2423 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2428 TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {
2429 typedef ::std::tuple<double, double> Tpl;
2432 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2433 std::numeric_limits<double>::quiet_NaN())));
2435 EXPECT_FALSE(
m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2436 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2440 TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
2442 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2447 TEST(FloatNear2Test, MatchesEqualArguments) {
2448 typedef ::std::tuple<float, float> Tpl;
2456 TEST(FloatNear2Test, CanDescribeSelf) {
2457 Matcher<const ::std::tuple<float, float>&>
m =
FloatNear(0.5f);
2458 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2463 TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {
2464 typedef ::std::tuple<float, float> Tpl;
2468 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2469 std::numeric_limits<float>::quiet_NaN())));
2471 EXPECT_FALSE(
m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2472 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2476 TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
2478 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2483 TEST(DoubleNear2Test, MatchesEqualArguments) {
2484 typedef ::std::tuple<double, double> Tpl;
2492 TEST(DoubleNear2Test, CanDescribeSelf) {
2493 Matcher<const ::std::tuple<double, double>&>
m =
DoubleNear(0.5);
2494 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2499 TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {
2500 typedef ::std::tuple<double, double> Tpl;
2504 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2505 std::numeric_limits<double>::quiet_NaN())));
2507 EXPECT_FALSE(
m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2508 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2512 TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
2514 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2518 TEST(NotTest, NegatesMatcher) {
2526 TEST(NotTest, CanDescribeSelf) {
2527 Matcher<int>
m =
Not(
Eq(5));
2532 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
2534 Matcher<int> greater_than_5 =
Gt(5);
2536 Matcher<const int&>
m =
Not(greater_than_5);
2537 Matcher<int&> m2 =
Not(greater_than_5);
2538 Matcher<int&> m3 =
Not(
m);
2542 void AllOfMatches(
int num,
const Matcher<int>&
m) {
2545 for (
int i = 1;
i <=
num; ++
i) {
2553 TEST(AllOfTest, MatchesWhenAllMatch) {
2596 50,
AllOf(
Ne(1),
Ne(2),
Ne(3),
Ne(4),
Ne(5),
Ne(6),
Ne(7),
Ne(8),
Ne(9),
2607 TEST(AllOfTest, CanDescribeSelf) {
2610 EXPECT_EQ(
"(is <= 2) and (is >= 1)", Describe(
m));
2614 "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
2619 "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
2625 "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
2626 "and (isn't equal to 7)";
2631 TEST(AllOfTest, CanDescribeNegation) {
2634 std::string expected_descr4 =
"(isn't <= 2) or (isn't >= 1)";
2635 EXPECT_EQ(expected_descr4, DescribeNegation(
m));
2639 "(isn't > 0) or (is equal to 1) or (is equal to 2)";
2640 EXPECT_EQ(expected_descr5, DescribeNegation(
m));
2644 "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
2645 EXPECT_EQ(expected_descr6, DescribeNegation(
m));
2649 "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
2651 EXPECT_EQ(expected_desr7, DescribeNegation(
m));
2655 AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2657 AllOfMatches(11,
m);
2661 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2663 Matcher<int> greater_than_5 =
Gt(5);
2664 Matcher<int> less_than_10 =
Lt(10);
2666 Matcher<const int&>
m =
AllOf(greater_than_5, less_than_10);
2667 Matcher<int&> m2 =
AllOf(greater_than_5, less_than_10);
2668 Matcher<int&> m3 =
AllOf(greater_than_5, m2);
2671 Matcher<const int&> m4 =
AllOf(greater_than_5, less_than_10, less_than_10);
2672 Matcher<int&> m5 =
AllOf(greater_than_5, less_than_10, less_than_10);
2675 TEST(AllOfTest, ExplainsResult) {
2681 m =
AllOf(GreaterThan(10),
Lt(30));
2682 EXPECT_EQ(
"which is 15 more than 10", Explain(
m, 25));
2685 m =
AllOf(GreaterThan(10), GreaterThan(20));
2686 EXPECT_EQ(
"which is 20 more than 10, and which is 10 more than 20",
2691 m =
AllOf(GreaterThan(10),
Lt(30), GreaterThan(20));
2692 EXPECT_EQ(
"which is 15 more than 10, and which is 5 more than 20",
2696 m =
AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2697 EXPECT_EQ(
"which is 30 more than 10, and which is 20 more than 20, "
2698 "and which is 10 more than 30",
2703 m =
AllOf(GreaterThan(10), GreaterThan(20));
2704 EXPECT_EQ(
"which is 5 less than 10", Explain(
m, 5));
2709 m =
AllOf(GreaterThan(10),
Lt(30));
2714 m =
AllOf(GreaterThan(10), GreaterThan(20));
2715 EXPECT_EQ(
"which is 5 less than 20", Explain(
m, 15));
2719 static void AnyOfMatches(
int num,
const Matcher<int>&
m) {
2722 for (
int i = 1;
i <=
num; ++
i) {
2728 static void AnyOfStringMatches(
int num,
const Matcher<std::string>&
m) {
2732 for (
int i = 1;
i <=
num; ++
i) {
2740 TEST(AnyOfTest, MatchesWhenAnyMatches) {
2770 AnyOfMatches(2,
AnyOf(1, 2));
2771 AnyOfMatches(3,
AnyOf(1, 2, 3));
2772 AnyOfMatches(4,
AnyOf(1, 2, 3, 4));
2773 AnyOfMatches(5,
AnyOf(1, 2, 3, 4, 5));
2774 AnyOfMatches(6,
AnyOf(1, 2, 3, 4, 5, 6));
2775 AnyOfMatches(7,
AnyOf(1, 2, 3, 4, 5, 6, 7));
2776 AnyOfMatches(8,
AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2777 AnyOfMatches(9,
AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2778 AnyOfMatches(10,
AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
2782 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
2785 Matcher<int>
m =
::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2788 AnyOfMatches(11,
m);
2789 AnyOfMatches(50,
AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2790 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
2791 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
2792 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2793 41, 42, 43, 44, 45, 46, 47, 48, 49, 50));
2795 50,
AnyOf(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
2796 "13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
2797 "23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"32",
2798 "33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
"41",
"42",
2799 "43",
"44",
"45",
"46",
"47",
"48",
"49",
"50"));
2802 TEST(ConditionalTest, MatchesFirstIfCondition) {
2803 Matcher<std::string> eq_red =
Eq(
"red");
2804 Matcher<std::string> ne_red =
Ne(
"red");
2805 Matcher<std::string>
m = Conditional(
true, eq_red, ne_red);
2809 StringMatchResultListener listener;
2810 StringMatchResultListener expected;
2812 EXPECT_FALSE(eq_red.MatchAndExplain(
"green", &expected));
2816 TEST(ConditionalTest, MatchesSecondIfCondition) {
2817 Matcher<std::string> eq_red =
Eq(
"red");
2818 Matcher<std::string> ne_red =
Ne(
"red");
2819 Matcher<std::string>
m = Conditional(
false, eq_red, ne_red);
2823 StringMatchResultListener listener;
2824 StringMatchResultListener expected;
2826 EXPECT_FALSE(ne_red.MatchAndExplain(
"red", &expected));
2831 TEST(ElementsAreTest, HugeMatcher) {
2832 vector<int>
test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
2840 TEST(ElementsAreTest, HugeMatcherStr) {
2842 "literal_string",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
2849 TEST(ElementsAreTest, HugeMatcherUnordered) {
2850 vector<int>
test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
2859 TEST(AnyOfTest, CanDescribeSelf) {
2867 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2)", Describe(
m));
2870 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
2875 "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
2881 TEST(AnyOfTest, CanDescribeNegation) {
2884 EXPECT_EQ(
"(isn't <= 1) and (isn't >= 3)",
2885 DescribeNegation(
m));
2888 EXPECT_EQ(
"(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
2889 DescribeNegation(
m));
2893 "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
2895 DescribeNegation(
m));
2899 "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
2900 "to 5) and (isn't equal to 7)",
2901 DescribeNegation(
m));
2905 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2907 Matcher<int> greater_than_5 =
Gt(5);
2908 Matcher<int> less_than_10 =
Lt(10);
2910 Matcher<const int&>
m =
AnyOf(greater_than_5, less_than_10);
2911 Matcher<int&> m2 =
AnyOf(greater_than_5, less_than_10);
2912 Matcher<int&> m3 =
AnyOf(greater_than_5, m2);
2915 Matcher<const int&> m4 =
AnyOf(greater_than_5, less_than_10, less_than_10);
2916 Matcher<int&> m5 =
AnyOf(greater_than_5, less_than_10, less_than_10);
2919 TEST(AnyOfTest, ExplainsResult) {
2926 EXPECT_EQ(
"which is 5 less than 10", Explain(
m, 5));
2929 m =
AnyOf(GreaterThan(10), GreaterThan(20));
2930 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20",
2935 m =
AnyOf(GreaterThan(10),
Gt(20), GreaterThan(30));
2936 EXPECT_EQ(
"which is 5 less than 10, and which is 25 less than 30",
2940 m =
AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2941 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20, "
2942 "and which is 25 less than 30",
2947 m =
AnyOf(GreaterThan(10), GreaterThan(20));
2948 EXPECT_EQ(
"which is 5 more than 10", Explain(
m, 15));
2953 m =
AnyOf(GreaterThan(10),
Lt(30));
2958 m =
AnyOf(GreaterThan(30), GreaterThan(20));
2959 EXPECT_EQ(
"which is 5 more than 20", Explain(
m, 25));
2969 int IsPositive(
double x) {
2970 return x > 0 ? 1 : 0;
2975 class IsGreaterThan {
2977 explicit IsGreaterThan(
int threshold) :
threshold_(threshold) {}
2979 bool operator()(
int n)
const {
return n >
threshold_; }
2990 bool ReferencesFooAndIsZero(
const int& n) {
2991 return (&
n == &
foo) && (
n == 0);
2996 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2997 Matcher<double>
m =
Truly(IsPositive);
3003 TEST(TrulyTest, CanBeUsedWithFunctor) {
3004 Matcher<int>
m =
Truly(IsGreaterThan(5));
3010 class ConvertibleToBool {
3019 ConvertibleToBool IsNotZero(
int number) {
3020 return ConvertibleToBool(
number);
3026 TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
3027 Matcher<int>
m =
Truly(IsNotZero);
3033 TEST(TrulyTest, CanDescribeSelf) {
3034 Matcher<double>
m =
Truly(IsPositive);
3035 EXPECT_EQ(
"satisfies the given predicate",
3041 TEST(TrulyTest, WorksForByRefArguments) {
3042 Matcher<const int&>
m =
Truly(ReferencesFooAndIsZero);
3049 TEST(TrulyTest, ExplainsFailures) {
3050 StringMatchResultListener listener;
3052 EXPECT_EQ(listener.str(),
"didn't satisfy the given predicate");
3057 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
3064 TEST(MatchesTest, WorksOnByRefArguments) {
3072 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
3073 Matcher<int> eq5 =
Eq(5);
3081 TEST(ValueTest, WorksWithPolymorphicMatcher) {
3086 TEST(ValueTest, WorksWithMonomorphicMatcher) {
3087 const Matcher<int> is_zero =
Eq(0);
3092 const Matcher<const int&> ref_n =
Ref(n);
3097 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
3098 StringMatchResultListener listener1;
3102 StringMatchResultListener listener2;
3107 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
3108 const Matcher<int> is_even = PolymorphicIsEven();
3109 StringMatchResultListener listener1;
3113 const Matcher<const double&> is_zero =
Eq(0);
3114 StringMatchResultListener listener2;
3119 MATCHER(ConstructNoArg,
"") {
return true; }
3120 MATCHER_P(Construct1Arg, arg1,
"") {
return true; }
3121 MATCHER_P2(Construct2Args, arg1, arg2,
"") {
return true; }
3123 TEST(MatcherConstruct, ExplicitVsImplicit) {
3126 ConstructNoArgMatcher
m = {};
3129 ConstructNoArgMatcher m2;
3135 using M = Construct1ArgMatcherP<int>;
3141 Construct2ArgsMatcherP2<int, double>
m = {1, 2.2};
3150 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
3154 TEST(DescribeMatcherTest, WorksWithValue) {
3155 EXPECT_EQ(
"is equal to 42", DescribeMatcher<int>(42));
3156 EXPECT_EQ(
"isn't equal to 42", DescribeMatcher<int>(42,
true));
3159 TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
3160 const Matcher<int> monomorphic =
Le(0);
3161 EXPECT_EQ(
"is <= 0", DescribeMatcher<int>(monomorphic));
3162 EXPECT_EQ(
"isn't <= 0", DescribeMatcher<int>(monomorphic,
true));
3165 TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
3166 EXPECT_EQ(
"is even", DescribeMatcher<int>(PolymorphicIsEven()));
3167 EXPECT_EQ(
"is odd", DescribeMatcher<int>(PolymorphicIsEven(),
true));
3170 TEST(AllArgsTest, WorksForTuple) {
3175 TEST(AllArgsTest, WorksForNonTuple) {
3180 class AllArgsHelper {
3190 TEST(AllArgsTest, WorksInWithClause) {
3191 AllArgsHelper helper;
3194 .WillByDefault(
Return(1));
3204 class OptionalMatchersHelper {
3206 OptionalMatchersHelper() {}
3221 TEST(AllArgsTest, WorksWithoutMatchers) {
3222 OptionalMatchersHelper helper;
3244 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
3253 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
3256 static unsigned short n;
3261 "Expected: is > 10\n"
3262 " Actual: 5" + OfType(
"unsigned short"));
3267 "Expected: (is <= 7) and (is >= 5)\n"
3268 " Actual: 0" + OfType(
"unsigned short"));
3273 TEST(MatcherAssertionTest, WorksForByRefArguments) {
3281 "Expected: does not reference the variable @");
3284 "Actual: 0" + OfType(
"int") +
", which is located @");
3289 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
3290 Matcher<const char*> starts_with_he =
StartsWith(
"he");
3293 Matcher<const std::string&> ends_with_ok =
EndsWith(
"ok");
3298 "Expected: ends with \"ok\"\n"
3299 " Actual: \"bad\"");
3300 Matcher<int> is_greater_than_5 =
Gt(5);
3303 "Expected: is > 5\n"
3304 " Actual: 5" + OfType(
"int"));
3308 template <
typename RawType>
3333 nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
3334 nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {
3338 EXPECT_EQ(
sizeof(RawType),
sizeof(Bits));
3345 Matcher<RawType> m1 = matcher_maker(0.0);
3354 Matcher<RawType> m3 = matcher_maker(1.0);
3361 Matcher<RawType> m4 = matcher_maker(-
infinity_);
3364 Matcher<RawType> m5 = matcher_maker(
infinity_);
3373 Matcher<const RawType&> m6 = matcher_maker(0.0);
3380 Matcher<RawType&> m7 = matcher_maker(0.0);
3418 template <
typename RawType>
3419 class FloatingPointNearTest :
public FloatingPointTest<RawType> {
3421 typedef FloatingPointTest<RawType> ParentType;
3425 void TestNearMatches(
3427 (*matcher_maker)(RawType, RawType)) {
3428 Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
3435 Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
3474 Matcher<RawType> m9 = matcher_maker(
3480 Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
3487 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
3502 typedef FloatingPointTest<float> FloatTest;
3504 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
3508 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
3512 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
3520 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
3528 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
3529 Matcher<float> m1 =
FloatEq(2.0f);
3530 EXPECT_EQ(
"is approximately 2", Describe(m1));
3531 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3533 Matcher<float> m2 =
FloatEq(0.5f);
3534 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3535 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3538 EXPECT_EQ(
"never matches", Describe(m3));
3539 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3542 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
3544 EXPECT_EQ(
"is approximately 2", Describe(m1));
3545 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3548 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3549 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3553 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3558 typedef FloatingPointNearTest<float> FloatNearTest;
3560 TEST_F(FloatNearTest, FloatNearMatches) {
3564 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
3568 TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
3569 Matcher<float> m1 =
FloatNear(2.0f, 0.5f);
3570 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3572 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3574 Matcher<float> m2 =
FloatNear(0.5f, 0.5f);
3575 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3577 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3580 EXPECT_EQ(
"never matches", Describe(m3));
3581 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3584 TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
3586 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3588 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3591 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3593 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3597 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3600 TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
3608 TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
3617 typedef FloatingPointTest<double> DoubleTest;
3619 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
3623 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
3627 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
3635 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
3643 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
3644 Matcher<double> m1 =
DoubleEq(2.0);
3645 EXPECT_EQ(
"is approximately 2", Describe(m1));
3646 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3648 Matcher<double> m2 =
DoubleEq(0.5);
3649 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3650 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3653 EXPECT_EQ(
"never matches", Describe(m3));
3654 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3657 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
3659 EXPECT_EQ(
"is approximately 2", Describe(m1));
3660 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3663 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3664 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3668 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3673 typedef FloatingPointNearTest<double> DoubleNearTest;
3675 TEST_F(DoubleNearTest, DoubleNearMatches) {
3679 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
3683 TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
3685 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3687 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3690 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3692 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3695 EXPECT_EQ(
"never matches", Describe(m3));
3696 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3699 TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {
3705 Explain(
DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);
3708 EXPECT_TRUE(explanation ==
"which is 1.2e-10 from 2.1" ||
3709 explanation ==
"which is 1.2e-010 from 2.1")
3710 <<
" where explanation is \"" << explanation <<
"\".";
3713 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
3715 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3717 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3720 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3722 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3726 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3729 TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
3737 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
3745 TEST(PointeeTest, RawPointer) {
3755 TEST(PointeeTest, RawPointerToConst) {
3756 const Matcher<const double*>
m =
Pointee(
Ge(0));
3765 TEST(PointeeTest, ReferenceToConstRawPointer) {
3766 const Matcher<int* const &>
m =
Pointee(
Ge(0));
3775 TEST(PointeeTest, ReferenceToNonConstRawPointer) {
3787 TEST(PointeeTest, SmartPointer) {
3788 const Matcher<std::unique_ptr<int>>
m =
Pointee(
Ge(0));
3790 std::unique_ptr<int>
n(
new int(1));
3794 TEST(PointeeTest, SmartPointerToConst) {
3795 const Matcher<std::unique_ptr<const int>>
m =
Pointee(
Ge(0));
3800 std::unique_ptr<const int>
n(
new int(1));
3804 TEST(PointerTest, RawPointer) {
3815 TEST(PointerTest, RawPointerToConst) {
3817 const Matcher<const int*>
m =
Pointer(
Eq(&n));
3826 TEST(PointerTest, SmartPointer) {
3827 std::unique_ptr<int>
n(
new int(10));
3828 int* raw_n =
n.get();
3829 const Matcher<std::unique_ptr<int>>
m =
Pointer(
Eq(raw_n));
3834 TEST(PointerTest, SmartPointerToConst) {
3835 std::unique_ptr<const int>
n(
new int(10));
3836 const int* raw_n =
n.get();
3837 const Matcher<std::unique_ptr<const int>>
m =
Pointer(
Eq(raw_n));
3842 std::unique_ptr<const int>
p(
new int(10));
3846 TEST(AddressTest, NonConst) {
3872 TEST(AddressTest, MatcherDoesntCopy) {
3873 std::unique_ptr<int>
n(
new int(1));
3874 const Matcher<std::unique_ptr<int>>
m =
Address(
Eq(&n));
3879 TEST(AddressTest, Describe) {
3881 EXPECT_EQ(
"has address that is anything", Describe(matcher));
3882 EXPECT_EQ(
"does not have address that is anything",
3883 DescribeNegation(matcher));
3886 MATCHER_P(FieldIIs, inner_matcher,
"") {
3891 TEST(WhenDynamicCastToTest, SameType) {
3896 Base* as_base_ptr = &derived;
3900 Not(WhenDynamicCastTo<Derived*>(
Pointee(FieldIIs(5)))));
3903 TEST(WhenDynamicCastToTest, WrongTypes) {
3906 OtherDerived other_derived;
3911 Base* as_base_ptr = &derived;
3914 as_base_ptr = &other_derived;
3919 TEST(WhenDynamicCastToTest, AlreadyNull) {
3921 Base* as_base_ptr =
nullptr;
3925 struct AmbiguousCastTypes {
3926 class VirtualDerived :
public virtual Base {};
3927 class DerivedSub1 :
public VirtualDerived {};
3928 class DerivedSub2 :
public VirtualDerived {};
3929 class ManyDerivedInHierarchy :
public DerivedSub1,
public DerivedSub2 {};
3932 TEST(WhenDynamicCastToTest, AmbiguousCast) {
3933 AmbiguousCastTypes::DerivedSub1 sub1;
3934 AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
3937 static_cast<AmbiguousCastTypes::DerivedSub1*
>(&many_derived);
3939 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(
IsNull()));
3940 as_base_ptr = &sub1;
3943 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(
Not(
IsNull())));
3946 TEST(WhenDynamicCastToTest, Describe) {
3947 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(
Pointee(
_));
3949 "when dynamic_cast to " + internal::GetTypeName<Derived*>() +
", ";
3950 EXPECT_EQ(
prefix +
"points to a value that is anything", Describe(matcher));
3952 DescribeNegation(matcher));
3955 TEST(WhenDynamicCastToTest, Explain) {
3956 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(
Pointee(
_));
3957 Base*
null =
nullptr;
3964 Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(
_);
3966 HasSubstr(
"which cannot be dynamic_cast"));
3969 TEST(WhenDynamicCastToTest, GoodReference) {
3972 Base& as_base_ref = derived;
3973 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
3974 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(
Not(FieldIIs(5))));
3977 TEST(WhenDynamicCastToTest, BadReference) {
3979 Base& as_base_ref = derived;
3980 EXPECT_THAT(as_base_ref,
Not(WhenDynamicCastTo<const OtherDerived&>(
_)));
3982 #endif // GTEST_HAS_RTTI
3985 template <
typename T>
3986 class ConstPropagatingPtr {
3988 typedef T element_type;
3990 ConstPropagatingPtr() :
val_() {}
3991 explicit ConstPropagatingPtr(
T* t) :
val_(
t) {}
3992 ConstPropagatingPtr(
const ConstPropagatingPtr& other) :
val_(other.
val_) {}
3997 const T*
get()
const {
return val_; }
4004 TEST(PointeeTest, WorksWithConstPropagatingPointers) {
4005 const Matcher< ConstPropagatingPtr<int> >
m =
Pointee(
Lt(5));
4007 const ConstPropagatingPtr<int> co(&three);
4008 ConstPropagatingPtr<int>
o(&three);
4016 TEST(PointeeTest, NeverMatchesNull) {
4017 const Matcher<const char*>
m =
Pointee(
_);
4022 TEST(PointeeTest, MatchesAgainstAValue) {
4023 const Matcher<int*>
m =
Pointee(5);
4032 TEST(PointeeTest, CanDescribeSelf) {
4034 EXPECT_EQ(
"points to a value that is > 3", Describe(
m));
4035 EXPECT_EQ(
"does not point to a value that is > 3",
4036 DescribeNegation(
m));
4039 TEST(PointeeTest, CanExplainMatchResult) {
4044 const Matcher<long*> m2 =
Pointee(GreaterThan(1));
4046 EXPECT_EQ(
"which points to 3" + OfType(
"long") +
", which is 2 more than 1",
4050 TEST(PointeeTest, AlwaysExplainsPointee) {
4051 const Matcher<int*>
m =
Pointee(0);
4053 EXPECT_EQ(
"which points to 42" + OfType(
"int"), Explain(
m, &n));
4059 Uncopyable() :
value_(-1) {}
4060 explicit Uncopyable(
int a_value) :
value_(a_value) {}
4063 void set_value(
int i) {
value_ =
i; }
4071 bool ValueIsPositive(
const Uncopyable& x) {
return x.value() > 0; }
4073 MATCHER_P(UncopyableIs, inner_matcher,
"") {
4079 AStruct() :
x(0),
y(1.0),
z(5),
p(nullptr) {}
4080 AStruct(
const AStruct& rhs)
4090 struct DerivedStruct :
public AStruct {
4095 TEST(FieldTest, WorksForNonConstField) {
4108 TEST(FieldTest, WorksForConstField) {
4122 TEST(FieldTest, WorksForUncopyableField) {
4132 TEST(FieldTest, WorksForPointerField) {
4134 Matcher<AStruct>
m =
Field(&
AStruct::p,
static_cast<const char*
>(
nullptr));
4149 TEST(FieldTest, WorksForByRefArgument) {
4160 TEST(FieldTest, WorksForArgumentOfSubType) {
4173 TEST(FieldTest, WorksForCompatibleMatcherType) {
4176 Matcher<signed char>(
Ge(0)));
4185 TEST(FieldTest, CanDescribeSelf) {
4188 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(
m));
4189 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(
m));
4192 TEST(FieldTest, CanDescribeSelfWithFieldName) {
4195 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(
m));
4196 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
4197 DescribeNegation(
m));
4201 TEST(FieldTest, CanExplainMatchResult) {
4206 EXPECT_EQ(
"whose given field is 1" + OfType(
"int"), Explain(
m, a));
4210 "whose given field is 1" + OfType(
"int") +
", which is 1 more than 0",
4214 TEST(FieldTest, CanExplainMatchResultWithFieldName) {
4219 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int"), Explain(
m, a));
4222 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int") +
4223 ", which is 1 more than 0",
4228 TEST(FieldForPointerTest, WorksForPointerToConst) {
4238 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
4248 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
4258 TEST(FieldForPointerTest, DoesNotMatchNull) {
4265 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
4277 TEST(FieldForPointerTest, CanDescribeSelf) {
4280 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(
m));
4281 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(
m));
4284 TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
4287 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(
m));
4288 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
4289 DescribeNegation(
m));
4293 TEST(FieldForPointerTest, CanExplainMatchResult) {
4298 EXPECT_EQ(
"", Explain(
m,
static_cast<const AStruct*
>(
nullptr)));
4299 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int"),
4303 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int") +
4304 ", which is 1 more than 0", Explain(
m, &a));
4307 TEST(FieldForPointerTest, CanExplainMatchResultWithFieldName) {
4312 EXPECT_EQ(
"", Explain(
m,
static_cast<const AStruct*
>(
nullptr)));
4314 "which points to an object whose field `field_name` is 1" + OfType(
"int"),
4318 EXPECT_EQ(
"which points to an object whose field `field_name` is 1" +
4319 OfType(
"int") +
", which is 1 more than 0",
4329 int n()
const {
return n_; }
4331 void set_n(
int new_n) {
n_ = new_n; }
4341 double&
x()
const {
return x_; }
4353 class DerivedClass :
public AClass {
4355 int k()
const {
return k_; }
4362 TEST(PropertyTest, WorksForNonReferenceProperty) {
4378 TEST(PropertyTest, WorksForReferenceToConstProperty) {
4380 Matcher<const AClass&> m_with_name =
4395 TEST(PropertyTest, WorksForRefQualifiedProperty) {
4397 Matcher<const AClass&> m_with_name =
4412 TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
4425 TEST(PropertyTest, WorksForByValueArgument) {
4438 TEST(PropertyTest, WorksForArgumentOfSubType) {
4453 TEST(PropertyTest, WorksForCompatibleMatcherType) {
4456 Matcher<signed char>(
Ge(0)));
4458 Matcher<const AClass&> m_with_name =
4470 TEST(PropertyTest, CanDescribeSelf) {
4473 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(
m));
4474 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4475 DescribeNegation(
m));
4478 TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
4481 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(
m));
4482 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4483 DescribeNegation(
m));
4487 TEST(PropertyTest, CanExplainMatchResult) {
4492 EXPECT_EQ(
"whose given property is 1" + OfType(
"int"), Explain(
m, a));
4496 "whose given property is 1" + OfType(
"int") +
", which is 1 more than 0",
4500 TEST(PropertyTest, CanExplainMatchResultWithPropertyName) {
4505 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int"), Explain(
m, a));
4508 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int") +
4509 ", which is 1 more than 0",
4514 TEST(PropertyForPointerTest, WorksForPointerToConst) {
4526 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
4539 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
4551 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
4558 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
4572 TEST(PropertyForPointerTest, CanDescribeSelf) {
4575 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(
m));
4576 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4577 DescribeNegation(
m));
4580 TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
4583 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(
m));
4584 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4585 DescribeNegation(
m));
4589 TEST(PropertyForPointerTest, CanExplainMatchResult) {
4594 EXPECT_EQ(
"", Explain(
m,
static_cast<const AClass*
>(
nullptr)));
4596 "which points to an object whose given property is 1" + OfType(
"int"),
4600 EXPECT_EQ(
"which points to an object whose given property is 1" +
4601 OfType(
"int") +
", which is 1 more than 0",
4605 TEST(PropertyForPointerTest, CanExplainMatchResultWithPropertyName) {
4610 EXPECT_EQ(
"", Explain(
m,
static_cast<const AClass*
>(
nullptr)));
4611 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4616 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4617 OfType(
"int") +
", which is 1 more than 0",
4626 return input == 1 ?
"foo" :
"bar";
4629 TEST(ResultOfTest, WorksForFunctionPointers) {
4637 TEST(ResultOfTest, CanDescribeItself) {
4638 Matcher<int> matcher =
ResultOf(&IntToStringFunction,
StrEq(
"foo"));
4640 EXPECT_EQ(
"is mapped by the given callable to a value that "
4641 "is equal to \"foo\"", Describe(matcher));
4642 EXPECT_EQ(
"is mapped by the given callable to a value that "
4643 "isn't equal to \"foo\"", DescribeNegation(matcher));
4647 TEST(ResultOfTest, CanDescribeItselfWithResultDescription) {
4648 Matcher<int> matcher =
4649 ResultOf(
"string conversion", &IntToStringFunction,
StrEq(
"foo"));
4651 EXPECT_EQ(
"whose string conversion is equal to \"foo\"", Describe(matcher));
4652 EXPECT_EQ(
"whose string conversion isn't equal to \"foo\"",
4653 DescribeNegation(matcher));
4657 int IntFunction(
int input) {
return input == 42 ? 80 : 90; }
4659 TEST(ResultOfTest, CanExplainMatchResult) {
4660 Matcher<int> matcher =
ResultOf(&IntFunction,
Ge(85));
4661 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int"),
4662 Explain(matcher, 36));
4664 matcher =
ResultOf(&IntFunction, GreaterThan(85));
4665 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int") +
4666 ", which is 5 more than 85", Explain(matcher, 36));
4671 TEST(ResultOfTest, WorksForNonReferenceResults) {
4672 Matcher<int> matcher =
ResultOf(&IntFunction,
Eq(80));
4680 double& DoubleFunction(
double&
input) {
return input; }
4682 Uncopyable& RefUncopyableFunction(Uncopyable&
obj) {
4686 TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
4689 Matcher<double&> matcher =
ResultOf(&DoubleFunction,
Ref(x));
4697 Matcher<Uncopyable&> matcher2 =
4708 TEST(ResultOfTest, WorksForReferenceToConstResults) {
4711 Matcher<const std::string&> matcher =
ResultOf(&StringFunction,
Ref(s));
4719 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
4721 Matcher<int> matcher =
ResultOf(IntFunction, Matcher<signed char>(
Ge(85)));
4729 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
4733 "NULL function pointer is passed into ResultOf\\(\\)\\.");
4738 TEST(ResultOfTest, WorksForFunctionReferences) {
4739 Matcher<int> matcher =
ResultOf(IntToStringFunction,
StrEq(
"foo"));
4748 return IntToStringFunction(
input);
4752 TEST(ResultOfTest, WorksForFunctors) {
4762 struct PolymorphicFunctor {
4764 int operator()(
int n) {
return n; }
4765 int operator()(
const char* s) {
return static_cast<int>(strlen(s)); }
4766 std::string operator()(
int *p) {
return p ?
"good ptr" :
"null"; }
4769 TEST(ResultOfTest, WorksForPolymorphicFunctors) {
4770 Matcher<int> matcher_int =
ResultOf(PolymorphicFunctor(),
Ge(5));
4775 Matcher<const char*> matcher_string =
ResultOf(PolymorphicFunctor(),
Ge(5));
4777 EXPECT_TRUE(matcher_string.Matches(
"long string"));
4781 TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
4782 Matcher<int*> matcher =
ResultOf(PolymorphicFunctor(),
"good ptr");
4789 TEST(ResultOfTest, WorksForLambdas) {
4792 return std::string(
static_cast<size_t>(str_len),
'x');
4799 TEST(ResultOfTest, WorksForNonCopyableArguments) {
4800 Matcher<std::unique_ptr<int>> matcher =
ResultOf(
4801 [](
const std::unique_ptr<int>& str_len) {
4802 return std::string(
static_cast<size_t>(*str_len),
'x');
4805 EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(
new int(3))));
4806 EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(
new int(1))));
4809 const int* ReferencingFunction(
const int& n) {
return &
n; }
4811 struct ReferencingFunctor {
4816 TEST(ResultOfTest, WorksForReferencingCallables) {
4819 Matcher<const int&> matcher2 =
ResultOf(ReferencingFunction,
Eq(&n));
4823 Matcher<const int&> matcher3 =
ResultOf(ReferencingFunctor(),
Eq(&n));
4828 class DivisibleByImpl {
4830 explicit DivisibleByImpl(
int a_divider) :
divider_(a_divider) {}
4833 template <
typename T>
4834 bool MatchAndExplain(
const T& n, MatchResultListener* listener)
const {
4835 *listener <<
"which is " << (
n %
divider_) <<
" modulo "
4840 void DescribeTo(ostream* os)
const {
4841 *os <<
"is divisible by " <<
divider_;
4844 void DescribeNegationTo(ostream* os)
const {
4845 *os <<
"is not divisible by " <<
divider_;
4848 void set_divider(
int a_divider) {
divider_ = a_divider; }
4849 int divider()
const {
return divider_; }
4855 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(
int n) {
4861 TEST(ExplainMatchResultTest, AllOf_False_False) {
4862 const Matcher<int>
m =
AllOf(DivisibleBy(4), DivisibleBy(3));
4863 EXPECT_EQ(
"which is 1 modulo 4", Explain(
m, 5));
4868 TEST(ExplainMatchResultTest, AllOf_False_True) {
4869 const Matcher<int>
m =
AllOf(DivisibleBy(4), DivisibleBy(3));
4870 EXPECT_EQ(
"which is 2 modulo 4", Explain(
m, 6));
4875 TEST(ExplainMatchResultTest, AllOf_True_False) {
4876 const Matcher<int>
m =
AllOf(
Ge(1), DivisibleBy(3));
4877 EXPECT_EQ(
"which is 2 modulo 3", Explain(
m, 5));
4882 TEST(ExplainMatchResultTest, AllOf_True_True) {
4883 const Matcher<int>
m =
AllOf(DivisibleBy(2), DivisibleBy(3));
4884 EXPECT_EQ(
"which is 0 modulo 2, and which is 0 modulo 3", Explain(
m, 6));
4887 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
4892 TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
4893 const Matcher<int>
m = GreaterThan(5);
4894 EXPECT_EQ(
"which is 1 more than 5", Explain(
m, 6));
4903 explicit NotCopyable(
int a_value) :
value_(a_value) {}
4907 bool operator==(
const NotCopyable& rhs)
const {
4908 return value() == rhs.value();
4911 bool operator>=(
const NotCopyable& rhs)
const {
4912 return value() >= rhs.value();
4920 TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
4921 const NotCopyable const_value1(1);
4922 const Matcher<const NotCopyable&>
m =
Eq(
ByRef(const_value1));
4924 const NotCopyable n1(1), n2(2);
4929 TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
4930 NotCopyable value2(2);
4931 const Matcher<NotCopyable&>
m =
Ge(
ByRef(value2));
4933 NotCopyable n1(1), n2(2);
4938 TEST(IsEmptyTest, ImplementsIsEmpty) {
4947 TEST(IsEmptyTest, WorksWithString) {
4956 TEST(IsEmptyTest, CanDescribeSelf) {
4957 Matcher<vector<int> >
m =
IsEmpty();
4959 EXPECT_EQ(
"isn't empty", DescribeNegation(
m));
4962 TEST(IsEmptyTest, ExplainsResult) {
4963 Matcher<vector<int> >
m =
IsEmpty();
4970 TEST(IsEmptyTest, WorksWithMoveOnly) {
4971 ContainerHelper helper;
4976 TEST(IsTrueTest, IsTrueIsFalse) {
5004 std::unique_ptr<int> null_unique;
5005 std::unique_ptr<int> nonnull_unique(
new int(0));
5012 TEST(SizeIsTest, ImplementsSizeIs) {
5024 TEST(SizeIsTest, WorksWithMap) {
5036 TEST(SizeIsTest, WorksWithReferences) {
5038 Matcher<const vector<int>&>
m =
SizeIs(1);
5044 TEST(SizeIsTest, WorksWithMoveOnly) {
5045 ContainerHelper helper;
5047 helper.Call(MakeUniquePtrs({1, 2, 3}));
5052 struct MinimalistCustomType {
5053 int size()
const {
return 1; }
5055 TEST(SizeIsTest, WorksWithMinimalistCustomType) {
5061 TEST(SizeIsTest, CanDescribeSelf) {
5062 Matcher<vector<int> >
m =
SizeIs(2);
5063 EXPECT_EQ(
"size is equal to 2", Describe(
m));
5064 EXPECT_EQ(
"size isn't equal to 2", DescribeNegation(
m));
5067 TEST(SizeIsTest, ExplainsResult) {
5068 Matcher<vector<int> > m1 =
SizeIs(2);
5069 Matcher<vector<int> > m2 =
SizeIs(
Lt(2u));
5071 Matcher<vector<int> > m4 =
SizeIs(
Gt(1u));
5085 #if GTEST_HAS_TYPED_TEST
5089 template <
typename T>
5097 ContainerEqTestTypes;
5103 static const int vals[] = {1, 1, 2, 3, 5, 8};
5112 static const int vals[] = {1, 1, 2, 3, 5, 8};
5113 static const int test_vals[] = {2, 1, 8, 5};
5115 TypeParam test_set(test_vals, test_vals + 4);
5118 EXPECT_EQ(
"which doesn't have these expected elements: 3",
5119 Explain(
m, test_set));
5124 static const int vals[] = {1, 1, 2, 3, 5, 8};
5125 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
5127 TypeParam test_set(test_vals, test_vals + 6);
5128 const Matcher<const TypeParam&>
m =
ContainerEq(my_set);
5130 EXPECT_EQ(
"which has these unexpected elements: 46", Explain(
m, test_set));
5134 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
5135 static const int vals[] = {1, 1, 2, 3, 5, 8};
5136 static const int test_vals[] = {1, 2, 3, 8, 46};
5138 TypeParam test_set(test_vals, test_vals + 5);
5141 EXPECT_EQ(
"which has these unexpected elements: 46,\n"
5142 "and doesn't have these expected elements: 5",
5143 Explain(
m, test_set));
5147 TYPED_TEST(ContainerEqTest, DuplicateDifference) {
5148 static const int vals[] = {1, 1, 2, 3, 5, 8};
5149 static const int test_vals[] = {1, 2, 3, 5, 8};
5151 TypeParam test_set(test_vals, test_vals + 5);
5152 const Matcher<const TypeParam&>
m =
ContainerEq(my_set);
5157 #endif // GTEST_HAS_TYPED_TEST
5161 TEST(ContainerEqExtraTest, MultipleValuesMissing) {
5162 static const int vals[] = {1, 1, 2, 3, 5, 8};
5163 static const int test_vals[] = {2, 1, 5};
5164 vector<int> my_set(
vals,
vals + 6);
5165 vector<int> test_set(test_vals, test_vals + 3);
5168 EXPECT_EQ(
"which doesn't have these expected elements: 3, 8",
5169 Explain(
m, test_set));
5174 TEST(ContainerEqExtraTest, MultipleValuesAdded) {
5175 static const int vals[] = {1, 1, 2, 3, 5, 8};
5176 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
5177 list<size_t> my_set(
vals,
vals + 6);
5178 list<size_t> test_set(test_vals, test_vals + 7);
5179 const Matcher<const list<size_t>&>
m =
ContainerEq(my_set);
5181 EXPECT_EQ(
"which has these unexpected elements: 92, 46",
5182 Explain(
m, test_set));
5186 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
5187 static const int vals[] = {1, 1, 2, 3, 5, 8};
5188 static const int test_vals[] = {1, 2, 3, 92, 46};
5189 list<size_t> my_set(
vals,
vals + 6);
5190 list<size_t> test_set(test_vals, test_vals + 5);
5191 const Matcher<const list<size_t> >
m =
ContainerEq(my_set);
5193 EXPECT_EQ(
"which has these unexpected elements: 92, 46,\n"
5194 "and doesn't have these expected elements: 5, 8",
5195 Explain(
m, test_set));
5200 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
5201 static const int vals[] = {1, 1, 2, 3, 5, 8};
5202 static const int test_vals[] = {1, 2, 3, 5, 8};
5203 vector<int> my_set(
vals,
vals + 6);
5204 vector<int> test_set(test_vals, test_vals + 5);
5214 TEST(ContainerEqExtraTest, WorksForMaps) {
5215 map<int, std::string> my_map;
5219 map<int, std::string> test_map;
5223 const Matcher<const map<int, std::string>&>
m =
ContainerEq(my_map);
5227 EXPECT_EQ(
"which has these unexpected elements: (0, \"aa\"),\n"
5228 "and doesn't have these expected elements: (0, \"a\")",
5229 Explain(
m, test_map));
5232 TEST(ContainerEqExtraTest, WorksForNativeArray) {
5233 int a1[] = {1, 2, 3};
5234 int a2[] = {1, 2, 3};
5235 int b[] = {1, 2, 4};
5241 TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
5242 const char a1[][3] = {
"hi",
"lo"};
5243 const char a2[][3] = {
"hi",
"lo"};
5244 const char b[][3] = {
"lo",
"hi"};
5255 TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
5256 const int a1[] = {1, 2, 3};
5257 const int a2[] = {1, 2, 3};
5258 const int b[] = {1, 2, 3, 4};
5260 const int*
const p1 =
a1;
5264 const int c[] = {1, 3, 2};
5268 TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
5270 {
"hi",
"hello",
"ciao"},
5271 {
"bye",
"see you",
"ciao"}
5275 {
"hi",
"hello",
"ciao"},
5276 {
"bye",
"see you",
"ciao"}
5286 TEST(WhenSortedByTest, WorksForEmptyContainer) {
5287 const vector<int> numbers;
5292 TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
5293 vector<unsigned> numbers;
5294 numbers.push_back(3);
5295 numbers.push_back(1);
5296 numbers.push_back(2);
5297 numbers.push_back(2);
5304 TEST(WhenSortedByTest, WorksForNonVectorContainer) {
5305 list<std::string>
words;
5306 words.push_back(
"say");
5307 words.push_back(
"hello");
5308 words.push_back(
"world");
5315 TEST(WhenSortedByTest, WorksForNativeArray) {
5316 const int numbers[] = {1, 3, 2, 4};
5317 const int sorted_numbers[] = {1, 2, 3, 4};
5324 TEST(WhenSortedByTest, CanDescribeSelf) {
5326 EXPECT_EQ(
"(when sorted) has 2 elements where\n"
5327 "element #0 is equal to 1,\n"
5328 "element #1 is equal to 2",
5330 EXPECT_EQ(
"(when sorted) doesn't have 2 elements, or\n"
5331 "element #0 isn't equal to 1, or\n"
5332 "element #1 isn't equal to 2",
5333 DescribeNegation(
m));
5336 TEST(WhenSortedByTest, ExplainsMatchResult) {
5337 const int a[] = {2, 1};
5338 EXPECT_EQ(
"which is { 1, 2 } when sorted, whose element #0 doesn't match",
5340 EXPECT_EQ(
"which is { 1, 2 } when sorted",
5347 TEST(WhenSortedTest, WorksForEmptyContainer) {
5348 const vector<int> numbers;
5353 TEST(WhenSortedTest, WorksForNonEmptyContainer) {
5354 list<std::string>
words;
5355 words.push_back(
"3");
5356 words.push_back(
"1");
5357 words.push_back(
"2");
5358 words.push_back(
"2");
5363 TEST(WhenSortedTest, WorksForMapTypes) {
5364 map<std::string, int> word_counts;
5365 word_counts[
"and"] = 1;
5366 word_counts[
"the"] = 1;
5367 word_counts[
"buffalo"] = 2;
5373 Pair(
"buffalo", 2)))));
5376 TEST(WhenSortedTest, WorksForMultiMapTypes) {
5377 multimap<int, int> ifib;
5378 ifib.insert(make_pair(8, 6));
5379 ifib.insert(make_pair(2, 3));
5380 ifib.insert(make_pair(1, 1));
5381 ifib.insert(make_pair(3, 4));
5382 ifib.insert(make_pair(1, 2));
5383 ifib.insert(make_pair(5, 5));
5398 TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
5406 TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
5410 Matcher<const std::vector<int>&> vector_match =
ElementsAre(1, 2);
5412 Matcher<const std::vector<int>&> not_vector_match =
ElementsAre(2, 1);
5418 template <
typename T>
5423 typedef ConstIter const_iterator;
5426 template <
typename InIter>
5429 const_iterator
begin()
const {
5430 return const_iterator(
this,
remainder_.begin());
5432 const_iterator
end()
const {
5433 return const_iterator(
this,
remainder_.end());
5439 using iterator_category = std::input_iterator_tag;
5441 using difference_type = ptrdiff_t;
5445 ConstIter(
const Streamlike* s,
5452 s_->remainder_.erase(
pos_++);
5458 class PostIncrProxy {
5466 PostIncrProxy proxy(**
this);
5471 friend bool operator==(
const ConstIter& a,
const ConstIter&
b) {
5472 return a.s_ ==
b.s_ &&
a.pos_ ==
b.pos_;
5474 friend bool operator!=(
const ConstIter& a,
const ConstIter&
b) {
5479 const Streamlike*
s_;
5483 friend std::ostream&
operator<<(std::ostream& os,
const Streamlike& s) {
5485 typedef typename std::list<value_type>::const_iterator
Iter;
5486 const char*
sep =
"";
5487 for (
Iter it =
s.remainder_.begin();
it !=
s.remainder_.end(); ++
it) {
5498 TEST(StreamlikeTest, Iteration) {
5499 const int a[5] = {2, 1, 4, 5, 3};
5500 Streamlike<int>
s(a, a + 5);
5501 Streamlike<int>::const_iterator
it =
s.begin();
5503 while (
it !=
s.end()) {
5509 TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
5521 TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
5522 const int a[5] = {1, 2, 3, 4, 5};
5523 Streamlike<int>
s(a, a + 5);
5527 TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
5529 EXPECT_EQ(
"distance between begin() and end() is equal to 2", Describe(
m));
5530 EXPECT_EQ(
"distance between begin() and end() isn't equal to 2",
5531 DescribeNegation(
m));
5534 TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
5535 ContainerHelper helper;
5537 helper.Call(MakeUniquePtrs({1, 2}));
5540 TEST(BeginEndDistanceIsTest, ExplainsResult) {
5546 EXPECT_EQ(
"whose distance between begin() and end() 0 doesn't match",
5548 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5550 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5553 "whose distance between begin() and end() 0 doesn't match, which is 1 "
5558 EXPECT_EQ(
"whose distance between begin() and end() 2 matches",
5560 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5562 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5565 "whose distance between begin() and end() 2 matches, which is 1 more "
5570 TEST(WhenSortedTest, WorksForStreamlike) {
5573 const int a[5] = {2, 1, 4, 5, 3};
5579 TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
5580 const int a[] = {2, 1, 4, 5, 3};
5582 Matcher<const std::vector<int>&> vector_match =
ElementsAre(1, 2, 3, 4, 5);
5587 TEST(IsSupersetOfTest, WorksForNativeArray) {
5588 const int subset[] = {1, 4};
5589 const int superset[] = {1, 2, 4};
5590 const int disjoint[] = {1, 0, 3};
5598 TEST(IsSupersetOfTest, WorksWithDuplicates) {
5599 const int not_enough[] = {1, 2};
5600 const int enough[] = {1, 1, 2};
5601 const int expected[] = {1, 1};
5606 TEST(IsSupersetOfTest, WorksForEmpty) {
5607 vector<int> numbers;
5608 vector<int> expected;
5610 expected.push_back(1);
5613 numbers.push_back(1);
5614 numbers.push_back(2);
5616 expected.push_back(1);
5618 expected.push_back(2);
5620 expected.push_back(3);
5624 TEST(IsSupersetOfTest, WorksForStreamlike) {
5625 const int a[5] = {1, 2, 3, 4, 5};
5628 vector<int> expected;
5629 expected.push_back(1);
5630 expected.push_back(2);
5631 expected.push_back(5);
5634 expected.push_back(0);
5638 TEST(IsSupersetOfTest, TakesStlContainer) {
5639 const int actual[] = {3, 1, 2};
5641 ::std::list<int> expected;
5642 expected.push_back(1);
5643 expected.push_back(3);
5646 expected.push_back(4);
5650 TEST(IsSupersetOfTest, Describe) {
5651 typedef std::vector<int> IntVec;
5653 expected.push_back(111);
5654 expected.push_back(222);
5655 expected.push_back(333);
5657 Describe<IntVec>(IsSupersetOf(expected)),
5658 Eq(
"a surjection from elements to requirements exists such that:\n"
5659 " - an element is equal to 111\n"
5660 " - an element is equal to 222\n"
5661 " - an element is equal to 333"));
5664 TEST(IsSupersetOfTest, DescribeNegation) {
5665 typedef std::vector<int> IntVec;
5667 expected.push_back(111);
5668 expected.push_back(222);
5669 expected.push_back(333);
5671 DescribeNegation<IntVec>(IsSupersetOf(expected)),
5672 Eq(
"no surjection from elements to requirements exists such that:\n"
5673 " - an element is equal to 111\n"
5674 " - an element is equal to 222\n"
5675 " - an element is equal to 333"));
5678 TEST(IsSupersetOfTest, MatchAndExplain) {
5682 std::vector<int> expected;
5683 expected.push_back(1);
5684 expected.push_back(2);
5685 StringMatchResultListener listener;
5689 Eq(
"where the following matchers don't match any elements:\n"
5690 "matcher #0: is equal to 1"));
5697 " - element #0 is matched by matcher #1,\n"
5698 " - element #2 is matched by matcher #0"));
5701 TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
5702 const int numbers[] = {1, 3, 6, 2, 4, 5};
5707 TEST(IsSupersetOfTest, WorksWithMoveOnly) {
5708 ContainerHelper helper;
5710 helper.Call(MakeUniquePtrs({1, 2}));
5712 helper.Call(MakeUniquePtrs({2}));
5715 TEST(IsSubsetOfTest, WorksForNativeArray) {
5716 const int subset[] = {1, 4};
5717 const int superset[] = {1, 2, 4};
5718 const int disjoint[] = {1, 0, 3};
5726 TEST(IsSubsetOfTest, WorksWithDuplicates) {
5727 const int not_enough[] = {1, 2};
5728 const int enough[] = {1, 1, 2};
5729 const int actual[] = {1, 1};
5734 TEST(IsSubsetOfTest, WorksForEmpty) {
5735 vector<int> numbers;
5736 vector<int> expected;
5738 expected.push_back(1);
5741 numbers.push_back(1);
5742 numbers.push_back(2);
5744 expected.push_back(1);
5746 expected.push_back(2);
5748 expected.push_back(3);
5752 TEST(IsSubsetOfTest, WorksForStreamlike) {
5753 const int a[5] = {1, 2};
5756 vector<int> expected;
5757 expected.push_back(1);
5759 expected.push_back(2);
5760 expected.push_back(5);
5764 TEST(IsSubsetOfTest, TakesStlContainer) {
5765 const int actual[] = {3, 1, 2};
5767 ::std::list<int> expected;
5768 expected.push_back(1);
5769 expected.push_back(3);
5772 expected.push_back(2);
5773 expected.push_back(4);
5777 TEST(IsSubsetOfTest, Describe) {
5778 typedef std::vector<int> IntVec;
5780 expected.push_back(111);
5781 expected.push_back(222);
5782 expected.push_back(333);
5785 Describe<IntVec>(IsSubsetOf(expected)),
5786 Eq(
"an injection from elements to requirements exists such that:\n"
5787 " - an element is equal to 111\n"
5788 " - an element is equal to 222\n"
5789 " - an element is equal to 333"));
5792 TEST(IsSubsetOfTest, DescribeNegation) {
5793 typedef std::vector<int> IntVec;
5795 expected.push_back(111);
5796 expected.push_back(222);
5797 expected.push_back(333);
5799 DescribeNegation<IntVec>(IsSubsetOf(expected)),
5800 Eq(
"no injection from elements to requirements exists such that:\n"
5801 " - an element is equal to 111\n"
5802 " - an element is equal to 222\n"
5803 " - an element is equal to 333"));
5806 TEST(IsSubsetOfTest, MatchAndExplain) {
5810 std::vector<int> expected;
5811 expected.push_back(1);
5812 expected.push_back(2);
5813 StringMatchResultListener listener;
5817 Eq(
"where the following elements don't match any matchers:\n"
5820 expected.push_back(3);
5825 " - element #0 is matched by matcher #1,\n"
5826 " - element #1 is matched by matcher #2"));
5829 TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
5830 const int numbers[] = {1, 2, 3};
5835 TEST(IsSubsetOfTest, WorksWithMoveOnly) {
5836 ContainerHelper helper;
5838 helper.Call(MakeUniquePtrs({1}));
5840 helper.Call(MakeUniquePtrs({2}));
5846 TEST(ElemensAreStreamTest, WorksForStreamlike) {
5847 const int a[5] = {1, 2, 3, 4, 5};
5853 TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
5854 const int a[5] = {1, 2, 3, 4, 5};
5857 vector<int> expected;
5858 expected.push_back(1);
5859 expected.push_back(2);
5860 expected.push_back(3);
5861 expected.push_back(4);
5862 expected.push_back(5);
5869 TEST(ElementsAreTest, WorksWithUncopyable) {
5871 objs[0].set_value(-3);
5872 objs[1].set_value(1);
5876 TEST(ElementsAreTest, WorksWithMoveOnly) {
5877 ContainerHelper helper;
5879 helper.Call(MakeUniquePtrs({1, 2}));
5882 helper.Call(MakeUniquePtrs({3, 4}));
5885 TEST(ElementsAreTest, TakesStlContainer) {
5886 const int actual[] = {3, 1, 2};
5888 ::std::list<int> expected;
5889 expected.push_back(3);
5890 expected.push_back(1);
5891 expected.push_back(2);
5894 expected.push_back(4);
5900 TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
5901 const int a[] = {0, 1, 2, 3, 4};
5904 StringMatchResultListener listener;
5906 s, &listener)) << listener.str();
5907 }
while (std::next_permutation(
s.begin(),
s.end()));
5910 TEST(UnorderedElementsAreArrayTest, VectorBool) {
5911 const bool a[] = {0, 1, 0, 1, 1};
5912 const bool b[] = {1, 0, 1, 1, 0};
5915 StringMatchResultListener listener;
5917 actual, &listener)) << listener.str();
5920 TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
5924 const int a[5] = {2, 1, 4, 5, 3};
5927 ::std::vector<int> expected;
5928 expected.push_back(1);
5929 expected.push_back(2);
5930 expected.push_back(3);
5931 expected.push_back(4);
5932 expected.push_back(5);
5935 expected.push_back(6);
5939 TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
5940 const int actual[] = {3, 1, 2};
5942 ::std::list<int> expected;
5943 expected.push_back(1);
5944 expected.push_back(2);
5945 expected.push_back(3);
5948 expected.push_back(4);
5953 TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
5954 const int a[5] = {2, 1, 4, 5, 3};
5959 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
5965 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
5966 const int a[5] = {2, 1, 4, 5, 3};
5973 TEST(UnorderedElementsAreArrayTest,
5974 TakesInitializerListOfDifferentTypedMatchers) {
5975 const int a[5] = {2, 1, 4, 5, 3};
5986 TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
5987 ContainerHelper helper;
5990 helper.Call(MakeUniquePtrs({2, 1}));
5995 typedef std::vector<int> IntVec;
5998 TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
6000 objs[0].set_value(-3);
6001 objs[1].set_value(1);
6006 TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
6007 const int a[] = {1, 2, 3};
6010 StringMatchResultListener listener;
6012 s, &listener)) << listener.str();
6013 }
while (std::next_permutation(
s.begin(),
s.end()));
6016 TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
6017 const int a[] = {1, 2, 3};
6019 std::vector<Matcher<int> > mv;
6024 StringMatchResultListener listener;
6026 s, &listener)) << listener.str();
6029 TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
6033 const int a[5] = {2, 1, 4, 5, 3};
6040 TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
6041 ContainerHelper helper;
6043 helper.Call(MakeUniquePtrs({2, 1}));
6052 TEST_F(UnorderedElementsAreTest, Performance) {
6054 std::vector<Matcher<int> > mv;
6055 for (
int i = 0;
i < 100; ++
i) {
6060 StringMatchResultListener listener;
6062 s, &listener)) << listener.str();
6068 TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
6070 std::vector<Matcher<int> > mv;
6071 for (
int i = 0;
i < 100; ++
i) {
6079 StringMatchResultListener listener;
6081 s, &listener)) << listener.str();
6084 TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
6087 StringMatchResultListener listener;
6089 v, &listener)) << listener.str();
6093 TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
6095 StringMatchResultListener listener;
6097 v, &listener)) << listener.str();
6101 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
6105 StringMatchResultListener listener;
6107 v, &listener)) << listener.str();
6110 Eq(
"where the following matchers don't match any elements:\n"
6111 "matcher #1: is equal to 2"));
6114 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
6118 StringMatchResultListener listener;
6120 v, &listener)) << listener.str();
6123 Eq(
"where the following elements don't match any matchers:\n"
6127 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
6131 StringMatchResultListener listener;
6133 v, &listener)) << listener.str();
6137 " the following matchers don't match any elements:\n"
6138 "matcher #0: is equal to 1\n"
6141 " the following elements don't match any matchers:\n"
6148 ss <<
"(element #" <<
element <<
", matcher #" << matcher <<
")";
6152 TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
6155 std::vector<std::string>
v;
6159 StringMatchResultListener listener;
6165 "where no permutation of the elements can satisfy all matchers, "
6166 "and the closest match is 2 of 3 matchers with the "
6173 ",\n " + EMString(1, 2) +
"\n}",
6174 prefix +
"{\n " + EMString(0, 1) +
6175 ",\n " + EMString(1, 2) +
"\n}",
6176 prefix +
"{\n " + EMString(0, 0) +
6177 ",\n " + EMString(2, 2) +
"\n}",
6178 prefix +
"{\n " + EMString(0, 1) +
6179 ",\n " + EMString(2, 2) +
"\n}"));
6182 TEST_F(UnorderedElementsAreTest, Describe) {
6187 Eq(
"has 1 element and that element is equal to 345"));
6190 Eq(
"has 3 elements and there exists some permutation "
6191 "of elements such that:\n"
6192 " - element #0 is equal to 111, and\n"
6193 " - element #1 is equal to 222, and\n"
6194 " - element #2 is equal to 333"));
6197 TEST_F(UnorderedElementsAreTest, DescribeNegation) {
6202 Eq(
"doesn't have 1 element, or has 1 element that isn't equal to 345"));
6205 Eq(
"doesn't have 3 elements, or there exists no permutation "
6206 "of elements such that:\n"
6207 " - element #0 is equal to 123, and\n"
6208 " - element #1 is equal to 234, and\n"
6209 " - element #2 is equal to 345"));
6218 template <
typename Graph>
6219 class BacktrackingMaxBPMState {
6222 explicit BacktrackingMaxBPMState(
const Graph* g) :
graph_(
g) { }
6225 if (
graph_->LhsSize() == 0 ||
graph_->RhsSize() == 0) {
6230 for (
size_t irhs = 0; irhs <
graph_->RhsSize(); ++irhs) {
6240 static const size_t kUnused =
static_cast<size_t>(-1);
6242 void PushMatch(
size_t lhs,
size_t rhs) {
6258 bool RecurseInto(
size_t irhs) {
6262 for (
size_t ilhs = 0; ilhs <
graph_->LhsSize(); ++ilhs) {
6266 if (!
graph_->HasEdge(ilhs, irhs)) {
6269 PushMatch(ilhs, irhs);
6273 for (
size_t mi = irhs + 1; mi <
graph_->RhsSize(); ++mi) {
6274 if (!RecurseInto(mi))
return false;
6288 template <
typename Graph>
6295 template <
typename Graph>
6297 FindBacktrackingMaxBPM(
const Graph& g) {
6298 return BacktrackingMaxBPMState<Graph>(&
g).Compute();
6308 TEST_P(BipartiteTest, Exhaustive) {
6309 size_t nodes = GetParam();
6310 MatchMatrix graph(nodes, nodes);
6314 EXPECT_EQ(FindBacktrackingMaxBPM(graph).
size(), matches.size())
6315 <<
"graph: " << graph.DebugString();
6318 std::vector<bool> seen_element(graph.LhsSize());
6319 std::vector<bool> seen_matcher(graph.RhsSize());
6321 for (
size_t i = 0;
i < matches.size(); ++
i) {
6322 size_t ilhs = matches[
i].first;
6323 size_t irhs = matches[
i].second;
6327 seen_element[ilhs] =
true;
6328 seen_matcher[irhs] =
true;
6330 }
while (graph.NextGraph());
6337 class BipartiteNonSquareTest
6341 TEST_F(BipartiteNonSquareTest, SimpleBacktracking) {
6349 MatchMatrix
g(4, 3);
6350 constexpr std::array<std::array<size_t, 2>, 4> kEdges = {
6351 {{{0, 2}}, {{1, 1}}, {{2, 1}}, {{3, 0}}}};
6352 for (
size_t i = 0;
i < kEdges.size(); ++
i) {
6353 g.SetEdge(kEdges[i][0], kEdges[i][1],
true);
6358 Pair(0, 2))) <<
g.DebugString();
6362 TEST_P(BipartiteNonSquareTest, Exhaustive) {
6363 size_t nlhs = GetParam().first;
6364 size_t nrhs = GetParam().second;
6365 MatchMatrix graph(nlhs, nrhs);
6369 <<
"graph: " << graph.DebugString()
6370 <<
"\nbacktracking: "
6374 }
while (graph.NextGraph());
6379 std::make_pair(1, 2),
6380 std::make_pair(2, 1),
6381 std::make_pair(3, 2),
6382 std::make_pair(2, 3),
6383 std::make_pair(4, 1),
6384 std::make_pair(1, 4),
6385 std::make_pair(4, 3),
6386 std::make_pair(3, 4)));
6388 class BipartiteRandomTest
6393 TEST_P(BipartiteRandomTest, LargerNets) {
6394 int nodes = GetParam().first;
6395 int iters = GetParam().second;
6396 MatchMatrix graph(
static_cast<size_t>(nodes),
static_cast<size_t>(nodes));
6403 for (; iters > 0; --iters, ++
seed) {
6404 srand(
static_cast<unsigned int>(
seed));
6408 <<
" graph: " << graph.DebugString()
6409 <<
"\nTo reproduce the failure, rerun the test with the flag"
6417 std::make_pair(5, 10000),
6418 std::make_pair(6, 5000),
6419 std::make_pair(7, 2000),
6420 std::make_pair(8, 500),
6421 std::make_pair(9, 100)));
6425 TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
6432 TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
6438 TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
6444 TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
6450 TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
6460 "is in range (a: 5, b: 8)",
6465 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
6466 PolymorphicMatcher<DivisibleByImpl>
m(DivisibleByImpl(42));
6467 DivisibleByImpl& impl =
m.mutable_impl();
6470 impl.set_divider(0);
6475 TEST(PolymorphicMatcherTest, CanAccessImpl) {
6476 const PolymorphicMatcher<DivisibleByImpl>
m(DivisibleByImpl(42));
6477 const DivisibleByImpl& impl =
m.impl();
6481 TEST(MatcherTupleTest, ExplainsMatchFailure) {
6493 " Actual: 2, which is 3 less than 5\n"
6494 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
6495 " Actual: 'b' (98, 0x62)\n",
6503 " Actual: 2, which is 3 less than 5\n",
6510 TEST(EachTest, ExplainsMatchResultCorrectly) {
6513 Matcher<set<int> >
m =
Each(2);
6516 Matcher<
const int(&)[1]>
n =
Each(1);
6518 const int b[1] = {1};
6522 EXPECT_EQ(
"whose element #0 doesn't match", Explain(n,
b));
6527 m =
Each(GreaterThan(0));
6530 m =
Each(GreaterThan(10));
6531 EXPECT_EQ(
"whose element #0 doesn't match, which is 9 less than 10",
6535 TEST(EachTest, DescribesItselfCorrectly) {
6536 Matcher<vector<int> >
m =
Each(1);
6537 EXPECT_EQ(
"only contains elements that is equal to 1", Describe(
m));
6539 Matcher<vector<int> > m2 =
Not(
m);
6540 EXPECT_EQ(
"contains some element that isn't equal to 1", Describe(m2));
6543 TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
6544 vector<int> some_vector;
6546 some_vector.push_back(3);
6549 some_vector.push_back(1);
6550 some_vector.push_back(2);
6554 vector<std::string> another_vector;
6555 another_vector.push_back(
"fee");
6557 another_vector.push_back(
"fie");
6558 another_vector.push_back(
"foe");
6559 another_vector.push_back(
"fum");
6563 TEST(EachTest, MatchesMapWhenAllElementsMatch) {
6564 map<const char*, int> my_map;
6565 const char*
bar =
"a string";
6569 map<std::string, int> another_map;
6571 another_map[
"fee"] = 1;
6573 another_map[
"fie"] = 2;
6574 another_map[
"foe"] = 3;
6575 another_map[
"fum"] = 4;
6581 TEST(EachTest, AcceptsMatcher) {
6582 const int a[] = {1, 2, 3};
6587 TEST(EachTest, WorksForNativeArrayAsTuple) {
6588 const int a[] = {1, 2};
6589 const int*
const pointer =
a;
6594 TEST(EachTest, WorksWithMoveOnly) {
6595 ContainerHelper helper;
6597 helper.Call(MakeUniquePtrs({1, 2}));
6601 class IsHalfOfMatcher {
6603 template <
typename T1,
typename T2>
6604 bool MatchAndExplain(
const std::tuple<T1, T2>& a_pair,
6605 MatchResultListener* listener)
const {
6606 if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
6607 *listener <<
"where the second is " << std::get<1>(a_pair);
6610 *listener <<
"where the second/2 is " << std::get<1>(a_pair) / 2;
6615 void DescribeTo(ostream* os)
const {
6616 *os <<
"are a pair where the first is half of the second";
6619 void DescribeNegationTo(ostream* os)
const {
6620 *os <<
"are a pair where the first isn't half of the second";
6624 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
6628 TEST(PointwiseTest, DescribesSelf) {
6633 const Matcher<const vector<int>&>
m =
Pointwise(IsHalfOf(), rhs);
6634 EXPECT_EQ(
"contains 3 values, where each value and its corresponding value "
6635 "in { 1, 2, 3 } are a pair where the first is half of the second",
6637 EXPECT_EQ(
"doesn't contain exactly 3 values, or contains a value x at some "
6638 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
6639 "where the first isn't half of the second",
6640 DescribeNegation(
m));
6643 TEST(PointwiseTest, MakesCopyOfRhs) {
6644 list<signed char> rhs;
6649 const Matcher<
const int (&)[2]>
m =
Pointwise(IsHalfOf(), rhs);
6657 TEST(PointwiseTest, WorksForLhsNativeArray) {
6658 const int lhs[] = {1, 2, 3};
6667 TEST(PointwiseTest, WorksForRhsNativeArray) {
6668 const int rhs[] = {1, 2, 3};
6678 TEST(PointwiseTest, WorksForVectorOfBool) {
6679 vector<bool> rhs(3,
false);
6681 vector<bool> lhs = rhs;
6688 TEST(PointwiseTest, WorksForRhsInitializerList) {
6689 const vector<int> lhs{2, 4, 6};
6695 TEST(PointwiseTest, RejectsWrongSize) {
6696 const double lhs[2] = {1, 2};
6697 const int rhs[1] = {0};
6702 const int rhs2[3] = {0, 1, 2};
6706 TEST(PointwiseTest, RejectsWrongContent) {
6707 const double lhs[3] = {1, 2, 3};
6708 const int rhs[3] = {2, 6, 4};
6710 EXPECT_EQ(
"where the value pair (2, 6) at index #1 don't match, "
6711 "where the second/2 is 3",
6712 Explain(
Pointwise(IsHalfOf(), rhs), lhs));
6715 TEST(PointwiseTest, AcceptsCorrectContent) {
6716 const double lhs[3] = {1, 2, 3};
6717 const int rhs[3] = {2, 4, 6};
6722 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
6723 const double lhs[3] = {1, 2, 3};
6724 const int rhs[3] = {2, 4, 6};
6725 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6731 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6736 MATCHER(PointeeEquals,
"Points to an equal value") {
6738 ::testing::get<0>(
arg), result_listener);
6741 TEST(PointwiseTest, WorksWithMoveOnly) {
6742 ContainerHelper helper;
6744 helper.Call(MakeUniquePtrs({1, 2}));
6747 TEST(UnorderedPointwiseTest, DescribesSelf) {
6754 "has 3 elements and there exists some permutation of elements such "
6756 " - element #0 and 1 are a pair where the first is half of the second, "
6758 " - element #1 and 2 are a pair where the first is half of the second, "
6760 " - element #2 and 3 are a pair where the first is half of the second",
6763 "doesn't have 3 elements, or there exists no permutation of elements "
6765 " - element #0 and 1 are a pair where the first is half of the second, "
6767 " - element #1 and 2 are a pair where the first is half of the second, "
6769 " - element #2 and 3 are a pair where the first is half of the second",
6770 DescribeNegation(
m));
6773 TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
6774 list<signed char> rhs;
6787 TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
6788 const int lhs[] = {1, 2, 3};
6797 TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
6798 const int rhs[] = {1, 2, 3};
6808 TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
6809 const vector<int> lhs{2, 4, 6};
6815 TEST(UnorderedPointwiseTest, RejectsWrongSize) {
6816 const double lhs[2] = {1, 2};
6817 const int rhs[1] = {0};
6822 const int rhs2[3] = {0, 1, 2};
6826 TEST(UnorderedPointwiseTest, RejectsWrongContent) {
6827 const double lhs[3] = {1, 2, 3};
6828 const int rhs[3] = {2, 6, 6};
6830 EXPECT_EQ(
"where the following elements don't match any matchers:\n"
6835 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
6836 const double lhs[3] = {1, 2, 3};
6837 const int rhs[3] = {2, 4, 6};
6841 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
6842 const double lhs[3] = {1, 2, 3};
6843 const int rhs[3] = {6, 4, 2};
6847 TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
6848 const double lhs[3] = {1, 2, 3};
6849 const int rhs[3] = {4, 6, 2};
6850 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6855 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6859 TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
6860 ContainerHelper helper;
6862 std::vector<int>{1, 2})));
6863 helper.Call(MakeUniquePtrs({2, 1}));
6868 template <
typename T>
6869 class SampleOptional {
6872 explicit SampleOptional(
T value)
6883 TEST(OptionalTest, DescribesSelf) {
6884 const Matcher<SampleOptional<int>>
m =
Optional(
Eq(1));
6885 EXPECT_EQ(
"value is equal to 1", Describe(
m));
6888 TEST(OptionalTest, ExplainsSelf) {
6889 const Matcher<SampleOptional<int>>
m =
Optional(
Eq(1));
6890 EXPECT_EQ(
"whose value 1 matches", Explain(
m, SampleOptional<int>(1)));
6891 EXPECT_EQ(
"whose value 2 doesn't match", Explain(
m, SampleOptional<int>(2)));
6894 TEST(OptionalTest, MatchesNonEmptyOptional) {
6895 const Matcher<SampleOptional<int>> m1 =
Optional(1);
6896 const Matcher<SampleOptional<int>> m2 =
Optional(
Eq(2));
6897 const Matcher<SampleOptional<int>> m3 =
Optional(
Lt(3));
6898 SampleOptional<int> opt(1);
6904 TEST(OptionalTest, DoesNotMatchNullopt) {
6905 const Matcher<SampleOptional<int>>
m =
Optional(1);
6906 SampleOptional<int>
empty;
6910 TEST(OptionalTest, WorksWithMoveOnly) {
6911 Matcher<SampleOptional<std::unique_ptr<int>>>
m =
Optional(
Eq(
nullptr));
6912 EXPECT_TRUE(
m.Matches(SampleOptional<std::unique_ptr<int>>(
nullptr)));
6915 class SampleVariantIntString {
6920 template <
typename T>
6925 template <
typename T>
6926 friend const T&
get(
const SampleVariantIntString&
value) {
6927 return value.get_impl(
static_cast<T*
>(
nullptr));
6931 const int& get_impl(
int*)
const {
return i_; }
6939 TEST(VariantTest, DescribesSelf) {
6940 const Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6942 "'.*' and the value is equal to 1"));
6945 TEST(VariantTest, ExplainsSelf) {
6946 const Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6950 HasSubstr(
"whose value is not of type '"));
6952 "whose value 2 doesn't match");
6955 TEST(VariantTest, FullMatch) {
6956 Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6959 m = VariantWith<std::string>(
Eq(
"1"));
6963 TEST(VariantTest, TypeDoesNotMatch) {
6964 Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6967 m = VariantWith<std::string>(
Eq(
"1"));
6971 TEST(VariantTest, InnerDoesNotMatch) {
6972 Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6975 m = VariantWith<std::string>(
Eq(
"1"));
6979 class SampleAnyType {
6981 explicit SampleAnyType(
int i) :
index_(0),
i_(
i) {}
6984 template <
typename T>
6985 friend const T*
any_cast(
const SampleAnyType* any) {
6986 return any->get_impl(
static_cast<T*
>(
nullptr));
6994 const int* get_impl(
int*)
const {
return index_ == 0 ? &
i_ :
nullptr; }
6996 return index_ == 1 ? &
s_ :
nullptr;
7000 TEST(AnyWithTest, FullMatch) {
7001 Matcher<SampleAnyType>
m = AnyWith<int>(
Eq(1));
7005 TEST(AnyWithTest, TestBadCastType) {
7006 Matcher<SampleAnyType>
m = AnyWith<std::string>(
Eq(
"fail"));
7010 TEST(AnyWithTest, TestUseInContainers) {
7011 std::vector<SampleAnyType>
a;
7018 std::vector<SampleAnyType>
b;
7019 b.emplace_back(
"hello");
7020 b.emplace_back(
"merhaba");
7021 b.emplace_back(
"salut");
7023 AnyWith<std::string>(
"merhaba"),
7024 AnyWith<std::string>(
"salut")}));
7026 TEST(AnyWithTest, TestCompare) {
7030 TEST(AnyWithTest, DescribesSelf) {
7031 const Matcher<const SampleAnyType&>
m = AnyWith<int>(
Eq(1));
7033 "'.*' and the value is equal to 1"));
7036 TEST(AnyWithTest, ExplainsSelf) {
7037 const Matcher<const SampleAnyType&>
m = AnyWith<int>(
Eq(1));
7041 HasSubstr(
"whose value is not of type '"));
7042 EXPECT_THAT(Explain(
m, SampleAnyType(2)),
"whose value 2 doesn't match");
7045 TEST(PointeeTest, WorksOnMoveOnlyType) {
7046 std::unique_ptr<int>
p(
new int(3));
7051 TEST(NotTest, WorksOnMoveOnlyType) {
7052 std::unique_ptr<int>
p(
new int(3));
7059 TEST(ArgsTest, AcceptsZeroTemplateArg) {
7060 const std::tuple<int, bool>
t(5,
true);
7065 TEST(ArgsTest, AcceptsOneTemplateArg) {
7066 const std::tuple<int, bool>
t(5,
true);
7072 TEST(ArgsTest, AcceptsTwoTemplateArgs) {
7073 const std::tuple<short, int, long>
t(4, 5, 6L);
7080 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
7081 const std::tuple<short, int, long>
t(4, 5, 6L);
7086 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
7087 const std::tuple<short, int, long>
t(4, 5, 6L);
7093 return std::get<0>(
arg) + std::get<1>(
arg) + std::get<2>(
arg) == 0;
7096 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
7101 TEST(ArgsTest, CanBeNested) {
7102 const std::tuple<short, int, long, int>
t(4, 5, 6L, 6);
7107 TEST(ArgsTest, CanMatchTupleByValue) {
7108 typedef std::tuple<char, int, int> Tuple3;
7109 const Matcher<Tuple3>
m = Args<1, 2>(
Lt());
7114 TEST(ArgsTest, CanMatchTupleByReference) {
7115 typedef std::tuple<char, char, int> Tuple3;
7116 const Matcher<const Tuple3&>
m = Args<0, 1>(
Lt());
7126 TEST(ArgsTest, AcceptsTenTemplateArgs) {
7127 EXPECT_THAT(
std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
7128 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
7129 PrintsAs(
"(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
7130 EXPECT_THAT(
std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
7131 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
7132 PrintsAs(
"(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
7135 TEST(ArgsTest, DescirbesSelfCorrectly) {
7136 const Matcher<std::tuple<int, bool, char> >
m = Args<2, 0>(
Lt());
7137 EXPECT_EQ(
"are a tuple whose fields (#2, #0) are a pair where "
7138 "the first < the second",
7142 TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
7143 const Matcher<const std::tuple<int, bool, char, int>&>
m =
7144 Args<0, 2, 3>(Args<2, 0>(
Lt()));
7145 EXPECT_EQ(
"are a tuple whose fields (#0, #2, #3) are a tuple "
7146 "whose fields (#2, #0) are a pair where the first < the second",
7150 TEST(ArgsTest, DescribesNegationCorrectly) {
7151 const Matcher<std::tuple<int, char> >
m = Args<1, 0>(
Gt());
7152 EXPECT_EQ(
"are a tuple whose fields (#1, #0) aren't a pair "
7153 "where the first > the second",
7154 DescribeNegation(
m));
7157 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
7158 const Matcher<std::tuple<bool, int, int> >
m = Args<1, 2>(
Eq());
7159 EXPECT_EQ(
"whose fields (#1, #2) are (42, 42)",
7161 EXPECT_EQ(
"whose fields (#1, #2) are (42, 43)",
7166 class LessThanMatcher :
public MatcherInterface<std::tuple<char, int> > {
7168 void DescribeTo(::std::ostream* )
const override {}
7170 bool MatchAndExplain(std::tuple<char, int>
value,
7171 MatchResultListener* listener)
const override {
7174 *listener <<
"where the first value is " <<
diff
7175 <<
" more than the second";
7181 Matcher<std::tuple<char, int> > LessThan() {
7185 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
7186 const Matcher<std::tuple<char, int, int> >
m = Args<0, 2>(LessThan());
7188 "whose fields (#0, #2) are ('a' (97, 0x61), 42), "
7189 "where the first value is 55 more than the second",
7191 EXPECT_EQ(
"whose fields (#0, #2) are ('\\0', 43)",
7197 enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
7202 class MockMatcher :
public MatcherInterface<Behavior> {
7204 bool MatchAndExplain(Behavior behavior,
7205 MatchResultListener* listener)
const override {
7206 *listener <<
"[MatchAndExplain]";
7208 case kInitialSuccess:
7212 return !listener->IsInterested();
7222 return listener->IsInterested();
7229 void DescribeTo(ostream* os)
const override { *os <<
"[DescribeTo]"; }
7231 void DescribeNegationTo(ostream* os)
const override {
7232 *os <<
"[DescribeNegationTo]";
7236 AssertionResult RunPredicateFormatter(Behavior behavior) {
7238 PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
7240 return predicate_formatter(
"dummy-name", behavior);
7244 TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
7245 AssertionResult
result = RunPredicateFormatter(kInitialSuccess);
7251 TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
7252 AssertionResult
result = RunPredicateFormatter(kAlwaysFail);
7255 "Value of: dummy-name\nExpected: [DescribeTo]\n"
7257 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
7261 TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
7262 AssertionResult
result = RunPredicateFormatter(kFlaky);
7265 "Value of: dummy-name\nExpected: [DescribeTo]\n"
7266 " The matcher failed on the initial attempt; but passed when rerun to "
7267 "generate the explanation.\n"
7269 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
7275 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
7280 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
7282 EXPECT_EQ(
"has 1 element that is > 5", Describe(
m));
7285 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
7288 "has 2 elements where\n"
7289 "element #0 is equal to \"one\",\n"
7290 "element #1 is equal to \"two\"",
7294 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
7296 EXPECT_EQ(
"isn't empty", DescribeNegation(
m));
7299 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElement) {
7302 "doesn't have 1 element, or\n"
7303 "element #0 isn't > 5",
7304 DescribeNegation(
m));
7307 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
7308 Matcher<const list<std::string>&>
m =
ElementsAre(
"one",
"two");
7310 "doesn't have 2 elements, or\n"
7311 "element #0 isn't equal to \"one\", or\n"
7312 "element #1 isn't equal to \"two\"",
7313 DescribeNegation(
m));
7316 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
7325 TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
7326 Matcher<const vector<int>&>
m =
7329 const int a[] = {10, 0, 100};
7332 "whose element #0 matches, which is 9 more than 1,\n"
7333 "and whose element #2 matches, which is 98 more than 2",
7337 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
7348 TEST(ElementsAreTest, CanExplainMismatchRightSize) {
7349 Matcher<const vector<int>&>
m =
ElementsAre(1, GreaterThan(5));
7354 EXPECT_EQ(
"whose element #0 doesn't match", Explain(
m,
v));
7357 EXPECT_EQ(
"whose element #1 doesn't match, which is 4 less than 5",
7361 TEST(ElementsAreTest, MatchesOneElementVector) {
7368 TEST(ElementsAreTest, MatchesOneElementList) {
7375 TEST(ElementsAreTest, MatchesThreeElementVector) {
7384 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
7391 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
7398 TEST(ElementsAreTest, MatchesOneElementValue) {
7405 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
7414 TEST(ElementsAreTest, MatchesTenElementVector) {
7415 const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
7424 TEST(ElementsAreTest, DoesNotMatchWrongSize) {
7433 TEST(ElementsAreTest, DoesNotMatchWrongValue) {
7441 TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
7447 Matcher<vector<std::string>>
m =
7452 TEST(ElementsAreTest, WorksForNestedContainer) {
7453 constexpr std::array<const char*, 2> strings = {{
"Hi",
"world"}};
7455 vector<list<char>>
nested;
7456 for (
const auto& s : strings) {
7457 nested.emplace_back(s, s + strlen(s));
7466 TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
7467 int a[] = {0, 1, 2};
7474 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
7475 int a[] = {0, 1, 2};
7482 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
7483 int array[] = {0, 1, 2};
7489 class NativeArrayPassedAsPointerAndSize {
7491 NativeArrayPassedAsPointerAndSize() {}
7499 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
7500 int array[] = {0, 1};
7501 ::std::tuple<int*, size_t> array_as_tuple(
array, 2);
7505 NativeArrayPassedAsPointerAndSize helper;
7507 helper.Helper(
array, 2);
7510 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
7511 const char a2[][3] = {
"hi",
"lo"};
7519 TEST(ElementsAreTest, AcceptsStringLiteral) {
7526 extern const char kHi[];
7528 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
7539 const char kHi[] =
"hi";
7541 TEST(ElementsAreTest, MakesCopyOfArguments) {
7549 const int array1[] = {1, 2};
7551 const int array2[] = {0, 0};
7559 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
7560 const int a[] = {1, 2, 3};
7569 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
7570 std::array<const char*, 3>
a = {{
"one",
"two",
"three"}};
7575 const char**
p =
a.data();
7580 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
7581 const char*
a[] = {
"one",
"two",
"three"};
7590 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
7591 const Matcher<std::string> kMatcherArray[] = {
StrEq(
"one"),
StrEq(
"two"),
7604 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
7605 const int a[] = {1, 2, 3};
7613 TEST(ElementsAreArrayTest, TakesInitializerList) {
7614 const int a[5] = {1, 2, 3, 4, 5};
7620 TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
7627 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
7628 const int a[5] = {1, 2, 3, 4, 5};
7633 TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) {
7634 const int a[5] = {1, 2, 3, 4, 5};
7644 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
7645 const int a[] = {1, 2, 3};
7646 const Matcher<int> kMatchers[] = {
Eq(1),
Eq(2),
Eq(3)};
7648 const vector<Matcher<int>> expected(
std::begin(kMatchers),
7655 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
7656 const int a[] = {1, 2, 3};
7663 int*
const null_int =
nullptr;
7670 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
7679 TEST(ElementsAreArrayTest, SourceLifeSpan) {
7680 const int a[] = {1, 2, 3};
7683 ElementsAreArrayMatcher<int> matcher_maker =
7688 for (
int& i : expect) {
7700 MATCHER(IsEven,
"") {
return (
arg % 2) == 0; }
7702 TEST(MatcherMacroTest, Works) {
7703 const Matcher<int>
m = IsEven();
7708 EXPECT_EQ(
"not (is even)", DescribeNegation(
m));
7714 MATCHER(IsEven2, negation ?
"is odd" :
"is even") {
7715 if ((
arg % 2) == 0) {
7718 *result_listener <<
"OK";
7721 *result_listener <<
"% 2 == " << (
arg % 2);
7729 std::string(negation ?
"doesn't equal" :
"equals") +
" the sum of " +
7731 if (
arg == (x +
y)) {
7732 *result_listener <<
"OK";
7737 if (result_listener->stream() !=
nullptr) {
7738 *result_listener->stream() <<
"diff == " << (
x +
y -
arg);
7746 TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
7747 const Matcher<int> m1 = IsEven2();
7749 EXPECT_EQ(
"is odd", DescribeNegation(m1));
7751 const Matcher<int> m2 = EqSumOf(5, 9);
7752 EXPECT_EQ(
"equals the sum of 5 and 9", Describe(m2));
7753 EXPECT_EQ(
"doesn't equal the sum of 5 and 9", DescribeNegation(m2));
7757 TEST(MatcherMacroTest, CanExplainMatchResult) {
7758 const Matcher<int> m1 = IsEven2();
7762 const Matcher<int> m2 = EqSumOf(1, 2);
7764 EXPECT_EQ(
"diff == -1", Explain(m2, 4));
7771 StaticAssertTypeEq<::std::string, arg_type>();
7775 MATCHER(IsEmptyStringByRef,
"") {
7776 StaticAssertTypeEq<const ::std::string&, arg_type>();
7780 TEST(MatcherMacroTest, CanReferenceArgType) {
7781 const Matcher<::std::string> m1 = IsEmptyString();
7784 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
7791 MATCHER(IsOdd,
"") {
return (
arg % 2) != 0; }
7794 TEST(MatcherMacroTest, WorksInNamespace) {
7802 return Value(
arg, matcher_test::IsOdd()) &&
arg > 0;
7805 TEST(MatcherMacroTest, CanBeComposedUsingValue) {
7815 TEST(MatcherPMacroTest, Works) {
7816 const Matcher<int>
m = IsGreaterThan32And(5);
7820 EXPECT_EQ(
"is greater than 32 and (n: 5)", Describe(
m));
7821 EXPECT_EQ(
"not (is greater than 32 and (n: 5))", DescribeNegation(
m));
7829 TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
7830 const Matcher<int>
m = _is_Greater_Than32and_(5);
7832 EXPECT_EQ(
"is greater than 32 and (n: 5)", Describe(
m));
7833 EXPECT_EQ(
"not (is greater than 32 and (n: 5))", DescribeNegation(
m));
7841 class UncopyableFoo {
7845 UncopyableFoo(
const UncopyableFoo&) =
delete;
7846 void operator=(
const UncopyableFoo&) =
delete;
7852 MATCHER_P(ReferencesUncopyable, variable,
"") {
return &
arg == &variable; }
7854 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
7855 UncopyableFoo foo1(
'1'), foo2(
'2');
7856 const Matcher<const UncopyableFoo&>
m =
7857 ReferencesUncopyable<const UncopyableFoo&>(foo1);
7866 EXPECT_EQ(
"references uncopyable (variable: 1-byte object <31>)",
7874 StaticAssertTypeEq<int, foo_type>();
7875 StaticAssertTypeEq<long, bar_type>();
7876 StaticAssertTypeEq<char, baz_type>();
7880 TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
7881 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L,
'a'));
7887 MATCHER_P2(ReferencesAnyOf, variable1, variable2,
"") {
7888 return &
arg == &variable1 || &
arg == &variable2;
7891 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
7892 UncopyableFoo foo1(
'1'), foo2(
'2'), foo3(
'3');
7893 const Matcher<const UncopyableFoo&> const_m =
7894 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
7900 const Matcher<UncopyableFoo&>
m =
7901 ReferencesAnyOf<UncopyableFoo&, UncopyableFoo&>(foo1, foo2);
7908 TEST(MatcherPnMacroTest,
7909 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
7910 UncopyableFoo foo1(
'1'), foo2(
'2');
7911 const Matcher<const UncopyableFoo&>
m =
7912 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
7919 "references any of (variable1: 1-byte object <31>, variable2: 1-byte "
7928 TEST(MatcherPnMacroTest, Works) {
7929 const Matcher<const long&>
m = IsNotInClosedRange(10, 20);
7933 EXPECT_EQ(
"is not in closed range (low: 10, hi: 20)", Describe(
m));
7934 EXPECT_EQ(
"not (is not in closed range (low: 10, hi: 20))",
7935 DescribeNegation(
m));
7943 MATCHER(EqualsSumOf,
"") {
return arg == 0; }
7952 MATCHER_P7(EqualsSumOf, a,
b, c, d, e, f, g,
"") {
7955 MATCHER_P8(EqualsSumOf, a,
b, c, d, e, f, g, h,
"") {
7958 MATCHER_P9(EqualsSumOf, a,
b, c, d, e, f, g, h, i,
"") {
7961 MATCHER_P10(EqualsSumOf, a,
b, c, d, e, f, g, h, i, j,
"") {
7965 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
7971 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
7973 EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f'));
7975 EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g'));
7979 'f',
'g',
"h",
'i'));
7981 EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
"h",
7995 "e",
'f',
'g',
"h")));
7997 "e",
'f',
'g',
"h",
'i')));
7999 Not(EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
8005 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
8006 EXPECT_THAT(123, EqualsSumOf(100L, 20,
static_cast<char>(3)));
8009 EXPECT_THAT(124,
Not(EqualsSumOf(100L, 20,
static_cast<char>(3))));
8018 char suffix_char =
static_cast<char>(
suffix);
8019 return arg == prefix_str + suffix_char;
8022 TEST(MatcherPnMacroTest, SimpleTypePromotion) {
8023 Matcher<std::string> no_promo = EqConcat(
std::string(
"foo"),
't');
8024 Matcher<const std::string&> promo = EqConcat(
"foo",
static_cast<int>(
't'));
8033 TEST(MatcherPnMacroTest, TypesAreCorrect) {
8035 EqualsSumOfMatcher a0 = EqualsSumOf();
8038 EqualsSumOfMatcherP<int>
a1 = EqualsSumOf(1);
8042 EqualsSumOfMatcherP2<int, char>
a2 = EqualsSumOf(1,
'2');
8043 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2,
'3');
8044 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3,
'4');
8045 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
8046 EqualsSumOf(1, 2, 3, 4,
'5');
8047 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
8048 EqualsSumOf(1, 2, 3, 4, 5,
'6');
8049 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
8050 EqualsSumOf(1, 2, 3, 4, 5, 6,
'7');
8051 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
8052 EqualsSumOf(1, 2, 3, 4, 5, 6, 7,
'8');
8053 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
8054 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8,
'9');
8055 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
8056 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9,
'0');
8083 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
8090 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
8091 list<int> some_list;
8092 some_list.push_back(3);
8093 some_list.push_back(1);
8094 some_list.push_back(2);
8095 some_list.push_back(3);
8100 list<std::string> another_list;
8101 another_list.push_back(
"fee");
8102 another_list.push_back(
"fie");
8103 another_list.push_back(
"foe");
8104 another_list.push_back(
"fum");
8108 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
8109 list<int> some_list;
8110 some_list.push_back(3);
8111 some_list.push_back(1);
8115 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
8124 set<std::string> another_set;
8125 another_set.insert(
"fee");
8126 another_set.insert(
"fie");
8127 another_set.insert(
"foe");
8128 another_set.insert(
"fum");
8132 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
8138 set<std::string> c_string_set;
8139 c_string_set.insert(
"hello");
8143 TEST(ContainsTest, ExplainsMatchResultCorrectly) {
8144 const int a[2] = {1, 2};
8146 EXPECT_EQ(
"whose element #1 matches", Explain(
m, a));
8152 EXPECT_EQ(
"whose element #0 matches, which is 1 more than 0", Explain(
m, a));
8158 TEST(ContainsTest, DescribesItselfCorrectly) {
8160 EXPECT_EQ(
"contains at least one element that is equal to 1", Describe(
m));
8162 Matcher<vector<int>> m2 =
Not(
m);
8163 EXPECT_EQ(
"doesn't contain any element that is equal to 1", Describe(m2));
8166 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
8167 map<std::string, int> my_map;
8168 const char*
bar =
"a string";
8172 map<std::string, int> another_map;
8173 another_map[
"fee"] = 1;
8174 another_map[
"fie"] = 2;
8175 another_map[
"foe"] = 3;
8176 another_map[
"fum"] = 4;
8182 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
8183 map<int, int> some_map;
8189 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
8190 const char* string_array[] = {
"fee",
"fie",
"foe",
"fum"};
8194 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
8195 int int_array[] = {1, 2, 3, 4};
8199 TEST(ContainsTest, AcceptsMatcher) {
8200 const int a[] = {1, 2, 3};
8205 TEST(ContainsTest, WorksForNativeArrayAsTuple) {
8206 const int a[] = {1, 2};
8207 const int*
const pointer =
a;
8212 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
8213 int a[][3] = {{1, 2, 3}, {4, 5, 6}};
8222 TEST(ContainsTimes, ListMatchesWhenElementQuantityMatches) {
8223 list<int> some_list;
8224 some_list.push_back(3);
8225 some_list.push_back(1);
8226 some_list.push_back(2);
8227 some_list.push_back(3);
8241 TEST(ContainsTimes, ExplainsMatchResultCorrectly) {
8242 const int a[2] = {1, 2};
8245 "whose element #1 matches but whose match quantity of 1 does not match",
8249 EXPECT_EQ(
"has no element that matches and whose match quantity of 0 matches",
8254 "has no element that matches and whose match quantity of 0 does not "
8260 "whose element #1 matches but whose match quantity of 1 does not "
8265 EXPECT_EQ(
"whose elements (0, 1) match and whose match quantity of 2 matches",
8270 "has no element that matches and whose match quantity of 0 does not "
8274 m =
Contains(GreaterThan(0)).Times(GreaterThan<size_t>(5));
8276 "whose elements (0, 1) match but whose match quantity of 2 does not "
8277 "match, which is 3 less than 5",
8281 TEST(ContainsTimes, DescribesItselfCorrectly) {
8282 Matcher<vector<int>>
m =
Contains(1).Times(2);
8283 EXPECT_EQ(
"quantity of elements that match is equal to 1 is equal to 2",
8286 Matcher<vector<int>> m2 =
Not(
m);
8287 EXPECT_EQ(
"quantity of elements that match is equal to 1 isn't equal to 2",
8293 TEST(AllOfArrayTest, BasicForms) {
8295 std::vector<int> v0{};
8296 std::vector<int> v1{1};
8297 std::vector<int> v2{2, 3};
8298 std::vector<int> v3{4, 4, 4};
8305 int ar[6] = {1, 2, 3, 4, 4, 4};
8314 int ar2[2] = {2, 3};
8315 int ar3[3] = {4, 4, 4};
8335 TEST(AllOfArrayTest, Matchers) {
8346 TEST(AnyOfArrayTest, BasicForms) {
8348 std::vector<int> v0{};
8349 std::vector<int> v1{1};
8350 std::vector<int> v2{2, 3};
8357 int ar[3] = {1, 2, 3};
8366 int ar2[2] = {2, 3};
8386 TEST(AnyOfArrayTest, Matchers) {
8398 TEST(AnyOfArrayTest, ExplainsMatchResultCorrectly) {
8401 const std::vector<int> v0{};
8402 const std::vector<int> v1{1};
8403 const std::vector<int> v2{2, 3};
8404 const Matcher<int> m0 = AnyOfArray(v0);
8405 const Matcher<int> m1 = AnyOfArray(v1);
8406 const Matcher<int> m2 = AnyOfArray(v2);
8413 EXPECT_EQ(
"(is equal to 1)", Describe(m1));
8414 EXPECT_EQ(
"(is equal to 2) or (is equal to 3)", Describe(m2));
8416 EXPECT_EQ(
"(isn't equal to 1)", DescribeNegation(m1));
8417 EXPECT_EQ(
"(isn't equal to 2) and (isn't equal to 3)", DescribeNegation(m2));
8419 const Matcher<int> g1 = AnyOfArray({GreaterThan(1)});
8420 const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)});
8422 EXPECT_EQ(
"which is 1 less than 1", Explain(g1, 0));
8423 EXPECT_EQ(
"which is the same as 1", Explain(g1, 1));
8424 EXPECT_EQ(
"which is 1 more than 1", Explain(g1, 2));
8425 EXPECT_EQ(
"which is 1 less than 1, and which is 2 less than 2",
8427 EXPECT_EQ(
"which is the same as 1, and which is 1 less than 2",
8433 TEST(AllOfTest, HugeMatcher) {
8436 EXPECT_THAT(0,
testing::AllOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
8440 TEST(AnyOfTest, HugeMatcher) {
8443 EXPECT_THAT(0,
testing::AnyOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
8461 template <
typename T1,
typename T2>
8466 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
8468 testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
8471 template <
typename T1,
typename T2>
8476 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
8478 testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
8483 TEST(AllOfTest, WorksOnMoveOnlyType) {
8484 std::unique_ptr<int> p(
new int(3));
8489 TEST(AnyOfTest, WorksOnMoveOnlyType) {
8490 std::unique_ptr<int> p(
new int(3));
8495 MATCHER(IsNotNull,
"") {
return arg !=
nullptr; }
8499 TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
8500 std::unique_ptr<int>
p(
new int(3));
8505 MATCHER_P(UniquePointee, pointee,
"") {
return *
arg == pointee; }
8509 TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
8510 std::unique_ptr<int>
p(
new int(3));
8515 #if GTEST_HAS_EXCEPTIONS
8521 TEST(ThrowsTest, Examples) {
8523 std::function<
void()>([]() {
throw std::runtime_error(
"message"); }),
8524 Throws<std::runtime_error>());
8527 std::function<
void()>([]() {
throw std::runtime_error(
"message"); }),
8528 ThrowsMessage<std::runtime_error>(
HasSubstr(
"message")));
8531 TEST(ThrowsTest, PrintsExceptionWhat) {
8533 std::function<
void()>([]() {
throw std::runtime_error(
"ABC123XYZ"); }),
8534 ThrowsMessage<std::runtime_error>(
HasSubstr(
"ABC123XYZ")));
8537 TEST(ThrowsTest, DoesNotGenerateDuplicateCatchClauseWarning) {
8539 Throws<std::exception>());
8542 TEST(ThrowsTest, CallableExecutedExactlyOnce) {
8554 throw std::runtime_error(
"message");
8556 Throws<std::runtime_error>());
8561 throw std::runtime_error(
"message");
8563 ThrowsMessage<std::runtime_error>(
HasSubstr(
"message")));
8568 throw std::runtime_error(
"message");
8570 Throws<std::runtime_error>(
8575 TEST(ThrowsTest, Describe) {
8576 Matcher<
std::function<void()>> matcher = Throws<std::runtime_error>();
8577 std::stringstream ss;
8578 matcher.DescribeTo(&ss);
8579 auto explanation = ss.str();
8583 TEST(ThrowsTest, Success) {
8584 Matcher<
std::function<void()>> matcher = Throws<std::runtime_error>();
8585 StringMatchResultListener listener;
8587 []() { throw std::runtime_error(
"error message"); }, &listener));
8591 TEST(ThrowsTest, FailWrongType) {
8592 Matcher<
std::function<void()>> matcher = Throws<std::runtime_error>();
8593 StringMatchResultListener listener;
8595 []() { throw std::logic_error(
"error message"); }, &listener));
8600 TEST(ThrowsTest, FailWrongTypeNonStd) {
8601 Matcher<
std::function<void()>> matcher = Throws<std::runtime_error>();
8602 StringMatchResultListener listener;
8603 EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener));
8605 HasSubstr(
"throws an exception of an unknown type"));
8608 TEST(ThrowsTest, FailNoThrow) {
8609 Matcher<
std::function<void()>> matcher = Throws<std::runtime_error>();
8610 StringMatchResultListener listener;
8611 EXPECT_FALSE(matcher.MatchAndExplain([]() { (void)0; }, &listener));
8615 class ThrowsPredicateTest
8616 :
public TestWithParam<Matcher<std::function<void()>>> {};
8618 TEST_P(ThrowsPredicateTest, Describe) {
8620 std::stringstream ss;
8621 matcher.DescribeTo(&ss);
8622 auto explanation = ss.str();
8627 TEST_P(ThrowsPredicateTest, Success) {
8629 StringMatchResultListener listener;
8631 []() { throw std::runtime_error(
"error message"); }, &listener));
8635 TEST_P(ThrowsPredicateTest, FailWrongType) {
8637 StringMatchResultListener listener;
8639 []() { throw std::logic_error(
"error message"); }, &listener));
8644 TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) {
8646 StringMatchResultListener listener;
8647 EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener));
8649 HasSubstr(
"throws an exception of an unknown type"));
8652 TEST_P(ThrowsPredicateTest, FailNoThrow) {
8654 StringMatchResultListener listener;
8655 EXPECT_FALSE(matcher.MatchAndExplain([]() {}, &listener));
8660 AllMessagePredicates, ThrowsPredicateTest,
8662 ThrowsMessage<std::runtime_error>(
HasSubstr(
"error message")))));
8665 TEST(ThrowsPredicateCompilesTest, ExceptionMatcherAcceptsBroadType) {
8668 ThrowsMessage<std::runtime_error>(
HasSubstr(
"error message"));
8670 matcher.Matches([]() { throw std::runtime_error(
"error message"); }));
8672 matcher.Matches([]() { throw std::runtime_error(
"wrong message"); }));
8676 Matcher<uint64_t> inner =
Eq(10);
8677 Matcher<
std::function<void()>> matcher = Throws<uint32_t>(inner);
8678 EXPECT_TRUE(matcher.Matches([]() { throw(uint32_t) 10; }));
8679 EXPECT_FALSE(matcher.Matches([]() { throw(uint32_t) 11; }));
8685 TEST(ThrowsPredicateCompilesTest, MessageMatcherAcceptsNonMatcher) {
8687 ThrowsMessage<std::runtime_error>(
"error message");
8689 matcher.Matches([]() { throw std::runtime_error(
"error message"); }));
8691 []() { throw std::runtime_error(
"wrong error message"); }));
8694 #endif // GTEST_HAS_EXCEPTIONS
8701 # pragma warning(pop)