// Copyright 2007, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Google Mock - a framework for writing C++ mock classes. // // This file tests some commonly used argument matchers. #include #include #include #include #include #include #include #include #include #include #include #include "gtest/gtest.h" // Silence warning C4244: 'initializing': conversion from 'int' to 'short', // possible loss of data and C4100, unreferenced local parameter GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100) #include "test/gmock-matchers_test.h" namespace testing { namespace gmock_matchers_test { namespace { std::vector> MakeUniquePtrs(const std::vector& ints) { std::vector> pointers; for (int i : ints) pointers.emplace_back(new int(i)); return pointers; } std::string OfType(const std::string& type_name) { #if GTEST_HAS_RTTI return IsReadableTypeName(type_name) ? " (of type " + type_name + ")" : ""; #else return ""; #endif } TEST(ContainsTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(Contains(Pointee(2)))); helper.Call(MakeUniquePtrs({1, 2})); } INSTANTIATE_GTEST_MATCHER_TEST_P(ElementsAreTest); // Tests the variadic version of the ElementsAreMatcher TEST(ElementsAreTest, HugeMatcher) { vector test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; EXPECT_THAT(test_vector, ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7), Eq(8), Eq(9), Eq(10), Gt(1), Eq(12))); } // Tests the variadic version of the UnorderedElementsAreMatcher TEST(ElementsAreTest, HugeMatcherStr) { vector test_vector{ "literal_string", "", "", "", "", "", "", "", "", "", "", ""}; EXPECT_THAT(test_vector, UnorderedElementsAre("literal_string", _, _, _, _, _, _, _, _, _, _, _)); } // Tests the variadic version of the UnorderedElementsAreMatcher TEST(ElementsAreTest, HugeMatcherUnordered) { vector test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10}; EXPECT_THAT(test_vector, UnorderedElementsAre( Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7), Eq(3), Eq(9), Eq(12), Eq(11), Ne(122))); } // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value // matches the matcher. TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) { ASSERT_THAT(5, Ge(2)) << "This should succeed."; ASSERT_THAT("Foo", EndsWith("oo")); EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too."; EXPECT_THAT("Hello", StartsWith("Hell")); } // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value // doesn't match the matcher. TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) { // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(), // which cannot reference auto variables. static unsigned short n; // NOLINT n = 5; EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)), "Value of: n\n" "Expected: is > 10\n" " Actual: 5" + OfType("unsigned short")); n = 0; EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))), "Value of: n\n" "Expected: (is <= 7) and (is >= 5)\n" " Actual: 0" + OfType("unsigned short")); } // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument // has a reference type. TEST(MatcherAssertionTest, WorksForByRefArguments) { // We use a static variable here as EXPECT_FATAL_FAILURE() cannot // reference auto variables. static int n; n = 0; EXPECT_THAT(n, AllOf(Le(7), Ref(n))); EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), "Value of: n\n" "Expected: does not reference the variable @"); // Tests the "Actual" part. EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), "Actual: 0" + OfType("int") + ", which is located @"); } // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is // monomorphic. TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { Matcher starts_with_he = StartsWith("he"); ASSERT_THAT("hello", starts_with_he); Matcher ends_with_ok = EndsWith("ok"); ASSERT_THAT("book", ends_with_ok); const std::string bad = "bad"; EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok), "Value of: bad\n" "Expected: ends with \"ok\"\n" " Actual: \"bad\""); Matcher is_greater_than_5 = Gt(5); EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), "Value of: 5\n" "Expected: is > 5\n" " Actual: 5" + OfType("int")); } TEST(PointeeTest, RawPointer) { const Matcher m = Pointee(Ge(0)); int n = 1; EXPECT_TRUE(m.Matches(&n)); n = -1; EXPECT_FALSE(m.Matches(&n)); EXPECT_FALSE(m.Matches(nullptr)); } TEST(PointeeTest, RawPointerToConst) { const Matcher m = Pointee(Ge(0)); double x = 1; EXPECT_TRUE(m.Matches(&x)); x = -1; EXPECT_FALSE(m.Matches(&x)); EXPECT_FALSE(m.Matches(nullptr)); } TEST(PointeeTest, ReferenceToConstRawPointer) { const Matcher m = Pointee(Ge(0)); int n = 1; EXPECT_TRUE(m.Matches(&n)); n = -1; EXPECT_FALSE(m.Matches(&n)); EXPECT_FALSE(m.Matches(nullptr)); } TEST(PointeeTest, ReferenceToNonConstRawPointer) { const Matcher m = Pointee(Ge(0)); double x = 1.0; double* p = &x; EXPECT_TRUE(m.Matches(p)); x = -1; EXPECT_FALSE(m.Matches(p)); p = nullptr; EXPECT_FALSE(m.Matches(p)); } TEST(PointeeTest, SmartPointer) { const Matcher> m = Pointee(Ge(0)); std::unique_ptr n(new int(1)); EXPECT_TRUE(m.Matches(n)); } TEST(PointeeTest, SmartPointerToConst) { const Matcher> m = Pointee(Ge(0)); // There's no implicit conversion from unique_ptr to const // unique_ptr, so we must pass a unique_ptr into the // matcher. std::unique_ptr n(new int(1)); EXPECT_TRUE(m.Matches(n)); } TEST(PointerTest, RawPointer) { int n = 1; const Matcher m = Pointer(Eq(&n)); EXPECT_TRUE(m.Matches(&n)); int* p = nullptr; EXPECT_FALSE(m.Matches(p)); EXPECT_FALSE(m.Matches(nullptr)); } TEST(PointerTest, RawPointerToConst) { int n = 1; const Matcher m = Pointer(Eq(&n)); EXPECT_TRUE(m.Matches(&n)); int* p = nullptr; EXPECT_FALSE(m.Matches(p)); EXPECT_FALSE(m.Matches(nullptr)); } TEST(PointerTest, SmartPointer) { std::unique_ptr n(new int(10)); int* raw_n = n.get(); const Matcher> m = Pointer(Eq(raw_n)); EXPECT_TRUE(m.Matches(n)); } TEST(PointerTest, SmartPointerToConst) { std::unique_ptr n(new int(10)); const int* raw_n = n.get(); const Matcher> m = Pointer(Eq(raw_n)); // There's no implicit conversion from unique_ptr to const // unique_ptr, so we must pass a unique_ptr into the // matcher. std::unique_ptr p(new int(10)); EXPECT_FALSE(m.Matches(p)); } // Minimal const-propagating pointer. template class ConstPropagatingPtr { public: typedef T element_type; ConstPropagatingPtr() : val_() {} explicit ConstPropagatingPtr(T* t) : val_(t) {} ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {} T* get() { return val_; } T& operator*() { return *val_; } // Most smart pointers return non-const T* and T& from the next methods. const T* get() const { return val_; } const T& operator*() const { return *val_; } private: T* val_; }; INSTANTIATE_GTEST_MATCHER_TEST_P(PointeeTest); TEST(PointeeTest, WorksWithConstPropagatingPointers) { const Matcher> m = Pointee(Lt(5)); int three = 3; const ConstPropagatingPtr co(&three); ConstPropagatingPtr o(&three); EXPECT_TRUE(m.Matches(o)); EXPECT_TRUE(m.Matches(co)); *o = 6; EXPECT_FALSE(m.Matches(o)); EXPECT_FALSE(m.Matches(ConstPropagatingPtr())); } TEST(PointeeTest, NeverMatchesNull) { const Matcher m = Pointee(_); EXPECT_FALSE(m.Matches(nullptr)); } // Tests that we can write Pointee(value) instead of Pointee(Eq(value)). TEST(PointeeTest, MatchesAgainstAValue) { const Matcher m = Pointee(5); int n = 5; EXPECT_TRUE(m.Matches(&n)); n = -1; EXPECT_FALSE(m.Matches(&n)); EXPECT_FALSE(m.Matches(nullptr)); } TEST(PointeeTest, CanDescribeSelf) { const Matcher m = Pointee(Gt(3)); EXPECT_EQ("points to a value that is > 3", Describe(m)); EXPECT_EQ("does not point to a value that is > 3", DescribeNegation(m)); } TEST_P(PointeeTestP, CanExplainMatchResult) { const Matcher m = Pointee(StartsWith("Hi")); EXPECT_EQ("", Explain(m, static_cast(nullptr))); const Matcher m2 = Pointee(GreaterThan(1)); // NOLINT long n = 3; // NOLINT EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1", Explain(m2, &n)); } TEST(PointeeTest, AlwaysExplainsPointee) { const Matcher m = Pointee(0); int n = 42; EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n)); } // An uncopyable class. class Uncopyable { public: Uncopyable() : value_(-1) {} explicit Uncopyable(int a_value) : value_(a_value) {} int value() const { return value_; } void set_value(int i) { value_ = i; } private: int value_; Uncopyable(const Uncopyable&) = delete; Uncopyable& operator=(const Uncopyable&) = delete; }; // Returns true if and only if x.value() is positive. bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; } MATCHER_P(UncopyableIs, inner_matcher, "") { return ExplainMatchResult(inner_matcher, arg.value(), result_listener); } // A user-defined struct for testing Field(). struct AStruct { AStruct() : x(0), y(1.0), z(5), p(nullptr) {} AStruct(const AStruct& rhs) : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {} int x; // A non-const field. const double y; // A const field. Uncopyable z; // An uncopyable field. const char* p; // A pointer field. }; // A derived struct for testing Field(). struct DerivedStruct : public AStruct { char ch; }; INSTANTIATE_GTEST_MATCHER_TEST_P(FieldTest); // Tests that Field(&Foo::field, ...) works when field is non-const. TEST(FieldTest, WorksForNonConstField) { Matcher m = Field(&AStruct::x, Ge(0)); Matcher m_with_name = Field("x", &AStruct::x, Ge(0)); AStruct a; EXPECT_TRUE(m.Matches(a)); EXPECT_TRUE(m_with_name.Matches(a)); a.x = -1; EXPECT_FALSE(m.Matches(a)); EXPECT_FALSE(m_with_name.Matches(a)); } // Tests that Field(&Foo::field, ...) works when field is const. TEST(FieldTest, WorksForConstField) { AStruct a; Matcher m = Field(&AStruct::y, Ge(0.0)); Matcher m_with_name = Field("y", &AStruct::y, Ge(0.0)); EXPECT_TRUE(m.Matches(a)); EXPECT_TRUE(m_with_name.Matches(a)); m = Field(&AStruct::y, Le(0.0)); m_with_name = Field("y", &AStruct::y, Le(0.0)); EXPECT_FALSE(m.Matches(a)); EXPECT_FALSE(m_with_name.Matches(a)); } // Tests that Field(&Foo::field, ...) works when field is not copyable. TEST(FieldTest, WorksForUncopyableField) { AStruct a; Matcher m = Field(&AStruct::z, Truly(ValueIsPositive)); EXPECT_TRUE(m.Matches(a)); m = Field(&AStruct::z, Not(Truly(ValueIsPositive))); EXPECT_FALSE(m.Matches(a)); } // Tests that Field(&Foo::field, ...) works when field is a pointer. TEST(FieldTest, WorksForPointerField) { // Matching against NULL. Matcher m = Field(&AStruct::p, static_cast(nullptr)); AStruct a; EXPECT_TRUE(m.Matches(a)); a.p = "hi"; EXPECT_FALSE(m.Matches(a)); // Matching a pointer that is not NULL. m = Field(&AStruct::p, StartsWith("hi")); a.p = "hill"; EXPECT_TRUE(m.Matches(a)); a.p = "hole"; EXPECT_FALSE(m.Matches(a)); } // Tests that Field() works when the object is passed by reference. TEST(FieldTest, WorksForByRefArgument) { Matcher m = Field(&AStruct::x, Ge(0)); AStruct a; EXPECT_TRUE(m.Matches(a)); a.x = -1; EXPECT_FALSE(m.Matches(a)); } // Tests that Field(&Foo::field, ...) works when the argument's type // is a sub-type of Foo. TEST(FieldTest, WorksForArgumentOfSubType) { // Note that the matcher expects DerivedStruct but we say AStruct // inside Field(). Matcher m = Field(&AStruct::x, Ge(0)); DerivedStruct d; EXPECT_TRUE(m.Matches(d)); d.x = -1; EXPECT_FALSE(m.Matches(d)); } // Tests that Field(&Foo::field, m) works when field's type and m's // argument type are compatible but not the same. TEST(FieldTest, WorksForCompatibleMatcherType) { // The field is an int, but the inner matcher expects a signed char. Matcher m = Field(&AStruct::x, Matcher(Ge(0))); AStruct a; EXPECT_TRUE(m.Matches(a)); a.x = -1; EXPECT_FALSE(m.Matches(a)); } // Tests that Field() can describe itself. TEST(FieldTest, CanDescribeSelf) { Matcher m = Field(&AStruct::x, Ge(0)); EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); } TEST(FieldTest, CanDescribeSelfWithFieldName) { Matcher m = Field("field_name", &AStruct::x, Ge(0)); EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m)); EXPECT_EQ("is an object whose field `field_name` isn't >= 0", DescribeNegation(m)); } // Tests that Field() can explain the match result. TEST_P(FieldTestP, CanExplainMatchResult) { Matcher m = Field(&AStruct::x, Ge(0)); AStruct a; a.x = 1; EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a)); m = Field(&AStruct::x, GreaterThan(0)); EXPECT_EQ( "whose given field is 1" + OfType("int") + ", which is 1 more than 0", Explain(m, a)); } TEST_P(FieldTestP, CanExplainMatchResultWithFieldName) { Matcher m = Field("field_name", &AStruct::x, Ge(0)); AStruct a; a.x = 1; EXPECT_EQ("whose field `field_name` is 1" + OfType("int"), Explain(m, a)); m = Field("field_name", &AStruct::x, GreaterThan(0)); EXPECT_EQ("whose field `field_name` is 1" + OfType("int") + ", which is 1 more than 0", Explain(m, a)); } INSTANTIATE_GTEST_MATCHER_TEST_P(FieldForPointerTest); // Tests that Field() works when the argument is a pointer to const. TEST(FieldForPointerTest, WorksForPointerToConst) { Matcher m = Field(&AStruct::x, Ge(0)); AStruct a; EXPECT_TRUE(m.Matches(&a)); a.x = -1; EXPECT_FALSE(m.Matches(&a)); } // Tests that Field() works when the argument is a pointer to non-const. TEST(FieldForPointerTest, WorksForPointerToNonConst) { Matcher m = Field(&AStruct::x, Ge(0)); AStruct a; EXPECT_TRUE(m.Matches(&a)); a.x = -1; EXPECT_FALSE(m.Matches(&a)); } // Tests that Field() works when the argument is a reference to a const pointer. TEST(FieldForPointerTest, WorksForReferenceToConstPointer) { Matcher m = Field(&AStruct::x, Ge(0)); AStruct a; EXPECT_TRUE(m.Matches(&a)); a.x = -1; EXPECT_FALSE(m.Matches(&a)); } // Tests that Field() does not match the NULL pointer. TEST(FieldForPointerTest, DoesNotMatchNull) { Matcher m = Field(&AStruct::x, _); EXPECT_FALSE(m.Matches(nullptr)); } // Tests that Field(&Foo::field, ...) works when the argument's type // is a sub-type of const Foo*. TEST(FieldForPointerTest, WorksForArgumentOfSubType) { // Note that the matcher expects DerivedStruct but we say AStruct // inside Field(). Matcher m = Field(&AStruct::x, Ge(0)); DerivedStruct d; EXPECT_TRUE(m.Matches(&d)); d.x = -1; EXPECT_FALSE(m.Matches(&d)); } // Tests that Field() can describe itself when used to match a pointer. TEST(FieldForPointerTest, CanDescribeSelf) { Matcher m = Field(&AStruct::x, Ge(0)); EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); } TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) { Matcher m = Field("field_name", &AStruct::x, Ge(0)); EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m)); EXPECT_EQ("is an object whose field `field_name` isn't >= 0", DescribeNegation(m)); } // Tests that Field() can explain the result of matching a pointer. TEST_P(FieldForPointerTestP, CanExplainMatchResult) { Matcher m = Field(&AStruct::x, Ge(0)); AStruct a; a.x = 1; EXPECT_EQ("", Explain(m, static_cast(nullptr))); EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"), Explain(m, &a)); m = Field(&AStruct::x, GreaterThan(0)); EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") + ", which is 1 more than 0", Explain(m, &a)); } TEST_P(FieldForPointerTestP, CanExplainMatchResultWithFieldName) { Matcher m = Field("field_name", &AStruct::x, Ge(0)); AStruct a; a.x = 1; EXPECT_EQ("", Explain(m, static_cast(nullptr))); EXPECT_EQ( "which points to an object whose field `field_name` is 1" + OfType("int"), Explain(m, &a)); m = Field("field_name", &AStruct::x, GreaterThan(0)); EXPECT_EQ("which points to an object whose field `field_name` is 1" + OfType("int") + ", which is 1 more than 0", Explain(m, &a)); } // A user-defined class for testing Property(). class AClass { public: AClass() : n_(0) {} // A getter that returns a non-reference. int n() const { return n_; } void set_n(int new_n) { n_ = new_n; } // A getter that returns a reference to const. const std::string& s() const { return s_; } const std::string& s_ref() const& { return s_; } void set_s(const std::string& new_s) { s_ = new_s; } // A getter that returns a reference to non-const. double& x() const { return x_; } private: int n_; std::string s_; static double x_; }; double AClass::x_ = 0.0; // A derived class for testing Property(). class DerivedClass : public AClass { public: int k() const { return k_; } private: int k_; }; INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyTest); // Tests that Property(&Foo::property, ...) works when property() // returns a non-reference. TEST(PropertyTest, WorksForNonReferenceProperty) { Matcher m = Property(&AClass::n, Ge(0)); Matcher m_with_name = Property("n", &AClass::n, Ge(0)); AClass a; a.set_n(1); EXPECT_TRUE(m.Matches(a)); EXPECT_TRUE(m_with_name.Matches(a)); a.set_n(-1); EXPECT_FALSE(m.Matches(a)); EXPECT_FALSE(m_with_name.Matches(a)); } // Tests that Property(&Foo::property, ...) works when property() // returns a reference to const. TEST(PropertyTest, WorksForReferenceToConstProperty) { Matcher m = Property(&AClass::s, StartsWith("hi")); Matcher m_with_name = Property("s", &AClass::s, StartsWith("hi")); AClass a; a.set_s("hill"); EXPECT_TRUE(m.Matches(a)); EXPECT_TRUE(m_with_name.Matches(a)); a.set_s("hole"); EXPECT_FALSE(m.Matches(a)); EXPECT_FALSE(m_with_name.Matches(a)); } // Tests that Property(&Foo::property, ...) works when property() is // ref-qualified. TEST(PropertyTest, WorksForRefQualifiedProperty) { Matcher m = Property(&AClass::s_ref, StartsWith("hi")); Matcher m_with_name = Property("s", &AClass::s_ref, StartsWith("hi")); AClass a; a.set_s("hill"); EXPECT_TRUE(m.Matches(a)); EXPECT_TRUE(m_with_name.Matches(a)); a.set_s("hole"); EXPECT_FALSE(m.Matches(a)); EXPECT_FALSE(m_with_name.Matches(a)); } // Tests that Property(&Foo::property, ...) works when property() // returns a reference to non-const. TEST(PropertyTest, WorksForReferenceToNonConstProperty) { double x = 0.0; AClass a; Matcher m = Property(&AClass::x, Ref(x)); EXPECT_FALSE(m.Matches(a)); m = Property(&AClass::x, Not(Ref(x))); EXPECT_TRUE(m.Matches(a)); } // Tests that Property(&Foo::property, ...) works when the argument is // passed by value. TEST(PropertyTest, WorksForByValueArgument) { Matcher m = Property(&AClass::s, StartsWith("hi")); AClass a; a.set_s("hill"); EXPECT_TRUE(m.Matches(a)); a.set_s("hole"); EXPECT_FALSE(m.Matches(a)); } // Tests that Property(&Foo::property, ...) works when the argument's // type is a sub-type of Foo. TEST(PropertyTest, WorksForArgumentOfSubType) { // The matcher expects a DerivedClass, but inside the Property() we // say AClass. Matcher m = Property(&AClass::n, Ge(0)); DerivedClass d; d.set_n(1); EXPECT_TRUE(m.Matches(d)); d.set_n(-1); EXPECT_FALSE(m.Matches(d)); } // Tests that Property(&Foo::property, m) works when property()'s type // and m's argument type are compatible but different. TEST(PropertyTest, WorksForCompatibleMatcherType) { // n() returns an int but the inner matcher expects a signed char. Matcher m = Property(&AClass::n, Matcher(Ge(0))); Matcher m_with_name = Property("n", &AClass::n, Matcher(Ge(0))); AClass a; EXPECT_TRUE(m.Matches(a)); EXPECT_TRUE(m_with_name.Matches(a)); a.set_n(-1); EXPECT_FALSE(m.Matches(a)); EXPECT_FALSE(m_with_name.Matches(a)); } // Tests that Property() can describe itself. TEST(PropertyTest, CanDescribeSelf) { Matcher m = Property(&AClass::n, Ge(0)); EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); EXPECT_EQ("is an object whose given property isn't >= 0", DescribeNegation(m)); } TEST(PropertyTest, CanDescribeSelfWithPropertyName) { Matcher m = Property("fancy_name", &AClass::n, Ge(0)); EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m)); EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0", DescribeNegation(m)); } // Tests that Property() can explain the match result. TEST_P(PropertyTestP, CanExplainMatchResult) { Matcher m = Property(&AClass::n, Ge(0)); AClass a; a.set_n(1); EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a)); m = Property(&AClass::n, GreaterThan(0)); EXPECT_EQ( "whose given property is 1" + OfType("int") + ", which is 1 more than 0", Explain(m, a)); } TEST_P(PropertyTestP, CanExplainMatchResultWithPropertyName) { Matcher m = Property("fancy_name", &AClass::n, Ge(0)); AClass a; a.set_n(1); EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int"), Explain(m, a)); m = Property("fancy_name", &AClass::n, GreaterThan(0)); EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int") + ", which is 1 more than 0", Explain(m, a)); } INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyForPointerTest); // Tests that Property() works when the argument is a pointer to const. TEST(PropertyForPointerTest, WorksForPointerToConst) { Matcher m = Property(&AClass::n, Ge(0)); AClass a; a.set_n(1); EXPECT_TRUE(m.Matches(&a)); a.set_n(-1); EXPECT_FALSE(m.Matches(&a)); } // Tests that Property() works when the argument is a pointer to non-const. TEST(PropertyForPointerTest, WorksForPointerToNonConst) { Matcher m = Property(&AClass::s, StartsWith("hi")); AClass a; a.set_s("hill"); EXPECT_TRUE(m.Matches(&a)); a.set_s("hole"); EXPECT_FALSE(m.Matches(&a)); } // Tests that Property() works when the argument is a reference to a // const pointer. TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) { Matcher m = Property(&AClass::s, StartsWith("hi")); AClass a; a.set_s("hill"); EXPECT_TRUE(m.Matches(&a)); a.set_s("hole"); EXPECT_FALSE(m.Matches(&a)); } // Tests that Property() does not match the NULL pointer. TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) { Matcher m = Property(&AClass::x, _); EXPECT_FALSE(m.Matches(nullptr)); } // Tests that Property(&Foo::property, ...) works when the argument's // type is a sub-type of const Foo*. TEST(PropertyForPointerTest, WorksForArgumentOfSubType) { // The matcher expects a DerivedClass, but inside the Property() we // say AClass. Matcher m = Property(&AClass::n, Ge(0)); DerivedClass d; d.set_n(1); EXPECT_TRUE(m.Matches(&d)); d.set_n(-1); EXPECT_FALSE(m.Matches(&d)); } // Tests that Property() can describe itself when used to match a pointer. TEST(PropertyForPointerTest, CanDescribeSelf) { Matcher m = Property(&AClass::n, Ge(0)); EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); EXPECT_EQ("is an object whose given property isn't >= 0", DescribeNegation(m)); } TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) { Matcher m = Property("fancy_name", &AClass::n, Ge(0)); EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m)); EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0", DescribeNegation(m)); } // Tests that Property() can explain the result of matching a pointer. TEST_P(PropertyForPointerTestP, CanExplainMatchResult) { Matcher m = Property(&AClass::n, Ge(0)); AClass a; a.set_n(1); EXPECT_EQ("", Explain(m, static_cast(nullptr))); EXPECT_EQ( "which points to an object whose given property is 1" + OfType("int"), Explain(m, &a)); m = Property(&AClass::n, GreaterThan(0)); EXPECT_EQ("which points to an object whose given property is 1" + OfType("int") + ", which is 1 more than 0", Explain(m, &a)); } TEST_P(PropertyForPointerTestP, CanExplainMatchResultWithPropertyName) { Matcher m = Property("fancy_name", &AClass::n, Ge(0)); AClass a; a.set_n(1); EXPECT_EQ("", Explain(m, static_cast(nullptr))); EXPECT_EQ("which points to an object whose property `fancy_name` is 1" + OfType("int"), Explain(m, &a)); m = Property("fancy_name", &AClass::n, GreaterThan(0)); EXPECT_EQ("which points to an object whose property `fancy_name` is 1" + OfType("int") + ", which is 1 more than 0", Explain(m, &a)); } // Tests ResultOf. // Tests that ResultOf(f, ...) compiles and works as expected when f is a // function pointer. std::string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; } INSTANTIATE_GTEST_MATCHER_TEST_P(ResultOfTest); TEST(ResultOfTest, WorksForFunctionPointers) { Matcher matcher = ResultOf(&IntToStringFunction, Eq(std::string("foo"))); EXPECT_TRUE(matcher.Matches(1)); EXPECT_FALSE(matcher.Matches(2)); } // Tests that ResultOf() can describe itself. TEST(ResultOfTest, CanDescribeItself) { Matcher matcher = ResultOf(&IntToStringFunction, StrEq("foo")); EXPECT_EQ( "is mapped by the given callable to a value that " "is equal to \"foo\"", Describe(matcher)); EXPECT_EQ( "is mapped by the given callable to a value that " "isn't equal to \"foo\"", DescribeNegation(matcher)); } // Tests that ResultOf() can describe itself when provided a result description. TEST(ResultOfTest, CanDescribeItselfWithResultDescription) { Matcher matcher = ResultOf("string conversion", &IntToStringFunction, StrEq("foo")); EXPECT_EQ("whose string conversion is equal to \"foo\"", Describe(matcher)); EXPECT_EQ("whose string conversion isn't equal to \"foo\"", DescribeNegation(matcher)); } // Tests that ResultOf() can explain the match result. int IntFunction(int input) { return input == 42 ? 80 : 90; } TEST_P(ResultOfTestP, CanExplainMatchResult) { Matcher matcher = ResultOf(&IntFunction, Ge(85)); EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"), Explain(matcher, 36)); matcher = ResultOf(&IntFunction, GreaterThan(85)); EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") + ", which is 5 more than 85", Explain(matcher, 36)); } TEST_P(ResultOfTestP, CanExplainMatchResultWithResultDescription) { Matcher matcher = ResultOf("magic int conversion", &IntFunction, Ge(85)); EXPECT_EQ("whose magic int conversion is 90" + OfType("int"), Explain(matcher, 36)); matcher = ResultOf("magic int conversion", &IntFunction, GreaterThan(85)); EXPECT_EQ("whose magic int conversion is 90" + OfType("int") + ", which is 5 more than 85", Explain(matcher, 36)); } // Tests that ResultOf(f, ...) compiles and works as expected when f(x) // returns a non-reference. TEST(ResultOfTest, WorksForNonReferenceResults) { Matcher matcher = ResultOf(&IntFunction, Eq(80)); EXPECT_TRUE(matcher.Matches(42)); EXPECT_FALSE(matcher.Matches(36)); } // Tests that ResultOf(f, ...) compiles and works as expected when f(x) // returns a reference to non-const. double& DoubleFunction(double& input) { return input; } // NOLINT Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT return obj; } TEST(ResultOfTest, WorksForReferenceToNonConstResults) { double x = 3.14; double x2 = x; Matcher matcher = ResultOf(&DoubleFunction, Ref(x)); EXPECT_TRUE(matcher.Matches(x)); EXPECT_FALSE(matcher.Matches(x2)); // Test that ResultOf works with uncopyable objects Uncopyable obj(0); Uncopyable obj2(0); Matcher matcher2 = ResultOf(&RefUncopyableFunction, Ref(obj)); EXPECT_TRUE(matcher2.Matches(obj)); EXPECT_FALSE(matcher2.Matches(obj2)); } // Tests that ResultOf(f, ...) compiles and works as expected when f(x) // returns a reference to const. const std::string& StringFunction(const std::string& input) { return input; } TEST(ResultOfTest, WorksForReferenceToConstResults) { std::string s = "foo"; std::string s2 = s; Matcher matcher = ResultOf(&StringFunction, Ref(s)); EXPECT_TRUE(matcher.Matches(s)); EXPECT_FALSE(matcher.Matches(s2)); } // Tests that ResultOf(f, m) works when f(x) and m's // argument types are compatible but different. TEST(ResultOfTest, WorksForCompatibleMatcherTypes) { // IntFunction() returns int but the inner matcher expects a signed char. Matcher matcher = ResultOf(IntFunction, Matcher(Ge(85))); EXPECT_TRUE(matcher.Matches(36)); EXPECT_FALSE(matcher.Matches(42)); } // Tests that the program aborts when ResultOf is passed // a NULL function pointer. TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) { EXPECT_DEATH_IF_SUPPORTED( ResultOf(static_cast(nullptr), Eq(std::string("foo"))), "NULL function pointer is passed into ResultOf\\(\\)\\."); } // Tests that ResultOf(f, ...) compiles and works as expected when f is a // function reference. TEST(ResultOfTest, WorksForFunctionReferences) { Matcher matcher = ResultOf(IntToStringFunction, StrEq("foo")); EXPECT_TRUE(matcher.Matches(1)); EXPECT_FALSE(matcher.Matches(2)); } // Tests that ResultOf(f, ...) compiles and works as expected when f is a // function object. struct Functor { std::string operator()(int input) const { return IntToStringFunction(input); } }; TEST(ResultOfTest, WorksForFunctors) { Matcher matcher = ResultOf(Functor(), Eq(std::string("foo"))); EXPECT_TRUE(matcher.Matches(1)); EXPECT_FALSE(matcher.Matches(2)); } // Tests that ResultOf(f, ...) compiles and works as expected when f is a // functor with more than one operator() defined. ResultOf() must work // for each defined operator(). struct PolymorphicFunctor { typedef int result_type; int operator()(int n) { return n; } int operator()(const char* s) { return static_cast(strlen(s)); } std::string operator()(int* p) { return p ? "good ptr" : "null"; } }; TEST(ResultOfTest, WorksForPolymorphicFunctors) { Matcher matcher_int = ResultOf(PolymorphicFunctor(), Ge(5)); EXPECT_TRUE(matcher_int.Matches(10)); EXPECT_FALSE(matcher_int.Matches(2)); Matcher matcher_string = ResultOf(PolymorphicFunctor(), Ge(5)); EXPECT_TRUE(matcher_string.Matches("long string")); EXPECT_FALSE(matcher_string.Matches("shrt")); } TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) { Matcher matcher = ResultOf(PolymorphicFunctor(), "good ptr"); int n = 0; EXPECT_TRUE(matcher.Matches(&n)); EXPECT_FALSE(matcher.Matches(nullptr)); } TEST(ResultOfTest, WorksForLambdas) { Matcher matcher = ResultOf( [](int str_len) { return std::string(static_cast(str_len), 'x'); }, "xxx"); EXPECT_TRUE(matcher.Matches(3)); EXPECT_FALSE(matcher.Matches(1)); } TEST(ResultOfTest, WorksForNonCopyableArguments) { Matcher> matcher = ResultOf( [](const std::unique_ptr& str_len) { return std::string(static_cast(*str_len), 'x'); }, "xxx"); EXPECT_TRUE(matcher.Matches(std::unique_ptr(new int(3)))); EXPECT_FALSE(matcher.Matches(std::unique_ptr(new int(1)))); } const int* ReferencingFunction(const int& n) { return &n; } struct ReferencingFunctor { typedef const int* result_type; result_type operator()(const int& n) { return &n; } }; TEST(ResultOfTest, WorksForReferencingCallables) { const int n = 1; const int n2 = 1; Matcher matcher2 = ResultOf(ReferencingFunction, Eq(&n)); EXPECT_TRUE(matcher2.Matches(n)); EXPECT_FALSE(matcher2.Matches(n2)); Matcher matcher3 = ResultOf(ReferencingFunctor(), Eq(&n)); EXPECT_TRUE(matcher3.Matches(n)); EXPECT_FALSE(matcher3.Matches(n2)); } TEST(SizeIsTest, ImplementsSizeIs) { vector container; EXPECT_THAT(container, SizeIs(0)); EXPECT_THAT(container, Not(SizeIs(1))); container.push_back(0); EXPECT_THAT(container, Not(SizeIs(0))); EXPECT_THAT(container, SizeIs(1)); container.push_back(0); EXPECT_THAT(container, Not(SizeIs(0))); EXPECT_THAT(container, SizeIs(2)); } TEST(SizeIsTest, WorksWithMap) { map container; EXPECT_THAT(container, SizeIs(0)); EXPECT_THAT(container, Not(SizeIs(1))); container.insert(make_pair("foo", 1)); EXPECT_THAT(container, Not(SizeIs(0))); EXPECT_THAT(container, SizeIs(1)); container.insert(make_pair("bar", 2)); EXPECT_THAT(container, Not(SizeIs(0))); EXPECT_THAT(container, SizeIs(2)); } TEST(SizeIsTest, WorksWithReferences) { vector container; Matcher&> m = SizeIs(1); EXPECT_THAT(container, Not(m)); container.push_back(0); EXPECT_THAT(container, m); } TEST(SizeIsTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(SizeIs(3))); helper.Call(MakeUniquePtrs({1, 2, 3})); } // SizeIs should work for any type that provides a size() member function. // For example, a size_type member type should not need to be provided. struct MinimalistCustomType { int size() const { return 1; } }; TEST(SizeIsTest, WorksWithMinimalistCustomType) { MinimalistCustomType container; EXPECT_THAT(container, SizeIs(1)); EXPECT_THAT(container, Not(SizeIs(0))); } TEST(SizeIsTest, CanDescribeSelf) { Matcher> m = SizeIs(2); EXPECT_EQ("has a size that is equal to 2", Describe(m)); EXPECT_EQ("has a size that isn't equal to 2", DescribeNegation(m)); } TEST(SizeIsTest, ExplainsResult) { Matcher> m1 = SizeIs(2); Matcher> m2 = SizeIs(Lt(2u)); Matcher> m3 = SizeIs(AnyOf(0, 3)); Matcher> m4 = SizeIs(Gt(1u)); vector container; EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container)); EXPECT_EQ("whose size 0 matches", Explain(m2, container)); EXPECT_EQ("whose size 0 matches", Explain(m3, container)); EXPECT_EQ("whose size 0 doesn't match", Explain(m4, container)); container.push_back(0); container.push_back(0); EXPECT_EQ("whose size 2 matches", Explain(m1, container)); EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container)); EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container)); EXPECT_EQ("whose size 2 matches", Explain(m4, container)); } TEST(WhenSortedByTest, WorksForEmptyContainer) { const vector numbers; EXPECT_THAT(numbers, WhenSortedBy(less(), ElementsAre())); EXPECT_THAT(numbers, Not(WhenSortedBy(less(), ElementsAre(1)))); } TEST(WhenSortedByTest, WorksForNonEmptyContainer) { vector numbers; numbers.push_back(3); numbers.push_back(1); numbers.push_back(2); numbers.push_back(2); EXPECT_THAT(numbers, WhenSortedBy(greater(), ElementsAre(3, 2, 2, 1))); EXPECT_THAT(numbers, Not(WhenSortedBy(greater(), ElementsAre(1, 2, 2, 3)))); } TEST(WhenSortedByTest, WorksForNonVectorContainer) { list words; words.push_back("say"); words.push_back("hello"); words.push_back("world"); EXPECT_THAT(words, WhenSortedBy(less(), ElementsAre("hello", "say", "world"))); EXPECT_THAT(words, Not(WhenSortedBy(less(), ElementsAre("say", "hello", "world")))); } TEST(WhenSortedByTest, WorksForNativeArray) { const int numbers[] = {1, 3, 2, 4}; const int sorted_numbers[] = {1, 2, 3, 4}; EXPECT_THAT(numbers, WhenSortedBy(less(), ElementsAre(1, 2, 3, 4))); EXPECT_THAT(numbers, WhenSortedBy(less(), ElementsAreArray(sorted_numbers))); EXPECT_THAT(numbers, Not(WhenSortedBy(less(), ElementsAre(1, 3, 2, 4)))); } TEST(WhenSortedByTest, CanDescribeSelf) { const Matcher> m = WhenSortedBy(less(), ElementsAre(1, 2)); EXPECT_EQ( "(when sorted) has 2 elements where\n" "element #0 is equal to 1,\n" "element #1 is equal to 2", Describe(m)); EXPECT_EQ( "(when sorted) doesn't have 2 elements, or\n" "element #0 isn't equal to 1, or\n" "element #1 isn't equal to 2", DescribeNegation(m)); } TEST(WhenSortedByTest, ExplainsMatchResult) { const int a[] = {2, 1}; EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match", Explain(WhenSortedBy(less(), ElementsAre(2, 3)), a)); EXPECT_EQ("which is { 1, 2 } when sorted", Explain(WhenSortedBy(less(), ElementsAre(1, 2)), a)); } // WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't // need to test it as exhaustively as we test the latter. TEST(WhenSortedTest, WorksForEmptyContainer) { const vector numbers; EXPECT_THAT(numbers, WhenSorted(ElementsAre())); EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1)))); } TEST(WhenSortedTest, WorksForNonEmptyContainer) { list words; words.push_back("3"); words.push_back("1"); words.push_back("2"); words.push_back("2"); EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3"))); EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2")))); } TEST(WhenSortedTest, WorksForMapTypes) { map word_counts; word_counts["and"] = 1; word_counts["the"] = 1; word_counts["buffalo"] = 2; EXPECT_THAT(word_counts, WhenSorted(ElementsAre(Pair("and", 1), Pair("buffalo", 2), Pair("the", 1)))); EXPECT_THAT(word_counts, Not(WhenSorted(ElementsAre(Pair("and", 1), Pair("the", 1), Pair("buffalo", 2))))); } TEST(WhenSortedTest, WorksForMultiMapTypes) { multimap ifib; ifib.insert(make_pair(8, 6)); ifib.insert(make_pair(2, 3)); ifib.insert(make_pair(1, 1)); ifib.insert(make_pair(3, 4)); ifib.insert(make_pair(1, 2)); ifib.insert(make_pair(5, 5)); EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1), Pair(1, 2), Pair(2, 3), Pair(3, 4), Pair(5, 5), Pair(8, 6)))); EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6), Pair(2, 3), Pair(1, 1), Pair(3, 4), Pair(1, 2), Pair(5, 5))))); } TEST(WhenSortedTest, WorksForPolymorphicMatcher) { std::deque d; d.push_back(2); d.push_back(1); EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2))); EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1)))); } TEST(WhenSortedTest, WorksForVectorConstRefMatcher) { std::deque d; d.push_back(2); d.push_back(1); Matcher&> vector_match = ElementsAre(1, 2); EXPECT_THAT(d, WhenSorted(vector_match)); Matcher&> not_vector_match = ElementsAre(2, 1); EXPECT_THAT(d, Not(WhenSorted(not_vector_match))); } // Deliberately bare pseudo-container. // Offers only begin() and end() accessors, yielding InputIterator. template class Streamlike { private: class ConstIter; public: typedef ConstIter const_iterator; typedef T value_type; template Streamlike(InIter first, InIter last) : remainder_(first, last) {} const_iterator begin() const { return const_iterator(this, remainder_.begin()); } const_iterator end() const { return const_iterator(this, remainder_.end()); } private: class ConstIter { public: using iterator_category = std::input_iterator_tag; using value_type = T; using difference_type = ptrdiff_t; using pointer = const value_type*; using reference = const value_type&; ConstIter(const Streamlike* s, typename std::list::iterator pos) : s_(s), pos_(pos) {} const value_type& operator*() const { return *pos_; } const value_type* operator->() const { return &*pos_; } ConstIter& operator++() { s_->remainder_.erase(pos_++); return *this; } // *iter++ is required to work (see std::istreambuf_iterator). // (void)iter++ is also required to work. class PostIncrProxy { public: explicit PostIncrProxy(const value_type& value) : value_(value) {} value_type operator*() const { return value_; } private: value_type value_; }; PostIncrProxy operator++(int) { PostIncrProxy proxy(**this); ++(*this); return proxy; } friend bool operator==(const ConstIter& a, const ConstIter& b) { return a.s_ == b.s_ && a.pos_ == b.pos_; } friend bool operator!=(const ConstIter& a, const ConstIter& b) { return !(a == b); } private: const Streamlike* s_; typename std::list::iterator pos_; }; friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) { os << "["; typedef typename std::list::const_iterator Iter; const char* sep = ""; for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) { os << sep << *it; sep = ","; } os << "]"; return os; } mutable std::list remainder_; // modified by iteration }; TEST(StreamlikeTest, Iteration) { const int a[5] = {2, 1, 4, 5, 3}; Streamlike s(a, a + 5); Streamlike::const_iterator it = s.begin(); const int* ip = a; while (it != s.end()) { SCOPED_TRACE(ip - a); EXPECT_EQ(*ip++, *it++); } } INSTANTIATE_GTEST_MATCHER_TEST_P(BeginEndDistanceIsTest); TEST(BeginEndDistanceIsTest, WorksWithForwardList) { std::forward_list container; EXPECT_THAT(container, BeginEndDistanceIs(0)); EXPECT_THAT(container, Not(BeginEndDistanceIs(1))); container.push_front(0); EXPECT_THAT(container, Not(BeginEndDistanceIs(0))); EXPECT_THAT(container, BeginEndDistanceIs(1)); container.push_front(0); EXPECT_THAT(container, Not(BeginEndDistanceIs(0))); EXPECT_THAT(container, BeginEndDistanceIs(2)); } TEST(BeginEndDistanceIsTest, WorksWithNonStdList) { const int a[5] = {1, 2, 3, 4, 5}; Streamlike s(a, a + 5); EXPECT_THAT(s, BeginEndDistanceIs(5)); } TEST(BeginEndDistanceIsTest, CanDescribeSelf) { Matcher> m = BeginEndDistanceIs(2); EXPECT_EQ("distance between begin() and end() is equal to 2", Describe(m)); EXPECT_EQ("distance between begin() and end() isn't equal to 2", DescribeNegation(m)); } TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(BeginEndDistanceIs(2))); helper.Call(MakeUniquePtrs({1, 2})); } TEST_P(BeginEndDistanceIsTestP, ExplainsResult) { Matcher> m1 = BeginEndDistanceIs(2); Matcher> m2 = BeginEndDistanceIs(Lt(2)); Matcher> m3 = BeginEndDistanceIs(AnyOf(0, 3)); Matcher> m4 = BeginEndDistanceIs(GreaterThan(1)); vector container; EXPECT_EQ("whose distance between begin() and end() 0 doesn't match", Explain(m1, container)); EXPECT_EQ("whose distance between begin() and end() 0 matches", Explain(m2, container)); EXPECT_EQ("whose distance between begin() and end() 0 matches", Explain(m3, container)); EXPECT_EQ( "whose distance between begin() and end() 0 doesn't match, which is 1 " "less than 1", Explain(m4, container)); container.push_back(0); container.push_back(0); EXPECT_EQ("whose distance between begin() and end() 2 matches", Explain(m1, container)); EXPECT_EQ("whose distance between begin() and end() 2 doesn't match", Explain(m2, container)); EXPECT_EQ("whose distance between begin() and end() 2 doesn't match", Explain(m3, container)); EXPECT_EQ( "whose distance between begin() and end() 2 matches, which is 1 more " "than 1", Explain(m4, container)); } TEST(WhenSortedTest, WorksForStreamlike) { // Streamlike 'container' provides only minimal iterator support. // Its iterators are tagged with input_iterator_tag. const int a[5] = {2, 1, 4, 5, 3}; Streamlike s(std::begin(a), std::end(a)); EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5))); EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3)))); } TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) { const int a[] = {2, 1, 4, 5, 3}; Streamlike s(std::begin(a), std::end(a)); Matcher&> vector_match = ElementsAre(1, 2, 3, 4, 5); EXPECT_THAT(s, WhenSorted(vector_match)); EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3)))); } TEST(IsSupersetOfTest, WorksForNativeArray) { const int subset[] = {1, 4}; const int superset[] = {1, 2, 4}; const int disjoint[] = {1, 0, 3}; EXPECT_THAT(subset, IsSupersetOf(subset)); EXPECT_THAT(subset, Not(IsSupersetOf(superset))); EXPECT_THAT(superset, IsSupersetOf(subset)); EXPECT_THAT(subset, Not(IsSupersetOf(disjoint))); EXPECT_THAT(disjoint, Not(IsSupersetOf(subset))); } TEST(IsSupersetOfTest, WorksWithDuplicates) { const int not_enough[] = {1, 2}; const int enough[] = {1, 1, 2}; const int expected[] = {1, 1}; EXPECT_THAT(not_enough, Not(IsSupersetOf(expected))); EXPECT_THAT(enough, IsSupersetOf(expected)); } TEST(IsSupersetOfTest, WorksForEmpty) { vector numbers; vector expected; EXPECT_THAT(numbers, IsSupersetOf(expected)); expected.push_back(1); EXPECT_THAT(numbers, Not(IsSupersetOf(expected))); expected.clear(); numbers.push_back(1); numbers.push_back(2); EXPECT_THAT(numbers, IsSupersetOf(expected)); expected.push_back(1); EXPECT_THAT(numbers, IsSupersetOf(expected)); expected.push_back(2); EXPECT_THAT(numbers, IsSupersetOf(expected)); expected.push_back(3); EXPECT_THAT(numbers, Not(IsSupersetOf(expected))); } TEST(IsSupersetOfTest, WorksForStreamlike) { const int a[5] = {1, 2, 3, 4, 5}; Streamlike s(std::begin(a), std::end(a)); vector expected; expected.push_back(1); expected.push_back(2); expected.push_back(5); EXPECT_THAT(s, IsSupersetOf(expected)); expected.push_back(0); EXPECT_THAT(s, Not(IsSupersetOf(expected))); } TEST(IsSupersetOfTest, TakesStlContainer) { const int actual[] = {3, 1, 2}; ::std::list expected; expected.push_back(1); expected.push_back(3); EXPECT_THAT(actual, IsSupersetOf(expected)); expected.push_back(4); EXPECT_THAT(actual, Not(IsSupersetOf(expected))); } TEST(IsSupersetOfTest, Describe) { typedef std::vector IntVec; IntVec expected; expected.push_back(111); expected.push_back(222); expected.push_back(333); EXPECT_THAT( Describe(IsSupersetOf(expected)), Eq("a surjection from elements to requirements exists such that:\n" " - an element is equal to 111\n" " - an element is equal to 222\n" " - an element is equal to 333")); } TEST(IsSupersetOfTest, DescribeNegation) { typedef std::vector IntVec; IntVec expected; expected.push_back(111); expected.push_back(222); expected.push_back(333); EXPECT_THAT( DescribeNegation(IsSupersetOf(expected)), Eq("no surjection from elements to requirements exists such that:\n" " - an element is equal to 111\n" " - an element is equal to 222\n" " - an element is equal to 333")); } TEST(IsSupersetOfTest, MatchAndExplain) { std::vector v; v.push_back(2); v.push_back(3); std::vector expected; expected.push_back(1); expected.push_back(2); StringMatchResultListener listener; ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected), v, &listener)) << listener.str(); EXPECT_THAT(listener.str(), Eq("where the following matchers don't match any elements:\n" "matcher #0: is equal to 1")); v.push_back(1); listener.Clear(); ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected), v, &listener)) << listener.str(); EXPECT_THAT(listener.str(), Eq("where:\n" " - element #0 is matched by matcher #1,\n" " - element #2 is matched by matcher #0")); } TEST(IsSupersetOfTest, WorksForRhsInitializerList) { const int numbers[] = {1, 3, 6, 2, 4, 5}; EXPECT_THAT(numbers, IsSupersetOf({1, 2})); EXPECT_THAT(numbers, Not(IsSupersetOf({3, 0}))); } TEST(IsSupersetOfTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)}))); helper.Call(MakeUniquePtrs({1, 2})); EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)})))); helper.Call(MakeUniquePtrs({2})); } TEST(IsSubsetOfTest, WorksForNativeArray) { const int subset[] = {1, 4}; const int superset[] = {1, 2, 4}; const int disjoint[] = {1, 0, 3}; EXPECT_THAT(subset, IsSubsetOf(subset)); EXPECT_THAT(subset, IsSubsetOf(superset)); EXPECT_THAT(superset, Not(IsSubsetOf(subset))); EXPECT_THAT(subset, Not(IsSubsetOf(disjoint))); EXPECT_THAT(disjoint, Not(IsSubsetOf(subset))); } TEST(IsSubsetOfTest, WorksWithDuplicates) { const int not_enough[] = {1, 2}; const int enough[] = {1, 1, 2}; const int actual[] = {1, 1}; EXPECT_THAT(actual, Not(IsSubsetOf(not_enough))); EXPECT_THAT(actual, IsSubsetOf(enough)); } TEST(IsSubsetOfTest, WorksForEmpty) { vector numbers; vector expected; EXPECT_THAT(numbers, IsSubsetOf(expected)); expected.push_back(1); EXPECT_THAT(numbers, IsSubsetOf(expected)); expected.clear(); numbers.push_back(1); numbers.push_back(2); EXPECT_THAT(numbers, Not(IsSubsetOf(expected))); expected.push_back(1); EXPECT_THAT(numbers, Not(IsSubsetOf(expected))); expected.push_back(2); EXPECT_THAT(numbers, IsSubsetOf(expected)); expected.push_back(3); EXPECT_THAT(numbers, IsSubsetOf(expected)); } TEST(IsSubsetOfTest, WorksForStreamlike) { const int a[5] = {1, 2}; Streamlike s(std::begin(a), std::end(a)); vector expected; expected.push_back(1); EXPECT_THAT(s, Not(IsSubsetOf(expected))); expected.push_back(2); expected.push_back(5); EXPECT_THAT(s, IsSubsetOf(expected)); } TEST(IsSubsetOfTest, TakesStlContainer) { const int actual[] = {3, 1, 2}; ::std::list expected; expected.push_back(1); expected.push_back(3); EXPECT_THAT(actual, Not(IsSubsetOf(expected))); expected.push_back(2); expected.push_back(4); EXPECT_THAT(actual, IsSubsetOf(expected)); } TEST(IsSubsetOfTest, Describe) { typedef std::vector IntVec; IntVec expected; expected.push_back(111); expected.push_back(222); expected.push_back(333); EXPECT_THAT( Describe(IsSubsetOf(expected)), Eq("an injection from elements to requirements exists such that:\n" " - an element is equal to 111\n" " - an element is equal to 222\n" " - an element is equal to 333")); } TEST(IsSubsetOfTest, DescribeNegation) { typedef std::vector IntVec; IntVec expected; expected.push_back(111); expected.push_back(222); expected.push_back(333); EXPECT_THAT( DescribeNegation(IsSubsetOf(expected)), Eq("no injection from elements to requirements exists such that:\n" " - an element is equal to 111\n" " - an element is equal to 222\n" " - an element is equal to 333")); } TEST(IsSubsetOfTest, MatchAndExplain) { std::vector v; v.push_back(2); v.push_back(3); std::vector expected; expected.push_back(1); expected.push_back(2); StringMatchResultListener listener; ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected), v, &listener)) << listener.str(); EXPECT_THAT(listener.str(), Eq("where the following elements don't match any matchers:\n" "element #1: 3")); expected.push_back(3); listener.Clear(); ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected), v, &listener)) << listener.str(); EXPECT_THAT(listener.str(), Eq("where:\n" " - element #0 is matched by matcher #1,\n" " - element #1 is matched by matcher #2")); } TEST(IsSubsetOfTest, WorksForRhsInitializerList) { const int numbers[] = {1, 2, 3}; EXPECT_THAT(numbers, IsSubsetOf({1, 2, 3, 4})); EXPECT_THAT(numbers, Not(IsSubsetOf({1, 2}))); } TEST(IsSubsetOfTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)}))); helper.Call(MakeUniquePtrs({1})); EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)})))); helper.Call(MakeUniquePtrs({2})); } // Tests using ElementsAre() and ElementsAreArray() with stream-like // "containers". TEST(ElemensAreStreamTest, WorksForStreamlike) { const int a[5] = {1, 2, 3, 4, 5}; Streamlike s(std::begin(a), std::end(a)); EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5)); EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3))); } TEST(ElemensAreArrayStreamTest, WorksForStreamlike) { const int a[5] = {1, 2, 3, 4, 5}; Streamlike s(std::begin(a), std::end(a)); vector expected; expected.push_back(1); expected.push_back(2); expected.push_back(3); expected.push_back(4); expected.push_back(5); EXPECT_THAT(s, ElementsAreArray(expected)); expected[3] = 0; EXPECT_THAT(s, Not(ElementsAreArray(expected))); } TEST(ElementsAreTest, WorksWithUncopyable) { Uncopyable objs[2]; objs[0].set_value(-3); objs[1].set_value(1); EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive))); } TEST(ElementsAreTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2)))); helper.Call(MakeUniquePtrs({1, 2})); EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)}))); helper.Call(MakeUniquePtrs({3, 4})); } TEST(ElementsAreTest, TakesStlContainer) { const int actual[] = {3, 1, 2}; ::std::list expected; expected.push_back(3); expected.push_back(1); expected.push_back(2); EXPECT_THAT(actual, ElementsAreArray(expected)); expected.push_back(4); EXPECT_THAT(actual, Not(ElementsAreArray(expected))); } // Tests for UnorderedElementsAreArray() TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) { const int a[] = {0, 1, 2, 3, 4}; std::vector s(std::begin(a), std::end(a)); do { StringMatchResultListener listener; EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a), s, &listener)) << listener.str(); } while (std::next_permutation(s.begin(), s.end())); } TEST(UnorderedElementsAreArrayTest, VectorBool) { const bool a[] = {false, true, false, true, true}; const bool b[] = {true, false, true, true, false}; std::vector expected(std::begin(a), std::end(a)); std::vector actual(std::begin(b), std::end(b)); StringMatchResultListener listener; EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected), actual, &listener)) << listener.str(); } TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) { // Streamlike 'container' provides only minimal iterator support. // Its iterators are tagged with input_iterator_tag, and it has no // size() or empty() methods. const int a[5] = {2, 1, 4, 5, 3}; Streamlike s(std::begin(a), std::end(a)); ::std::vector expected; expected.push_back(1); expected.push_back(2); expected.push_back(3); expected.push_back(4); expected.push_back(5); EXPECT_THAT(s, UnorderedElementsAreArray(expected)); expected.push_back(6); EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected))); } TEST(UnorderedElementsAreArrayTest, TakesStlContainer) { const int actual[] = {3, 1, 2}; ::std::list expected; expected.push_back(1); expected.push_back(2); expected.push_back(3); EXPECT_THAT(actual, UnorderedElementsAreArray(expected)); expected.push_back(4); EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected))); } TEST(UnorderedElementsAreArrayTest, TakesInitializerList) { const int a[5] = {2, 1, 4, 5, 3}; EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5})); EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6}))); } TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) { const std::string a[5] = {"a", "b", "c", "d", "e"}; EXPECT_THAT(a, UnorderedElementsAreArray({"a", "b", "c", "d", "e"})); EXPECT_THAT(a, Not(UnorderedElementsAreArray({"a", "b", "c", "d", "ef"}))); } TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) { const int a[5] = {2, 1, 4, 5, 3}; EXPECT_THAT(a, UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)})); EXPECT_THAT( a, Not(UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)}))); } TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) { const int a[5] = {2, 1, 4, 5, 3}; // The compiler cannot infer the type of the initializer list if its // elements have different types. We must explicitly specify the // unified element type in this case. EXPECT_THAT(a, UnorderedElementsAreArray>( {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)})); EXPECT_THAT(a, Not(UnorderedElementsAreArray>( {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)}))); } TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)}))); helper.Call(MakeUniquePtrs({2, 1})); } class UnorderedElementsAreTest : public testing::Test { protected: typedef std::vector IntVec; }; TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) { Uncopyable objs[2]; objs[0].set_value(-3); objs[1].set_value(1); EXPECT_THAT(objs, UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3))); } TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) { const int a[] = {1, 2, 3}; std::vector s(std::begin(a), std::end(a)); do { StringMatchResultListener listener; EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), s, &listener)) << listener.str(); } while (std::next_permutation(s.begin(), s.end())); } TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) { const int a[] = {1, 2, 3}; std::vector s(std::begin(a), std::end(a)); std::vector> mv; mv.push_back(1); mv.push_back(2); mv.push_back(2); // The element with value '3' matches nothing: fail fast. StringMatchResultListener listener; EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener)) << listener.str(); } TEST_F(UnorderedElementsAreTest, WorksForStreamlike) { // Streamlike 'container' provides only minimal iterator support. // Its iterators are tagged with input_iterator_tag, and it has no // size() or empty() methods. const int a[5] = {2, 1, 4, 5, 3}; Streamlike s(std::begin(a), std::end(a)); EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5)); EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5))); } TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2)))); helper.Call(MakeUniquePtrs({2, 1})); } // One naive implementation of the matcher runs in O(N!) time, which is too // slow for many real-world inputs. This test shows that our matcher can match // 100 inputs very quickly (a few milliseconds). An O(100!) is 10^158 // iterations and obviously effectively incomputable. // [ RUN ] UnorderedElementsAreTest.Performance // [ OK ] UnorderedElementsAreTest.Performance (4 ms) TEST_F(UnorderedElementsAreTest, Performance) { std::vector s; std::vector> mv; for (int i = 0; i < 100; ++i) { s.push_back(i); mv.push_back(_); } mv[50] = Eq(0); StringMatchResultListener listener; EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener)) << listener.str(); } // Another variant of 'Performance' with similar expectations. // [ RUN ] UnorderedElementsAreTest.PerformanceHalfStrict // [ OK ] UnorderedElementsAreTest.PerformanceHalfStrict (4 ms) TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) { std::vector s; std::vector> mv; for (int i = 0; i < 100; ++i) { s.push_back(i); if (i & 1) { mv.push_back(_); } else { mv.push_back(i); } } StringMatchResultListener listener; EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener)) << listener.str(); } TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) { std::vector v; v.push_back(4); StringMatchResultListener listener; EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener)) << listener.str(); EXPECT_THAT(listener.str(), Eq("which has 1 element\n" "where the following matchers don't match any elements:\n" "matcher #0: is equal to 1,\n" "matcher #1: is equal to 2,\n" "matcher #2: is equal to 3\n" "and where the following elements don't match any matchers:\n" "element #0: 4")); } TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) { std::vector v; StringMatchResultListener listener; EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener)) << listener.str(); EXPECT_THAT(listener.str(), Eq("where the following matchers don't match any elements:\n" "matcher #0: is equal to 1,\n" "matcher #1: is equal to 2,\n" "matcher #2: is equal to 3")); } TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) { std::vector v; v.push_back(1); v.push_back(1); StringMatchResultListener listener; EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener)) << listener.str(); EXPECT_THAT(listener.str(), Eq("where the following matchers don't match any elements:\n" "matcher #1: is equal to 2")); } TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) { std::vector v; v.push_back(1); v.push_back(2); StringMatchResultListener listener; EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1), v, &listener)) << listener.str(); EXPECT_THAT(listener.str(), Eq("where the following elements don't match any matchers:\n" "element #1: 2")); } TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) { std::vector v; v.push_back(2); v.push_back(3); StringMatchResultListener listener; EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener)) << listener.str(); EXPECT_THAT(listener.str(), Eq("where" " the following matchers don't match any elements:\n" "matcher #0: is equal to 1\n" "and" " where" " the following elements don't match any matchers:\n" "element #1: 3")); } // Test helper for formatting element, matcher index pairs in expectations. static std::string EMString(int element, int matcher) { stringstream ss; ss << "(element #" << element << ", matcher #" << matcher << ")"; return ss.str(); } TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) { // A situation where all elements and matchers have a match // associated with them, but the max matching is not perfect. std::vector v; v.push_back("a"); v.push_back("b"); v.push_back("c"); StringMatchResultListener listener; EXPECT_FALSE(ExplainMatchResult( UnorderedElementsAre("a", "a", AnyOf("b", "c")), v, &listener)) << listener.str(); std::string prefix = "where no permutation of the elements can satisfy all matchers, " "and the closest match is 2 of 3 matchers with the " "pairings:\n"; // We have to be a bit loose here, because there are 4 valid max matches. EXPECT_THAT( listener.str(), AnyOf( prefix + "{\n " + EMString(0, 0) + ",\n " + EMString(1, 2) + "\n}", prefix + "{\n " + EMString(0, 1) + ",\n " + EMString(1, 2) + "\n}", prefix + "{\n " + EMString(0, 0) + ",\n " + EMString(2, 2) + "\n}", prefix + "{\n " + EMString(0, 1) + ",\n " + EMString(2, 2) + "\n}")); } TEST_F(UnorderedElementsAreTest, Describe) { EXPECT_THAT(Describe(UnorderedElementsAre()), Eq("is empty")); EXPECT_THAT(Describe(UnorderedElementsAre(345)), Eq("has 1 element and that element is equal to 345")); EXPECT_THAT(Describe(UnorderedElementsAre(111, 222, 333)), Eq("has 3 elements and there exists some permutation " "of elements such that:\n" " - element #0 is equal to 111, and\n" " - element #1 is equal to 222, and\n" " - element #2 is equal to 333")); } TEST_F(UnorderedElementsAreTest, DescribeNegation) { EXPECT_THAT(DescribeNegation(UnorderedElementsAre()), Eq("isn't empty")); EXPECT_THAT( DescribeNegation(UnorderedElementsAre(345)), Eq("doesn't have 1 element, or has 1 element that isn't equal to 345")); EXPECT_THAT(DescribeNegation(UnorderedElementsAre(123, 234, 345)), Eq("doesn't have 3 elements, or there exists no permutation " "of elements such that:\n" " - element #0 is equal to 123, and\n" " - element #1 is equal to 234, and\n" " - element #2 is equal to 345")); } // Tests Each(). INSTANTIATE_GTEST_MATCHER_TEST_P(EachTest); TEST_P(EachTestP, ExplainsMatchResultCorrectly) { set a; // empty Matcher> m = Each(2); EXPECT_EQ("", Explain(m, a)); Matcher n = Each(1); // NOLINT const int b[1] = {1}; EXPECT_EQ("", Explain(n, b)); n = Each(3); EXPECT_EQ("whose element #0 doesn't match", Explain(n, b)); a.insert(1); a.insert(2); a.insert(3); m = Each(GreaterThan(0)); EXPECT_EQ("", Explain(m, a)); m = Each(GreaterThan(10)); EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10", Explain(m, a)); } TEST(EachTest, DescribesItselfCorrectly) { Matcher> m = Each(1); EXPECT_EQ("only contains elements that is equal to 1", Describe(m)); Matcher> m2 = Not(m); EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2)); } TEST(EachTest, MatchesVectorWhenAllElementsMatch) { vector some_vector; EXPECT_THAT(some_vector, Each(1)); some_vector.push_back(3); EXPECT_THAT(some_vector, Not(Each(1))); EXPECT_THAT(some_vector, Each(3)); some_vector.push_back(1); some_vector.push_back(2); EXPECT_THAT(some_vector, Not(Each(3))); EXPECT_THAT(some_vector, Each(Lt(3.5))); vector another_vector; another_vector.push_back("fee"); EXPECT_THAT(another_vector, Each(std::string("fee"))); another_vector.push_back("fie"); another_vector.push_back("foe"); another_vector.push_back("fum"); EXPECT_THAT(another_vector, Not(Each(std::string("fee")))); } TEST(EachTest, MatchesMapWhenAllElementsMatch) { map my_map; const char* bar = "a string"; my_map[bar] = 2; EXPECT_THAT(my_map, Each(make_pair(bar, 2))); map another_map; EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1))); another_map["fee"] = 1; EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1))); another_map["fie"] = 2; another_map["foe"] = 3; another_map["fum"] = 4; EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fee"), 1)))); EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fum"), 1)))); EXPECT_THAT(another_map, Each(Pair(_, Gt(0)))); } TEST(EachTest, AcceptsMatcher) { const int a[] = {1, 2, 3}; EXPECT_THAT(a, Each(Gt(0))); EXPECT_THAT(a, Not(Each(Gt(1)))); } TEST(EachTest, WorksForNativeArrayAsTuple) { const int a[] = {1, 2}; const int* const pointer = a; EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0))); EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1)))); } TEST(EachTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(Each(Pointee(Gt(0))))); helper.Call(MakeUniquePtrs({1, 2})); } // For testing Pointwise(). class IsHalfOfMatcher { public: template bool MatchAndExplain(const std::tuple& a_pair, MatchResultListener* listener) const { if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) { *listener << "where the second is " << std::get<1>(a_pair); return true; } else { *listener << "where the second/2 is " << std::get<1>(a_pair) / 2; return false; } } void DescribeTo(ostream* os) const { *os << "are a pair where the first is half of the second"; } void DescribeNegationTo(ostream* os) const { *os << "are a pair where the first isn't half of the second"; } }; PolymorphicMatcher IsHalfOf() { return MakePolymorphicMatcher(IsHalfOfMatcher()); } TEST(PointwiseTest, DescribesSelf) { vector rhs; rhs.push_back(1); rhs.push_back(2); rhs.push_back(3); const Matcher&> m = Pointwise(IsHalfOf(), rhs); EXPECT_EQ( "contains 3 values, where each value and its corresponding value " "in { 1, 2, 3 } are a pair where the first is half of the second", Describe(m)); EXPECT_EQ( "doesn't contain exactly 3 values, or contains a value x at some " "index i where x and the i-th value of { 1, 2, 3 } are a pair " "where the first isn't half of the second", DescribeNegation(m)); } TEST(PointwiseTest, MakesCopyOfRhs) { list rhs; rhs.push_back(2); rhs.push_back(4); int lhs[] = {1, 2}; const Matcher m = Pointwise(IsHalfOf(), rhs); EXPECT_THAT(lhs, m); // Changing rhs now shouldn't affect m, which made a copy of rhs. rhs.push_back(6); EXPECT_THAT(lhs, m); } TEST(PointwiseTest, WorksForLhsNativeArray) { const int lhs[] = {1, 2, 3}; vector rhs; rhs.push_back(2); rhs.push_back(4); rhs.push_back(6); EXPECT_THAT(lhs, Pointwise(Lt(), rhs)); EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); } TEST(PointwiseTest, WorksForRhsNativeArray) { const int rhs[] = {1, 2, 3}; vector lhs; lhs.push_back(2); lhs.push_back(4); lhs.push_back(6); EXPECT_THAT(lhs, Pointwise(Gt(), rhs)); EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs))); } // Test is effective only with sanitizers. TEST(PointwiseTest, WorksForVectorOfBool) { vector rhs(3, false); rhs[1] = true; vector lhs = rhs; EXPECT_THAT(lhs, Pointwise(Eq(), rhs)); rhs[0] = true; EXPECT_THAT(lhs, Not(Pointwise(Eq(), rhs))); } TEST(PointwiseTest, WorksForRhsInitializerList) { const vector lhs{2, 4, 6}; EXPECT_THAT(lhs, Pointwise(Gt(), {1, 2, 3})); EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7}))); } TEST(PointwiseTest, RejectsWrongSize) { const double lhs[2] = {1, 2}; const int rhs[1] = {0}; EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); EXPECT_EQ("which contains 2 values", Explain(Pointwise(Gt(), rhs), lhs)); const int rhs2[3] = {0, 1, 2}; EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2))); } TEST(PointwiseTest, RejectsWrongContent) { const double lhs[3] = {1, 2, 3}; const int rhs[3] = {2, 6, 4}; EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs))); EXPECT_EQ( "where the value pair (2, 6) at index #1 don't match, " "where the second/2 is 3", Explain(Pointwise(IsHalfOf(), rhs), lhs)); } TEST(PointwiseTest, AcceptsCorrectContent) { const double lhs[3] = {1, 2, 3}; const int rhs[3] = {2, 4, 6}; EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs)); EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs)); } TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) { const double lhs[3] = {1, 2, 3}; const int rhs[3] = {2, 4, 6}; const Matcher> m1 = IsHalfOf(); EXPECT_THAT(lhs, Pointwise(m1, rhs)); EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs)); // This type works as a std::tuple can be // implicitly cast to std::tuple. const Matcher> m2 = IsHalfOf(); EXPECT_THAT(lhs, Pointwise(m2, rhs)); EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs)); } MATCHER(PointeeEquals, "Points to an equal value") { return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)), ::testing::get<0>(arg), result_listener); } TEST(PointwiseTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector{1, 2}))); helper.Call(MakeUniquePtrs({1, 2})); } TEST(UnorderedPointwiseTest, DescribesSelf) { vector rhs; rhs.push_back(1); rhs.push_back(2); rhs.push_back(3); const Matcher&> m = UnorderedPointwise(IsHalfOf(), rhs); EXPECT_EQ( "has 3 elements and there exists some permutation of elements such " "that:\n" " - element #0 and 1 are a pair where the first is half of the second, " "and\n" " - element #1 and 2 are a pair where the first is half of the second, " "and\n" " - element #2 and 3 are a pair where the first is half of the second", Describe(m)); EXPECT_EQ( "doesn't have 3 elements, or there exists no permutation of elements " "such that:\n" " - element #0 and 1 are a pair where the first is half of the second, " "and\n" " - element #1 and 2 are a pair where the first is half of the second, " "and\n" " - element #2 and 3 are a pair where the first is half of the second", DescribeNegation(m)); } TEST(UnorderedPointwiseTest, MakesCopyOfRhs) { list rhs; rhs.push_back(2); rhs.push_back(4); int lhs[] = {2, 1}; const Matcher m = UnorderedPointwise(IsHalfOf(), rhs); EXPECT_THAT(lhs, m); // Changing rhs now shouldn't affect m, which made a copy of rhs. rhs.push_back(6); EXPECT_THAT(lhs, m); } TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) { const int lhs[] = {1, 2, 3}; vector rhs; rhs.push_back(4); rhs.push_back(6); rhs.push_back(2); EXPECT_THAT(lhs, UnorderedPointwise(Lt(), rhs)); EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs))); } TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) { const int rhs[] = {1, 2, 3}; vector lhs; lhs.push_back(4); lhs.push_back(2); lhs.push_back(6); EXPECT_THAT(lhs, UnorderedPointwise(Gt(), rhs)); EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs))); } TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) { const vector lhs{2, 4, 6}; EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3})); EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7}))); } TEST(UnorderedPointwiseTest, RejectsWrongSize) { const double lhs[2] = {1, 2}; const int rhs[1] = {0}; EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs))); EXPECT_EQ("which has 2 elements\n", Explain(UnorderedPointwise(Gt(), rhs), lhs)); const int rhs2[3] = {0, 1, 2}; EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2))); } TEST(UnorderedPointwiseTest, RejectsWrongContent) { const double lhs[3] = {1, 2, 3}; const int rhs[3] = {2, 6, 6}; EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs))); EXPECT_EQ( "where the following elements don't match any matchers:\n" "element #1: 2", Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs)); } TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) { const double lhs[3] = {1, 2, 3}; const int rhs[3] = {2, 4, 6}; EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs)); } TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) { const double lhs[3] = {1, 2, 3}; const int rhs[3] = {6, 4, 2}; EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs)); } TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) { const double lhs[3] = {1, 2, 3}; const int rhs[3] = {4, 6, 2}; const Matcher> m1 = IsHalfOf(); EXPECT_THAT(lhs, UnorderedPointwise(m1, rhs)); // This type works as a std::tuple can be // implicitly cast to std::tuple. const Matcher> m2 = IsHalfOf(); EXPECT_THAT(lhs, UnorderedPointwise(m2, rhs)); } TEST(UnorderedPointwiseTest, WorksWithMoveOnly) { ContainerHelper helper; EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(), std::vector{1, 2}))); helper.Call(MakeUniquePtrs({2, 1})); } TEST(PointeeTest, WorksOnMoveOnlyType) { std::unique_ptr p(new int(3)); EXPECT_THAT(p, Pointee(Eq(3))); EXPECT_THAT(p, Not(Pointee(Eq(2)))); } class PredicateFormatterFromMatcherTest : public ::testing::Test { protected: enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky }; // A matcher that can return different results when used multiple times on the // same input. No real matcher should do this; but this lets us test that we // detect such behavior and fail appropriately. class MockMatcher : public MatcherInterface { public: bool MatchAndExplain(Behavior behavior, MatchResultListener* listener) const override { *listener << "[MatchAndExplain]"; switch (behavior) { case kInitialSuccess: // The first call to MatchAndExplain should use a "not interested" // listener; so this is expected to return |true|. There should be no // subsequent calls. return !listener->IsInterested(); case kAlwaysFail: return false; case kFlaky: // The first call to MatchAndExplain should use a "not interested" // listener; so this will return |false|. Subsequent calls should have // an "interested" listener; so this will return |true|, thus // simulating a flaky matcher. return listener->IsInterested(); } GTEST_LOG_(FATAL) << "This should never be reached"; return false; } void DescribeTo(ostream* os) const override { *os << "[DescribeTo]"; } void DescribeNegationTo(ostream* os) const override { *os << "[DescribeNegationTo]"; } }; AssertionResult RunPredicateFormatter(Behavior behavior) { auto matcher = MakeMatcher(new MockMatcher); PredicateFormatterFromMatcher> predicate_formatter( matcher); return predicate_formatter("dummy-name", behavior); } }; TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) { AssertionResult result = RunPredicateFormatter(kInitialSuccess); EXPECT_TRUE(result); // Implicit cast to bool. std::string expect; EXPECT_EQ(expect, result.message()); } TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) { AssertionResult result = RunPredicateFormatter(kAlwaysFail); EXPECT_FALSE(result); // Implicit cast to bool. std::string expect = "Value of: dummy-name\nExpected: [DescribeTo]\n" " Actual: 1" + OfType(internal::GetTypeName()) + ", [MatchAndExplain]"; EXPECT_EQ(expect, result.message()); } TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) { AssertionResult result = RunPredicateFormatter(kFlaky); EXPECT_FALSE(result); // Implicit cast to bool. std::string expect = "Value of: dummy-name\nExpected: [DescribeTo]\n" " The matcher failed on the initial attempt; but passed when rerun to " "generate the explanation.\n" " Actual: 2" + OfType(internal::GetTypeName()) + ", [MatchAndExplain]"; EXPECT_EQ(expect, result.message()); } // Tests for ElementsAre(). TEST(ElementsAreTest, CanDescribeExpectingNoElement) { Matcher&> m = ElementsAre(); EXPECT_EQ("is empty", Describe(m)); } TEST(ElementsAreTest, CanDescribeExpectingOneElement) { Matcher> m = ElementsAre(Gt(5)); EXPECT_EQ("has 1 element that is > 5", Describe(m)); } TEST(ElementsAreTest, CanDescribeExpectingManyElements) { Matcher> m = ElementsAre(StrEq("one"), "two"); EXPECT_EQ( "has 2 elements where\n" "element #0 is equal to \"one\",\n" "element #1 is equal to \"two\"", Describe(m)); } TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { Matcher> m = ElementsAre(); EXPECT_EQ("isn't empty", DescribeNegation(m)); } TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElement) { Matcher&> m = ElementsAre(Gt(5)); EXPECT_EQ( "doesn't have 1 element, or\n" "element #0 isn't > 5", DescribeNegation(m)); } TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { Matcher&> m = ElementsAre("one", "two"); EXPECT_EQ( "doesn't have 2 elements, or\n" "element #0 isn't equal to \"one\", or\n" "element #1 isn't equal to \"two\"", DescribeNegation(m)); } TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { Matcher&> m = ElementsAre(1, Ne(2)); list test_list; test_list.push_back(1); test_list.push_back(3); EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything. } TEST_P(ElementsAreTestP, ExplainsNonTrivialMatch) { Matcher&> m = ElementsAre(GreaterThan(1), 0, GreaterThan(2)); const int a[] = {10, 0, 100}; vector test_vector(std::begin(a), std::end(a)); EXPECT_EQ( "whose element #0 matches, which is 9 more than 1,\n" "and whose element #2 matches, which is 98 more than 2", Explain(m, test_vector)); } TEST(ElementsAreTest, CanExplainMismatchWrongSize) { Matcher&> m = ElementsAre(1, 3); list test_list; // No need to explain when the container is empty. EXPECT_EQ("", Explain(m, test_list)); test_list.push_back(1); EXPECT_EQ("which has 1 element", Explain(m, test_list)); } TEST_P(ElementsAreTestP, CanExplainMismatchRightSize) { Matcher&> m = ElementsAre(1, GreaterThan(5)); vector v; v.push_back(2); v.push_back(1); EXPECT_EQ("whose element #0 doesn't match", Explain(m, v)); v[0] = 1; EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5", Explain(m, v)); } TEST(ElementsAreTest, MatchesOneElementVector) { vector test_vector; test_vector.push_back("test string"); EXPECT_THAT(test_vector, ElementsAre(StrEq("test string"))); } TEST(ElementsAreTest, MatchesOneElementList) { list test_list; test_list.push_back("test string"); EXPECT_THAT(test_list, ElementsAre("test string")); } TEST(ElementsAreTest, MatchesThreeElementVector) { vector test_vector; test_vector.push_back("one"); test_vector.push_back("two"); test_vector.push_back("three"); EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _)); } TEST(ElementsAreTest, MatchesOneElementEqMatcher) { vector test_vector; test_vector.push_back(4); EXPECT_THAT(test_vector, ElementsAre(Eq(4))); } TEST(ElementsAreTest, MatchesOneElementAnyMatcher) { vector test_vector; test_vector.push_back(4); EXPECT_THAT(test_vector, ElementsAre(_)); } TEST(ElementsAreTest, MatchesOneElementValue) { vector test_vector; test_vector.push_back(4); EXPECT_THAT(test_vector, ElementsAre(4)); } TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) { vector test_vector; test_vector.push_back(1); test_vector.push_back(2); test_vector.push_back(3); EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _)); } TEST(ElementsAreTest, MatchesTenElementVector) { const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; vector test_vector(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, // The element list can contain values and/or matchers // of different types. ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _)); } TEST(ElementsAreTest, DoesNotMatchWrongSize) { vector test_vector; test_vector.push_back("test string"); test_vector.push_back("test string"); Matcher> m = ElementsAre(StrEq("test string")); EXPECT_FALSE(m.Matches(test_vector)); } TEST(ElementsAreTest, DoesNotMatchWrongValue) { vector test_vector; test_vector.push_back("other string"); Matcher> m = ElementsAre(StrEq("test string")); EXPECT_FALSE(m.Matches(test_vector)); } TEST(ElementsAreTest, DoesNotMatchWrongOrder) { vector test_vector; test_vector.push_back("one"); test_vector.push_back("three"); test_vector.push_back("two"); Matcher> m = ElementsAre(StrEq("one"), StrEq("two"), StrEq("three")); EXPECT_FALSE(m.Matches(test_vector)); } TEST(ElementsAreTest, WorksForNestedContainer) { constexpr std::array strings = {{"Hi", "world"}}; vector> nested; for (const auto& s : strings) { nested.emplace_back(s, s + strlen(s)); } EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), ElementsAre('w', 'o', _, _, 'd'))); EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), ElementsAre('w', 'o', _, _, 'd')))); } TEST(ElementsAreTest, WorksWithByRefElementMatchers) { int a[] = {0, 1, 2}; vector v(std::begin(a), std::end(a)); EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2]))); EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2])))); } TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { int a[] = {0, 1, 2}; vector v(std::begin(a), std::end(a)); EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); } TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) { int array[] = {0, 1, 2}; EXPECT_THAT(array, ElementsAre(0, 1, _)); EXPECT_THAT(array, Not(ElementsAre(1, _, _))); EXPECT_THAT(array, Not(ElementsAre(0, _))); } class NativeArrayPassedAsPointerAndSize { public: NativeArrayPassedAsPointerAndSize() = default; MOCK_METHOD(void, Helper, (int* array, int size)); private: NativeArrayPassedAsPointerAndSize(const NativeArrayPassedAsPointerAndSize&) = delete; NativeArrayPassedAsPointerAndSize& operator=( const NativeArrayPassedAsPointerAndSize&) = delete; }; TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) { int array[] = {0, 1}; ::std::tuple array_as_tuple(array, 2); EXPECT_THAT(array_as_tuple, ElementsAre(0, 1)); EXPECT_THAT(array_as_tuple, Not(ElementsAre(0))); NativeArrayPassedAsPointerAndSize helper; EXPECT_CALL(helper, Helper(_, _)).With(ElementsAre(0, 1)); helper.Helper(array, 2); } TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) { const char a2[][3] = {"hi", "lo"}; EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'), ElementsAre('l', 'o', '\0'))); EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo"))); EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')), ElementsAre('l', 'o', '\0'))); } TEST(ElementsAreTest, AcceptsStringLiteral) { std::string array[] = {"hi", "one", "two"}; EXPECT_THAT(array, ElementsAre("hi", "one", "two")); EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too"))); } // Declared here with the size unknown. Defined AFTER the following test. extern const char kHi[]; TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) { // The size of kHi is not known in this test, but ElementsAre() should // still accept it. std::string array1[] = {"hi"}; EXPECT_THAT(array1, ElementsAre(kHi)); std::string array2[] = {"ho"}; EXPECT_THAT(array2, Not(ElementsAre(kHi))); } const char kHi[] = "hi"; TEST(ElementsAreTest, MakesCopyOfArguments) { int x = 1; int y = 2; // This should make a copy of x and y. ::testing::internal::ElementsAreMatcher> polymorphic_matcher = ElementsAre(x, y); // Changing x and y now shouldn't affect the meaning of the above matcher. x = y = 0; const int array1[] = {1, 2}; EXPECT_THAT(array1, polymorphic_matcher); const int array2[] = {0, 0}; EXPECT_THAT(array2, Not(polymorphic_matcher)); } // Tests for ElementsAreArray(). Since ElementsAreArray() shares most // of the implementation with ElementsAre(), we don't test it as // thoroughly here. TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { const int a[] = {1, 2, 3}; vector test_vector(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, ElementsAreArray(a)); test_vector[2] = 0; EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); } TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) { std::array a = {{"one", "two", "three"}}; vector test_vector(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size())); const char** p = a.data(); test_vector[0] = "1"; EXPECT_THAT(test_vector, Not(ElementsAreArray(p, a.size()))); } TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) { const char* a[] = {"one", "two", "three"}; vector test_vector(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, ElementsAreArray(a)); test_vector[0] = "1"; EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); } TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { const Matcher kMatcherArray[] = {StrEq("one"), StrEq("two"), StrEq("three")}; vector test_vector; test_vector.push_back("one"); test_vector.push_back("two"); test_vector.push_back("three"); EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); test_vector.push_back("three"); EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); } TEST(ElementsAreArrayTest, CanBeCreatedWithVector) { const int a[] = {1, 2, 3}; vector test_vector(std::begin(a), std::end(a)); const vector expected(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, ElementsAreArray(expected)); test_vector.push_back(4); EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); } TEST(ElementsAreArrayTest, TakesInitializerList) { const int a[5] = {1, 2, 3, 4, 5}; EXPECT_THAT(a, ElementsAreArray({1, 2, 3, 4, 5})); EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 5, 4}))); EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 4, 6}))); } TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) { const std::string a[5] = {"a", "b", "c", "d", "e"}; EXPECT_THAT(a, ElementsAreArray({"a", "b", "c", "d", "e"})); EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "e", "d"}))); EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "d", "ef"}))); } TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) { const int a[5] = {1, 2, 3, 4, 5}; EXPECT_THAT(a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)})); EXPECT_THAT(a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)}))); } TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) { const int a[5] = {1, 2, 3, 4, 5}; // The compiler cannot infer the type of the initializer list if its // elements have different types. We must explicitly specify the // unified element type in this case. EXPECT_THAT( a, ElementsAreArray>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)})); EXPECT_THAT(a, Not(ElementsAreArray>( {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)}))); } TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) { const int a[] = {1, 2, 3}; const Matcher kMatchers[] = {Eq(1), Eq(2), Eq(3)}; vector test_vector(std::begin(a), std::end(a)); const vector> expected(std::begin(kMatchers), std::end(kMatchers)); EXPECT_THAT(test_vector, ElementsAreArray(expected)); test_vector.push_back(4); EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); } TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) { const int a[] = {1, 2, 3}; const vector test_vector(std::begin(a), std::end(a)); const vector expected(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end())); // Pointers are iterators, too. EXPECT_THAT(test_vector, ElementsAreArray(std::begin(a), std::end(a))); // The empty range of NULL pointers should also be okay. int* const null_int = nullptr; EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int))); EXPECT_THAT((vector()), ElementsAreArray(null_int, null_int)); } // Since ElementsAre() and ElementsAreArray() share much of the // implementation, we only do a test for native arrays here. TEST(ElementsAreArrayTest, WorksWithNativeArray) { ::std::string a[] = {"hi", "ho"}; ::std::string b[] = {"hi", "ho"}; EXPECT_THAT(a, ElementsAreArray(b)); EXPECT_THAT(a, ElementsAreArray(b, 2)); EXPECT_THAT(a, Not(ElementsAreArray(b, 1))); } TEST(ElementsAreArrayTest, SourceLifeSpan) { const int a[] = {1, 2, 3}; vector test_vector(std::begin(a), std::end(a)); vector expect(std::begin(a), std::end(a)); ElementsAreArrayMatcher matcher_maker = ElementsAreArray(expect.begin(), expect.end()); EXPECT_THAT(test_vector, matcher_maker); // Changing in place the values that initialized matcher_maker should not // affect matcher_maker anymore. It should have made its own copy of them. for (int& i : expect) { i += 10; } EXPECT_THAT(test_vector, matcher_maker); test_vector.push_back(3); EXPECT_THAT(test_vector, Not(matcher_maker)); } // Tests Contains(). INSTANTIATE_GTEST_MATCHER_TEST_P(ContainsTest); TEST(ContainsTest, ListMatchesWhenElementIsInContainer) { list some_list; some_list.push_back(3); some_list.push_back(1); some_list.push_back(2); some_list.push_back(3); EXPECT_THAT(some_list, Contains(1)); EXPECT_THAT(some_list, Contains(Gt(2.5))); EXPECT_THAT(some_list, Contains(Eq(2.0f))); list another_list; another_list.push_back("fee"); another_list.push_back("fie"); another_list.push_back("foe"); another_list.push_back("fum"); EXPECT_THAT(another_list, Contains(std::string("fee"))); } TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) { list some_list; some_list.push_back(3); some_list.push_back(1); EXPECT_THAT(some_list, Not(Contains(4))); } TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { set some_set; some_set.insert(3); some_set.insert(1); some_set.insert(2); EXPECT_THAT(some_set, Contains(Eq(1.0))); EXPECT_THAT(some_set, Contains(Eq(3.0f))); EXPECT_THAT(some_set, Contains(2)); set another_set; another_set.insert("fee"); another_set.insert("fie"); another_set.insert("foe"); another_set.insert("fum"); EXPECT_THAT(another_set, Contains(Eq(std::string("fum")))); } TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { set some_set; some_set.insert(3); some_set.insert(1); EXPECT_THAT(some_set, Not(Contains(4))); set c_string_set; c_string_set.insert("hello"); EXPECT_THAT(c_string_set, Not(Contains(std::string("goodbye")))); } TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) { const int a[2] = {1, 2}; Matcher m = Contains(2); EXPECT_EQ("whose element #1 matches", Explain(m, a)); m = Contains(3); EXPECT_EQ("", Explain(m, a)); m = Contains(GreaterThan(0)); EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a)); m = Contains(GreaterThan(10)); EXPECT_EQ("", Explain(m, a)); } TEST(ContainsTest, DescribesItselfCorrectly) { Matcher> m = Contains(1); EXPECT_EQ("contains at least one element that is equal to 1", Describe(m)); Matcher> m2 = Not(m); EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2)); } TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { map my_map; const char* bar = "a string"; my_map[bar] = 2; EXPECT_THAT(my_map, Contains(pair(bar, 2))); map another_map; another_map["fee"] = 1; another_map["fie"] = 2; another_map["foe"] = 3; another_map["fum"] = 4; EXPECT_THAT(another_map, Contains(pair(std::string("fee"), 1))); EXPECT_THAT(another_map, Contains(pair("fie", 2))); } TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) { map some_map; some_map[1] = 11; some_map[2] = 22; EXPECT_THAT(some_map, Not(Contains(pair(2, 23)))); } TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) { const char* string_array[] = {"fee", "fie", "foe", "fum"}; EXPECT_THAT(string_array, Contains(Eq(std::string("fum")))); } TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { int int_array[] = {1, 2, 3, 4}; EXPECT_THAT(int_array, Not(Contains(5))); } TEST(ContainsTest, AcceptsMatcher) { const int a[] = {1, 2, 3}; EXPECT_THAT(a, Contains(Gt(2))); EXPECT_THAT(a, Not(Contains(Gt(4)))); } TEST(ContainsTest, WorksForNativeArrayAsTuple) { const int a[] = {1, 2}; const int* const pointer = a; EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1)); EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3)))); } TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { int a[][3] = {{1, 2, 3}, {4, 5, 6}}; EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); EXPECT_THAT(a, Contains(Contains(5))); EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); EXPECT_THAT(a, Contains(Not(Contains(5)))); } } // namespace } // namespace gmock_matchers_test } // namespace testing GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100