From 79b83505bcf73bf2903ebf2e2f82cb1e1f181816 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Wed, 18 Nov 2009 00:43:37 +0000 Subject: [PATCH] Updates IsNull and NotNull matchers to work with smart pointers. --- include/gmock/gmock-matchers.h | 12 +++++------ test/gmock-matchers_test.cc | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/include/gmock/gmock-matchers.h b/include/gmock/gmock-matchers.h index 3d82279b..deb09463 100644 --- a/include/gmock/gmock-matchers.h +++ b/include/gmock/gmock-matchers.h @@ -633,12 +633,12 @@ GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to"); #undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_ -// Implements the polymorphic IsNull() matcher, which matches any +// Implements the polymorphic IsNull() matcher, which matches any raw or smart // pointer that is NULL. class IsNullMatcher { public: - template - bool Matches(T* p) const { return p == NULL; } + template + bool Matches(const Pointer& p) const { return GetRawPointer(p) == NULL; } void DescribeTo(::std::ostream* os) const { *os << "is NULL"; } void DescribeNegationTo(::std::ostream* os) const { @@ -646,12 +646,12 @@ class IsNullMatcher { } }; -// Implements the polymorphic NotNull() matcher, which matches any +// Implements the polymorphic NotNull() matcher, which matches any raw or smart // pointer that is not NULL. class NotNullMatcher { public: - template - bool Matches(T* p) const { return p != NULL; } + template + bool Matches(const Pointer& p) const { return GetRawPointer(p) != NULL; } void DescribeTo(::std::ostream* os) const { *os << "is not NULL"; } void DescribeNegationTo(::std::ostream* os) const { diff --git a/test/gmock-matchers_test.cc b/test/gmock-matchers_test.cc index 20b9387f..08cbcb64 100644 --- a/test/gmock-matchers_test.cc +++ b/test/gmock-matchers_test.cc @@ -121,6 +121,7 @@ using testing::internal::ValidateMatcherDescription; using testing::internal::kInvalidInterpolation; using testing::internal::kPercentInterpolation; using testing::internal::kTupleInterpolation; +using testing::internal::linked_ptr; using testing::internal::string; #ifdef GMOCK_HAS_REGEX @@ -715,6 +716,24 @@ TEST(IsNullTest, MatchesNullPointer) { #endif } +TEST(IsNullTest, LinkedPtr) { + const Matcher > m = IsNull(); + const linked_ptr null_p; + const linked_ptr non_null_p(new int); + + EXPECT_TRUE(m.Matches(null_p)); + EXPECT_FALSE(m.Matches(non_null_p)); +} + +TEST(IsNullTest, ReferenceToConstLinkedPtr) { + const Matcher&> m = IsNull(); + const linked_ptr null_p; + const linked_ptr 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 m = IsNull(); @@ -736,6 +755,24 @@ TEST(NotNullTest, MatchesNonNullPointer) { EXPECT_TRUE(m2.Matches("hi")); } +TEST(NotNullTest, LinkedPtr) { + const Matcher > m = NotNull(); + const linked_ptr null_p; + const linked_ptr non_null_p(new int); + + EXPECT_FALSE(m.Matches(null_p)); + EXPECT_TRUE(m.Matches(non_null_p)); +} + +TEST(NotNullTest, ReferenceToConstLinkedPtr) { + const Matcher&> m = NotNull(); + const linked_ptr null_p; + const linked_ptr 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 m = NotNull();