37 #include <forward_list>
55 namespace gmock_matchers_test {
58 std::vector<std::unique_ptr<int>> MakeUniquePtrs(
const std::vector<int>& ints) {
59 std::vector<std::unique_ptr<int>> pointers;
60 for (
int i : ints) pointers.emplace_back(
new int(
i));
64 std::string OfType(
const std::string& type_name) {
66 return IsReadableTypeName(type_name) ?
" (of type " + type_name +
")" :
"";
72 TEST(ContainsTest, WorksWithMoveOnly) {
73 ContainerHelper helper;
75 helper.Call(MakeUniquePtrs({1, 2}));
81 TEST(ElementsAreTest, HugeMatcher) {
82 vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
85 ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7),
86 Eq(8), Eq(9), Eq(10), Gt(1), Eq(12)));
90 TEST(ElementsAreTest, HugeMatcherStr) {
91 vector<std::string> test_vector{
92 "literal_string",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
94 EXPECT_THAT(test_vector, UnorderedElementsAre(
"literal_string",
_,
_,
_,
_,
_,
99 TEST(ElementsAreTest, HugeMatcherUnordered) {
100 vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
103 Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7),
104 Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));
109 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
112 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) <<
"This should succeed too.";
118 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
121 static unsigned short n;
126 "Expected: is > 10\n"
128 OfType(
"unsigned short"));
132 "Expected: (is <= 7) and (is >= 5)\n"
134 OfType(
"unsigned short"));
139 TEST(MatcherAssertionTest, WorksForByRefArguments) {
147 "Expected: does not reference the variable @");
150 "Actual: 0" + OfType(
"int") +
", which is located @");
155 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
156 Matcher<const char*> starts_with_he =
StartsWith(
"he");
159 Matcher<const std::string&> ends_with_ok =
EndsWith(
"ok");
161 const std::string bad =
"bad";
164 "Expected: ends with \"ok\"\n"
166 Matcher<int> is_greater_than_5 = Gt(5);
174 TEST(PointeeTest, RawPointer) {
175 const Matcher<int*> m = Pointee(Ge(0));
184 TEST(PointeeTest, RawPointerToConst) {
185 const Matcher<const double*> m = Pointee(Ge(0));
194 TEST(PointeeTest, ReferenceToConstRawPointer) {
195 const Matcher<int* const&> m = Pointee(Ge(0));
204 TEST(PointeeTest, ReferenceToNonConstRawPointer) {
205 const Matcher<double*&> m = Pointee(Ge(0));
216 TEST(PointeeTest, SmartPointer) {
217 const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0));
219 std::unique_ptr<int> n(
new int(1));
223 TEST(PointeeTest, SmartPointerToConst) {
224 const Matcher<std::unique_ptr<const int>> m = Pointee(Ge(0));
229 std::unique_ptr<const int> n(
new int(1));
233 TEST(PointerTest, RawPointer) {
235 const Matcher<int*> m = Pointer(Eq(&n));
244 TEST(PointerTest, RawPointerToConst) {
246 const Matcher<const int*> m = Pointer(Eq(&n));
255 TEST(PointerTest, SmartPointer) {
256 std::unique_ptr<int> n(
new int(10));
257 int* raw_n = n.get();
258 const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n));
263 TEST(PointerTest, SmartPointerToConst) {
264 std::unique_ptr<const int> n(
new int(10));
265 const int* raw_n = n.get();
266 const Matcher<std::unique_ptr<const int>> m = Pointer(Eq(raw_n));
271 std::unique_ptr<const int>
p(
new int(10));
276 template <
typename T>
277 class ConstPropagatingPtr {
279 typedef T element_type;
281 ConstPropagatingPtr() :
val_() {}
282 explicit ConstPropagatingPtr(
T* t) :
val_(t) {}
283 ConstPropagatingPtr(
const ConstPropagatingPtr& other) :
val_(other.
val_) {}
285 T*
get() {
return val_; }
288 const T*
get()
const {
return val_; }
297 TEST(PointeeTest, WorksWithConstPropagatingPointers) {
298 const Matcher<ConstPropagatingPtr<int>> m = Pointee(Lt(5));
300 const ConstPropagatingPtr<int> co(&three);
301 ConstPropagatingPtr<int> o(&three);
309 TEST(PointeeTest, NeverMatchesNull) {
310 const Matcher<const char*> m = Pointee(
_);
315 TEST(PointeeTest, MatchesAgainstAValue) {
316 const Matcher<int*> m = Pointee(5);
325 TEST(PointeeTest, CanDescribeSelf) {
326 const Matcher<int*> m = Pointee(Gt(3));
331 TEST_P(PointeeTestP, CanExplainMatchResult) {
332 const Matcher<const std::string*> m = Pointee(
StartsWith(
"Hi"));
336 const Matcher<long*> m2 = Pointee(GreaterThan(1));
338 EXPECT_EQ(
"which points to 3" + OfType(
"long") +
", which is 2 more than 1",
342 TEST(PointeeTest, AlwaysExplainsPointee) {
343 const Matcher<int*> m = Pointee(0);
351 Uncopyable() :
value_(-1) {}
352 explicit Uncopyable(
int a_value) :
value_(a_value) {}
355 void set_value(
int i) {
value_ =
i; }
359 Uncopyable(
const Uncopyable&) =
delete;
360 Uncopyable& operator=(
const Uncopyable&) =
delete;
364 bool ValueIsPositive(
const Uncopyable& x) {
return x.value() > 0; }
366 MATCHER_P(UncopyableIs, inner_matcher,
"") {
367 return ExplainMatchResult(inner_matcher, arg.value(), result_listener);
372 AStruct() : x(0),
y(1.0),
z(5), p(nullptr) {}
373 AStruct(
const AStruct& rhs)
374 : x(rhs.x),
y(rhs.
y),
z(rhs.
z.
value()), p(rhs.p) {}
383 struct DerivedStruct :
public AStruct {
390 TEST(FieldTest, WorksForNonConstField) {
391 Matcher<AStruct> m = Field(&
AStruct::x, Ge(0));
392 Matcher<AStruct> m_with_name = Field(
"x", &
AStruct::x, Ge(0));
403 TEST(FieldTest, WorksForConstField) {
406 Matcher<AStruct> m = Field(&
AStruct::y, Ge(0.0));
407 Matcher<AStruct> m_with_name = Field(
"y", &
AStruct::y, Ge(0.0));
411 m_with_name = Field(
"y", &
AStruct::y, Le(0.0));
417 TEST(FieldTest, WorksForUncopyableField) {
420 Matcher<AStruct> m = Field(&
AStruct::z, Truly(ValueIsPositive));
422 m = Field(&
AStruct::z, Not(Truly(ValueIsPositive)));
427 TEST(FieldTest, WorksForPointerField) {
429 Matcher<AStruct> m = Field(&
AStruct::p, static_cast<const char*>(
nullptr));
444 TEST(FieldTest, WorksForByRefArgument) {
445 Matcher<const AStruct&> m = Field(&
AStruct::x, Ge(0));
455 TEST(FieldTest, WorksForArgumentOfSubType) {
458 Matcher<const DerivedStruct&> m = Field(&
AStruct::x, Ge(0));
468 TEST(FieldTest, WorksForCompatibleMatcherType) {
470 Matcher<const AStruct&> m = Field(&
AStruct::x, Matcher<signed char>(Ge(0)));
479 TEST(FieldTest, CanDescribeSelf) {
480 Matcher<const AStruct&> m = Field(&
AStruct::x, Ge(0));
486 TEST(FieldTest, CanDescribeSelfWithFieldName) {
487 Matcher<const AStruct&> m = Field(
"field_name", &
AStruct::x, Ge(0));
490 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
495 TEST_P(FieldTestP, CanExplainMatchResult) {
496 Matcher<const AStruct&> m = Field(&
AStruct::x, Ge(0));
504 "whose given field is 1" + OfType(
"int") +
", which is 1 more than 0",
508 TEST_P(FieldTestP, CanExplainMatchResultWithFieldName) {
509 Matcher<const AStruct&> m = Field(
"field_name", &
AStruct::x, Ge(0));
515 m = Field(
"field_name", &
AStruct::x, GreaterThan(0));
516 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int") +
517 ", which is 1 more than 0",
524 TEST(FieldForPointerTest, WorksForPointerToConst) {
525 Matcher<const AStruct*> m = Field(&
AStruct::x, Ge(0));
534 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
535 Matcher<AStruct*> m = Field(&
AStruct::x, Ge(0));
544 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
545 Matcher<AStruct* const&> m = Field(&
AStruct::x, Ge(0));
554 TEST(FieldForPointerTest, DoesNotMatchNull) {
561 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
564 Matcher<DerivedStruct*> m = Field(&
AStruct::x, Ge(0));
573 TEST(FieldForPointerTest, CanDescribeSelf) {
574 Matcher<const AStruct*> m = Field(&
AStruct::x, Ge(0));
580 TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
581 Matcher<const AStruct*> m = Field(
"field_name", &
AStruct::x, Ge(0));
584 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
589 TEST_P(FieldForPointerTestP, CanExplainMatchResult) {
590 Matcher<const AStruct*> m = Field(&
AStruct::x, Ge(0));
595 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int"),
599 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int") +
600 ", which is 1 more than 0",
604 TEST_P(FieldForPointerTestP, CanExplainMatchResultWithFieldName) {
605 Matcher<const AStruct*> m = Field(
"field_name", &
AStruct::x, Ge(0));
611 "which points to an object whose field `field_name` is 1" + OfType(
"int"),
614 m = Field(
"field_name", &
AStruct::x, GreaterThan(0));
615 EXPECT_EQ(
"which points to an object whose field `field_name` is 1" +
616 OfType(
"int") +
", which is 1 more than 0",
626 int n()
const {
return n_; }
628 void set_n(
int new_n) {
n_ = new_n; }
631 const std::string& s()
const {
return s_; }
633 const std::string& s_ref() const& {
return s_; }
635 void set_s(
const std::string& new_s) {
s_ = new_s; }
638 double&
x()
const {
return x_; }
650 class DerivedClass :
public AClass {
652 int k()
const {
return k_; }
662 TEST(PropertyTest, WorksForNonReferenceProperty) {
663 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
664 Matcher<const AClass&> m_with_name = Property(
"n", &AClass::n, Ge(0));
678 TEST(PropertyTest, WorksForReferenceToConstProperty) {
679 Matcher<const AClass&> m = Property(&AClass::s,
StartsWith(
"hi"));
680 Matcher<const AClass&> m_with_name =
695 TEST(PropertyTest, WorksForRefQualifiedProperty) {
696 Matcher<const AClass&> m = Property(&AClass::s_ref,
StartsWith(
"hi"));
697 Matcher<const AClass&> m_with_name =
698 Property(
"s", &AClass::s_ref,
StartsWith(
"hi"));
712 TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
716 Matcher<const AClass&> m = Property(&
AClass::x, Ref(x));
725 TEST(PropertyTest, WorksForByValueArgument) {
726 Matcher<AClass> m = Property(&AClass::s,
StartsWith(
"hi"));
738 TEST(PropertyTest, WorksForArgumentOfSubType) {
741 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
753 TEST(PropertyTest, WorksForCompatibleMatcherType) {
755 Matcher<const AClass&> m = Property(&AClass::n, Matcher<signed char>(Ge(0)));
757 Matcher<const AClass&> m_with_name =
758 Property(
"n", &AClass::n, Matcher<signed char>(Ge(0)));
769 TEST(PropertyTest, CanDescribeSelf) {
770 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
773 EXPECT_EQ(
"is an object whose given property isn't >= 0",
777 TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
778 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
781 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
786 TEST_P(PropertyTestP, CanExplainMatchResult) {
787 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
793 m = Property(&AClass::n, GreaterThan(0));
795 "whose given property is 1" + OfType(
"int") +
", which is 1 more than 0",
799 TEST_P(PropertyTestP, CanExplainMatchResultWithPropertyName) {
800 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
804 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int"),
Explain(m, a));
806 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
807 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int") +
808 ", which is 1 more than 0",
815 TEST(PropertyForPointerTest, WorksForPointerToConst) {
816 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
827 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
828 Matcher<AClass*> m = Property(&AClass::s,
StartsWith(
"hi"));
840 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
841 Matcher<AClass* const&> m = Property(&AClass::s,
StartsWith(
"hi"));
852 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
853 Matcher<const AClass*> m = Property(&
AClass::x,
_);
859 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
862 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
873 TEST(PropertyForPointerTest, CanDescribeSelf) {
874 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
877 EXPECT_EQ(
"is an object whose given property isn't >= 0",
881 TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
882 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
885 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
890 TEST_P(PropertyForPointerTestP, CanExplainMatchResult) {
891 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
897 "which points to an object whose given property is 1" + OfType(
"int"),
900 m = Property(&AClass::n, GreaterThan(0));
901 EXPECT_EQ(
"which points to an object whose given property is 1" +
902 OfType(
"int") +
", which is 1 more than 0",
906 TEST_P(PropertyForPointerTestP, CanExplainMatchResultWithPropertyName) {
907 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
912 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
916 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
917 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
918 OfType(
"int") +
", which is 1 more than 0",
926 std::string IntToStringFunction(
int input) {
927 return input == 1 ?
"foo" :
"bar";
932 TEST(ResultOfTest, WorksForFunctionPointers) {
933 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string(
"foo")));
940 TEST(ResultOfTest, CanDescribeItself) {
941 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq(
"foo"));
944 "is mapped by the given callable to a value that "
945 "is equal to \"foo\"",
948 "is mapped by the given callable to a value that "
949 "isn't equal to \"foo\"",
954 TEST(ResultOfTest, CanDescribeItselfWithResultDescription) {
955 Matcher<int> matcher =
956 ResultOf(
"string conversion", &IntToStringFunction, StrEq(
"foo"));
959 EXPECT_EQ(
"whose string conversion isn't equal to \"foo\"",
964 int IntFunction(
int input) {
return input == 42 ? 80 : 90; }
966 TEST_P(ResultOfTestP, CanExplainMatchResult) {
967 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
968 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int"),
971 matcher = ResultOf(&IntFunction, GreaterThan(85));
972 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int") +
973 ", which is 5 more than 85",
977 TEST_P(ResultOfTestP, CanExplainMatchResultWithResultDescription) {
978 Matcher<int> matcher = ResultOf(
"magic int conversion", &IntFunction, Ge(85));
979 EXPECT_EQ(
"whose magic int conversion is 90" + OfType(
"int"),
982 matcher = ResultOf(
"magic int conversion", &IntFunction, GreaterThan(85));
983 EXPECT_EQ(
"whose magic int conversion is 90" + OfType(
"int") +
984 ", which is 5 more than 85",
990 TEST(ResultOfTest, WorksForNonReferenceResults) {
991 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
999 double& DoubleFunction(
double& input) {
return input; }
1001 Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
1005 TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
1008 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
1016 Matcher<Uncopyable&> matcher2 = ResultOf(&RefUncopyableFunction, Ref(obj));
1024 const std::string& StringFunction(
const std::string& input) {
return input; }
1026 TEST(ResultOfTest, WorksForReferenceToConstResults) {
1027 std::string s =
"foo";
1029 Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s));
1037 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
1039 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
1047 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
1049 ResultOf(
static_cast<std::string (*)(
int dummy)
>(
nullptr),
1050 Eq(std::string(
"foo"))),
1051 "NULL function pointer is passed into ResultOf\\(\\)\\.");
1056 TEST(ResultOfTest, WorksForFunctionReferences) {
1057 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq(
"foo"));
1065 std::string operator()(
int input)
const {
return IntToStringFunction(input); }
1068 TEST(ResultOfTest, WorksForFunctors) {
1069 Matcher<int> matcher = ResultOf(Functor(), Eq(std::string(
"foo")));
1078 struct PolymorphicFunctor {
1079 typedef int result_type;
1080 int operator()(
int n) {
return n; }
1081 int operator()(
const char* s) {
return static_cast<int>(strlen(s)); }
1082 std::string operator()(
int* p) {
return p ?
"good ptr" :
"null"; }
1085 TEST(ResultOfTest, WorksForPolymorphicFunctors) {
1086 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
1091 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
1093 EXPECT_TRUE(matcher_string.Matches(
"long string"));
1097 TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
1098 Matcher<int*> matcher = ResultOf(PolymorphicFunctor(),
"good ptr");
1105 TEST(ResultOfTest, WorksForLambdas) {
1106 Matcher<int> matcher = ResultOf(
1108 return std::string(static_cast<size_t>(str_len),
'x');
1115 TEST(ResultOfTest, WorksForNonCopyableArguments) {
1116 Matcher<std::unique_ptr<int>> matcher = ResultOf(
1117 [](
const std::unique_ptr<int>& str_len) {
1118 return std::string(static_cast<size_t>(*str_len),
'x');
1121 EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(
new int(3))));
1122 EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(
new int(1))));
1125 const int* ReferencingFunction(
const int& n) {
return &n; }
1127 struct ReferencingFunctor {
1128 typedef const int* result_type;
1129 result_type operator()(
const int& n) {
return &n; }
1132 TEST(ResultOfTest, WorksForReferencingCallables) {
1135 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
1139 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
1144 TEST(SizeIsTest, ImplementsSizeIs) {
1148 container.push_back(0);
1151 container.push_back(0);
1156 TEST(SizeIsTest, WorksWithMap) {
1157 map<std::string, int> container;
1160 container.insert(make_pair(
"foo", 1));
1163 container.insert(make_pair(
"bar", 2));
1168 TEST(SizeIsTest, WorksWithReferences) {
1169 vector<int> container;
1170 Matcher<const vector<int>&> m = SizeIs(1);
1172 container.push_back(0);
1176 TEST(SizeIsTest, WorksWithMoveOnly) {
1177 ContainerHelper helper;
1179 helper.Call(MakeUniquePtrs({1, 2, 3}));
1184 struct MinimalistCustomType {
1185 int size()
const {
return 1; }
1187 TEST(SizeIsTest, WorksWithMinimalistCustomType) {
1188 MinimalistCustomType container;
1193 TEST(SizeIsTest, CanDescribeSelf) {
1194 Matcher<vector<int>> m = SizeIs(2);
1199 TEST(SizeIsTest, ExplainsResult) {
1200 Matcher<vector<int>> m1 = SizeIs(2);
1201 Matcher<vector<int>> m2 = SizeIs(Lt(2u));
1202 Matcher<vector<int>> m3 = SizeIs(AnyOf(0, 3));
1203 Matcher<vector<int>> m4 = SizeIs(Gt(1u));
1204 vector<int> container;
1207 EXPECT_EQ(
"whose size 0 matches, which matches (is equal to 0)",
1210 container.push_back(0);
1211 container.push_back(0);
1215 "whose size 2 doesn't match, isn't equal to 0, and isn't equal to 3",
1220 TEST(WhenSortedByTest, WorksForEmptyContainer) {
1221 const vector<int> numbers;
1222 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
1223 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
1226 TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
1227 vector<unsigned> numbers;
1228 numbers.push_back(3);
1229 numbers.push_back(1);
1230 numbers.push_back(2);
1231 numbers.push_back(2);
1233 WhenSortedBy(greater<unsigned>(), ElementsAre(3, 2, 2, 1)));
1235 Not(WhenSortedBy(greater<unsigned>(), ElementsAre(1, 2, 2, 3))));
1238 TEST(WhenSortedByTest, WorksForNonVectorContainer) {
1239 list<std::string> words;
1240 words.push_back(
"say");
1241 words.push_back(
"hello");
1242 words.push_back(
"world");
1243 EXPECT_THAT(words, WhenSortedBy(less<std::string>(),
1244 ElementsAre(
"hello",
"say",
"world")));
1245 EXPECT_THAT(words, Not(WhenSortedBy(less<std::string>(),
1246 ElementsAre(
"say",
"hello",
"world"))));
1249 TEST(WhenSortedByTest, WorksForNativeArray) {
1250 const int numbers[] = {1, 3, 2, 4};
1251 const int sorted_numbers[] = {1, 2, 3, 4};
1252 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
1254 WhenSortedBy(less<int>(), ElementsAreArray(sorted_numbers)));
1255 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
1258 TEST(WhenSortedByTest, CanDescribeSelf) {
1259 const Matcher<vector<int>> m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
1261 "(when sorted) has 2 elements where\n"
1262 "element #0 is equal to 1,\n"
1263 "element #1 is equal to 2",
1266 "(when sorted) doesn't have 2 elements, or\n"
1267 "element #0 isn't equal to 1, or\n"
1268 "element #1 isn't equal to 2",
1272 TEST(WhenSortedByTest, ExplainsMatchResult) {
1273 const int a[] = {2, 1};
1274 EXPECT_EQ(
"which is { 1, 2 } when sorted, whose element #0 doesn't match",
1275 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
1276 EXPECT_EQ(
"which is { 1, 2 } when sorted",
1277 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
1283 TEST(WhenSortedTest, WorksForEmptyContainer) {
1284 const vector<int> numbers;
1286 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
1289 TEST(WhenSortedTest, WorksForNonEmptyContainer) {
1290 list<std::string> words;
1291 words.push_back(
"3");
1292 words.push_back(
"1");
1293 words.push_back(
"2");
1294 words.push_back(
"2");
1295 EXPECT_THAT(words, WhenSorted(ElementsAre(
"1",
"2",
"2",
"3")));
1296 EXPECT_THAT(words, Not(WhenSorted(ElementsAre(
"3",
"1",
"2",
"2"))));
1299 TEST(WhenSortedTest, WorksForMapTypes) {
1300 map<std::string, int> word_counts;
1301 word_counts[
"and"] = 1;
1302 word_counts[
"the"] = 1;
1303 word_counts[
"buffalo"] = 2;
1305 WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"buffalo", 2),
1308 Not(WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"the", 1),
1309 Pair(
"buffalo", 2)))));
1312 TEST(WhenSortedTest, WorksForMultiMapTypes) {
1313 multimap<int, int> ifib;
1314 ifib.insert(make_pair(8, 6));
1315 ifib.insert(make_pair(2, 3));
1316 ifib.insert(make_pair(1, 1));
1317 ifib.insert(make_pair(3, 4));
1318 ifib.insert(make_pair(1, 2));
1319 ifib.insert(make_pair(5, 5));
1321 WhenSorted(ElementsAre(Pair(1, 1), Pair(1, 2), Pair(2, 3),
1322 Pair(3, 4), Pair(5, 5), Pair(8, 6))));
1324 Not(WhenSorted(ElementsAre(Pair(8, 6), Pair(2, 3), Pair(1, 1),
1325 Pair(3, 4), Pair(1, 2), Pair(5, 5)))));
1328 TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
1333 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
1336 TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
1340 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
1342 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
1343 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
1348 template <
typename T>
1354 typedef ConstIter const_iterator;
1355 typedef T value_type;
1357 template <
typename InIter>
1358 Streamlike(InIter first, InIter last) :
remainder_(first, last) {}
1360 const_iterator begin()
const {
1361 return const_iterator(
this,
remainder_.begin());
1363 const_iterator end()
const {
return const_iterator(
this,
remainder_.end()); }
1368 using iterator_category = std::input_iterator_tag;
1369 using value_type =
T;
1370 using difference_type = ptrdiff_t;
1371 using pointer =
const value_type*;
1372 using reference =
const value_type&;
1374 ConstIter(
const Streamlike* s,
typename std::list<value_type>::iterator pos)
1378 const value_type* operator->()
const {
return &*
pos_; }
1379 ConstIter& operator++() {
1380 s_->remainder_.erase(
pos_++);
1386 class PostIncrProxy {
1388 explicit PostIncrProxy(
const value_type&
value) :
value_(value) {}
1394 PostIncrProxy operator++(
int) {
1395 PostIncrProxy proxy(**
this);
1400 friend bool operator==(
const ConstIter& a,
const ConstIter& b) {
1401 return a.s_ == b.s_ && a.pos_ == b.pos_;
1403 friend bool operator!=(
const ConstIter& a,
const ConstIter& b) {
1408 const Streamlike*
s_;
1409 typename std::list<value_type>::iterator
pos_;
1412 friend std::ostream&
operator<<(std::ostream& os,
const Streamlike& s) {
1414 typedef typename std::list<value_type>::const_iterator Iter;
1415 const char* sep =
"";
1416 for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
1427 TEST(StreamlikeTest, Iteration) {
1428 const int a[5] = {2, 1, 4, 5, 3};
1429 Streamlike<int> s(a, a + 5);
1430 Streamlike<int>::const_iterator it = s.begin();
1432 while (it != s.end()) {
1440 TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
1441 std::forward_list<int> container;
1443 EXPECT_THAT(container, Not(BeginEndDistanceIs(1)));
1444 container.push_front(0);
1445 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
1447 container.push_front(0);
1448 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
1452 TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
1453 const int a[5] = {1, 2, 3, 4, 5};
1454 Streamlike<int> s(a, a + 5);
1458 TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
1459 Matcher<vector<int>> m = BeginEndDistanceIs(2);
1461 EXPECT_EQ(
"distance between begin() and end() isn't equal to 2",
1465 TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
1466 ContainerHelper helper;
1468 helper.Call(MakeUniquePtrs({1, 2}));
1471 TEST_P(BeginEndDistanceIsTestP, ExplainsResult) {
1472 Matcher<vector<int>> m1 = BeginEndDistanceIs(2);
1473 Matcher<vector<int>> m2 = BeginEndDistanceIs(Lt(2));
1474 Matcher<vector<int>> m3 = BeginEndDistanceIs(AnyOf(0, 3));
1475 Matcher<vector<int>> m4 = BeginEndDistanceIs(GreaterThan(1));
1476 vector<int> container;
1477 EXPECT_EQ(
"whose distance between begin() and end() 0 doesn't match",
1479 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
1482 "whose distance between begin() and end() 0 matches, which matches (is "
1486 "whose distance between begin() and end() 0 doesn't match, which is 1 "
1489 container.push_back(0);
1490 container.push_back(0);
1491 EXPECT_EQ(
"whose distance between begin() and end() 2 matches",
1493 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
1496 "whose distance between begin() and end() 2 doesn't match, isn't equal "
1497 "to 0, and isn't equal to 3",
1500 "whose distance between begin() and end() 2 matches, which is 1 more "
1505 TEST(WhenSortedTest, WorksForStreamlike) {
1508 const int a[5] = {2, 1, 4, 5, 3};
1509 Streamlike<int> s(std::begin(a), std::end(a));
1510 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
1511 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
1514 TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
1515 const int a[] = {2, 1, 4, 5, 3};
1516 Streamlike<int> s(std::begin(a), std::end(a));
1517 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
1519 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
1522 TEST(IsSupersetOfTest, WorksForNativeArray) {
1523 const int subset[] = {1, 4};
1524 const int superset[] = {1, 2, 4};
1525 const int disjoint[] = {1, 0, 3};
1533 TEST(IsSupersetOfTest, WorksWithDuplicates) {
1534 const int not_enough[] = {1, 2};
1535 const int enough[] = {1, 1, 2};
1537 EXPECT_THAT(not_enough, Not(IsSupersetOf(expected)));
1541 TEST(IsSupersetOfTest, WorksForEmpty) {
1542 vector<int> numbers;
1545 expected.push_back(1);
1546 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
1548 numbers.push_back(1);
1549 numbers.push_back(2);
1551 expected.push_back(1);
1553 expected.push_back(2);
1555 expected.push_back(3);
1556 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
1559 TEST(IsSupersetOfTest, WorksForStreamlike) {
1560 const int a[5] = {1, 2, 3, 4, 5};
1561 Streamlike<int> s(std::begin(a), std::end(a));
1564 expected.push_back(1);
1565 expected.push_back(2);
1566 expected.push_back(5);
1569 expected.push_back(0);
1573 TEST(IsSupersetOfTest, TakesStlContainer) {
1574 const int actual[] = {3, 1, 2};
1577 expected.push_back(1);
1578 expected.push_back(3);
1581 expected.push_back(4);
1586 typedef std::vector<int> IntVec;
1588 expected.push_back(111);
1589 expected.push_back(222);
1590 expected.push_back(333);
1592 Describe<IntVec>(IsSupersetOf(expected)),
1593 Eq(
"a surjection from elements to requirements exists such that:\n"
1594 " - an element is equal to 111\n"
1595 " - an element is equal to 222\n"
1596 " - an element is equal to 333"));
1600 typedef std::vector<int> IntVec;
1602 expected.push_back(111);
1603 expected.push_back(222);
1604 expected.push_back(333);
1606 DescribeNegation<IntVec>(IsSupersetOf(expected)),
1607 Eq(
"no surjection from elements to requirements exists such that:\n"
1608 " - an element is equal to 111\n"
1609 " - an element is equal to 222\n"
1610 " - an element is equal to 333"));
1613 TEST(IsSupersetOfTest, MatchAndExplain) {
1618 expected.push_back(1);
1619 expected.push_back(2);
1620 StringMatchResultListener listener;
1621 ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
1624 Eq(
"where the following matchers don't match any elements:\n"
1625 "matcher #0: is equal to 1"));
1629 ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
1632 " - element #0 is matched by matcher #1,\n"
1633 " - element #2 is matched by matcher #0"));
1636 TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
1637 const int numbers[] = {1, 3, 6, 2, 4, 5};
1642 TEST(IsSupersetOfTest, WorksWithMoveOnly) {
1643 ContainerHelper helper;
1644 EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));
1645 helper.Call(MakeUniquePtrs({1, 2}));
1646 EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));
1647 helper.Call(MakeUniquePtrs({2}));
1650 TEST(IsSubsetOfTest, WorksForNativeArray) {
1651 const int subset[] = {1, 4};
1652 const int superset[] = {1, 2, 4};
1653 const int disjoint[] = {1, 0, 3};
1661 TEST(IsSubsetOfTest, WorksWithDuplicates) {
1662 const int not_enough[] = {1, 2};
1663 const int enough[] = {1, 1, 2};
1664 const int actual[] = {1, 1};
1669 TEST(IsSubsetOfTest, WorksForEmpty) {
1670 vector<int> numbers;
1673 expected.push_back(1);
1676 numbers.push_back(1);
1677 numbers.push_back(2);
1679 expected.push_back(1);
1681 expected.push_back(2);
1683 expected.push_back(3);
1687 TEST(IsSubsetOfTest, WorksForStreamlike) {
1688 const int a[5] = {1, 2};
1689 Streamlike<int> s(std::begin(a), std::end(a));
1692 expected.push_back(1);
1694 expected.push_back(2);
1695 expected.push_back(5);
1699 TEST(IsSubsetOfTest, TakesStlContainer) {
1700 const int actual[] = {3, 1, 2};
1703 expected.push_back(1);
1704 expected.push_back(3);
1707 expected.push_back(2);
1708 expected.push_back(4);
1713 typedef std::vector<int> IntVec;
1715 expected.push_back(111);
1716 expected.push_back(222);
1717 expected.push_back(333);
1720 Describe<IntVec>(IsSubsetOf(expected)),
1721 Eq(
"an injection from elements to requirements exists such that:\n"
1722 " - an element is equal to 111\n"
1723 " - an element is equal to 222\n"
1724 " - an element is equal to 333"));
1728 typedef std::vector<int> IntVec;
1730 expected.push_back(111);
1731 expected.push_back(222);
1732 expected.push_back(333);
1734 DescribeNegation<IntVec>(IsSubsetOf(expected)),
1735 Eq(
"no injection from elements to requirements exists such that:\n"
1736 " - an element is equal to 111\n"
1737 " - an element is equal to 222\n"
1738 " - an element is equal to 333"));
1741 TEST(IsSubsetOfTest, MatchAndExplain) {
1746 expected.push_back(1);
1747 expected.push_back(2);
1748 StringMatchResultListener listener;
1749 ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
1752 Eq(
"where the following elements don't match any matchers:\n"
1755 expected.push_back(3);
1757 ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
1760 " - element #0 is matched by matcher #1,\n"
1761 " - element #1 is matched by matcher #2"));
1764 TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
1765 const int numbers[] = {1, 2, 3};
1770 TEST(IsSubsetOfTest, WorksWithMoveOnly) {
1771 ContainerHelper helper;
1772 EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));
1773 helper.Call(MakeUniquePtrs({1}));
1774 EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));
1775 helper.Call(MakeUniquePtrs({2}));
1781 TEST(ElemensAreStreamTest, WorksForStreamlike) {
1782 const int a[5] = {1, 2, 3, 4, 5};
1783 Streamlike<int> s(std::begin(a), std::end(a));
1788 TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
1789 const int a[5] = {1, 2, 3, 4, 5};
1790 Streamlike<int> s(std::begin(a), std::end(a));
1793 expected.push_back(1);
1794 expected.push_back(2);
1795 expected.push_back(3);
1796 expected.push_back(4);
1797 expected.push_back(5);
1804 TEST(ElementsAreTest, WorksWithUncopyable) {
1806 objs[0].set_value(-3);
1807 objs[1].set_value(1);
1808 EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));
1811 TEST(ElementsAreTest, WorksWithMoveOnly) {
1812 ContainerHelper helper;
1813 EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));
1814 helper.Call(MakeUniquePtrs({1, 2}));
1816 EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));
1817 helper.Call(MakeUniquePtrs({3, 4}));
1820 TEST(ElementsAreTest, TakesStlContainer) {
1821 const int actual[] = {3, 1, 2};
1824 expected.push_back(3);
1825 expected.push_back(1);
1826 expected.push_back(2);
1829 expected.push_back(4);
1830 EXPECT_THAT(actual, Not(ElementsAreArray(expected)));
1835 TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
1836 const int a[] = {0, 1, 2, 3, 4};
1837 std::vector<int> s(std::begin(a), std::end(a));
1839 StringMatchResultListener listener;
1840 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a), s, &listener))
1842 }
while (std::next_permutation(s.begin(), s.end()));
1845 TEST(UnorderedElementsAreArrayTest, VectorBool) {
1846 const bool a[] = {
false,
true,
false,
true,
true};
1847 const bool b[] = {
true,
false,
true,
true,
false};
1848 std::vector<bool>
expected(std::begin(a), std::end(a));
1849 std::vector<bool> actual(std::begin(b), std::end(b));
1850 StringMatchResultListener listener;
1851 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected), actual,
1856 TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
1860 const int a[5] = {2, 1, 4, 5, 3};
1861 Streamlike<int> s(std::begin(a), std::end(a));
1864 expected.push_back(1);
1865 expected.push_back(2);
1866 expected.push_back(3);
1867 expected.push_back(4);
1868 expected.push_back(5);
1869 EXPECT_THAT(s, UnorderedElementsAreArray(expected));
1871 expected.push_back(6);
1872 EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));
1875 TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
1876 const int actual[] = {3, 1, 2};
1879 expected.push_back(1);
1880 expected.push_back(2);
1881 expected.push_back(3);
1882 EXPECT_THAT(actual, UnorderedElementsAreArray(expected));
1884 expected.push_back(4);
1885 EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected)));
1888 TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
1889 const int a[5] = {2, 1, 4, 5, 3};
1890 EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5}));
1891 EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6})));
1894 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
1895 const std::string a[5] = {
"a",
"b",
"c",
"d",
"e"};
1896 EXPECT_THAT(a, UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
1897 EXPECT_THAT(a, Not(UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
1900 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
1901 const int a[5] = {2, 1, 4, 5, 3};
1903 UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
1905 a, Not(UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
1908 TEST(UnorderedElementsAreArrayTest,
1909 TakesInitializerListOfDifferentTypedMatchers) {
1910 const int a[5] = {2, 1, 4, 5, 3};
1914 EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int>>(
1915 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
1916 EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int>>(
1917 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
1920 TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
1921 ContainerHelper helper;
1923 Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)})));
1924 helper.Call(MakeUniquePtrs({2, 1}));
1929 typedef std::vector<int> IntVec;
1932 TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
1934 objs[0].set_value(-3);
1935 objs[1].set_value(1);
1937 UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3)));
1940 TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
1941 const int a[] = {1, 2, 3};
1942 std::vector<int> s(std::begin(a), std::end(a));
1944 StringMatchResultListener listener;
1945 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), s, &listener))
1947 }
while (std::next_permutation(s.begin(), s.end()));
1950 TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
1951 const int a[] = {1, 2, 3};
1952 std::vector<int> s(std::begin(a), std::end(a));
1953 std::vector<Matcher<int>> mv;
1958 StringMatchResultListener listener;
1959 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
1963 TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
1967 const int a[5] = {2, 1, 4, 5, 3};
1968 Streamlike<int> s(std::begin(a), std::end(a));
1970 EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
1971 EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
1974 TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
1975 ContainerHelper helper;
1976 EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2))));
1977 helper.Call(MakeUniquePtrs({2, 1}));
1986 TEST_F(UnorderedElementsAreTest, Performance) {
1988 std::vector<Matcher<int>> mv;
1989 for (
int i = 0;
i < 100; ++
i) {
1994 StringMatchResultListener listener;
1995 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
2002 TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
2004 std::vector<Matcher<int>> mv;
2005 for (
int i = 0;
i < 100; ++
i) {
2013 StringMatchResultListener listener;
2014 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
2018 TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
2021 StringMatchResultListener listener;
2022 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
2025 Eq(
"which has 1 element\n"
2026 "where the following matchers don't match any elements:\n"
2027 "matcher #0: is equal to 1,\n"
2028 "matcher #1: is equal to 2,\n"
2029 "matcher #2: is equal to 3\n"
2030 "and where the following elements don't match any matchers:\n"
2034 TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
2036 StringMatchResultListener listener;
2037 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
2040 Eq(
"where the following matchers don't match any elements:\n"
2041 "matcher #0: is equal to 1,\n"
2042 "matcher #1: is equal to 2,\n"
2043 "matcher #2: is equal to 3"));
2046 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
2050 StringMatchResultListener listener;
2051 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))
2054 Eq(
"where the following matchers don't match any elements:\n"
2055 "matcher #1: is equal to 2"));
2058 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
2062 StringMatchResultListener listener;
2063 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1), v, &listener))
2066 Eq(
"where the following elements don't match any matchers:\n"
2070 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
2074 StringMatchResultListener listener;
2075 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))
2079 " the following matchers don't match any elements:\n"
2080 "matcher #0: is equal to 1\n"
2083 " the following elements don't match any matchers:\n"
2088 static std::string EMString(
int element,
int matcher) {
2090 ss <<
"(element #" << element <<
", matcher #" << matcher <<
")";
2094 TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
2097 std::vector<std::string> v;
2101 StringMatchResultListener listener;
2103 UnorderedElementsAre(
"a",
"a", AnyOf(
"b",
"c")), v, &listener))
2106 std::string prefix =
2107 "where no permutation of the elements can satisfy all matchers, "
2108 "and the closest match is 2 of 3 matchers with the "
2115 prefix +
"{\n " + EMString(0, 0) +
",\n " + EMString(1, 2) +
"\n}",
2116 prefix +
"{\n " + EMString(0, 1) +
",\n " + EMString(1, 2) +
"\n}",
2117 prefix +
"{\n " + EMString(0, 0) +
",\n " + EMString(2, 2) +
"\n}",
2118 prefix +
"{\n " + EMString(0, 1) +
",\n " + EMString(2, 2) +
2123 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()), Eq(
"is empty"));
2124 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre(345)),
2125 Eq(
"has 1 element and that element is equal to 345"));
2126 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre(111, 222, 333)),
2127 Eq(
"has 3 elements and there exists some permutation "
2128 "of elements such that:\n"
2129 " - element #0 is equal to 111, and\n"
2130 " - element #1 is equal to 222, and\n"
2131 " - element #2 is equal to 333"));
2135 EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()),
2138 DescribeNegation<IntVec>(UnorderedElementsAre(345)),
2139 Eq(
"doesn't have 1 element, or has 1 element that isn't equal to 345"));
2140 EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)),
2141 Eq(
"doesn't have 3 elements, or there exists no permutation "
2142 "of elements such that:\n"
2143 " - element #0 is equal to 123, and\n"
2144 " - element #1 is equal to 234, and\n"
2145 " - element #2 is equal to 345"));
2152 TEST_P(EachTestP, ExplainsMatchResultCorrectly) {
2155 Matcher<set<int>> m = Each(2);
2158 Matcher<const int(&)[1]> n = Each(1);
2160 const int b[1] = {1};
2169 m = Each(GreaterThan(0));
2172 m = Each(GreaterThan(10));
2173 EXPECT_EQ(
"whose element #0 doesn't match, which is 9 less than 10",
2177 TEST(EachTest, DescribesItselfCorrectly) {
2178 Matcher<vector<int>> m = Each(1);
2181 Matcher<vector<int>> m2 = Not(m);
2185 TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
2186 vector<int> some_vector;
2188 some_vector.push_back(3);
2191 some_vector.push_back(1);
2192 some_vector.push_back(2);
2196 vector<std::string> another_vector;
2197 another_vector.push_back(
"fee");
2198 EXPECT_THAT(another_vector, Each(std::string(
"fee")));
2199 another_vector.push_back(
"fie");
2200 another_vector.push_back(
"foe");
2201 another_vector.push_back(
"fum");
2202 EXPECT_THAT(another_vector, Not(Each(std::string(
"fee"))));
2205 TEST(EachTest, MatchesMapWhenAllElementsMatch) {
2206 map<const char*, int> my_map;
2207 const char*
bar =
"a string";
2211 map<std::string, int> another_map;
2212 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
2213 another_map[
"fee"] = 1;
2214 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
2215 another_map[
"fie"] = 2;
2216 another_map[
"foe"] = 3;
2217 another_map[
"fum"] = 4;
2218 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fee"), 1))));
2219 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fum"), 1))));
2223 TEST(EachTest, AcceptsMatcher) {
2224 const int a[] = {1, 2, 3};
2229 TEST(EachTest, WorksForNativeArrayAsTuple) {
2230 const int a[] = {1, 2};
2231 const int*
const pointer =
a;
2232 EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0)));
2233 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1))));
2236 TEST(EachTest, WorksWithMoveOnly) {
2237 ContainerHelper helper;
2239 helper.Call(MakeUniquePtrs({1, 2}));
2243 class IsHalfOfMatcher {
2245 template <
typename T1,
typename T2>
2246 bool MatchAndExplain(
const std::tuple<T1, T2>& a_pair,
2247 MatchResultListener* listener)
const {
2248 if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
2249 *listener <<
"where the second is " << std::get<1>(a_pair);
2252 *listener <<
"where the second/2 is " << std::get<1>(a_pair) / 2;
2257 void DescribeTo(ostream* os)
const {
2258 *os <<
"are a pair where the first is half of the second";
2261 void DescribeNegationTo(ostream* os)
const {
2262 *os <<
"are a pair where the first isn't half of the second";
2266 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
2267 return MakePolymorphicMatcher(IsHalfOfMatcher());
2270 TEST(PointwiseTest, DescribesSelf) {
2275 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
2277 "contains 3 values, where each value and its corresponding value "
2278 "in { 1, 2, 3 } are a pair where the first is half of the second",
2281 "doesn't contain exactly 3 values, or contains a value x at some "
2282 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
2283 "where the first isn't half of the second",
2287 TEST(PointwiseTest, MakesCopyOfRhs) {
2288 list<signed char> rhs;
2293 const Matcher<const int(&)[2]> m = Pointwise(IsHalfOf(), rhs);
2301 TEST(PointwiseTest, WorksForLhsNativeArray) {
2302 const int lhs[] = {1, 2, 3};
2311 TEST(PointwiseTest, WorksForRhsNativeArray) {
2312 const int rhs[] = {1, 2, 3};
2322 TEST(PointwiseTest, WorksForVectorOfBool) {
2323 vector<bool> rhs(3,
false);
2325 vector<bool> lhs = rhs;
2331 TEST(PointwiseTest, WorksForRhsInitializerList) {
2332 const vector<int> lhs{2, 4, 6};
2334 EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));
2337 TEST(PointwiseTest, RejectsWrongSize) {
2338 const double lhs[2] = {1, 2};
2339 const int rhs[1] = {0};
2343 const int rhs2[3] = {0, 1, 2};
2347 TEST(PointwiseTest, RejectsWrongContent) {
2348 const double lhs[3] = {1, 2, 3};
2349 const int rhs[3] = {2, 6, 4};
2350 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
2352 "where the value pair (2, 6) at index #1 don't match, "
2353 "where the second/2 is 3",
2354 Explain(Pointwise(IsHalfOf(), rhs), lhs));
2357 TEST(PointwiseTest, AcceptsCorrectContent) {
2358 const double lhs[3] = {1, 2, 3};
2359 const int rhs[3] = {2, 4, 6};
2364 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
2365 const double lhs[3] = {1, 2, 3};
2366 const int rhs[3] = {2, 4, 6};
2367 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
2373 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
2378 MATCHER(PointeeEquals,
"Points to an equal value") {
2379 return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),
2380 ::testing::get<0>(arg), result_listener);
2383 TEST(PointwiseTest, WorksWithMoveOnly) {
2384 ContainerHelper helper;
2385 EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));
2386 helper.Call(MakeUniquePtrs({1, 2}));
2389 TEST(UnorderedPointwiseTest, DescribesSelf) {
2394 const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs);
2396 "has 3 elements and there exists some permutation of elements such "
2398 " - element #0 and 1 are a pair where the first is half of the second, "
2400 " - element #1 and 2 are a pair where the first is half of the second, "
2402 " - element #2 and 3 are a pair where the first is half of the second",
2405 "doesn't have 3 elements, or there exists no permutation of elements "
2407 " - element #0 and 1 are a pair where the first is half of the second, "
2409 " - element #1 and 2 are a pair where the first is half of the second, "
2411 " - element #2 and 3 are a pair where the first is half of the second",
2415 TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
2416 list<signed char> rhs;
2421 const Matcher<const int(&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs);
2429 TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
2430 const int lhs[] = {1, 2, 3};
2436 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
2439 TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
2440 const int rhs[] = {1, 2, 3};
2446 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));
2449 TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
2450 const vector<int> lhs{2, 4, 6};
2451 EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3}));
2452 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));
2455 TEST(UnorderedPointwiseTest, RejectsWrongSize) {
2456 const double lhs[2] = {1, 2};
2457 const int rhs[1] = {0};
2458 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
2460 Explain(UnorderedPointwise(Gt(), rhs), lhs));
2462 const int rhs2[3] = {0, 1, 2};
2463 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2)));
2466 TEST(UnorderedPointwiseTest, RejectsWrongContent) {
2467 const double lhs[3] = {1, 2, 3};
2468 const int rhs[3] = {2, 6, 6};
2469 EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs)));
2471 "where the following elements don't match any matchers:\n"
2473 Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs));
2476 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
2477 const double lhs[3] = {1, 2, 3};
2478 const int rhs[3] = {2, 4, 6};
2479 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
2482 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
2483 const double lhs[3] = {1, 2, 3};
2484 const int rhs[3] = {6, 4, 2};
2485 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
2488 TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
2489 const double lhs[3] = {1, 2, 3};
2490 const int rhs[3] = {4, 6, 2};
2491 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
2496 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
2500 TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
2501 ContainerHelper helper;
2502 EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),
2503 std::vector<int>{1, 2})));
2504 helper.Call(MakeUniquePtrs({2, 1}));
2507 TEST(PointeeTest, WorksOnMoveOnlyType) {
2508 std::unique_ptr<int>
p(
new int(3));
2515 enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
2520 class MockMatcher :
public MatcherInterface<Behavior> {
2522 bool MatchAndExplain(Behavior behavior,
2523 MatchResultListener* listener)
const override {
2524 *listener <<
"[MatchAndExplain]";
2526 case kInitialSuccess:
2530 return !listener->IsInterested();
2540 return listener->IsInterested();
2543 GTEST_LOG_(FATAL) <<
"This should never be reached";
2547 void DescribeTo(ostream* os)
const override { *os <<
"[DescribeTo]"; }
2549 void DescribeNegationTo(ostream* os)
const override {
2550 *os <<
"[DescribeNegationTo]";
2554 AssertionResult RunPredicateFormatter(Behavior behavior) {
2555 auto matcher = MakeMatcher(
new MockMatcher);
2556 PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
2558 return predicate_formatter(
"dummy-name", behavior);
2562 TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
2563 AssertionResult result = RunPredicateFormatter(kInitialSuccess);
2569 TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
2570 AssertionResult result = RunPredicateFormatter(kAlwaysFail);
2572 std::string expect =
2573 "Value of: dummy-name\nExpected: [DescribeTo]\n"
2575 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
2579 TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
2580 AssertionResult result = RunPredicateFormatter(kFlaky);
2582 std::string expect =
2583 "Value of: dummy-name\nExpected: [DescribeTo]\n"
2584 " The matcher failed on the initial attempt; but passed when rerun to "
2585 "generate the explanation.\n"
2587 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
2593 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
2594 Matcher<const vector<int>&> m = ElementsAre();
2598 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
2599 Matcher<vector<int>> m = ElementsAre(Gt(5));
2603 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
2604 Matcher<list<std::string>> m = ElementsAre(StrEq(
"one"),
"two");
2606 "has 2 elements where\n"
2607 "element #0 is equal to \"one\",\n"
2608 "element #1 is equal to \"two\"",
2612 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
2613 Matcher<vector<int>> m = ElementsAre();
2617 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElement) {
2618 Matcher<const list<int>&> m = ElementsAre(Gt(5));
2620 "doesn't have 1 element, or\n"
2621 "element #0 isn't > 5",
2625 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
2626 Matcher<const list<std::string>&> m = ElementsAre(
"one",
"two");
2628 "doesn't have 2 elements, or\n"
2629 "element #0 isn't equal to \"one\", or\n"
2630 "element #1 isn't equal to \"two\"",
2634 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
2635 Matcher<const list<int>&> m = ElementsAre(1, Ne(2));
2637 list<int> test_list;
2638 test_list.push_back(1);
2639 test_list.push_back(3);
2643 TEST_P(ElementsAreTestP, ExplainsNonTrivialMatch) {
2644 Matcher<const vector<int>&> m =
2645 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
2647 const int a[] = {10, 0, 100};
2648 vector<int> test_vector(std::begin(a), std::end(a));
2650 "whose element #0 matches, which is 9 more than 1,\n"
2651 "and whose element #2 matches, which is 98 more than 2",
2655 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
2656 Matcher<const list<int>&> m = ElementsAre(1, 3);
2658 list<int> test_list;
2662 test_list.push_back(1);
2666 TEST_P(ElementsAreTestP, CanExplainMismatchRightSize) {
2667 Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));
2675 EXPECT_EQ(
"whose element #1 doesn't match, which is 4 less than 5",
2679 TEST(ElementsAreTest, MatchesOneElementVector) {
2680 vector<std::string> test_vector;
2681 test_vector.push_back(
"test string");
2683 EXPECT_THAT(test_vector, ElementsAre(StrEq(
"test string")));
2686 TEST(ElementsAreTest, MatchesOneElementList) {
2687 list<std::string> test_list;
2688 test_list.push_back(
"test string");
2690 EXPECT_THAT(test_list, ElementsAre(
"test string"));
2693 TEST(ElementsAreTest, MatchesThreeElementVector) {
2694 vector<std::string> test_vector;
2695 test_vector.push_back(
"one");
2696 test_vector.push_back(
"two");
2697 test_vector.push_back(
"three");
2699 EXPECT_THAT(test_vector, ElementsAre(
"one", StrEq(
"two"),
_));
2702 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
2703 vector<int> test_vector;
2704 test_vector.push_back(4);
2709 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
2710 vector<int> test_vector;
2711 test_vector.push_back(4);
2716 TEST(ElementsAreTest, MatchesOneElementValue) {
2717 vector<int> test_vector;
2718 test_vector.push_back(4);
2723 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
2724 vector<int> test_vector;
2725 test_vector.push_back(1);
2726 test_vector.push_back(2);
2727 test_vector.push_back(3);
2732 TEST(ElementsAreTest, MatchesTenElementVector) {
2733 const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
2734 vector<int> test_vector(std::begin(a), std::end(a));
2739 ElementsAre(0, Ge(0),
_, 3, 4, Ne(2), Eq(6), 7, 8,
_));
2742 TEST(ElementsAreTest, DoesNotMatchWrongSize) {
2743 vector<std::string> test_vector;
2744 test_vector.push_back(
"test string");
2745 test_vector.push_back(
"test string");
2747 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
2751 TEST(ElementsAreTest, DoesNotMatchWrongValue) {
2752 vector<std::string> test_vector;
2753 test_vector.push_back(
"other string");
2755 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
2759 TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
2760 vector<std::string> test_vector;
2761 test_vector.push_back(
"one");
2762 test_vector.push_back(
"three");
2763 test_vector.push_back(
"two");
2765 Matcher<vector<std::string>> m =
2766 ElementsAre(StrEq(
"one"), StrEq(
"two"), StrEq(
"three"));
2770 TEST(ElementsAreTest, WorksForNestedContainer) {
2771 constexpr std::array<const char*, 2> strings = {{
"Hi",
"world"}};
2773 vector<list<char>> nested;
2774 for (
const auto& s : strings) {
2775 nested.emplace_back(s, s + strlen(s));
2778 EXPECT_THAT(nested, ElementsAre(ElementsAre(
'H', Ne(
'e')),
2779 ElementsAre(
'w',
'o',
_,
_,
'd')));
2780 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre(
'H',
'e'),
2781 ElementsAre(
'w',
'o',
_,
_,
'd'))));
2784 TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
2785 int a[] = {0, 1, 2};
2786 vector<int> v(std::begin(a), std::end(a));
2788 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
2789 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
2792 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
2793 int a[] = {0, 1, 2};
2794 vector<int> v(std::begin(a), std::end(a));
2800 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
2801 int array[] = {0, 1, 2};
2807 class NativeArrayPassedAsPointerAndSize {
2809 NativeArrayPassedAsPointerAndSize() =
default;
2811 MOCK_METHOD(
void, Helper, (
int* array,
int size));
2814 NativeArrayPassedAsPointerAndSize(
const NativeArrayPassedAsPointerAndSize&) =
2816 NativeArrayPassedAsPointerAndSize& operator=(
2817 const NativeArrayPassedAsPointerAndSize&) =
delete;
2820 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
2821 int array[] = {0, 1};
2822 ::std::tuple<int*, size_t> array_as_tuple(array, 2);
2826 NativeArrayPassedAsPointerAndSize helper;
2828 helper.Helper(array, 2);
2831 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
2832 const char a2[][3] = {
"hi",
"lo"};
2833 EXPECT_THAT(a2, ElementsAre(ElementsAre(
'h',
'i',
'\0'),
2834 ElementsAre(
'l',
'o',
'\0')));
2835 EXPECT_THAT(a2, ElementsAre(StrEq(
"hi"), StrEq(
"lo")));
2836 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre(
'h',
'o',
'\0')),
2837 ElementsAre(
'l',
'o',
'\0')));
2840 TEST(ElementsAreTest, AcceptsStringLiteral) {
2841 std::string array[] = {
"hi",
"one",
"two"};
2842 EXPECT_THAT(array, ElementsAre(
"hi",
"one",
"two"));
2843 EXPECT_THAT(array, Not(ElementsAre(
"hi",
"one",
"too")));
2847 extern const char kHi[];
2849 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
2853 std::string array1[] = {
"hi"};
2856 std::string array2[] = {
"ho"};
2860 const char kHi[] =
"hi";
2862 TEST(ElementsAreTest, MakesCopyOfArguments) {
2866 ::testing::internal::ElementsAreMatcher<std::tuple<int, int>>
2867 polymorphic_matcher = ElementsAre(x, y);
2870 const int array1[] = {1, 2};
2872 const int array2[] = {0, 0};
2880 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
2881 const int a[] = {1, 2, 3};
2883 vector<int> test_vector(std::begin(a), std::end(a));
2887 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
2890 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
2891 std::array<const char*, 3> a = {{
"one",
"two",
"three"}};
2893 vector<std::string> test_vector(std::begin(a), std::end(a));
2894 EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size()));
2896 const char** p = a.data();
2897 test_vector[0] =
"1";
2898 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, a.size())));
2901 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
2902 const char* a[] = {
"one",
"two",
"three"};
2904 vector<std::string> test_vector(std::begin(a), std::end(a));
2907 test_vector[0] =
"1";
2908 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
2911 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
2912 const Matcher<std::string> kMatcherArray[] = {StrEq(
"one"), StrEq(
"two"),
2915 vector<std::string> test_vector;
2916 test_vector.push_back(
"one");
2917 test_vector.push_back(
"two");
2918 test_vector.push_back(
"three");
2919 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
2921 test_vector.push_back(
"three");
2922 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
2925 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
2926 const int a[] = {1, 2, 3};
2927 vector<int> test_vector(std::begin(a), std::end(a));
2928 const vector<int>
expected(std::begin(a), std::end(a));
2929 EXPECT_THAT(test_vector, ElementsAreArray(expected));
2930 test_vector.push_back(4);
2931 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
2934 TEST(ElementsAreArrayTest, TakesInitializerList) {
2935 const int a[5] = {1, 2, 3, 4, 5};
2936 EXPECT_THAT(a, ElementsAreArray({1, 2, 3, 4, 5}));
2937 EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 5, 4})));
2938 EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 4, 6})));
2941 TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
2942 const std::string a[5] = {
"a",
"b",
"c",
"d",
"e"};
2943 EXPECT_THAT(a, ElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
2944 EXPECT_THAT(a, Not(ElementsAreArray({
"a",
"b",
"c",
"e",
"d"})));
2945 EXPECT_THAT(a, Not(ElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
2948 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
2949 const int a[5] = {1, 2, 3, 4, 5};
2950 EXPECT_THAT(a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
2951 EXPECT_THAT(a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
2954 TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) {
2955 const int a[5] = {1, 2, 3, 4, 5};
2960 a, ElementsAreArray<Matcher<int>>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
2961 EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int>>(
2962 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
2965 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
2966 const int a[] = {1, 2, 3};
2967 const Matcher<int> kMatchers[] = {Eq(1), Eq(2), Eq(3)};
2968 vector<int> test_vector(std::begin(a), std::end(a));
2969 const vector<Matcher<int>>
expected(std::begin(kMatchers),
2970 std::end(kMatchers));
2971 EXPECT_THAT(test_vector, ElementsAreArray(expected));
2972 test_vector.push_back(4);
2973 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
2976 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
2977 const int a[] = {1, 2, 3};
2978 const vector<int> test_vector(std::begin(a), std::end(a));
2979 const vector<int>
expected(std::begin(a), std::end(a));
2980 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
2982 EXPECT_THAT(test_vector, ElementsAreArray(std::begin(a), std::end(a)));
2984 int*
const null_int =
nullptr;
2985 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
2986 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
2991 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
2992 ::std::string a[] = {
"hi",
"ho"};
2993 ::std::string b[] = {
"hi",
"ho"};
3000 TEST(ElementsAreArrayTest, SourceLifeSpan) {
3001 const int a[] = {1, 2, 3};
3002 vector<int> test_vector(std::begin(a), std::end(a));
3003 vector<int> expect(std::begin(a), std::end(a));
3004 ElementsAreArrayMatcher<int> matcher_maker =
3005 ElementsAreArray(expect.begin(), expect.end());
3009 for (
int&
i : expect) {
3013 test_vector.push_back(3);
3021 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
3022 list<int> some_list;
3023 some_list.push_back(3);
3024 some_list.push_back(1);
3025 some_list.push_back(2);
3026 some_list.push_back(3);
3031 list<std::string> another_list;
3032 another_list.push_back(
"fee");
3033 another_list.push_back(
"fie");
3034 another_list.push_back(
"foe");
3035 another_list.push_back(
"fum");
3036 EXPECT_THAT(another_list, Contains(std::string(
"fee")));
3039 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
3040 list<int> some_list;
3041 some_list.push_back(3);
3042 some_list.push_back(1);
3046 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
3055 set<std::string> another_set;
3056 another_set.insert(
"fee");
3057 another_set.insert(
"fie");
3058 another_set.insert(
"foe");
3059 another_set.insert(
"fum");
3060 EXPECT_THAT(another_set, Contains(Eq(std::string(
"fum"))));
3063 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
3069 set<std::string> c_string_set;
3070 c_string_set.insert(
"hello");
3071 EXPECT_THAT(c_string_set, Not(Contains(std::string(
"goodbye"))));
3074 TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) {
3075 const int a[2] = {1, 2};
3076 Matcher<const int(&)[2]> m = Contains(2);
3082 m = Contains(GreaterThan(0));
3083 EXPECT_EQ(
"whose element #0 matches, which is 1 more than 0",
Explain(m, a));
3085 m = Contains(GreaterThan(10));
3089 TEST(ContainsTest, DescribesItselfCorrectly) {
3090 Matcher<vector<int>> m = Contains(1);
3093 Matcher<vector<int>> m2 = Not(m);
3097 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
3098 map<std::string, int> my_map;
3099 const char* bar =
"a string";
3101 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
3103 map<std::string, int> another_map;
3104 another_map[
"fee"] = 1;
3105 another_map[
"fie"] = 2;
3106 another_map[
"foe"] = 3;
3107 another_map[
"fum"] = 4;
3109 Contains(pair<const std::string, int>(std::string(
"fee"), 1)));
3110 EXPECT_THAT(another_map, Contains(pair<const std::string, int>(
"fie", 2)));
3113 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
3114 map<int, int> some_map;
3117 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
3120 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
3121 const char* string_array[] = {
"fee",
"fie",
"foe",
"fum"};
3122 EXPECT_THAT(string_array, Contains(Eq(std::string(
"fum"))));
3125 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
3126 int int_array[] = {1, 2, 3, 4};
3130 TEST(ContainsTest, AcceptsMatcher) {
3131 const int a[] = {1, 2, 3};
3136 TEST(ContainsTest, WorksForNativeArrayAsTuple) {
3137 const int a[] = {1, 2};
3138 const int*
const pointer =
a;
3139 EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1));
3140 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3))));
3143 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
3144 int a[][3] = {{1, 2, 3}, {4, 5, 6}};
3147 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
constexpr bool StartsWith(const char(&prefix)[N], const char(&str)[M])
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
GTEST_DISABLE_MSC_WARNINGS_POP_() TEST(LinkTest
#define EXPECT_NONFATAL_FAILURE(statement, substr)
std::string Explain(const MatcherType &m, const Value &x)
constexpr bool EndsWith(const char(&suffix)[N], const char(&str)[M])
#define GTEST_LOG_(severity)
#define ASSERT_THAT(value, matcher)
#define TEST_F(test_fixture, test_name)
#define TEST(test_suite_name, test_name)
#define INSTANTIATE_GTEST_MATCHER_TEST_P(TestSuite)
BigUInt< n > operator*(BigUInt< n > const &a, BigUInt< n > const &b)
#define EXPECT_FATAL_FAILURE(statement, substr)
#define MATCHER(name, description)
std::string Describe(const Matcher< T > &m)
bool operator!=(const Allocator< T > &a_t, const Allocator< U > &a_u)
std::ostream & operator<<(std::ostream &os, const Expr< T > &xx)
#define ASSERT_TRUE(condition)
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
#define SCOPED_TRACE(message)
#define EXPECT_THAT(value, matcher)
#define MATCHER_P(name, p0, description)
std::list< value_type >::iterator pos_
#define EXPECT_CALL(obj, call)
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.
std::string DescribeNegation(const Matcher< T > &m)
#define EXPECT_EQ(val1, val2)
#define TEST_P(test_suite_name, test_name)
#define EXPECT_TRUE(condition)
#define EXPECT_FALSE(condition)
#define ASSERT_FALSE(condition)
std::list< value_type > remainder_
static ExpectedAnswer expected[4]