From 097407fd3cfb4d1e5f89ae242635b1f321b222bb Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Sat, 12 Jan 2019 12:26:37 -0500 Subject: Googletest export Deduplicate testing::ReferenceWrapper with std::reference_wrapper. Minor cleanups in matchers_test. PiperOrigin-RevId: 229022872 --- googlemock/include/gmock/gmock-actions.h | 34 +++------------- googlemock/test/gmock-actions_test.cc | 8 ++-- googlemock/test/gmock-matchers_test.cc | 63 ----------------------------- googletest/include/gtest/gtest-printers.h | 3 +- googletest/test/googletest-printers-test.cc | 16 +++++--- 5 files changed, 19 insertions(+), 105 deletions(-) diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index 2814125..df49fbc 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -936,33 +936,6 @@ class IgnoreResultAction { GTEST_DISALLOW_ASSIGN_(IgnoreResultAction); }; -// A ReferenceWrapper object represents a reference to type T, -// which can be either const or not. It can be explicitly converted -// from, and implicitly converted to, a T&. Unlike a reference, -// ReferenceWrapper can be copied and can survive template type -// inference. This is used to support by-reference arguments in the -// InvokeArgument(...) action. The idea was from "reference -// wrappers" in tr1, which we don't have in our source tree yet. -template -class ReferenceWrapper { - public: - // Constructs a ReferenceWrapper object from a T&. - explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT - - // Allows a ReferenceWrapper object to be implicitly converted to - // a T&. - operator T&() const { return *pointer_; } - private: - T* pointer_; -}; - -// Allows the expression ByRef(x) to be printed as a reference to x. -template -void PrintTo(const ReferenceWrapper& ref, ::std::ostream* os) { - T& value = ref; - UniversalPrinter::Print(value, os); -} - template struct WithArgsAction { InnerAction action; @@ -1219,9 +1192,12 @@ inline internal::IgnoreResultAction IgnoreResult(const A& an_action) { // where Base is a base class of Derived, just write: // // ByRef(derived) +// +// N.B. ByRef is redundant with std::ref, std::cref and std::reference_wrapper. +// However, it may still be used for consistency with ByMove(). template -inline internal::ReferenceWrapper ByRef(T& l_value) { // NOLINT - return internal::ReferenceWrapper(l_value); +inline ::std::reference_wrapper ByRef(T& l_value) { // NOLINT + return ::std::reference_wrapper(l_value); } } // namespace testing diff --git a/googlemock/test/gmock-actions_test.cc b/googlemock/test/gmock-actions_test.cc index f918410..77280f4 100644 --- a/googlemock/test/gmock-actions_test.cc +++ b/googlemock/test/gmock-actions_test.cc @@ -1164,13 +1164,12 @@ TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { // Tests ByRef(). -// Tests that ReferenceWrapper is copyable. +// Tests that the result of ByRef() is copyable. TEST(ByRefTest, IsCopyable) { const std::string s1 = "Hi"; const std::string s2 = "Hello"; - ::testing::internal::ReferenceWrapper ref_wrapper = - ByRef(s1); + auto ref_wrapper = ByRef(s1); const std::string& r1 = ref_wrapper; EXPECT_EQ(&s1, &r1); @@ -1179,8 +1178,7 @@ TEST(ByRefTest, IsCopyable) { const std::string& r2 = ref_wrapper; EXPECT_EQ(&s2, &r2); - ::testing::internal::ReferenceWrapper ref_wrapper1 = - ByRef(s1); + auto ref_wrapper1 = ByRef(s1); // Copies ref_wrapper1 to ref_wrapper. ref_wrapper = ref_wrapper1; const std::string& r3 = ref_wrapper; diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index a35942d..26b0d9d 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -80,65 +80,6 @@ using std::pair; using std::set; using std::stringstream; using std::vector; -using testing::_; -using testing::A; -using testing::AllArgs; -using testing::AllOf; -using testing::An; -using testing::AnyOf; -using testing::ByRef; -using testing::ContainsRegex; -using testing::DoubleEq; -using testing::DoubleNear; -using testing::EndsWith; -using testing::Eq; -using testing::ExplainMatchResult; -using testing::Field; -using testing::FloatEq; -using testing::FloatNear; -using testing::Ge; -using testing::Gt; -using testing::HasSubstr; -using testing::IsEmpty; -using testing::IsNull; -using testing::Key; -using testing::Le; -using testing::Lt; -using testing::MakeMatcher; -using testing::MakePolymorphicMatcher; -using testing::Matcher; -using testing::MatcherCast; -using testing::MatcherInterface; -using testing::Matches; -using testing::MatchesRegex; -using testing::MatchResultListener; -using testing::NanSensitiveDoubleEq; -using testing::NanSensitiveDoubleNear; -using testing::NanSensitiveFloatEq; -using testing::NanSensitiveFloatNear; -using testing::Ne; -using testing::Not; -using testing::NotNull; -using testing::Pair; -using testing::Pointee; -using testing::Pointwise; -using testing::PolymorphicMatcher; -using testing::Property; -using testing::Ref; -using testing::ResultOf; -using testing::SizeIs; -using testing::StartsWith; -using testing::StrCaseEq; -using testing::StrCaseNe; -using testing::StrEq; -using testing::StringMatchResultListener; -using testing::StrNe; -using testing::Truly; -using testing::TypedEq; -using testing::UnorderedPointwise; -using testing::Value; -using testing::WhenSorted; -using testing::WhenSortedBy; using testing::internal::DummyMatchResultListener; using testing::internal::ElementMatcherPair; using testing::internal::ElementMatcherPairs; @@ -6995,10 +6936,6 @@ class PredicateFormatterFromMatcherTest : public ::testing::Test { matcher); return predicate_formatter("dummy-name", behavior); } - - const std::string kMatcherType = - "testing::gmock_matchers_test::PredicateFormatterFromMatcherTest::" - "Behavior"; }; TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) { diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index 9048459..ef166b0 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -637,8 +637,7 @@ inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; } template void PrintTo(std::reference_wrapper ref, ::std::ostream* os) { - // Delegate to wrapped value. - PrintTo(ref.get(), os); + UniversalPrinter::Print(ref.get(), os); } // Helper function for printing a tuple. T must be instantiated with diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc index 961e818..5ce2a77 100644 --- a/googletest/test/googletest-printers-test.cc +++ b/googletest/test/googletest-printers-test.cc @@ -1023,16 +1023,20 @@ TEST(PrintNullptrT, Basic) { TEST(PrintReferenceWrapper, Printable) { int x = 5; - EXPECT_EQ("5", Print(std::ref(x))); - EXPECT_EQ("5", Print(std::cref(x))); + EXPECT_EQ("@" + PrintPointer(&x) + " 5", Print(std::ref(x))); + EXPECT_EQ("@" + PrintPointer(&x) + " 5", Print(std::cref(x))); } TEST(PrintReferenceWrapper, Unprintable) { ::foo::UnprintableInFoo up; - EXPECT_EQ("16-byte object ", - Print(std::ref(up))); - EXPECT_EQ("16-byte object ", - Print(std::cref(up))); + EXPECT_EQ( + "@" + PrintPointer(&up) + + " 16-byte object ", + Print(std::ref(up))); + EXPECT_EQ( + "@" + PrintPointer(&up) + + " 16-byte object ", + Print(std::cref(up))); } // Tests printing user-defined unprintable types. -- cgit v0.12