summaryrefslogtreecommitdiffstats
path: root/googletest/include
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2023-07-21 17:35:31 (GMT)
committerCopybara-Service <copybara-worker@google.com>2023-07-21 17:36:25 (GMT)
commit01e18376efe643a82cff468734f87f8c60e314b6 (patch)
tree1a41ae767b41355e3fa4707143be0412aa5f625c /googletest/include
parent1ed6a8c67a0bd675149ece27bbec0ef1759854cf (diff)
downloadgoogletest-01e18376efe643a82cff468734f87f8c60e314b6.zip
googletest-01e18376efe643a82cff468734f87f8c60e314b6.tar.gz
googletest-01e18376efe643a82cff468734f87f8c60e314b6.tar.bz2
Make `AbslStringify` usage public in GoogleTest
Fixes #4314 PiperOrigin-RevId: 549986457 Change-Id: Iff74f02ab1c106696f288540e9c623d56b76e3f7
Diffstat (limited to 'googletest/include')
-rw-r--r--googletest/include/gtest/gtest-printers.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h
index 1ba5178..d1766e6 100644
--- a/googletest/include/gtest/gtest-printers.h
+++ b/googletest/include/gtest/gtest-printers.h
@@ -43,6 +43,9 @@
// 1. foo::PrintTo(const T&, ostream*)
// 2. operator<<(ostream&, const T&) defined in either foo or the
// global namespace.
+// * Prefer AbslStringify(..) to operator<<(..), per https://abseil.io/tips/215.
+// * Define foo::PrintTo(..) if the type already has AbslStringify(..), but an
+// alternative presentation in test results is of interest.
//
// However if T is an STL-style container then it is printed element-wise
// unless foo::PrintTo(const T&, ostream*) is defined. Note that
@@ -112,6 +115,10 @@
#include <utility>
#include <vector>
+#ifdef GTEST_HAS_ABSL
+#include "absl/strings/internal/has_absl_stringify.h"
+#include "absl/strings/str_cat.h"
+#endif // GTEST_HAS_ABSL
#include "gtest/internal/gtest-internal.h"
#include "gtest/internal/gtest-port.h"
@@ -260,6 +267,18 @@ struct ConvertibleToStringViewPrinter {
#endif
};
+#ifdef GTEST_HAS_ABSL
+struct ConvertibleToAbslStringifyPrinter {
+ template <
+ typename T,
+ typename = typename std::enable_if<
+ absl::strings_internal::HasAbslStringify<T>::value>::type> // NOLINT
+ static void PrintValue(const T& value, ::std::ostream* os) {
+ *os << absl::StrCat(value);
+ }
+};
+#endif // GTEST_HAS_ABSL
+
// Prints the given number of bytes in the given object to the given
// ostream.
GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
@@ -308,6 +327,9 @@ void PrintWithFallback(const T& value, ::std::ostream* os) {
using Printer = typename FindFirstPrinter<
T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
ProtobufPrinter,
+#ifdef GTEST_HAS_ABSL
+ ConvertibleToAbslStringifyPrinter,
+#endif // GTEST_HAS_ABSL
internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
ConvertibleToIntegerPrinter, ConvertibleToStringViewPrinter,
RawBytesPrinter, FallbackPrinter>::type;