diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-02-20 13:22:58 +0000 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-02-20 13:22:58 +0000 |
| commit | 28253ef50ffa976ed70e6635e4d0ccf7f1842fcd (patch) | |
| tree | e653776d8560e30b7885b9113defe7c073db5278 /spec/inputs/test/reserve_comments_spec.yue | |
| parent | 635ce55c778eb0ccf0cb0f5d495402dbbb42deb1 (diff) | |
| parent | 1c93c88a3f79cfce68ee6d8c4cd85156aaad1101 (diff) | |
| download | yuescript-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/inputs/test/reserve_comments_spec.yue')
| -rw-r--r-- | spec/inputs/test/reserve_comments_spec.yue | 413 |
1 files changed, 413 insertions, 0 deletions
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 @@ | |||
| 1 | describe "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 | ||
| 7 | x = 1 | ||
| 8 | -- Another comment | ||
| 9 | y = 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 | ||
| 18 | x = 1 | ||
| 19 | -- Another comment | ||
| 20 | y = 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 = [[ | ||
| 28 | t = { | ||
| 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 = [[ | ||
| 41 | if 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 = [[ | ||
| 50 | func = => | ||
| 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 = [[ | ||
| 59 | while 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 = [[ | ||
| 69 | for 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 = [[ | ||
| 78 | tbl = { | ||
| 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 | ||
| 92 | x = 1 | ||
| 93 | -- Assign y | ||
| 94 | y = 2 | ||
| 95 | -- Assign z | ||
| 96 | z = 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 = [[ | ||
| 105 | t = { | ||
| 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 = [[ | ||
| 121 | outer = { | ||
| 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 = [[ | ||
| 135 | if false | ||
| 136 | print "if" | ||
| 137 | else | ||
| 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 = [[ | ||
| 146 | if false | ||
| 147 | print "if" | ||
| 148 | elseif 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 = [[ | ||
| 157 | func = => | ||
| 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 = [[ | ||
| 166 | switch 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 = [[ | ||
| 180 | with 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 | ||
| 190 | x = 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 = [[ | ||
| 200 | class 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 = [[ | ||
| 212 | class 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 | ||
| 222 | export 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 | ||
| 230 | import 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 | ||
