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+ }
0 commit comments