Skip to content

Commit 01e1837

Browse files
Abseil Teamcopybara-github
authored andcommitted
Make AbslStringify usage public in GoogleTest
Fixes #4314 PiperOrigin-RevId: 549986457 Change-Id: Iff74f02ab1c106696f288540e9c623d56b76e3f7
1 parent 1ed6a8c commit 01e1837

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

googletest/include/gtest/gtest-printers.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
// 1. foo::PrintTo(const T&, ostream*)
4444
// 2. operator<<(ostream&, const T&) defined in either foo or the
4545
// 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.
4649
//
4750
// However if T is an STL-style container then it is printed element-wise
4851
// unless foo::PrintTo(const T&, ostream*) is defined. Note that
@@ -112,6 +115,10 @@
112115
#include <utility>
113116
#include <vector>
114117

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
115122
#include "gtest/internal/gtest-internal.h"
116123
#include "gtest/internal/gtest-port.h"
117124

@@ -260,6 +267,18 @@ struct ConvertibleToStringViewPrinter {
260267
#endif
261268
};
262269

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+
263282
// Prints the given number of bytes in the given object to the given
264283
// ostream.
265284
GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
@@ -308,6 +327,9 @@ void PrintWithFallback(const T& value, ::std::ostream* os) {
308327
using Printer = typename FindFirstPrinter<
309328
T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
310329
ProtobufPrinter,
330+
#ifdef GTEST_HAS_ABSL
331+
ConvertibleToAbslStringifyPrinter,
332+
#endif // GTEST_HAS_ABSL
311333
internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
312334
ConvertibleToIntegerPrinter, ConvertibleToStringViewPrinter,
313335
RawBytesPrinter, FallbackPrinter>::type;

googletest/test/googletest-printers-test.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
#include "gtest/gtest-printers.h"
5656
#include "gtest/gtest.h"
5757

58+
#ifdef GTEST_HAS_ABSL
59+
#include "absl/strings/str_format.h"
60+
#endif
61+
5862
// Some user-defined types for testing the universal value printer.
5963

6064
// An anonymous enum type.
@@ -119,6 +123,19 @@ void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) {
119123
os << "StreamableInGlobal*";
120124
}
121125

126+
#ifdef GTEST_HAS_ABSL
127+
// A user-defined type with AbslStringify
128+
struct Point {
129+
template <typename Sink>
130+
friend void AbslStringify(Sink& sink, const Point& p) {
131+
absl::Format(&sink, "(%d, %d)", p.x, p.y);
132+
}
133+
134+
int x = 10;
135+
int y = 20;
136+
};
137+
#endif
138+
122139
namespace foo {
123140

124141
// A user-defined unprintable type in a user namespace.
@@ -317,6 +334,11 @@ TEST(PrintEnumTest, EnumWithPrintTo) {
317334
EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
318335
}
319336

337+
#ifdef GTEST_HAS_ABSL
338+
// Tests printing a class that defines AbslStringify
339+
TEST(PrintClassTest, AbslStringify) { EXPECT_EQ("(10, 20)", Print(Point())); }
340+
#endif
341+
320342
// Tests printing a class implicitly convertible to BiggestInt.
321343

322344
TEST(PrintClassTest, BiggestIntConvertible) {
@@ -1636,6 +1658,12 @@ TEST(PrintToStringTest, PrintReferenceToStreamableInGlobal) {
16361658
EXPECT_STREQ("StreamableInGlobal", PrintToString(r).c_str());
16371659
}
16381660

1661+
#ifdef GTEST_HAS_ABSL
1662+
TEST(PrintToStringTest, AbslStringify) {
1663+
EXPECT_PRINT_TO_STRING_(Point(), "(10, 20)");
1664+
}
1665+
#endif
1666+
16391667
TEST(IsValidUTF8Test, IllFormedUTF8) {
16401668
// The following test strings are ill-formed UTF-8 and are printed
16411669
// as hex only (or ASCII, in case of ASCII bytes) because IsValidUTF8() is

0 commit comments

Comments
 (0)