diff options
| author | Eric Liu <ioeric@google.com> | 2017-12-20 17:24:31 +0000 |
|---|---|---|
| committer | Eric Liu <ioeric@google.com> | 2017-12-20 17:24:31 +0000 |
| commit | 3565d1a1a692fc9f5c21e634b470535da2bb4d25 (patch) | |
| tree | 104a96e935de410b69ab65666f66c7658f5f906a /unittests | |
| parent | bf3c9d97f5f0ea6678a0cfea4624291a97ab619c (diff) | |
[clangd] Pull CodeCompletionString handling logic into its own file and add unit test.
Reviewers: sammccall
Subscribers: klimek, mgorny, ilya-biryukov, cfe-commits
Differential Revision: https://reviews.llvm.org/D41450
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@321193 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
| -rw-r--r-- | unittests/clangd/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | unittests/clangd/CodeCompleteTests.cpp | 2 | ||||
| -rw-r--r-- | unittests/clangd/CodeCompletionStringsTests.cpp | 142 |
3 files changed, 144 insertions, 1 deletions
diff --git a/unittests/clangd/CMakeLists.txt b/unittests/clangd/CMakeLists.txt index ffe11b77..cbb7385c 100644 --- a/unittests/clangd/CMakeLists.txt +++ b/unittests/clangd/CMakeLists.txt @@ -12,6 +12,7 @@ add_extra_unittest(ClangdTests Annotations.cpp ClangdTests.cpp CodeCompleteTests.cpp + CodeCompletionStringsTests.cpp ContextTests.cpp FileIndexTests.cpp FuzzyMatchTests.cpp diff --git a/unittests/clangd/CodeCompleteTests.cpp b/unittests/clangd/CodeCompleteTests.cpp index 7411e1c3..db998b08 100644 --- a/unittests/clangd/CodeCompleteTests.cpp +++ b/unittests/clangd/CodeCompleteTests.cpp @@ -341,7 +341,7 @@ TEST(CompletionTest, Snippets) { )cpp", Opts); EXPECT_THAT(Results.items, - HasSubsequence(PlainText("a"), + HasSubsequence(Snippet("a"), Snippet("f(${1:int i}, ${2:const float f})"))); } diff --git a/unittests/clangd/CodeCompletionStringsTests.cpp b/unittests/clangd/CodeCompletionStringsTests.cpp new file mode 100644 index 00000000..9f2ed804 --- /dev/null +++ b/unittests/clangd/CodeCompletionStringsTests.cpp @@ -0,0 +1,142 @@ +//===-- 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("Is this brief?"); + EXPECT_EQ(getDocumentation(*Builder.TakeString()), "Is this brief?"); +} + +TEST_F(CompletionStringTest, DocumentationWithAnnotation) { + Builder.addBriefComment("Is this brief?"); + Builder.AddAnnotation("Ano"); + EXPECT_EQ(getDocumentation(*Builder.TakeString()), + "Annotation: Ano\n\nIs this brief?"); +} + +TEST_F(CompletionStringTest, MultipleAnnotations) { + Builder.AddAnnotation("Ano1"); + Builder.AddAnnotation("Ano2"); + Builder.AddAnnotation("Ano3"); + + EXPECT_EQ(getDocumentation(*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("Foo's comment"); + 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(getDocumentation(*CCS), "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 |
