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/formatting/formatting_context.h"
17 #include "lsp_api_test.h"
18 #include <gtest/gtest.h>
19 #include "ir/astNode.h"
20
21 namespace {
22
23 class LSPFormattingContextTests : public LSPAPITests {};
24
TEST_F(LSPFormattingContextTests,FormattingContextConstructorTest)25 TEST_F(LSPFormattingContextTests, FormattingContextConstructorTest)
26 {
27 const size_t defaultIndentSize = 4U;
28 const std::string defaultNewLine = "\n";
29
30 ark::es2panda::lsp::FormatCodeSettings settings;
31 settings.SetIndentSize(defaultIndentSize);
32 settings.SetNewLineCharacter(defaultNewLine);
33 settings.SetConvertTabsToSpaces(true);
34
35 ark::es2panda::lsp::FormattingContext context(ark::es2panda::lsp::FormattingRequestKind::FORMAT_DOCUMENT, settings);
36
37 EXPECT_EQ(context.GetformattingRequestKind(), ark::es2panda::lsp::FormattingRequestKind::FORMAT_DOCUMENT);
38 EXPECT_EQ(context.GetFormatCodeSettings().GetIndentSize(), defaultIndentSize);
39 }
40
TEST_F(LSPFormattingContextTests,FormattingContextSameLineTest)41 TEST_F(LSPFormattingContextTests, FormattingContextSameLineTest)
42 {
43 const size_t firstLine = 1U;
44 const size_t idStart = 4U;
45 const size_t numStart = 8U;
46 const size_t semiStart = 10U;
47
48 ark::es2panda::lsp::Initializer initializer;
49 auto ctx = initializer.CreateContext("sameLineTest.ets", ES2PANDA_STATE_CHECKED, "let x = 10;");
50 auto contextPtr = reinterpret_cast<ark::es2panda::public_lib::Context *>(ctx);
51 auto program = contextPtr->parserProgram;
52
53 ark::es2panda::lsp::FormatCodeSettings settings;
54 ark::es2panda::lsp::FormattingContext context(ark::es2panda::lsp::FormattingRequestKind::FORMAT_ON_ENTER, settings);
55
56 ark::es2panda::lexer::SourcePosition pos1(idStart, firstLine, program);
57 ark::es2panda::lexer::SourcePosition pos2(numStart, firstLine, program);
58 ark::es2panda::lexer::SourcePosition pos3(semiStart, firstLine, program);
59 ark::es2panda::lsp::RangeWithKind span1(pos1, pos2, ark::es2panda::ir::AstNodeType::IDENTIFIER);
60 ark::es2panda::lsp::RangeWithKind span2(pos2, pos3, ark::es2panda::ir::AstNodeType::NUMBER_LITERAL);
61
62 context.UpdateContext(ctx, span1, span2);
63
64 EXPECT_TRUE(context.TokensAreOnSameLine());
65
66 initializer.DestroyContext(ctx);
67 }
68
TEST_F(LSPFormattingContextTests,FormattingContextDifferentLinesTest)69 TEST_F(LSPFormattingContextTests, FormattingContextDifferentLinesTest)
70 {
71 const size_t firstLine = 1U;
72 const size_t secondLine = 2U;
73 const size_t idStart = 4U;
74 const size_t numStart = 8U;
75
76 ark::es2panda::lsp::Initializer initializer;
77 auto ctx = initializer.CreateContext("differentLinesTest.ets", ES2PANDA_STATE_CHECKED, "let x = 10;\nlet y = 20;");
78 auto contextPtr = reinterpret_cast<ark::es2panda::public_lib::Context *>(ctx);
79 auto program = contextPtr->parserProgram;
80
81 ark::es2panda::lsp::FormatCodeSettings settings;
82 ark::es2panda::lsp::FormattingContext context(ark::es2panda::lsp::FormattingRequestKind::FORMAT_ON_SEMICOLON,
83 settings);
84
85 ark::es2panda::lexer::SourcePosition pos1(idStart, firstLine, program);
86 ark::es2panda::lexer::SourcePosition pos2(numStart, firstLine, program);
87 ark::es2panda::lexer::SourcePosition pos3(idStart, secondLine, program);
88 ark::es2panda::lexer::SourcePosition pos4(numStart, secondLine, program);
89 ark::es2panda::lsp::RangeWithKind span1(pos1, pos2, ark::es2panda::ir::AstNodeType::IDENTIFIER);
90 ark::es2panda::lsp::RangeWithKind span2(pos3, pos4, ark::es2panda::ir::AstNodeType::IDENTIFIER);
91
92 context.UpdateContext(ctx, span1, span2);
93
94 EXPECT_TRUE(context.TokensAreOnSameLine());
95
96 initializer.DestroyContext(ctx);
97 }
98
TEST_F(LSPFormattingContextTests,FormattingCodeSettingsTest)99 TEST_F(LSPFormattingContextTests, FormattingCodeSettingsTest)
100 {
101 const size_t indentSize = 2U;
102 const size_t tabSize = 4U;
103 const std::string newlineChar = "\r\n";
104
105 ark::es2panda::lsp::FormatCodeSettings settings;
106
107 settings.SetIndentSize(indentSize);
108 settings.SetTabSize(tabSize);
109 settings.SetNewLineCharacter(newlineChar);
110 settings.SetConvertTabsToSpaces(true);
111 settings.SetInsertSpaceAfterCommaDelimiter(true);
112 settings.SetInsertSpaceAfterSemicolonInForStatements(true);
113 settings.SetInsertSpaceBeforeAndAfterBinaryOperators(true);
114
115 ark::es2panda::lsp::FormattingContext context(ark::es2panda::lsp::FormattingRequestKind::FORMAT_DOCUMENT, settings);
116
117 auto contextSettings = context.GetFormatCodeSettings();
118 EXPECT_EQ(contextSettings.GetIndentSize(), indentSize);
119 EXPECT_EQ(contextSettings.GetTabSize(), tabSize);
120 EXPECT_EQ(contextSettings.GetNewLineCharacter(), newlineChar);
121 EXPECT_TRUE(contextSettings.GetConvertTabsToSpaces());
122 EXPECT_TRUE(contextSettings.GetInsertSpaceAfterCommaDelimiter());
123 EXPECT_TRUE(contextSettings.GetInsertSpaceAfterSemicolonInForStatements());
124 EXPECT_TRUE(contextSettings.GetInsertSpaceBeforeAndAfterBinaryOperators());
125 }
126
127 } // namespace