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_api_test.h"
17
18 #include <gtest/gtest.h>
19 #include <cstddef>
20
21 #include "ir/astNode.h"
22 #include "lsp/include/internal_api.h"
23 #include "public/es2panda_lib.h"
24
25 namespace {
26
27 using ark::es2panda::lsp::Initializer;
28
TEST_F(LSPAPITests,GetCurrentTokenValue)29 TEST_F(LSPAPITests, GetCurrentTokenValue)
30 {
31 Initializer initializer = Initializer();
32 es2panda_Context *ctx = initializer.CreateContext("current_token.ets", ES2PANDA_STATE_CHECKED, "ab");
33 LSPAPI const *lspApi = GetImpl();
34 size_t offset = 2;
35 std::string result = lspApi->getCurrentTokenValue(ctx, offset);
36 initializer.DestroyContext(ctx);
37 ASSERT_EQ(result, "ab");
38 }
39
TEST_F(LSPAPITests,GetCurrentTokenValue1)40 TEST_F(LSPAPITests, GetCurrentTokenValue1)
41 {
42 Initializer initializer = Initializer();
43 es2panda_Context *ctx = initializer.CreateContext("file1.ets", ES2PANDA_STATE_CHECKED, "\"ab\"");
44 size_t offset = 3;
45 std::string result = ark::es2panda::lsp::GetCurrentTokenValueImpl(ctx, offset);
46 std::string expect = "ab";
47 ASSERT_EQ(result, expect);
48 initializer.DestroyContext(ctx);
49 }
50
TEST_F(LSPAPITests,GetCurrentTokenValue2)51 TEST_F(LSPAPITests, GetCurrentTokenValue2)
52 {
53 Initializer initializer = Initializer();
54 es2panda_Context *ctx = initializer.CreateContext("file1.ets", ES2PANDA_STATE_CHECKED, "\'ab\'");
55 size_t offset = 3;
56 std::string result = ark::es2panda::lsp::GetCurrentTokenValueImpl(ctx, offset);
57 std::string expect = "ab";
58 ASSERT_EQ(result, expect);
59 initializer.DestroyContext(ctx);
60 }
61
TEST_F(LSPAPITests,GetCurrentTokenValue3)62 TEST_F(LSPAPITests, GetCurrentTokenValue3)
63 {
64 Initializer initializer = Initializer();
65 es2panda_Context *ctx = initializer.CreateContext("file1.ets", ES2PANDA_STATE_CHECKED, "abc");
66 size_t offset = 2;
67 std::string result = ark::es2panda::lsp::GetCurrentTokenValueImpl(ctx, offset);
68 std::string expect = "ab";
69 ASSERT_EQ(result, expect);
70 initializer.DestroyContext(ctx);
71 }
72
TEST_F(LSPAPITests,GetTokenPosOfNode1)73 TEST_F(LSPAPITests, GetTokenPosOfNode1)
74 {
75 using ark::es2panda::ir::AstNode;
76
77 Initializer initializer = Initializer();
78 es2panda_Context *ctx = initializer.CreateContext("token-pos-identifier.ets", ES2PANDA_STATE_CHECKED,
79 "function A(a:number, b:number) {\n return a + b;\n}\nA(1, 2);");
80 ASSERT_EQ(ContextState(ctx), ES2PANDA_STATE_CHECKED);
81
82 auto ast = GetAstFromContext<AstNode>(ctx);
83 auto targetNode =
84 ast->FindChild([](AstNode *node) { return node->IsIdentifier() && node->AsIdentifier()->Name() == "A"; });
85
86 ASSERT_NE(targetNode, nullptr);
87 auto result = ark::es2panda::lsp::GetTokenPosOfNode(targetNode);
88 size_t const pos = 51;
89 ASSERT_EQ(result, pos);
90
91 initializer.DestroyContext(ctx);
92 }
93
TEST_F(LSPAPITests,GetTokenPosOfNode2)94 TEST_F(LSPAPITests, GetTokenPosOfNode2)
95 {
96 using ark::es2panda::ir::AstNode;
97
98 Initializer initializer = Initializer();
99 es2panda_Context *ctx = initializer.CreateContext("token-pos-expression.ets", ES2PANDA_STATE_CHECKED,
100 "function A(a:number, b:number) {\n return a + b;\n}\nA(1, 2);");
101 ASSERT_EQ(ContextState(ctx), ES2PANDA_STATE_CHECKED);
102
103 auto ast = GetAstFromContext<AstNode>(ctx);
104 auto targetNode = ast->FindChild([](AstNode *node) { return node->IsExpressionStatement(); });
105
106 ASSERT_NE(targetNode, nullptr);
107 auto result = ark::es2panda::lsp::GetTokenPosOfNode(targetNode);
108 size_t const pos = 51;
109 ASSERT_EQ(result, pos);
110
111 initializer.DestroyContext(ctx);
112 }
113
TEST_F(LSPAPITests,GetTokenPosOfNode3)114 TEST_F(LSPAPITests, GetTokenPosOfNode3)
115 {
116 using ark::es2panda::ir::AstNode;
117
118 Initializer initializer = Initializer();
119 es2panda_Context *ctx = initializer.CreateContext(
120 "token-pos-literal.ets", ES2PANDA_STATE_CHECKED,
121 "let number_literal: number = 1234;\nlet string_literal: string = \"hello\";\nconst str_property = "
122 "\"foo\";\n");
123 ASSERT_EQ(ContextState(ctx), ES2PANDA_STATE_CHECKED);
124
125 auto ast = GetAstFromContext<AstNode>(ctx);
126 auto targetNode = ast->FindChild(
127 [](AstNode *node) { return node->IsNumberLiteral() && node->AsNumberLiteral()->Str() == "1234"; });
128
129 ASSERT_NE(targetNode, nullptr);
130 auto result = ark::es2panda::lsp::GetTokenPosOfNode(targetNode);
131 size_t const pos = 29;
132 ASSERT_EQ(result, pos);
133
134 initializer.DestroyContext(ctx);
135 }
136
137 } // namespace
138