2017-12-21 01:24:31 +08:00
|
|
|
//===-- CodeCompletionStringsTests.cpp --------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-12-21 01:24:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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:
|
2019-05-28 23:33:37 +08:00
|
|
|
void computeSignature(const CodeCompletionString &CCS,
|
|
|
|
bool CompletingPattern = false) {
|
2018-06-23 00:11:35 +08:00
|
|
|
Signature.clear();
|
|
|
|
Snippet.clear();
|
2019-05-28 23:33:37 +08:00
|
|
|
getSignature(CCS, &Signature, &Snippet, /*RequiredQualifier=*/nullptr,
|
|
|
|
CompletingPattern);
|
2017-12-21 01:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
|
|
|
|
CodeCompletionTUInfo CCTUInfo;
|
|
|
|
CodeCompletionBuilder Builder;
|
2018-06-23 00:11:35 +08:00
|
|
|
std::string Signature;
|
|
|
|
std::string Snippet;
|
2017-12-21 01:24:31 +08:00
|
|
|
};
|
|
|
|
|
2018-06-23 00:11:35 +08:00
|
|
|
TEST_F(CompletionStringTest, ReturnType) {
|
2017-12-21 01:24:31 +08:00
|
|
|
Builder.AddResultTypeChunk("result");
|
|
|
|
Builder.AddResultTypeChunk("redundant result no no");
|
2018-06-23 00:11:35 +08:00
|
|
|
EXPECT_EQ(getReturnType(*Builder.TakeString()), "result");
|
2017-12-21 01:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompletionStringTest, Documentation) {
|
2018-05-16 20:32:44 +08:00
|
|
|
Builder.addBriefComment("This is ignored");
|
|
|
|
EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"),
|
|
|
|
"Is this brief?");
|
2017-12-21 01:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompletionStringTest, DocumentationWithAnnotation) {
|
2018-05-16 20:32:44 +08:00
|
|
|
Builder.addBriefComment("This is ignored");
|
2017-12-21 01:24:31 +08:00
|
|
|
Builder.AddAnnotation("Ano");
|
2018-05-16 20:32:44 +08:00
|
|
|
EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"),
|
2017-12-21 01:24:31 +08:00
|
|
|
"Annotation: Ano\n\nIs this brief?");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompletionStringTest, MultipleAnnotations) {
|
|
|
|
Builder.AddAnnotation("Ano1");
|
|
|
|
Builder.AddAnnotation("Ano2");
|
|
|
|
Builder.AddAnnotation("Ano3");
|
|
|
|
|
2018-05-16 20:32:44 +08:00
|
|
|
EXPECT_EQ(formatDocumentation(*Builder.TakeString(), ""),
|
2017-12-21 01:24:31 +08:00
|
|
|
"Annotations: Ano1 Ano2 Ano3\n");
|
|
|
|
}
|
|
|
|
|
2018-06-23 00:11:35 +08:00
|
|
|
TEST_F(CompletionStringTest, EmptySignature) {
|
2017-12-21 01:24:31 +08:00
|
|
|
Builder.AddTypedTextChunk("X");
|
|
|
|
Builder.AddResultTypeChunk("result no no");
|
2018-06-23 00:11:35 +08:00
|
|
|
computeSignature(*Builder.TakeString());
|
|
|
|
EXPECT_EQ(Signature, "");
|
|
|
|
EXPECT_EQ(Snippet, "");
|
2017-12-21 01:24:31 +08:00
|
|
|
}
|
|
|
|
|
2018-06-23 00:11:35 +08:00
|
|
|
TEST_F(CompletionStringTest, Function) {
|
2017-12-21 01:24:31 +08:00
|
|
|
Builder.AddResultTypeChunk("result no no");
|
2018-05-16 20:32:44 +08:00
|
|
|
Builder.addBriefComment("This comment is ignored");
|
2017-12-21 01:24:31 +08:00
|
|
|
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();
|
2018-06-23 00:11:35 +08:00
|
|
|
computeSignature(*CCS);
|
|
|
|
EXPECT_EQ(Signature, "(p1, p2)");
|
|
|
|
EXPECT_EQ(Snippet, "(${1:p1}, ${2:p2})");
|
2018-05-16 20:32:44 +08:00
|
|
|
EXPECT_EQ(formatDocumentation(*CCS, "Foo's comment"), "Foo's comment");
|
2017-12-21 01:24:31 +08:00
|
|
|
}
|
|
|
|
|
2019-08-08 00:52:21 +08:00
|
|
|
TEST_F(CompletionStringTest, FunctionWithDefaultParams) {
|
|
|
|
// return_type foo(p1, p2 = 0, p3 = 0)
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_Comma);
|
|
|
|
Builder.AddTypedTextChunk("p3 = 0");
|
|
|
|
auto *DefaultParam2 = Builder.TakeString();
|
|
|
|
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_Comma);
|
|
|
|
Builder.AddTypedTextChunk("p2 = 0");
|
|
|
|
Builder.AddOptionalChunk(DefaultParam2);
|
|
|
|
auto *DefaultParam1 = Builder.TakeString();
|
|
|
|
|
|
|
|
Builder.AddResultTypeChunk("return_type");
|
|
|
|
Builder.AddTypedTextChunk("Foo");
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_LeftParen);
|
|
|
|
Builder.AddPlaceholderChunk("p1");
|
|
|
|
Builder.AddOptionalChunk(DefaultParam1);
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_RightParen);
|
|
|
|
|
|
|
|
auto *CCS = Builder.TakeString();
|
|
|
|
computeSignature(*CCS);
|
|
|
|
EXPECT_EQ(Signature, "(p1, p2 = 0, p3 = 0)");
|
|
|
|
EXPECT_EQ(Snippet, "(${1:p1})");
|
|
|
|
}
|
|
|
|
|
2017-12-21 01:24:31 +08:00
|
|
|
TEST_F(CompletionStringTest, EscapeSnippet) {
|
|
|
|
Builder.AddTypedTextChunk("Foo");
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_LeftParen);
|
|
|
|
Builder.AddPlaceholderChunk("$p}1\\");
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_RightParen);
|
|
|
|
|
2018-06-23 00:11:35 +08:00
|
|
|
computeSignature(*Builder.TakeString());
|
|
|
|
EXPECT_EQ(Signature, "($p}1\\)");
|
|
|
|
EXPECT_EQ(Snippet, "(${1:\\$p\\}1\\\\})");
|
2017-12-21 01:24:31 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 23:33:37 +08:00
|
|
|
TEST_F(CompletionStringTest, SnippetsInPatterns) {
|
|
|
|
auto MakeCCS = [this]() -> const CodeCompletionString & {
|
|
|
|
CodeCompletionBuilder Builder(*Allocator, CCTUInfo);
|
|
|
|
Builder.AddTypedTextChunk("namespace");
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
|
|
|
|
Builder.AddPlaceholderChunk("name");
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_Equal);
|
|
|
|
Builder.AddPlaceholderChunk("target");
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_SemiColon);
|
|
|
|
return *Builder.TakeString();
|
|
|
|
};
|
|
|
|
computeSignature(MakeCCS(), /*CompletingPattern=*/false);
|
|
|
|
EXPECT_EQ(Snippet, " ${1:name} = ${2:target};");
|
|
|
|
|
|
|
|
// When completing a pattern, the last placeholder holds the cursor position.
|
|
|
|
computeSignature(MakeCCS(), /*CompletingPattern=*/true);
|
|
|
|
EXPECT_EQ(Snippet, " ${1:name} = ${0:target};");
|
|
|
|
}
|
|
|
|
|
2017-12-21 01:24:31 +08:00
|
|
|
TEST_F(CompletionStringTest, IgnoreInformativeQualifier) {
|
|
|
|
Builder.AddTypedTextChunk("X");
|
|
|
|
Builder.AddInformativeChunk("info ok");
|
|
|
|
Builder.AddInformativeChunk("info no no::");
|
2018-06-23 00:11:35 +08:00
|
|
|
computeSignature(*Builder.TakeString());
|
|
|
|
EXPECT_EQ(Signature, "info ok");
|
|
|
|
EXPECT_EQ(Snippet, "");
|
2017-12-21 01:24:31 +08:00
|
|
|
}
|
|
|
|
|
2018-11-14 17:05:19 +08:00
|
|
|
TEST_F(CompletionStringTest, ObjectiveCMethodNoArguments) {
|
|
|
|
Builder.AddResultTypeChunk("void");
|
|
|
|
Builder.AddTypedTextChunk("methodName");
|
|
|
|
|
|
|
|
auto *CCS = Builder.TakeString();
|
|
|
|
computeSignature(*CCS);
|
|
|
|
EXPECT_EQ(Signature, "");
|
|
|
|
EXPECT_EQ(Snippet, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompletionStringTest, ObjectiveCMethodOneArgument) {
|
|
|
|
Builder.AddResultTypeChunk("void");
|
|
|
|
Builder.AddTypedTextChunk("methodWithArg:");
|
|
|
|
Builder.AddPlaceholderChunk("(type)");
|
|
|
|
|
|
|
|
auto *CCS = Builder.TakeString();
|
|
|
|
computeSignature(*CCS);
|
|
|
|
EXPECT_EQ(Signature, "(type)");
|
|
|
|
EXPECT_EQ(Snippet, "${1:(type)}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompletionStringTest, ObjectiveCMethodTwoArgumentsFromBeginning) {
|
|
|
|
Builder.AddResultTypeChunk("int");
|
|
|
|
Builder.AddTypedTextChunk("withFoo:");
|
|
|
|
Builder.AddPlaceholderChunk("(type)");
|
|
|
|
Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
|
|
|
|
Builder.AddTypedTextChunk("bar:");
|
|
|
|
Builder.AddPlaceholderChunk("(type2)");
|
|
|
|
|
|
|
|
auto *CCS = Builder.TakeString();
|
|
|
|
computeSignature(*CCS);
|
|
|
|
EXPECT_EQ(Signature, "(type) bar:(type2)");
|
|
|
|
EXPECT_EQ(Snippet, "${1:(type)} bar:${2:(type2)}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompletionStringTest, ObjectiveCMethodTwoArgumentsFromMiddle) {
|
|
|
|
Builder.AddResultTypeChunk("int");
|
|
|
|
Builder.AddInformativeChunk("withFoo:");
|
|
|
|
Builder.AddTypedTextChunk("bar:");
|
|
|
|
Builder.AddPlaceholderChunk("(type2)");
|
|
|
|
|
|
|
|
auto *CCS = Builder.TakeString();
|
|
|
|
computeSignature(*CCS);
|
|
|
|
EXPECT_EQ(Signature, "(type2)");
|
|
|
|
EXPECT_EQ(Snippet, "${1:(type2)}");
|
|
|
|
}
|
|
|
|
|
2017-12-21 01:24:31 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|