11#include "clang/Sema/CodeCompleteConsumer.h"
12#include "gmock/gmock.h"
13#include "gtest/gtest.h"
19class CompletionStringTest :
public ::testing::Test {
21 CompletionStringTest()
22 : Allocator(std::make_shared<
clang::GlobalCodeCompletionAllocator>()),
23 CCTUInfo(Allocator),
Builder(*Allocator, CCTUInfo) {}
26 void computeSignature(
const CodeCompletionString &CCS,
27 CodeCompletionResult::ResultKind ResultKind =
28 CodeCompletionResult::ResultKind::RK_Declaration,
29 bool IncludeFunctionArguments =
true) {
33 CXCursorKind::CXCursor_NotImplemented,
34 IncludeFunctionArguments,
38 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
39 CodeCompletionTUInfo CCTUInfo;
46 Builder.AddResultTypeChunk(
"result");
47 Builder.AddResultTypeChunk(
"redundant result no no");
51TEST_F(CompletionStringTest, Documentation) {
52 Builder.addBriefComment(
"This is ignored");
57TEST_F(CompletionStringTest, DocumentationWithAnnotation) {
58 Builder.addBriefComment(
"This is ignored");
61 "Annotation: Ano\n\nIs this brief?");
64TEST_F(CompletionStringTest, GetDeclCommentBadUTF8) {
67 auto AST = TU.build();
68 EXPECT_EQ(
"x\xef\xbf\xbdy",
72TEST_F(CompletionStringTest, MultipleAnnotations) {
78 "Annotations: Ano1 Ano2 Ano3\n");
81TEST_F(CompletionStringTest, EmptySignature) {
83 Builder.AddResultTypeChunk(
"result no no");
84 computeSignature(*
Builder.TakeString());
90 Builder.AddResultTypeChunk(
"result no no");
91 Builder.addBriefComment(
"This comment is ignored");
92 Builder.AddTypedTextChunk(
"Foo");
93 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
94 Builder.AddPlaceholderChunk(
"p1");
95 Builder.AddChunk(CodeCompletionString::CK_Comma);
96 Builder.AddPlaceholderChunk(
"p2");
97 Builder.AddChunk(CodeCompletionString::CK_RightParen);
99 auto *CCS =
Builder.TakeString();
100 computeSignature(*CCS);
102 EXPECT_EQ(
Snippet,
"(${1:p1}, ${2:p2})");
106TEST_F(CompletionStringTest, FunctionWithDefaultParams) {
108 Builder.AddChunk(CodeCompletionString::CK_Comma);
109 Builder.AddTypedTextChunk(
"p3 = 0");
110 auto *DefaultParam2 =
Builder.TakeString();
112 Builder.AddChunk(CodeCompletionString::CK_Comma);
113 Builder.AddTypedTextChunk(
"p2 = 0");
114 Builder.AddOptionalChunk(DefaultParam2);
115 auto *DefaultParam1 =
Builder.TakeString();
117 Builder.AddResultTypeChunk(
"return_type");
118 Builder.AddTypedTextChunk(
"Foo");
119 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
120 Builder.AddPlaceholderChunk(
"p1");
121 Builder.AddOptionalChunk(DefaultParam1);
122 Builder.AddChunk(CodeCompletionString::CK_RightParen);
124 auto *CCS =
Builder.TakeString();
125 computeSignature(*CCS);
126 EXPECT_EQ(
Signature,
"(p1, p2 = 0, p3 = 0)");
127 EXPECT_EQ(
Snippet,
"(${1:p1})");
130TEST_F(CompletionStringTest, EscapeSnippet) {
131 Builder.AddTypedTextChunk(
"Foo");
132 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
133 Builder.AddPlaceholderChunk(
"$p}1\\");
134 Builder.AddChunk(CodeCompletionString::CK_RightParen);
136 computeSignature(*
Builder.TakeString());
138 EXPECT_EQ(
Snippet,
"(${1:\\$p\\}1\\\\})");
141TEST_F(CompletionStringTest, SnippetsInPatterns) {
142 auto MakeCCS = [
this]() ->
const CodeCompletionString & {
143 CodeCompletionBuilder
Builder(*Allocator, CCTUInfo);
144 Builder.AddTypedTextChunk(
"namespace");
145 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
146 Builder.AddPlaceholderChunk(
"name");
147 Builder.AddChunk(CodeCompletionString::CK_Equal);
148 Builder.AddPlaceholderChunk(
"target");
149 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
152 computeSignature(MakeCCS());
153 EXPECT_EQ(
Snippet,
" ${1:name} = ${2:target};");
156 computeSignature(MakeCCS(),
157 CodeCompletionResult::ResultKind::RK_Pattern);
158 EXPECT_EQ(
Snippet,
" ${1:name} = $0;");
161TEST_F(CompletionStringTest, DropFunctionArguments) {
162 Builder.AddTypedTextChunk(
"foo");
163 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
164 Builder.AddPlaceholderChunk(
"typename T");
165 Builder.AddChunk(CodeCompletionString::CK_Comma);
166 Builder.AddPlaceholderChunk(
"int U");
167 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
168 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
169 Builder.AddPlaceholderChunk(
"arg1");
170 Builder.AddChunk(CodeCompletionString::CK_Comma);
171 Builder.AddPlaceholderChunk(
"arg2");
172 Builder.AddChunk(CodeCompletionString::CK_RightParen);
176 CodeCompletionResult::ResultKind::RK_Declaration,
179 EXPECT_EQ(
Signature,
"<typename T, int U>(arg1, arg2)");
180 EXPECT_EQ(
Snippet,
"<${1:typename T}, ${2:int U}>");
183TEST_F(CompletionStringTest, IgnoreInformativeQualifier) {
184 Builder.AddTypedTextChunk(
"X");
185 Builder.AddInformativeChunk(
"info ok");
186 Builder.AddInformativeChunk(
"info no no::");
187 computeSignature(*
Builder.TakeString());
192TEST_F(CompletionStringTest, ObjectiveCMethodNoArguments) {
193 Builder.AddResultTypeChunk(
"void");
194 Builder.AddTypedTextChunk(
"methodName");
196 auto *CCS =
Builder.TakeString();
197 computeSignature(*CCS);
202TEST_F(CompletionStringTest, ObjectiveCMethodOneArgument) {
203 Builder.AddResultTypeChunk(
"void");
204 Builder.AddTypedTextChunk(
"methodWithArg:");
205 Builder.AddPlaceholderChunk(
"(type)");
207 auto *CCS =
Builder.TakeString();
208 computeSignature(*CCS);
210 EXPECT_EQ(
Snippet,
"${1:(type)}");
213TEST_F(CompletionStringTest, ObjectiveCMethodTwoArgumentsFromBeginning) {
214 Builder.AddResultTypeChunk(
"int");
215 Builder.AddTypedTextChunk(
"withFoo:");
216 Builder.AddPlaceholderChunk(
"(type)");
217 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
218 Builder.AddTypedTextChunk(
"bar:");
219 Builder.AddPlaceholderChunk(
"(type2)");
221 auto *CCS =
Builder.TakeString();
222 computeSignature(*CCS);
223 EXPECT_EQ(
Signature,
"(type) bar:(type2)");
224 EXPECT_EQ(
Snippet,
"${1:(type)} bar:${2:(type2)}");
227TEST_F(CompletionStringTest, ObjectiveCMethodTwoArgumentsFromMiddle) {
228 Builder.AddResultTypeChunk(
"int");
229 Builder.AddInformativeChunk(
"withFoo:");
230 Builder.AddTypedTextChunk(
"bar:");
231 Builder.AddPlaceholderChunk(
"(type2)");
233 auto *CCS =
Builder.TakeString();
234 computeSignature(*CCS);
236 EXPECT_EQ(
Snippet,
"${1:(type2)}");
CodeCompletionBuilder Builder
std::string formatDocumentation(const CodeCompletionString &CCS, llvm::StringRef DocComment)
Assembles formatted documentation for a completion result.
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl)
Similar to getDocComment, but returns the comment for a NamedDecl.
std::string getReturnType(const CodeCompletionString &CCS)
Gets detail to be used as the detail field in an LSP completion item.
void getSignature(const CodeCompletionString &CCS, std::string *Signature, std::string *Snippet, CodeCompletionResult::ResultKind ResultKind, CXCursorKind CursorKind, bool IncludeFunctionArguments, std::string *RequiredQualifiers)
Formats the signature for an item, as a display string and snippet.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static TestTU withCode(llvm::StringRef Code)