Path: blob/main/contrib/googletest/googlemock/test/gmock-matchers-containers_test.cc
48255 views
// Copyright 2007, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Mock - a framework for writing C++ mock classes.30//31// This file tests some commonly used argument matchers.3233#include <algorithm>34#include <array>35#include <deque>36#include <forward_list>37#include <iterator>38#include <list>39#include <memory>40#include <ostream>41#include <string>42#include <tuple>43#include <vector>4445#include "gtest/gtest.h"4647// Silence warning C4244: 'initializing': conversion from 'int' to 'short',48// possible loss of data and C4100, unreferenced local parameter49GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)5051#include "test/gmock-matchers_test.h"5253namespace testing {54namespace gmock_matchers_test {55namespace {5657std::vector<std::unique_ptr<int>> MakeUniquePtrs(const std::vector<int>& ints) {58std::vector<std::unique_ptr<int>> pointers;59for (int i : ints) pointers.emplace_back(new int(i));60return pointers;61}6263std::string OfType(const std::string& type_name) {64#if GTEST_HAS_RTTI65return IsReadableTypeName(type_name) ? " (of type " + type_name + ")" : "";66#else67return "";68#endif69}7071TEST(ContainsTest, WorksWithMoveOnly) {72ContainerHelper helper;73EXPECT_CALL(helper, Call(Contains(Pointee(2))));74helper.Call(MakeUniquePtrs({1, 2}));75}7677INSTANTIATE_GTEST_MATCHER_TEST_P(ElementsAreTest);7879// Tests the variadic version of the ElementsAreMatcher80TEST(ElementsAreTest, HugeMatcher) {81vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};8283EXPECT_THAT(test_vector,84ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7),85Eq(8), Eq(9), Eq(10), Gt(1), Eq(12)));86}8788// Tests the variadic version of the UnorderedElementsAreMatcher89TEST(ElementsAreTest, HugeMatcherStr) {90vector<std::string> test_vector{91"literal_string", "", "", "", "", "", "", "", "", "", "", ""};9293EXPECT_THAT(test_vector, UnorderedElementsAre("literal_string", _, _, _, _, _,94_, _, _, _, _, _));95}9697// Tests the variadic version of the UnorderedElementsAreMatcher98TEST(ElementsAreTest, HugeMatcherUnordered) {99vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};100101EXPECT_THAT(test_vector, UnorderedElementsAre(102Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7),103Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));104}105106// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value107// matches the matcher.108TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {109ASSERT_THAT(5, Ge(2)) << "This should succeed.";110ASSERT_THAT("Foo", EndsWith("oo"));111EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";112EXPECT_THAT("Hello", StartsWith("Hell"));113}114115// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value116// doesn't match the matcher.117TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {118// 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),119// which cannot reference auto variables.120static unsigned short n; // NOLINT121n = 5;122123EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)),124"Value of: n\n"125"Expected: is > 10\n"126" Actual: 5" +127OfType("unsigned short"));128n = 0;129EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))),130"Value of: n\n"131"Expected: (is <= 7) and (is >= 5)\n"132" Actual: 0" +133OfType("unsigned short"));134}135136// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument137// has a reference type.138TEST(MatcherAssertionTest, WorksForByRefArguments) {139// We use a static variable here as EXPECT_FATAL_FAILURE() cannot140// reference auto variables.141static int n;142n = 0;143EXPECT_THAT(n, AllOf(Le(7), Ref(n)));144EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),145"Value of: n\n"146"Expected: does not reference the variable @");147// Tests the "Actual" part.148EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),149"Actual: 0" + OfType("int") + ", which is located @");150}151152// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is153// monomorphic.154TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {155Matcher<const char*> starts_with_he = StartsWith("he");156ASSERT_THAT("hello", starts_with_he);157158Matcher<const std::string&> ends_with_ok = EndsWith("ok");159ASSERT_THAT("book", ends_with_ok);160const std::string bad = "bad";161EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),162"Value of: bad\n"163"Expected: ends with \"ok\"\n"164" Actual: \"bad\"");165Matcher<int> is_greater_than_5 = Gt(5);166EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),167"Value of: 5\n"168"Expected: is > 5\n"169" Actual: 5" +170OfType("int"));171}172173TEST(PointeeTest, RawPointer) {174const Matcher<int*> m = Pointee(Ge(0));175176int n = 1;177EXPECT_TRUE(m.Matches(&n));178n = -1;179EXPECT_FALSE(m.Matches(&n));180EXPECT_FALSE(m.Matches(nullptr));181}182183TEST(PointeeTest, RawPointerToConst) {184const Matcher<const double*> m = Pointee(Ge(0));185186double x = 1;187EXPECT_TRUE(m.Matches(&x));188x = -1;189EXPECT_FALSE(m.Matches(&x));190EXPECT_FALSE(m.Matches(nullptr));191}192193TEST(PointeeTest, ReferenceToConstRawPointer) {194const Matcher<int* const&> m = Pointee(Ge(0));195196int n = 1;197EXPECT_TRUE(m.Matches(&n));198n = -1;199EXPECT_FALSE(m.Matches(&n));200EXPECT_FALSE(m.Matches(nullptr));201}202203TEST(PointeeTest, ReferenceToNonConstRawPointer) {204const Matcher<double*&> m = Pointee(Ge(0));205206double x = 1.0;207double* p = &x;208EXPECT_TRUE(m.Matches(p));209x = -1;210EXPECT_FALSE(m.Matches(p));211p = nullptr;212EXPECT_FALSE(m.Matches(p));213}214215TEST(PointeeTest, SmartPointer) {216const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0));217218std::unique_ptr<int> n(new int(1));219EXPECT_TRUE(m.Matches(n));220}221222TEST(PointeeTest, SmartPointerToConst) {223const Matcher<std::unique_ptr<const int>> m = Pointee(Ge(0));224225// There's no implicit conversion from unique_ptr<int> to const226// unique_ptr<const int>, so we must pass a unique_ptr<const int> into the227// matcher.228std::unique_ptr<const int> n(new int(1));229EXPECT_TRUE(m.Matches(n));230}231232TEST(PointerTest, RawPointer) {233int n = 1;234const Matcher<int*> m = Pointer(Eq(&n));235236EXPECT_TRUE(m.Matches(&n));237238int* p = nullptr;239EXPECT_FALSE(m.Matches(p));240EXPECT_FALSE(m.Matches(nullptr));241}242243TEST(PointerTest, RawPointerToConst) {244int n = 1;245const Matcher<const int*> m = Pointer(Eq(&n));246247EXPECT_TRUE(m.Matches(&n));248249int* p = nullptr;250EXPECT_FALSE(m.Matches(p));251EXPECT_FALSE(m.Matches(nullptr));252}253254TEST(PointerTest, SmartPointer) {255std::unique_ptr<int> n(new int(10));256int* raw_n = n.get();257const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n));258259EXPECT_TRUE(m.Matches(n));260}261262TEST(PointerTest, SmartPointerToConst) {263std::unique_ptr<const int> n(new int(10));264const int* raw_n = n.get();265const Matcher<std::unique_ptr<const int>> m = Pointer(Eq(raw_n));266267// There's no implicit conversion from unique_ptr<int> to const268// unique_ptr<const int>, so we must pass a unique_ptr<const int> into the269// matcher.270std::unique_ptr<const int> p(new int(10));271EXPECT_FALSE(m.Matches(p));272}273274// Minimal const-propagating pointer.275template <typename T>276class ConstPropagatingPtr {277public:278typedef T element_type;279280ConstPropagatingPtr() : val_() {}281explicit ConstPropagatingPtr(T* t) : val_(t) {}282ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}283284T* get() { return val_; }285T& operator*() { return *val_; }286// Most smart pointers return non-const T* and T& from the next methods.287const T* get() const { return val_; }288const T& operator*() const { return *val_; }289290private:291T* val_;292};293294INSTANTIATE_GTEST_MATCHER_TEST_P(PointeeTest);295296TEST(PointeeTest, WorksWithConstPropagatingPointers) {297const Matcher<ConstPropagatingPtr<int>> m = Pointee(Lt(5));298int three = 3;299const ConstPropagatingPtr<int> co(&three);300ConstPropagatingPtr<int> o(&three);301EXPECT_TRUE(m.Matches(o));302EXPECT_TRUE(m.Matches(co));303*o = 6;304EXPECT_FALSE(m.Matches(o));305EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));306}307308TEST(PointeeTest, NeverMatchesNull) {309const Matcher<const char*> m = Pointee(_);310EXPECT_FALSE(m.Matches(nullptr));311}312313// Tests that we can write Pointee(value) instead of Pointee(Eq(value)).314TEST(PointeeTest, MatchesAgainstAValue) {315const Matcher<int*> m = Pointee(5);316317int n = 5;318EXPECT_TRUE(m.Matches(&n));319n = -1;320EXPECT_FALSE(m.Matches(&n));321EXPECT_FALSE(m.Matches(nullptr));322}323324TEST(PointeeTest, CanDescribeSelf) {325const Matcher<int*> m = Pointee(Gt(3));326EXPECT_EQ("points to a value that is > 3", Describe(m));327EXPECT_EQ("does not point to a value that is > 3", DescribeNegation(m));328}329330TEST_P(PointeeTestP, CanExplainMatchResult) {331const Matcher<const std::string*> m = Pointee(StartsWith("Hi"));332333EXPECT_EQ("", Explain(m, static_cast<const std::string*>(nullptr)));334335const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT336long n = 3; // NOLINT337EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",338Explain(m2, &n));339}340341TEST(PointeeTest, AlwaysExplainsPointee) {342const Matcher<int*> m = Pointee(0);343int n = 42;344EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));345}346347// An uncopyable class.348class Uncopyable {349public:350Uncopyable() : value_(-1) {}351explicit Uncopyable(int a_value) : value_(a_value) {}352353int value() const { return value_; }354void set_value(int i) { value_ = i; }355356private:357int value_;358Uncopyable(const Uncopyable&) = delete;359Uncopyable& operator=(const Uncopyable&) = delete;360};361362// Returns true if and only if x.value() is positive.363bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }364365MATCHER_P(UncopyableIs, inner_matcher, "") {366return ExplainMatchResult(inner_matcher, arg.value(), result_listener);367}368369// A user-defined struct for testing Field().370struct AStruct {371AStruct() : x(0), y(1.0), z(5), p(nullptr) {}372AStruct(const AStruct& rhs)373: x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}374375int x; // A non-const field.376const double y; // A const field.377Uncopyable z; // An uncopyable field.378const char* p; // A pointer field.379};380381// A derived struct for testing Field().382struct DerivedStruct : public AStruct {383char ch;384};385386INSTANTIATE_GTEST_MATCHER_TEST_P(FieldTest);387388// Tests that Field(&Foo::field, ...) works when field is non-const.389TEST(FieldTest, WorksForNonConstField) {390Matcher<AStruct> m = Field(&AStruct::x, Ge(0));391Matcher<AStruct> m_with_name = Field("x", &AStruct::x, Ge(0));392393AStruct a;394EXPECT_TRUE(m.Matches(a));395EXPECT_TRUE(m_with_name.Matches(a));396a.x = -1;397EXPECT_FALSE(m.Matches(a));398EXPECT_FALSE(m_with_name.Matches(a));399}400401// Tests that Field(&Foo::field, ...) works when field is const.402TEST(FieldTest, WorksForConstField) {403AStruct a;404405Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));406Matcher<AStruct> m_with_name = Field("y", &AStruct::y, Ge(0.0));407EXPECT_TRUE(m.Matches(a));408EXPECT_TRUE(m_with_name.Matches(a));409m = Field(&AStruct::y, Le(0.0));410m_with_name = Field("y", &AStruct::y, Le(0.0));411EXPECT_FALSE(m.Matches(a));412EXPECT_FALSE(m_with_name.Matches(a));413}414415// Tests that Field(&Foo::field, ...) works when field is not copyable.416TEST(FieldTest, WorksForUncopyableField) {417AStruct a;418419Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));420EXPECT_TRUE(m.Matches(a));421m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));422EXPECT_FALSE(m.Matches(a));423}424425// Tests that Field(&Foo::field, ...) works when field is a pointer.426TEST(FieldTest, WorksForPointerField) {427// Matching against NULL.428Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(nullptr));429AStruct a;430EXPECT_TRUE(m.Matches(a));431a.p = "hi";432EXPECT_FALSE(m.Matches(a));433434// Matching a pointer that is not NULL.435m = Field(&AStruct::p, StartsWith("hi"));436a.p = "hill";437EXPECT_TRUE(m.Matches(a));438a.p = "hole";439EXPECT_FALSE(m.Matches(a));440}441442// Tests that Field() works when the object is passed by reference.443TEST(FieldTest, WorksForByRefArgument) {444Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));445446AStruct a;447EXPECT_TRUE(m.Matches(a));448a.x = -1;449EXPECT_FALSE(m.Matches(a));450}451452// Tests that Field(&Foo::field, ...) works when the argument's type453// is a sub-type of Foo.454TEST(FieldTest, WorksForArgumentOfSubType) {455// Note that the matcher expects DerivedStruct but we say AStruct456// inside Field().457Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));458459DerivedStruct d;460EXPECT_TRUE(m.Matches(d));461d.x = -1;462EXPECT_FALSE(m.Matches(d));463}464465// Tests that Field(&Foo::field, m) works when field's type and m's466// argument type are compatible but not the same.467TEST(FieldTest, WorksForCompatibleMatcherType) {468// The field is an int, but the inner matcher expects a signed char.469Matcher<const AStruct&> m = Field(&AStruct::x, Matcher<signed char>(Ge(0)));470471AStruct a;472EXPECT_TRUE(m.Matches(a));473a.x = -1;474EXPECT_FALSE(m.Matches(a));475}476477// Tests that Field() can describe itself.478TEST(FieldTest, CanDescribeSelf) {479Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));480481EXPECT_EQ("is an object whose given field is >= 0", Describe(m));482EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));483}484485TEST(FieldTest, CanDescribeSelfWithFieldName) {486Matcher<const AStruct&> m = Field("field_name", &AStruct::x, Ge(0));487488EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m));489EXPECT_EQ("is an object whose field `field_name` isn't >= 0",490DescribeNegation(m));491}492493// Tests that Field() can explain the match result.494TEST_P(FieldTestP, CanExplainMatchResult) {495Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));496497AStruct a;498a.x = 1;499EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));500501m = Field(&AStruct::x, GreaterThan(0));502EXPECT_EQ(503"whose given field is 1" + OfType("int") + ", which is 1 more than 0",504Explain(m, a));505}506507TEST_P(FieldTestP, CanExplainMatchResultWithFieldName) {508Matcher<const AStruct&> m = Field("field_name", &AStruct::x, Ge(0));509510AStruct a;511a.x = 1;512EXPECT_EQ("whose field `field_name` is 1" + OfType("int"), Explain(m, a));513514m = Field("field_name", &AStruct::x, GreaterThan(0));515EXPECT_EQ("whose field `field_name` is 1" + OfType("int") +516", which is 1 more than 0",517Explain(m, a));518}519520INSTANTIATE_GTEST_MATCHER_TEST_P(FieldForPointerTest);521522// Tests that Field() works when the argument is a pointer to const.523TEST(FieldForPointerTest, WorksForPointerToConst) {524Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));525526AStruct a;527EXPECT_TRUE(m.Matches(&a));528a.x = -1;529EXPECT_FALSE(m.Matches(&a));530}531532// Tests that Field() works when the argument is a pointer to non-const.533TEST(FieldForPointerTest, WorksForPointerToNonConst) {534Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));535536AStruct a;537EXPECT_TRUE(m.Matches(&a));538a.x = -1;539EXPECT_FALSE(m.Matches(&a));540}541542// Tests that Field() works when the argument is a reference to a const pointer.543TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {544Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));545546AStruct a;547EXPECT_TRUE(m.Matches(&a));548a.x = -1;549EXPECT_FALSE(m.Matches(&a));550}551552// Tests that Field() does not match the NULL pointer.553TEST(FieldForPointerTest, DoesNotMatchNull) {554Matcher<const AStruct*> m = Field(&AStruct::x, _);555EXPECT_FALSE(m.Matches(nullptr));556}557558// Tests that Field(&Foo::field, ...) works when the argument's type559// is a sub-type of const Foo*.560TEST(FieldForPointerTest, WorksForArgumentOfSubType) {561// Note that the matcher expects DerivedStruct but we say AStruct562// inside Field().563Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));564565DerivedStruct d;566EXPECT_TRUE(m.Matches(&d));567d.x = -1;568EXPECT_FALSE(m.Matches(&d));569}570571// Tests that Field() can describe itself when used to match a pointer.572TEST(FieldForPointerTest, CanDescribeSelf) {573Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));574575EXPECT_EQ("is an object whose given field is >= 0", Describe(m));576EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));577}578579TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {580Matcher<const AStruct*> m = Field("field_name", &AStruct::x, Ge(0));581582EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m));583EXPECT_EQ("is an object whose field `field_name` isn't >= 0",584DescribeNegation(m));585}586587// Tests that Field() can explain the result of matching a pointer.588TEST_P(FieldForPointerTestP, CanExplainMatchResult) {589Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));590591AStruct a;592a.x = 1;593EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(nullptr)));594EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),595Explain(m, &a));596597m = Field(&AStruct::x, GreaterThan(0));598EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +599", which is 1 more than 0",600Explain(m, &a));601}602603TEST_P(FieldForPointerTestP, CanExplainMatchResultWithFieldName) {604Matcher<const AStruct*> m = Field("field_name", &AStruct::x, Ge(0));605606AStruct a;607a.x = 1;608EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(nullptr)));609EXPECT_EQ(610"which points to an object whose field `field_name` is 1" + OfType("int"),611Explain(m, &a));612613m = Field("field_name", &AStruct::x, GreaterThan(0));614EXPECT_EQ("which points to an object whose field `field_name` is 1" +615OfType("int") + ", which is 1 more than 0",616Explain(m, &a));617}618619// A user-defined class for testing Property().620class AClass {621public:622AClass() : n_(0) {}623624// A getter that returns a non-reference.625int n() const { return n_; }626627void set_n(int new_n) { n_ = new_n; }628629// A getter that returns a reference to const.630const std::string& s() const { return s_; }631632const std::string& s_ref() const& { return s_; }633634void set_s(const std::string& new_s) { s_ = new_s; }635636// A getter that returns a reference to non-const.637double& x() const { return x_; }638639private:640int n_;641std::string s_;642643static double x_;644};645646double AClass::x_ = 0.0;647648// A derived class for testing Property().649class DerivedClass : public AClass {650public:651int k() const { return k_; }652653private:654int k_;655};656657INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyTest);658659// Tests that Property(&Foo::property, ...) works when property()660// returns a non-reference.661TEST(PropertyTest, WorksForNonReferenceProperty) {662Matcher<const AClass&> m = Property(&AClass::n, Ge(0));663Matcher<const AClass&> m_with_name = Property("n", &AClass::n, Ge(0));664665AClass a;666a.set_n(1);667EXPECT_TRUE(m.Matches(a));668EXPECT_TRUE(m_with_name.Matches(a));669670a.set_n(-1);671EXPECT_FALSE(m.Matches(a));672EXPECT_FALSE(m_with_name.Matches(a));673}674675// Tests that Property(&Foo::property, ...) works when property()676// returns a reference to const.677TEST(PropertyTest, WorksForReferenceToConstProperty) {678Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));679Matcher<const AClass&> m_with_name =680Property("s", &AClass::s, StartsWith("hi"));681682AClass a;683a.set_s("hill");684EXPECT_TRUE(m.Matches(a));685EXPECT_TRUE(m_with_name.Matches(a));686687a.set_s("hole");688EXPECT_FALSE(m.Matches(a));689EXPECT_FALSE(m_with_name.Matches(a));690}691692// Tests that Property(&Foo::property, ...) works when property() is693// ref-qualified.694TEST(PropertyTest, WorksForRefQualifiedProperty) {695Matcher<const AClass&> m = Property(&AClass::s_ref, StartsWith("hi"));696Matcher<const AClass&> m_with_name =697Property("s", &AClass::s_ref, StartsWith("hi"));698699AClass a;700a.set_s("hill");701EXPECT_TRUE(m.Matches(a));702EXPECT_TRUE(m_with_name.Matches(a));703704a.set_s("hole");705EXPECT_FALSE(m.Matches(a));706EXPECT_FALSE(m_with_name.Matches(a));707}708709// Tests that Property(&Foo::property, ...) works when property()710// returns a reference to non-const.711TEST(PropertyTest, WorksForReferenceToNonConstProperty) {712double x = 0.0;713AClass a;714715Matcher<const AClass&> m = Property(&AClass::x, Ref(x));716EXPECT_FALSE(m.Matches(a));717718m = Property(&AClass::x, Not(Ref(x)));719EXPECT_TRUE(m.Matches(a));720}721722// Tests that Property(&Foo::property, ...) works when the argument is723// passed by value.724TEST(PropertyTest, WorksForByValueArgument) {725Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));726727AClass a;728a.set_s("hill");729EXPECT_TRUE(m.Matches(a));730731a.set_s("hole");732EXPECT_FALSE(m.Matches(a));733}734735// Tests that Property(&Foo::property, ...) works when the argument's736// type is a sub-type of Foo.737TEST(PropertyTest, WorksForArgumentOfSubType) {738// The matcher expects a DerivedClass, but inside the Property() we739// say AClass.740Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));741742DerivedClass d;743d.set_n(1);744EXPECT_TRUE(m.Matches(d));745746d.set_n(-1);747EXPECT_FALSE(m.Matches(d));748}749750// Tests that Property(&Foo::property, m) works when property()'s type751// and m's argument type are compatible but different.752TEST(PropertyTest, WorksForCompatibleMatcherType) {753// n() returns an int but the inner matcher expects a signed char.754Matcher<const AClass&> m = Property(&AClass::n, Matcher<signed char>(Ge(0)));755756Matcher<const AClass&> m_with_name =757Property("n", &AClass::n, Matcher<signed char>(Ge(0)));758759AClass a;760EXPECT_TRUE(m.Matches(a));761EXPECT_TRUE(m_with_name.Matches(a));762a.set_n(-1);763EXPECT_FALSE(m.Matches(a));764EXPECT_FALSE(m_with_name.Matches(a));765}766767// Tests that Property() can describe itself.768TEST(PropertyTest, CanDescribeSelf) {769Matcher<const AClass&> m = Property(&AClass::n, Ge(0));770771EXPECT_EQ("is an object whose given property is >= 0", Describe(m));772EXPECT_EQ("is an object whose given property isn't >= 0",773DescribeNegation(m));774}775776TEST(PropertyTest, CanDescribeSelfWithPropertyName) {777Matcher<const AClass&> m = Property("fancy_name", &AClass::n, Ge(0));778779EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m));780EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0",781DescribeNegation(m));782}783784// Tests that Property() can explain the match result.785TEST_P(PropertyTestP, CanExplainMatchResult) {786Matcher<const AClass&> m = Property(&AClass::n, Ge(0));787788AClass a;789a.set_n(1);790EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));791792m = Property(&AClass::n, GreaterThan(0));793EXPECT_EQ(794"whose given property is 1" + OfType("int") + ", which is 1 more than 0",795Explain(m, a));796}797798TEST_P(PropertyTestP, CanExplainMatchResultWithPropertyName) {799Matcher<const AClass&> m = Property("fancy_name", &AClass::n, Ge(0));800801AClass a;802a.set_n(1);803EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int"), Explain(m, a));804805m = Property("fancy_name", &AClass::n, GreaterThan(0));806EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int") +807", which is 1 more than 0",808Explain(m, a));809}810811INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyForPointerTest);812813// Tests that Property() works when the argument is a pointer to const.814TEST(PropertyForPointerTest, WorksForPointerToConst) {815Matcher<const AClass*> m = Property(&AClass::n, Ge(0));816817AClass a;818a.set_n(1);819EXPECT_TRUE(m.Matches(&a));820821a.set_n(-1);822EXPECT_FALSE(m.Matches(&a));823}824825// Tests that Property() works when the argument is a pointer to non-const.826TEST(PropertyForPointerTest, WorksForPointerToNonConst) {827Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));828829AClass a;830a.set_s("hill");831EXPECT_TRUE(m.Matches(&a));832833a.set_s("hole");834EXPECT_FALSE(m.Matches(&a));835}836837// Tests that Property() works when the argument is a reference to a838// const pointer.839TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {840Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));841842AClass a;843a.set_s("hill");844EXPECT_TRUE(m.Matches(&a));845846a.set_s("hole");847EXPECT_FALSE(m.Matches(&a));848}849850// Tests that Property() does not match the NULL pointer.851TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {852Matcher<const AClass*> m = Property(&AClass::x, _);853EXPECT_FALSE(m.Matches(nullptr));854}855856// Tests that Property(&Foo::property, ...) works when the argument's857// type is a sub-type of const Foo*.858TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {859// The matcher expects a DerivedClass, but inside the Property() we860// say AClass.861Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));862863DerivedClass d;864d.set_n(1);865EXPECT_TRUE(m.Matches(&d));866867d.set_n(-1);868EXPECT_FALSE(m.Matches(&d));869}870871// Tests that Property() can describe itself when used to match a pointer.872TEST(PropertyForPointerTest, CanDescribeSelf) {873Matcher<const AClass*> m = Property(&AClass::n, Ge(0));874875EXPECT_EQ("is an object whose given property is >= 0", Describe(m));876EXPECT_EQ("is an object whose given property isn't >= 0",877DescribeNegation(m));878}879880TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {881Matcher<const AClass*> m = Property("fancy_name", &AClass::n, Ge(0));882883EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m));884EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0",885DescribeNegation(m));886}887888// Tests that Property() can explain the result of matching a pointer.889TEST_P(PropertyForPointerTestP, CanExplainMatchResult) {890Matcher<const AClass*> m = Property(&AClass::n, Ge(0));891892AClass a;893a.set_n(1);894EXPECT_EQ("", Explain(m, static_cast<const AClass*>(nullptr)));895EXPECT_EQ(896"which points to an object whose given property is 1" + OfType("int"),897Explain(m, &a));898899m = Property(&AClass::n, GreaterThan(0));900EXPECT_EQ("which points to an object whose given property is 1" +901OfType("int") + ", which is 1 more than 0",902Explain(m, &a));903}904905TEST_P(PropertyForPointerTestP, CanExplainMatchResultWithPropertyName) {906Matcher<const AClass*> m = Property("fancy_name", &AClass::n, Ge(0));907908AClass a;909a.set_n(1);910EXPECT_EQ("", Explain(m, static_cast<const AClass*>(nullptr)));911EXPECT_EQ("which points to an object whose property `fancy_name` is 1" +912OfType("int"),913Explain(m, &a));914915m = Property("fancy_name", &AClass::n, GreaterThan(0));916EXPECT_EQ("which points to an object whose property `fancy_name` is 1" +917OfType("int") + ", which is 1 more than 0",918Explain(m, &a));919}920921// Tests ResultOf.922923// Tests that ResultOf(f, ...) compiles and works as expected when f is a924// function pointer.925std::string IntToStringFunction(int input) {926return input == 1 ? "foo" : "bar";927}928929INSTANTIATE_GTEST_MATCHER_TEST_P(ResultOfTest);930931TEST(ResultOfTest, WorksForFunctionPointers) {932Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string("foo")));933934EXPECT_TRUE(matcher.Matches(1));935EXPECT_FALSE(matcher.Matches(2));936}937938// Tests that ResultOf() can describe itself.939TEST(ResultOfTest, CanDescribeItself) {940Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));941942EXPECT_EQ(943"is mapped by the given callable to a value that "944"is equal to \"foo\"",945Describe(matcher));946EXPECT_EQ(947"is mapped by the given callable to a value that "948"isn't equal to \"foo\"",949DescribeNegation(matcher));950}951952// Tests that ResultOf() can describe itself when provided a result description.953TEST(ResultOfTest, CanDescribeItselfWithResultDescription) {954Matcher<int> matcher =955ResultOf("string conversion", &IntToStringFunction, StrEq("foo"));956957EXPECT_EQ("whose string conversion is equal to \"foo\"", Describe(matcher));958EXPECT_EQ("whose string conversion isn't equal to \"foo\"",959DescribeNegation(matcher));960}961962// Tests that ResultOf() can explain the match result.963int IntFunction(int input) { return input == 42 ? 80 : 90; }964965TEST_P(ResultOfTestP, CanExplainMatchResult) {966Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));967EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),968Explain(matcher, 36));969970matcher = ResultOf(&IntFunction, GreaterThan(85));971EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +972", which is 5 more than 85",973Explain(matcher, 36));974}975976TEST_P(ResultOfTestP, CanExplainMatchResultWithResultDescription) {977Matcher<int> matcher = ResultOf("magic int conversion", &IntFunction, Ge(85));978EXPECT_EQ("whose magic int conversion is 90" + OfType("int"),979Explain(matcher, 36));980981matcher = ResultOf("magic int conversion", &IntFunction, GreaterThan(85));982EXPECT_EQ("whose magic int conversion is 90" + OfType("int") +983", which is 5 more than 85",984Explain(matcher, 36));985}986987// Tests that ResultOf(f, ...) compiles and works as expected when f(x)988// returns a non-reference.989TEST(ResultOfTest, WorksForNonReferenceResults) {990Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));991992EXPECT_TRUE(matcher.Matches(42));993EXPECT_FALSE(matcher.Matches(36));994}995996// Tests that ResultOf(f, ...) compiles and works as expected when f(x)997// returns a reference to non-const.998double& DoubleFunction(double& input) { return input; } // NOLINT9991000Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT1001return obj;1002}10031004TEST(ResultOfTest, WorksForReferenceToNonConstResults) {1005double x = 3.14;1006double x2 = x;1007Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));10081009EXPECT_TRUE(matcher.Matches(x));1010EXPECT_FALSE(matcher.Matches(x2));10111012// Test that ResultOf works with uncopyable objects1013Uncopyable obj(0);1014Uncopyable obj2(0);1015Matcher<Uncopyable&> matcher2 = ResultOf(&RefUncopyableFunction, Ref(obj));10161017EXPECT_TRUE(matcher2.Matches(obj));1018EXPECT_FALSE(matcher2.Matches(obj2));1019}10201021// Tests that ResultOf(f, ...) compiles and works as expected when f(x)1022// returns a reference to const.1023const std::string& StringFunction(const std::string& input) { return input; }10241025TEST(ResultOfTest, WorksForReferenceToConstResults) {1026std::string s = "foo";1027std::string s2 = s;1028Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s));10291030EXPECT_TRUE(matcher.Matches(s));1031EXPECT_FALSE(matcher.Matches(s2));1032}10331034// Tests that ResultOf(f, m) works when f(x) and m's1035// argument types are compatible but different.1036TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {1037// IntFunction() returns int but the inner matcher expects a signed char.1038Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));10391040EXPECT_TRUE(matcher.Matches(36));1041EXPECT_FALSE(matcher.Matches(42));1042}10431044// Tests that the program aborts when ResultOf is passed1045// a NULL function pointer.1046TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {1047EXPECT_DEATH_IF_SUPPORTED(1048ResultOf(static_cast<std::string (*)(int dummy)>(nullptr),1049Eq(std::string("foo"))),1050"NULL function pointer is passed into ResultOf\\(\\)\\.");1051}10521053// Tests that ResultOf(f, ...) compiles and works as expected when f is a1054// function reference.1055TEST(ResultOfTest, WorksForFunctionReferences) {1056Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));1057EXPECT_TRUE(matcher.Matches(1));1058EXPECT_FALSE(matcher.Matches(2));1059}10601061// Tests that ResultOf(f, ...) compiles and works as expected when f is a1062// function object.1063struct Functor {1064std::string operator()(int input) const { return IntToStringFunction(input); }1065};10661067TEST(ResultOfTest, WorksForFunctors) {1068Matcher<int> matcher = ResultOf(Functor(), Eq(std::string("foo")));10691070EXPECT_TRUE(matcher.Matches(1));1071EXPECT_FALSE(matcher.Matches(2));1072}10731074// Tests that ResultOf(f, ...) compiles and works as expected when f is a1075// functor with more than one operator() defined. ResultOf() must work1076// for each defined operator().1077struct PolymorphicFunctor {1078typedef int result_type;1079int operator()(int n) { return n; }1080int operator()(const char* s) { return static_cast<int>(strlen(s)); }1081std::string operator()(int* p) { return p ? "good ptr" : "null"; }1082};10831084TEST(ResultOfTest, WorksForPolymorphicFunctors) {1085Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));10861087EXPECT_TRUE(matcher_int.Matches(10));1088EXPECT_FALSE(matcher_int.Matches(2));10891090Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));10911092EXPECT_TRUE(matcher_string.Matches("long string"));1093EXPECT_FALSE(matcher_string.Matches("shrt"));1094}10951096TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {1097Matcher<int*> matcher = ResultOf(PolymorphicFunctor(), "good ptr");10981099int n = 0;1100EXPECT_TRUE(matcher.Matches(&n));1101EXPECT_FALSE(matcher.Matches(nullptr));1102}11031104TEST(ResultOfTest, WorksForLambdas) {1105Matcher<int> matcher = ResultOf(1106[](int str_len) {1107return std::string(static_cast<size_t>(str_len), 'x');1108},1109"xxx");1110EXPECT_TRUE(matcher.Matches(3));1111EXPECT_FALSE(matcher.Matches(1));1112}11131114TEST(ResultOfTest, WorksForNonCopyableArguments) {1115Matcher<std::unique_ptr<int>> matcher = ResultOf(1116[](const std::unique_ptr<int>& str_len) {1117return std::string(static_cast<size_t>(*str_len), 'x');1118},1119"xxx");1120EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(new int(3))));1121EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(new int(1))));1122}11231124const int* ReferencingFunction(const int& n) { return &n; }11251126struct ReferencingFunctor {1127typedef const int* result_type;1128result_type operator()(const int& n) { return &n; }1129};11301131TEST(ResultOfTest, WorksForReferencingCallables) {1132const int n = 1;1133const int n2 = 1;1134Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));1135EXPECT_TRUE(matcher2.Matches(n));1136EXPECT_FALSE(matcher2.Matches(n2));11371138Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));1139EXPECT_TRUE(matcher3.Matches(n));1140EXPECT_FALSE(matcher3.Matches(n2));1141}11421143TEST(SizeIsTest, ImplementsSizeIs) {1144vector<int> container;1145EXPECT_THAT(container, SizeIs(0));1146EXPECT_THAT(container, Not(SizeIs(1)));1147container.push_back(0);1148EXPECT_THAT(container, Not(SizeIs(0)));1149EXPECT_THAT(container, SizeIs(1));1150container.push_back(0);1151EXPECT_THAT(container, Not(SizeIs(0)));1152EXPECT_THAT(container, SizeIs(2));1153}11541155TEST(SizeIsTest, WorksWithMap) {1156map<std::string, int> container;1157EXPECT_THAT(container, SizeIs(0));1158EXPECT_THAT(container, Not(SizeIs(1)));1159container.insert(make_pair("foo", 1));1160EXPECT_THAT(container, Not(SizeIs(0)));1161EXPECT_THAT(container, SizeIs(1));1162container.insert(make_pair("bar", 2));1163EXPECT_THAT(container, Not(SizeIs(0)));1164EXPECT_THAT(container, SizeIs(2));1165}11661167TEST(SizeIsTest, WorksWithReferences) {1168vector<int> container;1169Matcher<const vector<int>&> m = SizeIs(1);1170EXPECT_THAT(container, Not(m));1171container.push_back(0);1172EXPECT_THAT(container, m);1173}11741175TEST(SizeIsTest, WorksWithMoveOnly) {1176ContainerHelper helper;1177EXPECT_CALL(helper, Call(SizeIs(3)));1178helper.Call(MakeUniquePtrs({1, 2, 3}));1179}11801181// SizeIs should work for any type that provides a size() member function.1182// For example, a size_type member type should not need to be provided.1183struct MinimalistCustomType {1184int size() const { return 1; }1185};1186TEST(SizeIsTest, WorksWithMinimalistCustomType) {1187MinimalistCustomType container;1188EXPECT_THAT(container, SizeIs(1));1189EXPECT_THAT(container, Not(SizeIs(0)));1190}11911192TEST(SizeIsTest, CanDescribeSelf) {1193Matcher<vector<int>> m = SizeIs(2);1194EXPECT_EQ("has a size that is equal to 2", Describe(m));1195EXPECT_EQ("has a size that isn't equal to 2", DescribeNegation(m));1196}11971198TEST(SizeIsTest, ExplainsResult) {1199Matcher<vector<int>> m1 = SizeIs(2);1200Matcher<vector<int>> m2 = SizeIs(Lt(2u));1201Matcher<vector<int>> m3 = SizeIs(AnyOf(0, 3));1202Matcher<vector<int>> m4 = SizeIs(Gt(1u));1203vector<int> container;1204EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));1205EXPECT_EQ("whose size 0 matches", Explain(m2, container));1206EXPECT_EQ("whose size 0 matches", Explain(m3, container));1207EXPECT_EQ("whose size 0 doesn't match", Explain(m4, container));1208container.push_back(0);1209container.push_back(0);1210EXPECT_EQ("whose size 2 matches", Explain(m1, container));1211EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));1212EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container));1213EXPECT_EQ("whose size 2 matches", Explain(m4, container));1214}12151216TEST(WhenSortedByTest, WorksForEmptyContainer) {1217const vector<int> numbers;1218EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));1219EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));1220}12211222TEST(WhenSortedByTest, WorksForNonEmptyContainer) {1223vector<unsigned> numbers;1224numbers.push_back(3);1225numbers.push_back(1);1226numbers.push_back(2);1227numbers.push_back(2);1228EXPECT_THAT(numbers,1229WhenSortedBy(greater<unsigned>(), ElementsAre(3, 2, 2, 1)));1230EXPECT_THAT(numbers,1231Not(WhenSortedBy(greater<unsigned>(), ElementsAre(1, 2, 2, 3))));1232}12331234TEST(WhenSortedByTest, WorksForNonVectorContainer) {1235list<std::string> words;1236words.push_back("say");1237words.push_back("hello");1238words.push_back("world");1239EXPECT_THAT(words, WhenSortedBy(less<std::string>(),1240ElementsAre("hello", "say", "world")));1241EXPECT_THAT(words, Not(WhenSortedBy(less<std::string>(),1242ElementsAre("say", "hello", "world"))));1243}12441245TEST(WhenSortedByTest, WorksForNativeArray) {1246const int numbers[] = {1, 3, 2, 4};1247const int sorted_numbers[] = {1, 2, 3, 4};1248EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));1249EXPECT_THAT(numbers,1250WhenSortedBy(less<int>(), ElementsAreArray(sorted_numbers)));1251EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));1252}12531254TEST(WhenSortedByTest, CanDescribeSelf) {1255const Matcher<vector<int>> m = WhenSortedBy(less<int>(), ElementsAre(1, 2));1256EXPECT_EQ(1257"(when sorted) has 2 elements where\n"1258"element #0 is equal to 1,\n"1259"element #1 is equal to 2",1260Describe(m));1261EXPECT_EQ(1262"(when sorted) doesn't have 2 elements, or\n"1263"element #0 isn't equal to 1, or\n"1264"element #1 isn't equal to 2",1265DescribeNegation(m));1266}12671268TEST(WhenSortedByTest, ExplainsMatchResult) {1269const int a[] = {2, 1};1270EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",1271Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));1272EXPECT_EQ("which is { 1, 2 } when sorted",1273Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));1274}12751276// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't1277// need to test it as exhaustively as we test the latter.12781279TEST(WhenSortedTest, WorksForEmptyContainer) {1280const vector<int> numbers;1281EXPECT_THAT(numbers, WhenSorted(ElementsAre()));1282EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));1283}12841285TEST(WhenSortedTest, WorksForNonEmptyContainer) {1286list<std::string> words;1287words.push_back("3");1288words.push_back("1");1289words.push_back("2");1290words.push_back("2");1291EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));1292EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));1293}12941295TEST(WhenSortedTest, WorksForMapTypes) {1296map<std::string, int> word_counts;1297word_counts["and"] = 1;1298word_counts["the"] = 1;1299word_counts["buffalo"] = 2;1300EXPECT_THAT(word_counts,1301WhenSorted(ElementsAre(Pair("and", 1), Pair("buffalo", 2),1302Pair("the", 1))));1303EXPECT_THAT(word_counts,1304Not(WhenSorted(ElementsAre(Pair("and", 1), Pair("the", 1),1305Pair("buffalo", 2)))));1306}13071308TEST(WhenSortedTest, WorksForMultiMapTypes) {1309multimap<int, int> ifib;1310ifib.insert(make_pair(8, 6));1311ifib.insert(make_pair(2, 3));1312ifib.insert(make_pair(1, 1));1313ifib.insert(make_pair(3, 4));1314ifib.insert(make_pair(1, 2));1315ifib.insert(make_pair(5, 5));1316EXPECT_THAT(ifib,1317WhenSorted(ElementsAre(Pair(1, 1), Pair(1, 2), Pair(2, 3),1318Pair(3, 4), Pair(5, 5), Pair(8, 6))));1319EXPECT_THAT(ifib,1320Not(WhenSorted(ElementsAre(Pair(8, 6), Pair(2, 3), Pair(1, 1),1321Pair(3, 4), Pair(1, 2), Pair(5, 5)))));1322}13231324TEST(WhenSortedTest, WorksForPolymorphicMatcher) {1325std::deque<int> d;1326d.push_back(2);1327d.push_back(1);1328EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2)));1329EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));1330}13311332TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {1333std::deque<int> d;1334d.push_back(2);1335d.push_back(1);1336Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);1337EXPECT_THAT(d, WhenSorted(vector_match));1338Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);1339EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));1340}13411342// Deliberately bare pseudo-container.1343// Offers only begin() and end() accessors, yielding InputIterator.1344template <typename T>1345class Streamlike {1346private:1347class ConstIter;13481349public:1350typedef ConstIter const_iterator;1351typedef T value_type;13521353template <typename InIter>1354Streamlike(InIter first, InIter last) : remainder_(first, last) {}13551356const_iterator begin() const {1357return const_iterator(this, remainder_.begin());1358}1359const_iterator end() const { return const_iterator(this, remainder_.end()); }13601361private:1362class ConstIter {1363public:1364using iterator_category = std::input_iterator_tag;1365using value_type = T;1366using difference_type = ptrdiff_t;1367using pointer = const value_type*;1368using reference = const value_type&;13691370ConstIter(const Streamlike* s, typename std::list<value_type>::iterator pos)1371: s_(s), pos_(pos) {}13721373const value_type& operator*() const { return *pos_; }1374const value_type* operator->() const { return &*pos_; }1375ConstIter& operator++() {1376s_->remainder_.erase(pos_++);1377return *this;1378}13791380// *iter++ is required to work (see std::istreambuf_iterator).1381// (void)iter++ is also required to work.1382class PostIncrProxy {1383public:1384explicit PostIncrProxy(const value_type& value) : value_(value) {}1385value_type operator*() const { return value_; }13861387private:1388value_type value_;1389};1390PostIncrProxy operator++(int) {1391PostIncrProxy proxy(**this);1392++(*this);1393return proxy;1394}13951396friend bool operator==(const ConstIter& a, const ConstIter& b) {1397return a.s_ == b.s_ && a.pos_ == b.pos_;1398}1399friend bool operator!=(const ConstIter& a, const ConstIter& b) {1400return !(a == b);1401}14021403private:1404const Streamlike* s_;1405typename std::list<value_type>::iterator pos_;1406};14071408friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) {1409os << "[";1410typedef typename std::list<value_type>::const_iterator Iter;1411const char* sep = "";1412for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {1413os << sep << *it;1414sep = ",";1415}1416os << "]";1417return os;1418}14191420mutable std::list<value_type> remainder_; // modified by iteration1421};14221423TEST(StreamlikeTest, Iteration) {1424const int a[5] = {2, 1, 4, 5, 3};1425Streamlike<int> s(a, a + 5);1426Streamlike<int>::const_iterator it = s.begin();1427const int* ip = a;1428while (it != s.end()) {1429SCOPED_TRACE(ip - a);1430EXPECT_EQ(*ip++, *it++);1431}1432}14331434INSTANTIATE_GTEST_MATCHER_TEST_P(BeginEndDistanceIsTest);14351436TEST(BeginEndDistanceIsTest, WorksWithForwardList) {1437std::forward_list<int> container;1438EXPECT_THAT(container, BeginEndDistanceIs(0));1439EXPECT_THAT(container, Not(BeginEndDistanceIs(1)));1440container.push_front(0);1441EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));1442EXPECT_THAT(container, BeginEndDistanceIs(1));1443container.push_front(0);1444EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));1445EXPECT_THAT(container, BeginEndDistanceIs(2));1446}14471448TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {1449const int a[5] = {1, 2, 3, 4, 5};1450Streamlike<int> s(a, a + 5);1451EXPECT_THAT(s, BeginEndDistanceIs(5));1452}14531454TEST(BeginEndDistanceIsTest, CanDescribeSelf) {1455Matcher<vector<int>> m = BeginEndDistanceIs(2);1456EXPECT_EQ("distance between begin() and end() is equal to 2", Describe(m));1457EXPECT_EQ("distance between begin() and end() isn't equal to 2",1458DescribeNegation(m));1459}14601461TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {1462ContainerHelper helper;1463EXPECT_CALL(helper, Call(BeginEndDistanceIs(2)));1464helper.Call(MakeUniquePtrs({1, 2}));1465}14661467TEST_P(BeginEndDistanceIsTestP, ExplainsResult) {1468Matcher<vector<int>> m1 = BeginEndDistanceIs(2);1469Matcher<vector<int>> m2 = BeginEndDistanceIs(Lt(2));1470Matcher<vector<int>> m3 = BeginEndDistanceIs(AnyOf(0, 3));1471Matcher<vector<int>> m4 = BeginEndDistanceIs(GreaterThan(1));1472vector<int> container;1473EXPECT_EQ("whose distance between begin() and end() 0 doesn't match",1474Explain(m1, container));1475EXPECT_EQ("whose distance between begin() and end() 0 matches",1476Explain(m2, container));1477EXPECT_EQ("whose distance between begin() and end() 0 matches",1478Explain(m3, container));1479EXPECT_EQ(1480"whose distance between begin() and end() 0 doesn't match, which is 1 "1481"less than 1",1482Explain(m4, container));1483container.push_back(0);1484container.push_back(0);1485EXPECT_EQ("whose distance between begin() and end() 2 matches",1486Explain(m1, container));1487EXPECT_EQ("whose distance between begin() and end() 2 doesn't match",1488Explain(m2, container));1489EXPECT_EQ("whose distance between begin() and end() 2 doesn't match",1490Explain(m3, container));1491EXPECT_EQ(1492"whose distance between begin() and end() 2 matches, which is 1 more "1493"than 1",1494Explain(m4, container));1495}14961497TEST(WhenSortedTest, WorksForStreamlike) {1498// Streamlike 'container' provides only minimal iterator support.1499// Its iterators are tagged with input_iterator_tag.1500const int a[5] = {2, 1, 4, 5, 3};1501Streamlike<int> s(std::begin(a), std::end(a));1502EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));1503EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));1504}15051506TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {1507const int a[] = {2, 1, 4, 5, 3};1508Streamlike<int> s(std::begin(a), std::end(a));1509Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);1510EXPECT_THAT(s, WhenSorted(vector_match));1511EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));1512}15131514TEST(IsSupersetOfTest, WorksForNativeArray) {1515const int subset[] = {1, 4};1516const int superset[] = {1, 2, 4};1517const int disjoint[] = {1, 0, 3};1518EXPECT_THAT(subset, IsSupersetOf(subset));1519EXPECT_THAT(subset, Not(IsSupersetOf(superset)));1520EXPECT_THAT(superset, IsSupersetOf(subset));1521EXPECT_THAT(subset, Not(IsSupersetOf(disjoint)));1522EXPECT_THAT(disjoint, Not(IsSupersetOf(subset)));1523}15241525TEST(IsSupersetOfTest, WorksWithDuplicates) {1526const int not_enough[] = {1, 2};1527const int enough[] = {1, 1, 2};1528const int expected[] = {1, 1};1529EXPECT_THAT(not_enough, Not(IsSupersetOf(expected)));1530EXPECT_THAT(enough, IsSupersetOf(expected));1531}15321533TEST(IsSupersetOfTest, WorksForEmpty) {1534vector<int> numbers;1535vector<int> expected;1536EXPECT_THAT(numbers, IsSupersetOf(expected));1537expected.push_back(1);1538EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));1539expected.clear();1540numbers.push_back(1);1541numbers.push_back(2);1542EXPECT_THAT(numbers, IsSupersetOf(expected));1543expected.push_back(1);1544EXPECT_THAT(numbers, IsSupersetOf(expected));1545expected.push_back(2);1546EXPECT_THAT(numbers, IsSupersetOf(expected));1547expected.push_back(3);1548EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));1549}15501551TEST(IsSupersetOfTest, WorksForStreamlike) {1552const int a[5] = {1, 2, 3, 4, 5};1553Streamlike<int> s(std::begin(a), std::end(a));15541555vector<int> expected;1556expected.push_back(1);1557expected.push_back(2);1558expected.push_back(5);1559EXPECT_THAT(s, IsSupersetOf(expected));15601561expected.push_back(0);1562EXPECT_THAT(s, Not(IsSupersetOf(expected)));1563}15641565TEST(IsSupersetOfTest, TakesStlContainer) {1566const int actual[] = {3, 1, 2};15671568::std::list<int> expected;1569expected.push_back(1);1570expected.push_back(3);1571EXPECT_THAT(actual, IsSupersetOf(expected));15721573expected.push_back(4);1574EXPECT_THAT(actual, Not(IsSupersetOf(expected)));1575}15761577TEST(IsSupersetOfTest, Describe) {1578typedef std::vector<int> IntVec;1579IntVec expected;1580expected.push_back(111);1581expected.push_back(222);1582expected.push_back(333);1583EXPECT_THAT(1584Describe<IntVec>(IsSupersetOf(expected)),1585Eq("a surjection from elements to requirements exists such that:\n"1586" - an element is equal to 111\n"1587" - an element is equal to 222\n"1588" - an element is equal to 333"));1589}15901591TEST(IsSupersetOfTest, DescribeNegation) {1592typedef std::vector<int> IntVec;1593IntVec expected;1594expected.push_back(111);1595expected.push_back(222);1596expected.push_back(333);1597EXPECT_THAT(1598DescribeNegation<IntVec>(IsSupersetOf(expected)),1599Eq("no surjection from elements to requirements exists such that:\n"1600" - an element is equal to 111\n"1601" - an element is equal to 222\n"1602" - an element is equal to 333"));1603}16041605TEST(IsSupersetOfTest, MatchAndExplain) {1606std::vector<int> v;1607v.push_back(2);1608v.push_back(3);1609std::vector<int> expected;1610expected.push_back(1);1611expected.push_back(2);1612StringMatchResultListener listener;1613ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))1614<< listener.str();1615EXPECT_THAT(listener.str(),1616Eq("where the following matchers don't match any elements:\n"1617"matcher #0: is equal to 1"));16181619v.push_back(1);1620listener.Clear();1621ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))1622<< listener.str();1623EXPECT_THAT(listener.str(), Eq("where:\n"1624" - element #0 is matched by matcher #1,\n"1625" - element #2 is matched by matcher #0"));1626}16271628TEST(IsSupersetOfTest, WorksForRhsInitializerList) {1629const int numbers[] = {1, 3, 6, 2, 4, 5};1630EXPECT_THAT(numbers, IsSupersetOf({1, 2}));1631EXPECT_THAT(numbers, Not(IsSupersetOf({3, 0})));1632}16331634TEST(IsSupersetOfTest, WorksWithMoveOnly) {1635ContainerHelper helper;1636EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));1637helper.Call(MakeUniquePtrs({1, 2}));1638EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));1639helper.Call(MakeUniquePtrs({2}));1640}16411642TEST(IsSubsetOfTest, WorksForNativeArray) {1643const int subset[] = {1, 4};1644const int superset[] = {1, 2, 4};1645const int disjoint[] = {1, 0, 3};1646EXPECT_THAT(subset, IsSubsetOf(subset));1647EXPECT_THAT(subset, IsSubsetOf(superset));1648EXPECT_THAT(superset, Not(IsSubsetOf(subset)));1649EXPECT_THAT(subset, Not(IsSubsetOf(disjoint)));1650EXPECT_THAT(disjoint, Not(IsSubsetOf(subset)));1651}16521653TEST(IsSubsetOfTest, WorksWithDuplicates) {1654const int not_enough[] = {1, 2};1655const int enough[] = {1, 1, 2};1656const int actual[] = {1, 1};1657EXPECT_THAT(actual, Not(IsSubsetOf(not_enough)));1658EXPECT_THAT(actual, IsSubsetOf(enough));1659}16601661TEST(IsSubsetOfTest, WorksForEmpty) {1662vector<int> numbers;1663vector<int> expected;1664EXPECT_THAT(numbers, IsSubsetOf(expected));1665expected.push_back(1);1666EXPECT_THAT(numbers, IsSubsetOf(expected));1667expected.clear();1668numbers.push_back(1);1669numbers.push_back(2);1670EXPECT_THAT(numbers, Not(IsSubsetOf(expected)));1671expected.push_back(1);1672EXPECT_THAT(numbers, Not(IsSubsetOf(expected)));1673expected.push_back(2);1674EXPECT_THAT(numbers, IsSubsetOf(expected));1675expected.push_back(3);1676EXPECT_THAT(numbers, IsSubsetOf(expected));1677}16781679TEST(IsSubsetOfTest, WorksForStreamlike) {1680const int a[5] = {1, 2};1681Streamlike<int> s(std::begin(a), std::end(a));16821683vector<int> expected;1684expected.push_back(1);1685EXPECT_THAT(s, Not(IsSubsetOf(expected)));1686expected.push_back(2);1687expected.push_back(5);1688EXPECT_THAT(s, IsSubsetOf(expected));1689}16901691TEST(IsSubsetOfTest, TakesStlContainer) {1692const int actual[] = {3, 1, 2};16931694::std::list<int> expected;1695expected.push_back(1);1696expected.push_back(3);1697EXPECT_THAT(actual, Not(IsSubsetOf(expected)));16981699expected.push_back(2);1700expected.push_back(4);1701EXPECT_THAT(actual, IsSubsetOf(expected));1702}17031704TEST(IsSubsetOfTest, Describe) {1705typedef std::vector<int> IntVec;1706IntVec expected;1707expected.push_back(111);1708expected.push_back(222);1709expected.push_back(333);17101711EXPECT_THAT(1712Describe<IntVec>(IsSubsetOf(expected)),1713Eq("an injection from elements to requirements exists such that:\n"1714" - an element is equal to 111\n"1715" - an element is equal to 222\n"1716" - an element is equal to 333"));1717}17181719TEST(IsSubsetOfTest, DescribeNegation) {1720typedef std::vector<int> IntVec;1721IntVec expected;1722expected.push_back(111);1723expected.push_back(222);1724expected.push_back(333);1725EXPECT_THAT(1726DescribeNegation<IntVec>(IsSubsetOf(expected)),1727Eq("no injection from elements to requirements exists such that:\n"1728" - an element is equal to 111\n"1729" - an element is equal to 222\n"1730" - an element is equal to 333"));1731}17321733TEST(IsSubsetOfTest, MatchAndExplain) {1734std::vector<int> v;1735v.push_back(2);1736v.push_back(3);1737std::vector<int> expected;1738expected.push_back(1);1739expected.push_back(2);1740StringMatchResultListener listener;1741ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))1742<< listener.str();1743EXPECT_THAT(listener.str(),1744Eq("where the following elements don't match any matchers:\n"1745"element #1: 3"));17461747expected.push_back(3);1748listener.Clear();1749ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))1750<< listener.str();1751EXPECT_THAT(listener.str(), Eq("where:\n"1752" - element #0 is matched by matcher #1,\n"1753" - element #1 is matched by matcher #2"));1754}17551756TEST(IsSubsetOfTest, WorksForRhsInitializerList) {1757const int numbers[] = {1, 2, 3};1758EXPECT_THAT(numbers, IsSubsetOf({1, 2, 3, 4}));1759EXPECT_THAT(numbers, Not(IsSubsetOf({1, 2})));1760}17611762TEST(IsSubsetOfTest, WorksWithMoveOnly) {1763ContainerHelper helper;1764EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));1765helper.Call(MakeUniquePtrs({1}));1766EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));1767helper.Call(MakeUniquePtrs({2}));1768}17691770// Tests using ElementsAre() and ElementsAreArray() with stream-like1771// "containers".17721773TEST(ElemensAreStreamTest, WorksForStreamlike) {1774const int a[5] = {1, 2, 3, 4, 5};1775Streamlike<int> s(std::begin(a), std::end(a));1776EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5));1777EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3)));1778}17791780TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {1781const int a[5] = {1, 2, 3, 4, 5};1782Streamlike<int> s(std::begin(a), std::end(a));17831784vector<int> expected;1785expected.push_back(1);1786expected.push_back(2);1787expected.push_back(3);1788expected.push_back(4);1789expected.push_back(5);1790EXPECT_THAT(s, ElementsAreArray(expected));17911792expected[3] = 0;1793EXPECT_THAT(s, Not(ElementsAreArray(expected)));1794}17951796TEST(ElementsAreTest, WorksWithUncopyable) {1797Uncopyable objs[2];1798objs[0].set_value(-3);1799objs[1].set_value(1);1800EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));1801}18021803TEST(ElementsAreTest, WorksWithMoveOnly) {1804ContainerHelper helper;1805EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));1806helper.Call(MakeUniquePtrs({1, 2}));18071808EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));1809helper.Call(MakeUniquePtrs({3, 4}));1810}18111812TEST(ElementsAreTest, TakesStlContainer) {1813const int actual[] = {3, 1, 2};18141815::std::list<int> expected;1816expected.push_back(3);1817expected.push_back(1);1818expected.push_back(2);1819EXPECT_THAT(actual, ElementsAreArray(expected));18201821expected.push_back(4);1822EXPECT_THAT(actual, Not(ElementsAreArray(expected)));1823}18241825// Tests for UnorderedElementsAreArray()18261827TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {1828const int a[] = {0, 1, 2, 3, 4};1829std::vector<int> s(std::begin(a), std::end(a));1830do {1831StringMatchResultListener listener;1832EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a), s, &listener))1833<< listener.str();1834} while (std::next_permutation(s.begin(), s.end()));1835}18361837TEST(UnorderedElementsAreArrayTest, VectorBool) {1838const bool a[] = {false, true, false, true, true};1839const bool b[] = {true, false, true, true, false};1840std::vector<bool> expected(std::begin(a), std::end(a));1841std::vector<bool> actual(std::begin(b), std::end(b));1842StringMatchResultListener listener;1843EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected), actual,1844&listener))1845<< listener.str();1846}18471848TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {1849// Streamlike 'container' provides only minimal iterator support.1850// Its iterators are tagged with input_iterator_tag, and it has no1851// size() or empty() methods.1852const int a[5] = {2, 1, 4, 5, 3};1853Streamlike<int> s(std::begin(a), std::end(a));18541855::std::vector<int> expected;1856expected.push_back(1);1857expected.push_back(2);1858expected.push_back(3);1859expected.push_back(4);1860expected.push_back(5);1861EXPECT_THAT(s, UnorderedElementsAreArray(expected));18621863expected.push_back(6);1864EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));1865}18661867TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {1868const int actual[] = {3, 1, 2};18691870::std::list<int> expected;1871expected.push_back(1);1872expected.push_back(2);1873expected.push_back(3);1874EXPECT_THAT(actual, UnorderedElementsAreArray(expected));18751876expected.push_back(4);1877EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected)));1878}18791880TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {1881const int a[5] = {2, 1, 4, 5, 3};1882EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5}));1883EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6})));1884}18851886TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {1887const std::string a[5] = {"a", "b", "c", "d", "e"};1888EXPECT_THAT(a, UnorderedElementsAreArray({"a", "b", "c", "d", "e"}));1889EXPECT_THAT(a, Not(UnorderedElementsAreArray({"a", "b", "c", "d", "ef"})));1890}18911892TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {1893const int a[5] = {2, 1, 4, 5, 3};1894EXPECT_THAT(a,1895UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));1896EXPECT_THAT(1897a, Not(UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));1898}18991900TEST(UnorderedElementsAreArrayTest,1901TakesInitializerListOfDifferentTypedMatchers) {1902const int a[5] = {2, 1, 4, 5, 3};1903// The compiler cannot infer the type of the initializer list if its1904// elements have different types. We must explicitly specify the1905// unified element type in this case.1906EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int>>(1907{Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));1908EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int>>(1909{Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));1910}19111912TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {1913ContainerHelper helper;1914EXPECT_CALL(helper,1915Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)})));1916helper.Call(MakeUniquePtrs({2, 1}));1917}19181919class UnorderedElementsAreTest : public testing::Test {1920protected:1921typedef std::vector<int> IntVec;1922};19231924TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {1925Uncopyable objs[2];1926objs[0].set_value(-3);1927objs[1].set_value(1);1928EXPECT_THAT(objs,1929UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3)));1930}19311932TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {1933const int a[] = {1, 2, 3};1934std::vector<int> s(std::begin(a), std::end(a));1935do {1936StringMatchResultListener listener;1937EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), s, &listener))1938<< listener.str();1939} while (std::next_permutation(s.begin(), s.end()));1940}19411942TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {1943const int a[] = {1, 2, 3};1944std::vector<int> s(std::begin(a), std::end(a));1945std::vector<Matcher<int>> mv;1946mv.push_back(1);1947mv.push_back(2);1948mv.push_back(2);1949// The element with value '3' matches nothing: fail fast.1950StringMatchResultListener listener;1951EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))1952<< listener.str();1953}19541955TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {1956// Streamlike 'container' provides only minimal iterator support.1957// Its iterators are tagged with input_iterator_tag, and it has no1958// size() or empty() methods.1959const int a[5] = {2, 1, 4, 5, 3};1960Streamlike<int> s(std::begin(a), std::end(a));19611962EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));1963EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));1964}19651966TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {1967ContainerHelper helper;1968EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2))));1969helper.Call(MakeUniquePtrs({2, 1}));1970}19711972// One naive implementation of the matcher runs in O(N!) time, which is too1973// slow for many real-world inputs. This test shows that our matcher can match1974// 100 inputs very quickly (a few milliseconds). An O(100!) is 10^1581975// iterations and obviously effectively incomputable.1976// [ RUN ] UnorderedElementsAreTest.Performance1977// [ OK ] UnorderedElementsAreTest.Performance (4 ms)1978TEST_F(UnorderedElementsAreTest, Performance) {1979std::vector<int> s;1980std::vector<Matcher<int>> mv;1981for (int i = 0; i < 100; ++i) {1982s.push_back(i);1983mv.push_back(_);1984}1985mv[50] = Eq(0);1986StringMatchResultListener listener;1987EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))1988<< listener.str();1989}19901991// Another variant of 'Performance' with similar expectations.1992// [ RUN ] UnorderedElementsAreTest.PerformanceHalfStrict1993// [ OK ] UnorderedElementsAreTest.PerformanceHalfStrict (4 ms)1994TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {1995std::vector<int> s;1996std::vector<Matcher<int>> mv;1997for (int i = 0; i < 100; ++i) {1998s.push_back(i);1999if (i & 1) {2000mv.push_back(_);2001} else {2002mv.push_back(i);2003}2004}2005StringMatchResultListener listener;2006EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))2007<< listener.str();2008}20092010TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {2011std::vector<int> v;2012v.push_back(4);2013StringMatchResultListener listener;2014EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))2015<< listener.str();2016EXPECT_THAT(listener.str(),2017Eq("which has 1 element\n"2018"where the following matchers don't match any elements:\n"2019"matcher #0: is equal to 1,\n"2020"matcher #1: is equal to 2,\n"2021"matcher #2: is equal to 3\n"2022"and where the following elements don't match any matchers:\n"2023"element #0: 4"));2024}20252026TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {2027std::vector<int> v;2028StringMatchResultListener listener;2029EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))2030<< listener.str();2031EXPECT_THAT(listener.str(),2032Eq("where the following matchers don't match any elements:\n"2033"matcher #0: is equal to 1,\n"2034"matcher #1: is equal to 2,\n"2035"matcher #2: is equal to 3"));2036}20372038TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {2039std::vector<int> v;2040v.push_back(1);2041v.push_back(1);2042StringMatchResultListener listener;2043EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))2044<< listener.str();2045EXPECT_THAT(listener.str(),2046Eq("where the following matchers don't match any elements:\n"2047"matcher #1: is equal to 2"));2048}20492050TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {2051std::vector<int> v;2052v.push_back(1);2053v.push_back(2);2054StringMatchResultListener listener;2055EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1), v, &listener))2056<< listener.str();2057EXPECT_THAT(listener.str(),2058Eq("where the following elements don't match any matchers:\n"2059"element #1: 2"));2060}20612062TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {2063std::vector<int> v;2064v.push_back(2);2065v.push_back(3);2066StringMatchResultListener listener;2067EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))2068<< listener.str();2069EXPECT_THAT(listener.str(),2070Eq("where"2071" the following matchers don't match any elements:\n"2072"matcher #0: is equal to 1\n"2073"and"2074" where"2075" the following elements don't match any matchers:\n"2076"element #1: 3"));2077}20782079// Test helper for formatting element, matcher index pairs in expectations.2080static std::string EMString(int element, int matcher) {2081stringstream ss;2082ss << "(element #" << element << ", matcher #" << matcher << ")";2083return ss.str();2084}20852086TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {2087// A situation where all elements and matchers have a match2088// associated with them, but the max matching is not perfect.2089std::vector<std::string> v;2090v.push_back("a");2091v.push_back("b");2092v.push_back("c");2093StringMatchResultListener listener;2094EXPECT_FALSE(ExplainMatchResult(2095UnorderedElementsAre("a", "a", AnyOf("b", "c")), v, &listener))2096<< listener.str();20972098std::string prefix =2099"where no permutation of the elements can satisfy all matchers, "2100"and the closest match is 2 of 3 matchers with the "2101"pairings:\n";21022103// We have to be a bit loose here, because there are 4 valid max matches.2104EXPECT_THAT(2105listener.str(),2106AnyOf(2107prefix + "{\n " + EMString(0, 0) + ",\n " + EMString(1, 2) + "\n}",2108prefix + "{\n " + EMString(0, 1) + ",\n " + EMString(1, 2) + "\n}",2109prefix + "{\n " + EMString(0, 0) + ",\n " + EMString(2, 2) + "\n}",2110prefix + "{\n " + EMString(0, 1) + ",\n " + EMString(2, 2) +2111"\n}"));2112}21132114TEST_F(UnorderedElementsAreTest, Describe) {2115EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()), Eq("is empty"));2116EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre(345)),2117Eq("has 1 element and that element is equal to 345"));2118EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre(111, 222, 333)),2119Eq("has 3 elements and there exists some permutation "2120"of elements such that:\n"2121" - element #0 is equal to 111, and\n"2122" - element #1 is equal to 222, and\n"2123" - element #2 is equal to 333"));2124}21252126TEST_F(UnorderedElementsAreTest, DescribeNegation) {2127EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()),2128Eq("isn't empty"));2129EXPECT_THAT(2130DescribeNegation<IntVec>(UnorderedElementsAre(345)),2131Eq("doesn't have 1 element, or has 1 element that isn't equal to 345"));2132EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)),2133Eq("doesn't have 3 elements, or there exists no permutation "2134"of elements such that:\n"2135" - element #0 is equal to 123, and\n"2136" - element #1 is equal to 234, and\n"2137" - element #2 is equal to 345"));2138}21392140// Tests Each().21412142INSTANTIATE_GTEST_MATCHER_TEST_P(EachTest);21432144TEST_P(EachTestP, ExplainsMatchResultCorrectly) {2145set<int> a; // empty21462147Matcher<set<int>> m = Each(2);2148EXPECT_EQ("", Explain(m, a));21492150Matcher<const int(&)[1]> n = Each(1); // NOLINT21512152const int b[1] = {1};2153EXPECT_EQ("", Explain(n, b));21542155n = Each(3);2156EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));21572158a.insert(1);2159a.insert(2);2160a.insert(3);2161m = Each(GreaterThan(0));2162EXPECT_EQ("", Explain(m, a));21632164m = Each(GreaterThan(10));2165EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",2166Explain(m, a));2167}21682169TEST(EachTest, DescribesItselfCorrectly) {2170Matcher<vector<int>> m = Each(1);2171EXPECT_EQ("only contains elements that is equal to 1", Describe(m));21722173Matcher<vector<int>> m2 = Not(m);2174EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));2175}21762177TEST(EachTest, MatchesVectorWhenAllElementsMatch) {2178vector<int> some_vector;2179EXPECT_THAT(some_vector, Each(1));2180some_vector.push_back(3);2181EXPECT_THAT(some_vector, Not(Each(1)));2182EXPECT_THAT(some_vector, Each(3));2183some_vector.push_back(1);2184some_vector.push_back(2);2185EXPECT_THAT(some_vector, Not(Each(3)));2186EXPECT_THAT(some_vector, Each(Lt(3.5)));21872188vector<std::string> another_vector;2189another_vector.push_back("fee");2190EXPECT_THAT(another_vector, Each(std::string("fee")));2191another_vector.push_back("fie");2192another_vector.push_back("foe");2193another_vector.push_back("fum");2194EXPECT_THAT(another_vector, Not(Each(std::string("fee"))));2195}21962197TEST(EachTest, MatchesMapWhenAllElementsMatch) {2198map<const char*, int> my_map;2199const char* bar = "a string";2200my_map[bar] = 2;2201EXPECT_THAT(my_map, Each(make_pair(bar, 2)));22022203map<std::string, int> another_map;2204EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1)));2205another_map["fee"] = 1;2206EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1)));2207another_map["fie"] = 2;2208another_map["foe"] = 3;2209another_map["fum"] = 4;2210EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fee"), 1))));2211EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fum"), 1))));2212EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));2213}22142215TEST(EachTest, AcceptsMatcher) {2216const int a[] = {1, 2, 3};2217EXPECT_THAT(a, Each(Gt(0)));2218EXPECT_THAT(a, Not(Each(Gt(1))));2219}22202221TEST(EachTest, WorksForNativeArrayAsTuple) {2222const int a[] = {1, 2};2223const int* const pointer = a;2224EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0)));2225EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1))));2226}22272228TEST(EachTest, WorksWithMoveOnly) {2229ContainerHelper helper;2230EXPECT_CALL(helper, Call(Each(Pointee(Gt(0)))));2231helper.Call(MakeUniquePtrs({1, 2}));2232}22332234// For testing Pointwise().2235class IsHalfOfMatcher {2236public:2237template <typename T1, typename T2>2238bool MatchAndExplain(const std::tuple<T1, T2>& a_pair,2239MatchResultListener* listener) const {2240if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {2241*listener << "where the second is " << std::get<1>(a_pair);2242return true;2243} else {2244*listener << "where the second/2 is " << std::get<1>(a_pair) / 2;2245return false;2246}2247}22482249void DescribeTo(ostream* os) const {2250*os << "are a pair where the first is half of the second";2251}22522253void DescribeNegationTo(ostream* os) const {2254*os << "are a pair where the first isn't half of the second";2255}2256};22572258PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {2259return MakePolymorphicMatcher(IsHalfOfMatcher());2260}22612262TEST(PointwiseTest, DescribesSelf) {2263vector<int> rhs;2264rhs.push_back(1);2265rhs.push_back(2);2266rhs.push_back(3);2267const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);2268EXPECT_EQ(2269"contains 3 values, where each value and its corresponding value "2270"in { 1, 2, 3 } are a pair where the first is half of the second",2271Describe(m));2272EXPECT_EQ(2273"doesn't contain exactly 3 values, or contains a value x at some "2274"index i where x and the i-th value of { 1, 2, 3 } are a pair "2275"where the first isn't half of the second",2276DescribeNegation(m));2277}22782279TEST(PointwiseTest, MakesCopyOfRhs) {2280list<signed char> rhs;2281rhs.push_back(2);2282rhs.push_back(4);22832284int lhs[] = {1, 2};2285const Matcher<const int(&)[2]> m = Pointwise(IsHalfOf(), rhs);2286EXPECT_THAT(lhs, m);22872288// Changing rhs now shouldn't affect m, which made a copy of rhs.2289rhs.push_back(6);2290EXPECT_THAT(lhs, m);2291}22922293TEST(PointwiseTest, WorksForLhsNativeArray) {2294const int lhs[] = {1, 2, 3};2295vector<int> rhs;2296rhs.push_back(2);2297rhs.push_back(4);2298rhs.push_back(6);2299EXPECT_THAT(lhs, Pointwise(Lt(), rhs));2300EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));2301}23022303TEST(PointwiseTest, WorksForRhsNativeArray) {2304const int rhs[] = {1, 2, 3};2305vector<int> lhs;2306lhs.push_back(2);2307lhs.push_back(4);2308lhs.push_back(6);2309EXPECT_THAT(lhs, Pointwise(Gt(), rhs));2310EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));2311}23122313// Test is effective only with sanitizers.2314TEST(PointwiseTest, WorksForVectorOfBool) {2315vector<bool> rhs(3, false);2316rhs[1] = true;2317vector<bool> lhs = rhs;2318EXPECT_THAT(lhs, Pointwise(Eq(), rhs));2319rhs[0] = true;2320EXPECT_THAT(lhs, Not(Pointwise(Eq(), rhs)));2321}23222323TEST(PointwiseTest, WorksForRhsInitializerList) {2324const vector<int> lhs{2, 4, 6};2325EXPECT_THAT(lhs, Pointwise(Gt(), {1, 2, 3}));2326EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));2327}23282329TEST(PointwiseTest, RejectsWrongSize) {2330const double lhs[2] = {1, 2};2331const int rhs[1] = {0};2332EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));2333EXPECT_EQ("which contains 2 values", Explain(Pointwise(Gt(), rhs), lhs));23342335const int rhs2[3] = {0, 1, 2};2336EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));2337}23382339TEST(PointwiseTest, RejectsWrongContent) {2340const double lhs[3] = {1, 2, 3};2341const int rhs[3] = {2, 6, 4};2342EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));2343EXPECT_EQ(2344"where the value pair (2, 6) at index #1 don't match, "2345"where the second/2 is 3",2346Explain(Pointwise(IsHalfOf(), rhs), lhs));2347}23482349TEST(PointwiseTest, AcceptsCorrectContent) {2350const double lhs[3] = {1, 2, 3};2351const int rhs[3] = {2, 4, 6};2352EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));2353EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));2354}23552356TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {2357const double lhs[3] = {1, 2, 3};2358const int rhs[3] = {2, 4, 6};2359const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();2360EXPECT_THAT(lhs, Pointwise(m1, rhs));2361EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));23622363// This type works as a std::tuple<const double&, const int&> can be2364// implicitly cast to std::tuple<double, int>.2365const Matcher<std::tuple<double, int>> m2 = IsHalfOf();2366EXPECT_THAT(lhs, Pointwise(m2, rhs));2367EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));2368}23692370MATCHER(PointeeEquals, "Points to an equal value") {2371return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),2372::testing::get<0>(arg), result_listener);2373}23742375TEST(PointwiseTest, WorksWithMoveOnly) {2376ContainerHelper helper;2377EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));2378helper.Call(MakeUniquePtrs({1, 2}));2379}23802381TEST(UnorderedPointwiseTest, DescribesSelf) {2382vector<int> rhs;2383rhs.push_back(1);2384rhs.push_back(2);2385rhs.push_back(3);2386const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs);2387EXPECT_EQ(2388"has 3 elements and there exists some permutation of elements such "2389"that:\n"2390" - element #0 and 1 are a pair where the first is half of the second, "2391"and\n"2392" - element #1 and 2 are a pair where the first is half of the second, "2393"and\n"2394" - element #2 and 3 are a pair where the first is half of the second",2395Describe(m));2396EXPECT_EQ(2397"doesn't have 3 elements, or there exists no permutation of elements "2398"such that:\n"2399" - element #0 and 1 are a pair where the first is half of the second, "2400"and\n"2401" - element #1 and 2 are a pair where the first is half of the second, "2402"and\n"2403" - element #2 and 3 are a pair where the first is half of the second",2404DescribeNegation(m));2405}24062407TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {2408list<signed char> rhs;2409rhs.push_back(2);2410rhs.push_back(4);24112412int lhs[] = {2, 1};2413const Matcher<const int(&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs);2414EXPECT_THAT(lhs, m);24152416// Changing rhs now shouldn't affect m, which made a copy of rhs.2417rhs.push_back(6);2418EXPECT_THAT(lhs, m);2419}24202421TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {2422const int lhs[] = {1, 2, 3};2423vector<int> rhs;2424rhs.push_back(4);2425rhs.push_back(6);2426rhs.push_back(2);2427EXPECT_THAT(lhs, UnorderedPointwise(Lt(), rhs));2428EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));2429}24302431TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {2432const int rhs[] = {1, 2, 3};2433vector<int> lhs;2434lhs.push_back(4);2435lhs.push_back(2);2436lhs.push_back(6);2437EXPECT_THAT(lhs, UnorderedPointwise(Gt(), rhs));2438EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));2439}24402441TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {2442const vector<int> lhs{2, 4, 6};2443EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3}));2444EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));2445}24462447TEST(UnorderedPointwiseTest, RejectsWrongSize) {2448const double lhs[2] = {1, 2};2449const int rhs[1] = {0};2450EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));2451EXPECT_EQ("which has 2 elements\n",2452Explain(UnorderedPointwise(Gt(), rhs), lhs));24532454const int rhs2[3] = {0, 1, 2};2455EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2)));2456}24572458TEST(UnorderedPointwiseTest, RejectsWrongContent) {2459const double lhs[3] = {1, 2, 3};2460const int rhs[3] = {2, 6, 6};2461EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs)));2462EXPECT_EQ(2463"where the following elements don't match any matchers:\n"2464"element #1: 2",2465Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs));2466}24672468TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {2469const double lhs[3] = {1, 2, 3};2470const int rhs[3] = {2, 4, 6};2471EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));2472}24732474TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {2475const double lhs[3] = {1, 2, 3};2476const int rhs[3] = {6, 4, 2};2477EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));2478}24792480TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {2481const double lhs[3] = {1, 2, 3};2482const int rhs[3] = {4, 6, 2};2483const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();2484EXPECT_THAT(lhs, UnorderedPointwise(m1, rhs));24852486// This type works as a std::tuple<const double&, const int&> can be2487// implicitly cast to std::tuple<double, int>.2488const Matcher<std::tuple<double, int>> m2 = IsHalfOf();2489EXPECT_THAT(lhs, UnorderedPointwise(m2, rhs));2490}24912492TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {2493ContainerHelper helper;2494EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),2495std::vector<int>{1, 2})));2496helper.Call(MakeUniquePtrs({2, 1}));2497}24982499TEST(PointeeTest, WorksOnMoveOnlyType) {2500std::unique_ptr<int> p(new int(3));2501EXPECT_THAT(p, Pointee(Eq(3)));2502EXPECT_THAT(p, Not(Pointee(Eq(2))));2503}25042505class PredicateFormatterFromMatcherTest : public ::testing::Test {2506protected:2507enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };25082509// A matcher that can return different results when used multiple times on the2510// same input. No real matcher should do this; but this lets us test that we2511// detect such behavior and fail appropriately.2512class MockMatcher : public MatcherInterface<Behavior> {2513public:2514bool MatchAndExplain(Behavior behavior,2515MatchResultListener* listener) const override {2516*listener << "[MatchAndExplain]";2517switch (behavior) {2518case kInitialSuccess:2519// The first call to MatchAndExplain should use a "not interested"2520// listener; so this is expected to return |true|. There should be no2521// subsequent calls.2522return !listener->IsInterested();25232524case kAlwaysFail:2525return false;25262527case kFlaky:2528// The first call to MatchAndExplain should use a "not interested"2529// listener; so this will return |false|. Subsequent calls should have2530// an "interested" listener; so this will return |true|, thus2531// simulating a flaky matcher.2532return listener->IsInterested();2533}25342535GTEST_LOG_(FATAL) << "This should never be reached";2536return false;2537}25382539void DescribeTo(ostream* os) const override { *os << "[DescribeTo]"; }25402541void DescribeNegationTo(ostream* os) const override {2542*os << "[DescribeNegationTo]";2543}2544};25452546AssertionResult RunPredicateFormatter(Behavior behavior) {2547auto matcher = MakeMatcher(new MockMatcher);2548PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(2549matcher);2550return predicate_formatter("dummy-name", behavior);2551}2552};25532554TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {2555AssertionResult result = RunPredicateFormatter(kInitialSuccess);2556EXPECT_TRUE(result); // Implicit cast to bool.2557std::string expect;2558EXPECT_EQ(expect, result.message());2559}25602561TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {2562AssertionResult result = RunPredicateFormatter(kAlwaysFail);2563EXPECT_FALSE(result); // Implicit cast to bool.2564std::string expect =2565"Value of: dummy-name\nExpected: [DescribeTo]\n"2566" Actual: 1" +2567OfType(internal::GetTypeName<Behavior>()) + ", [MatchAndExplain]";2568EXPECT_EQ(expect, result.message());2569}25702571TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {2572AssertionResult result = RunPredicateFormatter(kFlaky);2573EXPECT_FALSE(result); // Implicit cast to bool.2574std::string expect =2575"Value of: dummy-name\nExpected: [DescribeTo]\n"2576" The matcher failed on the initial attempt; but passed when rerun to "2577"generate the explanation.\n"2578" Actual: 2" +2579OfType(internal::GetTypeName<Behavior>()) + ", [MatchAndExplain]";2580EXPECT_EQ(expect, result.message());2581}25822583// Tests for ElementsAre().25842585TEST(ElementsAreTest, CanDescribeExpectingNoElement) {2586Matcher<const vector<int>&> m = ElementsAre();2587EXPECT_EQ("is empty", Describe(m));2588}25892590TEST(ElementsAreTest, CanDescribeExpectingOneElement) {2591Matcher<vector<int>> m = ElementsAre(Gt(5));2592EXPECT_EQ("has 1 element that is > 5", Describe(m));2593}25942595TEST(ElementsAreTest, CanDescribeExpectingManyElements) {2596Matcher<list<std::string>> m = ElementsAre(StrEq("one"), "two");2597EXPECT_EQ(2598"has 2 elements where\n"2599"element #0 is equal to \"one\",\n"2600"element #1 is equal to \"two\"",2601Describe(m));2602}26032604TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {2605Matcher<vector<int>> m = ElementsAre();2606EXPECT_EQ("isn't empty", DescribeNegation(m));2607}26082609TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElement) {2610Matcher<const list<int>&> m = ElementsAre(Gt(5));2611EXPECT_EQ(2612"doesn't have 1 element, or\n"2613"element #0 isn't > 5",2614DescribeNegation(m));2615}26162617TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {2618Matcher<const list<std::string>&> m = ElementsAre("one", "two");2619EXPECT_EQ(2620"doesn't have 2 elements, or\n"2621"element #0 isn't equal to \"one\", or\n"2622"element #1 isn't equal to \"two\"",2623DescribeNegation(m));2624}26252626TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {2627Matcher<const list<int>&> m = ElementsAre(1, Ne(2));26282629list<int> test_list;2630test_list.push_back(1);2631test_list.push_back(3);2632EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.2633}26342635TEST_P(ElementsAreTestP, ExplainsNonTrivialMatch) {2636Matcher<const vector<int>&> m =2637ElementsAre(GreaterThan(1), 0, GreaterThan(2));26382639const int a[] = {10, 0, 100};2640vector<int> test_vector(std::begin(a), std::end(a));2641EXPECT_EQ(2642"whose element #0 matches, which is 9 more than 1,\n"2643"and whose element #2 matches, which is 98 more than 2",2644Explain(m, test_vector));2645}26462647TEST(ElementsAreTest, CanExplainMismatchWrongSize) {2648Matcher<const list<int>&> m = ElementsAre(1, 3);26492650list<int> test_list;2651// No need to explain when the container is empty.2652EXPECT_EQ("", Explain(m, test_list));26532654test_list.push_back(1);2655EXPECT_EQ("which has 1 element", Explain(m, test_list));2656}26572658TEST_P(ElementsAreTestP, CanExplainMismatchRightSize) {2659Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));26602661vector<int> v;2662v.push_back(2);2663v.push_back(1);2664EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));26652666v[0] = 1;2667EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",2668Explain(m, v));2669}26702671TEST(ElementsAreTest, MatchesOneElementVector) {2672vector<std::string> test_vector;2673test_vector.push_back("test string");26742675EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));2676}26772678TEST(ElementsAreTest, MatchesOneElementList) {2679list<std::string> test_list;2680test_list.push_back("test string");26812682EXPECT_THAT(test_list, ElementsAre("test string"));2683}26842685TEST(ElementsAreTest, MatchesThreeElementVector) {2686vector<std::string> test_vector;2687test_vector.push_back("one");2688test_vector.push_back("two");2689test_vector.push_back("three");26902691EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));2692}26932694TEST(ElementsAreTest, MatchesOneElementEqMatcher) {2695vector<int> test_vector;2696test_vector.push_back(4);26972698EXPECT_THAT(test_vector, ElementsAre(Eq(4)));2699}27002701TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {2702vector<int> test_vector;2703test_vector.push_back(4);27042705EXPECT_THAT(test_vector, ElementsAre(_));2706}27072708TEST(ElementsAreTest, MatchesOneElementValue) {2709vector<int> test_vector;2710test_vector.push_back(4);27112712EXPECT_THAT(test_vector, ElementsAre(4));2713}27142715TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {2716vector<int> test_vector;2717test_vector.push_back(1);2718test_vector.push_back(2);2719test_vector.push_back(3);27202721EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));2722}27232724TEST(ElementsAreTest, MatchesTenElementVector) {2725const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};2726vector<int> test_vector(std::begin(a), std::end(a));27272728EXPECT_THAT(test_vector,2729// The element list can contain values and/or matchers2730// of different types.2731ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));2732}27332734TEST(ElementsAreTest, DoesNotMatchWrongSize) {2735vector<std::string> test_vector;2736test_vector.push_back("test string");2737test_vector.push_back("test string");27382739Matcher<vector<std::string>> m = ElementsAre(StrEq("test string"));2740EXPECT_FALSE(m.Matches(test_vector));2741}27422743TEST(ElementsAreTest, DoesNotMatchWrongValue) {2744vector<std::string> test_vector;2745test_vector.push_back("other string");27462747Matcher<vector<std::string>> m = ElementsAre(StrEq("test string"));2748EXPECT_FALSE(m.Matches(test_vector));2749}27502751TEST(ElementsAreTest, DoesNotMatchWrongOrder) {2752vector<std::string> test_vector;2753test_vector.push_back("one");2754test_vector.push_back("three");2755test_vector.push_back("two");27562757Matcher<vector<std::string>> m =2758ElementsAre(StrEq("one"), StrEq("two"), StrEq("three"));2759EXPECT_FALSE(m.Matches(test_vector));2760}27612762TEST(ElementsAreTest, WorksForNestedContainer) {2763constexpr std::array<const char*, 2> strings = {{"Hi", "world"}};27642765vector<list<char>> nested;2766for (const auto& s : strings) {2767nested.emplace_back(s, s + strlen(s));2768}27692770EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),2771ElementsAre('w', 'o', _, _, 'd')));2772EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),2773ElementsAre('w', 'o', _, _, 'd'))));2774}27752776TEST(ElementsAreTest, WorksWithByRefElementMatchers) {2777int a[] = {0, 1, 2};2778vector<int> v(std::begin(a), std::end(a));27792780EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));2781EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));2782}27832784TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {2785int a[] = {0, 1, 2};2786vector<int> v(std::begin(a), std::end(a));27872788EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));2789EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));2790}27912792TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {2793int array[] = {0, 1, 2};2794EXPECT_THAT(array, ElementsAre(0, 1, _));2795EXPECT_THAT(array, Not(ElementsAre(1, _, _)));2796EXPECT_THAT(array, Not(ElementsAre(0, _)));2797}27982799class NativeArrayPassedAsPointerAndSize {2800public:2801NativeArrayPassedAsPointerAndSize() = default;28022803MOCK_METHOD(void, Helper, (int* array, int size));28042805private:2806NativeArrayPassedAsPointerAndSize(const NativeArrayPassedAsPointerAndSize&) =2807delete;2808NativeArrayPassedAsPointerAndSize& operator=(2809const NativeArrayPassedAsPointerAndSize&) = delete;2810};28112812TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {2813int array[] = {0, 1};2814::std::tuple<int*, size_t> array_as_tuple(array, 2);2815EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));2816EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));28172818NativeArrayPassedAsPointerAndSize helper;2819EXPECT_CALL(helper, Helper(_, _)).With(ElementsAre(0, 1));2820helper.Helper(array, 2);2821}28222823TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {2824const char a2[][3] = {"hi", "lo"};2825EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),2826ElementsAre('l', 'o', '\0')));2827EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));2828EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),2829ElementsAre('l', 'o', '\0')));2830}28312832TEST(ElementsAreTest, AcceptsStringLiteral) {2833std::string array[] = {"hi", "one", "two"};2834EXPECT_THAT(array, ElementsAre("hi", "one", "two"));2835EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too")));2836}28372838// Declared here with the size unknown. Defined AFTER the following test.2839extern const char kHi[];28402841TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {2842// The size of kHi is not known in this test, but ElementsAre() should2843// still accept it.28442845std::string array1[] = {"hi"};2846EXPECT_THAT(array1, ElementsAre(kHi));28472848std::string array2[] = {"ho"};2849EXPECT_THAT(array2, Not(ElementsAre(kHi)));2850}28512852const char kHi[] = "hi";28532854TEST(ElementsAreTest, MakesCopyOfArguments) {2855int x = 1;2856int y = 2;2857// This should make a copy of x and y.2858::testing::internal::ElementsAreMatcher<std::tuple<int, int>>2859polymorphic_matcher = ElementsAre(x, y);2860// Changing x and y now shouldn't affect the meaning of the above matcher.2861x = y = 0;2862const int array1[] = {1, 2};2863EXPECT_THAT(array1, polymorphic_matcher);2864const int array2[] = {0, 0};2865EXPECT_THAT(array2, Not(polymorphic_matcher));2866}28672868// Tests for ElementsAreArray(). Since ElementsAreArray() shares most2869// of the implementation with ElementsAre(), we don't test it as2870// thoroughly here.28712872TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {2873const int a[] = {1, 2, 3};28742875vector<int> test_vector(std::begin(a), std::end(a));2876EXPECT_THAT(test_vector, ElementsAreArray(a));28772878test_vector[2] = 0;2879EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));2880}28812882TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {2883std::array<const char*, 3> a = {{"one", "two", "three"}};28842885vector<std::string> test_vector(std::begin(a), std::end(a));2886EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size()));28872888const char** p = a.data();2889test_vector[0] = "1";2890EXPECT_THAT(test_vector, Not(ElementsAreArray(p, a.size())));2891}28922893TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {2894const char* a[] = {"one", "two", "three"};28952896vector<std::string> test_vector(std::begin(a), std::end(a));2897EXPECT_THAT(test_vector, ElementsAreArray(a));28982899test_vector[0] = "1";2900EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));2901}29022903TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {2904const Matcher<std::string> kMatcherArray[] = {StrEq("one"), StrEq("two"),2905StrEq("three")};29062907vector<std::string> test_vector;2908test_vector.push_back("one");2909test_vector.push_back("two");2910test_vector.push_back("three");2911EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));29122913test_vector.push_back("three");2914EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));2915}29162917TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {2918const int a[] = {1, 2, 3};2919vector<int> test_vector(std::begin(a), std::end(a));2920const vector<int> expected(std::begin(a), std::end(a));2921EXPECT_THAT(test_vector, ElementsAreArray(expected));2922test_vector.push_back(4);2923EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));2924}29252926TEST(ElementsAreArrayTest, TakesInitializerList) {2927const int a[5] = {1, 2, 3, 4, 5};2928EXPECT_THAT(a, ElementsAreArray({1, 2, 3, 4, 5}));2929EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 5, 4})));2930EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 4, 6})));2931}29322933TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {2934const std::string a[5] = {"a", "b", "c", "d", "e"};2935EXPECT_THAT(a, ElementsAreArray({"a", "b", "c", "d", "e"}));2936EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "e", "d"})));2937EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "d", "ef"})));2938}29392940TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {2941const int a[5] = {1, 2, 3, 4, 5};2942EXPECT_THAT(a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));2943EXPECT_THAT(a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));2944}29452946TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) {2947const int a[5] = {1, 2, 3, 4, 5};2948// The compiler cannot infer the type of the initializer list if its2949// elements have different types. We must explicitly specify the2950// unified element type in this case.2951EXPECT_THAT(2952a, ElementsAreArray<Matcher<int>>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));2953EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int>>(2954{Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));2955}29562957TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {2958const int a[] = {1, 2, 3};2959const Matcher<int> kMatchers[] = {Eq(1), Eq(2), Eq(3)};2960vector<int> test_vector(std::begin(a), std::end(a));2961const vector<Matcher<int>> expected(std::begin(kMatchers),2962std::end(kMatchers));2963EXPECT_THAT(test_vector, ElementsAreArray(expected));2964test_vector.push_back(4);2965EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));2966}29672968TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {2969const int a[] = {1, 2, 3};2970const vector<int> test_vector(std::begin(a), std::end(a));2971const vector<int> expected(std::begin(a), std::end(a));2972EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));2973// Pointers are iterators, too.2974EXPECT_THAT(test_vector, ElementsAreArray(std::begin(a), std::end(a)));2975// The empty range of NULL pointers should also be okay.2976int* const null_int = nullptr;2977EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));2978EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));2979}29802981// Since ElementsAre() and ElementsAreArray() share much of the2982// implementation, we only do a test for native arrays here.2983TEST(ElementsAreArrayTest, WorksWithNativeArray) {2984::std::string a[] = {"hi", "ho"};2985::std::string b[] = {"hi", "ho"};29862987EXPECT_THAT(a, ElementsAreArray(b));2988EXPECT_THAT(a, ElementsAreArray(b, 2));2989EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));2990}29912992TEST(ElementsAreArrayTest, SourceLifeSpan) {2993const int a[] = {1, 2, 3};2994vector<int> test_vector(std::begin(a), std::end(a));2995vector<int> expect(std::begin(a), std::end(a));2996ElementsAreArrayMatcher<int> matcher_maker =2997ElementsAreArray(expect.begin(), expect.end());2998EXPECT_THAT(test_vector, matcher_maker);2999// Changing in place the values that initialized matcher_maker should not3000// affect matcher_maker anymore. It should have made its own copy of them.3001for (int& i : expect) {3002i += 10;3003}3004EXPECT_THAT(test_vector, matcher_maker);3005test_vector.push_back(3);3006EXPECT_THAT(test_vector, Not(matcher_maker));3007}30083009// Tests Contains().30103011INSTANTIATE_GTEST_MATCHER_TEST_P(ContainsTest);30123013TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {3014list<int> some_list;3015some_list.push_back(3);3016some_list.push_back(1);3017some_list.push_back(2);3018some_list.push_back(3);3019EXPECT_THAT(some_list, Contains(1));3020EXPECT_THAT(some_list, Contains(Gt(2.5)));3021EXPECT_THAT(some_list, Contains(Eq(2.0f)));30223023list<std::string> another_list;3024another_list.push_back("fee");3025another_list.push_back("fie");3026another_list.push_back("foe");3027another_list.push_back("fum");3028EXPECT_THAT(another_list, Contains(std::string("fee")));3029}30303031TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {3032list<int> some_list;3033some_list.push_back(3);3034some_list.push_back(1);3035EXPECT_THAT(some_list, Not(Contains(4)));3036}30373038TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {3039set<int> some_set;3040some_set.insert(3);3041some_set.insert(1);3042some_set.insert(2);3043EXPECT_THAT(some_set, Contains(Eq(1.0)));3044EXPECT_THAT(some_set, Contains(Eq(3.0f)));3045EXPECT_THAT(some_set, Contains(2));30463047set<std::string> another_set;3048another_set.insert("fee");3049another_set.insert("fie");3050another_set.insert("foe");3051another_set.insert("fum");3052EXPECT_THAT(another_set, Contains(Eq(std::string("fum"))));3053}30543055TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {3056set<int> some_set;3057some_set.insert(3);3058some_set.insert(1);3059EXPECT_THAT(some_set, Not(Contains(4)));30603061set<std::string> c_string_set;3062c_string_set.insert("hello");3063EXPECT_THAT(c_string_set, Not(Contains(std::string("goodbye"))));3064}30653066TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) {3067const int a[2] = {1, 2};3068Matcher<const int(&)[2]> m = Contains(2);3069EXPECT_EQ("whose element #1 matches", Explain(m, a));30703071m = Contains(3);3072EXPECT_EQ("", Explain(m, a));30733074m = Contains(GreaterThan(0));3075EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));30763077m = Contains(GreaterThan(10));3078EXPECT_EQ("", Explain(m, a));3079}30803081TEST(ContainsTest, DescribesItselfCorrectly) {3082Matcher<vector<int>> m = Contains(1);3083EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));30843085Matcher<vector<int>> m2 = Not(m);3086EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));3087}30883089TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {3090map<std::string, int> my_map;3091const char* bar = "a string";3092my_map[bar] = 2;3093EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));30943095map<std::string, int> another_map;3096another_map["fee"] = 1;3097another_map["fie"] = 2;3098another_map["foe"] = 3;3099another_map["fum"] = 4;3100EXPECT_THAT(another_map,3101Contains(pair<const std::string, int>(std::string("fee"), 1)));3102EXPECT_THAT(another_map, Contains(pair<const std::string, int>("fie", 2)));3103}31043105TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {3106map<int, int> some_map;3107some_map[1] = 11;3108some_map[2] = 22;3109EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));3110}31113112TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {3113const char* string_array[] = {"fee", "fie", "foe", "fum"};3114EXPECT_THAT(string_array, Contains(Eq(std::string("fum"))));3115}31163117TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {3118int int_array[] = {1, 2, 3, 4};3119EXPECT_THAT(int_array, Not(Contains(5)));3120}31213122TEST(ContainsTest, AcceptsMatcher) {3123const int a[] = {1, 2, 3};3124EXPECT_THAT(a, Contains(Gt(2)));3125EXPECT_THAT(a, Not(Contains(Gt(4))));3126}31273128TEST(ContainsTest, WorksForNativeArrayAsTuple) {3129const int a[] = {1, 2};3130const int* const pointer = a;3131EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1));3132EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3))));3133}31343135TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {3136int a[][3] = {{1, 2, 3}, {4, 5, 6}};3137EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));3138EXPECT_THAT(a, Contains(Contains(5)));3139EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));3140EXPECT_THAT(a, Contains(Not(Contains(5))));3141}31423143} // namespace3144} // namespace gmock_matchers_test3145} // namespace testing31463147GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100314831493150