From 95b0ea2cf5046465f448f01efa7c4b764a62a4bb Mon Sep 17 00:00:00 2001 From: Krystian Kuzniarek Date: Sat, 7 Mar 2020 15:48:20 +0100 Subject: [PATCH] specialize UniversalPrinter<> for std::optional --- googletest/include/gtest/gtest-printers.h | 11 +++---- .../include/gtest/internal/gtest-port.h | 32 +++++++++++++++++++ googletest/test/googletest-printers-test.cc | 12 +++---- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index 39c72c43..4260e4f7 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -113,7 +113,6 @@ #if GTEST_HAS_ABSL #include "absl/strings/string_view.h" -#include "absl/types/optional.h" #endif // GTEST_HAS_ABSL namespace testing { @@ -680,14 +679,14 @@ class UniversalPrinter { GTEST_DISABLE_MSC_WARNINGS_POP_() }; -#if GTEST_HAS_ABSL +#if GTEST_INTERNAL_HAS_OPTIONAL -// Printer for absl::optional +// Printer for std::optional / absl::optional template -class UniversalPrinter<::absl::optional> { +class UniversalPrinter> { public: - static void Print(const ::absl::optional& value, ::std::ostream* os) { + static void Print(const Optional& value, ::std::ostream* os) { *os << '('; if (!value) { *os << "nullopt"; @@ -698,7 +697,7 @@ class UniversalPrinter<::absl::optional> { } }; -#endif // GTEST_HAS_ABSL +#endif // GTEST_INTERNAL_HAS_OPTIONAL #if GTEST_INTERNAL_HAS_VARIANT diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index ea37fe13..377c77fb 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -199,6 +199,8 @@ // suppressed (constant conditional). // GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127 // is suppressed. +// GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter or +// UniversalPrinter specializations. // GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher or // Matcher // specializations. @@ -2225,6 +2227,36 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val); #endif // !defined(GTEST_INTERNAL_DEPRECATED) +#if GTEST_HAS_ABSL +// Always use absl::optional for UniversalPrinter<> specializations if googletest +// is built with absl support. +# define GTEST_INTERNAL_HAS_OPTIONAL 1 +#include "absl/types/optional.h" +namespace testing { +namespace internal { +template +using Optional = ::absl::optional; +} // namespace internal +} // namespace testing +#else +# ifdef __has_include +# if __has_include() && __cplusplus >= 201703L +// Otherwise for C++17 and higher use std::optional for UniversalPrinter<> +// specializations. +# define GTEST_INTERNAL_HAS_OPTIONAL 1 +#include +namespace testing { +namespace internal { +template +using Optional = ::std::optional; +} // namespace internal +} // namespace testing +// The case where absl is configured NOT to alias std::optional is not +// supported. +# endif // __has_include() && __cplusplus >= 201703L +# endif // __has_include +#endif // GTEST_HAS_ABSL + #if GTEST_HAS_ABSL // Always use absl::string_view for Matcher<> specializations if googletest // is built with absl support. diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc index 8a8ca51e..d71c1871 100644 --- a/googletest/test/googletest-printers-test.cc +++ b/googletest/test/googletest-printers-test.cc @@ -1531,18 +1531,16 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) { EXPECT_EQ("\"a\"", result[1]); } -#if GTEST_HAS_ABSL - +#if GTEST_INTERNAL_HAS_OPTIONAL TEST(PrintOptionalTest, Basic) { - absl::optional value; + internal::Optional value; EXPECT_EQ("(nullopt)", PrintToString(value)); value = {7}; EXPECT_EQ("(7)", PrintToString(value)); - EXPECT_EQ("(1.1)", PrintToString(absl::optional{1.1})); - EXPECT_EQ("(\"A\")", PrintToString(absl::optional{"A"})); + EXPECT_EQ("(1.1)", PrintToString(internal::Optional{1.1})); + EXPECT_EQ("(\"A\")", PrintToString(internal::Optional{"A"})); } - -#endif // GTEST_HAS_ABSL +#endif // GTEST_INTERNAL_HAS_OPTIONAL #if GTEST_INTERNAL_HAS_VARIANT struct NonPrintable {