[PATCH] D66520: [gtest] Fix printing of StringRef and SmallString in assert messages.
Sam McCall via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 21 04:37:42 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL369518: [gtest] Fix printing of StringRef and SmallString in assert messages. (authored by sammccall, committed by ).
Changed prior to commit:
https://reviews.llvm.org/D66520?vs=216372&id=216373#toc
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D66520/new/
https://reviews.llvm.org/D66520
Files:
llvm/trunk/unittests/ADT/SmallStringTest.cpp
llvm/trunk/unittests/ADT/StringRefTest.cpp
llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
Index: llvm/trunk/unittests/ADT/SmallStringTest.cpp
===================================================================
--- llvm/trunk/unittests/ADT/SmallStringTest.cpp
+++ llvm/trunk/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 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: llvm/trunk/unittests/ADT/StringRefTest.cpp
===================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp
@@ -1055,6 +1055,12 @@
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: llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
===================================================================
--- llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
+++ llvm/trunk/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 <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_
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66520.216373.patch
Type: text/x-patch
Size: 3427 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190821/3c22c633/attachment.bin>
More information about the llvm-commits
mailing list