|
43 | 43 | // 1. foo::PrintTo(const T&, ostream*) |
44 | 44 | // 2. operator<<(ostream&, const T&) defined in either foo or the |
45 | 45 | // global namespace. |
| 46 | +// * Prefer AbslStringify(..) to operator<<(..), per https://abseil.io/tips/215. |
| 47 | +// * Define foo::PrintTo(..) if the type already has AbslStringify(..), but an |
| 48 | +// alternative presentation in test results is of interest. |
46 | 49 | // |
47 | 50 | // However if T is an STL-style container then it is printed element-wise |
48 | 51 | // unless foo::PrintTo(const T&, ostream*) is defined. Note that |
|
112 | 115 | #include <utility> |
113 | 116 | #include <vector> |
114 | 117 |
|
| 118 | +#ifdef GTEST_HAS_ABSL |
| 119 | +#include "absl/strings/internal/has_absl_stringify.h" |
| 120 | +#include "absl/strings/str_cat.h" |
| 121 | +#endif // GTEST_HAS_ABSL |
115 | 122 | #include "gtest/internal/gtest-internal.h" |
116 | 123 | #include "gtest/internal/gtest-port.h" |
117 | 124 |
|
@@ -260,6 +267,18 @@ struct ConvertibleToStringViewPrinter { |
260 | 267 | #endif |
261 | 268 | }; |
262 | 269 |
|
| 270 | +#ifdef GTEST_HAS_ABSL |
| 271 | +struct ConvertibleToAbslStringifyPrinter { |
| 272 | + template < |
| 273 | + typename T, |
| 274 | + typename = typename std::enable_if< |
| 275 | + absl::strings_internal::HasAbslStringify<T>::value>::type> // NOLINT |
| 276 | + static void PrintValue(const T& value, ::std::ostream* os) { |
| 277 | + *os << absl::StrCat(value); |
| 278 | + } |
| 279 | +}; |
| 280 | +#endif // GTEST_HAS_ABSL |
| 281 | + |
263 | 282 | // Prints the given number of bytes in the given object to the given |
264 | 283 | // ostream. |
265 | 284 | GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes, |
@@ -308,6 +327,9 @@ void PrintWithFallback(const T& value, ::std::ostream* os) { |
308 | 327 | using Printer = typename FindFirstPrinter< |
309 | 328 | T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter, |
310 | 329 | ProtobufPrinter, |
| 330 | +#ifdef GTEST_HAS_ABSL |
| 331 | + ConvertibleToAbslStringifyPrinter, |
| 332 | +#endif // GTEST_HAS_ABSL |
311 | 333 | internal_stream_operator_without_lexical_name_lookup::StreamPrinter, |
312 | 334 | ConvertibleToIntegerPrinter, ConvertibleToStringViewPrinter, |
313 | 335 | RawBytesPrinter, FallbackPrinter>::type; |
|
0 commit comments