diff --git a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp --- a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp @@ -10,6 +10,8 @@ #include "FindSymbols.h" #include "SyncAPI.h" #include "TestFS.h" +#include "TestTU.h" +#include "llvm/ADT/StringRef.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -42,160 +44,143 @@ return Field(&DocumentSymbol::children, ElementsAre(ChildrenM...)); } -ClangdServer::Options optsForTests() { - auto ServerOpts = ClangdServer::optsForTest(); - ServerOpts.WorkspaceRoot = testRoot(); - ServerOpts.BuildDynamicSymbolIndex = true; - return ServerOpts; +std::vector getSymbols(TestTU &TU, llvm::StringRef Query, + int Limit = 0) { + auto SymbolInfos = getWorkspaceSymbols(Query, Limit, TU.index().get(), + testPath(TU.Filename)); + EXPECT_TRUE(bool(SymbolInfos)) << "workspaceSymbols returned an error"; + return *SymbolInfos; } -class WorkspaceSymbolsTest : public ::testing::Test { -public: - WorkspaceSymbolsTest() : Server(CDB, FS, optsForTests()) { - // Make sure the test root directory is created. - FS.Files[testPath("unused")] = ""; - CDB.ExtraClangFlags = {"-xc++"}; - } - -protected: - MockFS FS; - MockCompilationDatabase CDB; - ClangdServer Server; - int Limit = 0; - - std::vector getSymbols(llvm::StringRef Query) { - EXPECT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for preamble"; - auto SymbolInfos = runWorkspaceSymbols(Server, Query, Limit); - EXPECT_TRUE(bool(SymbolInfos)) << "workspaceSymbols returned an error"; - return *SymbolInfos; - } - - void addFile(llvm::StringRef FileName, llvm::StringRef Contents) { - Server.addDocument(testPath(FileName), Contents); - } -}; - -} // namespace - -TEST_F(WorkspaceSymbolsTest, Macros) { - addFile("foo.cpp", R"cpp( +TEST(WorkspaceSymbols, Macros) { + TestTU TU; + TU.Code = R"cpp( #define MACRO X - )cpp"); + )cpp"; // LSP's SymbolKind doesn't have a "Macro" kind, and // indexSymbolKindToSymbolKind() currently maps macros // to SymbolKind::String. - EXPECT_THAT(getSymbols("macro"), + EXPECT_THAT(getSymbols(TU, "macro"), ElementsAre(AllOf(QName("MACRO"), WithKind(SymbolKind::String)))); } -TEST_F(WorkspaceSymbolsTest, NoLocals) { - addFile("foo.cpp", R"cpp( +TEST(WorkspaceSymbols, NoLocals) { + TestTU TU; + TU.Code = R"cpp( void test(int FirstParam, int SecondParam) { struct LocalClass {}; int local_var; - })cpp"); - EXPECT_THAT(getSymbols("l"), IsEmpty()); - EXPECT_THAT(getSymbols("p"), IsEmpty()); + })cpp"; + EXPECT_THAT(getSymbols(TU, "l"), IsEmpty()); + EXPECT_THAT(getSymbols(TU, "p"), IsEmpty()); } -TEST_F(WorkspaceSymbolsTest, Globals) { - addFile("foo.h", R"cpp( +TEST(WorkspaceSymbols, Globals) { + TestTU TU; + TU.AdditionalFiles["foo.h"] = R"cpp( int global_var; int global_func(); - struct GlobalStruct {};)cpp"); - addFile("foo.cpp", R"cpp( + struct GlobalStruct {};)cpp"; + TU.Code = R"cpp( #include "foo.h" - )cpp"); - EXPECT_THAT(getSymbols("global"), + )cpp"; + EXPECT_THAT(getSymbols(TU, "global"), UnorderedElementsAre( AllOf(QName("GlobalStruct"), WithKind(SymbolKind::Struct)), AllOf(QName("global_func"), WithKind(SymbolKind::Function)), AllOf(QName("global_var"), WithKind(SymbolKind::Variable)))); } -TEST_F(WorkspaceSymbolsTest, Unnamed) { - addFile("foo.h", R"cpp( +TEST(WorkspaceSymbols, Unnamed) { + TestTU TU; + TU.AdditionalFiles["foo.h"] = R"cpp( struct { int InUnnamed; - } UnnamedStruct;)cpp"); - addFile("foo.cpp", R"cpp( + } UnnamedStruct;)cpp"; + TU.Code = R"cpp( #include "foo.h" - )cpp"); - EXPECT_THAT(getSymbols("UnnamedStruct"), + )cpp"; + EXPECT_THAT(getSymbols(TU, "UnnamedStruct"), ElementsAre(AllOf(QName("UnnamedStruct"), WithKind(SymbolKind::Variable)))); - EXPECT_THAT(getSymbols("InUnnamed"), + EXPECT_THAT(getSymbols(TU, "InUnnamed"), ElementsAre(AllOf(QName("(anonymous struct)::InUnnamed"), WithKind(SymbolKind::Field)))); } -TEST_F(WorkspaceSymbolsTest, InMainFile) { - addFile("foo.cpp", R"cpp( +TEST(WorkspaceSymbols, InMainFile) { + TestTU TU; + TU.Code = R"cpp( int test() {} - static test2() {} - )cpp"); - EXPECT_THAT(getSymbols("test"), ElementsAre(QName("test"), QName("test2"))); + static void test2() {} + )cpp"; + EXPECT_THAT(getSymbols(TU, "test"), + ElementsAre(QName("test"), QName("test2"))); } -TEST_F(WorkspaceSymbolsTest, Namespaces) { - addFile("foo.h", R"cpp( +TEST(WorkspaceSymbols, Namespaces) { + TestTU TU; + TU.AdditionalFiles["foo.h"] = R"cpp( namespace ans1 { int ai1; namespace ans2 { int ai2; } } - )cpp"); - addFile("foo.cpp", R"cpp( + )cpp"; + TU.Code = R"cpp( #include "foo.h" - )cpp"); - EXPECT_THAT(getSymbols("a"), + )cpp"; + EXPECT_THAT(getSymbols(TU, "a"), UnorderedElementsAre(QName("ans1"), QName("ans1::ai1"), QName("ans1::ans2"), QName("ans1::ans2::ai2"))); - EXPECT_THAT(getSymbols("::"), ElementsAre(QName("ans1"))); - EXPECT_THAT(getSymbols("::a"), ElementsAre(QName("ans1"))); - EXPECT_THAT(getSymbols("ans1::"), + EXPECT_THAT(getSymbols(TU, "::"), ElementsAre(QName("ans1"))); + EXPECT_THAT(getSymbols(TU, "::a"), ElementsAre(QName("ans1"))); + EXPECT_THAT(getSymbols(TU, "ans1::"), UnorderedElementsAre(QName("ans1::ai1"), QName("ans1::ans2"))); - EXPECT_THAT(getSymbols("::ans1"), ElementsAre(QName("ans1"))); - EXPECT_THAT(getSymbols("::ans1::"), + EXPECT_THAT(getSymbols(TU, "::ans1"), ElementsAre(QName("ans1"))); + EXPECT_THAT(getSymbols(TU, "::ans1::"), UnorderedElementsAre(QName("ans1::ai1"), QName("ans1::ans2"))); - EXPECT_THAT(getSymbols("::ans1::ans2"), ElementsAre(QName("ans1::ans2"))); - EXPECT_THAT(getSymbols("::ans1::ans2::"), + EXPECT_THAT(getSymbols(TU, "::ans1::ans2"), ElementsAre(QName("ans1::ans2"))); + EXPECT_THAT(getSymbols(TU, "::ans1::ans2::"), ElementsAre(QName("ans1::ans2::ai2"))); } -TEST_F(WorkspaceSymbolsTest, AnonymousNamespace) { - addFile("foo.cpp", R"cpp( +TEST(WorkspaceSymbols, AnonymousNamespace) { + TestTU TU; + TU.Code = R"cpp( namespace { void test() {} } - )cpp"); - EXPECT_THAT(getSymbols("test"), ElementsAre(QName("test"))); + )cpp"; + EXPECT_THAT(getSymbols(TU, "test"), ElementsAre(QName("test"))); } -TEST_F(WorkspaceSymbolsTest, MultiFile) { - addFile("foo.h", R"cpp( +TEST(WorkspaceSymbols, MultiFile) { + TestTU TU; + TU.AdditionalFiles["foo.h"] = R"cpp( int foo() { } - )cpp"); - addFile("foo2.h", R"cpp( + )cpp"; + TU.AdditionalFiles["foo2.h"] = R"cpp( int foo2() { } - )cpp"); - addFile("foo.cpp", R"cpp( + )cpp"; + TU.Code = R"cpp( #include "foo.h" #include "foo2.h" - )cpp"); - EXPECT_THAT(getSymbols("foo"), + )cpp"; + EXPECT_THAT(getSymbols(TU, "foo"), UnorderedElementsAre(QName("foo"), QName("foo2"))); } -TEST_F(WorkspaceSymbolsTest, GlobalNamespaceQueries) { - addFile("foo.h", R"cpp( +TEST(WorkspaceSymbols, GlobalNamespaceQueries) { + TestTU TU; + TU.AdditionalFiles["foo.h"] = R"cpp( int foo() { } class Foo { @@ -205,21 +190,22 @@ int foo2() { } } - )cpp"); - addFile("foo.cpp", R"cpp( + )cpp"; + TU.Code = R"cpp( #include "foo.h" - )cpp"); - EXPECT_THAT(getSymbols("::"), + )cpp"; + EXPECT_THAT(getSymbols(TU, "::"), UnorderedElementsAre( AllOf(QName("Foo"), WithKind(SymbolKind::Class)), AllOf(QName("foo"), WithKind(SymbolKind::Function)), AllOf(QName("ns"), WithKind(SymbolKind::Namespace)))); - EXPECT_THAT(getSymbols(":"), IsEmpty()); - EXPECT_THAT(getSymbols(""), IsEmpty()); + EXPECT_THAT(getSymbols(TU, ":"), IsEmpty()); + EXPECT_THAT(getSymbols(TU, ""), IsEmpty()); } -TEST_F(WorkspaceSymbolsTest, Enums) { - addFile("foo.h", R"cpp( +TEST(WorkspaceSymbols, Enums) { + TestTU TU; + TU.AdditionalFiles["foo.h"] = R"cpp( enum { Red }; @@ -240,63 +226,66 @@ White }; } - )cpp"); - addFile("foo.cpp", R"cpp( + )cpp"; + TU.Code = R"cpp( #include "foo.h" - )cpp"); - EXPECT_THAT(getSymbols("Red"), ElementsAre(QName("Red"))); - EXPECT_THAT(getSymbols("::Red"), ElementsAre(QName("Red"))); - EXPECT_THAT(getSymbols("Green"), ElementsAre(QName("Green"))); - EXPECT_THAT(getSymbols("Green"), ElementsAre(QName("Green"))); - EXPECT_THAT(getSymbols("Color2::Yellow"), + )cpp"; + EXPECT_THAT(getSymbols(TU, "Red"), ElementsAre(QName("Red"))); + EXPECT_THAT(getSymbols(TU, "::Red"), ElementsAre(QName("Red"))); + EXPECT_THAT(getSymbols(TU, "Green"), ElementsAre(QName("Green"))); + EXPECT_THAT(getSymbols(TU, "Green"), ElementsAre(QName("Green"))); + EXPECT_THAT(getSymbols(TU, "Color2::Yellow"), ElementsAre(QName("Color2::Yellow"))); - EXPECT_THAT(getSymbols("Yellow"), ElementsAre(QName("Color2::Yellow"))); + EXPECT_THAT(getSymbols(TU, "Yellow"), ElementsAre(QName("Color2::Yellow"))); - EXPECT_THAT(getSymbols("ns::Black"), ElementsAre(QName("ns::Black"))); - EXPECT_THAT(getSymbols("ns::Blue"), ElementsAre(QName("ns::Blue"))); - EXPECT_THAT(getSymbols("ns::Color4::White"), + EXPECT_THAT(getSymbols(TU, "ns::Black"), ElementsAre(QName("ns::Black"))); + EXPECT_THAT(getSymbols(TU, "ns::Blue"), ElementsAre(QName("ns::Blue"))); + EXPECT_THAT(getSymbols(TU, "ns::Color4::White"), ElementsAre(QName("ns::Color4::White"))); } -TEST_F(WorkspaceSymbolsTest, Ranking) { - addFile("foo.h", R"cpp( +TEST(WorkspaceSymbols, Ranking) { + TestTU TU; + TU.AdditionalFiles["foo.h"] = R"cpp( namespace ns{} void func(); - )cpp"); - addFile("foo.cpp", R"cpp( + )cpp"; + TU.Code = R"cpp( #include "foo.h" - )cpp"); - EXPECT_THAT(getSymbols("::"), ElementsAre(QName("func"), QName("ns"))); + )cpp"; + EXPECT_THAT(getSymbols(TU, "::"), ElementsAre(QName("func"), QName("ns"))); } -TEST_F(WorkspaceSymbolsTest, WithLimit) { - addFile("foo.h", R"cpp( +TEST(WorkspaceSymbols, WithLimit) { + TestTU TU; + TU.AdditionalFiles["foo.h"] = R"cpp( int foo; int foo2; - )cpp"); - addFile("foo.cpp", R"cpp( + )cpp"; + TU.Code = R"cpp( #include "foo.h" - )cpp"); + )cpp"; // Foo is higher ranked because of exact name match. - EXPECT_THAT(getSymbols("foo"), + EXPECT_THAT(getSymbols(TU, "foo"), UnorderedElementsAre( AllOf(QName("foo"), WithKind(SymbolKind::Variable)), AllOf(QName("foo2"), WithKind(SymbolKind::Variable)))); - Limit = 1; - EXPECT_THAT(getSymbols("foo"), ElementsAre(QName("foo"))); + EXPECT_THAT(getSymbols(TU, "foo", 1), ElementsAre(QName("foo"))); } -TEST_F(WorkspaceSymbolsTest, TempSpecs) { - addFile("foo.h", R"cpp( +TEST(WorkspaceSymbols, TempSpecs) { + TestTU TU; + TU.ExtraArgs = {"-xc++"}; + TU.Code = R"cpp( template class Foo {}; template class Foo {}; template <> class Foo {}; template <> class Foo {}; - )cpp"); + )cpp"; // Foo is higher ranked because of exact name match. EXPECT_THAT( - getSymbols("Foo"), + getSymbols(TU, "Foo"), UnorderedElementsAre( AllOf(QName("Foo"), WithKind(SymbolKind::Class)), AllOf(QName("Foo"), WithKind(SymbolKind::Class)), @@ -304,31 +293,14 @@ AllOf(QName("Foo"), WithKind(SymbolKind::Class)))); } -namespace { -class DocumentSymbolsTest : public ::testing::Test { -public: - DocumentSymbolsTest() : Server(CDB, FS, optsForTests()) {} - -protected: - MockFS FS; - MockCompilationDatabase CDB; - ClangdServer Server; - - std::vector getSymbols(PathRef File) { - EXPECT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for preamble"; - auto SymbolInfos = runDocumentSymbols(Server, File); - EXPECT_TRUE(bool(SymbolInfos)) << "documentSymbols returned an error"; - return *SymbolInfos; - } - - void addFile(llvm::StringRef FilePath, llvm::StringRef Contents) { - Server.addDocument(FilePath, Contents); - } -}; -} // namespace +std::vector getSymbols(ParsedAST AST) { + auto SymbolInfos = getDocumentSymbols(AST); + EXPECT_TRUE(bool(SymbolInfos)) << "documentSymbols returned an error"; + return *SymbolInfos; +} -TEST_F(DocumentSymbolsTest, BasicSymbols) { - std::string FilePath = testPath("foo.cpp"); +TEST(DocumentSymbols, BasicSymbols) { + TestTU TU; Annotations Main(R"( class Foo; class Foo { @@ -372,9 +344,9 @@ } // namespace foo )"); - addFile(FilePath, Main.code()); + TU.Code = Main.code().str(); EXPECT_THAT( - getSymbols(FilePath), + getSymbols(TU.build()), ElementsAreArray( {AllOf(WithName("Foo"), WithKind(SymbolKind::Class), Children()), AllOf(WithName("Foo"), WithKind(SymbolKind::Class), @@ -416,8 +388,8 @@ AllOf(WithName("v2"), WithKind(SymbolKind::Namespace))))})); } -TEST_F(DocumentSymbolsTest, DeclarationDefinition) { - std::string FilePath = testPath("foo.cpp"); +TEST(DocumentSymbols, DeclarationDefinition) { + TestTU TU; Annotations Main(R"( class Foo { void $decl[[f]](); @@ -426,9 +398,9 @@ } )"); - addFile(FilePath, Main.code()); + TU.Code = Main.code().str(); EXPECT_THAT( - getSymbols(FilePath), + getSymbols(TU.build()), ElementsAre( AllOf(WithName("Foo"), WithKind(SymbolKind::Class), Children(AllOf(WithName("f"), WithKind(SymbolKind::Method), @@ -437,48 +409,45 @@ SymNameRange(Main.range("def"))))); } -TEST_F(DocumentSymbolsTest, Concepts) { - CDB.ExtraClangFlags = {"-std=c++20"}; - std::string FilePath = testPath("foo.cpp"); - addFile(FilePath, - "template concept C = requires(T t) { t.foo(); };"); +TEST(DocumentSymbols, Concepts) { + TestTU TU; + TU.ExtraArgs = {"-std=c++20"}; + TU.Code = "template concept C = requires(T t) { t.foo(); };"; - EXPECT_THAT(getSymbols(FilePath), ElementsAre(WithName("C"))); + EXPECT_THAT(getSymbols(TU.build()), ElementsAre(WithName("C"))); } -TEST_F(DocumentSymbolsTest, ExternSymbol) { - std::string FilePath = testPath("foo.cpp"); - addFile(testPath("foo.h"), R"cpp( +TEST(DocumentSymbols, ExternSymbol) { + TestTU TU; + TU.AdditionalFiles["foo.h"] = R"cpp( extern int var; - )cpp"); - addFile(FilePath, R"cpp( + )cpp"; + TU.Code = R"cpp( #include "foo.h" - )cpp"); + )cpp"; - EXPECT_THAT(getSymbols(FilePath), IsEmpty()); + EXPECT_THAT(getSymbols(TU.build()), IsEmpty()); } -TEST_F(DocumentSymbolsTest, NoLocals) { - std::string FilePath = testPath("foo.cpp"); - addFile(FilePath, - R"cpp( +TEST(DocumentSymbols, NoLocals) { + TestTU TU; + TU.Code = R"cpp( void test(int FirstParam, int SecondParam) { struct LocalClass {}; int local_var; - })cpp"); - EXPECT_THAT(getSymbols(FilePath), ElementsAre(WithName("test"))); + })cpp"; + EXPECT_THAT(getSymbols(TU.build()), ElementsAre(WithName("test"))); } -TEST_F(DocumentSymbolsTest, Unnamed) { - std::string FilePath = testPath("foo.h"); - addFile(FilePath, - R"cpp( +TEST(DocumentSymbols, Unnamed) { + TestTU TU; + TU.Code = R"cpp( struct { int InUnnamed; } UnnamedStruct; - )cpp"); + )cpp"; EXPECT_THAT( - getSymbols(FilePath), + getSymbols(TU.build()), ElementsAre( AllOf(WithName("(anonymous struct)"), WithKind(SymbolKind::Struct), Children(AllOf(WithName("InUnnamed"), @@ -487,26 +456,23 @@ Children()))); } -TEST_F(DocumentSymbolsTest, InHeaderFile) { - addFile(testPath("bar.h"), R"cpp( +TEST(DocumentSymbols, InHeaderFile) { + TestTU TU; + TU.AdditionalFiles["bar.h"] = R"cpp( int foo() { } - )cpp"); - std::string FilePath = testPath("foo.h"); - addFile(FilePath, R"cpp( + )cpp"; + TU.Code = R"cpp( #include "bar.h" int test() { } - )cpp"); - addFile(testPath("foo.cpp"), R"cpp( - #include "foo.h" - )cpp"); - EXPECT_THAT(getSymbols(FilePath), ElementsAre(WithName("test"))); + )cpp"; + EXPECT_THAT(getSymbols(TU.build()), ElementsAre(WithName("test"))); } -TEST_F(DocumentSymbolsTest, Template) { - std::string FilePath = testPath("foo.cpp"); - addFile(FilePath, R"( +TEST(DocumentSymbols, Template) { + TestTU TU; + TU.Code = R"( template struct Tmpl {T x = 0;}; template <> struct Tmpl { int y = 0; @@ -523,9 +489,9 @@ int varTmpl = T(); template <> double varTmpl = 10.0; - )"); + )"; EXPECT_THAT( - getSymbols(FilePath), + getSymbols(TU.build()), ElementsAre( AllOf(WithName("Tmpl"), WithKind(SymbolKind::Struct), Children(AllOf(WithName("x"), WithKind(SymbolKind::Field)))), @@ -541,9 +507,9 @@ AllOf(WithName("varTmpl"), Children()))); } -TEST_F(DocumentSymbolsTest, Namespaces) { - std::string FilePath = testPath("foo.cpp"); - addFile(FilePath, R"cpp( +TEST(DocumentSymbols, Namespaces) { + TestTU TU; + TU.Code = R"cpp( namespace ans1 { int ai1; namespace ans2 { @@ -565,9 +531,9 @@ class Bar {}; } } - )cpp"); + )cpp"; EXPECT_THAT( - getSymbols(FilePath), + getSymbols(TU.build()), ElementsAreArray<::testing::Matcher>( {AllOf(WithName("ans1"), Children(AllOf(WithName("ai1"), Children()), @@ -579,9 +545,9 @@ Children(AllOf(WithName("nb"), Children(WithName("Bar")))))})); } -TEST_F(DocumentSymbolsTest, Enums) { - std::string FilePath = testPath("foo.cpp"); - addFile(FilePath, R"( +TEST(DocumentSymbols, Enums) { + TestTU TU; + TU.Code = R"( enum { Red }; @@ -596,9 +562,9 @@ Black }; } - )"); + )"; EXPECT_THAT( - getSymbols(FilePath), + getSymbols(TU.build()), ElementsAre( AllOf(WithName("(anonymous enum)"), Children(WithName("Red"))), AllOf(WithName("Color"), Children(WithName("Green"))), @@ -607,8 +573,8 @@ Children(WithName("Black"))))))); } -TEST_F(DocumentSymbolsTest, FromMacro) { - std::string FilePath = testPath("foo.cpp"); +TEST(DocumentSymbols, FromMacro) { + TestTU TU; Annotations Main(R"( #define FF(name) \ class name##_Test {}; @@ -620,31 +586,31 @@ FF2(); )"); - addFile(FilePath, Main.code()); + TU.Code = Main.code().str(); EXPECT_THAT( - getSymbols(FilePath), + getSymbols(TU.build()), ElementsAre( AllOf(WithName("abc_Test"), SymNameRange(Main.range("expansion"))), AllOf(WithName("Test"), SymNameRange(Main.range("spelling"))))); } -TEST_F(DocumentSymbolsTest, FuncTemplates) { - std::string FilePath = testPath("foo.cpp"); +TEST(DocumentSymbols, FuncTemplates) { + TestTU TU; Annotations Source(R"cpp( template T foo() {} auto x = foo(); - auto y = foo() + auto y = foo(); )cpp"); - addFile(FilePath, Source.code()); + TU.Code = Source.code().str(); // Make sure we only see the template declaration, not instantiations. - EXPECT_THAT(getSymbols(FilePath), + EXPECT_THAT(getSymbols(TU.build()), ElementsAre(WithName("foo"), WithName("x"), WithName("y"))); } -TEST_F(DocumentSymbolsTest, UsingDirectives) { - std::string FilePath = testPath("foo.cpp"); +TEST(DocumentSymbols, UsingDirectives) { + TestTU TU; Annotations Source(R"cpp( namespace ns { int foo; @@ -655,24 +621,24 @@ using namespace ::ns; // check we don't loose qualifiers. using namespace ns_alias; // and namespace aliases. )cpp"); - addFile(FilePath, Source.code()); - EXPECT_THAT(getSymbols(FilePath), + TU.Code = Source.code().str(); + EXPECT_THAT(getSymbols(TU.build()), ElementsAre(WithName("ns"), WithName("ns_alias"), WithName("using namespace ::ns"), WithName("using namespace ns_alias"))); } -TEST_F(DocumentSymbolsTest, TempSpecs) { - std::string FilePath = testPath("foo.cpp"); - addFile(FilePath, R"cpp( +TEST(DocumentSymbols, TempSpecs) { + TestTU TU; + TU.Code = R"cpp( template class Foo {}; template class Foo {}; template <> class Foo {}; template <> class Foo {}; - )cpp"); + )cpp"; // Foo is higher ranked because of exact name match. EXPECT_THAT( - getSymbols(FilePath), + getSymbols(TU.build()), UnorderedElementsAre( AllOf(WithName("Foo"), WithKind(SymbolKind::Class)), AllOf(WithName("Foo"), WithKind(SymbolKind::Class)), @@ -680,9 +646,9 @@ AllOf(WithName("Foo"), WithKind(SymbolKind::Class)))); } -TEST_F(DocumentSymbolsTest, Qualifiers) { - std::string FilePath = testPath("foo.cpp"); - addFile(FilePath, R"cpp( +TEST(DocumentSymbols, Qualifiers) { + TestTU TU; + TU.Code = R"cpp( namespace foo { namespace bar { struct Cls; @@ -702,10 +668,10 @@ namespace alias = foo::bar; int ::alias::func4() { return 40; } - )cpp"); + )cpp"; // All the qualifiers should be preserved exactly as written. - EXPECT_THAT(getSymbols(FilePath), + EXPECT_THAT(getSymbols(TU.build()), UnorderedElementsAre( WithName("foo"), WithName("foo::bar::Cls"), WithName("foo::bar::func1"), WithName("::foo::bar::func2"), @@ -713,9 +679,9 @@ WithName("alias"), WithName("::alias::func4"))); } -TEST_F(DocumentSymbolsTest, QualifiersWithTemplateArgs) { - std::string FilePath = testPath("foo.cpp"); - addFile(FilePath, R"cpp( +TEST(DocumentSymbols, QualifiersWithTemplateArgs) { + TestTU TU; + TU.Code = R"cpp( template class Foo; template <> @@ -736,9 +702,9 @@ using Foo_type = Foo; // If the whole type is aliased, this should be preserved too! int Foo_type::method3() { return 30; } - )cpp"); + )cpp"; EXPECT_THAT( - getSymbols(FilePath), + getSymbols(TU.build()), UnorderedElementsAre(WithName("Foo"), WithName("Foo"), WithName("int_type"), WithName("Foo::method1"), @@ -746,5 +712,6 @@ WithName("Foo_type::method3"))); } +} // namespace } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/unittests/SyncAPI.h b/clang-tools-extra/clangd/unittests/SyncAPI.h --- a/clang-tools-extra/clangd/unittests/SyncAPI.h +++ b/clang-tools-extra/clangd/unittests/SyncAPI.h @@ -46,12 +46,6 @@ std::string runDumpAST(ClangdServer &Server, PathRef File); -llvm::Expected> -runWorkspaceSymbols(ClangdServer &Server, StringRef Query, int Limit); - -Expected> runDocumentSymbols(ClangdServer &Server, - PathRef File); - SymbolSlab runFuzzyFind(const SymbolIndex &Index, StringRef Query); SymbolSlab runFuzzyFind(const SymbolIndex &Index, const FuzzyFindRequest &Req); RefSlab getRefs(const SymbolIndex &Index, SymbolID ID); diff --git a/clang-tools-extra/clangd/unittests/SyncAPI.cpp b/clang-tools-extra/clangd/unittests/SyncAPI.cpp --- a/clang-tools-extra/clangd/unittests/SyncAPI.cpp +++ b/clang-tools-extra/clangd/unittests/SyncAPI.cpp @@ -111,20 +111,6 @@ return std::move(*Result); } -llvm::Expected> -runWorkspaceSymbols(ClangdServer &Server, llvm::StringRef Query, int Limit) { - llvm::Optional>> Result; - Server.workspaceSymbols(Query, Limit, capture(Result)); - return std::move(*Result); -} - -llvm::Expected> -runDocumentSymbols(ClangdServer &Server, PathRef File) { - llvm::Optional>> Result; - Server.documentSymbols(File, capture(Result)); - return std::move(*Result); -} - SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query) { FuzzyFindRequest Req; Req.Query = std::string(Query);