aboutsummaryrefslogtreecommitdiff
path: root/unittests/clangd/CodeCompletionStringsTests.cpp
blob: 7712686222c99f507cf32d36c85d7a79bcb3c95c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//===-- CodeCompletionStringsTests.cpp --------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "CodeCompletionStrings.h"
#include "clang/Sema/CodeCompleteConsumer.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {
namespace {

class CompletionStringTest : public ::testing::Test {
public:
  CompletionStringTest()
      : Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
        CCTUInfo(Allocator), Builder(*Allocator, CCTUInfo) {}

protected:
  void labelAndInsertText(const CodeCompletionString &CCS,
                          bool EnableSnippets = false) {
    Label.clear();
    InsertText.clear();
    getLabelAndInsertText(CCS, &Label, &InsertText, EnableSnippets);
  }

  std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
  CodeCompletionTUInfo CCTUInfo;
  CodeCompletionBuilder Builder;
  std::string Label;
  std::string InsertText;
};

TEST_F(CompletionStringTest, Detail) {
  Builder.AddResultTypeChunk("result");
  Builder.AddResultTypeChunk("redundant result no no");
  EXPECT_EQ(getDetail(*Builder.TakeString()), "result");
}

TEST_F(CompletionStringTest, FilterText) {
  Builder.AddTypedTextChunk("typed");
  Builder.AddTypedTextChunk("redundant typed no no");
  auto *S = Builder.TakeString();
  EXPECT_EQ(getFilterText(*S), "typed");
}

TEST_F(CompletionStringTest, Documentation) {
  Builder.addBriefComment("This is ignored");
  EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"),
            "Is this brief?");
}

TEST_F(CompletionStringTest, DocumentationWithAnnotation) {
  Builder.addBriefComment("This is ignored");
  Builder.AddAnnotation("Ano");
  EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"),
            "Annotation: Ano\n\nIs this brief?");
}

TEST_F(CompletionStringTest, MultipleAnnotations) {
  Builder.AddAnnotation("Ano1");
  Builder.AddAnnotation("Ano2");
  Builder.AddAnnotation("Ano3");

  EXPECT_EQ(formatDocumentation(*Builder.TakeString(), ""),
            "Annotations: Ano1 Ano2 Ano3\n");
}

TEST_F(CompletionStringTest, SimpleLabelAndInsert) {
  Builder.AddTypedTextChunk("X");
  Builder.AddResultTypeChunk("result no no");
  labelAndInsertText(*Builder.TakeString());
  EXPECT_EQ(Label, "X");
  EXPECT_EQ(InsertText, "X");
}

TEST_F(CompletionStringTest, FunctionPlainText) {
  Builder.AddResultTypeChunk("result no no");
  Builder.AddTypedTextChunk("Foo");
  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
  Builder.AddPlaceholderChunk("p1");
  Builder.AddChunk(CodeCompletionString::CK_Comma);
  Builder.AddPlaceholderChunk("p2");
  Builder.AddChunk(CodeCompletionString::CK_RightParen);
  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
  Builder.AddInformativeChunk("const");

  labelAndInsertText(*Builder.TakeString());
  EXPECT_EQ(Label, "Foo(p1, p2) const");
  EXPECT_EQ(InsertText, "Foo");
}

TEST_F(CompletionStringTest, FunctionSnippet) {
  Builder.AddResultTypeChunk("result no no");
  Builder.addBriefComment("This comment is ignored");
  Builder.AddTypedTextChunk("Foo");
  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
  Builder.AddPlaceholderChunk("p1");
  Builder.AddChunk(CodeCompletionString::CK_Comma);
  Builder.AddPlaceholderChunk("p2");
  Builder.AddChunk(CodeCompletionString::CK_RightParen);

  auto *CCS = Builder.TakeString();
  labelAndInsertText(*CCS);
  EXPECT_EQ(Label, "Foo(p1, p2)");
  EXPECT_EQ(InsertText, "Foo");

  labelAndInsertText(*CCS, /*EnableSnippets=*/true);
  EXPECT_EQ(Label, "Foo(p1, p2)");
  EXPECT_EQ(InsertText, "Foo(${1:p1}, ${2:p2})");
  EXPECT_EQ(formatDocumentation(*CCS, "Foo's comment"), "Foo's comment");
  EXPECT_EQ(getFilterText(*CCS), "Foo");
}

TEST_F(CompletionStringTest, EscapeSnippet) {
  Builder.AddTypedTextChunk("Foo");
  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
  Builder.AddPlaceholderChunk("$p}1\\");
  Builder.AddChunk(CodeCompletionString::CK_RightParen);

  labelAndInsertText(*Builder.TakeString(), /*EnableSnippets=*/true);
  EXPECT_EQ(Label, "Foo($p}1\\)");
  EXPECT_EQ(InsertText, "Foo(${1:\\$p\\}1\\\\})");
}

TEST_F(CompletionStringTest, IgnoreInformativeQualifier) {
  Builder.AddTypedTextChunk("X");
  Builder.AddInformativeChunk("info ok");
  Builder.AddInformativeChunk("info no no::");
  labelAndInsertText(*Builder.TakeString());
  EXPECT_EQ(Label, "Xinfo ok");
  EXPECT_EQ(InsertText, "X");
}

} // namespace
} // namespace clangd
} // namespace clang