aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/reserve_comments_spec.lua
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-02-20 13:22:58 +0000
committerLi Jin <dragon-fly@qq.com>2026-02-20 13:22:58 +0000
commit28253ef50ffa976ed70e6635e4d0ccf7f1842fcd (patch)
treee653776d8560e30b7885b9113defe7c073db5278 /spec/outputs/test/reserve_comments_spec.lua
parent635ce55c778eb0ccf0cb0f5d495402dbbb42deb1 (diff)
parent1c93c88a3f79cfce68ee6d8c4cd85156aaad1101 (diff)
downloadyuescript-28253ef50ffa976ed70e6635e4d0ccf7f1842fcd.tar.gz
yuescript-28253ef50ffa976ed70e6635e4d0ccf7f1842fcd.tar.bz2
yuescript-28253ef50ffa976ed70e6635e4d0ccf7f1842fcd.zip
Merge branch 'feature/reserve-comments-tests' into codex/remove-line-number-comments-from-empty-linesHEADmain
Diffstat (limited to 'spec/outputs/test/reserve_comments_spec.lua')
-rw-r--r--spec/outputs/test/reserve_comments_spec.lua471
1 files changed, 471 insertions, 0 deletions
diff --git a/spec/outputs/test/reserve_comments_spec.lua b/spec/outputs/test/reserve_comments_spec.lua
new file mode 100644
index 0000000..b4e8174
--- /dev/null
+++ b/spec/outputs/test/reserve_comments_spec.lua
@@ -0,0 +1,471 @@
1return describe("reserve_comments option", function()
2 local to_lua
3 do
4 local _obj_0 = require("yue")
5 to_lua = _obj_0.to_lua
6 end
7 it("should preserve top-level comments with reserve_comment option", function()
8 local code = [[-- Top level comment
9x = 1
10-- Another comment
11y = 2
12]]
13 local result = to_lua(code, {
14 reserve_comment = true
15 })
16 assert.is_true(result:match("Top level comment") ~= nil)
17 return assert.is_true(result:match("Another comment") ~= nil)
18 end)
19 it("should NOT preserve comments without reserve_comment option", function()
20 local code = [[-- Top level comment
21x = 1
22-- Another comment
23y = 2
24]]
25 local result = to_lua(code, { })
26 assert.is_true(result:match("Top level comment") == nil)
27 return assert.is_true(result:match("Another comment") == nil)
28 end)
29 it("should preserve comments in table literals", function()
30 local code = [[t = {
31 -- First value comment
32 1,
33 -- Second value comment
34 2
35}
36]]
37 local result = to_lua(code, {
38 reserve_comment = true
39 })
40 assert.is_true(result:match("First value comment") ~= nil)
41 return assert.is_true(result:match("Second value comment") ~= nil)
42 end)
43 it("should preserve comments in if statement", function()
44 local code = [[if true
45 -- Inside if block
46 print "test"
47]]
48 local result = to_lua(code, {
49 reserve_comment = true
50 })
51 return assert.is_true(result:match("Inside if block") ~= nil)
52 end)
53 it("should preserve comments in function body", function()
54 local code = [[func = =>
55 -- Inside function
56 print "hello"
57]]
58 local result = to_lua(code, {
59 reserve_comment = true
60 })
61 return assert.is_true(result:match("Inside function") ~= nil)
62 end)
63 it("should preserve comments in while loop", function()
64 local code = [[while true
65 -- Loop body comment
66 print "looping"
67 break
68]]
69 local result = to_lua(code, {
70 reserve_comment = true
71 })
72 return assert.is_true(result:match("Loop body comment") ~= nil)
73 end)
74 it("should preserve comments in for loop", function()
75 local code = [[for i = 1, 3
76 -- For loop comment
77 print i
78]]
79 local result = to_lua(code, {
80 reserve_comment = true
81 })
82 return assert.is_true(result:match("For loop comment") ~= nil)
83 end)
84 it("should preserve comments in TableBlock syntax", function()
85 local code = [[tbl = {
86 -- Key comment
87 key: "value"
88 -- Another key
89 another: 123
90}
91]]
92 local result = to_lua(code, {
93 reserve_comment = true
94 })
95 assert.is_true(result:match("Key comment") ~= nil)
96 return assert.is_true(result:match("Another key") ~= nil)
97 end)
98 it("should preserve multiple comments across statements", function()
99 local code = [[-- Assign x
100x = 1
101-- Assign y
102y = 2
103-- Assign z
104z = 3
105]]
106 local result = to_lua(code, {
107 reserve_comment = true
108 })
109 assert.is_true(result:match("Assign x") ~= nil)
110 assert.is_true(result:match("Assign y") ~= nil)
111 return assert.is_true(result:match("Assign z") ~= nil)
112 end)
113 it("should handle table with mixed values and comments", function()
114 local code = [[t = {
115 -- First item
116 1,
117 -- Second item
118 2,
119 -- Third item
120 3
121}
122]]
123 local result = to_lua(code, {
124 reserve_comment = true
125 })
126 assert.is_true(result:match("First item") ~= nil)
127 assert.is_true(result:match("Second item") ~= nil)
128 return assert.is_true(result:match("Third item") ~= nil)
129 end)
130 it("should preserve comments in nested structures", function()
131 local code = [[outer = {
132 -- outer comment
133 inner: {
134 -- inner comment
135 value: 42
136 }
137}
138]]
139 local result = to_lua(code, {
140 reserve_comment = true
141 })
142 assert.is_true(result:match("outer comment") ~= nil)
143 return assert.is_true(result:match("inner comment") ~= nil)
144 end)
145 it("should preserve comments in else block", function()
146 local code = [[if false
147 print "if"
148else
149 -- else comment
150 print "else"
151]]
152 local result = to_lua(code, {
153 reserve_comment = true
154 })
155 return assert.is_true(result:match("else comment") ~= nil)
156 end)
157 it("should preserve comments in elseif block", function()
158 local code = [[if false
159 print "if"
160elseif true
161 -- elseif comment
162 print "elseif"
163]]
164 local result = to_lua(code, {
165 reserve_comment = true
166 })
167 return assert.is_true(result:match("elseif comment") ~= nil)
168 end)
169 it("should preserve comments before return statement", function()
170 local code = [[func = =>
171 -- before return
172 return 42
173]]
174 local result = to_lua(code, {
175 reserve_comment = true
176 })
177 return assert.is_true(result:match("before return") ~= nil)
178 end)
179 it("should preserve comments in switch statement", function()
180 local code = [[switch 2
181 when 1
182 -- case 1 comment
183 print "one"
184 when 2
185 -- case 2 comment
186 print "two"
187]]
188 local result = to_lua(code, {
189 reserve_comment = true
190 })
191 assert.is_true(result:match("case 1 comment") ~= nil)
192 return assert.is_true(result:match("case 2 comment") ~= nil)
193 end)
194 it("should preserve comments in with statement", function()
195 local code = [[with t
196 -- with body comment
197 .value = 10
198]]
199 local result = to_lua(code, {
200 reserve_comment = true
201 })
202 return assert.is_true(result:match("with body comment") ~= nil)
203 end)
204 it("should handle empty lines with reserve_comment", function()
205 local code = [[-- First comment
206x = 1
207-- Second comment
208]]
209 local result = to_lua(code, {
210 reserve_comment = true
211 })
212 assert.is_true(result ~= nil)
213 return assert.is_true(type(result) == "string")
214 end)
215 it("should preserve comments in class body", function()
216 local code = [[class MyClass
217 -- property comment
218 value: 10
219 -- method comment
220 method: => print "hello"
221]]
222 local result = to_lua(code, {
223 reserve_comment = true
224 })
225 assert.is_true(result:match("property comment") ~= nil)
226 return assert.is_true(result:match("method comment") ~= nil)
227 end)
228 it("should preserve comments in class with inheritance", function()
229 local code = [[class Child extends Parent
230 -- child property
231 value: 100
232]]
233 local result = to_lua(code, {
234 reserve_comment = true
235 })
236 return assert.is_true(result:match("child property") ~= nil)
237 end)
238 it("should preserve comments in export statement", function()
239 local code = [[-- export value comment
240export x = 42
241]]
242 local result = to_lua(code, {
243 reserve_comment = true
244 })
245 return assert.is_true(result:match("export value comment") ~= nil)
246 end)
247 it("should preserve comments in import statement", function()
248 local code = [[-- import comment
249import format from "string"
250]]
251 local result = to_lua(code, {
252 reserve_comment = true
253 })
254 return assert.is_true(result:match("import comment") ~= nil)
255 end)
256 it("should preserve empty lines between comments in TableBlock", function()
257 local code = "tb =\n\t-- line\n\n\n\n\t--[[ajdjd]]\n\ta: ->\n\n\t-- line 2\n\tb: 123\n"
258 local result = to_lua(code, {
259 reserve_comment = true
260 })
261 assert.is_true(result:match("line") ~= nil)
262 assert.is_true(result:match("ajdjd") ~= nil)
263 return assert.is_true(result:match("line 2") ~= nil)
264 end)
265 it("should preserve block comments in TableBlock", function()
266 local code = "tb =\n\t--[[block comment]]\n\ta: 1\n\n\t--[[another block]]\n\tb: 2\n"
267 local result = to_lua(code, {
268 reserve_comment = true
269 })
270 assert.is_true(result:match("block comment") ~= nil)
271 return assert.is_true(result:match("another block") ~= nil)
272 end)
273 it("should preserve multiple empty lines in table literal", function()
274 local code = "tb = {\n\t-- line\n\n\n\n\t--[[ajdjd]]\n\ta: ->\n\n\t-- line 2\n\tb: 123\n}\n"
275 local result = to_lua(code, {
276 reserve_comment = true
277 })
278 assert.is_true(result:match("line") ~= nil)
279 assert.is_true(result:match("ajdjd") ~= nil)
280 return assert.is_true(result:match("line 2") ~= nil)
281 end)
282 it("should preserve mixed single and block comments in TableBlock", function()
283 local code = "tb =\n\t-- single line comment\n\ta: 1\n\n\t--[[multi\n\tline\n\tblock\n\tcomment]]\n\tb: 2\n\n\t-- another single\n\tc: 3\n"
284 local result = to_lua(code, {
285 reserve_comment = true
286 })
287 assert.is_true(result:match("single line comment") ~= nil)
288 assert.is_true(result:match("multi") ~= nil)
289 return assert.is_true(result:match("another single") ~= nil)
290 end)
291 it("should preserve comments and empty lines in table with colon syntax", function()
292 local code = "tbl = {\n\t-- first key\n\tkey1: \"value1\"\n\n\n\t-- second key\n\tkey2: \"value2\"\n\n\t-- third key\n\tkey3: \"value3\"\n}\n"
293 local result = to_lua(code, {
294 reserve_comment = true
295 })
296 assert.is_true(result:match("first key") ~= nil)
297 assert.is_true(result:match("second key") ~= nil)
298 return assert.is_true(result:match("third key") ~= nil)
299 end)
300 it("should preserve comments in nested TableBlock structures", function()
301 local code = "outer =\n\t-- outer item\n\ta: 1\n\n\t-- inner tableblock\n\tinner:\n\t\t-- inner item 1\n\t\tx: 10\n\t\t-- inner item 2\n\t\ty: 20\n"
302 local result = to_lua(code, {
303 reserve_comment = true
304 })
305 assert.is_true(result:match("outer item") ~= nil)
306 assert.is_true(result:match("inner tableblock") ~= nil)
307 assert.is_true(result:match("inner item 1") ~= nil)
308 return assert.is_true(result:match("inner item 2") ~= nil)
309 end)
310 it("should handle function values in TableBlock with comments", function()
311 local code = "tb =\n\t-- comment before function\n\tfunc1: => print \"a\"\n\n\t-- another function\n\tfunc2: (x) => x * 2\n\n\t-- method\n\tmethod: =>\n\t\t-- inside method\n\t\tprint \"method\"\n"
312 local result = to_lua(code, {
313 reserve_comment = true
314 })
315 assert.is_true(result:match("comment before function") ~= nil)
316 assert.is_true(result:match("another function") ~= nil)
317 assert.is_true(result:match("method") ~= nil)
318 return assert.is_true(result:match("inside method") ~= nil)
319 end)
320 it("should preserve comments in TableBlock with various value types", function()
321 local code = "tb =\n\t-- string value\n\tstr: \"hello\"\n\n\t-- number value\n\tnum: 42\n\n\t-- boolean value\n\tbool: true\n\n\t-- table value\n\ttbl: {1, 2, 3}\n"
322 local result = to_lua(code, {
323 reserve_comment = true
324 })
325 assert.is_true(result:match("string value") ~= nil)
326 assert.is_true(result:match("number value") ~= nil)
327 assert.is_true(result:match("boolean value") ~= nil)
328 return assert.is_true(result:match("table value") ~= nil)
329 end)
330 it("should preserve empty lines at end of TableBlock", function()
331 local code = "tb =\n\t-- item 1\n\ta: 1\n\n\t-- item 2\n\tb: 2\n\n\n"
332 local result = to_lua(code, {
333 reserve_comment = true
334 })
335 assert.is_true(result:match("item 1") ~= nil)
336 return assert.is_true(result:match("item 2") ~= nil)
337 end)
338 it("should preserve empty lines in TableBlock between comments", function()
339 local code = "tb =\n\t-- a\n\t\n\t\n\t\n\tval: 1\n"
340 local result = to_lua(code, {
341 reserve_comment = true
342 })
343 return assert.is_true(result:match("-- %d+") ~= nil)
344 end)
345 it("should preserve empty lines in TableBlock with comments", function()
346 local code = "tb =\n\t-- first\n\t\n\t\n\tval: 1\n\t\n\t-- second\n\tval2: 2\n"
347 local result = to_lua(code, {
348 reserve_comment = true
349 })
350 assert.is_true(result:match("first") ~= nil)
351 assert.is_true(result:match("second") ~= nil)
352 return assert.is_true(result:match("-- %d+") ~= nil)
353 end)
354 it("should preserve empty lines in table literal", function()
355 local code = "t = {\n\t-- item1\n\t\n\t\n\t1,\n\t\n\t-- item2\n\t2\n}\n"
356 local result = to_lua(code, {
357 reserve_comment = true
358 })
359 assert.is_true(result:match("item1") ~= nil)
360 assert.is_true(result:match("item2") ~= nil)
361 return assert.is_true(result:match("-- %d+") ~= nil)
362 end)
363 it("should have more newlines with reserve_comment than without", function()
364 local code = "-- comment1\nx = 1\n-- comment2\ny = 2\n"
365 local result_with = to_lua(code, {
366 reserve_comment = true
367 })
368 local result_without = to_lua(code, { })
369 local newlines_with = 0
370 local newlines_without = 0
371 for _ in result_with:gmatch("\n") do
372 newlines_with = newlines_with + 1
373 end
374 for _ in result_without:gmatch("\n") do
375 newlines_without = newlines_without + 1
376 end
377 return assert.is_true(newlines_with >= newlines_without)
378 end)
379 it("should preserve empty lines in TableBlock between entries", function()
380 local code = "tb =\n\t-- key1\n\tkey1: 1\n\t\n\t\n\t-- key2\n\tkey2: 2\n"
381 local result = to_lua(code, {
382 reserve_comment = true
383 })
384 assert.is_true(result:match("key1") ~= nil)
385 assert.is_true(result:match("key2") ~= nil)
386 return assert.is_true(result:match("\t-- %d+\n") ~= nil)
387 end)
388 it("should preserve empty lines in class body", function()
389 local code = "class C\n\t-- prop1\n\tprop1: 1\n\t\n\t\n\t-- prop2\n\tprop2: 2\n"
390 local result = to_lua(code, {
391 reserve_comment = true
392 })
393 assert.is_true(result:match("prop1") ~= nil)
394 return assert.is_true(result:match("prop2") ~= nil)
395 end)
396 it("should preserve empty lines between comments in table", function()
397 local code = "t = {\n\t-- first\n\t\n\t-- second\n\t\n\t-- third\n\tval: 1\n}\n"
398 local result = to_lua(code, {
399 reserve_comment = true
400 })
401 assert.is_true(result:match("first") ~= nil)
402 assert.is_true(result:match("second") ~= nil)
403 assert.is_true(result:match("third") ~= nil)
404 return assert.is_true(result:match("-- %d+") ~= nil)
405 end)
406 it("should preserve multiple consecutive empty lines in TableBlock", function()
407 local code = "tb =\n\t-- start\n\tval1: 1\n\t\n\t\n\t\n\t-- middle\n\tval2: 2\n\t\n\t\n\t-- end\n\tval3: 3\n"
408 local result = to_lua(code, {
409 reserve_comment = true
410 })
411 assert.is_true(result:match("start") ~= nil)
412 assert.is_true(result:match("middle") ~= nil)
413 assert.is_true(result:match("end") ~= nil)
414 return assert.is_true(result:match("-- %d+") ~= nil)
415 end)
416 it("should preserve comments in table literal", function()
417 local code = "t = {\n\t-- comment\n\tkey: 1\n}\n"
418 local result = to_lua(code, {
419 reserve_comment = true
420 })
421 return assert.is_true(result:match("comment") ~= nil)
422 end)
423 it("should preserve comments in TableBlock", function()
424 local code = "t =\n\t-- comment\n\tkey: 1\n"
425 local result = to_lua(code, {
426 reserve_comment = true
427 })
428 return assert.is_true(result:match("comment") ~= nil)
429 end)
430 it("should preserve comments in class body", function()
431 local code = "class C\n\t-- comment\n\tkey: 1\n"
432 local result = to_lua(code, {
433 reserve_comment = true
434 })
435 return assert.is_true(result:match("comment") ~= nil)
436 end)
437 it("should preserve multiple comments in class body", function()
438 local code = "class C\n\t-- prop1\n\tprop1: 1\n\t-- prop2\n\tprop2: 2\n"
439 local result = to_lua(code, {
440 reserve_comment = true
441 })
442 assert.is_true(result:match("prop1") ~= nil)
443 return assert.is_true(result:match("prop2") ~= nil)
444 end)
445 it("should preserve empty lines in table literal", function()
446 local code = "t = {\n\t-- a\n\t\n\t-- b\n\tkey: 1\n}\n"
447 local result = to_lua(code, {
448 reserve_comment = true
449 })
450 assert.is_true(result:match("a") ~= nil)
451 assert.is_true(result:match("b") ~= nil)
452 return assert.is_true(result:match("-- %d+") ~= nil)
453 end)
454 it("should preserve empty lines in TableBlock", function()
455 local code = "t =\n\t-- a\n\t\n\t-- b\n\tkey: 1\n"
456 local result = to_lua(code, {
457 reserve_comment = true
458 })
459 assert.is_true(result:match("a") ~= nil)
460 assert.is_true(result:match("b") ~= nil)
461 return assert.is_true(result:match("-- %d+") ~= nil)
462 end)
463 return it("should preserve empty lines in class body", function()
464 local code = "class C\n\t-- a\n\ta: 1\n\t\n\t-- b\n\tb: 2\n"
465 local result = to_lua(code, {
466 reserve_comment = true
467 })
468 assert.is_true(result:match("a") ~= nil)
469 return assert.is_true(result:match("b") ~= nil)
470 end)
471end)