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 #include <string>
18 #include <vector>
19
20 using ark::es2panda::lsp::Initializer;
21
22 class LspGetSpanTests : public LSPAPITests {};
23
TEST_F(LspGetSpanTests,getSpanOfEnclosingComment1)24 TEST_F(LspGetSpanTests, getSpanOfEnclosingComment1)
25 {
26 std::vector<std::string> files = {"get_span_1.ets"};
27 std::vector<std::string> texts = {"function A(a:number, b:number) {\n return a + b; // add\n}\nA(1, 2);"};
28 auto filePaths = CreateTempFile(files, texts);
29 LSPAPI const *lspApi = GetImpl();
30 size_t const offset = 60;
31 Initializer initializer = Initializer();
32 auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
33 auto result = lspApi->getSpanOfEnclosingComment(ctx, offset, false);
34 size_t const startPostion = 0;
35 size_t const length = 0;
36 ASSERT_EQ(result.start, startPostion);
37 ASSERT_EQ(result.length, length);
38 auto result1 = lspApi->getSpanOfEnclosingComment(ctx, offset, true);
39 ASSERT_EQ(result1.start, startPostion);
40 ASSERT_EQ(result1.length, length);
41 initializer.DestroyContext(ctx);
42 }
43
TEST_F(LspGetSpanTests,getSpanOfEnclosingComment2)44 TEST_F(LspGetSpanTests, getSpanOfEnclosingComment2)
45 {
46 std::vector<std::string> files = {"get_span_2.ets"};
47 std::vector<std::string> texts = {"function A(a:number, b:number) {\n return a + b; // add\n}\nA(1, 2);"};
48 auto filePaths = CreateTempFile(files, texts);
49 LSPAPI const *lspApi = GetImpl();
50 size_t const offset = 54;
51 Initializer initializer = Initializer();
52 auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
53 auto result = lspApi->getSpanOfEnclosingComment(ctx, offset, false);
54 size_t const startPostion = 50;
55 size_t const length = 6;
56 ASSERT_EQ(result.start, startPostion);
57 ASSERT_EQ(result.length, length);
58 auto result1 = lspApi->getSpanOfEnclosingComment(ctx, offset, true);
59 size_t const startPostion1 = 0;
60 size_t const length1 = 0;
61 ASSERT_EQ(result1.start, startPostion1);
62 ASSERT_EQ(result1.length, length1);
63 initializer.DestroyContext(ctx);
64 }
65
TEST_F(LspGetSpanTests,getSpanOfEnclosingComment3)66 TEST_F(LspGetSpanTests, getSpanOfEnclosingComment3)
67 {
68 std::vector<std::string> files = {"get_span_3.ets"};
69 std::vector<std::string> texts = {"function A(a:number, b:number) {\n return a + b; /* add */\n}\nA(1, 2);"};
70 auto filePaths = CreateTempFile(files, texts);
71 LSPAPI const *lspApi = GetImpl();
72 size_t const offset = 54;
73 Initializer initializer = Initializer();
74 auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
75 auto result = lspApi->getSpanOfEnclosingComment(ctx, offset, false);
76 size_t const startPostion = 50;
77 size_t const length = 9;
78 ASSERT_EQ(result.start, startPostion);
79 ASSERT_EQ(result.length, length);
80 auto result1 = lspApi->getSpanOfEnclosingComment(ctx, offset, true);
81 ASSERT_EQ(result1.start, startPostion);
82 ASSERT_EQ(result1.length, length);
83 initializer.DestroyContext(ctx);
84 }
85