Compare commits

...

3 Commits

Author SHA1 Message Date
udit
eada78e70f
Merge 806b89cf63a496933d721fca85d4e5db4d79cc56 into a0f06a70e3da7afa88da9527c43951bca1f7cef2 2026-07-29 15:06:03 -04:00
Abseil Team
a0f06a70e3 Make GoogleTest handle std::monostate and valueless_by_exception() for std::variant similarly to how it handles std::nullopt
Fixes: #2066
PiperOrigin-RevId: 955981465
Change-Id: I6b34dc2e634d4b727a50fb3e21e5d8e25c39a13b
2026-07-29 11:08:13 -07:00
uditDewan
806b89cf63 Print char8_t/char16_t code units that aren't code points as code units
PrintTo(char8_t) and PrintTo(char16_t) widened their argument to char32_t
and printed it with the U+XXXX notation. That notation names a code point,
but char8_t and char16_t hold code units, so it is wrong for every value
that does not encode a code point on its own: any non-ASCII char8_t (always
part of a multi-unit UTF-8 sequence) and the UTF-16 surrogates D800-DFFF.
For example, PrintTo(char8_t(0x80)) printed "U+0080", the code point of a
character that value cannot represent.

Print those values as code units instead, using the same escape-plus-code
form already used for char, unsigned char and wchar_t: char8_t(0x80) now
prints as u8'\x80' (128) and char16_t(0xD800) as u'\xD800' (55296). Values
that do encode a code point keep the U+XXXX notation.

Fixes #4762
2026-07-26 15:58:15 -04:00
3 changed files with 93 additions and 31 deletions

View File

