Skip to content

Commit 74bc0b0

Browse files
committed
添加一部分选项的单元测试
1 parent 03bd828 commit 74bc0b0

File tree

4 files changed

+324
-7
lines changed

4 files changed

+324
-7
lines changed

CodeService/src/Format/Analyzer/TokenAnalyzer.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,8 @@ bool IsSingleTableOrStringArg(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t)
150150
auto expr = exprs.front();
151151
if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
152152
return true;
153-
} else if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::LiteralExpression) {
154-
auto firstToken = expr.GetFirstToken(t);
155-
return firstToken.GetTokenKind(t) == TK_STRING
156-
|| firstToken.GetTokenKind(t) == TK_LONG_STRING;
153+
} else if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression) {
154+
return true;
157155
}
158156
}
159157
}
@@ -174,7 +172,7 @@ LuaSyntaxNode GetSingleArgStringOrTable(LuaSyntaxNode &syntaxNode, const LuaSynt
174172
auto expr = exprs.front();
175173
if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
176174
return expr;
177-
} else if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::LiteralExpression) {
175+
} else if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression) {
178176
auto firstToken = expr.GetFirstToken(t);
179177
if (firstToken.GetTokenKind(t) == TK_STRING
180178
|| firstToken.GetTokenKind(t) == TK_LONG_STRING) {

Test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ target_sources(CodeFormatTest
2020
src/FormatResult_unitest.cpp
2121
src/Performance_unitest.cpp
2222
src/RangeFormat_unitest.cpp
23+
src/FormatStyle_unitest.cpp
2324
)
2425

2526
target_link_libraries(CodeFormatTest CodeService Util gtest)

Test/src/FormatStyle_unitest.cpp

Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
#include <gtest/gtest.h>
2+
#include "TestHelper.h"
3+
4+
TEST(FormatByStyleOption, indent_style) {
5+
LuaStyle style;
6+
7+
style.indent_style = IndentStyle::Space;
8+
EXPECT_TRUE(TestHelper::TestFormatted(
9+
R"(
10+
local t = function()
11+
local a = 131
12+
local b = function()
13+
local c = {
14+
}
15+
end
16+
end
17+
)",
18+
R"(
19+
local t = function()
20+
local a = 131
21+
local b = function()
22+
local c = {
23+
}
24+
end
25+
end
26+
)", style));
27+
28+
style.indent_style = IndentStyle::Tab;
29+
// next is tab
30+
EXPECT_TRUE(TestHelper::TestFormatted(
31+
R"(
32+
local t = function()
33+
local a = 131
34+
local b = function()
35+
local c = {
36+
}
37+
end
38+
end
39+
)",
40+
R"(
41+
local t = function()
42+
local a = 131
43+
local b = function()
44+
local c = {
45+
}
46+
end
47+
end
48+
)", style));
49+
50+
}
51+
52+
TEST(FormatByStyleOption, indent_size) {
53+
LuaStyle style;
54+
55+
style.indent_size = 2;
56+
EXPECT_TRUE(TestHelper::TestFormatted(
57+
R"(
58+
local t = function()
59+
local a = 131
60+
local b = function()
61+
local c = {
62+
}
63+
end
64+
end
65+
)",
66+
R"(
67+
local t = function()
68+
local a = 131
69+
local b = function()
70+
local c = {
71+
}
72+
end
73+
end
74+
)", style));
75+
76+
style.indent_size = 3;
77+
EXPECT_TRUE(TestHelper::TestFormatted(
78+
R"(
79+
local t = function()
80+
local a = 131
81+
local b = function()
82+
local c = {
83+
}
84+
end
85+
end
86+
)",
87+
R"(
88+
local t = function()
89+
local a = 131
90+
local b = function()
91+
local c = {
92+
}
93+
end
94+
end
95+
)", style));
96+
97+
style.indent_size = 4;
98+
EXPECT_TRUE(TestHelper::TestFormatted(
99+
R"(
100+
local t = function()
101+
local a = 131
102+
local b = function()
103+
local c = {
104+
}
105+
end
106+
end
107+
)",
108+
R"(
109+
local t = function()
110+
local a = 131
111+
local b = function()
112+
local c = {
113+
}
114+
end
115+
end
116+
)", style));
117+
}
118+
119+
TEST(FormatByStyleOption, quote_style) {
120+
LuaStyle style;
121+
122+
style.quote_style = QuoteStyle::None;
123+
EXPECT_TRUE(TestHelper::TestFormatted(
124+
R"(
125+
local t = "aaa" .. 'bbb' .. '\"\"' .. [[123]]
126+
)",
127+
R"(
128+
local t = "aaa" .. 'bbb' .. '\"\"' .. [[123]]
129+
)", style));
130+
style.quote_style = QuoteStyle::Double;
131+
EXPECT_TRUE(TestHelper::TestFormatted(
132+
R"(
133+
local t = "aaa" .. 'bbb' .. [[123]] .. '""'
134+
)",
135+
R"(
136+
local t = "aaa" .. "bbb" .. [[123]] .. '""'
137+
)", style));
138+
style.quote_style = QuoteStyle::Single;
139+
EXPECT_TRUE(TestHelper::TestFormatted(
140+
R"(
141+
local t = "aaa" .. 'bbb' .. '\"\"' .. [[123]] .. '""'
142+
)",
143+
R"(
144+
local t = 'aaa' .. 'bbb' .. '\"\"' .. [[123]] .. '""'
145+
)", style));
146+
}
147+
148+
TEST(FormatByStyleOption, continuation_indent) {
149+
LuaStyle style;
150+
151+
style.continuation_indent = 4;
152+
EXPECT_TRUE(TestHelper::TestFormatted(
153+
R"(
154+
if a
155+
or c then
156+
157+
end
158+
table.insert(t, {
159+
aaa = 123
160+
})
161+
)",
162+
R"(
163+
if a
164+
or c then
165+
166+
end
167+
table.insert(t, {
168+
aaa = 123
169+
})
170+
)", style));
171+
style.continuation_indent = 8;
172+
EXPECT_TRUE(TestHelper::TestFormatted(
173+
R"(
174+
if a
175+
or c then
176+
177+
end
178+
table.insert(t, {
179+
aaa = 123
180+
})
181+
)",
182+
R"(
183+
if a
184+
or c then
185+
186+
end
187+
table.insert(t, {
188+
aaa = 123
189+
})
190+
)", style));
191+
}
192+
193+
TEST(FormatByStyleOption, max_line_length) {
194+
LuaStyle style;
195+
196+
style.max_line_length = 80;
197+
EXPECT_TRUE(TestHelper::TestFormatted(
198+
R"(
199+
function f(aaaaaaaaaaaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbbbb,cccccccccccccccccccccccccccccc)
200+
201+
end
202+
)",
203+
R"(
204+
function f(aaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbb,
205+
cccccccccccccccccccccccccccccc)
206+
207+
end
208+
)", style));
209+
style.max_line_length = 120;
210+
EXPECT_TRUE(TestHelper::TestFormatted(
211+
R"(
212+
function f(aaaaaaaaaaaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbbbb,cccccccccccccccccccccccccccccc)
213+
214+
end
215+
)",
216+
R"(
217+
function f(aaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccccccc)
218+
219+
end
220+
)", style));
221+
}
222+
223+
TEST(FormatByStyleOption, end_of_line_not_test_case) {
224+
}
225+
226+
TEST(FormatByStyleOption, trailing_table_separator) {
227+
LuaStyle style;
228+
229+
style.trailing_table_separator = TrailingTableSeparator::Keep;
230+
EXPECT_TRUE(TestHelper::TestFormatted(
231+
R"(
232+
local t = { aaa, bbb, ccc, }
233+
)",
234+
R"(
235+
local t = { aaa, bbb, ccc, }
236+
)", style));
237+
style.trailing_table_separator = TrailingTableSeparator::Never;
238+
EXPECT_TRUE(TestHelper::TestFormatted(
239+
R"(
240+
local t = { aaa, bbb, ccc, }
241+
)",
242+
R"(
243+
local t = { aaa, bbb, ccc }
244+
)", style));
245+
style.trailing_table_separator = TrailingTableSeparator::Always;
246+
EXPECT_TRUE(TestHelper::TestFormatted(
247+
R"(
248+
local t = { aaa, bbb, ccc }
249+
)",
250+
R"(
251+
local t = { aaa, bbb, ccc, }
252+
)", style));
253+
style.trailing_table_separator = TrailingTableSeparator::Smart;
254+
EXPECT_TRUE(TestHelper::TestFormatted(
255+
R"(
256+
local t = { aaa, bbb, ccc, }
257+
local d = {
258+
a = 123,
259+
b = 123
260+
}
261+
)",
262+
R"(
263+
local t = { aaa, bbb, ccc }
264+
local d = {
265+
a = 123,
266+
b = 123,
267+
}
268+
)", style));
269+
}
270+
271+
TEST(FormatByStyleOption, call_arg_parentheses) {
272+
LuaStyle style;
273+
274+
style.call_arg_parentheses = CallArgParentheses::Keep;
275+
EXPECT_TRUE(TestHelper::TestFormatted(
276+
R"(
277+
p(123)
278+
p "12313"
279+
p("456")
280+
)",
281+
R"(
282+
p(123)
283+
p "12313"
284+
p("456")
285+
)", style));
286+
style.call_arg_parentheses = CallArgParentheses::Remove;
287+
EXPECT_TRUE(TestHelper::TestFormatted(
288+
R"(
289+
p(123)
290+
p "12313"
291+
p("456")
292+
)",
293+
R"(
294+
p(123)
295+
p "12313"
296+
p "456"
297+
)", style));
298+
style.call_arg_parentheses = CallArgParentheses::RemoveStringOnly;
299+
EXPECT_TRUE(TestHelper::TestFormatted(
300+
R"(
301+
p("456")
302+
p({1231})
303+
)",
304+
R"(
305+
p "456"
306+
p({ 1231 })
307+
)", style));
308+
style.call_arg_parentheses = CallArgParentheses::RemoveTableOnly;
309+
EXPECT_TRUE(TestHelper::TestFormatted(
310+
R"(
311+
p("456")
312+
p({1231})
313+
)",
314+
R"(
315+
p("456")
316+
p { 1231 }
317+
)", style));
318+
}

Test2/src/FormatTest2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
int main() {
88
std::string buffer = R"(
9-
if a then
10-
else end
9+
p("456")
1110
)";
1211

1312
auto file = std::make_shared<LuaFile>(std::move(buffer));
@@ -22,6 +21,7 @@ else end
2221
std::cout << t.GetDebugView() << std::endl;
2322

2423
LuaStyle s;
24+
s.call_arg_parentheses = CallArgParentheses::Remove;
2525
FormatBuilder b(s);
2626
auto text = b.GetFormatResult(t);
2727
std::cout<< text << std::endl;

0 commit comments

Comments
 (0)