]> granicus.if.org Git - llvm/commitdiff
[gtest] Fix printing of StringRef and SmallString in assert messages.
authorSam McCall <sam.mccall@gmail.com>
Wed, 21 Aug 2019 11:37:06 +0000 (11:37 +0000)
committerSam McCall <sam.mccall@gmail.com>
Wed, 21 Aug 2019 11:37:06 +0000 (11:37 +0000)
Summary:
These are detected by gtest as containers, and so previously printed as e.g.
  { '.' (46, 0x2E), 's' (115, 0x73), 'e' (101, 0x65), 'c' (99, 0x63), '0' (48, 0x30) },

gtest itself overloads PrintTo for std::string and friends, we use the same mechanism.

Reviewers: labath

Subscribers: dexonsmith, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D66520

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@369518 91177308-0d34-0410-b5e6-96231b3b80d8

unittests/ADT/SmallStringTest.cpp
unittests/ADT/StringRefTest.cpp
utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h

index 6868381c5cf11508f1a5b13a88b5523614f9a29b..686215fd223896259f16c0656b6a726b561f4393 100644 (file)
@@ -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<char> &ErasedSmallString = SmallString<1>("foo");
+  EXPECT_EQ(R"("foo")", ::testing::PrintToString(ErasedSmallString));
 }
+
+} // namespace
index a45e83c1163d5964ddc5a9e31a8ae2d121667971..f341cf402a0121b4206b95c869cc3b8dbb57177c 100644 (file)
@@ -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<StringRef>::value, "trivially copyable");
 
 } // end anonymous namespace
index 60c1ea050b61e93bfcf146e6111bfa50315c82ee..da2b418525cf03941db03565960edd29ff26a43b 100644 (file)
 #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 <ostream>
+
+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) {
+  *OS << ::testing::PrintToString(S.str());
+}
+// We need both SmallString<N> and SmallVectorImpl<char> overloads:
+//  - the SmallString<N> template is needed as overload resolution will
+//    instantiate generic PrintTo<T> rather than do derived-to-base conversion
+//  - but SmallVectorImpl<char> is sometimes the actual static type, in code
+//    that erases the small size
+template <unsigned N>
+inline void PrintTo(const SmallString<N> &S, std::ostream *OS) {
+  *OS << ::testing::PrintToString(std::string(S.data(), S.size()));
+}
+inline void PrintTo(const SmallVectorImpl<char> &S, std::ostream *OS) {
+  *OS << ::testing::PrintToString(std::string(S.data(), S.size()));
+}
+
+} // namespace llvm
+
 #endif  // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_