blob: 1949d70f57c9f75cea31f475db7b13be741dc40b [file] [log] [blame]
Sam McCall9aad25f2017-12-05 07:20:26 +00001//===-- CodeCompleteTests.cpp -----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eric Liu6f648df2017-12-19 16:50:37 +00009
Sam McCall328cbdb2017-12-20 16:06:05 +000010#include "Annotations.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000011#include "ClangdServer.h"
Sam McCall328cbdb2017-12-20 16:06:05 +000012#include "CodeComplete.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000013#include "Compiler.h"
Ilya Biryukov5a85b8e2017-12-13 12:53:16 +000014#include "Matchers.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000015#include "Protocol.h"
Sam McCalle746a2b2018-07-02 11:13:16 +000016#include "Quality.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000017#include "SourceCode.h"
Ilya Biryukovcd5eb002018-02-12 11:37:28 +000018#include "SyncAPI.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000019#include "TestFS.h"
Eric Liu6f648df2017-12-19 16:50:37 +000020#include "index/MemIndex.h"
Eric Liu5d2a8072018-07-23 10:56:37 +000021#include "clang/Sema/CodeCompleteConsumer.h"
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +000022#include "llvm/Support/Error.h"
Ilya Biryukov981a35d2018-05-28 12:11:37 +000023#include "llvm/Testing/Support/Error.h"
Sam McCallf6ae3232017-12-05 20:11:29 +000024#include "gmock/gmock.h"
Sam McCall9aad25f2017-12-05 07:20:26 +000025#include "gtest/gtest.h"
26
27namespace clang {
28namespace clangd {
Sam McCallf6ae3232017-12-05 20:11:29 +000029
Sam McCall9aad25f2017-12-05 07:20:26 +000030namespace {
31using namespace llvm;
Sam McCallf6ae3232017-12-05 20:11:29 +000032using ::testing::AllOf;
33using ::testing::Contains;
Ilya Biryukov5a5e1ca2017-12-29 14:59:22 +000034using ::testing::Each;
Sam McCallf6ae3232017-12-05 20:11:29 +000035using ::testing::ElementsAre;
Ilya Biryukov71028b82018-03-12 15:28:22 +000036using ::testing::Field;
Sam McCallc18c2802018-06-15 11:06:29 +000037using ::testing::HasSubstr;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +000038using ::testing::IsEmpty;
Sam McCallf6ae3232017-12-05 20:11:29 +000039using ::testing::Not;
Sam McCall3d139c52018-01-12 18:30:08 +000040using ::testing::UnorderedElementsAre;
Sam McCall9aad25f2017-12-05 07:20:26 +000041
42class IgnoreDiagnostics : public DiagnosticsConsumer {
Ilya Biryukov71028b82018-03-12 15:28:22 +000043 void onDiagnosticsReady(PathRef File,
Sam McCalla7bb0cc2018-03-12 23:22:35 +000044 std::vector<Diag> Diagnostics) override {}
Sam McCall9aad25f2017-12-05 07:20:26 +000045};
46
Sam McCallf6ae3232017-12-05 20:11:29 +000047// GMock helpers for matching completion items.
Sam McCalle746a2b2018-07-02 11:13:16 +000048MATCHER_P(Named, Name, "") { return arg.Name == Name; }
49MATCHER_P(Scope, S, "") { return arg.Scope == S; }
50MATCHER_P(Qualifier, Q, "") { return arg.RequiredQualifier == Q; }
Eric Liu8f3678d2018-06-15 13:34:18 +000051MATCHER_P(Labeled, Label, "") {
Sam McCalle746a2b2018-07-02 11:13:16 +000052 return arg.RequiredQualifier + arg.Name + arg.Signature == Label;
Eric Liu8f3678d2018-06-15 13:34:18 +000053}
54MATCHER_P(SigHelpLabeled, Label, "") { return arg.label == Label; }
Sam McCalle746a2b2018-07-02 11:13:16 +000055MATCHER_P(Kind, K, "") { return arg.Kind == K; }
56MATCHER_P(Doc, D, "") { return arg.Documentation == D; }
57MATCHER_P(ReturnType, D, "") { return arg.ReturnType == D; }
Eric Liu63f419a2018-05-15 15:29:32 +000058MATCHER_P(InsertInclude, IncludeHeader, "") {
Sam McCalle746a2b2018-07-02 11:13:16 +000059 return arg.Header == IncludeHeader && bool(arg.HeaderInsertion);
Eric Liu63f419a2018-05-15 15:29:32 +000060}
Sam McCalle746a2b2018-07-02 11:13:16 +000061MATCHER(InsertInclude, "") { return bool(arg.HeaderInsertion); }
62MATCHER_P(SnippetSuffix, Text, "") { return arg.SnippetSuffix == Text; }
Sam McCall2161ec72018-07-05 06:20:41 +000063MATCHER_P(Origin, OriginSet, "") { return arg.Origin == OriginSet; }
Eric Liu63f419a2018-05-15 15:29:32 +000064
Sam McCallf6ae3232017-12-05 20:11:29 +000065// Shorthand for Contains(Named(Name)).
Sam McCalle746a2b2018-07-02 11:13:16 +000066Matcher<const std::vector<CodeCompletion> &> Has(std::string Name) {
Sam McCallf6ae3232017-12-05 20:11:29 +000067 return Contains(Named(std::move(Name)));
68}
Sam McCalle746a2b2018-07-02 11:13:16 +000069Matcher<const std::vector<CodeCompletion> &> Has(std::string Name,
Sam McCall44fdcec22017-12-08 15:00:59 +000070 CompletionItemKind K) {
71 return Contains(AllOf(Named(std::move(Name)), Kind(K)));
Sam McCallf6ae3232017-12-05 20:11:29 +000072}
Sam McCalle746a2b2018-07-02 11:13:16 +000073MATCHER(IsDocumented, "") { return !arg.Documentation.empty(); }
Sam McCall9aad25f2017-12-05 07:20:26 +000074
Sam McCalla15c2d62018-01-18 09:27:56 +000075std::unique_ptr<SymbolIndex> memIndex(std::vector<Symbol> Symbols) {
76 SymbolSlab::Builder Slab;
77 for (const auto &Sym : Symbols)
78 Slab.insert(Sym);
79 return MemIndex::build(std::move(Slab).build());
80}
81
Sam McCalle746a2b2018-07-02 11:13:16 +000082CodeCompleteResult completions(ClangdServer &Server, StringRef Text,
83 std::vector<Symbol> IndexSymbols = {},
84 clangd::CodeCompleteOptions Opts = {}) {
Sam McCalla15c2d62018-01-18 09:27:56 +000085 std::unique_ptr<SymbolIndex> OverrideIndex;
86 if (!IndexSymbols.empty()) {
87 assert(!Opts.Index && "both Index and IndexSymbols given!");
88 OverrideIndex = memIndex(std::move(IndexSymbols));
89 Opts.Index = OverrideIndex.get();
90 }
91
Sam McCallc1568062018-02-16 09:41:43 +000092 auto File = testPath("foo.cpp");
Sam McCall328cbdb2017-12-20 16:06:05 +000093 Annotations Test(Text);
Sam McCall7363a2f2018-03-05 17:28:54 +000094 runAddDocument(Server, File, Test.code());
Sam McCalla7bb0cc2018-03-12 23:22:35 +000095 auto CompletionList =
96 cantFail(runCodeComplete(Server, File, Test.point(), Opts));
Ilya Biryukov5a5e1ca2017-12-29 14:59:22 +000097 return CompletionList;
Sam McCall9aad25f2017-12-05 07:20:26 +000098}
99
Eric Liu63f419a2018-05-15 15:29:32 +0000100// Builds a server and runs code completion.
101// If IndexSymbols is non-empty, an index will be built and passed to opts.
Sam McCalle746a2b2018-07-02 11:13:16 +0000102CodeCompleteResult completions(StringRef Text,
103 std::vector<Symbol> IndexSymbols = {},
104 clangd::CodeCompleteOptions Opts = {}) {
Eric Liu63f419a2018-05-15 15:29:32 +0000105 MockFSProvider FS;
106 MockCompilationDatabase CDB;
107 IgnoreDiagnostics DiagConsumer;
108 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
109 return completions(Server, Text, std::move(IndexSymbols), std::move(Opts));
110}
111
Sam McCall545a20d2018-01-19 14:34:02 +0000112std::string replace(StringRef Haystack, StringRef Needle, StringRef Repl) {
113 std::string Result;
114 raw_string_ostream OS(Result);
115 std::pair<StringRef, StringRef> Split;
116 for (Split = Haystack.split(Needle); !Split.second.empty();
117 Split = Split.first.split(Needle))
118 OS << Split.first << Repl;
119 Result += Split.first;
120 OS.flush();
121 return Result;
122}
123
Sam McCalla15c2d62018-01-18 09:27:56 +0000124// Helpers to produce fake index symbols for memIndex() or completions().
Sam McCall545a20d2018-01-19 14:34:02 +0000125// USRFormat is a regex replacement string for the unqualified part of the USR.
126Symbol sym(StringRef QName, index::SymbolKind Kind, StringRef USRFormat) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000127 Symbol Sym;
Sam McCall545a20d2018-01-19 14:34:02 +0000128 std::string USR = "c:"; // We synthesize a few simple cases of USRs by hand!
Sam McCalla15c2d62018-01-18 09:27:56 +0000129 size_t Pos = QName.rfind("::");
130 if (Pos == llvm::StringRef::npos) {
131 Sym.Name = QName;
132 Sym.Scope = "";
133 } else {
134 Sym.Name = QName.substr(Pos + 2);
Sam McCall8b2faee2018-01-19 22:18:21 +0000135 Sym.Scope = QName.substr(0, Pos + 2);
136 USR += "@N@" + replace(QName.substr(0, Pos), "::", "@N@"); // ns:: -> @N@ns
Sam McCalla15c2d62018-01-18 09:27:56 +0000137 }
Sam McCall545a20d2018-01-19 14:34:02 +0000138 USR += Regex("^.*$").sub(USRFormat, Sym.Name); // e.g. func -> @F@func#
139 Sym.ID = SymbolID(USR);
Sam McCalla15c2d62018-01-18 09:27:56 +0000140 Sym.SymInfo.Kind = Kind;
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000141 Sym.IsIndexedForCodeCompletion = true;
Sam McCall2161ec72018-07-05 06:20:41 +0000142 Sym.Origin = SymbolOrigin::Static;
Sam McCalla15c2d62018-01-18 09:27:56 +0000143 return Sym;
144}
Sam McCall545a20d2018-01-19 14:34:02 +0000145Symbol func(StringRef Name) { // Assumes the function has no args.
146 return sym(Name, index::SymbolKind::Function, "@F@\\0#"); // no args
147}
148Symbol cls(StringRef Name) {
Eric Liu9b3cba72018-05-30 09:03:39 +0000149 return sym(Name, index::SymbolKind::Class, "@S@\\0");
Sam McCall545a20d2018-01-19 14:34:02 +0000150}
151Symbol var(StringRef Name) {
152 return sym(Name, index::SymbolKind::Variable, "@\\0");
153}
Sam McCalldc8abc42018-05-03 14:53:02 +0000154Symbol ns(StringRef Name) {
155 return sym(Name, index::SymbolKind::Namespace, "@N@\\0");
156}
157Symbol withReferences(int N, Symbol S) {
158 S.References = N;
159 return S;
160}
Sam McCalla15c2d62018-01-18 09:27:56 +0000161
Sam McCallf6ae3232017-12-05 20:11:29 +0000162TEST(CompletionTest, Limit) {
163 clangd::CodeCompleteOptions Opts;
164 Opts.Limit = 2;
165 auto Results = completions(R"cpp(
Sam McCall9aad25f2017-12-05 07:20:26 +0000166struct ClassWithMembers {
167 int AAA();
168 int BBB();
169 int CCC();
170}
Sam McCallf6ae3232017-12-05 20:11:29 +0000171int main() { ClassWithMembers().^ }
Sam McCall9aad25f2017-12-05 07:20:26 +0000172 )cpp",
Sam McCalla15c2d62018-01-18 09:27:56 +0000173 /*IndexSymbols=*/{}, Opts);
Sam McCall9aad25f2017-12-05 07:20:26 +0000174
Sam McCalle746a2b2018-07-02 11:13:16 +0000175 EXPECT_TRUE(Results.HasMore);
176 EXPECT_THAT(Results.Completions, ElementsAre(Named("AAA"), Named("BBB")));
Sam McCall9aad25f2017-12-05 07:20:26 +0000177}
178
Sam McCallf6ae3232017-12-05 20:11:29 +0000179TEST(CompletionTest, Filter) {
180 std::string Body = R"cpp(
Sam McCall8b2dcc12018-06-14 13:50:30 +0000181 #define MotorCar
182 int Car;
Sam McCall9aad25f2017-12-05 07:20:26 +0000183 struct S {
184 int FooBar;
185 int FooBaz;
186 int Qux;
187 };
188 )cpp";
Sam McCall8b2dcc12018-06-14 13:50:30 +0000189
190 // Only items matching the fuzzy query are returned.
Sam McCalle746a2b2018-07-02 11:13:16 +0000191 EXPECT_THAT(completions(Body + "int main() { S().Foba^ }").Completions,
Sam McCall8b2dcc12018-06-14 13:50:30 +0000192 AllOf(Has("FooBar"), Has("FooBaz"), Not(Has("Qux"))));
Sam McCall9aad25f2017-12-05 07:20:26 +0000193
Sam McCall8b2dcc12018-06-14 13:50:30 +0000194 // Macros require prefix match.
Sam McCalle746a2b2018-07-02 11:13:16 +0000195 EXPECT_THAT(completions(Body + "int main() { C^ }").Completions,
Sam McCall8b2dcc12018-06-14 13:50:30 +0000196 AllOf(Has("Car"), Not(Has("MotorCar"))));
Sam McCall9aad25f2017-12-05 07:20:26 +0000197}
198
Sam McCallf6ae3232017-12-05 20:11:29 +0000199void TestAfterDotCompletion(clangd::CodeCompleteOptions Opts) {
Sam McCall44fdcec22017-12-08 15:00:59 +0000200 auto Results = completions(
201 R"cpp(
202 #define MACRO X
Sam McCall9aad25f2017-12-05 07:20:26 +0000203
Sam McCall44fdcec22017-12-08 15:00:59 +0000204 int global_var;
Sam McCall9aad25f2017-12-05 07:20:26 +0000205
Sam McCall44fdcec22017-12-08 15:00:59 +0000206 int global_func();
Sam McCall9aad25f2017-12-05 07:20:26 +0000207
Sam McCall44fdcec22017-12-08 15:00:59 +0000208 struct GlobalClass {};
Sam McCall9aad25f2017-12-05 07:20:26 +0000209
Sam McCall44fdcec22017-12-08 15:00:59 +0000210 struct ClassWithMembers {
211 /// Doc for method.
212 int method();
Sam McCall9aad25f2017-12-05 07:20:26 +0000213
Sam McCall44fdcec22017-12-08 15:00:59 +0000214 int field;
215 private:
216 int private_field;
217 };
Sam McCall9aad25f2017-12-05 07:20:26 +0000218
Sam McCall44fdcec22017-12-08 15:00:59 +0000219 int test() {
220 struct LocalClass {};
Sam McCall9aad25f2017-12-05 07:20:26 +0000221
Sam McCall44fdcec22017-12-08 15:00:59 +0000222 /// Doc for local_var.
223 int local_var;
Sam McCall9aad25f2017-12-05 07:20:26 +0000224
Sam McCall44fdcec22017-12-08 15:00:59 +0000225 ClassWithMembers().^
226 }
227 )cpp",
Sam McCall545a20d2018-01-19 14:34:02 +0000228 {cls("IndexClass"), var("index_var"), func("index_func")}, Opts);
Sam McCall9aad25f2017-12-05 07:20:26 +0000229
Sam McCallf6ae3232017-12-05 20:11:29 +0000230 // Class members. The only items that must be present in after-dot
231 // completion.
Sam McCalle746a2b2018-07-02 11:13:16 +0000232 EXPECT_THAT(Results.Completions,
233 AllOf(Has("method"), Has("field"), Not(Has("ClassWithMembers")),
Sam McCall4caa8512018-06-07 12:49:17 +0000234 Not(Has("operator=")), Not(Has("~ClassWithMembers"))));
Sam McCalle746a2b2018-07-02 11:13:16 +0000235 EXPECT_IFF(Opts.IncludeIneligibleResults, Results.Completions,
Sam McCall44fdcec22017-12-08 15:00:59 +0000236 Has("private_field"));
Sam McCallf6ae3232017-12-05 20:11:29 +0000237 // Global items.
Sam McCall545a20d2018-01-19 14:34:02 +0000238 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +0000239 Results.Completions,
Sam McCall545a20d2018-01-19 14:34:02 +0000240 Not(AnyOf(Has("global_var"), Has("index_var"), Has("global_func"),
241 Has("global_func()"), Has("index_func"), Has("GlobalClass"),
242 Has("IndexClass"), Has("MACRO"), Has("LocalClass"))));
Sam McCallf6ae3232017-12-05 20:11:29 +0000243 // There should be no code patterns (aka snippets) in after-dot
244 // completion. At least there aren't any we're aware of.
Sam McCalle746a2b2018-07-02 11:13:16 +0000245 EXPECT_THAT(Results.Completions,
246 Not(Contains(Kind(CompletionItemKind::Snippet))));
Sam McCallf6ae3232017-12-05 20:11:29 +0000247 // Check documentation.
Sam McCalle746a2b2018-07-02 11:13:16 +0000248 EXPECT_IFF(Opts.IncludeComments, Results.Completions,
249 Contains(IsDocumented()));
Sam McCallf6ae3232017-12-05 20:11:29 +0000250}
Sam McCall9aad25f2017-12-05 07:20:26 +0000251
Sam McCallf6ae3232017-12-05 20:11:29 +0000252void TestGlobalScopeCompletion(clangd::CodeCompleteOptions Opts) {
Sam McCall44fdcec22017-12-08 15:00:59 +0000253 auto Results = completions(
254 R"cpp(
255 #define MACRO X
Sam McCall9aad25f2017-12-05 07:20:26 +0000256
Sam McCall44fdcec22017-12-08 15:00:59 +0000257 int global_var;
258 int global_func();
Sam McCall9aad25f2017-12-05 07:20:26 +0000259
Sam McCall44fdcec22017-12-08 15:00:59 +0000260 struct GlobalClass {};
Sam McCall9aad25f2017-12-05 07:20:26 +0000261
Sam McCall44fdcec22017-12-08 15:00:59 +0000262 struct ClassWithMembers {
263 /// Doc for method.
264 int method();
265 };
Sam McCall9aad25f2017-12-05 07:20:26 +0000266
Sam McCall44fdcec22017-12-08 15:00:59 +0000267 int test() {
268 struct LocalClass {};
Sam McCall9aad25f2017-12-05 07:20:26 +0000269
Sam McCall44fdcec22017-12-08 15:00:59 +0000270 /// Doc for local_var.
271 int local_var;
Sam McCall9aad25f2017-12-05 07:20:26 +0000272
Sam McCall44fdcec22017-12-08 15:00:59 +0000273 ^
274 }
275 )cpp",
Sam McCall545a20d2018-01-19 14:34:02 +0000276 {cls("IndexClass"), var("index_var"), func("index_func")}, Opts);
Sam McCallf6ae3232017-12-05 20:11:29 +0000277
278 // Class members. Should never be present in global completions.
Sam McCalle746a2b2018-07-02 11:13:16 +0000279 EXPECT_THAT(Results.Completions,
Sam McCallf6ae3232017-12-05 20:11:29 +0000280 Not(AnyOf(Has("method"), Has("method()"), Has("field"))));
281 // Global items.
Sam McCalle746a2b2018-07-02 11:13:16 +0000282 EXPECT_THAT(Results.Completions,
283 AllOf(Has("global_var"), Has("index_var"), Has("global_func"),
Sam McCall545a20d2018-01-19 14:34:02 +0000284 Has("index_func" /* our fake symbol doesn't include () */),
285 Has("GlobalClass"), Has("IndexClass")));
Sam McCallf6ae3232017-12-05 20:11:29 +0000286 // A macro.
Sam McCalle746a2b2018-07-02 11:13:16 +0000287 EXPECT_IFF(Opts.IncludeMacros, Results.Completions, Has("MACRO"));
Sam McCallf6ae3232017-12-05 20:11:29 +0000288 // Local items. Must be present always.
Sam McCalle746a2b2018-07-02 11:13:16 +0000289 EXPECT_THAT(Results.Completions,
Ilya Biryukov9b5ffc22017-12-12 12:56:46 +0000290 AllOf(Has("local_var"), Has("LocalClass"),
291 Contains(Kind(CompletionItemKind::Snippet))));
Sam McCallf6ae3232017-12-05 20:11:29 +0000292 // Check documentation.
Sam McCalle746a2b2018-07-02 11:13:16 +0000293 EXPECT_IFF(Opts.IncludeComments, Results.Completions,
294 Contains(IsDocumented()));
Sam McCallf6ae3232017-12-05 20:11:29 +0000295}
296
297TEST(CompletionTest, CompletionOptions) {
Sam McCall2c3849a2018-01-16 12:21:24 +0000298 auto Test = [&](const clangd::CodeCompleteOptions &Opts) {
299 TestAfterDotCompletion(Opts);
300 TestGlobalScopeCompletion(Opts);
301 };
302 // We used to test every combination of options, but that got too slow (2^N).
303 auto Flags = {
Ilya Biryukov71028b82018-03-12 15:28:22 +0000304 &clangd::CodeCompleteOptions::IncludeMacros,
Ilya Biryukov43714502018-05-16 12:32:44 +0000305 &clangd::CodeCompleteOptions::IncludeComments,
Ilya Biryukov71028b82018-03-12 15:28:22 +0000306 &clangd::CodeCompleteOptions::IncludeCodePatterns,
307 &clangd::CodeCompleteOptions::IncludeIneligibleResults,
Sam McCall2c3849a2018-01-16 12:21:24 +0000308 };
309 // Test default options.
310 Test({});
311 // Test with one flag flipped.
312 for (auto &F : Flags) {
313 clangd::CodeCompleteOptions O;
314 O.*F ^= true;
315 Test(O);
Sam McCall9aad25f2017-12-05 07:20:26 +0000316 }
317}
318
Sam McCall44fdcec22017-12-08 15:00:59 +0000319TEST(CompletionTest, Priorities) {
320 auto Internal = completions(R"cpp(
321 class Foo {
322 public: void pub();
323 protected: void prot();
324 private: void priv();
325 };
326 void Foo::pub() { this->^ }
327 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000328 EXPECT_THAT(Internal.Completions,
Sam McCall44fdcec22017-12-08 15:00:59 +0000329 HasSubsequence(Named("priv"), Named("prot"), Named("pub")));
330
331 auto External = completions(R"cpp(
332 class Foo {
333 public: void pub();
334 protected: void prot();
335 private: void priv();
336 };
337 void test() {
338 Foo F;
339 F.^
340 }
341 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000342 EXPECT_THAT(External.Completions,
Sam McCall44fdcec22017-12-08 15:00:59 +0000343 AllOf(Has("pub"), Not(Has("prot")), Not(Has("priv"))));
344}
345
346TEST(CompletionTest, Qualifiers) {
347 auto Results = completions(R"cpp(
348 class Foo {
349 public: int foo() const;
350 int bar() const;
351 };
352 class Bar : public Foo {
353 int foo() const;
354 };
355 void test() { Bar().^ }
356 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000357 EXPECT_THAT(Results.Completions,
358 HasSubsequence(AllOf(Qualifier(""), Named("bar")),
359 AllOf(Qualifier("Foo::"), Named("foo"))));
360 EXPECT_THAT(Results.Completions,
361 Not(Contains(AllOf(Qualifier(""), Named("foo"))))); // private
Sam McCall44fdcec22017-12-08 15:00:59 +0000362}
363
Sam McCall4caa8512018-06-07 12:49:17 +0000364TEST(CompletionTest, InjectedTypename) {
365 // These are suppressed when accessed as a member...
Sam McCalle746a2b2018-07-02 11:13:16 +0000366 EXPECT_THAT(completions("struct X{}; void foo(){ X().^ }").Completions,
Sam McCall4caa8512018-06-07 12:49:17 +0000367 Not(Has("X")));
Sam McCalle746a2b2018-07-02 11:13:16 +0000368 EXPECT_THAT(completions("struct X{ void foo(){ this->^ } };").Completions,
Sam McCall4caa8512018-06-07 12:49:17 +0000369 Not(Has("X")));
370 // ...but accessible in other, more useful cases.
Sam McCalle746a2b2018-07-02 11:13:16 +0000371 EXPECT_THAT(completions("struct X{ void foo(){ ^ } };").Completions,
372 Has("X"));
373 EXPECT_THAT(
374 completions("struct Y{}; struct X:Y{ void foo(){ ^ } };").Completions,
375 Has("Y"));
Sam McCall4caa8512018-06-07 12:49:17 +0000376 EXPECT_THAT(
377 completions(
378 "template<class> struct Y{}; struct X:Y<int>{ void foo(){ ^ } };")
Sam McCalle746a2b2018-07-02 11:13:16 +0000379 .Completions,
Sam McCall4caa8512018-06-07 12:49:17 +0000380 Has("Y"));
381 // This case is marginal (`using X::X` is useful), we allow it for now.
Sam McCalle746a2b2018-07-02 11:13:16 +0000382 EXPECT_THAT(completions("struct X{}; void foo(){ X::^ }").Completions,
383 Has("X"));
Sam McCall4caa8512018-06-07 12:49:17 +0000384}
385
Sam McCall44fdcec22017-12-08 15:00:59 +0000386TEST(CompletionTest, Snippets) {
387 clangd::CodeCompleteOptions Opts;
Sam McCall44fdcec22017-12-08 15:00:59 +0000388 auto Results = completions(
389 R"cpp(
390 struct fake {
391 int a;
392 int f(int i, const float f) const;
393 };
394 int main() {
395 fake f;
396 f.^
397 }
398 )cpp",
Sam McCalla15c2d62018-01-18 09:27:56 +0000399 /*IndexSymbols=*/{}, Opts);
Sam McCalle746a2b2018-07-02 11:13:16 +0000400 EXPECT_THAT(
401 Results.Completions,
402 HasSubsequence(Named("a"),
403 SnippetSuffix("(${1:int i}, ${2:const float f})")));
Sam McCall44fdcec22017-12-08 15:00:59 +0000404}
405
406TEST(CompletionTest, Kinds) {
Sam McCall545a20d2018-01-19 14:34:02 +0000407 auto Results = completions(
408 R"cpp(
409 #define MACRO X
410 int variable;
411 struct Struct {};
412 int function();
413 int X = ^
414 )cpp",
415 {func("indexFunction"), var("indexVariable"), cls("indexClass")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000416 EXPECT_THAT(Results.Completions,
Sam McCall545a20d2018-01-19 14:34:02 +0000417 AllOf(Has("function", CompletionItemKind::Function),
418 Has("variable", CompletionItemKind::Variable),
419 Has("int", CompletionItemKind::Keyword),
420 Has("Struct", CompletionItemKind::Class),
421 Has("MACRO", CompletionItemKind::Text),
422 Has("indexFunction", CompletionItemKind::Function),
423 Has("indexVariable", CompletionItemKind::Variable),
424 Has("indexClass", CompletionItemKind::Class)));
Sam McCall44fdcec22017-12-08 15:00:59 +0000425
Sam McCall44fdcec22017-12-08 15:00:59 +0000426 Results = completions("nam^");
Sam McCalle746a2b2018-07-02 11:13:16 +0000427 EXPECT_THAT(Results.Completions,
428 Has("namespace", CompletionItemKind::Snippet));
Sam McCall44fdcec22017-12-08 15:00:59 +0000429}
430
Sam McCall84652cc2018-01-12 16:16:09 +0000431TEST(CompletionTest, NoDuplicates) {
Sam McCall545a20d2018-01-19 14:34:02 +0000432 auto Results = completions(
433 R"cpp(
434 class Adapter {
Sam McCall545a20d2018-01-19 14:34:02 +0000435 };
Sam McCall84652cc2018-01-12 16:16:09 +0000436
Eric Liu9b3cba72018-05-30 09:03:39 +0000437 void f() {
Sam McCall545a20d2018-01-19 14:34:02 +0000438 Adapter^
439 }
440 )cpp",
441 {cls("Adapter")});
Sam McCall84652cc2018-01-12 16:16:09 +0000442
443 // Make sure there are no duplicate entries of 'Adapter'.
Sam McCalle746a2b2018-07-02 11:13:16 +0000444 EXPECT_THAT(Results.Completions, ElementsAre(Named("Adapter")));
Sam McCall84652cc2018-01-12 16:16:09 +0000445}
446
Sam McCall545a20d2018-01-19 14:34:02 +0000447TEST(CompletionTest, ScopedNoIndex) {
448 auto Results = completions(
449 R"cpp(
Sam McCall8b2dcc12018-06-14 13:50:30 +0000450 namespace fake { int BigBang, Babble, Box; };
451 int main() { fake::ba^ }
Sam McCall545a20d2018-01-19 14:34:02 +0000452 ")cpp");
Sam McCall8b2dcc12018-06-14 13:50:30 +0000453 // Babble is a better match than BigBang. Box doesn't match at all.
Sam McCalle746a2b2018-07-02 11:13:16 +0000454 EXPECT_THAT(Results.Completions,
455 ElementsAre(Named("Babble"), Named("BigBang")));
Sam McCall84652cc2018-01-12 16:16:09 +0000456}
457
Sam McCall545a20d2018-01-19 14:34:02 +0000458TEST(CompletionTest, Scoped) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000459 auto Results = completions(
460 R"cpp(
Sam McCall8b2dcc12018-06-14 13:50:30 +0000461 namespace fake { int Babble, Box; };
462 int main() { fake::ba^ }
Sam McCall545a20d2018-01-19 14:34:02 +0000463 ")cpp",
464 {var("fake::BigBang")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000465 EXPECT_THAT(Results.Completions,
466 ElementsAre(Named("Babble"), Named("BigBang")));
Sam McCalla15c2d62018-01-18 09:27:56 +0000467}
468
Sam McCall545a20d2018-01-19 14:34:02 +0000469TEST(CompletionTest, ScopedWithFilter) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000470 auto Results = completions(
471 R"cpp(
472 void f() { ns::x^ }
473 )cpp",
474 {cls("ns::XYZ"), func("ns::foo")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000475 EXPECT_THAT(Results.Completions, UnorderedElementsAre(Named("XYZ")));
Sam McCalla15c2d62018-01-18 09:27:56 +0000476}
477
Sam McCalldc8abc42018-05-03 14:53:02 +0000478TEST(CompletionTest, ReferencesAffectRanking) {
479 auto Results = completions("int main() { abs^ }", {ns("absl"), func("abs")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000480 EXPECT_THAT(Results.Completions, HasSubsequence(Named("abs"), Named("absl")));
Sam McCalldc8abc42018-05-03 14:53:02 +0000481 Results = completions("int main() { abs^ }",
482 {withReferences(10000, ns("absl")), func("abs")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000483 EXPECT_THAT(Results.Completions, HasSubsequence(Named("absl"), Named("abs")));
Sam McCalldc8abc42018-05-03 14:53:02 +0000484}
485
Sam McCall545a20d2018-01-19 14:34:02 +0000486TEST(CompletionTest, GlobalQualified) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000487 auto Results = completions(
488 R"cpp(
489 void f() { ::^ }
490 )cpp",
491 {cls("XYZ")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000492 EXPECT_THAT(Results.Completions,
493 AllOf(Has("XYZ", CompletionItemKind::Class),
494 Has("f", CompletionItemKind::Function)));
Sam McCalla15c2d62018-01-18 09:27:56 +0000495}
496
Sam McCall545a20d2018-01-19 14:34:02 +0000497TEST(CompletionTest, FullyQualified) {
Sam McCalla15c2d62018-01-18 09:27:56 +0000498 auto Results = completions(
499 R"cpp(
Sam McCall545a20d2018-01-19 14:34:02 +0000500 namespace ns { void bar(); }
Sam McCalla15c2d62018-01-18 09:27:56 +0000501 void f() { ::ns::^ }
502 )cpp",
503 {cls("ns::XYZ")});
Sam McCalle746a2b2018-07-02 11:13:16 +0000504 EXPECT_THAT(Results.Completions,
505 AllOf(Has("XYZ", CompletionItemKind::Class),
506 Has("bar", CompletionItemKind::Function)));
Sam McCall545a20d2018-01-19 14:34:02 +0000507}
508
509TEST(CompletionTest, SemaIndexMerge) {
510 auto Results = completions(
511 R"cpp(
512 namespace ns { int local; void both(); }
513 void f() { ::ns::^ }
514 )cpp",
515 {func("ns::both"), cls("ns::Index")});
516 // We get results from both index and sema, with no duplicates.
Sam McCall2161ec72018-07-05 06:20:41 +0000517 EXPECT_THAT(Results.Completions,
518 UnorderedElementsAre(
519 AllOf(Named("local"), Origin(SymbolOrigin::AST)),
520 AllOf(Named("Index"), Origin(SymbolOrigin::Static)),
521 AllOf(Named("both"),
522 Origin(SymbolOrigin::AST | SymbolOrigin::Static))));
Sam McCalla15c2d62018-01-18 09:27:56 +0000523}
524
Haojian Wu48b48652018-01-25 09:20:09 +0000525TEST(CompletionTest, SemaIndexMergeWithLimit) {
526 clangd::CodeCompleteOptions Opts;
527 Opts.Limit = 1;
528 auto Results = completions(
529 R"cpp(
530 namespace ns { int local; void both(); }
531 void f() { ::ns::^ }
532 )cpp",
533 {func("ns::both"), cls("ns::Index")}, Opts);
Sam McCalle746a2b2018-07-02 11:13:16 +0000534 EXPECT_EQ(Results.Completions.size(), Opts.Limit);
535 EXPECT_TRUE(Results.HasMore);
Haojian Wu48b48652018-01-25 09:20:09 +0000536}
537
Eric Liu63f419a2018-05-15 15:29:32 +0000538TEST(CompletionTest, IncludeInsertionPreprocessorIntegrationTests) {
539 MockFSProvider FS;
540 MockCompilationDatabase CDB;
541 std::string Subdir = testPath("sub");
542 std::string SearchDirArg = (llvm::Twine("-I") + Subdir).str();
543 CDB.ExtraClangFlags = {SearchDirArg.c_str()};
544 std::string BarHeader = testPath("sub/bar.h");
545 FS.Files[BarHeader] = "";
546
547 IgnoreDiagnostics DiagConsumer;
548 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
549 Symbol::Details Scratch;
550 auto BarURI = URI::createFile(BarHeader).toString();
551 Symbol Sym = cls("ns::X");
552 Sym.CanonicalDeclaration.FileURI = BarURI;
553 Scratch.IncludeHeader = BarURI;
554 Sym.Detail = &Scratch;
555 // Shoten include path based on search dirctory and insert.
556 auto Results = completions(Server,
557 R"cpp(
558 int main() { ns::^ }
559 )cpp",
560 {Sym});
Sam McCalle746a2b2018-07-02 11:13:16 +0000561 EXPECT_THAT(Results.Completions,
562 ElementsAre(AllOf(Named("X"), InsertInclude("\"bar.h\""))));
Eric Liu63f419a2018-05-15 15:29:32 +0000563 // Duplicate based on inclusions in preamble.
564 Results = completions(Server,
565 R"cpp(
566 #include "sub/bar.h" // not shortest, so should only match resolved.
567 int main() { ns::^ }
568 )cpp",
569 {Sym});
Sam McCalle746a2b2018-07-02 11:13:16 +0000570 EXPECT_THAT(Results.Completions, ElementsAre(AllOf(Named("X"), Labeled("X"),
571 Not(InsertInclude()))));
Eric Liu63f419a2018-05-15 15:29:32 +0000572}
573
Eric Liu9b3cba72018-05-30 09:03:39 +0000574TEST(CompletionTest, NoIncludeInsertionWhenDeclFoundInFile) {
575 MockFSProvider FS;
576 MockCompilationDatabase CDB;
577
578 IgnoreDiagnostics DiagConsumer;
579 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
580 Symbol::Details Scratch;
581 Symbol SymX = cls("ns::X");
582 Symbol SymY = cls("ns::Y");
583 std::string BarHeader = testPath("bar.h");
584 auto BarURI = URI::createFile(BarHeader).toString();
585 SymX.CanonicalDeclaration.FileURI = BarURI;
586 SymY.CanonicalDeclaration.FileURI = BarURI;
587 Scratch.IncludeHeader = "<bar>";
588 SymX.Detail = &Scratch;
589 SymY.Detail = &Scratch;
590 // Shoten include path based on search dirctory and insert.
591 auto Results = completions(Server,
592 R"cpp(
593 namespace ns {
594 class X;
595 class Y {}
596 }
597 int main() { ns::^ }
598 )cpp",
599 {SymX, SymY});
Sam McCalle746a2b2018-07-02 11:13:16 +0000600 EXPECT_THAT(Results.Completions,
601 ElementsAre(AllOf(Named("X"), Not(InsertInclude())),
602 AllOf(Named("Y"), Not(InsertInclude()))));
Eric Liu9b3cba72018-05-30 09:03:39 +0000603}
604
Sam McCalla15c2d62018-01-18 09:27:56 +0000605TEST(CompletionTest, IndexSuppressesPreambleCompletions) {
606 MockFSProvider FS;
607 MockCompilationDatabase CDB;
608 IgnoreDiagnostics DiagConsumer;
Sam McCall7363a2f2018-03-05 17:28:54 +0000609 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
Sam McCalla15c2d62018-01-18 09:27:56 +0000610
Sam McCallc1568062018-02-16 09:41:43 +0000611 FS.Files[testPath("bar.h")] =
Sam McCalld5ea3e32018-01-24 17:53:32 +0000612 R"cpp(namespace ns { struct preamble { int member; }; })cpp";
Sam McCallc1568062018-02-16 09:41:43 +0000613 auto File = testPath("foo.cpp");
Sam McCalla15c2d62018-01-18 09:27:56 +0000614 Annotations Test(R"cpp(
615 #include "bar.h"
616 namespace ns { int local; }
Sam McCalld5ea3e32018-01-24 17:53:32 +0000617 void f() { ns::^; }
618 void f() { ns::preamble().$2^; }
Sam McCalla15c2d62018-01-18 09:27:56 +0000619 )cpp");
Sam McCall7363a2f2018-03-05 17:28:54 +0000620 runAddDocument(Server, File, Test.code());
Sam McCalla15c2d62018-01-18 09:27:56 +0000621 clangd::CodeCompleteOptions Opts = {};
622
Sam McCalla15c2d62018-01-18 09:27:56 +0000623 auto I = memIndex({var("ns::index")});
624 Opts.Index = I.get();
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000625 auto WithIndex = cantFail(runCodeComplete(Server, File, Test.point(), Opts));
Sam McCalle746a2b2018-07-02 11:13:16 +0000626 EXPECT_THAT(WithIndex.Completions,
Sam McCalla15c2d62018-01-18 09:27:56 +0000627 UnorderedElementsAre(Named("local"), Named("index")));
Sam McCalld5ea3e32018-01-24 17:53:32 +0000628 auto ClassFromPreamble =
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000629 cantFail(runCodeComplete(Server, File, Test.point("2"), Opts));
Sam McCalle746a2b2018-07-02 11:13:16 +0000630 EXPECT_THAT(ClassFromPreamble.Completions, Contains(Named("member")));
Sam McCall0bb24cd2018-02-13 08:59:23 +0000631
632 Opts.Index = nullptr;
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000633 auto WithoutIndex =
634 cantFail(runCodeComplete(Server, File, Test.point(), Opts));
Sam McCalle746a2b2018-07-02 11:13:16 +0000635 EXPECT_THAT(WithoutIndex.Completions,
Sam McCall0bb24cd2018-02-13 08:59:23 +0000636 UnorderedElementsAre(Named("local"), Named("preamble")));
Sam McCalla15c2d62018-01-18 09:27:56 +0000637}
638
639TEST(CompletionTest, DynamicIndexMultiFile) {
640 MockFSProvider FS;
641 MockCompilationDatabase CDB;
642 IgnoreDiagnostics DiagConsumer;
Sam McCall7363a2f2018-03-05 17:28:54 +0000643 auto Opts = ClangdServer::optsForTest();
644 Opts.BuildDynamicSymbolIndex = true;
645 ClangdServer Server(CDB, FS, DiagConsumer, Opts);
Sam McCalla15c2d62018-01-18 09:27:56 +0000646
Eric Liu709bde82018-02-19 18:48:44 +0000647 FS.Files[testPath("foo.h")] = R"cpp(
Sam McCalla15c2d62018-01-18 09:27:56 +0000648 namespace ns { class XYZ {}; void foo(int x) {} }
Eric Liu709bde82018-02-19 18:48:44 +0000649 )cpp";
Sam McCall7363a2f2018-03-05 17:28:54 +0000650 runAddDocument(Server, testPath("foo.cpp"), R"cpp(
Eric Liu709bde82018-02-19 18:48:44 +0000651 #include "foo.h"
Sam McCall0bb24cd2018-02-13 08:59:23 +0000652 )cpp");
Sam McCalla15c2d62018-01-18 09:27:56 +0000653
Sam McCallc1568062018-02-16 09:41:43 +0000654 auto File = testPath("bar.cpp");
Sam McCalla15c2d62018-01-18 09:27:56 +0000655 Annotations Test(R"cpp(
656 namespace ns {
657 class XXX {};
658 /// Doooc
659 void fooooo() {}
660 }
661 void f() { ns::^ }
662 )cpp");
Sam McCall7363a2f2018-03-05 17:28:54 +0000663 runAddDocument(Server, File, Test.code());
Sam McCalla15c2d62018-01-18 09:27:56 +0000664
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000665 auto Results = cantFail(runCodeComplete(Server, File, Test.point(), {}));
Sam McCalla15c2d62018-01-18 09:27:56 +0000666 // "XYZ" and "foo" are not included in the file being completed but are still
667 // visible through the index.
Sam McCalle746a2b2018-07-02 11:13:16 +0000668 EXPECT_THAT(Results.Completions, Has("XYZ", CompletionItemKind::Class));
669 EXPECT_THAT(Results.Completions, Has("foo", CompletionItemKind::Function));
670 EXPECT_THAT(Results.Completions, Has("XXX", CompletionItemKind::Class));
671 EXPECT_THAT(Results.Completions,
672 Contains((Named("fooooo"), Kind(CompletionItemKind::Function),
673 Doc("Doooc"), ReturnType("void"))));
Sam McCalla15c2d62018-01-18 09:27:56 +0000674}
675
Ilya Biryukovc22d3442018-05-16 12:32:49 +0000676TEST(CompletionTest, Documentation) {
677 auto Results = completions(
678 R"cpp(
679 // Non-doxygen comment.
680 int foo();
681 /// Doxygen comment.
682 /// \param int a
683 int bar(int a);
684 /* Multi-line
685 block comment
686 */
687 int baz();
688
689 int x = ^
690 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000691 EXPECT_THAT(Results.Completions,
Ilya Biryukovc22d3442018-05-16 12:32:49 +0000692 Contains(AllOf(Named("foo"), Doc("Non-doxygen comment."))));
693 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +0000694 Results.Completions,
Ilya Biryukovc22d3442018-05-16 12:32:49 +0000695 Contains(AllOf(Named("bar"), Doc("Doxygen comment.\n\\param int a"))));
Sam McCalle746a2b2018-07-02 11:13:16 +0000696 EXPECT_THAT(Results.Completions,
Ilya Biryukovc22d3442018-05-16 12:32:49 +0000697 Contains(AllOf(Named("baz"), Doc("Multi-line\nblock comment"))));
698}
699
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000700TEST(CompletionTest, GlobalCompletionFiltering) {
701
702 Symbol Class = cls("XYZ");
703 Class.IsIndexedForCodeCompletion = false;
704 Symbol Func = func("XYZ::foooo");
705 Func.IsIndexedForCodeCompletion = false;
706
707 auto Results = completions(R"(// void f() {
708 XYZ::foooo^
709 })",
710 {Class, Func});
Sam McCalle746a2b2018-07-02 11:13:16 +0000711 EXPECT_THAT(Results.Completions, IsEmpty());
Marc-Andre Laperle945b5a32018-06-05 14:01:40 +0000712}
713
Haojian Wu58d208d2018-01-25 09:44:06 +0000714TEST(CodeCompleteTest, DisableTypoCorrection) {
715 auto Results = completions(R"cpp(
716 namespace clang { int v; }
717 void f() { clangd::^
718 )cpp");
Sam McCalle746a2b2018-07-02 11:13:16 +0000719 EXPECT_TRUE(Results.Completions.empty());
Haojian Wu58d208d2018-01-25 09:44:06 +0000720}
721
Ilya Biryukov53d6d932018-03-06 16:45:21 +0000722TEST(CodeCompleteTest, NoColonColonAtTheEnd) {
723 auto Results = completions(R"cpp(
724 namespace clang { }
725 void f() {
726 clan^
727 }
728 )cpp");
729
Sam McCalle746a2b2018-07-02 11:13:16 +0000730 EXPECT_THAT(Results.Completions, Contains(Labeled("clang")));
731 EXPECT_THAT(Results.Completions, Not(Contains(Labeled("clang::"))));
Ilya Biryukov53d6d932018-03-06 16:45:21 +0000732}
733
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000734TEST(CompletionTest, BacktrackCrashes) {
735 // Sema calls code completion callbacks twice in these cases.
736 auto Results = completions(R"cpp(
737 namespace ns {
738 struct FooBarBaz {};
739 } // namespace ns
740
741 int foo(ns::FooBar^
742 )cpp");
743
Sam McCalle746a2b2018-07-02 11:13:16 +0000744 EXPECT_THAT(Results.Completions, ElementsAre(Labeled("FooBarBaz")));
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000745
746 // Check we don't crash in that case too.
747 completions(R"cpp(
748 struct FooBarBaz {};
749 void test() {
750 if (FooBarBaz * x^) {}
751 }
752)cpp");
753}
754
Eric Liu42abe412018-05-24 11:20:19 +0000755TEST(CompletionTest, CompleteInMacroWithStringification) {
756 auto Results = completions(R"cpp(
757void f(const char *, int x);
758#define F(x) f(#x, x)
759
760namespace ns {
761int X;
762int Y;
763} // namespace ns
764
765int f(int input_num) {
766 F(ns::^)
767}
768)cpp");
769
Sam McCalle746a2b2018-07-02 11:13:16 +0000770 EXPECT_THAT(Results.Completions,
Eric Liu42abe412018-05-24 11:20:19 +0000771 UnorderedElementsAre(Named("X"), Named("Y")));
772}
773
774TEST(CompletionTest, CompleteInMacroAndNamespaceWithStringification) {
775 auto Results = completions(R"cpp(
776void f(const char *, int x);
777#define F(x) f(#x, x)
778
779namespace ns {
780int X;
781
782int f(int input_num) {
783 F(^)
784}
785} // namespace ns
786)cpp");
787
Sam McCalle746a2b2018-07-02 11:13:16 +0000788 EXPECT_THAT(Results.Completions, Contains(Named("X")));
Eric Liu42abe412018-05-24 11:20:19 +0000789}
790
Eric Liu485074f2018-07-11 13:15:31 +0000791TEST(CompletionTest, IgnoreCompleteInExcludedPPBranchWithRecoveryContext) {
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000792 auto Results = completions(R"cpp(
793 int bar(int param_in_bar) {
794 }
795
796 int foo(int param_in_foo) {
797#if 0
Eric Liu485074f2018-07-11 13:15:31 +0000798 // In recorvery mode, "param_in_foo" will also be suggested among many other
799 // unrelated symbols; however, this is really a special case where this works.
800 // If the #if block is outside of the function, "param_in_foo" is still
801 // suggested, but "bar" and "foo" are missing. So the recovery mode doesn't
802 // really provide useful results in excluded branches.
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000803 par^
804#endif
805 }
806)cpp");
807
Eric Liu485074f2018-07-11 13:15:31 +0000808 EXPECT_TRUE(Results.Completions.empty());
Ilya Biryukov94da7bd2018-03-16 15:23:44 +0000809}
810
Sam McCall800d4372017-12-19 10:29:27 +0000811SignatureHelp signatures(StringRef Text) {
812 MockFSProvider FS;
813 MockCompilationDatabase CDB;
814 IgnoreDiagnostics DiagConsumer;
Sam McCall7363a2f2018-03-05 17:28:54 +0000815 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
Sam McCallc1568062018-02-16 09:41:43 +0000816 auto File = testPath("foo.cpp");
Sam McCall328cbdb2017-12-20 16:06:05 +0000817 Annotations Test(Text);
Sam McCall7363a2f2018-03-05 17:28:54 +0000818 runAddDocument(Server, File, Test.code());
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000819 return cantFail(runSignatureHelp(Server, File, Test.point()));
Sam McCall800d4372017-12-19 10:29:27 +0000820}
821
822MATCHER_P(ParamsAre, P, "") {
823 if (P.size() != arg.parameters.size())
824 return false;
825 for (unsigned I = 0; I < P.size(); ++I)
826 if (P[I] != arg.parameters[I].label)
827 return false;
828 return true;
829}
830
831Matcher<SignatureInformation> Sig(std::string Label,
832 std::vector<std::string> Params) {
Eric Liu8f3678d2018-06-15 13:34:18 +0000833 return AllOf(SigHelpLabeled(Label), ParamsAre(Params));
Sam McCall800d4372017-12-19 10:29:27 +0000834}
835
836TEST(SignatureHelpTest, Overloads) {
837 auto Results = signatures(R"cpp(
838 void foo(int x, int y);
839 void foo(int x, float y);
840 void foo(float x, int y);
841 void foo(float x, float y);
842 void bar(int x, int y = 0);
843 int main() { foo(^); }
844 )cpp");
845 EXPECT_THAT(Results.signatures,
846 UnorderedElementsAre(
847 Sig("foo(float x, float y) -> void", {"float x", "float y"}),
848 Sig("foo(float x, int y) -> void", {"float x", "int y"}),
849 Sig("foo(int x, float y) -> void", {"int x", "float y"}),
850 Sig("foo(int x, int y) -> void", {"int x", "int y"})));
851 // We always prefer the first signature.
852 EXPECT_EQ(0, Results.activeSignature);
853 EXPECT_EQ(0, Results.activeParameter);
854}
855
856TEST(SignatureHelpTest, DefaultArgs) {
857 auto Results = signatures(R"cpp(
858 void bar(int x, int y = 0);
859 void bar(float x = 0, int y = 42);
860 int main() { bar(^
861 )cpp");
862 EXPECT_THAT(Results.signatures,
863 UnorderedElementsAre(
864 Sig("bar(int x, int y = 0) -> void", {"int x", "int y = 0"}),
865 Sig("bar(float x = 0, int y = 42) -> void",
866 {"float x = 0", "int y = 42"})));
867 EXPECT_EQ(0, Results.activeSignature);
868 EXPECT_EQ(0, Results.activeParameter);
869}
870
871TEST(SignatureHelpTest, ActiveArg) {
872 auto Results = signatures(R"cpp(
873 int baz(int a, int b, int c);
874 int main() { baz(baz(1,2,3), ^); }
875 )cpp");
876 EXPECT_THAT(Results.signatures,
877 ElementsAre(Sig("baz(int a, int b, int c) -> int",
878 {"int a", "int b", "int c"})));
879 EXPECT_EQ(0, Results.activeSignature);
880 EXPECT_EQ(1, Results.activeParameter);
881}
882
Haojian Wu061c73e2018-01-23 11:37:26 +0000883class IndexRequestCollector : public SymbolIndex {
884public:
885 bool
Sam McCalld1a7a372018-01-31 13:40:48 +0000886 fuzzyFind(const FuzzyFindRequest &Req,
Haojian Wu061c73e2018-01-23 11:37:26 +0000887 llvm::function_ref<void(const Symbol &)> Callback) const override {
888 Requests.push_back(Req);
Sam McCallab8e3932018-02-19 13:04:41 +0000889 return true;
Haojian Wu061c73e2018-01-23 11:37:26 +0000890 }
891
Eric Liu9ec459f2018-03-14 09:48:05 +0000892 void lookup(const LookupRequest &,
893 llvm::function_ref<void(const Symbol &)>) const override {}
894
Haojian Wu061c73e2018-01-23 11:37:26 +0000895 const std::vector<FuzzyFindRequest> allRequests() const { return Requests; }
896
897private:
898 mutable std::vector<FuzzyFindRequest> Requests;
899};
900
901std::vector<FuzzyFindRequest> captureIndexRequests(llvm::StringRef Code) {
902 clangd::CodeCompleteOptions Opts;
903 IndexRequestCollector Requests;
904 Opts.Index = &Requests;
905 completions(Code, {}, Opts);
906 return Requests.allRequests();
907}
908
909TEST(CompletionTest, UnqualifiedIdQuery) {
910 auto Requests = captureIndexRequests(R"cpp(
911 namespace std {}
912 using namespace std;
913 namespace ns {
914 void f() {
915 vec^
916 }
917 }
918 )cpp");
919
920 EXPECT_THAT(Requests,
921 ElementsAre(Field(&FuzzyFindRequest::Scopes,
922 UnorderedElementsAre("", "ns::", "std::"))));
923}
924
925TEST(CompletionTest, ResolvedQualifiedIdQuery) {
926 auto Requests = captureIndexRequests(R"cpp(
927 namespace ns1 {}
928 namespace ns2 {} // ignore
929 namespace ns3 { namespace nns3 {} }
930 namespace foo {
931 using namespace ns1;
932 using namespace ns3::nns3;
933 }
934 namespace ns {
935 void f() {
936 foo::^
937 }
938 }
939 )cpp");
940
941 EXPECT_THAT(Requests,
942 ElementsAre(Field(
943 &FuzzyFindRequest::Scopes,
944 UnorderedElementsAre("foo::", "ns1::", "ns3::nns3::"))));
945}
946
947TEST(CompletionTest, UnresolvedQualifierIdQuery) {
948 auto Requests = captureIndexRequests(R"cpp(
949 namespace a {}
950 using namespace a;
951 namespace ns {
952 void f() {
953 bar::^
954 }
955 } // namespace ns
956 )cpp");
957
958 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
959 UnorderedElementsAre("bar::"))));
960}
961
962TEST(CompletionTest, UnresolvedNestedQualifierIdQuery) {
963 auto Requests = captureIndexRequests(R"cpp(
964 namespace a {}
965 using namespace a;
966 namespace ns {
967 void f() {
968 ::a::bar::^
969 }
970 } // namespace ns
971 )cpp");
972
973 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
974 UnorderedElementsAre("a::bar::"))));
975}
976
977TEST(CompletionTest, EmptyQualifiedQuery) {
978 auto Requests = captureIndexRequests(R"cpp(
979 namespace ns {
980 void f() {
981 ^
982 }
983 } // namespace ns
984 )cpp");
985
986 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
987 UnorderedElementsAre("", "ns::"))));
988}
989
990TEST(CompletionTest, GlobalQualifiedQuery) {
991 auto Requests = captureIndexRequests(R"cpp(
992 namespace ns {
993 void f() {
994 ::^
995 }
996 } // namespace ns
997 )cpp");
998
999 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
1000 UnorderedElementsAre(""))));
1001}
1002
Ilya Biryukova907ba42018-05-14 10:50:04 +00001003TEST(CompletionTest, NoIndexCompletionsInsideClasses) {
1004 auto Completions = completions(
1005 R"cpp(
1006 struct Foo {
1007 int SomeNameOfField;
1008 typedef int SomeNameOfTypedefField;
1009 };
1010
1011 Foo::^)cpp",
1012 {func("::SomeNameInTheIndex"), func("::Foo::SomeNameInTheIndex")});
1013
Sam McCalle746a2b2018-07-02 11:13:16 +00001014 EXPECT_THAT(Completions.Completions,
Ilya Biryukova907ba42018-05-14 10:50:04 +00001015 AllOf(Contains(Labeled("SomeNameOfField")),
1016 Contains(Labeled("SomeNameOfTypedefField")),
1017 Not(Contains(Labeled("SomeNameInTheIndex")))));
1018}
1019
1020TEST(CompletionTest, NoIndexCompletionsInsideDependentCode) {
1021 {
1022 auto Completions = completions(
1023 R"cpp(
1024 template <class T>
1025 void foo() {
1026 T::^
1027 }
1028 )cpp",
1029 {func("::SomeNameInTheIndex")});
1030
Sam McCalle746a2b2018-07-02 11:13:16 +00001031 EXPECT_THAT(Completions.Completions,
Ilya Biryukova907ba42018-05-14 10:50:04 +00001032 Not(Contains(Labeled("SomeNameInTheIndex"))));
1033 }
1034
1035 {
1036 auto Completions = completions(
1037 R"cpp(
1038 template <class T>
1039 void foo() {
1040 T::template Y<int>::^
1041 }
1042 )cpp",
1043 {func("::SomeNameInTheIndex")});
1044
Sam McCalle746a2b2018-07-02 11:13:16 +00001045 EXPECT_THAT(Completions.Completions,
Ilya Biryukova907ba42018-05-14 10:50:04 +00001046 Not(Contains(Labeled("SomeNameInTheIndex"))));
1047 }
1048
1049 {
1050 auto Completions = completions(
1051 R"cpp(
1052 template <class T>
1053 void foo() {
1054 T::foo::^
1055 }
1056 )cpp",
1057 {func("::SomeNameInTheIndex")});
1058
Sam McCalle746a2b2018-07-02 11:13:16 +00001059 EXPECT_THAT(Completions.Completions,
Ilya Biryukova907ba42018-05-14 10:50:04 +00001060 Not(Contains(Labeled("SomeNameInTheIndex"))));
1061 }
1062}
1063
Sam McCallc18c2802018-06-15 11:06:29 +00001064TEST(CompletionTest, OverloadBundling) {
1065 clangd::CodeCompleteOptions Opts;
1066 Opts.BundleOverloads = true;
1067
1068 std::string Context = R"cpp(
1069 struct X {
1070 // Overload with int
1071 int a(int);
1072 // Overload with bool
1073 int a(bool);
1074 int b(float);
1075 };
1076 int GFuncC(int);
1077 int GFuncD(int);
1078 )cpp";
1079
1080 // Member completions are bundled.
Sam McCalle746a2b2018-07-02 11:13:16 +00001081 EXPECT_THAT(completions(Context + "int y = X().^", {}, Opts).Completions,
Sam McCallc18c2802018-06-15 11:06:29 +00001082 UnorderedElementsAre(Labeled("a(…)"), Labeled("b(float)")));
1083
1084 // Non-member completions are bundled, including index+sema.
1085 Symbol NoArgsGFunc = func("GFuncC");
1086 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +00001087 completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions,
Sam McCallc18c2802018-06-15 11:06:29 +00001088 UnorderedElementsAre(Labeled("GFuncC(…)"), Labeled("GFuncD(int)")));
1089
1090 // Differences in header-to-insert suppress bundling.
1091 Symbol::Details Detail;
1092 std::string DeclFile = URI::createFile(testPath("foo")).toString();
1093 NoArgsGFunc.CanonicalDeclaration.FileURI = DeclFile;
1094 Detail.IncludeHeader = "<foo>";
1095 NoArgsGFunc.Detail = &Detail;
1096 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +00001097 completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions,
1098 UnorderedElementsAre(AllOf(Named("GFuncC"), InsertInclude("<foo>")),
1099 Labeled("GFuncC(int)"), Labeled("GFuncD(int)")));
Sam McCallc18c2802018-06-15 11:06:29 +00001100
1101 // Examine a bundled completion in detail.
Sam McCalle746a2b2018-07-02 11:13:16 +00001102 auto A =
1103 completions(Context + "int y = X().a^", {}, Opts).Completions.front();
1104 EXPECT_EQ(A.Name, "a");
1105 EXPECT_EQ(A.Signature, "(…)");
1106 EXPECT_EQ(A.BundleSize, 2u);
1107 EXPECT_EQ(A.Kind, CompletionItemKind::Method);
1108 EXPECT_EQ(A.ReturnType, "int"); // All overloads return int.
Sam McCallc18c2802018-06-15 11:06:29 +00001109 // For now we just return one of the doc strings arbitrarily.
Sam McCalle746a2b2018-07-02 11:13:16 +00001110 EXPECT_THAT(A.Documentation, AnyOf(HasSubstr("Overload with int"),
Sam McCallc18c2802018-06-15 11:06:29 +00001111 HasSubstr("Overload with bool")));
Sam McCalle746a2b2018-07-02 11:13:16 +00001112 EXPECT_EQ(A.SnippetSuffix, "(${0})");
Sam McCallc18c2802018-06-15 11:06:29 +00001113}
1114
Ilya Biryukov30b04b12018-05-28 09:54:51 +00001115TEST(CompletionTest, DocumentationFromChangedFileCrash) {
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +00001116 MockFSProvider FS;
1117 auto FooH = testPath("foo.h");
1118 auto FooCpp = testPath("foo.cpp");
1119 FS.Files[FooH] = R"cpp(
1120 // this is my documentation comment.
1121 int func();
1122 )cpp";
1123 FS.Files[FooCpp] = "";
1124
1125 MockCompilationDatabase CDB;
1126 IgnoreDiagnostics DiagConsumer;
1127 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1128
1129 Annotations Source(R"cpp(
1130 #include "foo.h"
1131 int func() {
1132 // This makes sure we have func from header in the AST.
1133 }
1134 int a = fun^
1135 )cpp");
1136 Server.addDocument(FooCpp, Source.code(), WantDiagnostics::Yes);
1137 // We need to wait for preamble to build.
1138 ASSERT_TRUE(Server.blockUntilIdleForTest());
1139
1140 // Change the header file. Completion will reuse the old preamble!
1141 FS.Files[FooH] = R"cpp(
1142 int func();
1143 )cpp";
1144
1145 clangd::CodeCompleteOptions Opts;
1146 Opts.IncludeComments = true;
Sam McCalle746a2b2018-07-02 11:13:16 +00001147 CodeCompleteResult Completions =
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +00001148 cantFail(runCodeComplete(Server, FooCpp, Source.point(), Opts));
1149 // We shouldn't crash. Unfortunately, current workaround is to not produce
1150 // comments for symbols from headers.
Sam McCalle746a2b2018-07-02 11:13:16 +00001151 EXPECT_THAT(Completions.Completions,
Ilya Biryukovbe0eb8f2018-05-24 14:49:23 +00001152 Contains(AllOf(Not(IsDocumented()), Named("func"))));
1153}
1154
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001155TEST(CompletionTest, NonDocComments) {
1156 MockFSProvider FS;
1157 auto FooCpp = testPath("foo.cpp");
1158 FS.Files[FooCpp] = "";
1159
1160 MockCompilationDatabase CDB;
1161 IgnoreDiagnostics DiagConsumer;
1162 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1163
1164 Annotations Source(R"cpp(
Ilya Biryukovda8dd8b2018-06-27 09:47:20 +00001165 // We ignore namespace comments, for rationale see CodeCompletionStrings.h.
1166 namespace comments_ns {
1167 }
1168
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001169 // ------------------
1170 int comments_foo();
1171
1172 // A comment and a decl are separated by newlines.
1173 // Therefore, the comment shouldn't show up as doc comment.
1174
1175 int comments_bar();
1176
1177 // this comment should be in the results.
1178 int comments_baz();
1179
1180
1181 template <class T>
1182 struct Struct {
1183 int comments_qux();
1184 int comments_quux();
1185 };
1186
1187
1188 // This comment should not be there.
1189
1190 template <class T>
1191 int Struct<T>::comments_qux() {
1192 }
1193
1194 // This comment **should** be in results.
1195 template <class T>
1196 int Struct<T>::comments_quux() {
1197 int a = comments^;
1198 }
1199 )cpp");
Reid Kleckner80274b12018-06-18 18:55:10 +00001200 // FIXME: Auto-completion in a template requires disabling delayed template
1201 // parsing.
1202 CDB.ExtraClangFlags.push_back("-fno-delayed-template-parsing");
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001203 Server.addDocument(FooCpp, Source.code(), WantDiagnostics::Yes);
Sam McCalle746a2b2018-07-02 11:13:16 +00001204 CodeCompleteResult Completions = cantFail(runCodeComplete(
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001205 Server, FooCpp, Source.point(), clangd::CodeCompleteOptions()));
1206
1207 // We should not get any of those comments in completion.
1208 EXPECT_THAT(
Sam McCalle746a2b2018-07-02 11:13:16 +00001209 Completions.Completions,
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001210 UnorderedElementsAre(AllOf(Not(IsDocumented()), Named("comments_foo")),
1211 AllOf(IsDocumented(), Named("comments_baz")),
1212 AllOf(IsDocumented(), Named("comments_quux")),
Ilya Biryukovda8dd8b2018-06-27 09:47:20 +00001213 AllOf(Not(IsDocumented()), Named("comments_ns")),
Ilya Biryukov89fcf6b2018-06-15 08:31:17 +00001214 // FIXME(ibiryukov): the following items should have
1215 // empty documentation, since they are separated from
1216 // a comment with an empty line. Unfortunately, I
1217 // couldn't make Sema tests pass if we ignore those.
1218 AllOf(IsDocumented(), Named("comments_bar")),
1219 AllOf(IsDocumented(), Named("comments_qux"))));
1220}
1221
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001222TEST(CompletionTest, CompleteOnInvalidLine) {
1223 auto FooCpp = testPath("foo.cpp");
1224
1225 MockCompilationDatabase CDB;
1226 IgnoreDiagnostics DiagConsumer;
1227 MockFSProvider FS;
1228 FS.Files[FooCpp] = "// empty file";
1229
1230 ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
1231 // Run completion outside the file range.
1232 Position Pos;
1233 Pos.line = 100;
1234 Pos.character = 0;
1235 EXPECT_THAT_EXPECTED(
1236 runCodeComplete(Server, FooCpp, Pos, clangd::CodeCompleteOptions()),
1237 Failed());
1238}
1239
Eric Liu7ad16962018-06-22 10:46:59 +00001240TEST(CompletionTest, QualifiedNames) {
1241 auto Results = completions(
1242 R"cpp(
1243 namespace ns { int local; void both(); }
1244 void f() { ::ns::^ }
1245 )cpp",
1246 {func("ns::both"), cls("ns::Index")});
1247 // We get results from both index and sema, with no duplicates.
Sam McCalle746a2b2018-07-02 11:13:16 +00001248 EXPECT_THAT(
1249 Results.Completions,
1250 UnorderedElementsAre(Scope("ns::"), Scope("ns::"), Scope("ns::")));
1251}
1252
1253TEST(CompletionTest, Render) {
1254 CodeCompletion C;
1255 C.Name = "x";
1256 C.Signature = "(bool) const";
1257 C.SnippetSuffix = "(${0:bool})";
1258 C.ReturnType = "int";
1259 C.RequiredQualifier = "Foo::";
1260 C.Scope = "ns::Foo::";
1261 C.Documentation = "This is x().";
1262 C.Header = "\"foo.h\"";
1263 C.Kind = CompletionItemKind::Method;
1264 C.Score.Total = 1.0;
Sam McCall4e5742a2018-07-06 11:50:49 +00001265 C.Origin = SymbolOrigin::AST | SymbolOrigin::Static;
Sam McCalle746a2b2018-07-02 11:13:16 +00001266
1267 CodeCompleteOptions Opts;
1268 Opts.IncludeIndicator.Insert = "^";
1269 Opts.IncludeIndicator.NoInsert = "";
1270 Opts.EnableSnippets = false;
1271
1272 auto R = C.render(Opts);
1273 EXPECT_EQ(R.label, "Foo::x(bool) const");
1274 EXPECT_EQ(R.insertText, "Foo::x");
1275 EXPECT_EQ(R.insertTextFormat, InsertTextFormat::PlainText);
1276 EXPECT_EQ(R.filterText, "x");
1277 EXPECT_EQ(R.detail, "int\n\"foo.h\"");
1278 EXPECT_EQ(R.documentation, "This is x().");
1279 EXPECT_THAT(R.additionalTextEdits, IsEmpty());
Sam McCalle746a2b2018-07-02 11:13:16 +00001280 EXPECT_EQ(R.sortText, sortText(1.0, "x"));
1281
1282 Opts.EnableSnippets = true;
1283 R = C.render(Opts);
1284 EXPECT_EQ(R.insertText, "Foo::x(${0:bool})");
1285 EXPECT_EQ(R.insertTextFormat, InsertTextFormat::Snippet);
1286
1287 C.HeaderInsertion.emplace();
1288 R = C.render(Opts);
1289 EXPECT_EQ(R.label, "^Foo::x(bool) const");
1290 EXPECT_THAT(R.additionalTextEdits, Not(IsEmpty()));
1291
Sam McCall2161ec72018-07-05 06:20:41 +00001292 Opts.ShowOrigins = true;
1293 R = C.render(Opts);
1294 EXPECT_EQ(R.label, "^[AS]Foo::x(bool) const");
1295
Sam McCalle746a2b2018-07-02 11:13:16 +00001296 C.BundleSize = 2;
1297 R = C.render(Opts);
1298 EXPECT_EQ(R.detail, "[2 overloads]\n\"foo.h\"");
Eric Liu7ad16962018-06-22 10:46:59 +00001299}
Ilya Biryukov981a35d2018-05-28 12:11:37 +00001300
Eric Liu485074f2018-07-11 13:15:31 +00001301TEST(CompletionTest, IgnoreRecoveryResults) {
1302 auto Results = completions(
1303 R"cpp(
1304 namespace ns { int NotRecovered() { return 0; } }
1305 void f() {
1306 // Sema enters recovery mode first and then normal mode.
1307 if (auto x = ns::NotRecover^)
1308 }
1309 )cpp");
1310 EXPECT_THAT(Results.Completions, UnorderedElementsAre(Named("NotRecovered")));
1311}
1312
Eric Liuf433c2d2018-07-18 15:31:14 +00001313TEST(CompletionTest, ScopeOfClassFieldInConstructorInitializer) {
1314 auto Results = completions(
1315 R"cpp(
1316 namespace ns {
1317 class X { public: X(); int x_; };
1318 X::X() : x_^(0) {}
1319 }
1320 )cpp");
1321 EXPECT_THAT(Results.Completions,
1322 UnorderedElementsAre(AllOf(Scope("ns::X::"), Named("x_"))));
1323}
1324
Eric Liu5d2a8072018-07-23 10:56:37 +00001325TEST(CompletionTest, CodeCompletionContext) {
1326 auto Results = completions(
1327 R"cpp(
1328 namespace ns {
1329 class X { public: X(); int x_; };
1330 void f() {
1331 X x;
1332 x.^;
1333 }
1334 }
1335 )cpp");
1336
1337 EXPECT_THAT(Results.Context, CodeCompletionContext::CCC_DotMemberAccess);
1338}
1339
Sam McCall9aad25f2017-12-05 07:20:26 +00001340} // namespace
1341} // namespace clangd
1342} // namespace clang