Changeset View
Changeset View
Standalone View
Standalone View
clang-tools-extra/clangd/unittests/HeadersTests.cpp
Show First 20 Lines • Show All 111 Lines • ▼ Show 20 Lines | std::string calculate(PathRef Original, PathRef Preferred = "", | ||||
auto Inserted = ToHeaderFile(Preferred); | auto Inserted = ToHeaderFile(Preferred); | ||||
if (!Inserter.shouldInsertInclude(Original, Inserted)) | if (!Inserter.shouldInsertInclude(Original, Inserted)) | ||||
return ""; | return ""; | ||||
auto Path = Inserter.calculateIncludePath(Inserted, MainFile); | auto Path = Inserter.calculateIncludePath(Inserted, MainFile); | ||||
Action.EndSourceFile(); | Action.EndSourceFile(); | ||||
return Path.value_or(""); | return Path.value_or(""); | ||||
} | } | ||||
llvm::Optional<TextEdit> insert(llvm::StringRef VerbatimHeader) { | llvm::Optional<TextEdit> insert(llvm::StringRef VerbatimHeader, | ||||
bool ViaImport) { | |||||
Clang = setupClang(); | Clang = setupClang(); | ||||
PreprocessOnlyAction Action; | PreprocessOnlyAction Action; | ||||
EXPECT_TRUE( | EXPECT_TRUE( | ||||
Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])); | Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])); | ||||
IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(), | IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(), | ||||
CDB.getCompileCommand(MainFile)->Directory, | CDB.getCompileCommand(MainFile)->Directory, | ||||
&Clang->getPreprocessor().getHeaderSearchInfo()); | &Clang->getPreprocessor().getHeaderSearchInfo()); | ||||
auto Edit = Inserter.insert(VerbatimHeader); | auto Edit = Inserter.insert(VerbatimHeader, ViaImport); | ||||
Action.EndSourceFile(); | Action.EndSourceFile(); | ||||
return Edit; | return Edit; | ||||
} | } | ||||
MockFS FS; | MockFS FS; | ||||
MockCompilationDatabase CDB; | MockCompilationDatabase CDB; | ||||
std::string MainFile = testPath("main.cpp"); | std::string MainFile = testPath("main.cpp"); | ||||
std::string Subdir = testPath("sub"); | std::string Subdir = testPath("sub"); | ||||
▲ Show 20 Lines • Show All 187 Lines • ▼ Show 20 Lines | TEST_F(HeadersTest, DontInsertDuplicateResolved) { | ||||
Inc.Written = "fake-bar.h"; | Inc.Written = "fake-bar.h"; | ||||
Inc.Resolved = testPath("sub/bar.h"); | Inc.Resolved = testPath("sub/bar.h"); | ||||
EXPECT_EQ(calculate(Inc.Resolved, "", {Inc}), ""); | EXPECT_EQ(calculate(Inc.Resolved, "", {Inc}), ""); | ||||
// Do not insert preferred. | // Do not insert preferred. | ||||
EXPECT_EQ(calculate(Inc.Resolved, "\"BAR.h\"", {Inc}), ""); | EXPECT_EQ(calculate(Inc.Resolved, "\"BAR.h\"", {Inc}), ""); | ||||
} | } | ||||
TEST_F(HeadersTest, PreferInserted) { | TEST_F(HeadersTest, PreferInserted) { | ||||
auto Edit = insert("<y>"); | auto Edit = insert("<y>", /*ViaImport=*/false); | ||||
EXPECT_TRUE(Edit); | EXPECT_TRUE(Edit); | ||||
EXPECT_TRUE(StringRef(Edit->newText).contains("<y>")); | EXPECT_EQ(Edit->newText, "#include <y>\n"); | ||||
Edit = insert("\"header.h\"", /*ViaImport=*/true); | |||||
EXPECT_TRUE(Edit); | |||||
kadircet: nit: ASSERT_TRUE, here and above. | |||||
EXPECT_EQ(Edit->newText, "#import \"header.h\"\n"); | |||||
} | } | ||||
TEST(Headers, NoHeaderSearchInfo) { | TEST(Headers, NoHeaderSearchInfo) { | ||||
std::string MainFile = testPath("main.cpp"); | std::string MainFile = testPath("main.cpp"); | ||||
IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(), | IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(), | ||||
/*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr); | /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr); | ||||
auto HeaderPath = testPath("sub/bar.h"); | auto HeaderPath = testPath("sub/bar.h"); | ||||
▲ Show 20 Lines • Show All 109 Lines • Show Last 20 Lines |
nit: ASSERT_TRUE, here and above.