@ -524,17 +524,14 @@ inline void PrintTo(bool x, ::std::ostream* os) {
GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
inline void PrintTo(char16_t c, ::std::ostream* os) {
// TODO(b/418738869): Incorrect for values not representing valid codepoints.
// Also see https://github.com/google/googletest/issues/4762.
PrintTo(static_cast<char32_t>(c), os);
}
// Overloads for the UTF-8 and UTF-16 code unit types. A code unit that
// encodes a code point all by itself is printed with the U+XXXX notation;
// one that does not (any non-ASCII char8_t, and the UTF-16 surrogates) is
// printed as a code unit instead, the way wchar_t is.
GTEST_API_ void PrintTo(char16_t c, ::std::ostream* os);
#ifdef __cpp_lib_char8_t
inline void PrintTo(char8_t c, ::std::ostream* os) {
// TODO(b/418738869): Incorrect for values not representing valid codepoints.
// Also see https://github.com/google/googletest/issues/4762.
PrintTo(static_cast<char32_t>(c), os);
}
GTEST_API_ void PrintTo(char8_t c, ::std::ostream* os);
#endif
// gcc/clang __{u,}int128_t
@ -945,13 +942,13 @@ template <typename T>
class [[nodiscard]] UniversalPrinter<std::optional<T>> {
public:
static void Print(const std::optional<T>& value, ::std::ostream* os) {
*os << '(';
if (!value) {
*os << "nullopt";
UniversalPrint(std::nullopt, os);
} else {
*os << '(';
UniversalPrint(*value, os);
*os << ')';
}
*os << ')';
}
};
@ -961,27 +958,38 @@ class [[nodiscard]] UniversalPrinter<std::nullopt_t> {
static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
};
struct UniversalPrinterVisitor {
template <typename T>
void operator()(const T& arg) const {
*os << "'" << GetTypeName<T>() << "(index = " << index << ")' with value ";
UniversalPrint(arg, os);
}
::std::ostream* os;
std::size_t index;
};
// Printer for std::variant
template <typename... T>
class [[nodiscard]] UniversalPrinter<std::variant<T...>> {
public:
static void Print(const std::variant<T...>& value, ::std::ostream* os) {
*os << '(';
std::visit(Visitor{os, value.index()}, value);
*os << ')';
}
private:
struct Visitor {
template <typename U>
void operator()(const U& u) const {
*os << "'" << GetTypeName<U>() << "(index = " << index
<< ")' with value ";
UniversalPrint(u, os);
if (value.valueless_by_exception()) {
*os << "(valueless)";
} else {
*os << '(';
std::visit(UniversalPrinterVisitor{os, value.index()}, value);
*os << ')';
}
::std::ostream* os;
std::size_t index;
};
}
};
// Printer for std::monostate
template <>
class [[nodiscard]] UniversalPrinter<std::monostate> {
public:
static void Print(std::monostate, ::std::ostream* os) {
*os << "(monostate)";
}
};
// UniversalPrintArray(begin, len, os) prints an array of 'len'

View File

@ -285,6 +285,28 @@ void PrintTo(char32_t c, ::std::ostream* os) {
<< static_cast<uint32_t>(c);
}
// char8_t and char16_t hold code units, not code points, so the U+XXXX
// notation only applies to the values that encode a code point on their own.
// The rest -- non-ASCII char8_t, which are always part of a multi-unit UTF-8
// sequence, and the UTF-16 surrogates -- are printed as code units.
void PrintTo(char16_t c, ::std::ostream* os) {
if (c >= 0xD800 && c <= 0xDFFF) {
PrintCharAndCodeTo(c, os);
} else {
PrintTo(static_cast<char32_t>(c), os);
}
}
#ifdef __cpp_lib_char8_t
void PrintTo(char8_t c, ::std::ostream* os) {
if (c >= 0x80) {
PrintCharAndCodeTo(c, os);
} else {
PrintTo(static_cast<char32_t>(c), os);
}
}
#endif
// gcc/clang __{u,}int128_t
#if defined(__SIZEOF_INT128__)
void PrintTo(__uint128_t v, ::std::ostream* os) {

View File

@ -396,12 +396,24 @@ TEST(PrintCharTest, UnsignedChar) {
EXPECT_EQ("'b' (98, 0x62)", Print(static_cast<unsigned char>('b')));
}
TEST(PrintCharTest, Char16) { EXPECT_EQ("U+0041", Print(u'A')); }
TEST(PrintCharTest, Char16) {
EXPECT_EQ("U+0041", Print(u'A'));
EXPECT_EQ("U+754C", Print(u''));
// Surrogates are not code points, so they are printed as code units.
EXPECT_EQ("u'\\xD800' (55296)", Print(static_cast<char16_t>(0xD800)));
EXPECT_EQ("u'\\xDFFF' (57343)", Print(static_cast<char16_t>(0xDFFF)));
}
TEST(PrintCharTest, Char32) { EXPECT_EQ("U+0041", Print(U'A')); }
#ifdef __cpp_lib_char8_t
TEST(PrintCharTest, Char8) { EXPECT_EQ("U+0041", Print(u8'A')); }
TEST(PrintCharTest, Char8) {
EXPECT_EQ("U+0041", Print(u8'A'));
// Only ASCII code units encode a code point on their own; the rest are
// printed as code units.
EXPECT_EQ("u8'\\x80' (128)", Print(static_cast<char8_t>(0x80)));
EXPECT_EQ("u8'\\xFF' (255)", Print(static_cast<char8_t>(0xFF)));
}
#endif
// Tests printing other simple, built-in types.
@ -456,7 +468,7 @@ TEST(PrintBuiltInTypeTest, Integer) {
#ifdef __cpp_lib_char8_t
EXPECT_EQ("U+0000",
Print(std::numeric_limits<char8_t>::min())); // char8_t
EXPECT_EQ("U+00FF",
EXPECT_EQ("u8'\\xFF' (255)",
Print(std::numeric_limits<char8_t>::max())); // char8_t
#endif
EXPECT_EQ("U+0000",
@ -2030,6 +2042,26 @@ TEST(PrintOneofTest, Basic) {
PrintToString(Type(NonPrintable{})));
}
TEST(PrintVariantTest, Monostate) {
EXPECT_EQ("(monostate)", PrintToString(std::monostate()));
#if GTEST_HAS_EXCEPTIONS
struct ThrowOnMove {
ThrowOnMove() = default;
ThrowOnMove(ThrowOnMove&& other) { *this = std::move(other); }
ThrowOnMove& operator=(ThrowOnMove&&) {
(void)std::vector<bool>().at(0);
return *this;
}
};
std::variant<std::monostate, ThrowOnMove> v = std::monostate();
EXPECT_EQ("('std::monostate(index = 0)' with value (monostate))",
PrintToString(v));
EXPECT_THROW(v = ThrowOnMove(), std::out_of_range);
EXPECT_EQ("(valueless)", PrintToString(v));
#endif
}
#if GTEST_INTERNAL_HAS_COMPARE_LIB
TEST(PrintOrderingTest, Basic) {
EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less));