• 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 #include <gtest/gtest.h>
16 #include <cstdio>
17 #include "lsp_api_test.h"
18 #include "public/es2panda_lib.h"
19 #include "lsp/include/internal_api.h"
20 #include "ir/astNode.h"
21 #include "lsp/include/string_completions.h"
22 
23 using ark::es2panda::lsp::Initializer;
24 
25 class SLCTests : public LSPAPITests {};
26 
27 // SLC -> StringLiteralCompletions
TEST_F(SLCTests,SLCInterfaceTest)28 TEST_F(SLCTests, SLCInterfaceTest)
29 {
30     Initializer initializer = Initializer();
31     es2panda_Context *context = initializer.CreateContext("not-found-node.ets", ES2PANDA_STATE_CHECKED,
32                                                           "interface X { name: string}; let variable: X = { name: '");
33 
34     const size_t declarationPosition = 8;
35     const size_t cursorPosition = 55;
36     auto declaration = ark::es2panda::lsp::GetTouchingToken(context, declarationPosition, false);
37     auto cursor = ark::es2panda::lsp::GetTouchingToken(context, cursorPosition, false);
38 
39     auto completions = ark::es2panda::lsp::GetStringLiteralCompletions(cursor, declaration);
40     ASSERT_TRUE(completions != std::nullopt);
41 
42     std::string name = "name";
43     std::string type = "string";
44     auto expectedInsertText = name += std::string(": ") += type;
45     ASSERT_TRUE(completions->GetEntries().front().GetInsertText() == expectedInsertText);
46 
47     initializer.DestroyContext(context);
48 }
49 
TEST_F(SLCTests,SLCClassDeclarationTest)50 TEST_F(SLCTests, SLCClassDeclarationTest)
51 {
52     Initializer initializer = Initializer();
53     es2panda_Context *context = initializer.CreateContext("not-found-node.ets", ES2PANDA_STATE_CHECKED,
54                                                           "class C { name: string}; let variable: C = { name: '");
55 
56     const size_t declarationPosition = 4;
57     const size_t cursorPosition = 52;
58     auto declaration = ark::es2panda::lsp::GetTouchingToken(context, declarationPosition, false);
59     auto cursor = ark::es2panda::lsp::GetTouchingToken(context, cursorPosition, false);
60 
61     auto completions = ark::es2panda::lsp::GetStringLiteralCompletions(cursor, declaration);
62     ASSERT_TRUE(completions != std::nullopt);
63 
64     std::string name = "name";
65     std::string type = "string";
66     auto expectedInsertText = name += std::string(": ") += type;
67     ASSERT_TRUE(completions->GetEntries().front().GetInsertText() == expectedInsertText);
68 
69     initializer.DestroyContext(context);
70 }
71