Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 1 | //===-- CodeCompletionStringsTests.cpp --------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "CodeCompletionStrings.h" |
| 11 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 12 | #include "gmock/gmock.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace clangd { |
| 17 | namespace { |
| 18 | |
| 19 | class CompletionStringTest : public ::testing::Test { |
| 20 | public: |
| 21 | CompletionStringTest() |
| 22 | : Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()), |
| 23 | CCTUInfo(Allocator), Builder(*Allocator, CCTUInfo) {} |
| 24 | |
| 25 | protected: |
| 26 | void labelAndInsertText(const CodeCompletionString &CCS, |
| 27 | bool EnableSnippets = false) { |
| 28 | Label.clear(); |
| 29 | InsertText.clear(); |
| 30 | getLabelAndInsertText(CCS, &Label, &InsertText, EnableSnippets); |
| 31 | } |
| 32 | |
| 33 | std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator; |
| 34 | CodeCompletionTUInfo CCTUInfo; |
| 35 | CodeCompletionBuilder Builder; |
| 36 | std::string Label; |
| 37 | std::string InsertText; |
| 38 | }; |
| 39 | |
| 40 | TEST_F(CompletionStringTest, Detail) { |
| 41 | Builder.AddResultTypeChunk("result"); |
| 42 | Builder.AddResultTypeChunk("redundant result no no"); |
| 43 | EXPECT_EQ(getDetail(*Builder.TakeString()), "result"); |
| 44 | } |
| 45 | |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 46 | TEST_F(CompletionStringTest, Documentation) { |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 47 | Builder.addBriefComment("This is ignored"); |
| 48 | EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"), |
| 49 | "Is this brief?"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST_F(CompletionStringTest, DocumentationWithAnnotation) { |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 53 | Builder.addBriefComment("This is ignored"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 54 | Builder.AddAnnotation("Ano"); |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 55 | EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"), |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 56 | "Annotation: Ano\n\nIs this brief?"); |
| 57 | } |
| 58 | |
| 59 | TEST_F(CompletionStringTest, MultipleAnnotations) { |
| 60 | Builder.AddAnnotation("Ano1"); |
| 61 | Builder.AddAnnotation("Ano2"); |
| 62 | Builder.AddAnnotation("Ano3"); |
| 63 | |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 64 | EXPECT_EQ(formatDocumentation(*Builder.TakeString(), ""), |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 65 | "Annotations: Ano1 Ano2 Ano3\n"); |
| 66 | } |
| 67 | |
| 68 | TEST_F(CompletionStringTest, SimpleLabelAndInsert) { |
| 69 | Builder.AddTypedTextChunk("X"); |
| 70 | Builder.AddResultTypeChunk("result no no"); |
| 71 | labelAndInsertText(*Builder.TakeString()); |
| 72 | EXPECT_EQ(Label, "X"); |
| 73 | EXPECT_EQ(InsertText, "X"); |
| 74 | } |
| 75 | |
| 76 | TEST_F(CompletionStringTest, FunctionPlainText) { |
| 77 | Builder.AddResultTypeChunk("result no no"); |
| 78 | Builder.AddTypedTextChunk("Foo"); |
| 79 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 80 | Builder.AddPlaceholderChunk("p1"); |
| 81 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 82 | Builder.AddPlaceholderChunk("p2"); |
| 83 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 84 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 85 | Builder.AddInformativeChunk("const"); |
| 86 | |
| 87 | labelAndInsertText(*Builder.TakeString()); |
| 88 | EXPECT_EQ(Label, "Foo(p1, p2) const"); |
| 89 | EXPECT_EQ(InsertText, "Foo"); |
| 90 | } |
| 91 | |
| 92 | TEST_F(CompletionStringTest, FunctionSnippet) { |
| 93 | Builder.AddResultTypeChunk("result no no"); |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 94 | Builder.addBriefComment("This comment is ignored"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 95 | Builder.AddTypedTextChunk("Foo"); |
| 96 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 97 | Builder.AddPlaceholderChunk("p1"); |
| 98 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 99 | Builder.AddPlaceholderChunk("p2"); |
| 100 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 101 | |
| 102 | auto *CCS = Builder.TakeString(); |
| 103 | labelAndInsertText(*CCS); |
| 104 | EXPECT_EQ(Label, "Foo(p1, p2)"); |
| 105 | EXPECT_EQ(InsertText, "Foo"); |
| 106 | |
| 107 | labelAndInsertText(*CCS, /*EnableSnippets=*/true); |
| 108 | EXPECT_EQ(Label, "Foo(p1, p2)"); |
| 109 | EXPECT_EQ(InsertText, "Foo(${1:p1}, ${2:p2})"); |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 110 | EXPECT_EQ(formatDocumentation(*CCS, "Foo's comment"), "Foo's comment"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST_F(CompletionStringTest, EscapeSnippet) { |
| 114 | Builder.AddTypedTextChunk("Foo"); |
| 115 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 116 | Builder.AddPlaceholderChunk("$p}1\\"); |
| 117 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 118 | |
| 119 | labelAndInsertText(*Builder.TakeString(), /*EnableSnippets=*/true); |
| 120 | EXPECT_EQ(Label, "Foo($p}1\\)"); |
| 121 | EXPECT_EQ(InsertText, "Foo(${1:\\$p\\}1\\\\})"); |
| 122 | } |
| 123 | |
| 124 | TEST_F(CompletionStringTest, IgnoreInformativeQualifier) { |
| 125 | Builder.AddTypedTextChunk("X"); |
| 126 | Builder.AddInformativeChunk("info ok"); |
| 127 | Builder.AddInformativeChunk("info no no::"); |
| 128 | labelAndInsertText(*Builder.TakeString()); |
| 129 | EXPECT_EQ(Label, "Xinfo ok"); |
| 130 | EXPECT_EQ(InsertText, "X"); |
| 131 | } |
| 132 | |
| 133 | } // namespace |
| 134 | } // namespace clangd |
| 135 | } // namespace clang |