/third_party/gmock/test/gmock-matchers_test.cc
https://code.google.com/ · C++ · 5247 lines · 3835 code · 837 blank · 575 comment · 70 complexity · 66027d3769968bdc28b9a9671a8d1e39 MD5 · raw file
Large files are truncated click here to view the full file
- // 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.
- //
- // Author: wan@google.com (Zhanyong Wan)
- // Google Mock - a framework for writing C++ mock classes.
- //
- // This file tests some commonly used argument matchers.
- #include "gmock/gmock-matchers.h"
- #include "gmock/gmock-more-matchers.h"
- #include <string.h>
- #include <time.h>
- #include <deque>
- #include <functional>
- #include <iostream>
- #include <iterator>
- #include <limits>
- #include <list>
- #include <map>
- #include <set>
- #include <sstream>
- #include <string>
- #include <utility>
- #include <vector>
- #include "gmock/gmock.h"
- #include "gtest/gtest.h"
- #include "gtest/gtest-spi.h"
- namespace testing {
- namespace internal {
- GTEST_API_ string JoinAsTuple(const Strings& fields);
- } // namespace internal
- namespace gmock_matchers_test {
- using std::greater;
- using std::less;
- using std::list;
- using std::make_pair;
- using std::map;
- using std::multimap;
- using std::multiset;
- using std::ostream;
- using std::pair;
- using std::set;
- using std::stringstream;
- using std::tr1::get;
- using std::tr1::make_tuple;
- using std::tr1::tuple;
- using std::vector;
- using testing::A;
- using testing::AllArgs;
- using testing::AllOf;
- using testing::An;
- using testing::AnyOf;
- using testing::ByRef;
- using testing::ContainsRegex;
- using testing::DoubleEq;
- using testing::DoubleNear;
- using testing::EndsWith;
- using testing::Eq;
- using testing::ExplainMatchResult;
- using testing::Field;
- using testing::FloatEq;
- using testing::FloatNear;
- using testing::Ge;
- using testing::Gt;
- using testing::HasSubstr;
- using testing::IsEmpty;
- using testing::IsNull;
- using testing::Key;
- using testing::Le;
- using testing::Lt;
- using testing::MakeMatcher;
- using testing::MakePolymorphicMatcher;
- using testing::MatchResultListener;
- using testing::Matcher;
- using testing::MatcherCast;
- using testing::MatcherInterface;
- using testing::Matches;
- using testing::MatchesRegex;
- using testing::NanSensitiveDoubleEq;
- using testing::NanSensitiveDoubleNear;
- using testing::NanSensitiveFloatEq;
- using testing::NanSensitiveFloatNear;
- using testing::Ne;
- using testing::Not;
- using testing::NotNull;
- using testing::Pair;
- using testing::Pointee;
- using testing::Pointwise;
- using testing::PolymorphicMatcher;
- using testing::Property;
- using testing::Ref;
- using testing::ResultOf;
- using testing::SizeIs;
- using testing::StartsWith;
- using testing::StringMatchResultListener;
- using testing::StrCaseEq;
- using testing::StrCaseNe;
- using testing::StrEq;
- using testing::StrNe;
- using testing::Truly;
- using testing::TypedEq;
- using testing::Value;
- using testing::WhenSorted;
- using testing::WhenSortedBy;
- using testing::_;
- using testing::internal::DummyMatchResultListener;
- using testing::internal::ElementMatcherPair;
- using testing::internal::ElementMatcherPairs;
- using testing::internal::ExplainMatchFailureTupleTo;
- using testing::internal::FloatingEqMatcher;
- using testing::internal::FormatMatcherDescription;
- using testing::internal::IsReadableTypeName;
- using testing::internal::JoinAsTuple;
- using testing::internal::MatchMatrix;
- using testing::internal::RE;
- using testing::internal::StreamMatchResultListener;
- using testing::internal::Strings;
- using testing::internal::linked_ptr;
- using testing::internal::scoped_ptr;
- using testing::internal::string;
- // Evaluates to the number of elements in 'array'.
- #define GMOCK_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))
- // For testing ExplainMatchResultTo().
- class GreaterThanMatcher : public MatcherInterface<int> {
- public:
- explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
- virtual void DescribeTo(ostream* os) const {
- *os << "is > " << rhs_;
- }
- virtual bool MatchAndExplain(int lhs,
- MatchResultListener* listener) const {
- const int diff = lhs - rhs_;
- if (diff > 0) {
- *listener << "which is " << diff << " more than " << rhs_;
- } else if (diff == 0) {
- *listener << "which is the same as " << rhs_;
- } else {
- *listener << "which is " << -diff << " less than " << rhs_;
- }
- return lhs > rhs_;
- }
- private:
- int rhs_;
- };
- Matcher<int> GreaterThan(int n) {
- return MakeMatcher(new GreaterThanMatcher(n));
- }
- string OfType(const string& type_name) {
- #if GTEST_HAS_RTTI
- return " (of type " + type_name + ")";
- #else
- return "";
- #endif
- }
- // Returns the description of the given matcher.
- template <typename T>
- string Describe(const Matcher<T>& m) {
- stringstream ss;
- m.DescribeTo(&ss);
- return ss.str();
- }
- // Returns the description of the negation of the given matcher.
- template <typename T>
- string DescribeNegation(const Matcher<T>& m) {
- stringstream ss;
- m.DescribeNegationTo(&ss);
- return ss.str();
- }
- // Returns the reason why x matches, or doesn't match, m.
- template <typename MatcherType, typename Value>
- string Explain(const MatcherType& m, const Value& x) {
- StringMatchResultListener listener;
- ExplainMatchResult(m, x, &listener);
- return listener.str();
- }
- TEST(MatchResultListenerTest, StreamingWorks) {
- StringMatchResultListener listener;
- listener << "hi" << 5;
- EXPECT_EQ("hi5", listener.str());
- listener.Clear();
- EXPECT_EQ("", listener.str());
- listener << 42;
- EXPECT_EQ("42", listener.str());
- // Streaming shouldn't crash when the underlying ostream is NULL.
- DummyMatchResultListener dummy;
- dummy << "hi" << 5;
- }
- TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
- EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
- EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
- EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
- }
- TEST(MatchResultListenerTest, IsInterestedWorks) {
- EXPECT_TRUE(StringMatchResultListener().IsInterested());
- EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
- EXPECT_FALSE(DummyMatchResultListener().IsInterested());
- EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
- }
- // Makes sure that the MatcherInterface<T> interface doesn't
- // change.
- class EvenMatcherImpl : public MatcherInterface<int> {
- public:
- virtual bool MatchAndExplain(int x,
- MatchResultListener* /* listener */) const {
- return x % 2 == 0;
- }
- virtual void DescribeTo(ostream* os) const {
- *os << "is an even number";
- }
- // We deliberately don't define DescribeNegationTo() and
- // ExplainMatchResultTo() here, to make sure the definition of these
- // two methods is optional.
- };
- // Makes sure that the MatcherInterface API doesn't change.
- TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
- EvenMatcherImpl m;
- }
- // Tests implementing a monomorphic matcher using MatchAndExplain().
- class NewEvenMatcherImpl : public MatcherInterface<int> {
- public:
- virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
- const bool match = x % 2 == 0;
- // Verifies that we can stream to a listener directly.
- *listener << "value % " << 2;
- if (listener->stream() != NULL) {
- // Verifies that we can stream to a listener's underlying stream
- // too.
- *listener->stream() << " == " << (x % 2);
- }
- return match;
- }
- virtual void DescribeTo(ostream* os) const {
- *os << "is an even number";
- }
- };
- TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
- Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
- EXPECT_TRUE(m.Matches(2));
- EXPECT_FALSE(m.Matches(3));
- EXPECT_EQ("value % 2 == 0", Explain(m, 2));
- EXPECT_EQ("value % 2 == 1", Explain(m, 3));
- }
- // Tests default-constructing a matcher.
- TEST(MatcherTest, CanBeDefaultConstructed) {
- Matcher<double> m;
- }
- // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
- TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
- const MatcherInterface<int>* impl = new EvenMatcherImpl;
- Matcher<int> m(impl);
- EXPECT_TRUE(m.Matches(4));
- EXPECT_FALSE(m.Matches(5));
- }
- // Tests that value can be used in place of Eq(value).
- TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
- Matcher<int> m1 = 5;
- EXPECT_TRUE(m1.Matches(5));
- EXPECT_FALSE(m1.Matches(6));
- }
- // Tests that NULL can be used in place of Eq(NULL).
- TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
- Matcher<int*> m1 = NULL;
- EXPECT_TRUE(m1.Matches(NULL));
- int n = 0;
- EXPECT_FALSE(m1.Matches(&n));
- }
- // Tests that matchers are copyable.
- TEST(MatcherTest, IsCopyable) {
- // Tests the copy constructor.
- Matcher<bool> m1 = Eq(false);
- EXPECT_TRUE(m1.Matches(false));
- EXPECT_FALSE(m1.Matches(true));
- // Tests the assignment operator.
- m1 = Eq(true);
- EXPECT_TRUE(m1.Matches(true));
- EXPECT_FALSE(m1.Matches(false));
- }
- // Tests that Matcher<T>::DescribeTo() calls
- // MatcherInterface<T>::DescribeTo().
- TEST(MatcherTest, CanDescribeItself) {
- EXPECT_EQ("is an even number",
- Describe(Matcher<int>(new EvenMatcherImpl)));
- }
- // Tests Matcher<T>::MatchAndExplain().
- TEST(MatcherTest, MatchAndExplain) {
- Matcher<int> m = GreaterThan(0);
- StringMatchResultListener listener1;
- EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
- EXPECT_EQ("which is 42 more than 0", listener1.str());
- StringMatchResultListener listener2;
- EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
- EXPECT_EQ("which is 9 less than 0", listener2.str());
- }
- // Tests that a C-string literal can be implicitly converted to a
- // Matcher<string> or Matcher<const string&>.
- TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
- Matcher<string> m1 = "hi";
- EXPECT_TRUE(m1.Matches("hi"));
- EXPECT_FALSE(m1.Matches("hello"));
- Matcher<const string&> m2 = "hi";
- EXPECT_TRUE(m2.Matches("hi"));
- EXPECT_FALSE(m2.Matches("hello"));
- }
- // Tests that a string object can be implicitly converted to a
- // Matcher<string> or Matcher<const string&>.
- TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
- Matcher<string> m1 = string("hi");
- EXPECT_TRUE(m1.Matches("hi"));
- EXPECT_FALSE(m1.Matches("hello"));
- Matcher<const string&> m2 = string("hi");
- EXPECT_TRUE(m2.Matches("hi"));
- EXPECT_FALSE(m2.Matches("hello"));
- }
- #if GTEST_HAS_STRING_PIECE_
- // Tests that a C-string literal can be implicitly converted to a
- // Matcher<StringPiece> or Matcher<const StringPiece&>.
- TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
- Matcher<StringPiece> m1 = "cats";
- EXPECT_TRUE(m1.Matches("cats"));
- EXPECT_FALSE(m1.Matches("dogs"));
- Matcher<const StringPiece&> m2 = "cats";
- EXPECT_TRUE(m2.Matches("cats"));
- EXPECT_FALSE(m2.Matches("dogs"));
- }
- // Tests that a string object can be implicitly converted to a
- // Matcher<StringPiece> or Matcher<const StringPiece&>.
- TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromString) {
- Matcher<StringPiece> m1 = string("cats");
- EXPECT_TRUE(m1.Matches("cats"));
- EXPECT_FALSE(m1.Matches("dogs"));
- Matcher<const StringPiece&> m2 = string("cats");
- EXPECT_TRUE(m2.Matches("cats"));
- EXPECT_FALSE(m2.Matches("dogs"));
- }
- // Tests that a StringPiece object can be implicitly converted to a
- // Matcher<StringPiece> or Matcher<const StringPiece&>.
- TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromStringPiece) {
- Matcher<StringPiece> m1 = StringPiece("cats");
- EXPECT_TRUE(m1.Matches("cats"));
- EXPECT_FALSE(m1.Matches("dogs"));
- Matcher<const StringPiece&> m2 = StringPiece("cats");
- EXPECT_TRUE(m2.Matches("cats"));
- EXPECT_FALSE(m2.Matches("dogs"));
- }
- #endif // GTEST_HAS_STRING_PIECE_
- // Tests that MakeMatcher() constructs a Matcher<T> from a
- // MatcherInterface* without requiring the user to explicitly
- // write the type.
- TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
- const MatcherInterface<int>* dummy_impl = NULL;
- Matcher<int> m = MakeMatcher(dummy_impl);
- }
- // Tests that MakePolymorphicMatcher() can construct a polymorphic
- // matcher from its implementation using the old API.
- const int g_bar = 1;
- class ReferencesBarOrIsZeroImpl {
- public:
- template <typename T>
- bool MatchAndExplain(const T& x,
- MatchResultListener* /* listener */) const {
- const void* p = &x;
- return p == &g_bar || x == 0;
- }
- void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
- void DescribeNegationTo(ostream* os) const {
- *os << "doesn't reference g_bar and is not zero";
- }
- };
- // This function verifies that MakePolymorphicMatcher() returns a
- // PolymorphicMatcher<T> where T is the argument's type.
- PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
- return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
- }
- TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
- // Using a polymorphic matcher to match a reference type.
- Matcher<const int&> m1 = ReferencesBarOrIsZero();
- EXPECT_TRUE(m1.Matches(0));
- // Verifies that the identity of a by-reference argument is preserved.
- EXPECT_TRUE(m1.Matches(g_bar));
- EXPECT_FALSE(m1.Matches(1));
- EXPECT_EQ("g_bar or zero", Describe(m1));
- // Using a polymorphic matcher to match a value type.
- Matcher<double> m2 = ReferencesBarOrIsZero();
- EXPECT_TRUE(m2.Matches(0.0));
- EXPECT_FALSE(m2.Matches(0.1));
- EXPECT_EQ("g_bar or zero", Describe(m2));
- }
- // Tests implementing a polymorphic matcher using MatchAndExplain().
- class PolymorphicIsEvenImpl {
- public:
- void DescribeTo(ostream* os) const { *os << "is even"; }
- void DescribeNegationTo(ostream* os) const {
- *os << "is odd";
- }
- template <typename T>
- bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
- // Verifies that we can stream to the listener directly.
- *listener << "% " << 2;
- if (listener->stream() != NULL) {
- // Verifies that we can stream to the listener's underlying stream
- // too.
- *listener->stream() << " == " << (x % 2);
- }
- return (x % 2) == 0;
- }
- };
- PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
- return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
- }
- TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
- // Using PolymorphicIsEven() as a Matcher<int>.
- const Matcher<int> m1 = PolymorphicIsEven();
- EXPECT_TRUE(m1.Matches(42));
- EXPECT_FALSE(m1.Matches(43));
- EXPECT_EQ("is even", Describe(m1));
- const Matcher<int> not_m1 = Not(m1);
- EXPECT_EQ("is odd", Describe(not_m1));
- EXPECT_EQ("% 2 == 0", Explain(m1, 42));
- // Using PolymorphicIsEven() as a Matcher<char>.
- const Matcher<char> m2 = PolymorphicIsEven();
- EXPECT_TRUE(m2.Matches('\x42'));
- EXPECT_FALSE(m2.Matches('\x43'));
- EXPECT_EQ("is even", Describe(m2));
- const Matcher<char> not_m2 = Not(m2);
- EXPECT_EQ("is odd", Describe(not_m2));
- EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
- }
- // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
- TEST(MatcherCastTest, FromPolymorphicMatcher) {
- Matcher<int> m = MatcherCast<int>(Eq(5));
- EXPECT_TRUE(m.Matches(5));
- EXPECT_FALSE(m.Matches(6));
- }
- // For testing casting matchers between compatible types.
- class IntValue {
- public:
- // An int can be statically (although not implicitly) cast to a
- // IntValue.
- explicit IntValue(int a_value) : value_(a_value) {}
- int value() const { return value_; }
- private:
- int value_;
- };
- // For testing casting matchers between compatible types.
- bool IsPositiveIntValue(const IntValue& foo) {
- return foo.value() > 0;
- }
- // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
- // can be statically converted to U.
- TEST(MatcherCastTest, FromCompatibleType) {
- Matcher<double> m1 = Eq(2.0);
- Matcher<int> m2 = MatcherCast<int>(m1);
- EXPECT_TRUE(m2.Matches(2));
- EXPECT_FALSE(m2.Matches(3));
- Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
- Matcher<int> m4 = MatcherCast<int>(m3);
- // In the following, the arguments 1 and 0 are statically converted
- // to IntValue objects, and then tested by the IsPositiveIntValue()
- // predicate.
- EXPECT_TRUE(m4.Matches(1));
- EXPECT_FALSE(m4.Matches(0));
- }
- // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
- TEST(MatcherCastTest, FromConstReferenceToNonReference) {
- Matcher<const int&> m1 = Eq(0);
- Matcher<int> m2 = MatcherCast<int>(m1);
- EXPECT_TRUE(m2.Matches(0));
- EXPECT_FALSE(m2.Matches(1));
- }
- // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
- TEST(MatcherCastTest, FromReferenceToNonReference) {
- Matcher<int&> m1 = Eq(0);
- Matcher<int> m2 = MatcherCast<int>(m1);
- EXPECT_TRUE(m2.Matches(0));
- EXPECT_FALSE(m2.Matches(1));
- }
- // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
- TEST(MatcherCastTest, FromNonReferenceToConstReference) {
- Matcher<int> m1 = Eq(0);
- Matcher<const int&> m2 = MatcherCast<const int&>(m1);
- EXPECT_TRUE(m2.Matches(0));
- EXPECT_FALSE(m2.Matches(1));
- }
- // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
- TEST(MatcherCastTest, FromNonReferenceToReference) {
- Matcher<int> m1 = Eq(0);
- Matcher<int&> m2 = MatcherCast<int&>(m1);
- int n = 0;
- EXPECT_TRUE(m2.Matches(n));
- n = 1;
- EXPECT_FALSE(m2.Matches(n));
- }
- // Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
- TEST(MatcherCastTest, FromSameType) {
- Matcher<int> m1 = Eq(0);
- Matcher<int> m2 = MatcherCast<int>(m1);
- EXPECT_TRUE(m2.Matches(0));
- EXPECT_FALSE(m2.Matches(1));
- }
- // Implicitly convertible form any type.
- struct ConvertibleFromAny {
- ConvertibleFromAny(int a_value) : value(a_value) {}
- template <typename T>
- ConvertibleFromAny(const T& a_value) : value(-1) {
- ADD_FAILURE() << "Conversion constructor called";
- }
- int value;
- };
- bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
- return a.value == b.value;
- }
- ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
- return os << a.value;
- }
- TEST(MatcherCastTest, ConversionConstructorIsUsed) {
- Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
- EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
- EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
- }
- TEST(MatcherCastTest, FromConvertibleFromAny) {
- Matcher<ConvertibleFromAny> m =
- MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
- EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
- EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
- }
- class Base {};
- class Derived : public Base {};
- // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
- TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
- Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
- EXPECT_TRUE(m2.Matches(' '));
- EXPECT_FALSE(m2.Matches('\n'));
- }
- // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
- // T and U are arithmetic types and T can be losslessly converted to
- // U.
- TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
- Matcher<double> m1 = DoubleEq(1.0);
- Matcher<float> m2 = SafeMatcherCast<float>(m1);
- EXPECT_TRUE(m2.Matches(1.0f));
- EXPECT_FALSE(m2.Matches(2.0f));
- Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
- EXPECT_TRUE(m3.Matches('a'));
- EXPECT_FALSE(m3.Matches('b'));
- }
- // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
- // are pointers or references to a derived and a base class, correspondingly.
- TEST(SafeMatcherCastTest, FromBaseClass) {
- Derived d, d2;
- Matcher<Base*> m1 = Eq(&d);
- Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
- EXPECT_TRUE(m2.Matches(&d));
- EXPECT_FALSE(m2.Matches(&d2));
- Matcher<Base&> m3 = Ref(d);
- Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
- EXPECT_TRUE(m4.Matches(d));
- EXPECT_FALSE(m4.Matches(d2));
- }
- // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
- TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
- int n = 0;
- Matcher<const int&> m1 = Ref(n);
- Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
- int n1 = 0;
- EXPECT_TRUE(m2.Matches(n));
- EXPECT_FALSE(m2.Matches(n1));
- }
- // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
- TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
- Matcher<int> m1 = Eq(0);
- Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
- EXPECT_TRUE(m2.Matches(0));
- EXPECT_FALSE(m2.Matches(1));
- }
- // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
- TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
- Matcher<int> m1 = Eq(0);
- Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
- int n = 0;
- EXPECT_TRUE(m2.Matches(n));
- n = 1;
- EXPECT_FALSE(m2.Matches(n));
- }
- // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
- TEST(SafeMatcherCastTest, FromSameType) {
- Matcher<int> m1 = Eq(0);
- Matcher<int> m2 = SafeMatcherCast<int>(m1);
- EXPECT_TRUE(m2.Matches(0));
- EXPECT_FALSE(m2.Matches(1));
- }
- TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
- Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
- EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
- EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
- }
- TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
- Matcher<ConvertibleFromAny> m =
- SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
- EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
- EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
- }
- // Tests that A<T>() matches any value of type T.
- TEST(ATest, MatchesAnyValue) {
- // Tests a matcher for a value type.
- Matcher<double> m1 = A<double>();
- EXPECT_TRUE(m1.Matches(91.43));
- EXPECT_TRUE(m1.Matches(-15.32));
- // Tests a matcher for a reference type.
- int a = 2;
- int b = -6;
- Matcher<int&> m2 = A<int&>();
- EXPECT_TRUE(m2.Matches(a));
- EXPECT_TRUE(m2.Matches(b));
- }
- TEST(ATest, WorksForDerivedClass) {
- Base base;
- Derived derived;
- EXPECT_THAT(&base, A<Base*>());
- // This shouldn't compile: EXPECT_THAT(&base, A<Derived*>());
- EXPECT_THAT(&derived, A<Base*>());
- EXPECT_THAT(&derived, A<Derived*>());
- }
- // Tests that A<T>() describes itself properly.
- TEST(ATest, CanDescribeSelf) {
- EXPECT_EQ("is anything", Describe(A<bool>()));
- }
- // Tests that An<T>() matches any value of type T.
- TEST(AnTest, MatchesAnyValue) {
- // Tests a matcher for a value type.
- Matcher<int> m1 = An<int>();
- EXPECT_TRUE(m1.Matches(9143));
- EXPECT_TRUE(m1.Matches(-1532));
- // Tests a matcher for a reference type.
- int a = 2;
- int b = -6;
- Matcher<int&> m2 = An<int&>();
- EXPECT_TRUE(m2.Matches(a));
- EXPECT_TRUE(m2.Matches(b));
- }
- // Tests that An<T>() describes itself properly.
- TEST(AnTest, CanDescribeSelf) {
- EXPECT_EQ("is anything", Describe(An<int>()));
- }
- // Tests that _ can be used as a matcher for any type and matches any
- // value of that type.
- TEST(UnderscoreTest, MatchesAnyValue) {
- // Uses _ as a matcher for a value type.
- Matcher<int> m1 = _;
- EXPECT_TRUE(m1.Matches(123));
- EXPECT_TRUE(m1.Matches(-242));
- // Uses _ as a matcher for a reference type.
- bool a = false;
- const bool b = true;
- Matcher<const bool&> m2 = _;
- EXPECT_TRUE(m2.Matches(a));
- EXPECT_TRUE(m2.Matches(b));
- }
- // Tests that _ describes itself properly.
- TEST(UnderscoreTest, CanDescribeSelf) {
- Matcher<int> m = _;
- EXPECT_EQ("is anything", Describe(m));
- }
- // Tests that Eq(x) matches any value equal to x.
- TEST(EqTest, MatchesEqualValue) {
- // 2 C-strings with same content but different addresses.
- const char a1[] = "hi";
- const char a2[] = "hi";
- Matcher<const char*> m1 = Eq(a1);
- EXPECT_TRUE(m1.Matches(a1));
- EXPECT_FALSE(m1.Matches(a2));
- }
- // Tests that Eq(v) describes itself properly.
- class Unprintable {
- public:
- Unprintable() : c_('a') {}
- bool operator==(const Unprintable& /* rhs */) { return true; }
- private:
- char c_;
- };
- TEST(EqTest, CanDescribeSelf) {
- Matcher<Unprintable> m = Eq(Unprintable());
- EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
- }
- // Tests that Eq(v) can be used to match any type that supports
- // comparing with type T, where T is v's type.
- TEST(EqTest, IsPolymorphic) {
- Matcher<int> m1 = Eq(1);
- EXPECT_TRUE(m1.Matches(1));
- EXPECT_FALSE(m1.Matches(2));
- Matcher<char> m2 = Eq(1);
- EXPECT_TRUE(m2.Matches('\1'));
- EXPECT_FALSE(m2.Matches('a'));
- }
- // Tests that TypedEq<T>(v) matches values of type T that's equal to v.
- TEST(TypedEqTest, ChecksEqualityForGivenType) {
- Matcher<char> m1 = TypedEq<char>('a');
- EXPECT_TRUE(m1.Matches('a'));
- EXPECT_FALSE(m1.Matches('b'));
- Matcher<int> m2 = TypedEq<int>(6);
- EXPECT_TRUE(m2.Matches(6));
- EXPECT_FALSE(m2.Matches(7));
- }
- // Tests that TypedEq(v) describes itself properly.
- TEST(TypedEqTest, CanDescribeSelf) {
- EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
- }
- // Tests that TypedEq<T>(v) has type Matcher<T>.
- // Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
- // is a "bare" type (i.e. not in the form of const U or U&). If v's
- // type is not T, the compiler will generate a message about
- // "undefined referece".
- template <typename T>
- struct Type {
- static bool IsTypeOf(const T& /* v */) { return true; }
- template <typename T2>
- static void IsTypeOf(T2 v);
- };
- TEST(TypedEqTest, HasSpecifiedType) {
- // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
- Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
- Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
- }
- // Tests that Ge(v) matches anything >= v.
- TEST(GeTest, ImplementsGreaterThanOrEqual) {
- Matcher<int> m1 = Ge(0);
- EXPECT_TRUE(m1.Matches(1));
- EXPECT_TRUE(m1.Matches(0));
- EXPECT_FALSE(m1.Matches(-1));
- }
- // Tests that Ge(v) describes itself properly.
- TEST(GeTest, CanDescribeSelf) {
- Matcher<int> m = Ge(5);
- EXPECT_EQ("is >= 5", Describe(m));
- }
- // Tests that Gt(v) matches anything > v.
- TEST(GtTest, ImplementsGreaterThan) {
- Matcher<double> m1 = Gt(0);
- EXPECT_TRUE(m1.Matches(1.0));
- EXPECT_FALSE(m1.Matches(0.0));
- EXPECT_FALSE(m1.Matches(-1.0));
- }
- // Tests that Gt(v) describes itself properly.
- TEST(GtTest, CanDescribeSelf) {
- Matcher<int> m = Gt(5);
- EXPECT_EQ("is > 5", Describe(m));
- }
- // Tests that Le(v) matches anything <= v.
- TEST(LeTest, ImplementsLessThanOrEqual) {
- Matcher<char> m1 = Le('b');
- EXPECT_TRUE(m1.Matches('a'));
- EXPECT_TRUE(m1.Matches('b'));
- EXPECT_FALSE(m1.Matches('c'));
- }
- // Tests that Le(v) describes itself properly.
- TEST(LeTest, CanDescribeSelf) {
- Matcher<int> m = Le(5);
- EXPECT_EQ("is <= 5", Describe(m));
- }
- // Tests that Lt(v) matches anything < v.
- TEST(LtTest, ImplementsLessThan) {
- Matcher<const string&> m1 = Lt("Hello");
- EXPECT_TRUE(m1.Matches("Abc"));
- EXPECT_FALSE(m1.Matches("Hello"));
- EXPECT_FALSE(m1.Matches("Hello, world!"));
- }
- // Tests that Lt(v) describes itself properly.
- TEST(LtTest, CanDescribeSelf) {
- Matcher<int> m = Lt(5);
- EXPECT_EQ("is < 5", Describe(m));
- }
- // Tests that Ne(v) matches anything != v.
- TEST(NeTest, ImplementsNotEqual) {
- Matcher<int> m1 = Ne(0);
- EXPECT_TRUE(m1.Matches(1));
- EXPECT_TRUE(m1.Matches(-1));
- EXPECT_FALSE(m1.Matches(0));
- }
- // Tests that Ne(v) describes itself properly.
- TEST(NeTest, CanDescribeSelf) {
- Matcher<int> m = Ne(5);
- EXPECT_EQ("isn't equal to 5", Describe(m));
- }
- // Tests that IsNull() matches any NULL pointer of any type.
- TEST(IsNullTest, MatchesNullPointer) {
- Matcher<int*> m1 = IsNull();
- int* p1 = NULL;
- int n = 0;
- EXPECT_TRUE(m1.Matches(p1));
- EXPECT_FALSE(m1.Matches(&n));
- Matcher<const char*> m2 = IsNull();
- const char* p2 = NULL;
- EXPECT_TRUE(m2.Matches(p2));
- EXPECT_FALSE(m2.Matches("hi"));
- #if !GTEST_OS_SYMBIAN
- // Nokia's Symbian compiler generates:
- // gmock-matchers.h: ambiguous access to overloaded function
- // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
- // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
- // MatcherInterface<void *> *)'
- // gmock-matchers.h: (point of instantiation: 'testing::
- // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
- // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
- Matcher<void*> m3 = IsNull();
- void* p3 = NULL;
- EXPECT_TRUE(m3.Matches(p3));
- EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
- #endif
- }
- TEST(IsNullTest, LinkedPtr) {
- const Matcher<linked_ptr<int> > m = IsNull();
- const linked_ptr<int> null_p;
- const linked_ptr<int> non_null_p(new int);
- EXPECT_TRUE(m.Matches(null_p));
- EXPECT_FALSE(m.Matches(non_null_p));
- }
- TEST(IsNullTest, ReferenceToConstLinkedPtr) {
- const Matcher<const linked_ptr<double>&> m = IsNull();
- const linked_ptr<double> null_p;
- const linked_ptr<double> non_null_p(new double);
- EXPECT_TRUE(m.Matches(null_p));
- EXPECT_FALSE(m.Matches(non_null_p));
- }
- TEST(IsNullTest, ReferenceToConstScopedPtr) {
- const Matcher<const scoped_ptr<double>&> m = IsNull();
- const scoped_ptr<double> null_p;
- const scoped_ptr<double> non_null_p(new double);
- EXPECT_TRUE(m.Matches(null_p));
- EXPECT_FALSE(m.Matches(non_null_p));
- }
- // Tests that IsNull() describes itself properly.
- TEST(IsNullTest, CanDescribeSelf) {
- Matcher<int*> m = IsNull();
- EXPECT_EQ("is NULL", Describe(m));
- EXPECT_EQ("isn't NULL", DescribeNegation(m));
- }
- // Tests that NotNull() matches any non-NULL pointer of any type.
- TEST(NotNullTest, MatchesNonNullPointer) {
- Matcher<int*> m1 = NotNull();
- int* p1 = NULL;
- int n = 0;
- EXPECT_FALSE(m1.Matches(p1));
- EXPECT_TRUE(m1.Matches(&n));
- Matcher<const char*> m2 = NotNull();
- const char* p2 = NULL;
- EXPECT_FALSE(m2.Matches(p2));
- EXPECT_TRUE(m2.Matches("hi"));
- }
- TEST(NotNullTest, LinkedPtr) {
- const Matcher<linked_ptr<int> > m = NotNull();
- const linked_ptr<int> null_p;
- const linked_ptr<int> non_null_p(new int);
- EXPECT_FALSE(m.Matches(null_p));
- EXPECT_TRUE(m.Matches(non_null_p));
- }
- TEST(NotNullTest, ReferenceToConstLinkedPtr) {
- const Matcher<const linked_ptr<double>&> m = NotNull();
- const linked_ptr<double> null_p;
- const linked_ptr<double> non_null_p(new double);
- EXPECT_FALSE(m.Matches(null_p));
- EXPECT_TRUE(m.Matches(non_null_p));
- }
- TEST(NotNullTest, ReferenceToConstScopedPtr) {
- const Matcher<const scoped_ptr<double>&> m = NotNull();
- const scoped_ptr<double> null_p;
- const scoped_ptr<double> non_null_p(new double);
- EXPECT_FALSE(m.Matches(null_p));
- EXPECT_TRUE(m.Matches(non_null_p));
- }
- // Tests that NotNull() describes itself properly.
- TEST(NotNullTest, CanDescribeSelf) {
- Matcher<int*> m = NotNull();
- EXPECT_EQ("isn't NULL", Describe(m));
- }
- // Tests that Ref(variable) matches an argument that references
- // 'variable'.
- TEST(RefTest, MatchesSameVariable) {
- int a = 0;
- int b = 0;
- Matcher<int&> m = Ref(a);
- EXPECT_TRUE(m.Matches(a));
- EXPECT_FALSE(m.Matches(b));
- }
- // Tests that Ref(variable) describes itself properly.
- TEST(RefTest, CanDescribeSelf) {
- int n = 5;
- Matcher<int&> m = Ref(n);
- stringstream ss;
- ss << "references the variable @" << &n << " 5";
- EXPECT_EQ(string(ss.str()), Describe(m));
- }
- // Test that Ref(non_const_varialbe) can be used as a matcher for a
- // const reference.
- TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
- int a = 0;
- int b = 0;
- Matcher<const int&> m = Ref(a);
- EXPECT_TRUE(m.Matches(a));
- EXPECT_FALSE(m.Matches(b));
- }
- // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
- // used wherever Ref(base) can be used (Ref(derived) is a sub-type
- // of Ref(base), but not vice versa.
- TEST(RefTest, IsCovariant) {
- Base base, base2;
- Derived derived;
- Matcher<const Base&> m1 = Ref(base);
- EXPECT_TRUE(m1.Matches(base));
- EXPECT_FALSE(m1.Matches(base2));
- EXPECT_FALSE(m1.Matches(derived));
- m1 = Ref(derived);
- EXPECT_TRUE(m1.Matches(derived));
- EXPECT_FALSE(m1.Matches(base));
- EXPECT_FALSE(m1.Matches(base2));
- }
- TEST(RefTest, ExplainsResult) {
- int n = 0;
- EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
- StartsWith("which is located @"));
- int m = 0;
- EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
- StartsWith("which is located @"));
- }
- // Tests string comparison matchers.
- TEST(StrEqTest, MatchesEqualString) {
- Matcher<const char*> m = StrEq(string("Hello"));
- EXPECT_TRUE(m.Matches("Hello"));
- EXPECT_FALSE(m.Matches("hello"));
- EXPECT_FALSE(m.Matches(NULL));
- Matcher<const string&> m2 = StrEq("Hello");
- EXPECT_TRUE(m2.Matches("Hello"));
- EXPECT_FALSE(m2.Matches("Hi"));
- }
- TEST(StrEqTest, CanDescribeSelf) {
- Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
- EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
- Describe(m));
- string str("01204500800");
- str[3] = '\0';
- Matcher<string> m2 = StrEq(str);
- EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
- str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
- Matcher<string> m3 = StrEq(str);
- EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
- }
- TEST(StrNeTest, MatchesUnequalString) {
- Matcher<const char*> m = StrNe("Hello");
- EXPECT_TRUE(m.Matches(""));
- EXPECT_TRUE(m.Matches(NULL));
- EXPECT_FALSE(m.Matches("Hello"));
- Matcher<string> m2 = StrNe(string("Hello"));
- EXPECT_TRUE(m2.Matches("hello"));
- EXPECT_FALSE(m2.Matches("Hello"));
- }
- TEST(StrNeTest, CanDescribeSelf) {
- Matcher<const char*> m = StrNe("Hi");
- EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
- }
- TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
- Matcher<const char*> m = StrCaseEq(string("Hello"));
- EXPECT_TRUE(m.Matches("Hello"));
- EXPECT_TRUE(m.Matches("hello"));
- EXPECT_FALSE(m.Matches("Hi"));
- EXPECT_FALSE(m.Matches(NULL));
- Matcher<const string&> m2 = StrCaseEq("Hello");
- EXPECT_TRUE(m2.Matches("hello"));
- EXPECT_FALSE(m2.Matches("Hi"));
- }
- TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
- string str1("oabocdooeoo");
- string str2("OABOCDOOEOO");
- Matcher<const string&> m0 = StrCaseEq(str1);
- EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
- str1[3] = str2[3] = '\0';
- Matcher<const string&> m1 = StrCaseEq(str1);
- EXPECT_TRUE(m1.Matches(str2));
- str1[0] = str1[6] = str1[7] = str1[10] = '\0';
- str2[0] = str2[6] = str2[7] = str2[10] = '\0';
- Matcher<const string&> m2 = StrCaseEq(str1);
- str1[9] = str2[9] = '\0';
- EXPECT_FALSE(m2.Matches(str2));
- Matcher<const string&> m3 = StrCaseEq(str1);
- EXPECT_TRUE(m3.Matches(str2));
- EXPECT_FALSE(m3.Matches(str2 + "x"));
- str2.append(1, '\0');
- EXPECT_FALSE(m3.Matches(str2));
- EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
- }
- TEST(StrCaseEqTest, CanDescribeSelf) {
- Matcher<string> m = StrCaseEq("Hi");
- EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
- }
- TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
- Matcher<const char*> m = StrCaseNe("Hello");
- EXPECT_TRUE(m.Matches("Hi"));
- EXPECT_TRUE(m.Matches(NULL));
- EXPECT_FALSE(m.Matches("Hello"));
- EXPECT_FALSE(m.Matches("hello"));
- Matcher<string> m2 = StrCaseNe(string("Hello"));
- EXPECT_TRUE(m2.Matches(""));
- EXPECT_FALSE(m2.Matches("Hello"));
- }
- TEST(StrCaseNeTest, CanDescribeSelf) {
- Matcher<const char*> m = StrCaseNe("Hi");
- EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
- }
- // Tests that HasSubstr() works for matching string-typed values.
- TEST(HasSubstrTest, WorksForStringClasses) {
- const Matcher<string> m1 = HasSubstr("foo");
- EXPECT_TRUE(m1.Matches(string("I love food.")));
- EXPECT_FALSE(m1.Matches(string("tofo")));
- const Matcher<const std::string&> m2 = HasSubstr("foo");
- EXPECT_TRUE(m2.Matches(std::string("I love food.")));
- EXPECT_FALSE(m2.Matches(std::string("tofo")));
- }
- // Tests that HasSubstr() works for matching C-string-typed values.
- TEST(HasSubstrTest, WorksForCStrings) {
- const Matcher<char*> m1 = HasSubstr("foo");
- EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
- EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
- EXPECT_FALSE(m1.Matches(NULL));
- const Matcher<const char*> m2 = HasSubstr("foo");
- EXPECT_TRUE(m2.Matches("I love food."));
- EXPECT_FALSE(m2.Matches("tofo"));
- EXPECT_FALSE(m2.Matches(NULL));
- }
- // Tests that HasSubstr(s) describes itself properly.
- TEST(HasSubstrTest, CanDescribeSelf) {
- Matcher<string> m = HasSubstr("foo\n\"");
- EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
- }
- TEST(KeyTest, CanDescribeSelf) {
- Matcher<const pair<std::string, int>&> m = Key("foo");
- EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
- EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
- }
- TEST(KeyTest, ExplainsResult) {
- Matcher<pair<int, bool> > m = Key(GreaterThan(10));
- EXPECT_EQ("whose first field is a value which is 5 less than 10",
- Explain(m, make_pair(5, true)));
- EXPECT_EQ("whose first field is a value which is 5 more than 10",
- Explain(m, make_pair(15, true)));
- }
- TEST(KeyTest, MatchesCorrectly) {
- pair<int, std::string> p(25, "foo");
- EXPECT_THAT(p, Key(25));
- EXPECT_THAT(p, Not(Key(42)));
- EXPECT_THAT(p, Key(Ge(20)));
- EXPECT_THAT(p, Not(Key(Lt(25))));
- }
- TEST(KeyTest, SafelyCastsInnerMatcher) {
- Matcher<int> is_positive = Gt(0);
- Matcher<int> is_negative = Lt(0);
- pair<char, bool> p('a', true);
- EXPECT_THAT(p, Key(is_positive));
- EXPECT_THAT(p, Not(Key(is_negative)));
- }
- TEST(KeyTest, InsideContainsUsingMap) {
- map<int, char> container;
- container.insert(make_pair(1, 'a'));
- container.insert(make_pair(2, 'b'));
- container.insert(make_pair(4, 'c'));
- EXPECT_THAT(container, Contains(Key(1)));
- EXPECT_THAT(container, Not(Contains(Key(3))));
- }
- TEST(KeyTest, InsideContainsUsingMultimap) {
- multimap<int, char> container;
- container.insert(make_pair(1, 'a'));
- container.insert(make_pair(2, 'b'));
- container.insert(make_pair(4, 'c'));
- EXPECT_THAT(container, Not(Contains(Key(25))));
- container.insert(make_pair(25, 'd'));
- EXPECT_THAT(container, Contains(Key(25)));
- container.insert(make_pair(25, 'e'));
- EXPECT_THAT(container, Contains(Key(25)));
- EXPECT_THAT(container, Contains(Key(1)));
- EXPECT_THAT(container, Not(Contains(Key(3))));
- }
- TEST(PairTest, Typing) {
- // Test verifies the following type conversions can be compiled.
- Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
- Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
- Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
- Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
- Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
- }
- TEST(PairTest, CanDescribeSelf) {
- Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
- EXPECT_EQ("has a first field that is equal to \"foo\""
- ", and has a second field that is equal to 42",
- Describe(m1));
- EXPECT_EQ("has a first field that isn't equal to \"foo\""
- ", or has a second field that isn't equal to 42",
- DescribeNegation(m1));
- // Double and triple negation (1 or 2 times not and description of negation).
- Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
- EXPECT_EQ("has a first field that isn't equal to 13"
- ", and has a second field that is equal to 42",
- DescribeNegation(m2));
- }
- TEST(PairTest, CanExplainMatchResultTo) {
- // If neither field matches, Pair() should explain about the first
- // field.
- const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
- EXPECT_EQ("whose first field does not match, which is 1 less than 0",
- Explain(m, make_pair(-1, -2)));
- // If the first field matches but the second doesn't, Pair() should
- // explain about the second field.
- EXPECT_EQ("whose second field does not match, which is 2 less than 0",
- Explain(m, make_pair(1, -2)));
- // If the first field doesn't match but the second does, Pair()
- // should explain about the first field.
- EXPECT_EQ("whose first field does not match, which is 1 less than 0",
- Explain(m, make_pair(-1, 2)));
- // If both fields match, Pair() should explain about them both.
- EXPECT_EQ("whose both fields match, where the first field is a value "
- "which is 1 more than 0, and the second field is a value "
- "which is 2 more than 0",
- Explain(m, make_pair(1, 2)));
- // If only the first match has an explanation, only this explanation should
- // be printed.
- const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
- EXPECT_EQ("whose both fields match, where the first field is a value "
- "which is 1 more than 0",
- Explain(explain_first, make_pair(1, 0)));
- // If only the second match has an explanation, only this explanation should
- // be printed.
- const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
- EXPECT_EQ("whose both fields match, where the second field is a value "
- "which is 1 more than 0",
- Explain(explain_second, make_pair(0, 1)));
- }
- TEST(PairTest, MatchesCorrectly) {
- pair<int, std::string> p(25, "foo");
- // Both fields match.
- EXPECT_THAT(p, Pair(25, "foo"));
- EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
- // 'first' doesnt' match, but 'second' matches.
- EXPECT_THAT(p, Not(Pair(42, "foo")));
- EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
- // 'first' matches, but 'second' doesn't match.
- EXPECT_THAT(p, Not(Pair(25, "bar")));
- EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
- // Neither field matches.
- EXPECT_THAT(p, Not(Pair(13, "bar")));
- EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
- }
- TEST(PairTest, SafelyCastsInnerMatchers) {
- Matcher<int> is_positive = Gt(0);
- Matcher<int> is_negative = Lt(0);
- pair<char, bool> p('a', true);
- EXPECT_THAT(p, Pair(is_positive, _));
- EXPECT_THAT(p, Not(Pair(is_negative, _)));
- EXPECT_THAT(p, Pair(_, is_positive));
- EXPECT_THAT(p, Not(Pair(_, is_negative)));
- }
- TEST(PairTest, InsideContainsUsingMap) {
- map<int, char> container;
- container.insert(make_pair(1, 'a'));
- container.insert(make_pair(2, 'b'));
- container.insert(make_pair(4, 'c'));
- EXPECT_THAT(container, Contains(Pair(1, 'a')));
- EXPECT_THAT(container, Contains(Pair(1, _)));
- EXPECT_THAT(container, Contains(Pair(_, 'a')));
- EXPECT_THAT(container, Not(Contains(Pair(3, _))));
- }
- // Tests StartsWith(s).
- TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
- const Matcher<const char*> m1 = StartsWith(string(""));
- EXPECT_TRUE(m1.Matches("Hi"));
- EXPECT_TRUE(m1.Matches(""));
- EXPECT_FALSE(m1.Matches(NULL));
- const Matcher<const string&> m2 = StartsWith("Hi");
- EXPECT_TRUE(m2.Matches("Hi"));
- EXPECT_TRUE(m2.Matches("Hi Hi!"));
- EXPECT_TRUE(m2.Matches("High"));
- EXPECT_FALSE(m2.Matches("H"));
- EXPECT_FALSE(m2.Matches(" Hi"));
- }
- TEST(StartsWithTest, CanDescribeSelf) {
- Matcher<const std::string> m = StartsWith("Hi");
- EXPECT_EQ("starts with \"Hi\"", Describe(m));
- }
- // Tests EndsWith(s).
- TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
- const Matcher<const char*> m1 = EndsWith("");
- EXPECT_TRUE(m1.Matches("Hi"));
- EXPECT_TRUE(m1.Matches(""));
- EXPECT_FALSE(m1.Matches(NULL));
- const Matcher<const string&> m2 = EndsWith(string("Hi"));
- EXPECT_TRUE(m2.Matches("Hi"));
- EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
- EXPECT_TRUE(m2.Matches("Super Hi"));
- EXPECT_FALSE(m2.Matches("i"));
- EXPECT_FALSE(m2.Matches("Hi "));
- }
- TEST(EndsWithTest, CanDescribeSelf) {
- Matcher<const std::string> m = EndsWith("Hi");
- EXPECT_EQ("ends with \"Hi\"", Describe(m));
- }
- // Tests MatchesRegex().
- TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
- const Matcher<const char*> m1 = MatchesRegex("a.*z");
- EXPECT_TRUE(m1.Matches("az"));
- EXPECT_TRUE(m1.Matches("abcz"));
- EXPECT_FALSE(m1.Matches(NULL));
- const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
- EXPECT_TRUE(m2.Matches("azbz"));
- EXPECT_FALSE(m2.Matches("az1"));
- EXPECT_FALSE(m2.Matches("1az"));
- }
- TEST(MatchesRegexTest, CanDescribeSelf) {
- Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
- EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
- Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
- EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
- }
- // Tests ContainsRegex().
- TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
- const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
- EXPECT_TRUE(m1.Matches("az"));
- EXPECT_TRUE(m1.Matches("0abcz1"));
- EXPECT_FALSE(m1.Matches(NULL));
- const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
- EXPECT_TRUE(m2.Matches("azbz"));
- EXPECT_TRUE(m2.Matches("az1"));
- EXPECT_FALSE(m2.Matches("1a"));
- }
- TEST(ContainsRegexTest, CanDescribeSelf) {
- Matcher<const std::string> m1 = ContainsRegex("Hi.*");
- EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
- Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
- EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
- }
- // Tests for wide strings.
- #if GTEST_HAS_STD_WSTRING
- TEST(StdWideStrEqTest, MatchesEqual) {
- Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
- EXPECT_TRUE(m.Matches(L"Hello"));
- EXPECT_FALSE(m.Matches(L"hello"));
- EXPECT_FALSE(m.Matches(NULL));
- Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
- EXPECT_TRUE(m2.Matches(L"Hello"));
- EXPECT_FALSE(m2.Matches(L"Hi"));
- Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
- EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
- EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
- ::std::wstring str(L"01204500800");
- str[3] = L'\0';
- Matcher<const ::std::wstring&> m4 = StrEq(str);
- EXPECT_TRUE(m4.Matches(str));
- str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
- Matcher<const ::std::wstring&> m5 = StrEq(str);
- EXPECT_TRUE(m5.Matches(str));
- }
- TEST(StdWideStrEqTest, CanDescribeSelf) {
- Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
- EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
- Describe(m));
- Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
- EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
- Describe(m2));
- ::std::wstring str(L"01204500800");
- str[3] = L'\0';
- Matcher<const ::std::wstring&> m4 = StrEq(str);
- EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
- str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
- Matcher<const ::std::wstring&> m5 = StrEq(str);
- EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
- }
- TEST(StdWideStrNeTest, MatchesUnequalString) {
- Matcher<const wchar_t*> m = StrNe(L"Hello");
- EXPECT_TRUE(m.Matches(L""));
- EXPECT_TRUE(m.Matches(NULL));
- EXPECT_FALSE(m.Matches(L"Hello"));
- Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
- EXPECT_TRUE(m2.Matches(L"hello"));
- EXPECT_FALSE(m2.Matches(L"Hello"));
- }
- TEST(StdWideStrNeTest, CanDescribeSelf) {
- Matcher<const wchar_t*> m = StrNe(L"Hi");
- EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
- }
- TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
- Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
- EXPECT_TRUE(m.Matches(L"Hello"));
- EXPECT_TRUE(m.Matches(L"hello"));
- EXPECT_FALSE(m.Matches(L"Hi"));
- EXPECT_FALSE(m.Matches(NULL));
- Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
- EXPECT_TRUE(m2.Matches(L"hello"));
- EXPECT_FALSE(m2.Matches(L"Hi"));
- }
- TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
- ::std::wstring str1(L"oabocdooeoo");
- ::std::wstring str2(L"OABOCDOOEOO");
- Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
- EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
- str1[3] = str2[3] = L'\0';
- Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
- EXPECT_TRUE(m1.Matches(str2));
- str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
- str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
- Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
- str1[9] = str2[9] = L'\0';
- EXPECT_FALSE(m2.Matches(str2));
- Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
- EXPECT_TRUE(m3.Matches(str2));
- EXPECT_FALSE(m3.Matches(str2 + L"x"));
- str2.append(1, L'\0');
- EXPECT_FALSE(m3.Matches(str2));
- EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
- }
- TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
- Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
- EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
- }
- TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
- Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
- EXPECT_TRUE(m.Matches(L"Hi"));
- EXPECT_TRUE(m.Matches(NULL));
- EXPECT_FALSE(m.Matches(L"Hello"));
- EXPECT_FALSE(m.Matches(L"hello"));
- Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
- EXPECT_TRUE(m2.Matches(L""));
- EXPECT_FALSE(m2.Matches(L"Hello"));
- }
- TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
- Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
- EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
- }
- // Tests that HasSubstr() works for matching wstring-typed values.
- TEST(StdWideHasSubstrTest, WorksForStringClasses) {
- const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
- EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
- EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
- const…