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 | 63696e14e310501bbc0b48d3215442d23df84408 (patch) | |
tree | 98c6e467acca69a139c081bfc04599224620a76a /clang-tools-extra/unittests/clangd/CodeCompletionStringsTests.cpp | |
parent | 188adaf46b98354e1758d065db27eff0f23bf0c6 (diff) | |
download | bcm5719-llvm-63696e14e310501bbc0b48d3215442d23df84408.tar.gz bcm5719-llvm-63696e14e310501bbc0b48d3215442d23df84408.zip |
[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
llvm-svn: 321193
Diffstat (limited to 'clang-tools-extra/unittests/clangd/CodeCompletionStringsTests.cpp')
-rw-r--r-- | clang-tools-extra/unittests/clangd/CodeCompletionStringsTests.cpp | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/clang-tools-extra/unittests/clangd/CodeCompletionStringsTests.cpp b/clang-tools-extra/unittests/clangd/CodeCompletionStringsTests.cpp new file mode 100644 index 00000000000..9f2ed804d77 --- /dev/null +++ b/clang-tools-extra/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 |