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: |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 26 | void computeSignature(const CodeCompletionString &CCS) { |
| 27 | Signature.clear(); |
| 28 | Snippet.clear(); |
| 29 | getSignature(CCS, &Signature, &Snippet); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator; |
| 33 | CodeCompletionTUInfo CCTUInfo; |
| 34 | CodeCompletionBuilder Builder; |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 35 | std::string Signature; |
| 36 | std::string Snippet; |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 39 | TEST_F(CompletionStringTest, ReturnType) { |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 40 | Builder.AddResultTypeChunk("result"); |
| 41 | Builder.AddResultTypeChunk("redundant result no no"); |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 42 | EXPECT_EQ(getReturnType(*Builder.TakeString()), "result"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 45 | TEST_F(CompletionStringTest, Documentation) { |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 46 | Builder.addBriefComment("This is ignored"); |
| 47 | EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"), |
| 48 | "Is this brief?"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST_F(CompletionStringTest, DocumentationWithAnnotation) { |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 52 | Builder.addBriefComment("This is ignored"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 53 | Builder.AddAnnotation("Ano"); |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 54 | EXPECT_EQ(formatDocumentation(*Builder.TakeString(), "Is this brief?"), |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 55 | "Annotation: Ano\n\nIs this brief?"); |
| 56 | } |
| 57 | |
| 58 | TEST_F(CompletionStringTest, MultipleAnnotations) { |
| 59 | Builder.AddAnnotation("Ano1"); |
| 60 | Builder.AddAnnotation("Ano2"); |
| 61 | Builder.AddAnnotation("Ano3"); |
| 62 | |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 63 | EXPECT_EQ(formatDocumentation(*Builder.TakeString(), ""), |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 64 | "Annotations: Ano1 Ano2 Ano3\n"); |
| 65 | } |
| 66 | |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 67 | TEST_F(CompletionStringTest, EmptySignature) { |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 68 | Builder.AddTypedTextChunk("X"); |
| 69 | Builder.AddResultTypeChunk("result no no"); |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 70 | computeSignature(*Builder.TakeString()); |
| 71 | EXPECT_EQ(Signature, ""); |
| 72 | EXPECT_EQ(Snippet, ""); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 75 | TEST_F(CompletionStringTest, Function) { |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 76 | Builder.AddResultTypeChunk("result no no"); |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 77 | Builder.addBriefComment("This comment is ignored"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 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 | |
| 85 | auto *CCS = Builder.TakeString(); |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 86 | computeSignature(*CCS); |
| 87 | EXPECT_EQ(Signature, "(p1, p2)"); |
| 88 | EXPECT_EQ(Snippet, "(${1:p1}, ${2:p2})"); |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 89 | EXPECT_EQ(formatDocumentation(*CCS, "Foo's comment"), "Foo's comment"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST_F(CompletionStringTest, EscapeSnippet) { |
| 93 | Builder.AddTypedTextChunk("Foo"); |
| 94 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 95 | Builder.AddPlaceholderChunk("$p}1\\"); |
| 96 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 97 | |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 98 | computeSignature(*Builder.TakeString()); |
| 99 | EXPECT_EQ(Signature, "($p}1\\)"); |
| 100 | EXPECT_EQ(Snippet, "(${1:\\$p\\}1\\\\})"); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | TEST_F(CompletionStringTest, IgnoreInformativeQualifier) { |
| 104 | Builder.AddTypedTextChunk("X"); |
| 105 | Builder.AddInformativeChunk("info ok"); |
| 106 | Builder.AddInformativeChunk("info no no::"); |
Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 107 | computeSignature(*Builder.TakeString()); |
| 108 | EXPECT_EQ(Signature, "info ok"); |
| 109 | EXPECT_EQ(Snippet, ""); |
Eric Liu | 63696e1 | 2017-12-20 17:24:31 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace |
| 113 | } // namespace clangd |
| 114 | } // namespace clang |