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,GetTouchingToken1)29 TEST_F(LSPAPITests, GetTouchingToken1)
30 {
31 Initializer initializer = Initializer();
32 es2panda_Context *ctx = initializer.CreateContext("not-found-node.ets", ES2PANDA_STATE_CHECKED,
33 "function A(a:number, b:number) {\n return a + b;\n}\nA(1, 2);");
34 ASSERT_EQ(ContextState(ctx), ES2PANDA_STATE_CHECKED);
35 size_t const offset = 50;
36 auto result = ark::es2panda::lsp::GetTouchingToken(ctx, offset, false);
37 ASSERT_EQ(result, nullptr);
38
39 auto result1 = ark::es2panda::lsp::GetTouchingToken(ctx, offset, true);
40 ASSERT_EQ(result1, nullptr);
41 initializer.DestroyContext(ctx);
42 }
43
TEST_F(LSPAPITests,GetTouchingToken2)44 TEST_F(LSPAPITests, GetTouchingToken2)
45 {
46 Initializer initializer = Initializer();
47 es2panda_Context *ctx = initializer.CreateContext("nested-node.ets", ES2PANDA_STATE_CHECKED,
48 "function A(a:number, b:number) {\n return a + b;\n}\nA(1, 2);");
49 ASSERT_EQ(ContextState(ctx), ES2PANDA_STATE_CHECKED);
50 size_t const offset = 51;
51 auto result = ark::es2panda::lsp::GetTouchingToken(ctx, offset, false);
52 auto ast = GetAstFromContext<ark::es2panda::ir::AstNode>(ctx);
53 auto expectedNode = ast->FindChild(
54 [](ark::es2panda::ir::AstNode *node) { return node->IsIdentifier() && node->AsIdentifier()->Name() == "A"; });
55 ASSERT_EQ(result->DumpJSON(), expectedNode->DumpJSON());
56 ASSERT_EQ(result->Start().index, expectedNode->Start().index);
57 ASSERT_EQ(result->End().index, expectedNode->End().index);
58 initializer.DestroyContext(ctx);
59 }
60
TEST_F(LSPAPITests,GetTouchingToken3)61 TEST_F(LSPAPITests, GetTouchingToken3)
62 {
63 Initializer initializer = Initializer();
64 es2panda_Context *ctx = initializer.CreateContext("first-node.ets", ES2PANDA_STATE_CHECKED,
65 "function A(a:number, b:number) {\n return a + b;\n}\nA(1, 2);");
66 ASSERT_EQ(ContextState(ctx), ES2PANDA_STATE_CHECKED);
67 size_t const offset = 51;
68 auto result = ark::es2panda::lsp::GetTouchingToken(ctx, offset, true);
69 auto ast = GetAstFromContext<ark::es2panda::ir::AstNode>(ctx);
70 auto expectedNode = ast->FindChild([](ark::es2panda::ir::AstNode *node) { return node->IsExpressionStatement(); });
71 ASSERT_EQ(result->DumpJSON(), expectedNode->DumpJSON());
72 ASSERT_EQ(result->Start().index, expectedNode->Start().index);
73 ASSERT_EQ(result->End().index, expectedNode->End().index);
74 initializer.DestroyContext(ctx);
75 }
76
77 } // namespace
78