From 6953a725fc2151eff18078f8315d92811cd4d90e Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 13 Jan 2010 05:15:07 +0000 Subject: [PATCH] Allows Field() and Property() to work when the matcher argument is a pointer passed by reference. --- include/gmock/gmock-matchers.h | 9 ++++++--- test/gmock-matchers_test.cc | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/include/gmock/gmock-matchers.h b/include/gmock/gmock-matchers.h index 09e469e5..f6e877d2 100644 --- a/include/gmock/gmock-matchers.h +++ b/include/gmock/gmock-matchers.h @@ -1784,7 +1784,8 @@ template bool MatchAndExplain(const FieldMatcher& matcher, T& value, MatchResultListener* listener) { return matcher.MatchAndExplain( - typename ::testing::internal::is_pointer::type(), value, listener); + typename ::testing::internal::is_pointer::type(), + value, listener); } // Implements the Property() matcher for matching a property @@ -1849,7 +1850,8 @@ template bool MatchAndExplain(const PropertyMatcher& matcher, T& value, MatchResultListener* listener) { return matcher.MatchAndExplain( - typename ::testing::internal::is_pointer::type(), value, listener); + typename ::testing::internal::is_pointer::type(), + value, listener); } // Type traits specifying various features of different functors for ResultOf. @@ -2018,7 +2020,8 @@ class ContainerEqMatcher { *os << "Only in actual: "; printed_header = true; } - UniversalPrinter::Print(*it, os); + UniversalPrinter:: + Print(*it, os); } } diff --git a/test/gmock-matchers_test.cc b/test/gmock-matchers_test.cc index e69844fa..e10b7275 100644 --- a/test/gmock-matchers_test.cc +++ b/test/gmock-matchers_test.cc @@ -2648,6 +2648,16 @@ TEST(FieldForPointerTest, WorksForPointerToNonConst) { 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, _); @@ -2846,6 +2856,19 @@ TEST(PropertyForPointerTest, WorksForPointerToNonConst) { 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, _);