Googletest export

Adds support for printing the types char8_t, char16_t, and char32_t

This changes prints these types as Unicode code points. It is possible
that there is a better way of printing these types, but that change is
more complex, and the format in which Googletest prints these types is
subject to change if someone implements a better way of printing them.

This fixes the C++20 build, which removed support for printing these types.
https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2

Fixes #2854

PiperOrigin-RevId: 314826912
This commit is contained in:
dmauro 2020-06-04 19:34:01 -04:00 committed by Gennadiy Rozental
parent 07d4a6e93d
commit 4fe018038f
3 changed files with 117 additions and 0 deletions

View File

@ -360,6 +360,14 @@ GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
#ifdef __cpp_char8_t
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t);
#endif
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t);
#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
@ -377,6 +385,14 @@ GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
#ifdef __cpp_char8_t
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string);
#endif
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string);
#if GTEST_HAS_STD_WSTRING
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
@ -453,6 +469,16 @@ inline void PrintTo(bool x, ::std::ostream* os) {
// is implemented as an unsigned type.
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) {
PrintTo(ImplicitCast_<char32_t>(c), os);
}
#ifdef __cpp_char8_t
inline void PrintTo(char8_t c, ::std::ostream* os) {
PrintTo(ImplicitCast_<char32_t>(c), os);
}
#endif
// Overloads for C strings.
GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
inline void PrintTo(char* s, ::std::ostream* os) {

View File

@ -251,6 +251,11 @@ void PrintTo(wchar_t wc, ostream* os) {
PrintCharAndCodeTo<wchar_t>(wc, os);
}
void PrintTo(char32_t c, ::std::ostream* os) {
*os << std::hex << "U+" << std::uppercase << std::setfill('0') << std::setw(4)
<< c;
}
// Prints the given array of characters to the ostream. CharType must be either
// char or wchar_t.
// The array starts at begin, the length is len, it may include '\0' characters

View File

@ -310,6 +310,20 @@ TEST(PrintCharTest, UnsignedChar) {
Print(static_cast<unsigned char>('b')));
}
TEST(PrintCharTest, Char16) {
EXPECT_EQ("U+0041", Print(u'A'));
}
TEST(PrintCharTest, Char32) {
EXPECT_EQ("U+0041", Print(U'A'));
}
#ifdef __cpp_char8_t
TEST(PrintCharTest, Char8) {
EXPECT_EQ("U+0041", Print(u8'A'));
}
#endif
// Tests printing other simple, built-in types.
// bool.
@ -359,6 +373,20 @@ TEST(PrintBuiltInTypeTest, Integer) {
Print(std::numeric_limits<uint64_t>::max())); // uint64
EXPECT_EQ("-9223372036854775808",
Print(std::numeric_limits<int64_t>::min())); // int64
#ifdef __cpp_char8_t
EXPECT_EQ("U+0000",
Print(std::numeric_limits<char8_t>::min())); // char8_t
EXPECT_EQ("U+00FF",
Print(std::numeric_limits<char8_t>::max())); // char8_t
#endif
EXPECT_EQ("U+0000",
Print(std::numeric_limits<char16_t>::min())); // char16_t
EXPECT_EQ("U+FFFF",
Print(std::numeric_limits<char16_t>::max())); // char16_t
EXPECT_EQ("U+0000",
Print(std::numeric_limits<char32_t>::min())); // char32_t
EXPECT_EQ("U+FFFFFFFF",
Print(std::numeric_limits<char32_t>::max())); // char32_t
}
// Size types.
@ -643,6 +671,35 @@ TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
}
#ifdef __cpp_char8_t
// char8_t array.
TEST(PrintArrayTest, Char8Array) {
const char8_t a[] = u8"Hello, world!";
EXPECT_EQ(
"{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+0077, "
"U+006F, U+0072, U+006C, U+0064, U+0021, U+0000 }",
PrintArrayHelper(a));
}
#endif
// char16_t array.
TEST(PrintArrayTest, Char16Array) {
const char16_t a[] = u"Hello, 世界";
EXPECT_EQ(
"{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+4E16, "
"U+754C, U+0000 }",
PrintArrayHelper(a));
}
// char32_t array.
TEST(PrintArrayTest, Char32Array) {
const char32_t a[] = U"Hello, 世界";
EXPECT_EQ(
"{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+4E16, "
"U+754C, U+0000 }",
PrintArrayHelper(a));
}
// Array of objects.
TEST(PrintArrayTest, ObjectArray) {
std::string a[3] = {"Hi", "Hello", "Ni hao"};
@ -702,6 +759,35 @@ TEST(PrintWideStringTest, StringAmbiguousHex) {
}
#endif // GTEST_HAS_STD_WSTRING
#ifdef __cpp_char8_t
TEST(PrintStringTest, U8String) {
std::u8string str = u8"Hello, world!";
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
EXPECT_EQ(
"{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+0077, "
"U+006F, U+0072, U+006C, U+0064, U+0021 }",
Print(str));
}
#endif
TEST(PrintStringTest, U16String) {
std::u16string str = u"Hello, 世界";
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
EXPECT_EQ(
"{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+4E16, "
"U+754C }",
Print(str));
}
TEST(PrintStringTest, U32String) {
std::u32string str = U"Hello, 世界";
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
EXPECT_EQ(
"{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+4E16, "
"U+754C }",
Print(str));
}
// Tests printing types that support generic streaming (i.e. streaming
// to std::basic_ostream<Char, CharTraits> for any valid Char and
// CharTraits types).