17 #include "clang/Index/IndexSymbol.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/StringSaver.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
31 using llvm::sys::path::convert_to_slash;
33 const char *testPathURI(llvm::StringRef
Path,
34 llvm::UniqueStringSaver &
Strings) {
36 return Strings.save(URI.toString()).begin();
39 TEST(RemoteMarshallingTest, URITranslation) {
40 llvm::BumpPtrAllocator
Arena;
43 Original.Location.FileURI =
44 testPathURI(
"remote/machine/projects/llvm-project/clang-tools-extra/"
45 "clangd/unittests/remote/MarshallingTests.cpp",
49 EXPECT_EQ(Serialized.location().file_path(),
50 "clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp");
51 const std::string LocalIndexPrefix =
testPath(
"local/machine/project/");
53 testPath(
"home/my-projects/llvm-project/"));
54 EXPECT_TRUE(Deserialized);
55 EXPECT_EQ(Deserialized->Location.FileURI,
56 testPathURI(
"home/my-projects/llvm-project/clang-tools-extra/"
57 "clangd/unittests/remote/MarshallingTests.cpp",
60 clangd::Ref WithInvalidURI;
62 WithInvalidURI.Location.FileURI =
"This is not a URI";
64 EXPECT_EQ(Serialized.location().file_path(),
"");
69 EXPECT_TRUE(
bool(UnittestURI));
70 WithInvalidURI.Location.FileURI =
71 Strings.save(UnittestURI->toString()).begin();
73 EXPECT_EQ(Serialized.location().file_path(),
"");
76 *WithAbsolutePath.mutable_location()->mutable_file_path() =
77 "/usr/local/user/home/HelloWorld.cpp";
81 EXPECT_FALSE(Deserialized);
84 TEST(RemoteMarshallingTest, SymbolSerialization) {
88 EXPECT_TRUE(
bool(ID));
92 Info.Kind = index::SymbolKind::Function;
93 Info.SubKind = index::SymbolSubKind::AccessorGetter;
94 Info.Lang = index::SymbolLanguage::CXX;
95 Info.Properties = static_cast<index::SymbolPropertySet>(
96 index::SymbolProperty::TemplateSpecialization);
99 llvm::BumpPtrAllocator
Arena;
102 Sym.Name =
Strings.save(
"Foo");
103 Sym.Scope =
Strings.save(
"llvm::foo::bar::");
118 Sym.CanonicalDeclaration =
Location;
120 Sym.References = 9000;
122 Sym.Signature =
Strings.save(
"(int X, char Y, Type T)");
123 Sym.TemplateSpecializationArgs =
Strings.save(
"<int, char, bool, Type>");
124 Sym.CompletionSnippetSuffix =
125 Strings.save(
"({1: int X}, {2: char Y}, {3: Type T})");
126 Sym.Documentation =
Strings.save(
"This is my amazing Foo constructor!");
127 Sym.ReturnType =
Strings.save(
"Foo");
129 Sym.Flags = clangd::Symbol::SymbolFlag::IndexedForCodeCompletion;
135 EXPECT_TRUE(Deserialized);
138 EXPECT_EQ(convert_to_slash(Serialized.definition().file_path(),
139 llvm::sys::path::Style::posix),
140 Serialized.definition().file_path());
142 llvm::sys::path::is_relative(Serialized.definition().file_path()));
149 EXPECT_FALSE(Deserialized);
153 EXPECT_TRUE(
bool(UnittestURI));
158 EXPECT_FALSE(Deserialized);
166 EXPECT_TRUE(Deserialized);
167 EXPECT_EQ(Deserialized->Definition.FileURI,
168 testPathURI(
"home/File.h",
Strings));
172 EXPECT_FALSE(Deserialized);
175 TEST(RemoteMarshallingTest, RefSerialization) {
179 llvm::BumpPtrAllocator
Arena;
188 "llvm-project/llvm/clang-tools-extra/clangd/Protocol.h",
Strings);
194 EXPECT_TRUE(Deserialized);
198 TEST(RemoteMarshallingTest, IncludeHeaderURIs) {
199 llvm::BumpPtrAllocator
Arena;
202 llvm::SmallVector<clangd::Symbol::IncludeHeaderWithReferences, 1>
204 clangd::Symbol::IncludeHeaderWithReferences Header;
205 Header.IncludeHeader =
Strings.save(
207 Header.References = 21;
208 ValidHeaders.push_back(Header);
209 Header.IncludeHeader =
Strings.save(
"<iostream>");
210 Header.References = 100;
211 ValidHeaders.push_back(Header);
212 Header.IncludeHeader =
Strings.save(
"\"cstdio\"");
213 Header.References = 200;
214 ValidHeaders.push_back(Header);
216 llvm::SmallVector<clangd::Symbol::IncludeHeaderWithReferences, 1>
219 Header.IncludeHeader =
Strings.save(
testPath(
"project/include/Common.h"));
220 Header.References = 42;
221 InvalidHeaders.push_back(Header);
223 Header.IncludeHeader =
Strings.save(
"NotAHeader");
224 Header.References = 5;
225 InvalidHeaders.push_back(Header);
236 Sym.CanonicalDeclaration =
Location;
240 auto AllHeaders = ValidHeaders;
241 AllHeaders.insert(AllHeaders.end(), InvalidHeaders.begin(),
242 InvalidHeaders.end());
243 Sym.IncludeHeaders = AllHeaders;
245 auto Serialized =
toProtobuf(Sym, convert_to_slash(
"/"));
246 EXPECT_EQ(static_cast<size_t>(Serialized.headers_size()),
247 ValidHeaders.size());
249 EXPECT_TRUE(Deserialized);
251 Sym.IncludeHeaders = ValidHeaders;
255 TEST(RemoteMarshallingTest, FuzzyFindRequestSerialization) {
256 clangd::FuzzyFindRequest Request;
257 Request.ProximityPaths = {
testPath(
"remote/Header.h"),
258 testPath(
"remote/subdir/OtherHeader.h"),
259 testPath(
"notremote/File.h"),
"Not a Path."};
261 EXPECT_EQ(Serialized.proximity_paths_size(), 2);
263 EXPECT_THAT(Deserialized.ProximityPaths,
264 testing::ElementsAre(
testPath(
"home/Header.h"),
265 testPath(
"home/subdir/OtherHeader.h")));
268 TEST(RemoteMarshallingTest, RelativePathToURITranslation) {
276 TEST(RemoteMarshallingTest, URIToRelativePathTranslation) {
277 llvm::BumpPtrAllocator
Arena;
284 testPathURI(
"home/project/lib/File.cpp",
Strings),
"home/project/"));