HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Android 10
|
10.0.0_r6
下载
查看原文件
收藏
根目录
external
googletest
googlemock
test
gmock-matchers_test.cc
// 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. // Silence warning C4244: 'initializing': conversion from 'int' to 'short', // possible loss of data and C4100, unreferenced local parameter #ifdef _MSC_VER # pragma warning(push) # pragma warning(disable:4244) # pragma warning(disable:4100) #endif #include "gmock/gmock-matchers.h" #include "gmock/gmock-more-matchers.h" #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "gmock/gmock.h" #include "gtest/gtest.h" #include "gtest/gtest-spi.h" namespace testing { namespace gmock_matchers_test { namespace { 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::vector; 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::MatchMatrix; using testing::internal::PredicateFormatterFromMatcher; using testing::internal::RE; using testing::internal::StreamMatchResultListener; using testing::internal::string; using testing::internal::Strings; // Helper for testing container-valued matchers in mock method context. It is // important to test matchers in this context, since it requires additional type // deduction beyond what EXPECT_THAT does, thus making it more restrictive. struct ContainerHelper { MOCK_METHOD1(Call, void(std::vector
>)); }; std::vector
> MakeUniquePtrs(const std::vector
& ints) { std::vector
> pointers; for (int i : ints) pointers.emplace_back(new int(i)); return pointers; } // For testing ExplainMatchResultTo(). class GreaterThanMatcher : public MatcherInterface
{ public: explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} void DescribeTo(ostream* os) const override { *os << "is > " << rhs_; } bool MatchAndExplain(int lhs, MatchResultListener* listener) const override { 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
GreaterThan(int n) { return MakeMatcher(new GreaterThanMatcher(n)); } std::string OfType(const std::string& type_name) { #if GTEST_HAS_RTTI return " (of type " + type_name + ")"; #else return ""; #endif } // Returns the description of the given matcher. template
std::string Describe(const Matcher
& m) { return DescribeMatcher
(m); } // Returns the description of the negation of the given matcher. template
std::string DescribeNegation(const Matcher
& m) { return DescribeMatcher
(m, true); } // Returns the reason why x matches, or doesn't match, m. template
std::string Explain(const MatcherType& m, const Value& x) { StringMatchResultListener listener; ExplainMatchResult(m, x, &listener); return listener.str(); } TEST(MonotonicMatcherTest, IsPrintable) { stringstream ss; ss << GreaterThan(5); EXPECT_EQ("is > 5", ss.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() == nullptr); EXPECT_TRUE(StreamMatchResultListener(nullptr).stream() == nullptr); 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(nullptr).IsInterested()); } // Makes sure that the MatcherInterface
interface doesn't // change. class EvenMatcherImpl : public MatcherInterface
{ public: bool MatchAndExplain(int x, MatchResultListener* /* listener */) const override { return x % 2 == 0; } void DescribeTo(ostream* os) const override { *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
{ public: bool MatchAndExplain(int x, MatchResultListener* listener) const override { const bool match = x % 2 == 0; // Verifies that we can stream to a listener directly. *listener << "value % " << 2; if (listener->stream() != nullptr) { // Verifies that we can stream to a listener's underlying stream // too. *listener->stream() << " == " << (x % 2); } return match; } void DescribeTo(ostream* os) const override { *os << "is an even number"; } }; TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) { Matcher
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
m; } // Tests that Matcher
can be constructed from a MatcherInterface
*. TEST(MatcherTest, CanBeConstructedFromMatcherInterface) { const MatcherInterface
* impl = new EvenMatcherImpl; Matcher
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
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
m1 = nullptr; EXPECT_TRUE(m1.Matches(nullptr)); int n = 0; EXPECT_FALSE(m1.Matches(&n)); } // Tests that matchers can be constructed from a variable that is not properly // defined. This should be illegal, but many users rely on this accidentally. struct Undefined { virtual ~Undefined() = 0; static const int kInt = 1; }; TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) { Matcher
m1 = Undefined::kInt; EXPECT_TRUE(m1.Matches(1)); EXPECT_FALSE(m1.Matches(2)); } // Test that a matcher parameterized with an abstract class compiles. TEST(MatcherTest, CanAcceptAbstractClass) { Matcher
m = _; } // Tests that matchers are copyable. TEST(MatcherTest, IsCopyable) { // Tests the copy constructor. Matcher
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
::DescribeTo() calls // MatcherInterface
::DescribeTo(). TEST(MatcherTest, CanDescribeItself) { EXPECT_EQ("is an even number", Describe(Matcher
(new EvenMatcherImpl))); } // Tests Matcher
::MatchAndExplain(). TEST(MatcherTest, MatchAndExplain) { Matcher
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
or Matcher
. TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { Matcher
m1 = "hi"; EXPECT_TRUE(m1.Matches("hi")); EXPECT_FALSE(m1.Matches("hello")); Matcher
m2 = "hi"; EXPECT_TRUE(m2.Matches("hi")); EXPECT_FALSE(m2.Matches("hello")); } // Tests that a string object can be implicitly converted to a // Matcher
or Matcher
. TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) { Matcher
m1 = std::string("hi"); EXPECT_TRUE(m1.Matches("hi")); EXPECT_FALSE(m1.Matches("hello")); Matcher
m2 = std::string("hi"); EXPECT_TRUE(m2.Matches("hi")); EXPECT_FALSE(m2.Matches("hello")); } #if GTEST_HAS_GLOBAL_STRING // Tests that a ::string object can be implicitly converted to a // Matcher
or Matcher
. TEST(StringMatcherTest, CanBeImplicitlyConstructedFromGlobalString) { Matcher
m1 = ::string("hi"); EXPECT_TRUE(m1.Matches("hi")); EXPECT_FALSE(m1.Matches("hello")); Matcher
m2 = ::string("hi"); EXPECT_TRUE(m2.Matches("hi")); EXPECT_FALSE(m2.Matches("hello")); } #endif // GTEST_HAS_GLOBAL_STRING #if GTEST_HAS_GLOBAL_STRING // Tests that a C-string literal can be implicitly converted to a // Matcher<::string> or Matcher
. TEST(GlobalStringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { Matcher< ::string> m1 = "hi"; EXPECT_TRUE(m1.Matches("hi")); EXPECT_FALSE(m1.Matches("hello")); Matcher
m2 = "hi"; EXPECT_TRUE(m2.Matches("hi")); EXPECT_FALSE(m2.Matches("hello")); } // Tests that a std::string object can be implicitly converted to a // Matcher<::string> or Matcher
. TEST(GlobalStringMatcherTest, CanBeImplicitlyConstructedFromString) { Matcher< ::string> m1 = std::string("hi"); EXPECT_TRUE(m1.Matches("hi")); EXPECT_FALSE(m1.Matches("hello")); Matcher
m2 = std::string("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
. TEST(GlobalStringMatcherTest, CanBeImplicitlyConstructedFromGlobalString) { Matcher< ::string> m1 = ::string("hi"); EXPECT_TRUE(m1.Matches("hi")); EXPECT_FALSE(m1.Matches("hello")); Matcher
m2 = ::string("hi"); EXPECT_TRUE(m2.Matches("hi")); EXPECT_FALSE(m2.Matches("hello")); } #endif // GTEST_HAS_GLOBAL_STRING #if GTEST_HAS_ABSL // Tests that a C-string literal can be implicitly converted to a // Matcher
or Matcher
. TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { Matcher
m1 = "cats"; EXPECT_TRUE(m1.Matches("cats")); EXPECT_FALSE(m1.Matches("dogs")); Matcher
m2 = "cats"; EXPECT_TRUE(m2.Matches("cats")); EXPECT_FALSE(m2.Matches("dogs")); } // Tests that a std::string object can be implicitly converted to a // Matcher
or Matcher
. TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) { Matcher
m1 = std::string("cats"); EXPECT_TRUE(m1.Matches("cats")); EXPECT_FALSE(m1.Matches("dogs")); Matcher
m2 = std::string("cats"); EXPECT_TRUE(m2.Matches("cats")); EXPECT_FALSE(m2.Matches("dogs")); } #if GTEST_HAS_GLOBAL_STRING // Tests that a ::string object can be implicitly converted to a // Matcher
or Matcher
. TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromGlobalString) { Matcher
m1 = ::string("cats"); EXPECT_TRUE(m1.Matches("cats")); EXPECT_FALSE(m1.Matches("dogs")); Matcher
m2 = ::string("cats"); EXPECT_TRUE(m2.Matches("cats")); EXPECT_FALSE(m2.Matches("dogs")); } #endif // GTEST_HAS_GLOBAL_STRING // Tests that a absl::string_view object can be implicitly converted to a // Matcher
or Matcher
. TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) { Matcher
m1 = absl::string_view("cats"); EXPECT_TRUE(m1.Matches("cats")); EXPECT_FALSE(m1.Matches("dogs")); Matcher
m2 = absl::string_view("cats"); EXPECT_TRUE(m2.Matches("cats")); EXPECT_FALSE(m2.Matches("dogs")); } #endif // GTEST_HAS_ABSL // Tests that a std::reference_wrapper
object can be implicitly // converted to a Matcher
or Matcher
via Eq(). TEST(StringMatcherTest, CanBeImplicitlyConstructedFromEqReferenceWrapperString) { std::string value = "cats"; Matcher
m1 = Eq(std::ref(value)); EXPECT_TRUE(m1.Matches("cats")); EXPECT_FALSE(m1.Matches("dogs")); Matcher
m2 = Eq(std::ref(value)); EXPECT_TRUE(m2.Matches("cats")); EXPECT_FALSE(m2.Matches("dogs")); } // Tests that MakeMatcher() constructs a Matcher
from a // MatcherInterface* without requiring the user to explicitly // write the type. TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { const MatcherInterface
* dummy_impl = nullptr; Matcher
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
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
where T is the argument's type. PolymorphicMatcher
ReferencesBarOrIsZero() { return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); } TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) { // Using a polymorphic matcher to match a reference type. Matcher
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
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
bool MatchAndExplain(const T& x, MatchResultListener* listener) const { // Verifies that we can stream to the listener directly. *listener << "% " << 2; if (listener->stream() != nullptr) { // Verifies that we can stream to the listener's underlying stream // too. *listener->stream() << " == " << (x % 2); } return (x % 2) == 0; } }; PolymorphicMatcher
PolymorphicIsEven() { return MakePolymorphicMatcher(PolymorphicIsEvenImpl()); } TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) { // Using PolymorphicIsEven() as a Matcher
. const Matcher
m1 = PolymorphicIsEven(); EXPECT_TRUE(m1.Matches(42)); EXPECT_FALSE(m1.Matches(43)); EXPECT_EQ("is even", Describe(m1)); const Matcher
not_m1 = Not(m1); EXPECT_EQ("is odd", Describe(not_m1)); EXPECT_EQ("% 2 == 0", Explain(m1, 42)); // Using PolymorphicIsEven() as a Matcher
. const Matcher
m2 = PolymorphicIsEven(); EXPECT_TRUE(m2.Matches('\x42')); EXPECT_FALSE(m2.Matches('\x43')); EXPECT_EQ("is even", Describe(m2)); const Matcher
not_m2 = Not(m2); EXPECT_EQ("is odd", Describe(not_m2)); EXPECT_EQ("% 2 == 0", Explain(m2, '\x42')); } // Tests that MatcherCast
(m) works when m is a polymorphic matcher. TEST(MatcherCastTest, FromPolymorphicMatcher) { Matcher
m = MatcherCast
(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
(m) works when m is a Matcher
where T // can be statically converted to U. TEST(MatcherCastTest, FromCompatibleType) { Matcher
m1 = Eq(2.0); Matcher
m2 = MatcherCast
(m1); EXPECT_TRUE(m2.Matches(2)); EXPECT_FALSE(m2.Matches(3)); Matcher
m3 = Truly(IsPositiveIntValue); Matcher
m4 = MatcherCast
(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
(m) works when m is a Matcher
. TEST(MatcherCastTest, FromConstReferenceToNonReference) { Matcher
m1 = Eq(0); Matcher
m2 = MatcherCast
(m1); EXPECT_TRUE(m2.Matches(0)); EXPECT_FALSE(m2.Matches(1)); } // Tests that MatcherCast
(m) works when m is a Matcher
. TEST(MatcherCastTest, FromReferenceToNonReference) { Matcher
m1 = Eq(0); Matcher
m2 = MatcherCast
(m1); EXPECT_TRUE(m2.Matches(0)); EXPECT_FALSE(m2.Matches(1)); } // Tests that MatcherCast
(m) works when m is a Matcher
. TEST(MatcherCastTest, FromNonReferenceToConstReference) { Matcher
m1 = Eq(0); Matcher
m2 = MatcherCast
(m1); EXPECT_TRUE(m2.Matches(0)); EXPECT_FALSE(m2.Matches(1)); } // Tests that MatcherCast
(m) works when m is a Matcher
. TEST(MatcherCastTest, FromNonReferenceToReference) { Matcher
m1 = Eq(0); Matcher
m2 = MatcherCast
(m1); int n = 0; EXPECT_TRUE(m2.Matches(n)); n = 1; EXPECT_FALSE(m2.Matches(n)); } // Tests that MatcherCast
(m) works when m is a Matcher
. TEST(MatcherCastTest, FromSameType) { Matcher
m1 = Eq(0); Matcher
m2 = MatcherCast
(m1); EXPECT_TRUE(m2.Matches(0)); EXPECT_FALSE(m2.Matches(1)); } // Tests that MatcherCast
(m) works when m is a value of the same type as the // value type of the Matcher. TEST(MatcherCastTest, FromAValue) { Matcher
m = MatcherCast
(42); EXPECT_TRUE(m.Matches(42)); EXPECT_FALSE(m.Matches(239)); } // Tests that MatcherCast
(m) works when m is a value of the type implicitly // convertible to the value type of the Matcher. TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) { const int kExpected = 'c'; Matcher
m = MatcherCast
('c'); EXPECT_TRUE(m.Matches(kExpected)); EXPECT_FALSE(m.Matches(kExpected + 1)); } struct NonImplicitlyConstructibleTypeWithOperatorEq { friend bool operator==( const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */, int rhs) { return 42 == rhs; } friend bool operator==( int lhs, const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */) { return lhs == 42; } }; // Tests that MatcherCast
(m) works when m is a neither a matcher nor // implicitly convertible to the value type of the Matcher, but the value type // of the matcher has operator==() overload accepting m. TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) { Matcher
m1 = MatcherCast
(42); EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq())); Matcher
m2 = MatcherCast
(239); EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq())); // When updating the following lines please also change the comment to // namespace convertible_from_any. Matcher
m3 = MatcherCast
(NonImplicitlyConstructibleTypeWithOperatorEq()); EXPECT_TRUE(m3.Matches(42)); EXPECT_FALSE(m3.Matches(239)); } // ConvertibleFromAny does not work with MSVC. resulting in // error C2440: 'initializing': cannot convert from 'Eq' to 'M' // No constructor could take the source type, or constructor overload // resolution was ambiguous #if !defined _MSC_VER // The below ConvertibleFromAny struct is implicitly constructible from anything // and when in the same namespace can interact with other tests. In particular, // if it is in the same namespace as other tests and one removes // NonImplicitlyConstructibleTypeWithOperatorEq::operator==(int lhs, ...); // then the corresponding test still compiles (and it should not!) by implicitly // converting NonImplicitlyConstructibleTypeWithOperatorEq to ConvertibleFromAny // in m3.Matcher(). namespace convertible_from_any { // Implicitly convertible from any type. struct ConvertibleFromAny { ConvertibleFromAny(int a_value) : value(a_value) {} template
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
m = MatcherCast
(1); EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); } TEST(MatcherCastTest, FromConvertibleFromAny) { Matcher
m = MatcherCast
(Eq(ConvertibleFromAny(1))); EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); } } // namespace convertible_from_any #endif // !defined _MSC_VER struct IntReferenceWrapper { IntReferenceWrapper(const int& a_value) : value(&a_value) {} const int* value; }; bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) { return a.value == b.value; } TEST(MatcherCastTest, ValueIsNotCopied) { int n = 42; Matcher
m = MatcherCast
(n); // Verify that the matcher holds a reference to n, not to its temporary copy. EXPECT_TRUE(m.Matches(n)); } class Base { public: virtual ~Base() {} Base() {} private: GTEST_DISALLOW_COPY_AND_ASSIGN_(Base); }; class Derived : public Base { public: Derived() : Base() {} int i; }; class OtherDerived : public Base {}; // Tests that SafeMatcherCast
(m) works when m is a polymorphic matcher. TEST(SafeMatcherCastTest, FromPolymorphicMatcher) { Matcher
m2 = SafeMatcherCast
(Eq(32)); EXPECT_TRUE(m2.Matches(' ')); EXPECT_FALSE(m2.Matches('\n')); } // Tests that SafeMatcherCast
(m) works when m is a Matcher
where // T and U are arithmetic types and T can be losslessly converted to // U. TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) { Matcher
m1 = DoubleEq(1.0); Matcher
m2 = SafeMatcherCast
(m1); EXPECT_TRUE(m2.Matches(1.0f)); EXPECT_FALSE(m2.Matches(2.0f)); Matcher
m3 = SafeMatcherCast
(TypedEq
('a')); EXPECT_TRUE(m3.Matches('a')); EXPECT_FALSE(m3.Matches('b')); } // Tests that SafeMatcherCast
(m) works when m is a Matcher
where T and U // are pointers or references to a derived and a base class, correspondingly. TEST(SafeMatcherCastTest, FromBaseClass) { Derived d, d2; Matcher
m1 = Eq(&d); Matcher
m2 = SafeMatcherCast
(m1); EXPECT_TRUE(m2.Matches(&d)); EXPECT_FALSE(m2.Matches(&d2)); Matcher
m3 = Ref(d); Matcher
m4 = SafeMatcherCast
(m3); EXPECT_TRUE(m4.Matches(d)); EXPECT_FALSE(m4.Matches(d2)); } // Tests that SafeMatcherCast
(m) works when m is a Matcher
. TEST(SafeMatcherCastTest, FromConstReferenceToReference) { int n = 0; Matcher
m1 = Ref(n); Matcher
m2 = SafeMatcherCast
(m1); int n1 = 0; EXPECT_TRUE(m2.Matches(n)); EXPECT_FALSE(m2.Matches(n1)); } // Tests that MatcherCast
(m) works when m is a Matcher
. TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) { Matcher
m1 = Eq(0); Matcher
m2 = SafeMatcherCast
(m1); EXPECT_TRUE(m2.Matches(0)); EXPECT_FALSE(m2.Matches(1)); } // Tests that SafeMatcherCast
(m) works when m is a Matcher
. TEST(SafeMatcherCastTest, FromNonReferenceToReference) { Matcher
m1 = Eq(0); Matcher
m2 = SafeMatcherCast
(m1); int n = 0; EXPECT_TRUE(m2.Matches(n)); n = 1; EXPECT_FALSE(m2.Matches(n)); } // Tests that SafeMatcherCast
(m) works when m is a Matcher
. TEST(SafeMatcherCastTest, FromSameType) { Matcher
m1 = Eq(0); Matcher
m2 = SafeMatcherCast
(m1); EXPECT_TRUE(m2.Matches(0)); EXPECT_FALSE(m2.Matches(1)); } #if !defined _MSC_VER namespace convertible_from_any { TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) { Matcher
m = SafeMatcherCast
(1); EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); } TEST(SafeMatcherCastTest, FromConvertibleFromAny) { Matcher
m = SafeMatcherCast
(Eq(ConvertibleFromAny(1))); EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); } } // namespace convertible_from_any #endif // !defined _MSC_VER TEST(SafeMatcherCastTest, ValueIsNotCopied) { int n = 42; Matcher
m = SafeMatcherCast
(n); // Verify that the matcher holds a reference to n, not to its temporary copy. EXPECT_TRUE(m.Matches(n)); } TEST(ExpectThat, TakesLiterals) { EXPECT_THAT(1, 1); EXPECT_THAT(1.0, 1.0); EXPECT_THAT(std::string(), ""); } TEST(ExpectThat, TakesFunctions) { struct Helper { static void Func() {} }; void (*func)() = Helper::Func; EXPECT_THAT(func, Helper::Func); EXPECT_THAT(func, &Helper::Func); } // Tests that A
() matches any value of type T. TEST(ATest, MatchesAnyValue) { // Tests a matcher for a value type. Matcher
m1 = A
(); 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
m2 = A
(); EXPECT_TRUE(m2.Matches(a)); EXPECT_TRUE(m2.Matches(b)); } TEST(ATest, WorksForDerivedClass) { Base base; Derived derived; EXPECT_THAT(&base, A
()); // This shouldn't compile: EXPECT_THAT(&base, A
()); EXPECT_THAT(&derived, A
()); EXPECT_THAT(&derived, A
()); } // Tests that A
() describes itself properly. TEST(ATest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(A
())); } // Tests that An
() matches any value of type T. TEST(AnTest, MatchesAnyValue) { // Tests a matcher for a value type. Matcher
m1 = An
(); EXPECT_TRUE(m1.Matches(9143)); EXPECT_TRUE(m1.Matches(-1532)); // Tests a matcher for a reference type. int a = 2; int b = -6; Matcher
m2 = An
(); EXPECT_TRUE(m2.Matches(a)); EXPECT_TRUE(m2.Matches(b)); } // Tests that An
() describes itself properly. TEST(AnTest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(An
())); } // 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
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
m2 = _; EXPECT_TRUE(m2.Matches(a)); EXPECT_TRUE(m2.Matches(b)); } // Tests that _ describes itself properly. TEST(UnderscoreTest, CanDescribeSelf) { Matcher
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
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 */) const { return true; } // -Wunused-private-field: dummy accessor for `c_`. char dummy_c() { return c_; } private: char c_; }; TEST(EqTest, CanDescribeSelf) { Matcher
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
m1 = Eq(1); EXPECT_TRUE(m1.Matches(1)); EXPECT_FALSE(m1.Matches(2)); Matcher
m2 = Eq(1); EXPECT_TRUE(m2.Matches('\1')); EXPECT_FALSE(m2.Matches('a')); } // Tests that TypedEq
(v) matches values of type T that's equal to v. TEST(TypedEqTest, ChecksEqualityForGivenType) { Matcher
m1 = TypedEq
('a'); EXPECT_TRUE(m1.Matches('a')); EXPECT_FALSE(m1.Matches('b')); Matcher
m2 = TypedEq
(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
(2))); } // Tests that TypedEq
(v) has type Matcher
. // Type
::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 reference". template
struct Type { static bool IsTypeOf(const T& /* v */) { return true; } template
static void IsTypeOf(T2 v); }; TEST(TypedEqTest, HasSpecifiedType) { // Verfies that the type of TypedEq
(v) is Matcher
. Type
>::IsTypeOf(TypedEq
(5)); Type
>::IsTypeOf(TypedEq
(5)); } // Tests that Ge(v) matches anything >= v. TEST(GeTest, ImplementsGreaterThanOrEqual) { Matcher
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
m = Ge(5); EXPECT_EQ("is >= 5", Describe(m)); } // Tests that Gt(v) matches anything > v. TEST(GtTest, ImplementsGreaterThan) { Matcher
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
m = Gt(5); EXPECT_EQ("is > 5", Describe(m)); } // Tests that Le(v) matches anything <= v. TEST(LeTest, ImplementsLessThanOrEqual) { Matcher
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
m = Le(5); EXPECT_EQ("is <= 5", Describe(m)); } // Tests that Lt(v) matches anything < v. TEST(LtTest, ImplementsLessThan) { Matcher
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
m = Lt(5); EXPECT_EQ("is < 5", Describe(m)); } // Tests that Ne(v) matches anything != v. TEST(NeTest, ImplementsNotEqual) { Matcher
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
m = Ne(5); EXPECT_EQ("isn't equal to 5", Describe(m)); } class MoveOnly { public: explicit MoveOnly(int i) : i_(i) {} MoveOnly(const MoveOnly&) = delete; MoveOnly(MoveOnly&&) = default; MoveOnly& operator=(const MoveOnly&) = delete; MoveOnly& operator=(MoveOnly&&) = default; bool operator==(const MoveOnly& other) const { return i_ == other.i_; } bool operator!=(const MoveOnly& other) const { return i_ != other.i_; } bool operator<(const MoveOnly& other) const { return i_ < other.i_; } bool operator<=(const MoveOnly& other) const { return i_ <= other.i_; } bool operator>(const MoveOnly& other) const { return i_ > other.i_; } bool operator>=(const MoveOnly& other) const { return i_ >= other.i_; } private: int i_; }; struct MoveHelper { MOCK_METHOD1(Call, void(MoveOnly)); }; TEST(ComparisonBaseTest, WorksWithMoveOnly) { MoveOnly m{0}; MoveHelper helper; EXPECT_CALL(helper, Call(Eq(ByRef(m)))); helper.Call(MoveOnly(0)); EXPECT_CALL(helper, Call(Ne(ByRef(m)))); helper.Call(MoveOnly(1)); EXPECT_CALL(helper, Call(Le(ByRef(m)))); helper.Call(MoveOnly(0)); EXPECT_CALL(helper, Call(Lt(ByRef(m)))); helper.Call(MoveOnly(-1)); EXPECT_CALL(helper, Call(Ge(ByRef(m)))); helper.Call(MoveOnly(0)); EXPECT_CALL(helper, Call(Gt(ByRef(m)))); helper.Call(MoveOnly(1)); } // Tests that IsNull() matches any NULL pointer of any type. TEST(IsNullTest, MatchesNullPointer) { Matcher