From ada23475e27babd85fb9c13250243f6acfd3ffd8 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Tue, 14 Aug 2012 15:38:49 +0000 Subject: [PATCH] Makes gmock's Pointee() work for optional (by Jeffrey Yasskin). --- include/gmock/internal/gmock-internal-utils.h | 2 +- test/gmock-matchers_test.cc | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/include/gmock/internal/gmock-internal-utils.h b/include/gmock/internal/gmock-internal-utils.h index 6b6de970..d63fb223 100644 --- a/include/gmock/internal/gmock-internal-utils.h +++ b/include/gmock/internal/gmock-internal-utils.h @@ -73,7 +73,7 @@ struct PointeeOf { typedef T type; }; // NOLINT // smart pointer, or returns p itself when p is already a raw pointer. // The following default implementation is for the smart pointer case. template -inline typename Pointer::element_type* GetRawPointer(const Pointer& p) { +inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) { return p.get(); } // This overloaded version is for the raw pointer case. diff --git a/test/gmock-matchers_test.cc b/test/gmock-matchers_test.cc index 35d59fa8..6e5d5c31 100644 --- a/test/gmock-matchers_test.cc +++ b/test/gmock-matchers_test.cc @@ -2824,6 +2824,38 @@ TEST(PointeeTest, ReferenceToNonConstRawPointer) { 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_; +}; + +TEST(PointeeTest, WorksWithConstPropagatingPointers) { + const Matcher< ConstPropagatingPtr > 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(NULL));