38#pragma warning(disable : 4244)
39#pragma warning(disable : 4100)
42#include "test/gmock-matchers_test.h"
45namespace gmock_matchers_test {
48std::vector<std::unique_ptr<int>> MakeUniquePtrs(
const std::vector<int>& ints) {
49 std::vector<std::unique_ptr<int>> pointers;
50 for (
int i : ints) pointers.emplace_back(
new int(i));
54std::string OfType(
const std::string& type_name) {
56 return IsReadableTypeName(type_name) ?
" (of type " + type_name +
")" :
"";
62TEST(ContainsTest, WorksWithMoveOnly) {
63 ContainerHelper helper;
64 EXPECT_CALL(helper, Call(Contains(Pointee(2))));
65 helper.Call(MakeUniquePtrs({1, 2}));
68INSTANTIATE_GTEST_MATCHER_TEST_P(ElementsAreTest);
71TEST(ElementsAreTest, HugeMatcher) {
72 vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
74 EXPECT_THAT(test_vector,
75 ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7),
76 Eq(8), Eq(9), Eq(10), Gt(1), Eq(12)));
80TEST(ElementsAreTest, HugeMatcherStr) {
81 vector<std::string> test_vector{
82 "literal_string",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
84 EXPECT_THAT(test_vector, UnorderedElementsAre(
"literal_string", _, _, _, _, _,
89TEST(ElementsAreTest, HugeMatcherUnordered) {
90 vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
92 EXPECT_THAT(test_vector, UnorderedElementsAre(
93 Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7),
94 Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));
99TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
100 ASSERT_THAT(5, Ge(2)) <<
"This should succeed.";
101 ASSERT_THAT(
"Foo", EndsWith(
"oo"));
102 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) <<
"This should succeed too.";
103 EXPECT_THAT(
"Hello", StartsWith(
"Hell"));
108TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
111 static unsigned short n;
114 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)),
116 "Expected: is > 10\n"
118 OfType(
"unsigned short"));
120 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))),
122 "Expected: (is <= 7) and (is >= 5)\n"
124 OfType(
"unsigned short"));
129TEST(MatcherAssertionTest, WorksForByRefArguments) {
134 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
135 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
137 "Expected: does not reference the variable @");
139 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
140 "Actual: 0" + OfType(
"int") +
", which is located @");
145TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
146 Matcher<const char*> starts_with_he = StartsWith(
"he");
147 ASSERT_THAT(
"hello", starts_with_he);
149 Matcher<const std::string&> ends_with_ok = EndsWith(
"ok");
150 ASSERT_THAT(
"book", ends_with_ok);
151 const std::string bad =
"bad";
152 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
154 "Expected: ends with \"ok\"\n"
156 Matcher<int> is_greater_than_5 = Gt(5);
157 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
164TEST(PointeeTest, RawPointer) {
165 const Matcher<int*> m = Pointee(Ge(0));
168 EXPECT_TRUE(m.Matches(&n));
170 EXPECT_FALSE(m.Matches(&n));
171 EXPECT_FALSE(m.Matches(
nullptr));
174TEST(PointeeTest, RawPointerToConst) {
175 const Matcher<const double*> m = Pointee(Ge(0));
178 EXPECT_TRUE(m.Matches(&x));
180 EXPECT_FALSE(m.Matches(&x));
181 EXPECT_FALSE(m.Matches(
nullptr));
184TEST(PointeeTest, ReferenceToConstRawPointer) {
185 const Matcher<int* const&> m = Pointee(Ge(0));
188 EXPECT_TRUE(m.Matches(&n));
190 EXPECT_FALSE(m.Matches(&n));
191 EXPECT_FALSE(m.Matches(
nullptr));
194TEST(PointeeTest, ReferenceToNonConstRawPointer) {
195 const Matcher<double*&> m = Pointee(Ge(0));
199 EXPECT_TRUE(m.Matches(p));
201 EXPECT_FALSE(m.Matches(p));
203 EXPECT_FALSE(m.Matches(p));
206TEST(PointeeTest, SmartPointer) {
207 const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0));
209 std::unique_ptr<int> n(
new int(1));
210 EXPECT_TRUE(m.Matches(n));
213TEST(PointeeTest, SmartPointerToConst) {
214 const Matcher<std::unique_ptr<const int>> m = Pointee(Ge(0));
219 std::unique_ptr<const int> n(
new int(1));
220 EXPECT_TRUE(m.Matches(n));
223TEST(PointerTest, RawPointer) {
225 const Matcher<int*> m = Pointer(Eq(&n));
227 EXPECT_TRUE(m.Matches(&n));
230 EXPECT_FALSE(m.Matches(p));
231 EXPECT_FALSE(m.Matches(
nullptr));
234TEST(PointerTest, RawPointerToConst) {
236 const Matcher<const int*> m = Pointer(Eq(&n));
238 EXPECT_TRUE(m.Matches(&n));
241 EXPECT_FALSE(m.Matches(p));
242 EXPECT_FALSE(m.Matches(
nullptr));
245TEST(PointerTest, SmartPointer) {
246 std::unique_ptr<int> n(
new int(10));
247 int* raw_n = n.get();
248 const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n));
250 EXPECT_TRUE(m.Matches(n));
253TEST(PointerTest, SmartPointerToConst) {
254 std::unique_ptr<const int> n(
new int(10));
255 const int* raw_n = n.get();
256 const Matcher<std::unique_ptr<const int>> m = Pointer(Eq(raw_n));
261 std::unique_ptr<const int> p(
new int(10));
262 EXPECT_FALSE(m.Matches(p));
267class ConstPropagatingPtr {
269 typedef T element_type;
271 ConstPropagatingPtr() : val_() {}
272 explicit ConstPropagatingPtr(T* t) : val_(t) {}
273 ConstPropagatingPtr(
const ConstPropagatingPtr& other) : val_(other.val_) {}
275 T* get() {
return val_; }
276 T& operator*() {
return *val_; }
278 const T* get()
const {
return val_; }
279 const T& operator*()
const {
return *val_; }
285INSTANTIATE_GTEST_MATCHER_TEST_P(PointeeTest);
287TEST(PointeeTest, WorksWithConstPropagatingPointers) {
288 const Matcher<ConstPropagatingPtr<int>> m = Pointee(Lt(5));
290 const ConstPropagatingPtr<int> co(&three);
291 ConstPropagatingPtr<int> o(&three);
292 EXPECT_TRUE(m.Matches(o));
293 EXPECT_TRUE(m.Matches(co));
295 EXPECT_FALSE(m.Matches(o));
296 EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
299TEST(PointeeTest, NeverMatchesNull) {
300 const Matcher<const char*> m = Pointee(_);
301 EXPECT_FALSE(m.Matches(
nullptr));
305TEST(PointeeTest, MatchesAgainstAValue) {
306 const Matcher<int*> m = Pointee(5);
309 EXPECT_TRUE(m.Matches(&n));
311 EXPECT_FALSE(m.Matches(&n));
312 EXPECT_FALSE(m.Matches(
nullptr));
315TEST(PointeeTest, CanDescribeSelf) {
316 const Matcher<int*> m = Pointee(Gt(3));
317 EXPECT_EQ(
"points to a value that is > 3", Describe(m));
318 EXPECT_EQ(
"does not point to a value that is > 3", DescribeNegation(m));
321TEST_P(PointeeTestP, CanExplainMatchResult) {
322 const Matcher<const std::string*> m = Pointee(StartsWith(
"Hi"));
324 EXPECT_EQ(
"", Explain(m,
static_cast<const std::string*
>(
nullptr)));
326 const Matcher<long*> m2 = Pointee(GreaterThan(1));
328 EXPECT_EQ(
"which points to 3" + OfType(
"long") +
", which is 2 more than 1",
332TEST(PointeeTest, AlwaysExplainsPointee) {
333 const Matcher<int*> m = Pointee(0);
335 EXPECT_EQ(
"which points to 42" + OfType(
"int"), Explain(m, &n));
341 Uncopyable() : value_(-1) {}
342 explicit Uncopyable(
int a_value) : value_(a_value) {}
344 int value()
const {
return value_; }
345 void set_value(
int i) { value_ = i; }
349 Uncopyable(
const Uncopyable&) =
delete;
350 Uncopyable& operator=(
const Uncopyable&) =
delete;
354bool ValueIsPositive(
const Uncopyable& x) {
return x.value() > 0; }
356MATCHER_P(UncopyableIs, inner_matcher,
"") {
357 return ExplainMatchResult(inner_matcher, arg.value(), result_listener);
362 AStruct() : x(0), y(1.0), z(5), p(nullptr) {}
363 AStruct(
const AStruct& rhs)
364 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
373struct DerivedStruct :
public AStruct {
377INSTANTIATE_GTEST_MATCHER_TEST_P(FieldTest);
380TEST(FieldTest, WorksForNonConstField) {
381 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
382 Matcher<AStruct> m_with_name = Field(
"x", &AStruct::x, Ge(0));
385 EXPECT_TRUE(m.Matches(a));
386 EXPECT_TRUE(m_with_name.Matches(a));
388 EXPECT_FALSE(m.Matches(a));
389 EXPECT_FALSE(m_with_name.Matches(a));
393TEST(FieldTest, WorksForConstField) {
396 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
397 Matcher<AStruct> m_with_name = Field(
"y", &AStruct::y, Ge(0.0));
398 EXPECT_TRUE(m.Matches(a));
399 EXPECT_TRUE(m_with_name.Matches(a));
400 m = Field(&AStruct::y, Le(0.0));
401 m_with_name = Field(
"y", &AStruct::y, Le(0.0));
402 EXPECT_FALSE(m.Matches(a));
403 EXPECT_FALSE(m_with_name.Matches(a));
407TEST(FieldTest, WorksForUncopyableField) {
410 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
411 EXPECT_TRUE(m.Matches(a));
412 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
413 EXPECT_FALSE(m.Matches(a));
417TEST(FieldTest, WorksForPointerField) {
419 Matcher<AStruct> m = Field(&AStruct::p,
static_cast<const char*
>(
nullptr));
421 EXPECT_TRUE(m.Matches(a));
423 EXPECT_FALSE(m.Matches(a));
426 m = Field(&AStruct::p, StartsWith(
"hi"));
428 EXPECT_TRUE(m.Matches(a));
430 EXPECT_FALSE(m.Matches(a));
434TEST(FieldTest, WorksForByRefArgument) {
435 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
438 EXPECT_TRUE(m.Matches(a));
440 EXPECT_FALSE(m.Matches(a));
445TEST(FieldTest, WorksForArgumentOfSubType) {
448 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
451 EXPECT_TRUE(m.Matches(d));
453 EXPECT_FALSE(m.Matches(d));
458TEST(FieldTest, WorksForCompatibleMatcherType) {
460 Matcher<const AStruct&> m = Field(&AStruct::x, Matcher<signed char>(Ge(0)));
463 EXPECT_TRUE(m.Matches(a));
465 EXPECT_FALSE(m.Matches(a));
469TEST(FieldTest, CanDescribeSelf) {
470 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
472 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(m));
473 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(m));
476TEST(FieldTest, CanDescribeSelfWithFieldName) {
477 Matcher<const AStruct&> m = Field(
"field_name", &AStruct::x, Ge(0));
479 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(m));
480 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
481 DescribeNegation(m));
485TEST_P(FieldTestP, CanExplainMatchResult) {
486 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
490 EXPECT_EQ(
"whose given field is 1" + OfType(
"int"), Explain(m, a));
492 m = Field(&AStruct::x, GreaterThan(0));
494 "whose given field is 1" + OfType(
"int") +
", which is 1 more than 0",
498TEST_P(FieldTestP, CanExplainMatchResultWithFieldName) {
499 Matcher<const AStruct&> m = Field(
"field_name", &AStruct::x, Ge(0));
503 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int"), Explain(m, a));
505 m = Field(
"field_name", &AStruct::x, GreaterThan(0));
506 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int") +
507 ", which is 1 more than 0",
511INSTANTIATE_GTEST_MATCHER_TEST_P(FieldForPointerTest);
514TEST(FieldForPointerTest, WorksForPointerToConst) {
515 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
518 EXPECT_TRUE(m.Matches(&a));
520 EXPECT_FALSE(m.Matches(&a));
524TEST(FieldForPointerTest, WorksForPointerToNonConst) {
525 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
528 EXPECT_TRUE(m.Matches(&a));
530 EXPECT_FALSE(m.Matches(&a));
534TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
535 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
538 EXPECT_TRUE(m.Matches(&a));
540 EXPECT_FALSE(m.Matches(&a));
544TEST(FieldForPointerTest, DoesNotMatchNull) {
545 Matcher<const AStruct*> m = Field(&AStruct::x, _);
546 EXPECT_FALSE(m.Matches(
nullptr));
551TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
554 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
557 EXPECT_TRUE(m.Matches(&d));
559 EXPECT_FALSE(m.Matches(&d));
563TEST(FieldForPointerTest, CanDescribeSelf) {
564 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
566 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(m));
567 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(m));
570TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
571 Matcher<const AStruct*> m = Field(
"field_name", &AStruct::x, Ge(0));
573 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(m));
574 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
575 DescribeNegation(m));
579TEST_P(FieldForPointerTestP, CanExplainMatchResult) {
580 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
584 EXPECT_EQ(
"", Explain(m,
static_cast<const AStruct*
>(
nullptr)));
585 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int"),
588 m = Field(&AStruct::x, GreaterThan(0));
589 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int") +
590 ", which is 1 more than 0",
594TEST_P(FieldForPointerTestP, CanExplainMatchResultWithFieldName) {
595 Matcher<const AStruct*> m = Field(
"field_name", &AStruct::x, Ge(0));
599 EXPECT_EQ(
"", Explain(m,
static_cast<const AStruct*
>(
nullptr)));
601 "which points to an object whose field `field_name` is 1" + OfType(
"int"),
604 m = Field(
"field_name", &AStruct::x, GreaterThan(0));
605 EXPECT_EQ(
"which points to an object whose field `field_name` is 1" +
606 OfType(
"int") +
", which is 1 more than 0",
616 int n()
const {
return n_; }
618 void set_n(
int new_n) { n_ = new_n; }
621 const std::string& s()
const {
return s_; }
623 const std::string& s_ref() const& {
return s_; }
625 void set_s(
const std::string& new_s) { s_ = new_s; }
628 double& x()
const {
return x_; }
637double AClass::x_ = 0.0;
640class DerivedClass :
public AClass {
642 int k()
const {
return k_; }
648INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyTest);
652TEST(PropertyTest, WorksForNonReferenceProperty) {
653 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
654 Matcher<const AClass&> m_with_name = Property(
"n", &AClass::n, Ge(0));
658 EXPECT_TRUE(m.Matches(a));
659 EXPECT_TRUE(m_with_name.Matches(a));
662 EXPECT_FALSE(m.Matches(a));
663 EXPECT_FALSE(m_with_name.Matches(a));
668TEST(PropertyTest, WorksForReferenceToConstProperty) {
669 Matcher<const AClass&> m = Property(&AClass::s, StartsWith(
"hi"));
670 Matcher<const AClass&> m_with_name =
671 Property(
"s", &AClass::s, StartsWith(
"hi"));
675 EXPECT_TRUE(m.Matches(a));
676 EXPECT_TRUE(m_with_name.Matches(a));
679 EXPECT_FALSE(m.Matches(a));
680 EXPECT_FALSE(m_with_name.Matches(a));
685TEST(PropertyTest, WorksForRefQualifiedProperty) {
686 Matcher<const AClass&> m = Property(&AClass::s_ref, StartsWith(
"hi"));
687 Matcher<const AClass&> m_with_name =
688 Property(
"s", &AClass::s_ref, StartsWith(
"hi"));
692 EXPECT_TRUE(m.Matches(a));
693 EXPECT_TRUE(m_with_name.Matches(a));
696 EXPECT_FALSE(m.Matches(a));
697 EXPECT_FALSE(m_with_name.Matches(a));
702TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
706 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
707 EXPECT_FALSE(m.Matches(a));
709 m = Property(&AClass::x, Not(Ref(x)));
710 EXPECT_TRUE(m.Matches(a));
715TEST(PropertyTest, WorksForByValueArgument) {
716 Matcher<AClass> m = Property(&AClass::s, StartsWith(
"hi"));
720 EXPECT_TRUE(m.Matches(a));
723 EXPECT_FALSE(m.Matches(a));
728TEST(PropertyTest, WorksForArgumentOfSubType) {
731 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
735 EXPECT_TRUE(m.Matches(d));
738 EXPECT_FALSE(m.Matches(d));
743TEST(PropertyTest, WorksForCompatibleMatcherType) {
745 Matcher<const AClass&> m = Property(&AClass::n, Matcher<signed char>(Ge(0)));
747 Matcher<const AClass&> m_with_name =
748 Property(
"n", &AClass::n, Matcher<signed char>(Ge(0)));
751 EXPECT_TRUE(m.Matches(a));
752 EXPECT_TRUE(m_with_name.Matches(a));
754 EXPECT_FALSE(m.Matches(a));
755 EXPECT_FALSE(m_with_name.Matches(a));
759TEST(PropertyTest, CanDescribeSelf) {
760 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
762 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(m));
763 EXPECT_EQ(
"is an object whose given property isn't >= 0",
764 DescribeNegation(m));
767TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
768 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
770 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(m));
771 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
772 DescribeNegation(m));
776TEST_P(PropertyTestP, CanExplainMatchResult) {
777 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
781 EXPECT_EQ(
"whose given property is 1" + OfType(
"int"), Explain(m, a));
783 m = Property(&AClass::n, GreaterThan(0));
785 "whose given property is 1" + OfType(
"int") +
", which is 1 more than 0",
789TEST_P(PropertyTestP, CanExplainMatchResultWithPropertyName) {
790 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
794 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int"), Explain(m, a));
796 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
797 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int") +
798 ", which is 1 more than 0",
802INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyForPointerTest);
805TEST(PropertyForPointerTest, WorksForPointerToConst) {
806 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
810 EXPECT_TRUE(m.Matches(&a));
813 EXPECT_FALSE(m.Matches(&a));
817TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
818 Matcher<AClass*> m = Property(&AClass::s, StartsWith(
"hi"));
822 EXPECT_TRUE(m.Matches(&a));
825 EXPECT_FALSE(m.Matches(&a));
830TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
831 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith(
"hi"));
835 EXPECT_TRUE(m.Matches(&a));
838 EXPECT_FALSE(m.Matches(&a));
842TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
843 Matcher<const AClass*> m = Property(&AClass::x, _);
844 EXPECT_FALSE(m.Matches(
nullptr));
849TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
852 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
856 EXPECT_TRUE(m.Matches(&d));
859 EXPECT_FALSE(m.Matches(&d));
863TEST(PropertyForPointerTest, CanDescribeSelf) {
864 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
866 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(m));
867 EXPECT_EQ(
"is an object whose given property isn't >= 0",
868 DescribeNegation(m));
871TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
872 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
874 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(m));
875 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
876 DescribeNegation(m));
880TEST_P(PropertyForPointerTestP, CanExplainMatchResult) {
881 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
885 EXPECT_EQ(
"", Explain(m,
static_cast<const AClass*
>(
nullptr)));
887 "which points to an object whose given property is 1" + OfType(
"int"),
890 m = Property(&AClass::n, GreaterThan(0));
891 EXPECT_EQ(
"which points to an object whose given property is 1" +
892 OfType(
"int") +
", which is 1 more than 0",
896TEST_P(PropertyForPointerTestP, CanExplainMatchResultWithPropertyName) {
897 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
901 EXPECT_EQ(
"", Explain(m,
static_cast<const AClass*
>(
nullptr)));
902 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
906 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
907 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
908 OfType(
"int") +
", which is 1 more than 0",
916std::string IntToStringFunction(
int input) {
917 return input == 1 ?
"foo" :
"bar";
920INSTANTIATE_GTEST_MATCHER_TEST_P(ResultOfTest);
922TEST(ResultOfTest, WorksForFunctionPointers) {
923 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string(
"foo")));
925 EXPECT_TRUE(matcher.Matches(1));
926 EXPECT_FALSE(matcher.Matches(2));
930TEST(ResultOfTest, CanDescribeItself) {
931 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq(
"foo"));
934 "is mapped by the given callable to a value that "
935 "is equal to \"foo\"",
938 "is mapped by the given callable to a value that "
939 "isn't equal to \"foo\"",
940 DescribeNegation(matcher));
944TEST(ResultOfTest, CanDescribeItselfWithResultDescription) {
945 Matcher<int> matcher =
946 ResultOf(
"string conversion", &IntToStringFunction, StrEq(
"foo"));
948 EXPECT_EQ(
"whose string conversion is equal to \"foo\"", Describe(matcher));
949 EXPECT_EQ(
"whose string conversion isn't equal to \"foo\"",
950 DescribeNegation(matcher));
954int IntFunction(
int input) {
return input == 42 ? 80 : 90; }
956TEST_P(ResultOfTestP, CanExplainMatchResult) {
957 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
958 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int"),
959 Explain(matcher, 36));
961 matcher = ResultOf(&IntFunction, GreaterThan(85));
962 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int") +
963 ", which is 5 more than 85",
964 Explain(matcher, 36));
967TEST_P(ResultOfTestP, CanExplainMatchResultWithResultDescription) {
968 Matcher<int> matcher = ResultOf(
"magic int conversion", &IntFunction, Ge(85));
969 EXPECT_EQ(
"whose magic int conversion is 90" + OfType(
"int"),
970 Explain(matcher, 36));
972 matcher = ResultOf(
"magic int conversion", &IntFunction, GreaterThan(85));
973 EXPECT_EQ(
"whose magic int conversion is 90" + OfType(
"int") +
974 ", which is 5 more than 85",
975 Explain(matcher, 36));
980TEST(ResultOfTest, WorksForNonReferenceResults) {
981 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
983 EXPECT_TRUE(matcher.Matches(42));
984 EXPECT_FALSE(matcher.Matches(36));
989double& DoubleFunction(
double& input) {
return input; }
991Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
995TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
998 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
1000 EXPECT_TRUE(matcher.Matches(x));
1001 EXPECT_FALSE(matcher.Matches(x2));
1006 Matcher<Uncopyable&> matcher2 = ResultOf(&RefUncopyableFunction, Ref(obj));
1008 EXPECT_TRUE(matcher2.Matches(obj));
1009 EXPECT_FALSE(matcher2.Matches(obj2));
1014const std::string& StringFunction(
const std::string& input) {
return input; }
1016TEST(ResultOfTest, WorksForReferenceToConstResults) {
1017 std::string s =
"foo";
1019 Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s));
1021 EXPECT_TRUE(matcher.Matches(s));
1022 EXPECT_FALSE(matcher.Matches(s2));
1027TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
1029 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
1031 EXPECT_TRUE(matcher.Matches(36));
1032 EXPECT_FALSE(matcher.Matches(42));
1037TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
1038 EXPECT_DEATH_IF_SUPPORTED(
1039 ResultOf(
static_cast<std::string (*)(
int dummy)
>(
nullptr),
1040 Eq(std::string(
"foo"))),
1041 "NULL function pointer is passed into ResultOf\\(\\)\\.");
1046TEST(ResultOfTest, WorksForFunctionReferences) {
1047 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq(
"foo"));
1048 EXPECT_TRUE(matcher.Matches(1));
1049 EXPECT_FALSE(matcher.Matches(2));
1055 std::string operator()(
int input)
const {
return IntToStringFunction(input); }
1058TEST(ResultOfTest, WorksForFunctors) {
1059 Matcher<int> matcher = ResultOf(Functor(), Eq(std::string(
"foo")));
1061 EXPECT_TRUE(matcher.Matches(1));
1062 EXPECT_FALSE(matcher.Matches(2));
1068struct PolymorphicFunctor {
1069 typedef int result_type;
1070 int operator()(
int n) {
return n; }
1071 int operator()(
const char* s) {
return static_cast<int>(strlen(s)); }
1072 std::string operator()(
int* p) {
return p ?
"good ptr" :
"null"; }
1075TEST(ResultOfTest, WorksForPolymorphicFunctors) {
1076 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
1078 EXPECT_TRUE(matcher_int.Matches(10));
1079 EXPECT_FALSE(matcher_int.Matches(2));
1081 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
1083 EXPECT_TRUE(matcher_string.Matches(
"long string"));
1084 EXPECT_FALSE(matcher_string.Matches(
"shrt"));
1087TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
1088 Matcher<int*> matcher = ResultOf(PolymorphicFunctor(),
"good ptr");
1091 EXPECT_TRUE(matcher.Matches(&n));
1092 EXPECT_FALSE(matcher.Matches(
nullptr));
1095TEST(ResultOfTest, WorksForLambdas) {
1096 Matcher<int> matcher = ResultOf(
1098 return std::string(
static_cast<size_t>(str_len),
'x');
1101 EXPECT_TRUE(matcher.Matches(3));
1102 EXPECT_FALSE(matcher.Matches(1));
1105TEST(ResultOfTest, WorksForNonCopyableArguments) {
1106 Matcher<std::unique_ptr<int>> matcher = ResultOf(
1107 [](
const std::unique_ptr<int>& str_len) {
1108 return std::string(
static_cast<size_t>(*str_len),
'x');
1111 EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(
new int(3))));
1112 EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(
new int(1))));
1115const int* ReferencingFunction(
const int& n) {
return &n; }
1117struct ReferencingFunctor {
1118 typedef const int* result_type;
1119 result_type operator()(
const int& n) {
return &n; }
1122TEST(ResultOfTest, WorksForReferencingCallables) {
1125 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
1126 EXPECT_TRUE(matcher2.Matches(n));
1127 EXPECT_FALSE(matcher2.Matches(n2));
1129 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
1130 EXPECT_TRUE(matcher3.Matches(n));
1131 EXPECT_FALSE(matcher3.Matches(n2));
1134TEST(SizeIsTest, ImplementsSizeIs) {
1135 vector<int> container;
1136 EXPECT_THAT(container, SizeIs(0));
1137 EXPECT_THAT(container, Not(SizeIs(1)));
1138 container.push_back(0);
1139 EXPECT_THAT(container, Not(SizeIs(0)));
1140 EXPECT_THAT(container, SizeIs(1));
1141 container.push_back(0);
1142 EXPECT_THAT(container, Not(SizeIs(0)));
1143 EXPECT_THAT(container, SizeIs(2));
1146TEST(SizeIsTest, WorksWithMap) {
1147 map<std::string, int> container;
1148 EXPECT_THAT(container, SizeIs(0));
1149 EXPECT_THAT(container, Not(SizeIs(1)));
1150 container.insert(make_pair(
"foo", 1));
1151 EXPECT_THAT(container, Not(SizeIs(0)));
1152 EXPECT_THAT(container, SizeIs(1));
1153 container.insert(make_pair(
"bar", 2));
1154 EXPECT_THAT(container, Not(SizeIs(0)));
1155 EXPECT_THAT(container, SizeIs(2));
1158TEST(SizeIsTest, WorksWithReferences) {
1159 vector<int> container;
1160 Matcher<const vector<int>&> m = SizeIs(1);
1161 EXPECT_THAT(container, Not(m));
1162 container.push_back(0);
1163 EXPECT_THAT(container, m);
1166TEST(SizeIsTest, WorksWithMoveOnly) {
1167 ContainerHelper helper;
1168 EXPECT_CALL(helper, Call(SizeIs(3)));
1169 helper.Call(MakeUniquePtrs({1, 2, 3}));
1174struct MinimalistCustomType {
1175 int size()
const {
return 1; }
1177TEST(SizeIsTest, WorksWithMinimalistCustomType) {
1178 MinimalistCustomType container;
1179 EXPECT_THAT(container, SizeIs(1));
1180 EXPECT_THAT(container, Not(SizeIs(0)));
1183TEST(SizeIsTest, CanDescribeSelf) {
1184 Matcher<vector<int>> m = SizeIs(2);
1185 EXPECT_EQ(
"size is equal to 2", Describe(m));
1186 EXPECT_EQ(
"size isn't equal to 2", DescribeNegation(m));
1189TEST(SizeIsTest, ExplainsResult) {
1190 Matcher<vector<int>> m1 = SizeIs(2);
1191 Matcher<vector<int>> m2 = SizeIs(Lt(2u));
1192 Matcher<vector<int>> m3 = SizeIs(AnyOf(0, 3));
1193 Matcher<vector<int>> m4 = SizeIs(Gt(1u));
1194 vector<int> container;
1195 EXPECT_EQ(
"whose size 0 doesn't match", Explain(m1, container));
1196 EXPECT_EQ(
"whose size 0 matches", Explain(m2, container));
1197 EXPECT_EQ(
"whose size 0 matches", Explain(m3, container));
1198 EXPECT_EQ(
"whose size 0 doesn't match", Explain(m4, container));
1199 container.push_back(0);
1200 container.push_back(0);
1201 EXPECT_EQ(
"whose size 2 matches", Explain(m1, container));
1202 EXPECT_EQ(
"whose size 2 doesn't match", Explain(m2, container));
1203 EXPECT_EQ(
"whose size 2 doesn't match", Explain(m3, container));
1204 EXPECT_EQ(
"whose size 2 matches", Explain(m4, container));
1207TEST(WhenSortedByTest, WorksForEmptyContainer) {
1208 const vector<int> numbers;
1209 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
1210 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
1213TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
1214 vector<unsigned> numbers;
1215 numbers.push_back(3);
1216 numbers.push_back(1);
1217 numbers.push_back(2);
1218 numbers.push_back(2);
1219 EXPECT_THAT(numbers,
1220 WhenSortedBy(greater<unsigned>(), ElementsAre(3, 2, 2, 1)));
1221 EXPECT_THAT(numbers,
1222 Not(WhenSortedBy(greater<unsigned>(), ElementsAre(1, 2, 2, 3))));
1225TEST(WhenSortedByTest, WorksForNonVectorContainer) {
1226 list<std::string> words;
1227 words.push_back(
"say");
1228 words.push_back(
"hello");
1229 words.push_back(
"world");
1230 EXPECT_THAT(words, WhenSortedBy(less<std::string>(),
1231 ElementsAre(
"hello",
"say",
"world")));
1232 EXPECT_THAT(words, Not(WhenSortedBy(less<std::string>(),
1233 ElementsAre(
"say",
"hello",
"world"))));
1236TEST(WhenSortedByTest, WorksForNativeArray) {
1237 const int numbers[] = {1, 3, 2, 4};
1238 const int sorted_numbers[] = {1, 2, 3, 4};
1239 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
1240 EXPECT_THAT(numbers,
1241 WhenSortedBy(less<int>(), ElementsAreArray(sorted_numbers)));
1242 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
1245TEST(WhenSortedByTest, CanDescribeSelf) {
1246 const Matcher<vector<int>> m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
1248 "(when sorted) has 2 elements where\n"
1249 "element #0 is equal to 1,\n"
1250 "element #1 is equal to 2",
1253 "(when sorted) doesn't have 2 elements, or\n"
1254 "element #0 isn't equal to 1, or\n"
1255 "element #1 isn't equal to 2",
1256 DescribeNegation(m));
1259TEST(WhenSortedByTest, ExplainsMatchResult) {
1260 const int a[] = {2, 1};
1261 EXPECT_EQ(
"which is { 1, 2 } when sorted, whose element #0 doesn't match",
1262 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
1263 EXPECT_EQ(
"which is { 1, 2 } when sorted",
1264 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
1270TEST(WhenSortedTest, WorksForEmptyContainer) {
1271 const vector<int> numbers;
1272 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
1273 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
1276TEST(WhenSortedTest, WorksForNonEmptyContainer) {
1277 list<std::string> words;
1278 words.push_back(
"3");
1279 words.push_back(
"1");
1280 words.push_back(
"2");
1281 words.push_back(
"2");
1282 EXPECT_THAT(words, WhenSorted(ElementsAre(
"1",
"2",
"2",
"3")));
1283 EXPECT_THAT(words, Not(WhenSorted(ElementsAre(
"3",
"1",
"2",
"2"))));
1286TEST(WhenSortedTest, WorksForMapTypes) {
1287 map<std::string, int> word_counts;
1288 word_counts[
"and"] = 1;
1289 word_counts[
"the"] = 1;
1290 word_counts[
"buffalo"] = 2;
1291 EXPECT_THAT(word_counts,
1292 WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"buffalo", 2),
1294 EXPECT_THAT(word_counts,
1295 Not(WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"the", 1),
1296 Pair(
"buffalo", 2)))));
1299TEST(WhenSortedTest, WorksForMultiMapTypes) {
1300 multimap<int, int> ifib;
1301 ifib.insert(make_pair(8, 6));
1302 ifib.insert(make_pair(2, 3));
1303 ifib.insert(make_pair(1, 1));
1304 ifib.insert(make_pair(3, 4));
1305 ifib.insert(make_pair(1, 2));
1306 ifib.insert(make_pair(5, 5));
1308 WhenSorted(ElementsAre(Pair(1, 1), Pair(1, 2), Pair(2, 3),
1309 Pair(3, 4), Pair(5, 5), Pair(8, 6))));
1311 Not(WhenSorted(ElementsAre(Pair(8, 6), Pair(2, 3), Pair(1, 1),
1312 Pair(3, 4), Pair(1, 2), Pair(5, 5)))));
1315TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
1319 EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2)));
1320 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
1323TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
1327 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
1328 EXPECT_THAT(d, WhenSorted(vector_match));
1329 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
1330 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
1335template <
typename T>
1341 typedef ConstIter const_iterator;
1342 typedef T value_type;
1344 template <
typename InIter>
1345 Streamlike(InIter first, InIter last) : remainder_(first, last) {}
1347 const_iterator begin()
const {
1348 return const_iterator(
this, remainder_.begin());
1350 const_iterator end()
const {
return const_iterator(
this, remainder_.end()); }
1355 using iterator_category = std::input_iterator_tag;
1356 using value_type = T;
1357 using difference_type = ptrdiff_t;
1358 using pointer =
const value_type*;
1359 using reference =
const value_type&;
1361 ConstIter(
const Streamlike* s,
typename std::list<value_type>::iterator pos)
1362 : s_(s), pos_(pos) {}
1364 const value_type& operator*()
const {
return *pos_; }
1365 const value_type* operator->()
const {
return &*pos_; }
1366 ConstIter& operator++() {
1367 s_->remainder_.erase(pos_++);
1373 class PostIncrProxy {
1375 explicit PostIncrProxy(
const value_type& value) : value_(value) {}
1376 value_type operator*()
const {
return value_; }
1381 PostIncrProxy operator++(
int) {
1382 PostIncrProxy proxy(**
this);
1387 friend bool operator==(
const ConstIter& a,
const ConstIter& b) {
1388 return a.s_ == b.s_ && a.pos_ == b.pos_;
1390 friend bool operator!=(
const ConstIter& a,
const ConstIter& b) {
1395 const Streamlike* s_;
1396 typename std::list<value_type>::iterator pos_;
1399 friend std::ostream& operator<<(std::ostream& os,
const Streamlike& s) {
1401 typedef typename std::list<value_type>::const_iterator Iter;
1402 const char* sep =
"";
1403 for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
1411 mutable std::list<value_type> remainder_;
1414TEST(StreamlikeTest, Iteration) {
1415 const int a[5] = {2, 1, 4, 5, 3};
1416 Streamlike<int> s(a, a + 5);
1417 Streamlike<int>::const_iterator it = s.begin();
1419 while (it != s.end()) {
1420 SCOPED_TRACE(ip - a);
1421 EXPECT_EQ(*ip++, *it++);
1425INSTANTIATE_GTEST_MATCHER_TEST_P(BeginEndDistanceIsTest);
1427TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
1428 std::forward_list<int> container;
1429 EXPECT_THAT(container, BeginEndDistanceIs(0));
1430 EXPECT_THAT(container, Not(BeginEndDistanceIs(1)));
1431 container.push_front(0);
1432 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
1433 EXPECT_THAT(container, BeginEndDistanceIs(1));
1434 container.push_front(0);
1435 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
1436 EXPECT_THAT(container, BeginEndDistanceIs(2));
1439TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
1440 const int a[5] = {1, 2, 3, 4, 5};
1441 Streamlike<int> s(a, a + 5);
1442 EXPECT_THAT(s, BeginEndDistanceIs(5));
1445TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
1446 Matcher<vector<int>> m = BeginEndDistanceIs(2);
1447 EXPECT_EQ(
"distance between begin() and end() is equal to 2", Describe(m));
1448 EXPECT_EQ(
"distance between begin() and end() isn't equal to 2",
1449 DescribeNegation(m));
1452TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
1453 ContainerHelper helper;
1454 EXPECT_CALL(helper, Call(BeginEndDistanceIs(2)));
1455 helper.Call(MakeUniquePtrs({1, 2}));
1458TEST_P(BeginEndDistanceIsTestP, ExplainsResult) {
1459 Matcher<vector<int>> m1 = BeginEndDistanceIs(2);
1460 Matcher<vector<int>> m2 = BeginEndDistanceIs(Lt(2));
1461 Matcher<vector<int>> m3 = BeginEndDistanceIs(AnyOf(0, 3));
1462 Matcher<vector<int>> m4 = BeginEndDistanceIs(GreaterThan(1));
1463 vector<int> container;
1464 EXPECT_EQ(
"whose distance between begin() and end() 0 doesn't match",
1465 Explain(m1, container));
1466 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
1467 Explain(m2, container));
1468 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
1469 Explain(m3, container));
1471 "whose distance between begin() and end() 0 doesn't match, which is 1 "
1473 Explain(m4, container));
1474 container.push_back(0);
1475 container.push_back(0);
1476 EXPECT_EQ(
"whose distance between begin() and end() 2 matches",
1477 Explain(m1, container));
1478 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
1479 Explain(m2, container));
1480 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
1481 Explain(m3, container));
1483 "whose distance between begin() and end() 2 matches, which is 1 more "
1485 Explain(m4, container));
1488TEST(WhenSortedTest, WorksForStreamlike) {
1491 const int a[5] = {2, 1, 4, 5, 3};
1492 Streamlike<int> s(std::begin(a), std::end(a));
1493 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
1494 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
1497TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
1498 const int a[] = {2, 1, 4, 5, 3};
1499 Streamlike<int> s(std::begin(a), std::end(a));
1500 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
1501 EXPECT_THAT(s, WhenSorted(vector_match));
1502 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
1505TEST(IsSupersetOfTest, WorksForNativeArray) {
1506 const int subset[] = {1, 4};
1507 const int superset[] = {1, 2, 4};
1508 const int disjoint[] = {1, 0, 3};
1509 EXPECT_THAT(subset, IsSupersetOf(subset));
1510 EXPECT_THAT(subset, Not(IsSupersetOf(superset)));
1511 EXPECT_THAT(superset, IsSupersetOf(subset));
1512 EXPECT_THAT(subset, Not(IsSupersetOf(disjoint)));
1513 EXPECT_THAT(disjoint, Not(IsSupersetOf(subset)));
1516TEST(IsSupersetOfTest, WorksWithDuplicates) {
1517 const int not_enough[] = {1, 2};
1518 const int enough[] = {1, 1, 2};
1519 const int expected[] = {1, 1};
1520 EXPECT_THAT(not_enough, Not(IsSupersetOf(expected)));
1521 EXPECT_THAT(enough, IsSupersetOf(expected));
1524TEST(IsSupersetOfTest, WorksForEmpty) {
1525 vector<int> numbers;
1526 vector<int> expected;
1527 EXPECT_THAT(numbers, IsSupersetOf(expected));
1528 expected.push_back(1);
1529 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
1531 numbers.push_back(1);
1532 numbers.push_back(2);
1533 EXPECT_THAT(numbers, IsSupersetOf(expected));
1534 expected.push_back(1);
1535 EXPECT_THAT(numbers, IsSupersetOf(expected));
1536 expected.push_back(2);
1537 EXPECT_THAT(numbers, IsSupersetOf(expected));
1538 expected.push_back(3);
1539 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
1542TEST(IsSupersetOfTest, WorksForStreamlike) {
1543 const int a[5] = {1, 2, 3, 4, 5};
1544 Streamlike<int> s(std::begin(a), std::end(a));
1546 vector<int> expected;
1547 expected.push_back(1);
1548 expected.push_back(2);
1549 expected.push_back(5);
1550 EXPECT_THAT(s, IsSupersetOf(expected));
1552 expected.push_back(0);
1553 EXPECT_THAT(s, Not(IsSupersetOf(expected)));
1556TEST(IsSupersetOfTest, TakesStlContainer) {
1557 const int actual[] = {3, 1, 2};
1559 ::std::list<int> expected;
1560 expected.push_back(1);
1561 expected.push_back(3);
1562 EXPECT_THAT(actual, IsSupersetOf(expected));
1564 expected.push_back(4);
1565 EXPECT_THAT(actual, Not(IsSupersetOf(expected)));
1568TEST(IsSupersetOfTest, Describe) {
1569 typedef std::vector<int> IntVec;
1571 expected.push_back(111);
1572 expected.push_back(222);
1573 expected.push_back(333);
1575 Describe<IntVec>(IsSupersetOf(expected)),
1576 Eq(
"a surjection from elements to requirements exists such that:\n"
1577 " - an element is equal to 111\n"
1578 " - an element is equal to 222\n"
1579 " - an element is equal to 333"));
1582TEST(IsSupersetOfTest, DescribeNegation) {
1583 typedef std::vector<int> IntVec;
1585 expected.push_back(111);
1586 expected.push_back(222);
1587 expected.push_back(333);
1589 DescribeNegation<IntVec>(IsSupersetOf(expected)),
1590 Eq(
"no surjection from elements to requirements exists such that:\n"
1591 " - an element is equal to 111\n"
1592 " - an element is equal to 222\n"
1593 " - an element is equal to 333"));
1596TEST(IsSupersetOfTest, MatchAndExplain) {
1600 std::vector<int> expected;
1601 expected.push_back(1);
1602 expected.push_back(2);
1603 StringMatchResultListener listener;
1604 ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
1606 EXPECT_THAT(listener.str(),
1607 Eq(
"where the following matchers don't match any elements:\n"
1608 "matcher #0: is equal to 1"));
1612 ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
1614 EXPECT_THAT(listener.str(), Eq(
"where:\n"
1615 " - element #0 is matched by matcher #1,\n"
1616 " - element #2 is matched by matcher #0"));
1619TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
1620 const int numbers[] = {1, 3, 6, 2, 4, 5};
1621 EXPECT_THAT(numbers, IsSupersetOf({1, 2}));
1622 EXPECT_THAT(numbers, Not(IsSupersetOf({3, 0})));
1625TEST(IsSupersetOfTest, WorksWithMoveOnly) {
1626 ContainerHelper helper;
1627 EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));
1628 helper.Call(MakeUniquePtrs({1, 2}));
1629 EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));
1630 helper.Call(MakeUniquePtrs({2}));
1633TEST(IsSubsetOfTest, WorksForNativeArray) {
1634 const int subset[] = {1, 4};
1635 const int superset[] = {1, 2, 4};
1636 const int disjoint[] = {1, 0, 3};
1637 EXPECT_THAT(subset, IsSubsetOf(subset));
1638 EXPECT_THAT(subset, IsSubsetOf(superset));
1639 EXPECT_THAT(superset, Not(IsSubsetOf(subset)));
1640 EXPECT_THAT(subset, Not(IsSubsetOf(disjoint)));
1641 EXPECT_THAT(disjoint, Not(IsSubsetOf(subset)));
1644TEST(IsSubsetOfTest, WorksWithDuplicates) {
1645 const int not_enough[] = {1, 2};
1646 const int enough[] = {1, 1, 2};
1647 const int actual[] = {1, 1};
1648 EXPECT_THAT(actual, Not(IsSubsetOf(not_enough)));
1649 EXPECT_THAT(actual, IsSubsetOf(enough));
1652TEST(IsSubsetOfTest, WorksForEmpty) {
1653 vector<int> numbers;
1654 vector<int> expected;
1655 EXPECT_THAT(numbers, IsSubsetOf(expected));
1656 expected.push_back(1);
1657 EXPECT_THAT(numbers, IsSubsetOf(expected));
1659 numbers.push_back(1);
1660 numbers.push_back(2);
1661 EXPECT_THAT(numbers, Not(IsSubsetOf(expected)));
1662 expected.push_back(1);
1663 EXPECT_THAT(numbers, Not(IsSubsetOf(expected)));
1664 expected.push_back(2);
1665 EXPECT_THAT(numbers, IsSubsetOf(expected));
1666 expected.push_back(3);
1667 EXPECT_THAT(numbers, IsSubsetOf(expected));
1670TEST(IsSubsetOfTest, WorksForStreamlike) {
1671 const int a[5] = {1, 2};
1672 Streamlike<int> s(std::begin(a), std::end(a));
1674 vector<int> expected;
1675 expected.push_back(1);
1676 EXPECT_THAT(s, Not(IsSubsetOf(expected)));
1677 expected.push_back(2);
1678 expected.push_back(5);
1679 EXPECT_THAT(s, IsSubsetOf(expected));
1682TEST(IsSubsetOfTest, TakesStlContainer) {
1683 const int actual[] = {3, 1, 2};
1685 ::std::list<int> expected;
1686 expected.push_back(1);
1687 expected.push_back(3);
1688 EXPECT_THAT(actual, Not(IsSubsetOf(expected)));
1690 expected.push_back(2);
1691 expected.push_back(4);
1692 EXPECT_THAT(actual, IsSubsetOf(expected));
1695TEST(IsSubsetOfTest, Describe) {
1696 typedef std::vector<int> IntVec;
1698 expected.push_back(111);
1699 expected.push_back(222);
1700 expected.push_back(333);
1703 Describe<IntVec>(IsSubsetOf(expected)),
1704 Eq(
"an injection from elements to requirements exists such that:\n"
1705 " - an element is equal to 111\n"
1706 " - an element is equal to 222\n"
1707 " - an element is equal to 333"));
1710TEST(IsSubsetOfTest, DescribeNegation) {
1711 typedef std::vector<int> IntVec;
1713 expected.push_back(111);
1714 expected.push_back(222);
1715 expected.push_back(333);
1717 DescribeNegation<IntVec>(IsSubsetOf(expected)),
1718 Eq(
"no injection from elements to requirements exists such that:\n"
1719 " - an element is equal to 111\n"
1720 " - an element is equal to 222\n"
1721 " - an element is equal to 333"));
1724TEST(IsSubsetOfTest, MatchAndExplain) {
1728 std::vector<int> expected;
1729 expected.push_back(1);
1730 expected.push_back(2);
1731 StringMatchResultListener listener;
1732 ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
1734 EXPECT_THAT(listener.str(),
1735 Eq(
"where the following elements don't match any matchers:\n"
1738 expected.push_back(3);
1740 ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
1742 EXPECT_THAT(listener.str(), Eq(
"where:\n"
1743 " - element #0 is matched by matcher #1,\n"
1744 " - element #1 is matched by matcher #2"));
1747TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
1748 const int numbers[] = {1, 2, 3};
1749 EXPECT_THAT(numbers, IsSubsetOf({1, 2, 3, 4}));
1750 EXPECT_THAT(numbers, Not(IsSubsetOf({1, 2})));
1753TEST(IsSubsetOfTest, WorksWithMoveOnly) {
1754 ContainerHelper helper;
1755 EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));
1756 helper.Call(MakeUniquePtrs({1}));
1757 EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));
1758 helper.Call(MakeUniquePtrs({2}));
1764TEST(ElemensAreStreamTest, WorksForStreamlike) {
1765 const int a[5] = {1, 2, 3, 4, 5};
1766 Streamlike<int> s(std::begin(a), std::end(a));
1767 EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5));
1768 EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3)));
1771TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
1772 const int a[5] = {1, 2, 3, 4, 5};
1773 Streamlike<int> s(std::begin(a), std::end(a));
1775 vector<int> expected;
1776 expected.push_back(1);
1777 expected.push_back(2);
1778 expected.push_back(3);
1779 expected.push_back(4);
1780 expected.push_back(5);
1781 EXPECT_THAT(s, ElementsAreArray(expected));
1784 EXPECT_THAT(s, Not(ElementsAreArray(expected)));
1787TEST(ElementsAreTest, WorksWithUncopyable) {
1789 objs[0].set_value(-3);
1790 objs[1].set_value(1);
1791 EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));
1794TEST(ElementsAreTest, WorksWithMoveOnly) {
1795 ContainerHelper helper;
1796 EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));
1797 helper.Call(MakeUniquePtrs({1, 2}));
1799 EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));
1800 helper.Call(MakeUniquePtrs({3, 4}));
1803TEST(ElementsAreTest, TakesStlContainer) {
1804 const int actual[] = {3, 1, 2};
1806 ::std::list<int> expected;
1807 expected.push_back(3);
1808 expected.push_back(1);
1809 expected.push_back(2);
1810 EXPECT_THAT(actual, ElementsAreArray(expected));
1812 expected.push_back(4);
1813 EXPECT_THAT(actual, Not(ElementsAreArray(expected)));
1818TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
1819 const int a[] = {0, 1, 2, 3, 4};
1820 std::vector<int> s(std::begin(a), std::end(a));
1822 StringMatchResultListener listener;
1823 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a), s, &listener))
1825 }
while (std::next_permutation(s.begin(), s.end()));
1828TEST(UnorderedElementsAreArrayTest, VectorBool) {
1829 const bool a[] = {0, 1, 0, 1, 1};
1830 const bool b[] = {1, 0, 1, 1, 0};
1831 std::vector<bool> expected(std::begin(a), std::end(a));
1832 std::vector<bool> actual(std::begin(b), std::end(b));
1833 StringMatchResultListener listener;
1834 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected), actual,
1839TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
1843 const int a[5] = {2, 1, 4, 5, 3};
1844 Streamlike<int> s(std::begin(a), std::end(a));
1846 ::std::vector<int> expected;
1847 expected.push_back(1);
1848 expected.push_back(2);
1849 expected.push_back(3);
1850 expected.push_back(4);
1851 expected.push_back(5);
1852 EXPECT_THAT(s, UnorderedElementsAreArray(expected));
1854 expected.push_back(6);
1855 EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));
1858TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
1859 const int actual[] = {3, 1, 2};
1861 ::std::list<int> expected;
1862 expected.push_back(1);
1863 expected.push_back(2);
1864 expected.push_back(3);
1865 EXPECT_THAT(actual, UnorderedElementsAreArray(expected));
1867 expected.push_back(4);
1868 EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected)));
1871TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
1872 const int a[5] = {2, 1, 4, 5, 3};
1873 EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5}));
1874 EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6})));
1877TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
1878 const std::string a[5] = {
"a",
"b",
"c",
"d",
"e"};
1879 EXPECT_THAT(a, UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
1880 EXPECT_THAT(a, Not(UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
1883TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
1884 const int a[5] = {2, 1, 4, 5, 3};
1886 UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
1888 a, Not(UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
1891TEST(UnorderedElementsAreArrayTest,
1892 TakesInitializerListOfDifferentTypedMatchers) {
1893 const int a[5] = {2, 1, 4, 5, 3};
1897 EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int>>(
1898 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
1899 EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int>>(
1900 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
1903TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
1904 ContainerHelper helper;
1906 Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)})));
1907 helper.Call(MakeUniquePtrs({2, 1}));
1912 typedef std::vector<int> IntVec;
1915TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
1917 objs[0].set_value(-3);
1918 objs[1].set_value(1);
1920 UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3)));
1923TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
1924 const int a[] = {1, 2, 3};
1925 std::vector<int> s(std::begin(a), std::end(a));
1927 StringMatchResultListener listener;
1928 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), s, &listener))
1930 }
while (std::next_permutation(s.begin(), s.end()));
1933TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
1934 const int a[] = {1, 2, 3};
1935 std::vector<int> s(std::begin(a), std::end(a));
1936 std::vector<Matcher<int>> mv;
1941 StringMatchResultListener listener;
1942 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
1946TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
1950 const int a[5] = {2, 1, 4, 5, 3};
1951 Streamlike<int> s(std::begin(a), std::end(a));
1953 EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
1954 EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
1957TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
1958 ContainerHelper helper;
1959 EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2))));
1960 helper.Call(MakeUniquePtrs({2, 1}));
1969TEST_F(UnorderedElementsAreTest, Performance) {
1971 std::vector<Matcher<int>> mv;
1972 for (
int i = 0; i < 100; ++i) {
1977 StringMatchResultListener listener;
1978 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
1985TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
1987 std::vector<Matcher<int>> mv;
1988 for (
int i = 0; i < 100; ++i) {
1996 StringMatchResultListener listener;
1997 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
2001TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
2004 StringMatchResultListener listener;
2005 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
2007 EXPECT_THAT(listener.str(), Eq(
"which has 1 element"));
2010TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
2012 StringMatchResultListener listener;
2013 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
2015 EXPECT_THAT(listener.str(), Eq(
""));
2018TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
2022 StringMatchResultListener listener;
2023 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))
2025 EXPECT_THAT(listener.str(),
2026 Eq(
"where the following matchers don't match any elements:\n"
2027 "matcher #1: is equal to 2"));
2030TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
2034 StringMatchResultListener listener;
2035 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1), v, &listener))
2037 EXPECT_THAT(listener.str(),
2038 Eq(
"where the following elements don't match any matchers:\n"
2042TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
2046 StringMatchResultListener listener;
2047 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))
2049 EXPECT_THAT(listener.str(),
2051 " the following matchers don't match any elements:\n"
2052 "matcher #0: is equal to 1\n"
2055 " the following elements don't match any matchers:\n"
2060static std::string EMString(
int element,
int matcher) {
2062 ss <<
"(element #" << element <<
", matcher #" << matcher <<
")";
2066TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
2069 std::vector<std::string> v;
2073 StringMatchResultListener listener;
2074 EXPECT_FALSE(ExplainMatchResult(
2075 UnorderedElementsAre(
"a",
"a", AnyOf(
"b",
"c")), v, &listener))
2078 std::string prefix =
2079 "where no permutation of the elements can satisfy all matchers, "
2080 "and the closest match is 2 of 3 matchers with the "
2087 prefix +
"{\n " + EMString(0, 0) +
",\n " + EMString(1, 2) +
"\n}",
2088 prefix +
"{\n " + EMString(0, 1) +
",\n " + EMString(1, 2) +
"\n}",
2089 prefix +
"{\n " + EMString(0, 0) +
",\n " + EMString(2, 2) +
"\n}",
2090 prefix +
"{\n " + EMString(0, 1) +
",\n " + EMString(2, 2) +
2094TEST_F(UnorderedElementsAreTest, Describe) {
2095 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()), Eq(
"is empty"));
2096 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre(345)),
2097 Eq(
"has 1 element and that element is equal to 345"));
2098 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre(111, 222, 333)),
2099 Eq(
"has 3 elements and there exists some permutation "
2100 "of elements such that:\n"
2101 " - element #0 is equal to 111, and\n"
2102 " - element #1 is equal to 222, and\n"
2103 " - element #2 is equal to 333"));
2106TEST_F(UnorderedElementsAreTest, DescribeNegation) {
2107 EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()),
2110 DescribeNegation<IntVec>(UnorderedElementsAre(345)),
2111 Eq(
"doesn't have 1 element, or has 1 element that isn't equal to 345"));
2112 EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)),
2113 Eq(
"doesn't have 3 elements, or there exists no permutation "
2114 "of elements such that:\n"
2115 " - element #0 is equal to 123, and\n"
2116 " - element #1 is equal to 234, and\n"
2117 " - element #2 is equal to 345"));
2122INSTANTIATE_GTEST_MATCHER_TEST_P(EachTest);
2124TEST_P(EachTestP, ExplainsMatchResultCorrectly) {
2127 Matcher<set<int>> m = Each(2);
2128 EXPECT_EQ(
"", Explain(m, a));
2130 Matcher<
const int(&)[1]> n = Each(1);
2132 const int b[1] = {1};
2133 EXPECT_EQ(
"", Explain(n, b));
2136 EXPECT_EQ(
"whose element #0 doesn't match", Explain(n, b));
2141 m = Each(GreaterThan(0));
2142 EXPECT_EQ(
"", Explain(m, a));
2144 m = Each(GreaterThan(10));
2145 EXPECT_EQ(
"whose element #0 doesn't match, which is 9 less than 10",
2149TEST(EachTest, DescribesItselfCorrectly) {
2150 Matcher<vector<int>> m = Each(1);
2151 EXPECT_EQ(
"only contains elements that is equal to 1", Describe(m));
2153 Matcher<vector<int>> m2 = Not(m);
2154 EXPECT_EQ(
"contains some element that isn't equal to 1", Describe(m2));
2157TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
2158 vector<int> some_vector;
2159 EXPECT_THAT(some_vector, Each(1));
2160 some_vector.push_back(3);
2161 EXPECT_THAT(some_vector, Not(Each(1)));
2162 EXPECT_THAT(some_vector, Each(3));
2163 some_vector.push_back(1);
2164 some_vector.push_back(2);
2165 EXPECT_THAT(some_vector, Not(Each(3)));
2166 EXPECT_THAT(some_vector, Each(Lt(3.5)));
2168 vector<std::string> another_vector;
2169 another_vector.push_back(
"fee");
2170 EXPECT_THAT(another_vector, Each(std::string(
"fee")));
2171 another_vector.push_back(
"fie");
2172 another_vector.push_back(
"foe");
2173 another_vector.push_back(
"fum");
2174 EXPECT_THAT(another_vector, Not(Each(std::string(
"fee"))));
2177TEST(EachTest, MatchesMapWhenAllElementsMatch) {
2178 map<const char*, int> my_map;
2179 const char* bar =
"a string";
2181 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
2183 map<std::string, int> another_map;
2184 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
2185 another_map[
"fee"] = 1;
2186 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
2187 another_map[
"fie"] = 2;
2188 another_map[
"foe"] = 3;
2189 another_map[
"fum"] = 4;
2190 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fee"), 1))));
2191 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fum"), 1))));
2192 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
2195TEST(EachTest, AcceptsMatcher) {
2196 const int a[] = {1, 2, 3};
2197 EXPECT_THAT(a, Each(Gt(0)));
2198 EXPECT_THAT(a, Not(Each(Gt(1))));
2201TEST(EachTest, WorksForNativeArrayAsTuple) {
2202 const int a[] = {1, 2};
2203 const int*
const pointer = a;
2204 EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0)));
2205 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1))));
2208TEST(EachTest, WorksWithMoveOnly) {
2209 ContainerHelper helper;
2210 EXPECT_CALL(helper, Call(Each(Pointee(Gt(0)))));
2211 helper.Call(MakeUniquePtrs({1, 2}));
2215class IsHalfOfMatcher {
2217 template <
typename T1,
typename T2>
2218 bool MatchAndExplain(
const std::tuple<T1, T2>& a_pair,
2219 MatchResultListener* listener)
const {
2220 if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
2221 *listener <<
"where the second is " << std::get<1>(a_pair);
2224 *listener <<
"where the second/2 is " << std::get<1>(a_pair) / 2;
2229 void DescribeTo(ostream* os)
const {
2230 *os <<
"are a pair where the first is half of the second";
2233 void DescribeNegationTo(ostream* os)
const {
2234 *os <<
"are a pair where the first isn't half of the second";
2238PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
2239 return MakePolymorphicMatcher(IsHalfOfMatcher());
2242TEST(PointwiseTest, DescribesSelf) {
2247 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
2249 "contains 3 values, where each value and its corresponding value "
2250 "in { 1, 2, 3 } are a pair where the first is half of the second",
2253 "doesn't contain exactly 3 values, or contains a value x at some "
2254 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
2255 "where the first isn't half of the second",
2256 DescribeNegation(m));
2259TEST(PointwiseTest, MakesCopyOfRhs) {
2260 list<signed char> rhs;
2265 const Matcher<
const int(&)[2]> m = Pointwise(IsHalfOf(), rhs);
2266 EXPECT_THAT(lhs, m);
2270 EXPECT_THAT(lhs, m);
2273TEST(PointwiseTest, WorksForLhsNativeArray) {
2274 const int lhs[] = {1, 2, 3};
2279 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
2280 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
2283TEST(PointwiseTest, WorksForRhsNativeArray) {
2284 const int rhs[] = {1, 2, 3};
2289 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
2290 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
2294TEST(PointwiseTest, WorksForVectorOfBool) {
2295 vector<bool> rhs(3,
false);
2297 vector<bool> lhs = rhs;
2298 EXPECT_THAT(lhs, Pointwise(Eq(), rhs));
2300 EXPECT_THAT(lhs, Not(Pointwise(Eq(), rhs)));
2303TEST(PointwiseTest, WorksForRhsInitializerList) {
2304 const vector<int> lhs{2, 4, 6};
2305 EXPECT_THAT(lhs, Pointwise(Gt(), {1, 2, 3}));
2306 EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));
2309TEST(PointwiseTest, RejectsWrongSize) {
2310 const double lhs[2] = {1, 2};
2311 const int rhs[1] = {0};
2312 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
2313 EXPECT_EQ(
"which contains 2 values", Explain(Pointwise(Gt(), rhs), lhs));
2315 const int rhs2[3] = {0, 1, 2};
2316 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
2319TEST(PointwiseTest, RejectsWrongContent) {
2320 const double lhs[3] = {1, 2, 3};
2321 const int rhs[3] = {2, 6, 4};
2322 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
2324 "where the value pair (2, 6) at index #1 don't match, "
2325 "where the second/2 is 3",
2326 Explain(Pointwise(IsHalfOf(), rhs), lhs));
2329TEST(PointwiseTest, AcceptsCorrectContent) {
2330 const double lhs[3] = {1, 2, 3};
2331 const int rhs[3] = {2, 4, 6};
2332 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
2333 EXPECT_EQ(
"", Explain(Pointwise(IsHalfOf(), rhs), lhs));
2336TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
2337 const double lhs[3] = {1, 2, 3};
2338 const int rhs[3] = {2, 4, 6};
2339 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
2340 EXPECT_THAT(lhs, Pointwise(m1, rhs));
2341 EXPECT_EQ(
"", Explain(Pointwise(m1, rhs), lhs));
2345 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
2346 EXPECT_THAT(lhs, Pointwise(m2, rhs));
2347 EXPECT_EQ(
"", Explain(Pointwise(m2, rhs), lhs));
2350MATCHER(PointeeEquals,
"Points to an equal value") {
2351 return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),
2352 ::testing::get<0>(arg), result_listener);
2355TEST(PointwiseTest, WorksWithMoveOnly) {
2356 ContainerHelper helper;
2357 EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));
2358 helper.Call(MakeUniquePtrs({1, 2}));
2361TEST(UnorderedPointwiseTest, DescribesSelf) {
2366 const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs);
2368 "has 3 elements and there exists some permutation of elements such "
2370 " - element #0 and 1 are a pair where the first is half of the second, "
2372 " - element #1 and 2 are a pair where the first is half of the second, "
2374 " - element #2 and 3 are a pair where the first is half of the second",
2377 "doesn't have 3 elements, or there exists no permutation of elements "
2379 " - element #0 and 1 are a pair where the first is half of the second, "
2381 " - element #1 and 2 are a pair where the first is half of the second, "
2383 " - element #2 and 3 are a pair where the first is half of the second",
2384 DescribeNegation(m));
2387TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
2388 list<signed char> rhs;
2393 const Matcher<
const int(&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs);
2394 EXPECT_THAT(lhs, m);
2398 EXPECT_THAT(lhs, m);
2401TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
2402 const int lhs[] = {1, 2, 3};
2407 EXPECT_THAT(lhs, UnorderedPointwise(Lt(), rhs));
2408 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
2411TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
2412 const int rhs[] = {1, 2, 3};
2417 EXPECT_THAT(lhs, UnorderedPointwise(Gt(), rhs));
2418 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));
2421TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
2422 const vector<int> lhs{2, 4, 6};
2423 EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3}));
2424 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));
2427TEST(UnorderedPointwiseTest, RejectsWrongSize) {
2428 const double lhs[2] = {1, 2};
2429 const int rhs[1] = {0};
2430 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
2431 EXPECT_EQ(
"which has 2 elements",
2432 Explain(UnorderedPointwise(Gt(), rhs), lhs));
2434 const int rhs2[3] = {0, 1, 2};
2435 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2)));
2438TEST(UnorderedPointwiseTest, RejectsWrongContent) {
2439 const double lhs[3] = {1, 2, 3};
2440 const int rhs[3] = {2, 6, 6};
2441 EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs)));
2443 "where the following elements don't match any matchers:\n"
2445 Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs));
2448TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
2449 const double lhs[3] = {1, 2, 3};
2450 const int rhs[3] = {2, 4, 6};
2451 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
2454TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
2455 const double lhs[3] = {1, 2, 3};
2456 const int rhs[3] = {6, 4, 2};
2457 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
2460TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
2461 const double lhs[3] = {1, 2, 3};
2462 const int rhs[3] = {4, 6, 2};
2463 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
2464 EXPECT_THAT(lhs, UnorderedPointwise(m1, rhs));
2468 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
2469 EXPECT_THAT(lhs, UnorderedPointwise(m2, rhs));
2472TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
2473 ContainerHelper helper;
2474 EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),
2475 std::vector<int>{1, 2})));
2476 helper.Call(MakeUniquePtrs({2, 1}));
2479TEST(PointeeTest, WorksOnMoveOnlyType) {
2480 std::unique_ptr<int> p(
new int(3));
2481 EXPECT_THAT(p, Pointee(Eq(3)));
2482 EXPECT_THAT(p, Not(Pointee(Eq(2))));
2487 enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
2492 class MockMatcher :
public MatcherInterface<Behavior> {
2494 bool MatchAndExplain(Behavior behavior,
2495 MatchResultListener* listener)
const override {
2496 *listener <<
"[MatchAndExplain]";
2498 case kInitialSuccess:
2502 return !listener->IsInterested();
2512 return listener->IsInterested();
2515 GTEST_LOG_(FATAL) <<
"This should never be reached";
2519 void DescribeTo(ostream* os)
const override { *os <<
"[DescribeTo]"; }
2521 void DescribeNegationTo(ostream* os)
const override {
2522 *os <<
"[DescribeNegationTo]";
2526 AssertionResult RunPredicateFormatter(Behavior behavior) {
2527 auto matcher = MakeMatcher(
new MockMatcher);
2528 PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
2530 return predicate_formatter(
"dummy-name", behavior);
2534TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
2535 AssertionResult result = RunPredicateFormatter(kInitialSuccess);
2536 EXPECT_TRUE(result);
2538 EXPECT_EQ(expect, result.message());
2541TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
2542 AssertionResult result = RunPredicateFormatter(kAlwaysFail);
2543 EXPECT_FALSE(result);
2544 std::string expect =
2545 "Value of: dummy-name\nExpected: [DescribeTo]\n"
2547 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
2548 EXPECT_EQ(expect, result.message());
2551TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
2552 AssertionResult result = RunPredicateFormatter(kFlaky);
2553 EXPECT_FALSE(result);
2554 std::string expect =
2555 "Value of: dummy-name\nExpected: [DescribeTo]\n"
2556 " The matcher failed on the initial attempt; but passed when rerun to "
2557 "generate the explanation.\n"
2559 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
2560 EXPECT_EQ(expect, result.message());
2565TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
2566 Matcher<const vector<int>&> m = ElementsAre();
2567 EXPECT_EQ(
"is empty", Describe(m));
2570TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
2571 Matcher<vector<int>> m = ElementsAre(Gt(5));
2572 EXPECT_EQ(
"has 1 element that is > 5", Describe(m));
2575TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
2576 Matcher<list<std::string>> m = ElementsAre(StrEq(
"one"),
"two");
2578 "has 2 elements where\n"
2579 "element #0 is equal to \"one\",\n"
2580 "element #1 is equal to \"two\"",
2584TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
2585 Matcher<vector<int>> m = ElementsAre();
2586 EXPECT_EQ(
"isn't empty", DescribeNegation(m));
2589TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElement) {
2590 Matcher<const list<int>&> m = ElementsAre(Gt(5));
2592 "doesn't have 1 element, or\n"
2593 "element #0 isn't > 5",
2594 DescribeNegation(m));
2597TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
2598 Matcher<const list<std::string>&> m = ElementsAre(
"one",
"two");
2600 "doesn't have 2 elements, or\n"
2601 "element #0 isn't equal to \"one\", or\n"
2602 "element #1 isn't equal to \"two\"",
2603 DescribeNegation(m));
2606TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
2607 Matcher<const list<int>&> m = ElementsAre(1, Ne(2));
2609 list<int> test_list;
2610 test_list.push_back(1);
2611 test_list.push_back(3);
2612 EXPECT_EQ(
"", Explain(m, test_list));
2615TEST_P(ElementsAreTestP, ExplainsNonTrivialMatch) {
2616 Matcher<const vector<int>&> m =
2617 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
2619 const int a[] = {10, 0, 100};
2620 vector<int> test_vector(std::begin(a), std::end(a));
2622 "whose element #0 matches, which is 9 more than 1,\n"
2623 "and whose element #2 matches, which is 98 more than 2",
2624 Explain(m, test_vector));
2627TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
2628 Matcher<const list<int>&> m = ElementsAre(1, 3);
2630 list<int> test_list;
2632 EXPECT_EQ(
"", Explain(m, test_list));
2634 test_list.push_back(1);
2635 EXPECT_EQ(
"which has 1 element", Explain(m, test_list));
2638TEST_P(ElementsAreTestP, CanExplainMismatchRightSize) {
2639 Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));
2644 EXPECT_EQ(
"whose element #0 doesn't match", Explain(m, v));
2647 EXPECT_EQ(
"whose element #1 doesn't match, which is 4 less than 5",
2651TEST(ElementsAreTest, MatchesOneElementVector) {
2652 vector<std::string> test_vector;
2653 test_vector.push_back(
"test string");
2655 EXPECT_THAT(test_vector, ElementsAre(StrEq(
"test string")));
2658TEST(ElementsAreTest, MatchesOneElementList) {
2659 list<std::string> test_list;
2660 test_list.push_back(
"test string");
2662 EXPECT_THAT(test_list, ElementsAre(
"test string"));
2665TEST(ElementsAreTest, MatchesThreeElementVector) {
2666 vector<std::string> test_vector;
2667 test_vector.push_back(
"one");
2668 test_vector.push_back(
"two");
2669 test_vector.push_back(
"three");
2671 EXPECT_THAT(test_vector, ElementsAre(
"one", StrEq(
"two"), _));
2674TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
2675 vector<int> test_vector;
2676 test_vector.push_back(4);
2678 EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
2681TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
2682 vector<int> test_vector;
2683 test_vector.push_back(4);
2685 EXPECT_THAT(test_vector, ElementsAre(_));
2688TEST(ElementsAreTest, MatchesOneElementValue) {
2689 vector<int> test_vector;
2690 test_vector.push_back(4);
2692 EXPECT_THAT(test_vector, ElementsAre(4));
2695TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
2696 vector<int> test_vector;
2697 test_vector.push_back(1);
2698 test_vector.push_back(2);
2699 test_vector.push_back(3);
2701 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
2704TEST(ElementsAreTest, MatchesTenElementVector) {
2705 const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
2706 vector<int> test_vector(std::begin(a), std::end(a));
2708 EXPECT_THAT(test_vector,
2711 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
2714TEST(ElementsAreTest, DoesNotMatchWrongSize) {
2715 vector<std::string> test_vector;
2716 test_vector.push_back(
"test string");
2717 test_vector.push_back(
"test string");
2719 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
2720 EXPECT_FALSE(m.Matches(test_vector));
2723TEST(ElementsAreTest, DoesNotMatchWrongValue) {
2724 vector<std::string> test_vector;
2725 test_vector.push_back(
"other string");
2727 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
2728 EXPECT_FALSE(m.Matches(test_vector));
2731TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
2732 vector<std::string> test_vector;
2733 test_vector.push_back(
"one");
2734 test_vector.push_back(
"three");
2735 test_vector.push_back(
"two");
2737 Matcher<vector<std::string>> m =
2738 ElementsAre(StrEq(
"one"), StrEq(
"two"), StrEq(
"three"));
2739 EXPECT_FALSE(m.Matches(test_vector));
2742TEST(ElementsAreTest, WorksForNestedContainer) {
2743 constexpr std::array<const char*, 2> strings = {{
"Hi",
"world"}};
2745 vector<list<char>> nested;
2746 for (
const auto& s : strings) {
2747 nested.emplace_back(s, s + strlen(s));
2750 EXPECT_THAT(nested, ElementsAre(ElementsAre(
'H', Ne(
'e')),
2751 ElementsAre(
'w',
'o', _, _,
'd')));
2752 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre(
'H',
'e'),
2753 ElementsAre(
'w',
'o', _, _,
'd'))));
2756TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
2757 int a[] = {0, 1, 2};
2758 vector<int> v(std::begin(a), std::end(a));
2760 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
2761 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
2764TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
2765 int a[] = {0, 1, 2};
2766 vector<int> v(std::begin(a), std::end(a));
2768 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
2769 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
2772TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
2773 int array[] = {0, 1, 2};
2774 EXPECT_THAT(array, ElementsAre(0, 1, _));
2775 EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
2776 EXPECT_THAT(array, Not(ElementsAre(0, _)));
2779class NativeArrayPassedAsPointerAndSize {
2781 NativeArrayPassedAsPointerAndSize() {}
2783 MOCK_METHOD(
void, Helper, (
int* array,
int size));
2786 NativeArrayPassedAsPointerAndSize(
const NativeArrayPassedAsPointerAndSize&) =
2788 NativeArrayPassedAsPointerAndSize& operator=(
2789 const NativeArrayPassedAsPointerAndSize&) =
delete;
2792TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
2793 int array[] = {0, 1};
2794 ::std::tuple<int*, size_t> array_as_tuple(array, 2);
2795 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
2796 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
2798 NativeArrayPassedAsPointerAndSize helper;
2799 EXPECT_CALL(helper, Helper(_, _)).With(ElementsAre(0, 1));
2800 helper.Helper(array, 2);
2803TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
2804 const char a2[][3] = {
"hi",
"lo"};
2805 EXPECT_THAT(a2, ElementsAre(ElementsAre(
'h',
'i',
'\0'),
2806 ElementsAre(
'l',
'o',
'\0')));
2807 EXPECT_THAT(a2, ElementsAre(StrEq(
"hi"), StrEq(
"lo")));
2808 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre(
'h',
'o',
'\0')),
2809 ElementsAre(
'l',
'o',
'\0')));
2812TEST(ElementsAreTest, AcceptsStringLiteral) {
2813 std::string array[] = {
"hi",
"one",
"two"};
2814 EXPECT_THAT(array, ElementsAre(
"hi",
"one",
"two"));
2815 EXPECT_THAT(array, Not(ElementsAre(
"hi",
"one",
"too")));
2819extern const char kHi[];
2821TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
2825 std::string array1[] = {
"hi"};
2826 EXPECT_THAT(array1, ElementsAre(kHi));
2828 std::string array2[] = {
"ho"};
2829 EXPECT_THAT(array2, Not(ElementsAre(kHi)));
2832const char kHi[] =
"hi";
2834TEST(ElementsAreTest, MakesCopyOfArguments) {
2838 ::testing::internal::ElementsAreMatcher<std::tuple<int, int>>
2839 polymorphic_matcher = ElementsAre(x, y);
2842 const int array1[] = {1, 2};
2843 EXPECT_THAT(array1, polymorphic_matcher);
2844 const int array2[] = {0, 0};
2845 EXPECT_THAT(array2, Not(polymorphic_matcher));
2852TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
2853 const int a[] = {1, 2, 3};
2855 vector<int> test_vector(std::begin(a), std::end(a));
2856 EXPECT_THAT(test_vector, ElementsAreArray(a));
2859 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
2862TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
2863 std::array<const char*, 3> a = {{
"one",
"two",
"three"}};
2865 vector<std::string> test_vector(std::begin(a), std::end(a));
2866 EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size()));
2868 const char** p = a.data();
2869 test_vector[0] =
"1";
2870 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, a.size())));
2873TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
2874 const char* a[] = {
"one",
"two",
"three"};
2876 vector<std::string> test_vector(std::begin(a), std::end(a));
2877 EXPECT_THAT(test_vector, ElementsAreArray(a));
2879 test_vector[0] =
"1";
2880 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
2883TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
2884 const Matcher<std::string> kMatcherArray[] = {StrEq(
"one"), StrEq(
"two"),
2887 vector<std::string> test_vector;
2888 test_vector.push_back(
"one");
2889 test_vector.push_back(
"two");
2890 test_vector.push_back(
"three");
2891 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
2893 test_vector.push_back(
"three");
2894 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
2897TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
2898 const int a[] = {1, 2, 3};
2899 vector<int> test_vector(std::begin(a), std::end(a));
2900 const vector<int> expected(std::begin(a), std::end(a));
2901 EXPECT_THAT(test_vector, ElementsAreArray(expected));
2902 test_vector.push_back(4);
2903 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
2906TEST(ElementsAreArrayTest, TakesInitializerList) {
2907 const int a[5] = {1, 2, 3, 4, 5};
2908 EXPECT_THAT(a, ElementsAreArray({1, 2, 3, 4, 5}));
2909 EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 5, 4})));
2910 EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 4, 6})));
2913TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
2914 const std::string a[5] = {
"a",
"b",
"c",
"d",
"e"};
2915 EXPECT_THAT(a, ElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
2916 EXPECT_THAT(a, Not(ElementsAreArray({
"a",
"b",
"c",
"e",
"d"})));
2917 EXPECT_THAT(a, Not(ElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
2920TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
2921 const int a[5] = {1, 2, 3, 4, 5};
2922 EXPECT_THAT(a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
2923 EXPECT_THAT(a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
2926TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) {
2927 const int a[5] = {1, 2, 3, 4, 5};
2932 a, ElementsAreArray<Matcher<int>>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
2933 EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int>>(
2934 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
2937TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
2938 const int a[] = {1, 2, 3};
2939 const Matcher<int> kMatchers[] = {Eq(1), Eq(2), Eq(3)};
2940 vector<int> test_vector(std::begin(a), std::end(a));
2941 const vector<Matcher<int>> expected(std::begin(kMatchers),
2942 std::end(kMatchers));
2943 EXPECT_THAT(test_vector, ElementsAreArray(expected));
2944 test_vector.push_back(4);
2945 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
2948TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
2949 const int a[] = {1, 2, 3};
2950 const vector<int> test_vector(std::begin(a), std::end(a));
2951 const vector<int> expected(std::begin(a), std::end(a));
2952 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
2954 EXPECT_THAT(test_vector, ElementsAreArray(std::begin(a), std::end(a)));
2956 int*
const null_int =
nullptr;
2957 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
2958 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
2963TEST(ElementsAreArrayTest, WorksWithNativeArray) {
2964 ::std::string a[] = {
"hi",
"ho"};
2965 ::std::string b[] = {
"hi",
"ho"};
2967 EXPECT_THAT(a, ElementsAreArray(b));
2968 EXPECT_THAT(a, ElementsAreArray(b, 2));
2969 EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
2972TEST(ElementsAreArrayTest, SourceLifeSpan) {
2973 const int a[] = {1, 2, 3};
2974 vector<int> test_vector(std::begin(a), std::end(a));
2975 vector<int> expect(std::begin(a), std::end(a));
2976 ElementsAreArrayMatcher<int> matcher_maker =
2977 ElementsAreArray(expect.begin(), expect.end());
2978 EXPECT_THAT(test_vector, matcher_maker);
2981 for (
int& i : expect) {
2984 EXPECT_THAT(test_vector, matcher_maker);
2985 test_vector.push_back(3);
2986 EXPECT_THAT(test_vector, Not(matcher_maker));
2991INSTANTIATE_GTEST_MATCHER_TEST_P(ContainsTest);
2993TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
2994 list<int> some_list;
2995 some_list.push_back(3);
2996 some_list.push_back(1);
2997 some_list.push_back(2);
2998 some_list.push_back(3);
2999 EXPECT_THAT(some_list, Contains(1));
3000 EXPECT_THAT(some_list, Contains(Gt(2.5)));
3001 EXPECT_THAT(some_list, Contains(Eq(2.0f)));
3003 list<std::string> another_list;
3004 another_list.push_back(
"fee");
3005 another_list.push_back(
"fie");
3006 another_list.push_back(
"foe");
3007 another_list.push_back(
"fum");
3008 EXPECT_THAT(another_list, Contains(std::string(
"fee")));
3011TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
3012 list<int> some_list;
3013 some_list.push_back(3);
3014 some_list.push_back(1);
3015 EXPECT_THAT(some_list, Not(Contains(4)));
3018TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
3023 EXPECT_THAT(some_set, Contains(Eq(1.0)));
3024 EXPECT_THAT(some_set, Contains(Eq(3.0f)));
3025 EXPECT_THAT(some_set, Contains(2));
3027 set<std::string> another_set;
3028 another_set.insert(
"fee");
3029 another_set.insert(
"fie");
3030 another_set.insert(
"foe");
3031 another_set.insert(
"fum");
3032 EXPECT_THAT(another_set, Contains(Eq(std::string(
"fum"))));
3035TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
3039 EXPECT_THAT(some_set, Not(Contains(4)));
3041 set<std::string> c_string_set;
3042 c_string_set.insert(
"hello");
3043 EXPECT_THAT(c_string_set, Not(Contains(std::string(
"goodbye"))));
3046TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) {
3047 const int a[2] = {1, 2};
3048 Matcher<
const int(&)[2]> m = Contains(2);
3049 EXPECT_EQ(
"whose element #1 matches", Explain(m, a));
3052 EXPECT_EQ(
"", Explain(m, a));
3054 m = Contains(GreaterThan(0));
3055 EXPECT_EQ(
"whose element #0 matches, which is 1 more than 0", Explain(m, a));
3057 m = Contains(GreaterThan(10));
3058 EXPECT_EQ(
"", Explain(m, a));
3061TEST(ContainsTest, DescribesItselfCorrectly) {
3062 Matcher<vector<int>> m = Contains(1);
3063 EXPECT_EQ(
"contains at least one element that is equal to 1", Describe(m));
3065 Matcher<vector<int>> m2 = Not(m);
3066 EXPECT_EQ(
"doesn't contain any element that is equal to 1", Describe(m2));
3069TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
3070 map<std::string, int> my_map;
3071 const char* bar =
"a string";
3073 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
3075 map<std::string, int> another_map;
3076 another_map[
"fee"] = 1;
3077 another_map[
"fie"] = 2;
3078 another_map[
"foe"] = 3;
3079 another_map[
"fum"] = 4;
3080 EXPECT_THAT(another_map,
3081 Contains(pair<const std::string, int>(std::string(
"fee"), 1)));
3082 EXPECT_THAT(another_map, Contains(pair<const std::string, int>(
"fie", 2)));
3085TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
3086 map<int, int> some_map;
3089 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
3092TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
3093 const char* string_array[] = {
"fee",
"fie",
"foe",
"fum"};
3094 EXPECT_THAT(string_array, Contains(Eq(std::string(
"fum"))));
3097TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
3098 int int_array[] = {1, 2, 3, 4};
3099 EXPECT_THAT(int_array, Not(Contains(5)));
3102TEST(ContainsTest, AcceptsMatcher) {
3103 const int a[] = {1, 2, 3};
3104 EXPECT_THAT(a, Contains(Gt(2)));
3105 EXPECT_THAT(a, Not(Contains(Gt(4))));
3108TEST(ContainsTest, WorksForNativeArrayAsTuple) {
3109 const int a[] = {1, 2};
3110 const int*
const pointer = a;
3111 EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1));
3112 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3))));
3115TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
3116 int a[][3] = {{1, 2, 3}, {4, 5, 6}};
3117 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
3118 EXPECT_THAT(a, Contains(Contains(5)));
3119 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
3120 EXPECT_THAT(a, Contains(Not(Contains(5))));