• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include <cstddef>
18 #include <string>
19 #include "lsp_api_test.h"
20 #include "lsp/include/applicable_refactors.h"
21 
22 namespace {
23 using ark::es2panda::lsp::Initializer;
24 
25 class LspConvImpTests : public LSPAPITests {
26 public:
27     static constexpr std::string_view TO_NAMED_IMPORT_KIND = "refactor.rewrite.import.named";
28     static constexpr std::string_view TO_NAMED_IMPORT_NAME = "Convert namespace import to named imports";
29     static constexpr std::string_view TO_NAMESPACE_IMPORT_KIND = "refactor.rewrite.import.namespace";
30     static constexpr std::string_view TO_NAMESPACE_IMPORT_NAME = "Convert named imports to namespace import";
31 };
32 
TEST_F(LspConvImpTests,ConvertImportRefactor1)33 TEST_F(LspConvImpTests, ConvertImportRefactor1)
34 {
35     std::vector<std::string> files = {"convertImportRefactor1.ets"};
36     std::vector<std::string> texts = {R"(import { add, subtract, multiply } from './math';
37 
38 const result = add(1, subtract(5, multiply(2, 3)));)"};
39     auto filePaths = CreateTempFile(files, texts);
40     size_t const expectedFileCount = 1;
41     ASSERT_EQ(filePaths.size(), expectedFileCount);
42 
43     size_t const position = 28;
44     Initializer initializer = Initializer();
45     auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
46     ark::es2panda::lsp::RefactorContext refactorContext;
47     refactorContext.context = ctx;
48     refactorContext.kind = std::string(TO_NAMESPACE_IMPORT_KIND);
49     refactorContext.span.pos = position;
50     auto result = GetApplicableRefactorsImpl(&refactorContext);
51     initializer.DestroyContext(ctx);
52     ASSERT_EQ(1, result.size());
53     ASSERT_EQ(std::string(TO_NAMESPACE_IMPORT_NAME), result[0].action.name);
54 }
55 
TEST_F(LspConvImpTests,ConvertImportRefactor2)56 TEST_F(LspConvImpTests, ConvertImportRefactor2)
57 {
58     std::vector<std::string> files = {"convertImportRefactor2.ets"};
59     std::vector<std::string> texts = {R"(import * as math from './math';
60 
61 const result = math.add(1, math.subtract(5, math.multiply(2, 3)));)"};
62     auto filePaths = CreateTempFile(files, texts);
63     size_t const expectedFileCount = 1;
64     ASSERT_EQ(filePaths.size(), expectedFileCount);
65 
66     size_t const position = 15;
67     Initializer initializer = Initializer();
68     auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
69     ark::es2panda::lsp::RefactorContext refactorContext;
70     refactorContext.context = ctx;
71     refactorContext.kind = std::string(TO_NAMED_IMPORT_KIND);
72     refactorContext.span.pos = position;
73     auto result = GetApplicableRefactorsImpl(&refactorContext);
74     initializer.DestroyContext(ctx);
75     ASSERT_EQ(1, result.size());
76     ASSERT_EQ(std::string(TO_NAMED_IMPORT_NAME), result[0].action.name);
77 }
78 }  // namespace