From af058521adb3a12c0e2d69a219f81a133b244ee6 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 9 Feb 2021 11:37:05 -0500 Subject: [PATCH] Googletest export Fix #2987 Removing const before passing any types through UniversalPrinter. PiperOrigin-RevId: 356508875 --- googletest/include/gtest/gtest-printers.h | 4 +++ googletest/test/googletest-printers-test.cc | 34 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index 0a8da3bd..cd094949 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -677,6 +677,10 @@ class UniversalPrinter { GTEST_DISABLE_MSC_WARNINGS_POP_() }; +// Remove any const-qualifiers before passing a type to UniversalPrinter. +template +class UniversalPrinter : public UniversalPrinter {}; + #if GTEST_INTERNAL_HAS_ANY // Printer for std::any / absl::any diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc index 0653d9e1..8247d4e1 100644 --- a/googletest/test/googletest-printers-test.cc +++ b/googletest/test/googletest-printers-test.cc @@ -229,6 +229,33 @@ class PathLike { } // namespace foo namespace testing { +namespace { +template +class Wrapper { + public: + explicit Wrapper(T&& value) : value_(std::forward(value)) {} + + const T& value() const { return value_; } + + private: + T value_; +}; + +} // namespace + +namespace internal { +template +class UniversalPrinter> { + public: + static void Print(const Wrapper& w, ::std::ostream* os) { + *os << "Wrapper("; + UniversalPrint(w.value(), os); + *os << ')'; + } +}; +} // namespace internal + + namespace gtest_printers_test { using ::std::deque; @@ -1667,6 +1694,13 @@ TEST(UniversalPrintTest, WorksForReference) { EXPECT_EQ("123", ss.str()); } +TEST(UniversalPrintTest, WorksForPairWithConst) { + std::pair, int> p(Wrapper("abc"), 1); + ::std::stringstream ss; + UniversalPrint(p, &ss); + EXPECT_EQ("(Wrapper(\"abc\"), 1)", ss.str()); +} + TEST(UniversalPrintTest, WorksForCString) { const char* s1 = "abc"; ::std::stringstream ss1;