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/include/find_safe_delete_location.h"
17 #include "lsp_api_test.h"
18 #include "lsp/include/references.h"
19 #include <gtest/gtest.h>
20
21 namespace {
22 using ark::es2panda::lsp::Initializer;
23 } // namespace
24
25 class FindSafeDeleteLocationTests : public LSPAPITests {};
26
TEST_F(FindSafeDeleteLocationTests,FindSafeDeleteLocationBasic)27 TEST_F(FindSafeDeleteLocationTests, FindSafeDeleteLocationBasic)
28 {
29 std::vector<std::string> files = {"safe_delete_test_basic.ets"};
30 std::vector<std::string> texts = {R"(function foo() {} foo(); foo();)"};
31
32 auto filePaths = CreateTempFile(files, texts);
33
34 Initializer initializer;
35 es2panda_Context *ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
36
37 constexpr size_t TOKEN_POSITION = 9;
38 constexpr size_t RESULT_SIZE = 3;
39 auto astNode = ark::es2panda::lsp::GetTouchingToken(ctx, TOKEN_POSITION, false);
40
41 auto declInfo = ark::es2panda::lsp::GetDeclInfoImpl(astNode);
42
43 LSPAPI const *lspApi = GetImpl();
44 auto locations = lspApi->FindSafeDeleteLocation(ctx, &declInfo);
45
46 ASSERT_EQ(locations.size(), RESULT_SIZE);
47
48 initializer.DestroyContext(ctx);
49 }
50
TEST_F(FindSafeDeleteLocationTests,FindSafeDeleteLocationNoResult)51 TEST_F(FindSafeDeleteLocationTests, FindSafeDeleteLocationNoResult)
52 {
53 std::vector<std::string> files = {"safe_delete_test_no_result.ets"};
54 std::vector<std::string> texts = {R"(let x = 1;)"};
55
56 auto filePaths = CreateTempFile(files, texts);
57
58 Initializer initializer;
59 es2panda_Context *ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
60
61 constexpr size_t TOKEN_POSITION = 4;
62 constexpr size_t RESULT_SIZE = 1;
63 auto astNode = ark::es2panda::lsp::GetTouchingToken(ctx, TOKEN_POSITION, false);
64
65 auto declInfo = ark::es2panda::lsp::GetDeclInfoImpl(astNode);
66
67 LSPAPI const *lspApi = GetImpl();
68 auto locations = lspApi->FindSafeDeleteLocation(ctx, &declInfo);
69
70 ASSERT_EQ(locations.size(), RESULT_SIZE);
71
72 initializer.DestroyContext(ctx);
73 }
74
TEST_F(FindSafeDeleteLocationTests,FindSafeDeleteLocationDeclInfoEmpty)75 TEST_F(FindSafeDeleteLocationTests, FindSafeDeleteLocationDeclInfoEmpty)
76 {
77 std::vector<std::string> files = {"safe_delete_test_empty.ets"};
78 std::vector<std::string> texts = {R"(let x: number = 1;)"};
79
80 auto filePaths = CreateTempFile(files, texts);
81
82 Initializer initializer;
83 es2panda_Context *ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
84
85 std::tuple<std::string, std::string> declInfo = {"", ""};
86
87 LSPAPI const *lspApi = GetImpl();
88 auto locations = lspApi->FindSafeDeleteLocation(ctx, &declInfo);
89
90 ASSERT_TRUE(locations.empty());
91
92 initializer.DestroyContext(ctx);
93 }
94