llvm.org GIT mirror llvm / 07be7d1
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@369527 91177308-0d34-0410-b5e6-96231b3b80d8 Sam McCall 27 days ago
4 changed file(s) with 45 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
168168 EXPECT_EQ("abcdyyy", theString.slice(0, 7));
169169 }
170170
171 TEST(StringRefTest, Comparisons) {
171 TEST_F(SmallStringTest, Comparisons) {
172172 EXPECT_EQ(-1, SmallString<10>("aab").compare("aad"));
173173 EXPECT_EQ( 0, SmallString<10>("aab").compare("aab"));
174174 EXPECT_EQ( 1, SmallString<10>("aab").compare("aaa"));
202202 EXPECT_EQ( 1, SmallString<10>("V8_q0").compare_numeric("V1_q0"));
203203 }
204204
205 }
205 // Check gtest prints SmallString as a string instead of a container of chars.
206 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h
207 TEST_F(SmallStringTest, GTestPrinter) {
208 EXPECT_EQ(R"("foo")", ::testing::PrintToString(SmallString<1>("foo")));
209 const SmallVectorImpl &ErasedSmallString = SmallString<1>("foo");
210 EXPECT_EQ(R"("foo")", ::testing::PrintToString(ErasedSmallString));
211 }
212
213 } // namespace
10541054 EXPECT_EQ(StringRef("Bar"), Strings[1]);
10551055 }
10561056
1057 // Check gtest prints StringRef as a string instead of a container of chars.
1058 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h
1059 TEST(StringRefTest, GTestPrinter) {
1060 EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo")));
1061 }
1062
10571063 static_assert(is_trivially_copyable::value, "trivially copyable");
10581064
10591065 } // end anonymous namespace
3838 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_
3939 #define GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_
4040
41 #if !GTEST_NO_LLVM_SUPPORT
42 #include "llvm/ADT/SmallString.h"
43 #include "llvm/ADT/StringRef.h"
44 #include
45 // Printing of llvm String types.
46 // gtest sees these as containers of char (they have nested iterator types),
47 // so their operator<< is never considered unless we provide PrintTo().
48 // PrintStringTo provides quotes and escaping, at the cost of a copy.
49 namespace llvm {
50 inline void PrintTo(llvm::StringRef S, std::ostream *OS) {
51 *OS << ::testing::PrintToString(S.str());
52 }
53 // We need both SmallString and SmallVectorImpl overloads:
54 // - the SmallString template is needed as overload resolution will
55 // instantiate generic PrintTo rather than do derived-to-base conversion
56 // - but SmallVectorImpl is sometimes the actual static type, in code
57 // that erases the small size
58 template
59 inline void PrintTo(const SmallString &S, std::ostream *OS) {
60 *OS << ::testing::PrintToString(std::string(S.data(), S.size()));
61 }
62 inline void PrintTo(const SmallVectorImpl &S, std::ostream *OS) {
63 *OS << ::testing::PrintToString(std::string(S.data(), S.size()));
64 }
65 } // namespace llvm
66 #endif // !GTEST_NO_LLVM_SUPPORT
67
4168 #endif // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_
3939
4040 // If raw_ostream support is enabled, we specialize for types with operator<<
4141 // that takes a raw_ostream.
42 #if !GTEST_NO_LLVM_RAW_OSTREAM
42 #if !GTEST_NO_LLVM_SUPPORT
4343 #include "llvm/ADT/Optional.h"
4444 #include "llvm/Support/raw_os_ostream.h"
4545 #include "llvm/Support/raw_ostream.h"
8080 }
8181 };
8282 } // namespace llvm_gtest
83 #endif // !GTEST_NO_LLVM_RAW_OSTREAM
83 #endif // !GTEST_NO_LLVM_SUPPORT
8484
8585 #endif // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_RAW_OSTREAM_H_