• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "lsp/include/completions.h"
17 #include <gtest/gtest.h>
18 #include "lsp_api_test.h"
19 
20 class LspKeywordCompletionTests : public LSPAPITests {};
21 
AssertCompletionsContainAndNotContainEntries(const std::vector<std::string> & entries,const std::vector<std::string> & expectedEntries,const std::vector<std::string> & unexpectedEntries)22 static void AssertCompletionsContainAndNotContainEntries(const std::vector<std::string> &entries,
23                                                          const std::vector<std::string> &expectedEntries,
24                                                          const std::vector<std::string> &unexpectedEntries)
25 {
26     auto emptyCheck = expectedEntries.empty() && !entries.empty();
27     ASSERT_FALSE(emptyCheck) << "Expected empty but the result is not. Actual account: " << entries.size();
28 
29     for (const auto &expectedEntry : expectedEntries) {
30         bool found = false;
31         for (const auto &entry : entries) {
32             if (entry == expectedEntry) {
33                 found = true;
34                 break;
35             }
36         }
37         ASSERT_TRUE(found) << "Expected completion '" << expectedEntry << "' not found";
38     }
39 
40     for (const auto &unexpectedEntry : unexpectedEntries) {
41         bool found = false;
42         for (const auto &entry : entries) {
43             if (entry == unexpectedEntry) {
44                 found = true;
45                 break;
46             }
47         }
48         ASSERT_FALSE(found) << "Unexpected completion '" << unexpectedEntry << "' found";
49     }
50 }
51 
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsStartWithA)52 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsStartWithA)
53 {
54     std::string input = "a";
55     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
56 
57     std::vector<std::string> expected = {"abstract", "any", "anyref", "arguments", "as", "asserts", "async", "await"};
58 
59     std::vector<std::string> actual;
60     for (const auto &entry : result.keywordCompletions) {
61         actual.push_back(entry.GetName());
62     }
63 
64     AssertCompletionsContainAndNotContainEntries(actual, expected, {});
65 }
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsStartWithAs)66 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsStartWithAs)
67 {
68     std::string input = "as";
69 
70     std::vector<std::string> expected = {"as", "asserts", "async"};
71     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
72 
73     std::vector<std::string> actual;
74     for (const auto &entry : result.keywordCompletions) {
75         actual.push_back(entry.GetName());
76     }
77 
78     AssertCompletionsContainAndNotContainEntries(actual, expected, {});
79 }
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsStartWithAs2)80 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsStartWithAs2)
81 {
82     std::string input = "asse";
83     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
84 
85     std::vector<std::string> expected = {"asserts"};
86 
87     std::vector<std::string> actual;
88     for (const auto &entry : result.keywordCompletions) {
89         actual.push_back(entry.GetName());
90     }
91 
92     AssertCompletionsContainAndNotContainEntries(actual, expected, {});
93 }
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsStartWithRe)94 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsStartWithRe)
95 {
96     std::string input = "re";
97     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
98 
99     std::vector<std::string> expected = {"readonly", "rethrows", "return", "require"};
100 
101     std::vector<std::string> actual;
102     for (const auto &entry : result.keywordCompletions) {
103         actual.push_back(entry.GetName());
104     }
105 
106     AssertCompletionsContainAndNotContainEntries(actual, expected, {});
107 }
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsStartWithRet)108 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsStartWithRet)
109 {
110     std::string input = "ret";
111     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
112 
113     std::vector<std::string> expected = {"rethrows", "return"};
114 
115     std::vector<std::string> actual;
116     for (const auto &entry : result.keywordCompletions) {
117         actual.push_back(entry.GetName());
118     }
119 
120     AssertCompletionsContainAndNotContainEntries(actual, expected, {});
121 }
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsStartWithI)122 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsStartWithI)
123 {
124     std::string input = "i";
125     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
126 
127     std::vector<std::string> expected = {"i8",         "i16",    "i31ref", "i32",     "i64",        "if",
128                                          "implements", "import", "in",     "infer",   "instanceof", "int",
129                                          "interface",  "is",     "isize",  "internal"};
130 
131     std::vector<std::string> actual;
132     for (const auto &entry : result.keywordCompletions) {
133         actual.push_back(entry.GetName());
134     }
135 
136     AssertCompletionsContainAndNotContainEntries(actual, expected, {});
137 }
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsStartWithInt)138 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsStartWithInt)
139 {
140     std::string input = "int";
141     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
142 
143     std::vector<std::string> expected = {"int", "interface", "internal"};
144 
145     std::vector<std::string> actual;
146     for (const auto &entry : result.keywordCompletions) {
147         actual.push_back(entry.GetName());
148     }
149 
150     AssertCompletionsContainAndNotContainEntries(actual, expected, {});
151 }
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsSensitiveStartWithT)152 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsSensitiveStartWithT)
153 {
154     std::string input = "T";
155     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
156     std::vector<std::string> expected = {"target", "this", "throw", "throws", "true", "try", "type", "typeof"};
157 
158     std::vector<std::string> actual;
159     for (const auto &entry : result.keywordCompletions) {
160         actual.push_back(entry.GetName());
161     }
162 
163     AssertCompletionsContainAndNotContainEntries(actual, expected, {});
164 }
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsSensitiveStartWithTy)165 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsSensitiveStartWithTy)
166 {
167     std::string input = "TY";
168     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
169     std::vector<std::string> expected = {"type", "typeof"};
170 
171     std::vector<std::string> actual;
172     for (const auto &entry : result.keywordCompletions) {
173         actual.push_back(entry.GetName());
174     }
175 
176     AssertCompletionsContainAndNotContainEntries(actual, expected, {});
177 }
TEST_F(LspKeywordCompletionTests,GetKeywordCompletionsInvalid)178 TEST_F(LspKeywordCompletionTests, GetKeywordCompletionsInvalid)
179 {
180     std::string input = "xyzabc";
181     ark::es2panda::lsp::Request result = ark::es2panda::lsp::KeywordCompletionData(input);
182     auto completions = result.keywordCompletions;
183 
184     ASSERT_TRUE(completions.empty());
185 }
186