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 <cstddef>
17 #include <iostream>
18 #include "lsp_api_test.h"
19 #include "lsp/include/get_definition_and_bound_span.h"
20 #include <gtest/gtest.h>
21
22 namespace {
23
24 class LSPGetDefinitionAndBoundSpanTests : public LSPAPITests {};
25
TEST_F(LSPGetDefinitionAndBoundSpanTests,GetDefinitionAndBoundSpan_FunctionReference)26 TEST_F(LSPGetDefinitionAndBoundSpanTests, GetDefinitionAndBoundSpan_FunctionReference)
27 {
28 const auto fileName = "GetDefinitionAndBoundSpan1.ets";
29 const auto fileContent = R"(
30 function sum(a: number, b: number): number {
31 return a + b;
32 }
33
34 let total = sum(5, 10);
35 )";
36
37 const size_t offset = 79;
38 const size_t index0 = 0;
39 const size_t index3 = 3;
40 const size_t index13 = 13;
41
42 std::vector<std::string> files = {fileName};
43 std::vector<std::string> texts = {fileContent};
44 auto filePaths = CreateTempFile(files, texts);
45
46 ark::es2panda::lsp::Initializer initializer;
47 es2panda_Context *ctx = initializer.CreateContext(filePaths.at(index0).c_str(), ES2PANDA_STATE_CHECKED);
48
49 const auto result = ark::es2panda::lsp::GetDefinitionAndBoundSpan(ctx, offset);
50
51 EXPECT_FALSE(result.definitionInfo.fileName.empty());
52 EXPECT_EQ(result.definitionInfo.fileName, filePaths.at(index0));
53 EXPECT_EQ(result.definitionInfo.length, index3);
54 EXPECT_EQ(result.boundSpan.length, index3);
55 EXPECT_EQ(result.definitionInfo.start + result.definitionInfo.length, index13);
56
57 const size_t expectedDefOffset = 10;
58 EXPECT_EQ(result.definitionInfo.start, expectedDefOffset);
59 initializer.DestroyContext(ctx);
60 }
61
TEST_F(LSPGetDefinitionAndBoundSpanTests,GetDefinitionAndBoundSpan_FunctionReferenceTwoFile)62 TEST_F(LSPGetDefinitionAndBoundSpanTests, GetDefinitionAndBoundSpan_FunctionReferenceTwoFile)
63 {
64 std::vector<std::string> fileNames = {"GetDefinitionAndBoundSpan_export.ets", "GetDefinitionAndBoundSpan2.ets"};
65 std::vector<std::string> texts = {
66 R"(export function A(a:number, b:number): number {
67 return a + b;
68 }
69 export function B(a:number, b:number): number {
70 return a + b;
71 })",
72 R"(import {A} from "./GetDefinitionAndBoundSpan_export";
73 import {B} from "./GetDefinitionAndBoundSpan_export.ets";
74 A(1, 2);
75 B(1, 2);)"};
76
77 auto filePaths = CreateTempFile(fileNames, texts);
78 int const expectedFileCount = 2;
79 ASSERT_EQ(filePaths.size(), expectedFileCount);
80
81 const size_t offset = 116;
82 const size_t index0 = 0;
83 const size_t index1 = 1;
84 const size_t index13 = 17;
85 ark::es2panda::lsp::Initializer initializer;
86 es2panda_Context *ctx = initializer.CreateContext(filePaths.at(index1).c_str(), ES2PANDA_STATE_CHECKED);
87
88 const auto result = ark::es2panda::lsp::GetDefinitionAndBoundSpan(ctx, offset);
89
90 EXPECT_FALSE(result.definitionInfo.fileName.empty());
91 EXPECT_EQ(result.definitionInfo.fileName, filePaths.at(index0));
92 EXPECT_EQ(result.definitionInfo.length, index1);
93 EXPECT_EQ(result.boundSpan.length, index1);
94 EXPECT_EQ(result.definitionInfo.start + result.definitionInfo.length, index13);
95
96 const size_t expectedDefOffset = 16;
97 EXPECT_EQ(result.definitionInfo.start, expectedDefOffset);
98 initializer.DestroyContext(ctx);
99 }
100
101 } // namespace