21#include "clang/Index/IndexSymbol.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Twine.h"
25#include "llvm/Support/Error.h"
26#include "llvm/Support/Path.h"
27#include "llvm/Support/StringSaver.h"
28#include "gmock/gmock.h"
29#include "gtest/gtest.h"
37using llvm::sys::path::convert_to_slash;
39const char *testPathURI(llvm::StringRef
Path,
40 llvm::UniqueStringSaver &Strings) {
42 return Strings.save(URI.toString()).begin();
45clangd::Symbol createSymbol(llvm::StringRef PathPrefix,
46 llvm::UniqueStringSaver &Strings) {
50 index::SymbolInfo
Info;
51 Info.Kind = index::SymbolKind::Function;
52 Info.SubKind = index::SymbolSubKind::AccessorGetter;
53 Info.Lang = index::SymbolLanguage::CXX;
54 Info.Properties =
static_cast<index::SymbolPropertySet
>(
55 index::SymbolProperty::TemplateSpecialization);
58 Sym.Name = Strings.save(
"Foo");
59 Sym.Scope = Strings.save(
"llvm::foo::bar::");
61 clangd::SymbolLocation Location;
62 Location.Start.setLine(1);
63 Location.Start.setColumn(15);
64 Location.End.setLine(3);
65 Location.End.setColumn(121);
66 Location.FileURI = testPathURI(PathPrefix.str() +
"Definition.cpp", Strings);
67 Sym.Definition = Location;
69 Location.Start.setLine(42);
70 Location.Start.setColumn(31);
71 Location.End.setLine(20);
72 Location.End.setColumn(400);
73 Location.FileURI = testPathURI(PathPrefix.str() +
"Declaration.h", Strings);
74 Sym.CanonicalDeclaration = Location;
76 Sym.References = 9000;
78 Sym.Signature = Strings.save(
"(int X, char Y, Type T)");
79 Sym.TemplateSpecializationArgs = Strings.save(
"<int, char, bool, Type>");
80 Sym.CompletionSnippetSuffix =
81 Strings.save(
"({1: int X}, {2: char Y}, {3: Type T})");
82 Sym.Documentation = Strings.save(
"This is my amazing Foo constructor!");
83 Sym.ReturnType = Strings.save(
"Foo");
90TEST(RemoteMarshallingTest, URITranslation) {
91 llvm::BumpPtrAllocator Arena;
92 llvm::UniqueStringSaver Strings(Arena);
94 testPath(
"remote/machine/projects/llvm-project/"),
95 testPath(
"home/my-projects/llvm-project/"));
97 Original.Location.FileURI =
98 testPathURI(
"remote/machine/projects/llvm-project/clang-tools-extra/"
99 "clangd/unittests/remote/MarshallingTests.cpp",
101 auto Serialized = ProtobufMarshaller.toProtobuf(Original);
102 ASSERT_TRUE(
bool(Serialized));
103 EXPECT_EQ(Serialized->location().file_path(),
104 "clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp");
105 auto Deserialized = ProtobufMarshaller.fromProtobuf(*Serialized);
106 ASSERT_TRUE(
bool(Deserialized));
107 EXPECT_STREQ(Deserialized->Location.FileURI,
108 testPathURI(
"home/my-projects/llvm-project/clang-tools-extra/"
109 "clangd/unittests/remote/MarshallingTests.cpp",
113 *Serialized->mutable_location()->mutable_file_path() = std::string();
114 Deserialized = ProtobufMarshaller.fromProtobuf(*Serialized);
115 EXPECT_FALSE(
bool(Deserialized));
116 llvm::consumeError(Deserialized.takeError());
118 clangd::Ref WithInvalidURI;
120 WithInvalidURI.Location.FileURI =
"This is not a URI";
121 auto DeserializedRef = ProtobufMarshaller.toProtobuf(WithInvalidURI);
122 EXPECT_FALSE(
bool(DeserializedRef));
123 llvm::consumeError(DeserializedRef.takeError());
128 ASSERT_TRUE(
bool(UnittestURI));
129 WithInvalidURI.Location.FileURI =
130 Strings.save(UnittestURI->toString()).begin();
131 auto DeserializedSymbol = ProtobufMarshaller.toProtobuf(WithInvalidURI);
132 EXPECT_FALSE(
bool(DeserializedSymbol));
133 llvm::consumeError(DeserializedSymbol.takeError());
137 Ref WithAbsolutePath;
138 *WithAbsolutePath.mutable_location()->mutable_file_path() =
139 "/usr/local/user/home/HelloWorld.cpp";
140 Deserialized = ProtobufMarshaller.fromProtobuf(WithAbsolutePath);
141 EXPECT_FALSE(
bool(Deserialized));
142 llvm::consumeError(Deserialized.takeError());
145TEST(RemoteMarshallingTest, SymbolSerialization) {
146 llvm::BumpPtrAllocator Arena;
147 llvm::UniqueStringSaver Strings(Arena);
149 clangd::Symbol Sym = createSymbol(
"home/", Strings);
154 auto Serialized = ProtobufMarshaller.toProtobuf(Sym);
155 ASSERT_TRUE(
bool(Serialized));
156 auto Deserialized = ProtobufMarshaller.fromProtobuf(*Serialized);
157 ASSERT_TRUE(
bool(Deserialized));
162 EXPECT_EQ(convert_to_slash(Serialized->definition().file_path(),
163 llvm::sys::path::Style::posix),
164 Serialized->definition().file_path());
166 llvm::sys::path::is_relative(Serialized->definition().file_path()));
169 Sym.Definition = clangd::SymbolLocation();
170 Serialized = ProtobufMarshaller.toProtobuf(Sym);
171 ASSERT_TRUE(
bool(Serialized));
172 ASSERT_TRUE(
bool(ProtobufMarshaller.fromProtobuf(*Serialized)));
175 *Serialized->mutable_canonical_declaration()->mutable_file_path() =
176 convert_to_slash(
"/path/to/Declaration.h");
177 Deserialized = ProtobufMarshaller.fromProtobuf(*Serialized);
178 EXPECT_FALSE(
bool(Deserialized));
179 llvm::consumeError(Deserialized.takeError());
182 Sym.Definition.FileURI =
"Not A URI";
183 Serialized = ProtobufMarshaller.toProtobuf(Sym);
184 EXPECT_FALSE(
bool(Serialized));
185 llvm::consumeError(Serialized.takeError());
189 ASSERT_TRUE(
bool(UnittestURI));
190 Sym.Definition.FileURI = Strings.save(UnittestURI->toString()).begin();
191 Serialized = ProtobufMarshaller.toProtobuf(Sym);
192 EXPECT_FALSE(
bool(Serialized));
193 llvm::consumeError(Serialized.takeError());
196 Sym.Definition.FileURI = testPathURI(
"home/File.h", Strings);
198 Serialized = ProtobufMarshaller.toProtobuf(Sym);
199 ASSERT_TRUE(
bool(Serialized));
200 Deserialized = ProtobufMarshaller.fromProtobuf(*Serialized);
201 ASSERT_TRUE(
bool(Deserialized));
202 EXPECT_STREQ(Deserialized->Definition.FileURI,
203 testPathURI(
"home/File.h", Strings));
206 Serialized = WrongMarshaller.toProtobuf(Sym);
207 EXPECT_FALSE(Serialized);
208 llvm::consumeError(Serialized.takeError());
211TEST(RemoteMarshallingTest, RefSerialization) {
215 llvm::BumpPtrAllocator Arena;
216 llvm::UniqueStringSaver Strings(Arena);
218 clangd::SymbolLocation Location;
219 Location.Start.setLine(124);
220 Location.Start.setColumn(21);
221 Location.End.setLine(3213);
222 Location.End.setColumn(541);
223 Location.FileURI = testPathURI(
224 "llvm-project/llvm/clang-tools-extra/clangd/Protocol.h", Strings);
225 Ref.Location = Location;
231 auto Serialized = ProtobufMarshaller.toProtobuf(Ref);
232 ASSERT_TRUE(
bool(Serialized));
233 auto Deserialized = ProtobufMarshaller.fromProtobuf(*Serialized);
234 ASSERT_TRUE(
bool(Deserialized));
238TEST(RemoteMarshallingTest, IncludeHeaderURIs) {
239 llvm::BumpPtrAllocator Arena;
240 llvm::UniqueStringSaver Strings(Arena);
242 clangd::Symbol Sym = createSymbol(
"remote/", Strings);
244 clangd::Symbol::IncludeHeaderWithReferences Header;
246 Header.IncludeHeader =
248 Header.References = 21;
249 Sym.IncludeHeaders.push_back(Header);
250 Header.IncludeHeader = Strings.save(
"<iostream>");
251 Header.References = 100;
252 Sym.IncludeHeaders.push_back(Header);
253 Header.IncludeHeader = Strings.save(
"\"cstdio\"");
254 Header.References = 200;
255 Sym.IncludeHeaders.push_back(Header);
259 auto Serialized = ProtobufMarshaller.toProtobuf(Sym);
260 ASSERT_TRUE(
bool(Serialized));
261 EXPECT_EQ(
static_cast<size_t>(Serialized->headers_size()),
262 Sym.IncludeHeaders.size());
263 auto Deserialized = ProtobufMarshaller.fromProtobuf(*Serialized);
264 ASSERT_TRUE(
bool(Deserialized));
269 Header.IncludeHeader = Strings.save(
testPath(
"project/include/Common.h"));
270 Header.References = 42;
271 Sym.IncludeHeaders.push_back(Header);
272 Serialized = ProtobufMarshaller.toProtobuf(Sym);
273 EXPECT_FALSE(
bool(Serialized));
274 llvm::consumeError(Serialized.takeError());
277 Sym.IncludeHeaders.pop_back();
279 Header.IncludeHeader = Strings.save(
"NotAHeader");
280 Header.References = 5;
281 Sym.IncludeHeaders.push_back(Header);
282 Serialized = ProtobufMarshaller.toProtobuf(Sym);
283 EXPECT_FALSE(
bool(Serialized));
284 llvm::consumeError(Serialized.takeError());
287 Sym.IncludeHeaders.pop_back();
288 Serialized = ProtobufMarshaller.toProtobuf(Sym);
289 ASSERT_TRUE(
bool(Serialized));
290 HeaderWithReferences InvalidHeader;
291 InvalidHeader.set_header(convert_to_slash(
"/absolute/path/Header.h"));
292 InvalidHeader.set_references(9000);
293 *Serialized->add_headers() = InvalidHeader;
294 Deserialized = ProtobufMarshaller.fromProtobuf(*Serialized);
295 EXPECT_FALSE(
bool(Deserialized));
296 llvm::consumeError(Deserialized.takeError());
299TEST(RemoteMarshallingTest, LookupRequestSerialization) {
300 clangd::LookupRequest Request;
306 auto Serialized = ProtobufMarshaller.toProtobuf(Request);
307 EXPECT_EQ(
static_cast<unsigned>(Serialized.ids_size()), Request.IDs.size());
308 auto Deserialized = ProtobufMarshaller.fromProtobuf(&Serialized);
309 ASSERT_TRUE(
bool(Deserialized));
310 EXPECT_EQ(Deserialized->IDs, Request.IDs);
313TEST(RemoteMarshallingTest, LookupRequestFailingSerialization) {
314 clangd::LookupRequest Request;
316 auto Serialized = ProtobufMarshaller.toProtobuf(Request);
317 Serialized.add_ids(
"Invalid Symbol ID");
318 auto Deserialized = ProtobufMarshaller.fromProtobuf(&Serialized);
319 EXPECT_FALSE(
bool(Deserialized));
320 llvm::consumeError(Deserialized.takeError());
323TEST(RemoteMarshallingTest, FuzzyFindRequestSerialization) {
324 clangd::FuzzyFindRequest Request;
325 Request.ProximityPaths = {
testPath(
"local/Header.h"),
326 testPath(
"local/subdir/OtherHeader.h"),
327 testPath(
"remote/File.h"),
"Not a Path."};
329 auto Serialized = ProtobufMarshaller.toProtobuf(Request);
330 EXPECT_EQ(Serialized.proximity_paths_size(), 2);
331 auto Deserialized = ProtobufMarshaller.fromProtobuf(&Serialized);
332 ASSERT_TRUE(
bool(Deserialized));
333 EXPECT_THAT(Deserialized->ProximityPaths,
334 testing::ElementsAre(
testPath(
"remote/Header.h"),
335 testPath(
"remote/subdir/OtherHeader.h")));
338TEST(RemoteMarshallingTest, RefsRequestSerialization) {
339 clangd::RefsRequest Request;
343 Request.Limit = 9000;
348 auto Serialized = ProtobufMarshaller.toProtobuf(Request);
349 EXPECT_EQ(
static_cast<unsigned>(Serialized.ids_size()), Request.IDs.size());
350 EXPECT_EQ(Serialized.limit(), Request.Limit);
351 auto Deserialized = ProtobufMarshaller.fromProtobuf(&Serialized);
352 ASSERT_TRUE(
bool(Deserialized));
353 EXPECT_EQ(Deserialized->IDs, Request.IDs);
354 ASSERT_TRUE(Deserialized->Limit);
355 EXPECT_EQ(*Deserialized->Limit, Request.Limit);
356 EXPECT_EQ(Deserialized->Filter, Request.Filter);
359TEST(RemoteMarshallingTest, RefsRequestFailingSerialization) {
360 clangd::RefsRequest Request;
362 auto Serialized = ProtobufMarshaller.toProtobuf(Request);
363 Serialized.add_ids(
"Invalid Symbol ID");
364 auto Deserialized = ProtobufMarshaller.fromProtobuf(&Serialized);
365 EXPECT_FALSE(
bool(Deserialized));
366 llvm::consumeError(Deserialized.takeError());
369TEST(RemoteMarshallingTest, RelationsRequestSerialization) {
370 clangd::RelationsRequest Request;
371 Request.Subjects.insert(
373 Request.Subjects.insert(
376 Request.Limit = 9000;
381 auto Serialized = ProtobufMarshaller.toProtobuf(Request);
382 EXPECT_EQ(
static_cast<unsigned>(Serialized.subjects_size()),
383 Request.Subjects.size());
384 EXPECT_EQ(Serialized.limit(), Request.Limit);
385 EXPECT_EQ(
static_cast<RelationKind>(Serialized.predicate()),
387 auto Deserialized = ProtobufMarshaller.fromProtobuf(&Serialized);
388 ASSERT_TRUE(
bool(Deserialized));
389 EXPECT_EQ(Deserialized->Subjects, Request.Subjects);
390 ASSERT_TRUE(Deserialized->Limit);
391 EXPECT_EQ(*Deserialized->Limit, Request.Limit);
392 EXPECT_EQ(Deserialized->Predicate, Request.Predicate);
395TEST(RemoteMarshallingTest, RelationsRequestFailingSerialization) {
396 RelationsRequest Serialized;
397 Serialized.add_subjects(
"ZZZZZZZZZZZZZZZZ");
399 auto Deserialized = ProtobufMarshaller.fromProtobuf(&Serialized);
400 EXPECT_FALSE(
bool(Deserialized));
401 llvm::consumeError(Deserialized.takeError());
404TEST(RemoteMarshallingTest, RelationsSerializion) {
405 llvm::BumpPtrAllocator Arena;
406 llvm::UniqueStringSaver Strings(Arena);
408 clangd::Symbol Sym = createSymbol(
"remote/", Strings);
411 auto Serialized = ProtobufMarshaller.toProtobuf(ID, Sym);
412 ASSERT_TRUE(
bool(Serialized));
413 auto Deserialized = ProtobufMarshaller.fromProtobuf(*Serialized);
414 ASSERT_TRUE(
bool(Deserialized));
415 EXPECT_THAT(Deserialized->first, ID);
416 EXPECT_THAT(Deserialized->second.ID, Sym.ID);
419TEST(RemoteMarshallingTest, RelativePathToURITranslation) {
422 auto URIString = ProtobufMarshaller.relativePathToURI(
"lib/File.cpp");
423 ASSERT_TRUE(
bool(URIString));
425 URIString = ProtobufMarshaller.relativePathToURI(
"/lib/File.cpp");
426 EXPECT_FALSE(
bool(URIString));
427 llvm::consumeError(URIString.takeError());
429 URIString = ProtobufMarshaller.relativePathToURI(std::string());
430 EXPECT_FALSE(
bool(URIString));
431 llvm::consumeError(URIString.takeError());
434TEST(RemoteMarshallingTest, URIToRelativePathTranslation) {
435 llvm::BumpPtrAllocator Arena;
436 llvm::UniqueStringSaver Strings(Arena);
439 auto RelativePath = ProtobufMarshaller.uriToRelativePath(
440 testPathURI(
"remote/project/lib/File.cpp", Strings));
441 ASSERT_TRUE(
bool(RelativePath));
446 RelativePath = WrongMarshaller.uriToRelativePath(
447 testPathURI(
"remote/project/lib/File.cpp", Strings));
448 EXPECT_FALSE(
bool(RelativePath));
449 llvm::consumeError(RelativePath.takeError());
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
static llvm::Expected< URI > create(llvm::StringRef AbsolutePath, llvm::StringRef Scheme)
Creates a URI for a file in the given scheme.
static URI createFile(llvm::StringRef AbsolutePath)
This creates a file:// URI for AbsolutePath. The path must be absolute.
A notable exception is URI translation.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
@ Info
An information message.
static const char * toString(OffsetEncoding OE)
std::string testPath(PathRef File, llvm::sys::path::Style Style)
TEST(BackgroundQueueTest, Priority)
std::string toYAML(const Symbol &)
std::string Path
A typedef to represent a file path.
std::array< uint8_t, 20 > SymbolID
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
@ IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.