From 983d1e34f3014b3aa84579610811ef0ab0599134 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Wed, 11 Jan 2017 00:16:03 +0000 Subject: [PATCH] [gmock] Teach gmock ElementsAre and BeginEndDistanceIs matchers to handle generic ranges by using std::begin and std::end rather than requiring things to look exactly like an STL container. Much of the credit for this goes to Dave Blaikie who helped me figure out the right incantations. This will probably be re-designed when I send this to the maintainers of gmock, so I've instead structured it to change is little as possible while it is a local patch. That makes it somewhat ugly, but I think a focused change is better for getting this to work for LLVM today and letting the upstream maintainers figure out the correct long-term pattern. Differential Revision: https://reviews.llvm.org/D28288 llvm-svn: 291623 --- .../googlemock/include/gmock/gmock-matchers.h | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/utils/unittest/googlemock/include/gmock/gmock-matchers.h b/utils/unittest/googlemock/include/gmock/gmock-matchers.h index 33b37a7a5d6..749a30e4e6d 100644 --- a/utils/unittest/googlemock/include/gmock/gmock-matchers.h +++ b/utils/unittest/googlemock/include/gmock/gmock-matchers.h @@ -2462,11 +2462,14 @@ class BeginEndDistanceIsMatcher { template class Impl : public MatcherInterface { public: - typedef internal::StlContainerView< - GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView; + typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; + typedef internal::StlContainerView View; + typedef typename View::type StlContainer; + typedef typename View::const_reference StlContainerReference; + typedef decltype(std::begin( + std::declval())) StlContainerConstIterator; typedef typename std::iterator_traits< - typename ContainerView::type::const_iterator>::difference_type - DistanceType; + StlContainerConstIterator>::difference_type DistanceType; explicit Impl(const DistanceMatcher& distance_matcher) : distance_matcher_(MatcherCast(distance_matcher)) {} @@ -3111,7 +3114,10 @@ class ElementsAreMatcherImpl : public MatcherInterface { typedef internal::StlContainerView View; typedef typename View::type StlContainer; typedef typename View::const_reference StlContainerReference; - typedef typename StlContainer::value_type Element; + typedef decltype(std::begin( + std::declval())) StlContainerConstIterator; + typedef typename std::remove_reference())>::type Element; // Constructs the matcher from a sequence of element values or // element matchers. @@ -3168,7 +3174,7 @@ class ElementsAreMatcherImpl : public MatcherInterface { // explanations[i] is the explanation of the element at index i. ::std::vector explanations(count()); StlContainerReference stl_container = View::ConstReference(container); - typename StlContainer::const_iterator it = stl_container.begin(); + StlContainerConstIterator it = stl_container.begin(); size_t exam_pos = 0; bool mismatch_found = false; // Have we found a mismatched element yet? @@ -3350,8 +3356,10 @@ class UnorderedElementsAreMatcherImpl typedef internal::StlContainerView View; typedef typename View::type StlContainer; typedef typename View::const_reference StlContainerReference; - typedef typename StlContainer::const_iterator StlContainerConstIterator; - typedef typename StlContainer::value_type Element; + typedef decltype(std::begin( + std::declval())) StlContainerConstIterator; + typedef typename std::remove_reference())>::type Element; // Constructs the matcher from a sequence of element values or // element matchers. @@ -3456,8 +3464,12 @@ class UnorderedElementsAreMatcher { template operator Matcher() const { typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; - typedef typename internal::StlContainerView::type View; - typedef typename View::value_type Element; + typedef internal::StlContainerView View; + typedef typename View::const_reference StlContainerReference; + typedef decltype(std::begin( + std::declval())) StlContainerConstIterator; + typedef typename std::remove_reference())>::type Element; typedef ::std::vector > MatcherVec; MatcherVec matchers; matchers.reserve(::testing::tuple_size::value); @@ -3481,8 +3493,12 @@ class ElementsAreMatcher { template operator Matcher() const { typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; - typedef typename internal::StlContainerView::type View; - typedef typename View::value_type Element; + typedef internal::StlContainerView View; + typedef typename View::const_reference StlContainerReference; + typedef decltype(std::begin( + std::declval())) StlContainerConstIterator; + typedef typename std::remove_reference())>::type Element; typedef ::std::vector > MatcherVec; MatcherVec matchers; matchers.reserve(::testing::tuple_size::value);