blob: 82ab0f57a01053e175680454a0bfc20f7991b1d1 [file] [log] [blame]
Eric Liu63696e12017-12-20 17:24:31 +00001//===-- CodeCompletionStringsTests.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//===----------------------------------------------------------------------===//
9
10#include "CodeCompletionStrings.h"
11#include "clang/Sema/CodeCompleteConsumer.h"
12#include "gmock/gmock.h"
13#include "gtest/gtest.h"
14
15namespace clang {
16namespace clangd {
17namespace {
18
19class CompletionStringTest : public ::testing::Test {
20public:
21 CompletionStringTest()
22 : Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
23 CCTUInfo(Allocator), Builder(*Allocator, CCTUInfo) {}
24
25protected:
26 void labelAndInsertText(const CodeCompletionString &CCS,
27 bool EnableSnippets = false) {
28 Label.clear();
29 InsertText.clear();
30 getLabelAndInsertText(CCS, &Label, &InsertText, EnableSnippets);
31 }
32
33 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
34 CodeCompletionTUInfo CCTUInfo;
35 CodeCompletionBuilder Builder;
36 std::string Label;
37 std::string InsertText;
38};
39
40TEST_F(CompletionStringTest, Detail) {
41 Builder.AddResultTypeChunk("result");
42 Builder.AddResultTypeChunk("redundant result no no");
43 EXPECT_EQ(getDetail(*Builder.TakeString()), "result");
44}
45
Eric Liu63696e12017-12-20 17:24:31 +000046TEST_F(CompletionStringTest, Documentation) {
Ilya Biryukov43714502018-05-16 12:32:44 +000047 Builder.addBriefComment("This is ignored");
48 EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"),
49 "Is this brief?");
Eric Liu63696e12017-12-20 17:24:31 +000050}
51
52TEST_F(CompletionStringTest, DocumentationWithAnnotation) {
Ilya Biryukov43714502018-05-16 12:32:44 +000053 Builder.addBriefComment("This is ignored");
Eric Liu63696e12017-12-20 17:24:31 +000054 Builder.AddAnnotation("Ano");
Ilya Biryukov43714502018-05-16 12:32:44 +000055 EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"),
Eric Liu63696e12017-12-20 17:24:31 +000056 "Annotation: Ano\n\nIs this brief?");
57}
58
59TEST_F(CompletionStringTest, MultipleAnnotations) {
60 Builder.AddAnnotation("Ano1");
61 Builder.AddAnnotation("Ano2");
62 Builder.AddAnnotation("Ano3");
63
Ilya Biryukov43714502018-05-16 12:32:44 +000064 EXPECT_EQ(formatDocumentation(*Builder.TakeString(), ""),
Eric Liu63696e12017-12-20 17:24:31 +000065 "Annotations: Ano1 Ano2 Ano3\n");
66}
67
68TEST_F(CompletionStringTest, SimpleLabelAndInsert) {
69 Builder.AddTypedTextChunk("X");
70 Builder.AddResultTypeChunk("result no no");
71 labelAndInsertText(*Builder.TakeString());
72 EXPECT_EQ(Label, "X");
73 EXPECT_EQ(InsertText, "X");
74}
75
76TEST_F(CompletionStringTest, FunctionPlainText) {
77 Builder.AddResultTypeChunk("result no no");
78 Builder.AddTypedTextChunk("Foo");
79 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
80 Builder.AddPlaceholderChunk("p1");
81 Builder.AddChunk(CodeCompletionString::CK_Comma);
82 Builder.AddPlaceholderChunk("p2");
83 Builder.AddChunk(CodeCompletionString::CK_RightParen);
84 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
85 Builder.AddInformativeChunk("const");
86
87 labelAndInsertText(*Builder.TakeString());
88 EXPECT_EQ(Label, "Foo(p1, p2) const");
89 EXPECT_EQ(InsertText, "Foo");
90}
91
92TEST_F(CompletionStringTest, FunctionSnippet) {
93 Builder.AddResultTypeChunk("result no no");
Ilya Biryukov43714502018-05-16 12:32:44 +000094 Builder.addBriefComment("This comment is ignored");
Eric Liu63696e12017-12-20 17:24:31 +000095 Builder.AddTypedTextChunk("Foo");
96 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
97 Builder.AddPlaceholderChunk("p1");
98 Builder.AddChunk(CodeCompletionString::CK_Comma);
99 Builder.AddPlaceholderChunk("p2");
100 Builder.AddChunk(CodeCompletionString::CK_RightParen);
101
102 auto *CCS = Builder.TakeString();
103 labelAndInsertText(*CCS);
104 EXPECT_EQ(Label, "Foo(p1, p2)");
105 EXPECT_EQ(InsertText, "Foo");
106
107 labelAndInsertText(*CCS, /*EnableSnippets=*/true);
108 EXPECT_EQ(Label, "Foo(p1, p2)");
109 EXPECT_EQ(InsertText, "Foo(${1:p1}, ${2:p2})");
Ilya Biryukov43714502018-05-16 12:32:44 +0000110 EXPECT_EQ(formatDocumentation(*CCS, "Foo's comment"), "Foo's comment");
Eric Liu63696e12017-12-20 17:24:31 +0000111}
112
113TEST_F(CompletionStringTest, EscapeSnippet) {
114 Builder.AddTypedTextChunk("Foo");
115 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
116 Builder.AddPlaceholderChunk("$p}1\\");
117 Builder.AddChunk(CodeCompletionString::CK_RightParen);
118
119 labelAndInsertText(*Builder.TakeString(), /*EnableSnippets=*/true);
120 EXPECT_EQ(Label, "Foo($p}1\\)");
121 EXPECT_EQ(InsertText, "Foo(${1:\\$p\\}1\\\\})");
122}
123
124TEST_F(CompletionStringTest, IgnoreInformativeQualifier) {
125 Builder.AddTypedTextChunk("X");
126 Builder.AddInformativeChunk("info ok");
127 Builder.AddInformativeChunk("info no no::");
128 labelAndInsertText(*Builder.TakeString());
129 EXPECT_EQ(Label, "Xinfo ok");
130 EXPECT_EQ(InsertText, "X");
131}
132
133} // namespace
134} // namespace clangd
135} // namespace clang