From cb3f7ce1deef5084306c929528a9728afdb17164 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 25 Sep 2019 18:40:52 -0400 Subject: Googletest export Makes testing::ResultOf() work with non-copyable arguments. PiperOrigin-RevId: 271222632 --- googlemock/include/gmock/gmock-matchers.h | 6 ++++-- googlemock/test/gmock-matchers_test.cc | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index bf9eb20..28e188b 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -1858,7 +1858,9 @@ struct CallableTraits { static void CheckIsValid(Functor /* functor */) {} template - static auto Invoke(Functor f, T arg) -> decltype(f(arg)) { return f(arg); } + static auto Invoke(Functor f, const T& arg) -> decltype(f(arg)) { + return f(arg); + } }; // Specialization for function pointers. @@ -1889,7 +1891,7 @@ class ResultOfMatcher { template operator Matcher() const { - return Matcher(new Impl(callable_, matcher_)); + return Matcher(new Impl(callable_, matcher_)); } private: diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index 88f144d..0373526 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -4318,6 +4318,16 @@ TEST(ResultOfTest, WorksForLambdas) { EXPECT_FALSE(matcher.Matches(1)); } +TEST(ResultOfTest, WorksForNonCopyableArguments) { + Matcher> matcher = ResultOf( + [](const std::unique_ptr& str_len) { + return std::string(static_cast(*str_len), 'x'); + }, + "xxx"); + EXPECT_TRUE(matcher.Matches(std::unique_ptr(new int(3)))); + EXPECT_FALSE(matcher.Matches(std::unique_ptr(new int(1)))); +} + const int* ReferencingFunction(const int& n) { return &n; } struct ReferencingFunctor { -- cgit v0.12