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 <gtest/gtest.h>
17 #include "lsp/include/user_preferences.h"
18 #include "lsp_api_test.h"
19
20 class LspUserPrefTests : public LSPAPITests {};
21
TEST_F(LspUserPrefTests,UserPreferencesDefaultConstructor)22 TEST_F(LspUserPrefTests, UserPreferencesDefaultConstructor)
23 {
24 using ark::es2panda::lsp::UserPreferences;
25 UserPreferences defaultPref;
26
27 ASSERT_EQ(defaultPref.GetQuotePreference(), UserPreferences::QuotePreference::AUTO);
28 ASSERT_EQ(defaultPref.GetIncludePackageJsonAutoImports(), UserPreferences::IncludePackageJsonAutoImports::AUTO);
29 ASSERT_EQ(defaultPref.GetIncludeInlayParameterNameHints(), UserPreferences::IncludeInlayParameterNameHints::NONE);
30 ASSERT_EQ(defaultPref.GetImportModuleSpecifierPreference(),
31 UserPreferences::ImportModuleSpecifierPreference::SHORTEST);
32 ASSERT_EQ(defaultPref.GetImportModuleSpecifierEnding(), UserPreferences::ImportModuleSpecifierEnding::AUTO);
33 ASSERT_EQ(defaultPref.GetAutoImportFileExcludePatterns(), "");
34 ASSERT_EQ(defaultPref.GetDisableSuggestions(), false);
35 ASSERT_EQ(defaultPref.GetIncludeCompletionsForModuleExports(), false);
36 ASSERT_EQ(defaultPref.GetIncludeCompletionsForImportStatements(), false);
37 ASSERT_EQ(defaultPref.GetIncludeCompletionsWithSnippetText(), false);
38 ASSERT_EQ(defaultPref.GetIncludeCompletionsChainCompletions(), false);
39 ASSERT_EQ(defaultPref.GetIncludeCompletionsWithInsertText(), false);
40 ASSERT_EQ(defaultPref.GetIncludeCompletionsClassMemberSnippets(), false);
41 ASSERT_EQ(defaultPref.GetIncludeCompletionsWithObjectLiteralMethodSnippets(), false);
42 ASSERT_EQ(defaultPref.GetUseLabelDetailsIncompletionsEntries(), false);
43 ASSERT_EQ(defaultPref.GetAllowIncompleteCompletions(), false);
44 ASSERT_EQ(defaultPref.GetAllowTextChangesInNewFiles(), false);
45 ASSERT_EQ(defaultPref.GetProvidePrefixAndSuffixTextForRename(), false);
46 ASSERT_EQ(defaultPref.GetProvideRefactorNotApplicableReason(), false);
47 ASSERT_EQ(defaultPref.GetIncludeInlayParameterNameHintsWhenArgumentMatchesName(), false);
48 ASSERT_EQ(defaultPref.GetIncludeInlayFunctionParameterTypeHints(), false);
49 ASSERT_EQ(defaultPref.GetIncludeInlayVariableTypeHints(), false);
50 ASSERT_EQ(defaultPref.GetIncludeInlayVariableTypeHintsWhenTypeMatchesName(), false);
51 ASSERT_EQ(defaultPref.GetIncludeInlayPropertyDeclarationTypeHints(), false);
52 ASSERT_EQ(defaultPref.GetIncludeInlayFunctionLikeReturnTypeHints(), false);
53 ASSERT_EQ(defaultPref.GetIncludeInlayEnumMemberValueHints(), false);
54 ASSERT_EQ(defaultPref.GetAllowRenameOfImportPath(), false);
55 }
56
TEST_F(LspUserPrefTests,UserPreferencesQuotePreferenceSetter)57 TEST_F(LspUserPrefTests, UserPreferencesQuotePreferenceSetter)
58 {
59 using ark::es2panda::lsp::UserPreferences;
60 UserPreferences userpreferences;
61
62 userpreferences.SetQuotePreference(UserPreferences::QuotePreference::DOUBLE);
63 ASSERT_EQ(userpreferences.GetQuotePreference(), UserPreferences::QuotePreference::DOUBLE);
64
65 userpreferences.SetQuotePreference(UserPreferences::QuotePreference::SINGLE);
66 ASSERT_EQ(userpreferences.GetQuotePreference(), UserPreferences::QuotePreference::SINGLE);
67
68 userpreferences.SetQuotePreference(UserPreferences::QuotePreference::AUTO);
69 ASSERT_EQ(userpreferences.GetQuotePreference(), UserPreferences::QuotePreference::AUTO);
70 }
71
TEST_F(LspUserPrefTests,UserPreferencesIncludePackageJsonAutoImportsSetter)72 TEST_F(LspUserPrefTests, UserPreferencesIncludePackageJsonAutoImportsSetter)
73 {
74 using ark::es2panda::lsp::UserPreferences;
75 UserPreferences userpreferences;
76
77 userpreferences.SetIncludePackageJsonAutoImports(UserPreferences::IncludePackageJsonAutoImports::ON);
78 ASSERT_EQ(userpreferences.GetIncludePackageJsonAutoImports(), UserPreferences::IncludePackageJsonAutoImports::ON);
79
80 userpreferences.SetIncludePackageJsonAutoImports(UserPreferences::IncludePackageJsonAutoImports::OFF);
81 ASSERT_EQ(userpreferences.GetIncludePackageJsonAutoImports(), UserPreferences::IncludePackageJsonAutoImports::OFF);
82
83 userpreferences.SetIncludePackageJsonAutoImports(UserPreferences::IncludePackageJsonAutoImports::AUTO);
84 ASSERT_EQ(userpreferences.GetIncludePackageJsonAutoImports(), UserPreferences::IncludePackageJsonAutoImports::AUTO);
85 }
86
TEST_F(LspUserPrefTests,UserPreferencesIncludeInlayParameterNameHintsSetter)87 TEST_F(LspUserPrefTests, UserPreferencesIncludeInlayParameterNameHintsSetter)
88 {
89 using ark::es2panda::lsp::UserPreferences;
90 UserPreferences userpref;
91
92 userpref.SetIncludeInlayParameterNameHints(UserPreferences::IncludeInlayParameterNameHints::ALL);
93 ASSERT_EQ(userpref.GetIncludeInlayParameterNameHints(), UserPreferences::IncludeInlayParameterNameHints::ALL);
94
95 userpref.SetIncludeInlayParameterNameHints(UserPreferences::IncludeInlayParameterNameHints::LITERALS);
96 ASSERT_EQ(userpref.GetIncludeInlayParameterNameHints(), UserPreferences::IncludeInlayParameterNameHints::LITERALS);
97 }
98
TEST_F(LspUserPrefTests,UserPreferencesImportModuleSpecifierPreferenceSetter)99 TEST_F(LspUserPrefTests, UserPreferencesImportModuleSpecifierPreferenceSetter)
100 {
101 using ark::es2panda::lsp::UserPreferences;
102 UserPreferences usp;
103
104 usp.SetImportModuleSpecifierPreference(UserPreferences::ImportModuleSpecifierPreference::SHORTEST);
105 ASSERT_EQ(usp.GetImportModuleSpecifierPreference(), UserPreferences::ImportModuleSpecifierPreference::SHORTEST);
106
107 usp.SetImportModuleSpecifierPreference(UserPreferences::ImportModuleSpecifierPreference::PROJECT_RELATIVE);
108 ASSERT_EQ(usp.GetImportModuleSpecifierPreference(),
109 UserPreferences::ImportModuleSpecifierPreference::PROJECT_RELATIVE);
110
111 usp.SetImportModuleSpecifierPreference(UserPreferences::ImportModuleSpecifierPreference::RELATIVE);
112 ASSERT_EQ(usp.GetImportModuleSpecifierPreference(), UserPreferences::ImportModuleSpecifierPreference::RELATIVE);
113
114 usp.SetImportModuleSpecifierPreference(UserPreferences::ImportModuleSpecifierPreference::NON_RELATIVE);
115 EXPECT_EQ(usp.GetImportModuleSpecifierPreference(), UserPreferences::ImportModuleSpecifierPreference::NON_RELATIVE);
116 }
117
TEST_F(LspUserPrefTests,UserPreferencesImportModuleSpecifierEndingSetter)118 TEST_F(LspUserPrefTests, UserPreferencesImportModuleSpecifierEndingSetter)
119 {
120 using ark::es2panda::lsp::UserPreferences;
121 UserPreferences userpreferences;
122
123 userpreferences.SetImportModuleSpecifierEnding(UserPreferences::ImportModuleSpecifierEnding::AUTO);
124 ASSERT_EQ(userpreferences.GetImportModuleSpecifierEnding(), UserPreferences::ImportModuleSpecifierEnding::AUTO);
125
126 userpreferences.SetImportModuleSpecifierEnding(UserPreferences::ImportModuleSpecifierEnding::MINIMAL);
127 ASSERT_EQ(userpreferences.GetImportModuleSpecifierEnding(), UserPreferences::ImportModuleSpecifierEnding::MINIMAL);
128
129 userpreferences.SetImportModuleSpecifierEnding(UserPreferences::ImportModuleSpecifierEnding::JS);
130 ASSERT_EQ(userpreferences.GetImportModuleSpecifierEnding(), UserPreferences::ImportModuleSpecifierEnding::JS);
131
132 userpreferences.SetImportModuleSpecifierEnding(UserPreferences::ImportModuleSpecifierEnding::INDEX);
133 ASSERT_EQ(userpreferences.GetImportModuleSpecifierEnding(), UserPreferences::ImportModuleSpecifierEnding::INDEX);
134 }
135
TEST_F(LspUserPrefTests,UserPreferencesAutoImportFileExcludePatternsSetter)136 TEST_F(LspUserPrefTests, UserPreferencesAutoImportFileExcludePatternsSetter)
137 {
138 using ark::es2panda::lsp::UserPreferences;
139 UserPreferences userpreferences;
140
141 const std::string testPattern = "*.test.js,*.spec.js";
142 userpreferences.SetAutoImportFileExcludePatterns(testPattern);
143 ASSERT_EQ(userpreferences.GetAutoImportFileExcludePatterns(), testPattern);
144 }
145
TEST_F(LspUserPrefTests,UserPreferencesBooleanSetters)146 TEST_F(LspUserPrefTests, UserPreferencesBooleanSetters)
147 {
148 using ark::es2panda::lsp::UserPreferences;
149 UserPreferences userpreferences;
150
151 userpreferences.SetDisableSuggestions(true);
152 ASSERT_TRUE(userpreferences.GetDisableSuggestions());
153 // Reset and test false values
154 userpreferences.SetDisableSuggestions(false);
155 ASSERT_FALSE(userpreferences.GetDisableSuggestions());
156
157 userpreferences.SetIncludeCompletionsForModuleExports(true);
158 ASSERT_TRUE(userpreferences.GetIncludeCompletionsForModuleExports());
159
160 userpreferences.SetIncludeCompletionsForModuleExports(false);
161 ASSERT_FALSE(userpreferences.GetIncludeCompletionsForModuleExports());
162
163 userpreferences.SetIncludeCompletionsForImportStatements(true);
164 ASSERT_TRUE(userpreferences.GetIncludeCompletionsForImportStatements());
165
166 userpreferences.SetIncludeCompletionsForImportStatements(false);
167 ASSERT_FALSE(userpreferences.GetIncludeCompletionsForImportStatements());
168
169 userpreferences.SetIncludeCompletionsWithSnippetText(true);
170 ASSERT_TRUE(userpreferences.GetIncludeCompletionsWithSnippetText());
171
172 userpreferences.SetIncludeCompletionsWithSnippetText(false);
173 ASSERT_FALSE(userpreferences.GetIncludeCompletionsWithSnippetText());
174
175 userpreferences.SetIncludeCompletionsChainCompletions(true);
176 ASSERT_TRUE(userpreferences.GetIncludeCompletionsChainCompletions());
177
178 userpreferences.SetIncludeCompletionsChainCompletions(false);
179 ASSERT_FALSE(userpreferences.GetIncludeCompletionsChainCompletions());
180
181 userpreferences.SetIncludeCompletionsWithInsertText(true);
182 ASSERT_TRUE(userpreferences.GetIncludeCompletionsWithInsertText());
183
184 userpreferences.SetIncludeCompletionsWithInsertText(false);
185 ASSERT_FALSE(userpreferences.GetIncludeCompletionsWithInsertText());
186
187 userpreferences.SetIncludeCompletionsClassMemberSnippets(true);
188 ASSERT_TRUE(userpreferences.GetIncludeCompletionsClassMemberSnippets());
189
190 userpreferences.SetIncludeCompletionsClassMemberSnippets(false);
191 ASSERT_FALSE(userpreferences.GetIncludeCompletionsClassMemberSnippets());
192
193 userpreferences.SetIncludeCompletionsWithObjectLiteralMethodSnippets(true);
194 ASSERT_TRUE(userpreferences.GetIncludeCompletionsWithObjectLiteralMethodSnippets());
195
196 userpreferences.SetIncludeCompletionsWithObjectLiteralMethodSnippets(false);
197 ASSERT_FALSE(userpreferences.GetIncludeCompletionsWithObjectLiteralMethodSnippets());
198
199 userpreferences.SetUseLabelDetailsIncompletionsEntries(true);
200 ASSERT_TRUE(userpreferences.GetUseLabelDetailsIncompletionsEntries());
201
202 userpreferences.SetUseLabelDetailsIncompletionsEntries(false);
203 ASSERT_FALSE(userpreferences.GetUseLabelDetailsIncompletionsEntries());
204
205 userpreferences.SetAllowIncompleteCompletions(true);
206 ASSERT_TRUE(userpreferences.GetAllowIncompleteCompletions());
207
208 userpreferences.SetAllowIncompleteCompletions(false);
209 ASSERT_FALSE(userpreferences.GetAllowIncompleteCompletions());
210
211 userpreferences.SetAllowTextChangesInNewFiles(true);
212 ASSERT_TRUE(userpreferences.GetAllowTextChangesInNewFiles());
213
214 userpreferences.SetAllowTextChangesInNewFiles(false);
215 ASSERT_FALSE(userpreferences.GetAllowTextChangesInNewFiles());
216 }
217
TEST_F(LspUserPrefTests,UserPreferencesBooleanSetters2)218 TEST_F(LspUserPrefTests, UserPreferencesBooleanSetters2)
219 {
220 using ark::es2panda::lsp::UserPreferences;
221 UserPreferences userpreferences;
222
223 userpreferences.SetProvidePrefixAndSuffixTextForRename(true);
224 ASSERT_TRUE(userpreferences.GetProvidePrefixAndSuffixTextForRename());
225
226 userpreferences.SetProvidePrefixAndSuffixTextForRename(false);
227 ASSERT_FALSE(userpreferences.GetProvidePrefixAndSuffixTextForRename());
228
229 userpreferences.SetProvideRefactorNotApplicableReason(true);
230 ASSERT_TRUE(userpreferences.GetProvideRefactorNotApplicableReason());
231
232 userpreferences.SetProvideRefactorNotApplicableReason(false);
233 ASSERT_FALSE(userpreferences.GetProvideRefactorNotApplicableReason());
234
235 userpreferences.SetIncludeInlayParameterNameHintsWhenArgumentMatchesName(true);
236 ASSERT_TRUE(userpreferences.GetIncludeInlayParameterNameHintsWhenArgumentMatchesName());
237
238 userpreferences.SetIncludeInlayParameterNameHintsWhenArgumentMatchesName(false);
239 ASSERT_FALSE(userpreferences.GetIncludeInlayParameterNameHintsWhenArgumentMatchesName());
240
241 userpreferences.SetIncludeInlayFunctionParameterTypeHints(true);
242 ASSERT_TRUE(userpreferences.GetIncludeInlayFunctionParameterTypeHints());
243
244 userpreferences.SetIncludeInlayFunctionParameterTypeHints(false);
245 ASSERT_FALSE(userpreferences.GetIncludeInlayFunctionParameterTypeHints());
246
247 userpreferences.SetIncludeInlayVariableTypeHints(true);
248 ASSERT_TRUE(userpreferences.GetIncludeInlayVariableTypeHints());
249
250 userpreferences.SetIncludeInlayVariableTypeHints(false);
251 ASSERT_FALSE(userpreferences.GetIncludeInlayVariableTypeHints());
252
253 userpreferences.SetIncludeInlayVariableTypeHintsWhenTypeMatchesName(true);
254 ASSERT_TRUE(userpreferences.GetIncludeInlayVariableTypeHintsWhenTypeMatchesName());
255
256 userpreferences.SetIncludeInlayVariableTypeHintsWhenTypeMatchesName(false);
257 ASSERT_FALSE(userpreferences.GetIncludeInlayVariableTypeHintsWhenTypeMatchesName());
258
259 userpreferences.SetIncludeInlayPropertyDeclarationTypeHints(true);
260 ASSERT_TRUE(userpreferences.GetIncludeInlayPropertyDeclarationTypeHints());
261
262 userpreferences.SetIncludeInlayPropertyDeclarationTypeHints(false);
263 ASSERT_FALSE(userpreferences.GetIncludeInlayPropertyDeclarationTypeHints());
264
265 userpreferences.SetIncludeInlayFunctionLikeReturnTypeHints(true);
266 ASSERT_TRUE(userpreferences.GetIncludeInlayFunctionLikeReturnTypeHints());
267
268 userpreferences.SetIncludeInlayFunctionLikeReturnTypeHints(false);
269 ASSERT_FALSE(userpreferences.GetIncludeInlayFunctionLikeReturnTypeHints());
270
271 userpreferences.SetIncludeInlayEnumMemberValueHints(true);
272 ASSERT_TRUE(userpreferences.GetIncludeInlayEnumMemberValueHints());
273
274 userpreferences.SetIncludeInlayEnumMemberValueHints(false);
275 ASSERT_FALSE(userpreferences.GetIncludeInlayEnumMemberValueHints());
276
277 userpreferences.SetAllowRenameOfImportPath(true);
278 ASSERT_TRUE(userpreferences.GetAllowRenameOfImportPath());
279
280 userpreferences.SetAllowRenameOfImportPath(false);
281 ASSERT_FALSE(userpreferences.GetAllowRenameOfImportPath());
282 }
283
TEST_F(LspUserPrefTests,UserPreferencesGetDefaultUserPreferences)284 TEST_F(LspUserPrefTests, UserPreferencesGetDefaultUserPreferences)
285 {
286 using ark::es2panda::lsp::UserPreferences;
287 UserPreferences defaultPrefs = UserPreferences::GetDefaultUserPreferences();
288
289 ASSERT_EQ(defaultPrefs.GetQuotePreference(), UserPreferences::QuotePreference::AUTO);
290 ASSERT_EQ(defaultPrefs.GetIncludePackageJsonAutoImports(), UserPreferences::IncludePackageJsonAutoImports::AUTO);
291 ASSERT_EQ(defaultPrefs.GetIncludeInlayParameterNameHints(), UserPreferences::IncludeInlayParameterNameHints::NONE);
292 ASSERT_EQ(defaultPrefs.GetImportModuleSpecifierPreference(),
293 UserPreferences::ImportModuleSpecifierPreference::SHORTEST);
294 ASSERT_EQ(defaultPrefs.GetImportModuleSpecifierEnding(), UserPreferences::ImportModuleSpecifierEnding::AUTO);
295 ASSERT_EQ(defaultPrefs.GetAutoImportFileExcludePatterns(), "");
296 ASSERT_FALSE(defaultPrefs.GetDisableSuggestions());
297 ASSERT_FALSE(defaultPrefs.GetIncludeCompletionsForModuleExports());
298 ASSERT_FALSE(defaultPrefs.GetIncludeCompletionsForImportStatements());
299 ASSERT_FALSE(defaultPrefs.GetIncludeCompletionsWithSnippetText());
300 ASSERT_FALSE(defaultPrefs.GetIncludeCompletionsChainCompletions());
301 ASSERT_FALSE(defaultPrefs.GetIncludeCompletionsWithInsertText());
302 ASSERT_FALSE(defaultPrefs.GetIncludeCompletionsClassMemberSnippets());
303 ASSERT_FALSE(defaultPrefs.GetIncludeCompletionsWithObjectLiteralMethodSnippets());
304 ASSERT_FALSE(defaultPrefs.GetUseLabelDetailsIncompletionsEntries());
305 ASSERT_FALSE(defaultPrefs.GetAllowIncompleteCompletions());
306 ASSERT_FALSE(defaultPrefs.GetAllowTextChangesInNewFiles());
307 ASSERT_FALSE(defaultPrefs.GetProvidePrefixAndSuffixTextForRename());
308 ASSERT_FALSE(defaultPrefs.GetProvideRefactorNotApplicableReason());
309 ASSERT_FALSE(defaultPrefs.GetIncludeInlayParameterNameHintsWhenArgumentMatchesName());
310 ASSERT_FALSE(defaultPrefs.GetIncludeInlayFunctionParameterTypeHints());
311 ASSERT_FALSE(defaultPrefs.GetIncludeInlayVariableTypeHints());
312 ASSERT_FALSE(defaultPrefs.GetIncludeInlayVariableTypeHintsWhenTypeMatchesName());
313 ASSERT_FALSE(defaultPrefs.GetIncludeInlayPropertyDeclarationTypeHints());
314 ASSERT_FALSE(defaultPrefs.GetIncludeInlayFunctionLikeReturnTypeHints());
315 ASSERT_FALSE(defaultPrefs.GetIncludeInlayEnumMemberValueHints());
316 ASSERT_FALSE(defaultPrefs.GetAllowRenameOfImportPath());
317 }
318
TEST_F(LspUserPrefTests,UserPreferencesGetUserPreferences)319 TEST_F(LspUserPrefTests, UserPreferencesGetUserPreferences)
320 {
321 using ark::es2panda::lsp::UserPreferences;
322 UserPreferences userpreferences;
323
324 UserPreferences *currentPrefs = userpreferences.GetUserPreferences();
325 ASSERT_EQ(currentPrefs, &userpreferences);
326
327 userpreferences.SetQuotePreference(UserPreferences::QuotePreference::DOUBLE);
328 userpreferences.SetIncludePackageJsonAutoImports(UserPreferences::IncludePackageJsonAutoImports::ON);
329 userpreferences.SetDisableSuggestions(true);
330
331 UserPreferences *modifiedPrefs = userpreferences.GetUserPreferences();
332 ASSERT_EQ(modifiedPrefs->GetQuotePreference(), UserPreferences::QuotePreference::DOUBLE);
333 ASSERT_EQ(modifiedPrefs->GetIncludePackageJsonAutoImports(), UserPreferences::IncludePackageJsonAutoImports::ON);
334 ASSERT_TRUE(modifiedPrefs->GetDisableSuggestions());
335 }
336