diff --git a/llvm/unittests/ADT/SmallStringTest.cpp b/llvm/unittests/ADT/SmallStringTest.cpp --- a/llvm/unittests/ADT/SmallStringTest.cpp +++ b/llvm/unittests/ADT/SmallStringTest.cpp @@ -169,7 +169,7 @@ 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 @@ EXPECT_EQ( 1, SmallString<10>("V8_q0").compare_numeric("V1_q0")); } +// Check gtest prints SmallString as 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/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -14,6 +14,7 @@ #include "llvm/Support/Allocator.h" #include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" +#include using namespace llvm; namespace llvm { @@ -1055,6 +1056,12 @@ EXPECT_EQ(StringRef("Bar"), Strings[1]); } +// Check gtest prints StringRef as 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/llvm/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h b/llvm/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h --- a/llvm/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h +++ b/llvm/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h @@ -39,4 +39,33 @@ #ifndef GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ #define GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringRef.h" +#include + +namespace llvm { + +// 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. + +inline void PrintTo(llvm::StringRef S, std::ostream *OS) { + ::testing::internal::PrintStringTo(S.str(), OS); +} +// 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) { + ::testing::internal::PrintStringTo(std::string(S.data(), S.size()), OS); +} +inline void PrintTo(const SmallVectorImpl &S, std::ostream *OS) { + ::testing::internal::PrintStringTo(std::string(S.data(), S.size()), OS); +} + +} // namespace llvm + #endif // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_