aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rwxr-xr-xspec/cli/cli_test_helper.sh27
-rwxr-xr-xspec/cli/run_all_tests.sh1
-rwxr-xr-xspec/cli/test_reserve_comments.sh189
-rw-r--r--spec/inputs/test/reserve_comments_spec.lua471
-rw-r--r--spec/inputs/test/reserve_comments_spec.yue413
-rw-r--r--spec/outputs/test/reserve_comments_spec.lua471
6 files changed, 1572 insertions, 0 deletions
diff --git a/spec/cli/cli_test_helper.sh b/spec/cli/cli_test_helper.sh
index ade1546..0f46969 100755
--- a/spec/cli/cli_test_helper.sh
+++ b/spec/cli/cli_test_helper.sh
@@ -123,6 +123,33 @@ assert_output_equals() {
123 fi 123 fi
124} 124}
125 125
126# Assert that output does NOT contain expected string
127assert_output_not_contains() {
128 local description="$1"
129 local unexpected="$2"
130 shift 2
131 TESTS_RUN=$((TESTS_RUN + 1))
132
133 if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then
134 if grep -qF -- "$unexpected" /tmp/test_stdout.txt || grep -qF -- "$unexpected" /tmp/test_stderr.txt; then
135 echo -e "${RED}✗${NC} $description (output contains '$unexpected')"
136 echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}"
137 echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}"
138 TESTS_FAILED=$((TESTS_FAILED + 1))
139 return 1
140 else
141 echo -e "${GREEN}✓${NC} $description"
142 TESTS_PASSED=$((TESTS_PASSED + 1))
143 return 0
144 fi
145 else
146 echo -e "${RED}✗${NC} $description (command failed)"
147 echo -e " ${YELLOW}Exit code: $?${NC}"
148 TESTS_FAILED=$((TESTS_FAILED + 1))
149 return 1
150 fi
151}
152
126# Assert file exists 153# Assert file exists
127assert_file_exists() { 154assert_file_exists() {
128 local description="$1" 155 local description="$1"
diff --git a/spec/cli/run_all_tests.sh b/spec/cli/run_all_tests.sh
index 43b74bc..c4836ca 100755
--- a/spec/cli/run_all_tests.sh
+++ b/spec/cli/run_all_tests.sh
@@ -105,6 +105,7 @@ run_test_suite() {
105# Run all test suites 105# Run all test suites
106run_test_suite "Basic Options Test" "$SCRIPT_DIR/test_basic_options.sh" 106run_test_suite "Basic Options Test" "$SCRIPT_DIR/test_basic_options.sh"
107run_test_suite "Compilation Test" "$SCRIPT_DIR/test_compilation.sh" 107run_test_suite "Compilation Test" "$SCRIPT_DIR/test_compilation.sh"
108run_test_suite "Reserve Comments Test" "$SCRIPT_DIR/test_reserve_comments.sh"
108run_test_suite "Error Handling Test" "$SCRIPT_DIR/test_error_handling.sh" 109run_test_suite "Error Handling Test" "$SCRIPT_DIR/test_error_handling.sh"
109run_test_suite "Execution Test" "$SCRIPT_DIR/test_execution.sh" 110run_test_suite "Execution Test" "$SCRIPT_DIR/test_execution.sh"
110 111
diff --git a/spec/cli/test_reserve_comments.sh b/spec/cli/test_reserve_comments.sh
new file mode 100755
index 0000000..6c0c1a7
--- /dev/null
+++ b/spec/cli/test_reserve_comments.sh
@@ -0,0 +1,189 @@
1#!/bin/bash
2# Test Reserve Comments Functionality for YueScript CLI
3# Tests: -c, --reserve-comments option
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6source "$SCRIPT_DIR/cli_test_helper.sh"
7
8# Check binary
9check_yue_binary
10
11# Setup test environment
12setup_test_env
13TMP_DIR=$(get_test_tmp_dir)
14
15echo "========================================"
16echo "Testing Reserve Comments (-c) Option"
17echo "========================================"
18echo ""
19
20# Test 1: Reserve top-level comments
21echo "Testing top-level comments preservation..."
22cat > "$TMP_DIR/top_level.yue" << 'EOF'
23-- Top level comment
24x = 1
25-- Another comment
26y = 2
27EOF
28
29assert_output_contains "Reserve top-level comments" "Top level comment" $YUE_BIN -c -p "$TMP_DIR/top_level.yue"
30assert_output_contains "Reserve second comment" "Another comment" $YUE_BIN -c -p "$TMP_DIR/top_level.yue"
31
32# Test 2: Without -c option, comments should not appear
33echo ""
34echo "Testing comments are removed without -c option..."
35assert_output_not_contains "Comments should be removed without -c" "Top level comment" $YUE_BIN -p "$TMP_DIR/top_level.yue"
36
37# Test 3: Reserve comments in table
38echo ""
39echo "Testing comments in tables..."
40cat > "$TMP_DIR/table_comments.yue" << 'EOF'
41t = {
42 -- First value comment
43 1,
44 -- Second value comment
45 2
46}
47EOF
48
49assert_output_contains "Table comments should be preserved" "First value comment" $YUE_BIN -c -p "$TMP_DIR/table_comments.yue"
50assert_output_contains "Table second comment preserved" "Second value comment" $YUE_BIN -c -p "$TMP_DIR/table_comments.yue"
51
52# Test 4: Reserve comments in if statement
53echo ""
54echo "Testing comments in if statements..."
55cat > "$TMP_DIR/if_comments.yue" << 'EOF'
56if true
57 -- Inside if block
58 print "test"
59EOF
60
61assert_output_contains "If block comments should be preserved" "Inside if block" $YUE_BIN -c -p "$TMP_DIR/if_comments.yue"
62
63# Test 5: Reserve comments in function
64echo ""
65echo "Testing comments in functions..."
66cat > "$TMP_DIR/func_comments.yue" << 'EOF'
67func = =>
68 -- Inside function
69 print "hello"
70EOF
71
72assert_output_contains "Function comments should be preserved" "Inside function" $YUE_BIN -c -p "$TMP_DIR/func_comments.yue"
73
74# Test 6: Reserve comments with empty lines
75echo ""
76echo "Testing empty lines preservation..."
77cat > "$TMP_DIR/empty_lines.yue" << 'EOF'
78-- First comment
79x = 1
80-- Second comment
81EOF
82
83OUTPUT_WITHOUT_C=$($YUE_BIN -p "$TMP_DIR/empty_lines.yue")
84OUTPUT_WITH_C=$($YUE_BIN -c -p "$TMP_DIR/empty_lines.yue")
85if [ $? -eq 0 ]; then
86 # Count newlines - with -c should have more (or equal) lines due to comment preservation
87 LINES_WITHOUT_C=$(echo "$OUTPUT_WITHOUT_C" | wc -l)
88 LINES_WITH_C=$(echo "$OUTPUT_WITH_C" | wc -l)
89 if [ $LINES_WITH_C -ge $LINES_WITHOUT_C ]; then
90 echo -e "${GREEN}✓${NC} Empty lines and comments should be preserved"
91 TESTS_PASSED=$((TESTS_PASSED + 1))
92 else
93 echo -e "${RED}✗${NC} Empty lines and comments should be preserved"
94 echo -e " ${YELLOW}Lines without -c: $LINES_WITHOUT_C, with -c: $LINES_WITH_C${NC}"
95 TESTS_FAILED=$((TESTS_FAILED + 1))
96 fi
97 TESTS_RUN=$((TESTS_RUN + 1))
98else
99 echo -e "${RED}✗${NC} Empty lines test failed"
100 TESTS_FAILED=$((TESTS_FAILED + 1))
101 TESTS_RUN=$((TESTS_RUN + 1))
102fi
103
104# Test 7: Reserve comments in table with TableBlock syntax
105echo ""
106echo "Testing comments in TableBlock..."
107cat > "$TMP_DIR/tableblock_comments.yue" << 'EOF'
108tbl = {
109 -- Key comment
110 key: "value"
111 -- Another key
112 another: 123
113}
114EOF
115
116assert_output_contains "TableBlock key comment preserved" "Key comment" $YUE_BIN -c -p "$TMP_DIR/tableblock_comments.yue"
117assert_output_contains "TableBlock second comment preserved" "Another key" $YUE_BIN -c -p "$TMP_DIR/tableblock_comments.yue"
118
119# Test 8: Reserve comments - long form option
120echo ""
121echo "Testing --reserve-comments long form option..."
122assert_output_contains "Long form option should preserve comments" "First value comment" $YUE_BIN --reserve-comments -p "$TMP_DIR/table_comments.yue"
123
124# Test 9: Compile to file with reserve comments
125echo ""
126echo "Testing compilation to file with comments..."
127cat > "$TMP_DIR/file_comment.yue" << 'EOF'
128-- This is a test
129value = 42
130EOF
131
132assert_success "Compile with -c to file" $YUE_BIN -c "$TMP_DIR/file_comment.yue" -o "$TMP_DIR/file_comment.lua"
133assert_file_exists "Output file should exist" "$TMP_DIR/file_comment.lua"
134assert_output_contains "Compiled file should contain comments" "This is a test" cat "$TMP_DIR/file_comment.lua"
135
136# Test 10: Reserve comments with multiple statements
137echo ""
138echo "Testing comments with multiple statements..."
139cat > "$TMP_DIR/multi_stmt.yue" << 'EOF'
140-- Assign x
141x = 1
142-- Assign y
143y = 2
144-- Assign z
145z = 3
146EOF
147
148OUTPUT=$($YUE_BIN -c -p "$TMP_DIR/multi_stmt.yue")
149if [ $? -eq 0 ]; then
150 if echo "$OUTPUT" | grep -q "Assign x" && echo "$OUTPUT" | grep -q "Assign y" && echo "$OUTPUT" | grep -q "Assign z"; then
151 echo -e "${GREEN}✓${NC} All comments should be preserved"
152 TESTS_PASSED=$((TESTS_PASSED + 1))
153 else
154 echo -e "${RED}✗${NC} All comments should be preserved"
155 echo -e " ${YELLOW}Output: $OUTPUT${NC}"
156 TESTS_FAILED=$((TESTS_FAILED + 1))
157 fi
158 TESTS_RUN=$((TESTS_RUN + 1))
159else
160 echo -e "${RED}✗${NC} Multiple statements test failed"
161 TESTS_FAILED=$((TESTS_FAILED + 1))
162 TESTS_RUN=$((TESTS_RUN + 1))
163fi
164
165# Test 11: Comments in while loop
166echo ""
167echo "Testing comments in while loop..."
168cat > "$TMP_DIR/while_comments.yue" << 'EOF'
169while true
170 -- Loop body comment
171 print "looping"
172 break
173EOF
174
175assert_output_contains "While loop comments preserved" "Loop body comment" $YUE_BIN -c -p "$TMP_DIR/while_comments.yue"
176
177# Test 12: Comments in for loop
178echo ""
179echo "Testing comments in for loop..."
180cat > "$TMP_DIR/for_comments.yue" << 'EOF'
181for i = 1, 3
182 -- For loop comment
183 print i
184EOF
185
186assert_output_contains "For loop comments preserved" "For loop comment" $YUE_BIN -c -p "$TMP_DIR/for_comments.yue"
187
188echo ""
189print_summary
diff --git a/spec/inputs/test/reserve_comments_spec.lua b/spec/inputs/test/reserve_comments_spec.lua
new file mode 100644
index 0000000..b4e8174
--- /dev/null
+++ b/spec/inputs/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)
diff --git a/spec/inputs/test/reserve_comments_spec.yue b/spec/inputs/test/reserve_comments_spec.yue
new file mode 100644
index 0000000..3c0b824
--- /dev/null
+++ b/spec/inputs/test/reserve_comments_spec.yue
@@ -0,0 +1,413 @@
1describe "reserve_comments option", ->
2 import to_lua from require("yue")
3
4 it "should preserve top-level comments with reserve_comment option", ->
5 code = [[
6-- Top level comment
7x = 1
8-- Another comment
9y = 2
10]]
11 result = to_lua code, {reserve_comment: true}
12 assert.is_true result\match("Top level comment") ~= nil
13 assert.is_true result\match("Another comment") ~= nil
14
15 it "should NOT preserve comments without reserve_comment option", ->
16 code = [[
17-- Top level comment
18x = 1
19-- Another comment
20y = 2
21]]
22 result = to_lua code, {}
23 assert.is_true result\match("Top level comment") == nil
24 assert.is_true result\match("Another comment") == nil
25
26 it "should preserve comments in table literals", ->
27 code = [[
28t = {
29 -- First value comment
30 1,
31 -- Second value comment
32 2
33}
34]]
35 result = to_lua code, {reserve_comment: true}
36 assert.is_true result\match("First value comment") ~= nil
37 assert.is_true result\match("Second value comment") ~= nil
38
39 it "should preserve comments in if statement", ->
40 code = [[
41if true
42 -- Inside if block
43 print "test"
44]]
45 result = to_lua code, {reserve_comment: true}
46 assert.is_true result\match("Inside if block") ~= nil
47
48 it "should preserve comments in function body", ->
49 code = [[
50func = =>
51 -- Inside function
52 print "hello"
53]]
54 result = to_lua code, {reserve_comment: true}
55 assert.is_true result\match("Inside function") ~= nil
56
57 it "should preserve comments in while loop", ->
58 code = [[
59while true
60 -- Loop body comment
61 print "looping"
62 break
63]]
64 result = to_lua code, {reserve_comment: true}
65 assert.is_true result\match("Loop body comment") ~= nil
66
67 it "should preserve comments in for loop", ->
68 code = [[
69for i = 1, 3
70 -- For loop comment
71 print i
72]]
73 result = to_lua code, {reserve_comment: true}
74 assert.is_true result\match("For loop comment") ~= nil
75
76 it "should preserve comments in TableBlock syntax", ->
77 code = [[
78tbl = {
79 -- Key comment
80 key: "value"
81 -- Another key
82 another: 123
83}
84]]
85 result = to_lua code, {reserve_comment: true}
86 assert.is_true result\match("Key comment") ~= nil
87 assert.is_true result\match("Another key") ~= nil
88
89 it "should preserve multiple comments across statements", ->
90 code = [[
91-- Assign x
92x = 1
93-- Assign y
94y = 2
95-- Assign z
96z = 3
97]]
98 result = to_lua code, {reserve_comment: true}
99 assert.is_true result\match("Assign x") ~= nil
100 assert.is_true result\match("Assign y") ~= nil
101 assert.is_true result\match("Assign z") ~= nil
102
103 it "should handle table with mixed values and comments", ->
104 code = [[
105t = {
106 -- First item
107 1,
108 -- Second item
109 2,
110 -- Third item
111 3
112}
113]]
114 result = to_lua code, {reserve_comment: true}
115 assert.is_true result\match("First item") ~= nil
116 assert.is_true result\match("Second item") ~= nil
117 assert.is_true result\match("Third item") ~= nil
118
119 it "should preserve comments in nested structures", ->
120 code = [[
121outer = {
122 -- outer comment
123 inner: {
124 -- inner comment
125 value: 42
126 }
127}
128]]
129 result = to_lua code, {reserve_comment: true}
130 assert.is_true result\match("outer comment") ~= nil
131 assert.is_true result\match("inner comment") ~= nil
132
133 it "should preserve comments in else block", ->
134 code = [[
135if false
136 print "if"
137else
138 -- else comment
139 print "else"
140]]
141 result = to_lua code, {reserve_comment: true}
142 assert.is_true result\match("else comment") ~= nil
143
144 it "should preserve comments in elseif block", ->
145 code = [[
146if false
147 print "if"
148elseif true
149 -- elseif comment
150 print "elseif"
151]]
152 result = to_lua code, {reserve_comment: true}
153 assert.is_true result\match("elseif comment") ~= nil
154
155 it "should preserve comments before return statement", ->
156 code = [[
157func = =>
158 -- before return
159 return 42
160]]
161 result = to_lua code, {reserve_comment: true}
162 assert.is_true result\match("before return") ~= nil
163
164 it "should preserve comments in switch statement", ->
165 code = [[
166switch 2
167 when 1
168 -- case 1 comment
169 print "one"
170 when 2
171 -- case 2 comment
172 print "two"
173]]
174 result = to_lua code, {reserve_comment: true}
175 assert.is_true result\match("case 1 comment") ~= nil
176 assert.is_true result\match("case 2 comment") ~= nil
177
178 it "should preserve comments in with statement", ->
179 code = [[
180with t
181 -- with body comment
182 .value = 10
183]]
184 result = to_lua code, {reserve_comment: true}
185 assert.is_true result\match("with body comment") ~= nil
186
187 it "should handle empty lines with reserve_comment", ->
188 code = [[
189-- First comment
190x = 1
191-- Second comment
192]]
193 -- Just verify it compiles without error
194 result = to_lua code, {reserve_comment: true}
195 assert.is_true result ~= nil
196 assert.is_true type(result) == "string"
197
198 it "should preserve comments in class body", ->
199 code = [[
200class MyClass
201 -- property comment
202 value: 10
203 -- method comment
204 method: => print "hello"
205]]
206 result = to_lua code, {reserve_comment: true}
207 assert.is_true result\match("property comment") ~= nil
208 assert.is_true result\match("method comment") ~= nil
209
210 it "should preserve comments in class with inheritance", ->
211 code = [[
212class Child extends Parent
213 -- child property
214 value: 100
215]]
216 result = to_lua code, {reserve_comment: true}
217 assert.is_true result\match("child property") ~= nil
218
219 it "should preserve comments in export statement", ->
220 code = [[
221-- export value comment
222export x = 42
223]]
224 result = to_lua code, {reserve_comment: true}
225 assert.is_true result\match("export value comment") ~= nil
226
227 it "should preserve comments in import statement", ->
228 code = [[
229-- import comment
230import format from "string"
231]]
232 result = to_lua code, {reserve_comment: true}
233 assert.is_true result\match("import comment") ~= nil
234
235 -- Additional tests for TableBlock syntax with multiple empty lines
236 it "should preserve empty lines between comments in TableBlock", ->
237 code = "tb =\n\t-- line\n\n\n\n\t--[[ajdjd]]\n\ta: ->\n\n\t-- line 2\n\tb: 123\n"
238 result = to_lua code, {reserve_comment: true}
239 assert.is_true result\match("line") ~= nil
240 assert.is_true result\match("ajdjd") ~= nil
241 assert.is_true result\match("line 2") ~= nil
242
243 it "should preserve block comments in TableBlock", ->
244 code = "tb =\n\t--[[block comment]]\n\ta: 1\n\n\t--[[another block]]\n\tb: 2\n"
245 result = to_lua code, {reserve_comment: true}
246 assert.is_true result\match("block comment") ~= nil
247 assert.is_true result\match("another block") ~= nil
248
249 it "should preserve multiple empty lines in table literal", ->
250 code = "tb = {\n\t-- line\n\n\n\n\t--[[ajdjd]]\n\ta: ->\n\n\t-- line 2\n\tb: 123\n}\n"
251 result = to_lua code, {reserve_comment: true}
252 assert.is_true result\match("line") ~= nil
253 assert.is_true result\match("ajdjd") ~= nil
254 assert.is_true result\match("line 2") ~= nil
255
256 it "should preserve mixed single and block comments in TableBlock", ->
257 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"
258 result = to_lua code, {reserve_comment: true}
259 assert.is_true result\match("single line comment") ~= nil
260 assert.is_true result\match("multi") ~= nil
261 assert.is_true result\match("another single") ~= nil
262
263 it "should preserve comments and empty lines in table with colon syntax", ->
264 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"
265 result = to_lua code, {reserve_comment: true}
266 assert.is_true result\match("first key") ~= nil
267 assert.is_true result\match("second key") ~= nil
268 assert.is_true result\match("third key") ~= nil
269
270 it "should preserve comments in nested TableBlock structures", ->
271 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"
272 result = to_lua code, {reserve_comment: true}
273 assert.is_true result\match("outer item") ~= nil
274 assert.is_true result\match("inner tableblock") ~= nil
275 assert.is_true result\match("inner item 1") ~= nil
276 assert.is_true result\match("inner item 2") ~= nil
277
278 it "should handle function values in TableBlock with comments", ->
279 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"
280 result = to_lua code, {reserve_comment: true}
281 assert.is_true result\match("comment before function") ~= nil
282 assert.is_true result\match("another function") ~= nil
283 assert.is_true result\match("method") ~= nil
284 assert.is_true result\match("inside method") ~= nil
285
286 it "should preserve comments in TableBlock with various value types", ->
287 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"
288 result = to_lua code, {reserve_comment: true}
289 assert.is_true result\match("string value") ~= nil
290 assert.is_true result\match("number value") ~= nil
291 assert.is_true result\match("boolean value") ~= nil
292 assert.is_true result\match("table value") ~= nil
293
294 it "should preserve empty lines at end of TableBlock", ->
295 code = "tb =\n\t-- item 1\n\ta: 1\n\n\t-- item 2\n\tb: 2\n\n\n"
296 result = to_lua code, {reserve_comment: true}
297 assert.is_true result\match("item 1") ~= nil
298 assert.is_true result\match("item 2") ~= nil
299
300 -- Tests specifically for empty lines between comments
301 it "should preserve empty lines in TableBlock between comments", ->
302 code = "tb =\n\t-- a\n\t\n\t\n\t\n\tval: 1\n"
303 result = to_lua code, {reserve_comment: true}
304 -- Empty lines should produce line number comments in output
305 -- Check that there's a line with just a comment marker (line number)
306 assert.is_true result\match("-- %d+") ~= nil
307
308 it "should preserve empty lines in TableBlock with comments", ->
309 code = "tb =\n\t-- first\n\t\n\t\n\tval: 1\n\t\n\t-- second\n\tval2: 2\n"
310 result = to_lua code, {reserve_comment: true}
311 assert.is_true result\match("first") ~= nil
312 assert.is_true result\match("second") ~= nil
313 -- Should have empty line representations (lines with line number comments)
314 assert.is_true result\match("-- %d+") ~= nil
315
316 it "should preserve empty lines in table literal", ->
317 code = "t = {\n\t-- item1\n\t\n\t\n\t1,\n\t\n\t-- item2\n\t2\n}\n"
318 result = to_lua code, {reserve_comment: true}
319 assert.is_true result\match("item1") ~= nil
320 assert.is_true result\match("item2") ~= nil
321 -- Empty lines should produce line comments
322 assert.is_true result\match("-- %d+") ~= nil
323
324 it "should have more newlines with reserve_comment than without", ->
325 code = "-- comment1\nx = 1\n-- comment2\ny = 2\n"
326 result_with = to_lua code, {reserve_comment: true}
327 result_without = to_lua code, {}
328 -- Count newlines in both results using gmatch
329 newlines_with = 0
330 newlines_without = 0
331 for _ in result_with\gmatch("\n")
332 newlines_with += 1
333 for _ in result_without\gmatch("\n")
334 newlines_without += 1
335 -- With reserve_comment should have equal or more newlines
336 assert.is_true newlines_with >= newlines_without
337
338 it "should preserve empty lines in TableBlock between entries", ->
339 code = "tb =\n\t-- key1\n\tkey1: 1\n\t\n\t\n\t-- key2\n\tkey2: 2\n"
340 result = to_lua code, {reserve_comment: true}
341 assert.is_true result\match("key1") ~= nil
342 assert.is_true result\match("key2") ~= nil
343 -- Empty lines should produce lines with line number comments
344 assert.is_true result\match("\t-- %d+\n") ~= nil
345
346 it "should preserve empty lines in class body", ->
347 code = "class C\n\t-- prop1\n\tprop1: 1\n\t\n\t\n\t-- prop2\n\tprop2: 2\n"
348 result = to_lua code, {reserve_comment: true}
349 assert.is_true result\match("prop1") ~= nil
350 assert.is_true result\match("prop2") ~= nil
351
352 it "should preserve empty lines between comments in table", ->
353 code = "t = {\n\t-- first\n\t\n\t-- second\n\t\n\t-- third\n\tval: 1\n}\n"
354 result = to_lua code, {reserve_comment: true}
355 assert.is_true result\match("first") ~= nil
356 assert.is_true result\match("second") ~= nil
357 assert.is_true result\match("third") ~= nil
358 -- Empty lines should produce line comments
359 assert.is_true result\match("-- %d+") ~= nil
360
361 it "should preserve multiple consecutive empty lines in TableBlock", ->
362 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"
363 result = to_lua code, {reserve_comment: true}
364 assert.is_true result\match("start") ~= nil
365 assert.is_true result\match("middle") ~= nil
366 assert.is_true result\match("end") ~= nil
367 -- Should have line number comments for empty lines
368 assert.is_true result\match("-- %d+") ~= nil
369
370 -- Comparison tests: Table literal vs TableBlock vs Class
371 it "should preserve comments in table literal", ->
372 code = "t = {\n\t-- comment\n\tkey: 1\n}\n"
373 result = to_lua code, {reserve_comment: true}
374 assert.is_true result\match("comment") ~= nil
375
376 it "should preserve comments in TableBlock", ->
377 code = "t =\n\t-- comment\n\tkey: 1\n"
378 result = to_lua code, {reserve_comment: true}
379 assert.is_true result\match("comment") ~= nil
380
381 it "should preserve comments in class body", ->
382 code = "class C\n\t-- comment\n\tkey: 1\n"
383 result = to_lua code, {reserve_comment: true}
384 assert.is_true result\match("comment") ~= nil
385
386 it "should preserve multiple comments in class body", ->
387 code = "class C\n\t-- prop1\n\tprop1: 1\n\t-- prop2\n\tprop2: 2\n"
388 result = to_lua code, {reserve_comment: true}
389 assert.is_true result\match("prop1") ~= nil
390 assert.is_true result\match("prop2") ~= nil
391
392 it "should preserve empty lines in table literal", ->
393 code = "t = {\n\t-- a\n\t\n\t-- b\n\tkey: 1\n}\n"
394 result = to_lua code, {reserve_comment: true}
395 assert.is_true result\match("a") ~= nil
396 assert.is_true result\match("b") ~= nil
397 -- Empty lines produce line comments
398 assert.is_true result\match("-- %d+") ~= nil
399
400 it "should preserve empty lines in TableBlock", ->
401 code = "t =\n\t-- a\n\t\n\t-- b\n\tkey: 1\n"
402 result = to_lua code, {reserve_comment: true}
403 assert.is_true result\match("a") ~= nil
404 assert.is_true result\match("b") ~= nil
405 -- Empty lines produce line comments
406 assert.is_true result\match("-- %d+") ~= nil
407
408 it "should preserve empty lines in class body", ->
409 code = "class C\n\t-- a\n\ta: 1\n\t\n\t-- b\n\tb: 2\n"
410 result = to_lua code, {reserve_comment: true}
411 assert.is_true result\match("a") ~= nil
412 assert.is_true result\match("b") ~= nil
413 -- Empty lines in class should also be preserved
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)