[llvm] r369527 - reland [gtest] Fix printing of StringRef and SmallString in assert messages.
Sam McCall via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 21 06:56:29 PDT 2019
Author: sammccall
Date: Wed Aug 21 06:56:29 2019
New Revision: 369527
URL: http://llvm.org/viewvc/llvm-project?rev=369527&view=rev
Log:
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.
Modified:
llvm/trunk/unittests/ADT/SmallStringTest.cpp
llvm/trunk/unittests/ADT/StringRefTest.cpp
llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h
Modified: llvm/trunk/unittests/ADT/SmallStringTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallStringTest.cpp?rev=369527&r1=369526&r2=369527&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SmallStringTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SmallStringTest.cpp Wed Aug 21 06:56:29 2019
@@ -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
Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=369527&r1=369526&r2=369527&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Wed Aug 21 06:56:29 2019
@@ -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
Modified: llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h?rev=369527&r1=369526&r2=369527&view=diff
==============================================================================
--- llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h (original)
+++ llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/gtest-printers.h Wed Aug 21 06:56:29 2019
@@ -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 <ostream>
+// 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<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_NO_LLVM_SUPPORT
+
#endif // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_
Modified: llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h?rev=369527&r1=369526&r2=369527&view=diff
==============================================================================
--- llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h (original)
+++ llvm/trunk/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h Wed Aug 21 06:56:29 2019
@@ -40,7 +40,7 @@ auto printable(const T &V) -> decltype(S
// 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<llvm::Optional<T>,
}
};
} // namespace llvm_gtest
-#endif // !GTEST_NO_LLVM_RAW_OSTREAM
+#endif // !GTEST_NO_LLVM_SUPPORT
#endif // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_RAW_OSTREAM_H_
More information about the llvm-commits
mailing list