From 5c34df512ce0fdec646a8dea5d3ba87357e874ea Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Wed, 21 Aug 2019 13:56:29 +0000 Subject: [PATCH] reland [gtest] Fix printing of StringRef and SmallString in assert messages. Renames GTEST_NO_LLVM_RAW_OSTREAM -> GTEST_NO_LLVM_SUPPORT and guards the new features behind it. This reverts commit a063bcf3ef5a879adbe9639a3c187d876eee0e66. llvm-svn: 369527 --- unittests/ADT/SmallStringTest.cpp | 10 ++++++- unittests/ADT/StringRefTest.cpp | 6 +++++ .../gtest/internal/custom/gtest-printers.h | 27 +++++++++++++++++++ .../gtest/internal/custom/raw-ostream.h | 4 +-- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/unittests/ADT/SmallStringTest.cpp b/unittests/ADT/SmallStringTest.cpp index 6868381c5cf..686215fd223 100644 --- a/unittests/ADT/SmallStringTest.cpp +++ b/unittests/ADT/SmallStringTest.cpp @@ -169,7 +169,7 @@ TEST_F(SmallStringTest, Realloc) { EXPECT_EQ("abcdyyy", theString.slice(0, 7)); } -TEST(StringRefTest, Comparisons) { +TEST_F(SmallStringTest, Comparisons) { EXPECT_EQ(-1, SmallString<10>("aab").compare("aad")); EXPECT_EQ( 0, SmallString<10>("aab").compare("aab")); EXPECT_EQ( 1, SmallString<10>("aab").compare("aaa")); @@ -203,4 +203,12 @@ TEST(StringRefTest, Comparisons) { EXPECT_EQ( 1, SmallString<10>("V8_q0").compare_numeric("V1_q0")); } +// Check gtest prints SmallString as a string instead of a container of chars. +// The code is in utils/unittest/googletest/internal/custom/gtest-printers.h +TEST_F(SmallStringTest, GTestPrinter) { + EXPECT_EQ(R"("foo")", ::testing::PrintToString(SmallString<1>("foo"))); + const SmallVectorImpl &ErasedSmallString = SmallString<1>("foo"); + EXPECT_EQ(R"("foo")", ::testing::PrintToString(ErasedSmallString)); } + +} // namespace diff --git a/unittests/ADT/StringRefTest.cpp b/unittests/ADT/StringRefTest.cpp index a45e83c1163..f341cf402a0 100644 --- a/unittests/ADT/StringRefTest.cpp +++ b/unittests/ADT/StringRefTest.cpp @@ -1055,6 +1055,12 @@ TEST(StringRefTest, StringLiteral) { EXPECT_EQ(StringRef("Bar"), Strings[1]); } +// Check gtest prints StringRef as a string instead of a container of chars. +// The code is in utils/unittest/googletest/internal/custom/gtest-printers.h +TEST(StringRefTest, GTestPrinter) { + EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo"))); +} + static_assert(is_trivially_copyable::value, "trivially copyable"); } // end anonymous namespace diff --git a/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h b/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h index 60c1ea050b6..d4ae0834100 100644 --- a/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h +++ b/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h @@ -39,4 +39,31 @@ #ifndef GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ #define GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ +#if !GTEST_NO_LLVM_SUPPORT +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringRef.h" +#include +// Printing of llvm String types. +// gtest sees these as containers of char (they have nested iterator types), +// so their operator<< is never considered unless we provide PrintTo(). +// PrintStringTo provides quotes and escaping, at the cost of a copy. +namespace llvm { +inline void PrintTo(llvm::StringRef S, std::ostream *OS) { + *OS << ::testing::PrintToString(S.str()); +} +// We need both SmallString and SmallVectorImpl overloads: +// - the SmallString template is needed as overload resolution will +// instantiate generic PrintTo rather than do derived-to-base conversion +// - but SmallVectorImpl is sometimes the actual static type, in code +// that erases the small size +template +inline void PrintTo(const SmallString &S, std::ostream *OS) { + *OS << ::testing::PrintToString(std::string(S.data(), S.size())); +} +inline void PrintTo(const SmallVectorImpl &S, std::ostream *OS) { + *OS << ::testing::PrintToString(std::string(S.data(), S.size())); +} +} // namespace llvm +#endif // !GTEST_NO_LLVM_SUPPORT + #endif // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ diff --git a/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h b/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h index a9837c5037c..72f23184abd 100644 --- a/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h +++ b/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h @@ -40,7 +40,7 @@ auto printable(const T &V) -> decltype(StreamSwitch::printable(V)) { // If raw_ostream support is enabled, we specialize for types with operator<< // that takes a raw_ostream. -#if !GTEST_NO_LLVM_RAW_OSTREAM +#if !GTEST_NO_LLVM_SUPPORT #include "llvm/ADT/Optional.h" #include "llvm/Support/raw_os_ostream.h" #include "llvm/Support/raw_ostream.h" @@ -81,6 +81,6 @@ struct StreamSwitch, } }; } // namespace llvm_gtest -#endif // !GTEST_NO_LLVM_RAW_OSTREAM +#endif // !GTEST_NO_LLVM_SUPPORT #endif // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_RAW_OSTREAM_H_