From 33b44c4b35b5b24624c1203001e36df2dbab346b Mon Sep 17 00:00:00 2001 From: Krystian Kuzniarek Date: Sat, 7 Mar 2020 15:41:43 +0100 Subject: [PATCH] specialize UniversalPrinter<> for std::variant --- googletest/include/gtest/gtest-printers.h | 22 +++++++++---- .../include/gtest/internal/gtest-port.h | 32 +++++++++++++++++++ googletest/test/googletest-printers-test.cc | 13 +++++--- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index 407d1f18..39c72c43 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -114,7 +114,6 @@ #if GTEST_HAS_ABSL #include "absl/strings/string_view.h" #include "absl/types/optional.h" -#include "absl/types/variant.h" #endif // GTEST_HAS_ABSL namespace testing { @@ -699,14 +698,22 @@ class UniversalPrinter<::absl::optional> { } }; -// Printer for absl::variant +#endif // GTEST_HAS_ABSL + +#if GTEST_INTERNAL_HAS_VARIANT + +// Printer for std::variant / absl::variant template -class UniversalPrinter<::absl::variant> { +class UniversalPrinter> { public: - static void Print(const ::absl::variant& value, ::std::ostream* os) { + static void Print(const Variant& value, ::std::ostream* os) { *os << '('; - absl::visit(Visitor{os}, value); +#if GTEST_HAS_ABSL + absl::visit(Visitor{os, value.index()}, value); +#else + std::visit(Visitor{os, value.index()}, value); +#endif // GTEST_HAS_ABSL *os << ')'; } @@ -714,14 +721,15 @@ class UniversalPrinter<::absl::variant> { struct Visitor { template void operator()(const U& u) const { - *os << "'" << GetTypeName() << "' with value "; + *os << "'" << GetTypeName() << "(" << index << ")' with value "; UniversalPrint(u, os); } ::std::ostream* os; + std::size_t index; }; }; -#endif // GTEST_HAS_ABSL +#endif // GTEST_INTERNAL_HAS_VARIANT // UniversalPrintArray(begin, len, os) prints an array of 'len' // elements, starting at address 'begin'. diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 60ff4716..ea37fe13 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -202,6 +202,8 @@ // GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher or // Matcher // specializations. +// GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter or +// UniversalPrinter specializations. // // Synchronization: // Mutex, MutexLock, ThreadLocal, GetThreadCount() @@ -2251,4 +2253,34 @@ using StringView = ::std::string_view; # endif // __has_include #endif // GTEST_HAS_ABSL +#if GTEST_HAS_ABSL +// Always use absl::variant for UniversalPrinter<> specializations if googletest +// is built with absl support. +# define GTEST_INTERNAL_HAS_VARIANT 1 +#include "absl/types/variant.h" +namespace testing { +namespace internal { +template +using Variant = ::absl::variant; +} // namespace internal +} // namespace testing +#else +# ifdef __has_include +# if __has_include() && __cplusplus >= 201703L +// Otherwise for C++17 and higher use std::variant for UniversalPrinter<> +// specializations. +# define GTEST_INTERNAL_HAS_VARIANT 1 +#include +namespace testing { +namespace internal { +template +using Variant = ::std::variant; +} // namespace internal +} // namespace testing +// The case where absl is configured NOT to alias std::variant is not +// supported. +# endif // __has_include() && __cplusplus >= 201703L +# endif // __has_include +#endif // GTEST_HAS_ABSL + #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc index ddf2c0e1..8a8ca51e 100644 --- a/googletest/test/googletest-printers-test.cc +++ b/googletest/test/googletest-printers-test.cc @@ -1542,21 +1542,24 @@ TEST(PrintOptionalTest, Basic) { EXPECT_EQ("(\"A\")", PrintToString(absl::optional{"A"})); } +#endif // GTEST_HAS_ABSL + +#if GTEST_INTERNAL_HAS_VARIANT struct NonPrintable { unsigned char contents = 17; }; TEST(PrintOneofTest, Basic) { - using Type = absl::variant; - EXPECT_EQ("('int' with value 7)", PrintToString(Type(7))); - EXPECT_EQ("('StreamableInGlobal' with value StreamableInGlobal)", + using Type = internal::Variant; + EXPECT_EQ("('int(0)' with value 7)", PrintToString(Type(7))); + EXPECT_EQ("('StreamableInGlobal(1)' with value StreamableInGlobal)", PrintToString(Type(StreamableInGlobal{}))); EXPECT_EQ( - "('testing::gtest_printers_test::NonPrintable' with value 1-byte object " + "('testing::gtest_printers_test::NonPrintable(2)' with value 1-byte object " "<11>)", PrintToString(Type(NonPrintable{}))); } -#endif // GTEST_HAS_ABSL +#endif // GTEST_INTERNAL_HAS_VARIANT namespace { class string_ref;