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 LspConvExpTests : public LSPAPITests {
26 public:
27 static constexpr std::string_view TO_NAMED_EXPORT_KIND = "refactor.rewrite.export.named";
28 static constexpr std::string_view TO_NAMED_EXPORT_NAME = "Convert default export to named export";
29 static constexpr std::string_view TO_DEFAULT_EXPORT_KIND = "refactor.rewrite.export.default";
30 static constexpr std::string_view TO_DEFAULT_EXPORT_NAME = "Convert named export to default export";
31 };
32
TEST_F(LspConvExpTests,ConvertExportRefactor1)33 TEST_F(LspConvExpTests, ConvertExportRefactor1)
34 {
35 std::vector<std::string> files = {"convertExportRefactor1.ets"};
36 std::vector<std::string> texts = {R"(export default function add(a: number, b: number): number {
37 return a + b;
38 })"};
39 auto filePaths = CreateTempFile(files, texts);
40 size_t const expectedFileCount = 1;
41 ASSERT_EQ(filePaths.size(), expectedFileCount);
42
43 size_t const position = 26;
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_NAMED_EXPORT_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_NAMED_EXPORT_NAME), result[0].action.name);
54 }
55
TEST_F(LspConvExpTests,ConvertExportRefactor2)56 TEST_F(LspConvExpTests, ConvertExportRefactor2)
57 {
58 std::vector<std::string> files = {"convertExportRefactor2.ets"};
59 std::vector<std::string> texts = {R"(export default class User {
60 name: string = "";
61 })"};
62 auto filePaths = CreateTempFile(files, texts);
63 size_t const expectedFileCount = 1;
64 ASSERT_EQ(filePaths.size(), expectedFileCount);
65
66 size_t const position = 24;
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_EXPORT_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_EXPORT_NAME), result[0].action.name);
77 }
78
TEST_F(LspConvExpTests,ConvertExportRefactor5)79 TEST_F(LspConvExpTests, ConvertExportRefactor5)
80 {
81 std::vector<std::string> files = {"convertExportRefactor5.ets"};
82 std::vector<std::string> texts = {R"(export function add(a: number, b: number): number {
83 return a + b;
84 })"};
85 auto filePaths = CreateTempFile(files, texts);
86 size_t const expectedFileCount = 1;
87 ASSERT_EQ(filePaths.size(), expectedFileCount);
88
89 size_t const position = 19;
90 Initializer initializer = Initializer();
91 auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
92 ark::es2panda::lsp::RefactorContext refactorContext;
93 refactorContext.context = ctx;
94 refactorContext.kind = std::string(TO_DEFAULT_EXPORT_KIND);
95 refactorContext.span.pos = position;
96 auto result = GetApplicableRefactorsImpl(&refactorContext);
97 initializer.DestroyContext(ctx);
98 ASSERT_EQ(1, result.size());
99 ASSERT_EQ(std::string(TO_DEFAULT_EXPORT_NAME), result[0].action.name);
100 }
101 } // namespace