diff options
Diffstat (limited to 'spec/outputs/test')
| -rw-r--r-- | spec/outputs/test/break_multiple_values_spec.lua | 1697 |
1 files changed, 1697 insertions, 0 deletions
diff --git a/spec/outputs/test/break_multiple_values_spec.lua b/spec/outputs/test/break_multiple_values_spec.lua new file mode 100644 index 0000000..ba14a64 --- /dev/null +++ b/spec/outputs/test/break_multiple_values_spec.lua | |||
| @@ -0,0 +1,1697 @@ | |||
| 1 | local _anon_func_0 = function(_with_0) | ||
| 2 | local _with_1 = nil | ||
| 3 | local _val_0 | ||
| 4 | repeat | ||
| 5 | if _with_1 ~= nil then | ||
| 6 | _val_0 = _with_1.value | ||
| 7 | break | ||
| 8 | end | ||
| 9 | until true | ||
| 10 | return _val_0 | ||
| 11 | end | ||
| 12 | local _anon_func_1 = function() | ||
| 13 | local _val_0, _val_1 | ||
| 14 | for i = 1, 10 do | ||
| 15 | if i == 5 then | ||
| 16 | _val_0, _val_1 = i, i * 10 | ||
| 17 | break | ||
| 18 | end | ||
| 19 | end | ||
| 20 | return _val_0, _val_1 | ||
| 21 | end | ||
| 22 | local _anon_func_2 = function() | ||
| 23 | local _val_0, _val_1 | ||
| 24 | for i = 1, 5 do | ||
| 25 | if i == 3 then | ||
| 26 | _val_0, _val_1 = i, i * 10 | ||
| 27 | break | ||
| 28 | end | ||
| 29 | end | ||
| 30 | return _val_0, _val_1 | ||
| 31 | end | ||
| 32 | local _anon_func_3 = function(self) | ||
| 33 | local _val_0, _val_1 | ||
| 34 | for i = 1, 10 do | ||
| 35 | if i == 4 then | ||
| 36 | _val_0, _val_1 = self.start + i, self.end_val + i | ||
| 37 | break | ||
| 38 | end | ||
| 39 | end | ||
| 40 | return _val_0, _val_1 | ||
| 41 | end | ||
| 42 | local _anon_func_4 = function() | ||
| 43 | local _val_0, _val_1 | ||
| 44 | for i = 1, 5 do | ||
| 45 | do | ||
| 46 | if i == 3 then | ||
| 47 | _val_0, _val_1 = i, i * 2 | ||
| 48 | break | ||
| 49 | end | ||
| 50 | end | ||
| 51 | end | ||
| 52 | return _val_0, _val_1 | ||
| 53 | end | ||
| 54 | return describe("break with multiple values", function() | ||
| 55 | it("should break with multiple values from numeric for loop", function() | ||
| 56 | local x, y | ||
| 57 | for i = 1, 10 do | ||
| 58 | if i > 5 then | ||
| 59 | x, y = i, i * 2 | ||
| 60 | break | ||
| 61 | end | ||
| 62 | end | ||
| 63 | assert.same(x, 6) | ||
| 64 | return assert.same(y, 12) | ||
| 65 | end) | ||
| 66 | it("should break with multiple values from ipairs iterator", function() | ||
| 67 | local x, y | ||
| 68 | for _, v in ipairs({ | ||
| 69 | 1, | ||
| 70 | 2, | ||
| 71 | 3 | ||
| 72 | }) do | ||
| 73 | if v > 2 then | ||
| 74 | x, y = v, v * 2 | ||
| 75 | break | ||
| 76 | end | ||
| 77 | end | ||
| 78 | assert.same(x, 3) | ||
| 79 | return assert.same(y, 6) | ||
| 80 | end) | ||
| 81 | it("should break with multiple values from destructuring iterator", function() | ||
| 82 | local x, y | ||
| 83 | local _list_0 = { | ||
| 84 | { | ||
| 85 | 10, | ||
| 86 | 20 | ||
| 87 | }, | ||
| 88 | { | ||
| 89 | 30, | ||
| 90 | 40 | ||
| 91 | }, | ||
| 92 | { | ||
| 93 | 40, | ||
| 94 | 50 | ||
| 95 | } | ||
| 96 | } | ||
| 97 | for _index_0 = 1, #_list_0 do | ||
| 98 | local _des_0 = _list_0[_index_0] | ||
| 99 | local a, b = _des_0[1], _des_0[2] | ||
| 100 | if b > 20 then | ||
| 101 | x, y = a, b * 2 | ||
| 102 | break | ||
| 103 | end | ||
| 104 | end | ||
| 105 | assert.same(x, 30) | ||
| 106 | return assert.same(y, 80) | ||
| 107 | end) | ||
| 108 | it("should break with multiple values from do block", function() | ||
| 109 | local x, y | ||
| 110 | do | ||
| 111 | repeat | ||
| 112 | x, y = 1, 2 | ||
| 113 | break | ||
| 114 | until true | ||
| 115 | end | ||
| 116 | assert.same(x, 1) | ||
| 117 | return assert.same(y, 2) | ||
| 118 | end) | ||
| 119 | it("should break with multiple values from with statement", function() | ||
| 120 | local x | ||
| 121 | do | ||
| 122 | local _with_0 = { | ||
| 123 | abc = 99, | ||
| 124 | flag = true | ||
| 125 | } | ||
| 126 | repeat | ||
| 127 | if _with_0.flag then | ||
| 128 | x = _with_0.abc | ||
| 129 | break | ||
| 130 | end | ||
| 131 | x = 0 | ||
| 132 | break | ||
| 133 | until true | ||
| 134 | end | ||
| 135 | return assert.same(x, 99) | ||
| 136 | end) | ||
| 137 | it("should work with continue in comprehension", function() | ||
| 138 | local input = { | ||
| 139 | 1, | ||
| 140 | 2, | ||
| 141 | 3, | ||
| 142 | 4, | ||
| 143 | 5, | ||
| 144 | 6 | ||
| 145 | } | ||
| 146 | local output | ||
| 147 | do | ||
| 148 | local _accum_0 = { } | ||
| 149 | local _len_0 = 1 | ||
| 150 | for _index_0 = 1, #input do | ||
| 151 | local x = input[_index_0] | ||
| 152 | if x % 2 == 1 then | ||
| 153 | goto _continue_0 | ||
| 154 | end | ||
| 155 | _accum_0[_len_0] = x | ||
| 156 | _len_0 = _len_0 + 1 | ||
| 157 | ::_continue_0:: | ||
| 158 | end | ||
| 159 | output = _accum_0 | ||
| 160 | end | ||
| 161 | return assert.same(output, { | ||
| 162 | 2, | ||
| 163 | 4, | ||
| 164 | 6 | ||
| 165 | }) | ||
| 166 | end) | ||
| 167 | it("should break in comprehension with index", function() | ||
| 168 | local f1 | ||
| 169 | f1 = function() | ||
| 170 | local tb = { | ||
| 171 | true, | ||
| 172 | true, | ||
| 173 | false | ||
| 174 | } | ||
| 175 | local index | ||
| 176 | for i = 1, #tb do | ||
| 177 | if tb[i] then | ||
| 178 | index = i | ||
| 179 | break | ||
| 180 | end | ||
| 181 | end | ||
| 182 | return index | ||
| 183 | end | ||
| 184 | return assert.same(f1(), 1) | ||
| 185 | end) | ||
| 186 | it("should work with continue in indexed comprehension", function() | ||
| 187 | local input = { | ||
| 188 | 1, | ||
| 189 | 2, | ||
| 190 | 3, | ||
| 191 | 4, | ||
| 192 | 5, | ||
| 193 | 6 | ||
| 194 | } | ||
| 195 | local output | ||
| 196 | do | ||
| 197 | local _accum_0 = { } | ||
| 198 | local _len_0 = 1 | ||
| 199 | for i = 1, #input do | ||
| 200 | local x = input[i] | ||
| 201 | if x % 2 == 1 then | ||
| 202 | goto _continue_0 | ||
| 203 | end | ||
| 204 | _accum_0[_len_0] = x | ||
| 205 | _len_0 = _len_0 + 1 | ||
| 206 | ::_continue_0:: | ||
| 207 | end | ||
| 208 | output = _accum_0 | ||
| 209 | end | ||
| 210 | return assert.same(output, { | ||
| 211 | 2, | ||
| 212 | 4, | ||
| 213 | 6 | ||
| 214 | }) | ||
| 215 | end) | ||
| 216 | it("should break with multiple values in simple for loop", function() | ||
| 217 | local a, b | ||
| 218 | for i = 1, 5 do | ||
| 219 | if i == 2 then | ||
| 220 | a, b = i, i * 10 | ||
| 221 | break | ||
| 222 | end | ||
| 223 | end | ||
| 224 | assert.same(a, 2) | ||
| 225 | return assert.same(b, 20) | ||
| 226 | end) | ||
| 227 | it("should break with expression values", function() | ||
| 228 | local sum, product | ||
| 229 | for i = 1, 3 do | ||
| 230 | if i == 2 then | ||
| 231 | sum, product = i + i, i * i | ||
| 232 | break | ||
| 233 | end | ||
| 234 | end | ||
| 235 | assert.same(sum, 4) | ||
| 236 | return assert.same(product, 4) | ||
| 237 | end) | ||
| 238 | it("should break with table iteration values", function() | ||
| 239 | local key, value | ||
| 240 | for k, v in pairs({ | ||
| 241 | a = 1, | ||
| 242 | b = 2, | ||
| 243 | c = 3 | ||
| 244 | }) do | ||
| 245 | if k == "b" then | ||
| 246 | key, value = k, v * 10 | ||
| 247 | break | ||
| 248 | end | ||
| 249 | end | ||
| 250 | assert.same(key, "b") | ||
| 251 | return assert.same(value, 20) | ||
| 252 | end) | ||
| 253 | it("should break in for loop with multiple values", function() | ||
| 254 | local count, total | ||
| 255 | for i = 1, 10 do | ||
| 256 | if i == 5 then | ||
| 257 | count, total = i, i * 10 | ||
| 258 | break | ||
| 259 | end | ||
| 260 | end | ||
| 261 | assert.same(count, 5) | ||
| 262 | return assert.same(total, 50) | ||
| 263 | end) | ||
| 264 | it("should break in repeat loop with multiple values", function() | ||
| 265 | local i = 1 | ||
| 266 | local doubled | ||
| 267 | do | ||
| 268 | local _val_0, _val_1 | ||
| 269 | repeat | ||
| 270 | if i > 3 then | ||
| 271 | _val_0, _val_1 = i, i * 100 | ||
| 272 | break | ||
| 273 | end | ||
| 274 | i = i + 1 | ||
| 275 | until false | ||
| 276 | i, doubled = _val_0, _val_1 | ||
| 277 | end | ||
| 278 | assert.same(i, 4) | ||
| 279 | return assert.same(doubled, 400) | ||
| 280 | end) | ||
| 281 | it("should work with with? and break", function() | ||
| 282 | local dummy | ||
| 283 | dummy = function(x) | ||
| 284 | return x | ||
| 285 | end | ||
| 286 | local result | ||
| 287 | do | ||
| 288 | local _with_0 = { | ||
| 289 | value = 1 | ||
| 290 | } | ||
| 291 | repeat | ||
| 292 | if _with_0 ~= nil then | ||
| 293 | dummy(_anon_func_0(_with_0)) | ||
| 294 | result = _with_0.value | ||
| 295 | break | ||
| 296 | end | ||
| 297 | until true | ||
| 298 | end | ||
| 299 | return assert.same(result, 1) | ||
| 300 | end) | ||
| 301 | it("should break with multiple values in do block", function() | ||
| 302 | local x, y | ||
| 303 | do | ||
| 304 | repeat | ||
| 305 | x, y = 10, 30 | ||
| 306 | break | ||
| 307 | until true | ||
| 308 | end | ||
| 309 | assert.same(x, 10) | ||
| 310 | return assert.same(y, 30) | ||
| 311 | end) | ||
| 312 | it("should break with string and number values", function() | ||
| 313 | local name, age | ||
| 314 | for k, v in pairs({ | ||
| 315 | name = "Alice", | ||
| 316 | age = 30, | ||
| 317 | city = "NYC" | ||
| 318 | }) do | ||
| 319 | if k == "name" then | ||
| 320 | name, age = v, 25 | ||
| 321 | break | ||
| 322 | end | ||
| 323 | end | ||
| 324 | assert.same(name, "Alice") | ||
| 325 | return assert.same(age, 25) | ||
| 326 | end) | ||
| 327 | it("should break with nil values", function() | ||
| 328 | local a, b, c | ||
| 329 | for i = 1, 5 do | ||
| 330 | if i == 3 then | ||
| 331 | a, b, c = i, nil, "test" | ||
| 332 | break | ||
| 333 | end | ||
| 334 | end | ||
| 335 | assert.same(a, 3) | ||
| 336 | assert.same(b, nil) | ||
| 337 | return assert.same(c, "test") | ||
| 338 | end) | ||
| 339 | it("should break with boolean values", function() | ||
| 340 | local found, value | ||
| 341 | for i = 1, 5 do | ||
| 342 | if i == 3 then | ||
| 343 | found, value = true, "found" | ||
| 344 | break | ||
| 345 | end | ||
| 346 | end | ||
| 347 | assert.is_true(found) | ||
| 348 | return assert.same(value, "found") | ||
| 349 | end) | ||
| 350 | it("should break with function call values", function() | ||
| 351 | local fn | ||
| 352 | fn = function() | ||
| 353 | return 42 | ||
| 354 | end | ||
| 355 | local val, dbl | ||
| 356 | for i = 1, 3 do | ||
| 357 | if i == 2 then | ||
| 358 | val, dbl = fn(), fn() * 2 | ||
| 359 | break | ||
| 360 | end | ||
| 361 | end | ||
| 362 | assert.same(val, 42) | ||
| 363 | return assert.same(dbl, 84) | ||
| 364 | end) | ||
| 365 | it("should break with multiple values for destructuring", function() | ||
| 366 | local x, y | ||
| 367 | local _list_0 = { | ||
| 368 | { | ||
| 369 | 1, | ||
| 370 | 2 | ||
| 371 | }, | ||
| 372 | { | ||
| 373 | 3, | ||
| 374 | 4 | ||
| 375 | }, | ||
| 376 | { | ||
| 377 | 5, | ||
| 378 | 6 | ||
| 379 | } | ||
| 380 | } | ||
| 381 | for _index_0 = 1, #_list_0 do | ||
| 382 | local _des_0 = _list_0[_index_0] | ||
| 383 | local a, b = _des_0[1], _des_0[2] | ||
| 384 | if a > 2 then | ||
| 385 | x, y = b, a | ||
| 386 | break | ||
| 387 | end | ||
| 388 | end | ||
| 389 | assert.same(x, 4) | ||
| 390 | return assert.same(y, 3) | ||
| 391 | end) | ||
| 392 | it("should break in switch-like logic", function() | ||
| 393 | local status, message | ||
| 394 | for i = 1, 3 do | ||
| 395 | do | ||
| 396 | if i == 1 then | ||
| 397 | status, message = "ok", "one" | ||
| 398 | break | ||
| 399 | end | ||
| 400 | if i == 2 then | ||
| 401 | status, message = "ok", "two" | ||
| 402 | break | ||
| 403 | end | ||
| 404 | end | ||
| 405 | end | ||
| 406 | assert.same(status, "ok") | ||
| 407 | return assert.same(message, "one") | ||
| 408 | end) | ||
| 409 | it("should handle multiple break conditions", function() | ||
| 410 | local idx, val | ||
| 411 | for i = 1, 10 do | ||
| 412 | if i == 3 then | ||
| 413 | idx, val = i, "first" | ||
| 414 | break | ||
| 415 | end | ||
| 416 | if i == 7 then | ||
| 417 | idx, val = i, "second" | ||
| 418 | break | ||
| 419 | end | ||
| 420 | end | ||
| 421 | assert.same(idx, 3) | ||
| 422 | return assert.same(val, "first") | ||
| 423 | end) | ||
| 424 | it("should break with computed values", function() | ||
| 425 | local sum, count | ||
| 426 | for i = 1, 5 do | ||
| 427 | if i == 4 then | ||
| 428 | sum, count = 10 + i, i | ||
| 429 | break | ||
| 430 | end | ||
| 431 | end | ||
| 432 | assert.same(sum, 14) | ||
| 433 | return assert.same(count, 4) | ||
| 434 | end) | ||
| 435 | it("should break with table literal", function() | ||
| 436 | local name, data | ||
| 437 | for i = 1, 3 do | ||
| 438 | if i == 2 then | ||
| 439 | name, data = "item", { | ||
| 440 | value = i, | ||
| 441 | index = i | ||
| 442 | } | ||
| 443 | break | ||
| 444 | end | ||
| 445 | end | ||
| 446 | assert.same(name, "item") | ||
| 447 | assert.same(data.value, 2) | ||
| 448 | return assert.same(data.index, 2) | ||
| 449 | end) | ||
| 450 | it("should break with string concatenation", function() | ||
| 451 | local prefix, suffix | ||
| 452 | for i = 1, 3 do | ||
| 453 | if i == 2 then | ||
| 454 | prefix, suffix = "pre", "fix" | ||
| 455 | break | ||
| 456 | end | ||
| 457 | end | ||
| 458 | return assert.same(prefix .. suffix, "prefix") | ||
| 459 | end) | ||
| 460 | it("should break with continue in loop", function() | ||
| 461 | local x, y | ||
| 462 | for i = 1, 5 do | ||
| 463 | if i == 3 then | ||
| 464 | x, y = i, i * 2 | ||
| 465 | break | ||
| 466 | end | ||
| 467 | end | ||
| 468 | assert.same(x, 3) | ||
| 469 | return assert.same(y, 6) | ||
| 470 | end) | ||
| 471 | it("should break with ternary-like expression", function() | ||
| 472 | local result, is_valid | ||
| 473 | for i = 1, 5 do | ||
| 474 | if i == 3 then | ||
| 475 | result, is_valid = i, i > 2 | ||
| 476 | break | ||
| 477 | end | ||
| 478 | end | ||
| 479 | assert.same(result, 3) | ||
| 480 | return assert.is_true(is_valid) | ||
| 481 | end) | ||
| 482 | it("should break with arithmetic operations", function() | ||
| 483 | local sum, diff | ||
| 484 | for i = 1, 5 do | ||
| 485 | if i == 3 then | ||
| 486 | sum, diff = i + 10, 10 - i | ||
| 487 | break | ||
| 488 | end | ||
| 489 | end | ||
| 490 | assert.same(sum, 13) | ||
| 491 | return assert.same(diff, 7) | ||
| 492 | end) | ||
| 493 | it("should break with table access", function() | ||
| 494 | local data = { | ||
| 495 | a = 1, | ||
| 496 | b = 2 | ||
| 497 | } | ||
| 498 | local first, second | ||
| 499 | for i = 1, 3 do | ||
| 500 | if i == 2 then | ||
| 501 | first, second = data.a, data.b | ||
| 502 | break | ||
| 503 | end | ||
| 504 | end | ||
| 505 | assert.same(first, 1) | ||
| 506 | return assert.same(second, 2) | ||
| 507 | end) | ||
| 508 | it("should break with literal values", function() | ||
| 509 | local a, b | ||
| 510 | for i = 1, 3 do | ||
| 511 | if i == 2 then | ||
| 512 | a, b = "x", "y" | ||
| 513 | break | ||
| 514 | end | ||
| 515 | end | ||
| 516 | assert.same(a, "x") | ||
| 517 | return assert.same(b, "y") | ||
| 518 | end) | ||
| 519 | it("should break with conditional expression", function() | ||
| 520 | local status, msg | ||
| 521 | for i = 1, 5 do | ||
| 522 | if i == 3 then | ||
| 523 | status, msg = "ok", "done" | ||
| 524 | break | ||
| 525 | end | ||
| 526 | end | ||
| 527 | assert.same(status, "ok") | ||
| 528 | return assert.same(msg, "done") | ||
| 529 | end) | ||
| 530 | it("should handle multiple breaks in sequence", function() | ||
| 531 | local val1, val2 | ||
| 532 | for i = 1, 10 do | ||
| 533 | if i == 3 then | ||
| 534 | val1, val2 = i, i * 2 | ||
| 535 | break | ||
| 536 | end | ||
| 537 | if i == 5 then | ||
| 538 | val1, val2 = i, i * 3 | ||
| 539 | break | ||
| 540 | end | ||
| 541 | if i == 7 then | ||
| 542 | val1, val2 = i, i * 4 | ||
| 543 | break | ||
| 544 | end | ||
| 545 | end | ||
| 546 | assert.same(val1, 3) | ||
| 547 | return assert.same(val2, 6) | ||
| 548 | end) | ||
| 549 | it("should handle multiple breaks in switch", function() | ||
| 550 | local val1, val2 | ||
| 551 | for i = 1, 10 do | ||
| 552 | if 3 == i then | ||
| 553 | val1, val2 = i, i * 2 | ||
| 554 | break | ||
| 555 | elseif 5 == i then | ||
| 556 | val1, val2 = i, i * 3 | ||
| 557 | break | ||
| 558 | elseif 7 == i then | ||
| 559 | val1, val2 = i, i * 4 | ||
| 560 | break | ||
| 561 | end | ||
| 562 | end | ||
| 563 | assert.same(val1, 3) | ||
| 564 | return assert.same(val2, 6) | ||
| 565 | end) | ||
| 566 | it("should break with nil and default", function() | ||
| 567 | local a, b | ||
| 568 | for i = 1, 5 do | ||
| 569 | if i == 2 then | ||
| 570 | a, b = i, nil | ||
| 571 | break | ||
| 572 | end | ||
| 573 | end | ||
| 574 | assert.same(a, 2) | ||
| 575 | return assert.is_nil(b) | ||
| 576 | end) | ||
| 577 | it("should break with false and value", function() | ||
| 578 | local found, data | ||
| 579 | for i = 1, 5 do | ||
| 580 | if i == 3 then | ||
| 581 | found, data = false, "item" | ||
| 582 | break | ||
| 583 | end | ||
| 584 | end | ||
| 585 | assert.is_false(found) | ||
| 586 | return assert.same(data, "item") | ||
| 587 | end) | ||
| 588 | it("should break with three values", function() | ||
| 589 | local a, b, c | ||
| 590 | for i = 1, 5 do | ||
| 591 | if i == 3 then | ||
| 592 | a, b, c = i, i * 2, i * 3 | ||
| 593 | break | ||
| 594 | end | ||
| 595 | end | ||
| 596 | assert.same(a, 3) | ||
| 597 | assert.same(b, 6) | ||
| 598 | return assert.same(c, 9) | ||
| 599 | end) | ||
| 600 | it("should break in while loop", function() | ||
| 601 | local i = 1 | ||
| 602 | local count, total | ||
| 603 | do | ||
| 604 | local _val_0, _val_1 | ||
| 605 | while i < 10 do | ||
| 606 | if i == 5 then | ||
| 607 | _val_0, _val_1 = i, i * 10 | ||
| 608 | break | ||
| 609 | end | ||
| 610 | i = i + 1 | ||
| 611 | end | ||
| 612 | count, total = _val_0, _val_1 | ||
| 613 | end | ||
| 614 | assert.same(count, 5) | ||
| 615 | return assert.same(total, 50) | ||
| 616 | end) | ||
| 617 | it("should handle early break in loop", function() | ||
| 618 | local a, b | ||
| 619 | for i = 1, 100 do | ||
| 620 | if i == 1 then | ||
| 621 | a, b = "first", "second" | ||
| 622 | break | ||
| 623 | end | ||
| 624 | end | ||
| 625 | assert.same(a, "first") | ||
| 626 | return assert.same(b, "second") | ||
| 627 | end) | ||
| 628 | it("should break with multiple function calls", function() | ||
| 629 | local add | ||
| 630 | add = function(a, b) | ||
| 631 | return a + b | ||
| 632 | end | ||
| 633 | local mul | ||
| 634 | mul = function(a, b) | ||
| 635 | return a * b | ||
| 636 | end | ||
| 637 | local sum, product | ||
| 638 | for i = 1, 5 do | ||
| 639 | if i == 3 then | ||
| 640 | sum, product = add(i, i), mul(i, i) | ||
| 641 | break | ||
| 642 | end | ||
| 643 | end | ||
| 644 | assert.same(sum, 6) | ||
| 645 | return assert.same(product, 9) | ||
| 646 | end) | ||
| 647 | it("should work with chained comparisons", function() | ||
| 648 | local x, y | ||
| 649 | for i = 1, 10 do | ||
| 650 | if 3 < i and i < 7 then | ||
| 651 | x, y = i, i * i | ||
| 652 | break | ||
| 653 | end | ||
| 654 | end | ||
| 655 | assert.same(x, 4) | ||
| 656 | return assert.same(y, 16) | ||
| 657 | end) | ||
| 658 | it("should work with logical expressions", function() | ||
| 659 | local flag, value | ||
| 660 | for i = 1, 5 do | ||
| 661 | if i > 2 and i < 4 then | ||
| 662 | flag, value = true, i | ||
| 663 | break | ||
| 664 | end | ||
| 665 | end | ||
| 666 | assert.is_true(flag) | ||
| 667 | return assert.same(value, 3) | ||
| 668 | end) | ||
| 669 | it("should work with table unpacking in break", function() | ||
| 670 | local tbl = { | ||
| 671 | 10, | ||
| 672 | 20 | ||
| 673 | } | ||
| 674 | local a, b | ||
| 675 | for i = 1, 5 do | ||
| 676 | if i == 3 then | ||
| 677 | a, b = tbl[1], tbl[2] | ||
| 678 | break | ||
| 679 | end | ||
| 680 | end | ||
| 681 | assert.same(a, 10) | ||
| 682 | return assert.same(b, 20) | ||
| 683 | end) | ||
| 684 | it("should work with nested conditions", function() | ||
| 685 | local result, flag | ||
| 686 | for i = 1, 10 do | ||
| 687 | if i > 2 then | ||
| 688 | if i < 5 then | ||
| 689 | if i == 3 then | ||
| 690 | result, flag = i, true | ||
| 691 | break | ||
| 692 | end | ||
| 693 | end | ||
| 694 | end | ||
| 695 | end | ||
| 696 | assert.same(result, 3) | ||
| 697 | return assert.is_true(flag) | ||
| 698 | end) | ||
| 699 | it("should work with class methods", function() | ||
| 700 | local MyClass | ||
| 701 | do | ||
| 702 | local _class_0 | ||
| 703 | local _base_0 = { | ||
| 704 | find_pair = function(self) | ||
| 705 | local result = { | ||
| 706 | _anon_func_1() | ||
| 707 | } | ||
| 708 | return result | ||
| 709 | end | ||
| 710 | } | ||
| 711 | if _base_0.__index == nil then | ||
| 712 | _base_0.__index = _base_0 | ||
| 713 | end | ||
| 714 | _class_0 = setmetatable({ | ||
| 715 | __init = function() end, | ||
| 716 | __base = _base_0, | ||
| 717 | __name = "MyClass" | ||
| 718 | }, { | ||
| 719 | __index = _base_0, | ||
| 720 | __call = function(cls, ...) | ||
| 721 | local _self_0 = setmetatable({ }, _base_0) | ||
| 722 | cls.__init(_self_0, ...) | ||
| 723 | return _self_0 | ||
| 724 | end | ||
| 725 | }) | ||
| 726 | _base_0.__class = _class_0 | ||
| 727 | MyClass = _class_0 | ||
| 728 | end | ||
| 729 | local obj = MyClass() | ||
| 730 | local result = obj:find_pair() | ||
| 731 | assert.same(result[1], 5) | ||
| 732 | return assert.same(result[2], 50) | ||
| 733 | end) | ||
| 734 | it("should work with string interpolation", function() | ||
| 735 | local a, b | ||
| 736 | for i = 1, 5 do | ||
| 737 | if i == 3 then | ||
| 738 | a, b = "item-" .. tostring(i), "value-" .. tostring(i * 10) | ||
| 739 | break | ||
| 740 | end | ||
| 741 | end | ||
| 742 | assert.same(a, "item-3") | ||
| 743 | return assert.same(b, "value-30") | ||
| 744 | end) | ||
| 745 | it("should work with try-catch", function() | ||
| 746 | return xpcall(function() | ||
| 747 | local result, error_msg | ||
| 748 | for i = 1, 5 do | ||
| 749 | if i == 3 then | ||
| 750 | result, error_msg = i, "found" | ||
| 751 | break | ||
| 752 | end | ||
| 753 | end | ||
| 754 | assert.same(result, 3) | ||
| 755 | return assert.same(error_msg, "found") | ||
| 756 | end, function(e) | ||
| 757 | return error("Should not reach here") | ||
| 758 | end) | ||
| 759 | end) | ||
| 760 | it("should work with const attribute", function() | ||
| 761 | local MAX <const> = 10 | ||
| 762 | local first, second | ||
| 763 | for i = 1, MAX do | ||
| 764 | if i == 5 then | ||
| 765 | first, second = i, i * 2 | ||
| 766 | break | ||
| 767 | end | ||
| 768 | end | ||
| 769 | assert.same(first, 5) | ||
| 770 | return assert.same(second, 10) | ||
| 771 | end) | ||
| 772 | it("should work with backcall style processing", function() | ||
| 773 | local a, b | ||
| 774 | for i = 1, 5 do | ||
| 775 | if i == 3 then | ||
| 776 | a, b = i, i * 2 | ||
| 777 | break | ||
| 778 | end | ||
| 779 | end | ||
| 780 | assert.same(a, 3) | ||
| 781 | return assert.same(b, 6) | ||
| 782 | end) | ||
| 783 | it("should work with varargs", function() | ||
| 784 | local process | ||
| 785 | process = function(...) | ||
| 786 | local items = { | ||
| 787 | ... | ||
| 788 | } | ||
| 789 | local a, b | ||
| 790 | for i = 1, #items do | ||
| 791 | if items[i] == 3 then | ||
| 792 | a, b = items[i], items[i] * 2 | ||
| 793 | break | ||
| 794 | end | ||
| 795 | end | ||
| 796 | return a, b | ||
| 797 | end | ||
| 798 | local result1, result2 = process(1, 2, 3, 4, 5) | ||
| 799 | assert.same(result1, 3) | ||
| 800 | return assert.same(result2, 6) | ||
| 801 | end) | ||
| 802 | it("should work with table slicing", function() | ||
| 803 | local matrix = { | ||
| 804 | { | ||
| 805 | 1, | ||
| 806 | 2, | ||
| 807 | 3 | ||
| 808 | }, | ||
| 809 | { | ||
| 810 | 4, | ||
| 811 | 5, | ||
| 812 | 6 | ||
| 813 | }, | ||
| 814 | { | ||
| 815 | 7, | ||
| 816 | 8, | ||
| 817 | 9 | ||
| 818 | } | ||
| 819 | } | ||
| 820 | local sub, val | ||
| 821 | for i = 1, #matrix do | ||
| 822 | if matrix[i][1] == 4 then | ||
| 823 | sub, val = matrix[i][1], matrix[i][2] | ||
| 824 | break | ||
| 825 | end | ||
| 826 | end | ||
| 827 | assert.same(sub, 4) | ||
| 828 | return assert.same(val, 5) | ||
| 829 | end) | ||
| 830 | it("should work with chaining comparison", function() | ||
| 831 | local result, flag | ||
| 832 | for i = 1, 10 do | ||
| 833 | if 1 < i and i <= 5 and 5 < 10 then | ||
| 834 | result, flag = i, i * i | ||
| 835 | break | ||
| 836 | end | ||
| 837 | end | ||
| 838 | assert.same(result, 2) | ||
| 839 | return assert.same(flag, 4) | ||
| 840 | end) | ||
| 841 | it("should work with implicit return", function() | ||
| 842 | local fn | ||
| 843 | fn = function() | ||
| 844 | local result = { | ||
| 845 | _anon_func_2() | ||
| 846 | } | ||
| 847 | return result | ||
| 848 | end | ||
| 849 | local result = fn() | ||
| 850 | assert.same(result[1], 3) | ||
| 851 | return assert.same(result[2], 30) | ||
| 852 | end) | ||
| 853 | it("should work with nil coalescing", function() | ||
| 854 | local data = nil | ||
| 855 | local first, second | ||
| 856 | for i = 1, 5 do | ||
| 857 | if i == 3 then | ||
| 858 | first, second = i, i * 2 | ||
| 859 | break | ||
| 860 | end | ||
| 861 | end | ||
| 862 | assert.same(first, 3) | ||
| 863 | return assert.same(second, 6) | ||
| 864 | end) | ||
| 865 | it("should work with table literal shorthand", function() | ||
| 866 | local a, b | ||
| 867 | for i = 1, 5 do | ||
| 868 | if i == 2 then | ||
| 869 | a, b = "first", "second" | ||
| 870 | break | ||
| 871 | end | ||
| 872 | end | ||
| 873 | assert.same(a, "first") | ||
| 874 | return assert.same(b, "second") | ||
| 875 | end) | ||
| 876 | it("should work with method calls", function() | ||
| 877 | local obj = { | ||
| 878 | start = 10, | ||
| 879 | end_val = 20, | ||
| 880 | find = function(self) | ||
| 881 | local result = { | ||
| 882 | _anon_func_3(self) | ||
| 883 | } | ||
| 884 | return result | ||
| 885 | end | ||
| 886 | } | ||
| 887 | local result = obj:find() | ||
| 888 | assert.same(result[1], 14) | ||
| 889 | return assert.same(result[2], 24) | ||
| 890 | end) | ||
| 891 | it("should work with do-end assignment", function() | ||
| 892 | local a, b | ||
| 893 | do | ||
| 894 | local x, y | ||
| 895 | for i = 1, 5 do | ||
| 896 | if i == 2 then | ||
| 897 | x, y = i, i * 5 | ||
| 898 | break | ||
| 899 | end | ||
| 900 | end | ||
| 901 | a, b = x, y | ||
| 902 | end | ||
| 903 | assert.same(a, 2) | ||
| 904 | return assert.same(b, 10) | ||
| 905 | end) | ||
| 906 | it("should work with unary operators", function() | ||
| 907 | local result, negated | ||
| 908 | for i = 1, 5 do | ||
| 909 | if i == 3 then | ||
| 910 | result, negated = i, -i | ||
| 911 | break | ||
| 912 | end | ||
| 913 | end | ||
| 914 | assert.same(result, 3) | ||
| 915 | return assert.same(negated, -3) | ||
| 916 | end) | ||
| 917 | it("should work with bitwise operators", function() | ||
| 918 | local a, b | ||
| 919 | for i = 1, 10 do | ||
| 920 | if i == 4 then | ||
| 921 | a, b = i ~ 1, i | 8 | ||
| 922 | break | ||
| 923 | end | ||
| 924 | end | ||
| 925 | assert.same(a, 5) | ||
| 926 | return assert.same(b, 12) | ||
| 927 | end) | ||
| 928 | it("should work with modulo operation", function() | ||
| 929 | local quotient, remainder | ||
| 930 | for i = 1, 10 do | ||
| 931 | if i == 7 then | ||
| 932 | quotient, remainder = i // 3, i % 3 | ||
| 933 | break | ||
| 934 | end | ||
| 935 | end | ||
| 936 | assert.same(quotient, 2) | ||
| 937 | return assert.same(remainder, 1) | ||
| 938 | end) | ||
| 939 | it("should work with exponentiation", function() | ||
| 940 | local base, squared | ||
| 941 | for i = 1, 5 do | ||
| 942 | if i == 3 then | ||
| 943 | base, squared = i, i ^ 2 | ||
| 944 | break | ||
| 945 | end | ||
| 946 | end | ||
| 947 | assert.same(base, 3) | ||
| 948 | return assert.same(squared, 9) | ||
| 949 | end) | ||
| 950 | it("should work with length operator", function() | ||
| 951 | local str, len | ||
| 952 | for i = 1, 5 do | ||
| 953 | if i == 3 then | ||
| 954 | str, len = "hello", #"hello" | ||
| 955 | break | ||
| 956 | end | ||
| 957 | end | ||
| 958 | assert.same(str, "hello") | ||
| 959 | return assert.same(len, 5) | ||
| 960 | end) | ||
| 961 | it("should work with table concatenation", function() | ||
| 962 | local part1, part2 | ||
| 963 | for i = 1, 5 do | ||
| 964 | if i == 2 then | ||
| 965 | part1, part2 = { | ||
| 966 | 1, | ||
| 967 | 2 | ||
| 968 | }, { | ||
| 969 | 3, | ||
| 970 | 4 | ||
| 971 | } | ||
| 972 | break | ||
| 973 | end | ||
| 974 | end | ||
| 975 | assert.same(part1, { | ||
| 976 | 1, | ||
| 977 | 2 | ||
| 978 | }) | ||
| 979 | return assert.same(part2, { | ||
| 980 | 3, | ||
| 981 | 4 | ||
| 982 | }) | ||
| 983 | end) | ||
| 984 | it("should work with nested do blocks", function() | ||
| 985 | local result | ||
| 986 | do | ||
| 987 | local temp = { | ||
| 988 | _anon_func_4() | ||
| 989 | } | ||
| 990 | result = temp[1] + temp[2] | ||
| 991 | end | ||
| 992 | return assert.same(result, 9) | ||
| 993 | end) | ||
| 994 | it("should work with table.insert", function() | ||
| 995 | local results = { } | ||
| 996 | local item, index | ||
| 997 | for i = 1, 5 do | ||
| 998 | if i == 3 then | ||
| 999 | item, index = i, i * 2 | ||
| 1000 | break | ||
| 1001 | end | ||
| 1002 | table.insert(results, i) | ||
| 1003 | end | ||
| 1004 | return assert.same(results, { | ||
| 1005 | 1, | ||
| 1006 | 2 | ||
| 1007 | }) | ||
| 1008 | end) | ||
| 1009 | it("should break with multiple values in nested for loops", function() | ||
| 1010 | local x, y | ||
| 1011 | for i = 1, 5 do | ||
| 1012 | local a, b | ||
| 1013 | for j = 1, 5 do | ||
| 1014 | if i == 2 and j == 3 then | ||
| 1015 | a, b = i, j | ||
| 1016 | break | ||
| 1017 | end | ||
| 1018 | end | ||
| 1019 | if a then | ||
| 1020 | x, y = a, b | ||
| 1021 | break | ||
| 1022 | end | ||
| 1023 | end | ||
| 1024 | assert.same(x, 2) | ||
| 1025 | return assert.same(y, 3) | ||
| 1026 | end) | ||
| 1027 | it("should break with multiple values in for nested while", function() | ||
| 1028 | local x, y | ||
| 1029 | for i = 1, 10 do | ||
| 1030 | local j = 1 | ||
| 1031 | local a, b | ||
| 1032 | do | ||
| 1033 | local _val_0, _val_1 | ||
| 1034 | while j < 10 do | ||
| 1035 | if i == 3 and j == 5 then | ||
| 1036 | _val_0, _val_1 = i, j | ||
| 1037 | break | ||
| 1038 | end | ||
| 1039 | j = j + 1 | ||
| 1040 | end | ||
| 1041 | a, b = _val_0, _val_1 | ||
| 1042 | end | ||
| 1043 | if a and b then | ||
| 1044 | x, y = a, b | ||
| 1045 | break | ||
| 1046 | end | ||
| 1047 | end | ||
| 1048 | assert.same(x, 3) | ||
| 1049 | return assert.same(y, 5) | ||
| 1050 | end) | ||
| 1051 | it("should break with multiple values in while nested for", function() | ||
| 1052 | local i = 1 | ||
| 1053 | local x, y | ||
| 1054 | do | ||
| 1055 | local _val_0, _val_1 | ||
| 1056 | while i < 10 do | ||
| 1057 | local a, b | ||
| 1058 | for j = 1, 10 do | ||
| 1059 | if i == 3 and j == 4 then | ||
| 1060 | a, b = i, j | ||
| 1061 | break | ||
| 1062 | end | ||
| 1063 | end | ||
| 1064 | i = i + 1 | ||
| 1065 | if a and b then | ||
| 1066 | _val_0, _val_1 = a, b | ||
| 1067 | break | ||
| 1068 | end | ||
| 1069 | end | ||
| 1070 | x, y = _val_0, _val_1 | ||
| 1071 | end | ||
| 1072 | assert.same(x, 3) | ||
| 1073 | return assert.same(y, 4) | ||
| 1074 | end) | ||
| 1075 | it("should break with multiple values in for nested repeat", function() | ||
| 1076 | local x, y | ||
| 1077 | for i = 1, 5 do | ||
| 1078 | local j = 1 | ||
| 1079 | local a, b | ||
| 1080 | do | ||
| 1081 | local _val_0, _val_1 | ||
| 1082 | repeat | ||
| 1083 | if i == 2 and j == 3 then | ||
| 1084 | _val_0, _val_1 = i, j | ||
| 1085 | break | ||
| 1086 | end | ||
| 1087 | j = j + 1 | ||
| 1088 | until j > 10 | ||
| 1089 | a, b = _val_0, _val_1 | ||
| 1090 | end | ||
| 1091 | if a and b then | ||
| 1092 | x, y = a, b | ||
| 1093 | break | ||
| 1094 | end | ||
| 1095 | end | ||
| 1096 | assert.same(x, 2) | ||
| 1097 | return assert.same(y, 3) | ||
| 1098 | end) | ||
| 1099 | it("should break with multiple values in repeat nested for", function() | ||
| 1100 | local i = 1 | ||
| 1101 | local x, y | ||
| 1102 | do | ||
| 1103 | local _val_0, _val_1 | ||
| 1104 | repeat | ||
| 1105 | local a, b | ||
| 1106 | for j = 1, 5 do | ||
| 1107 | if i == 3 and j == 2 then | ||
| 1108 | a, b = i, j | ||
| 1109 | break | ||
| 1110 | end | ||
| 1111 | end | ||
| 1112 | i = i + 1 | ||
| 1113 | if a and b then | ||
| 1114 | _val_0, _val_1 = a, b | ||
| 1115 | break | ||
| 1116 | end | ||
| 1117 | until i > 10 | ||
| 1118 | x, y = _val_0, _val_1 | ||
| 1119 | end | ||
| 1120 | assert.same(x, 3) | ||
| 1121 | return assert.same(y, 2) | ||
| 1122 | end) | ||
| 1123 | it("should break with multiple values in do nested for", function() | ||
| 1124 | local x, y | ||
| 1125 | do | ||
| 1126 | repeat | ||
| 1127 | local a1, b1 | ||
| 1128 | for i = 1, 5 do | ||
| 1129 | local a, b | ||
| 1130 | for j = 1, 5 do | ||
| 1131 | if i == 2 and j == 3 then | ||
| 1132 | a, b = i, j | ||
| 1133 | break | ||
| 1134 | end | ||
| 1135 | end | ||
| 1136 | if a and b then | ||
| 1137 | a1, b1 = a, b | ||
| 1138 | break | ||
| 1139 | end | ||
| 1140 | end | ||
| 1141 | x, y = a1, b1 | ||
| 1142 | break | ||
| 1143 | until true | ||
| 1144 | end | ||
| 1145 | assert.same(x, 2) | ||
| 1146 | return assert.same(y, 3) | ||
| 1147 | end) | ||
| 1148 | it("should break with multiple values in for nested do", function() | ||
| 1149 | local x, y | ||
| 1150 | for i = 1, 5 do | ||
| 1151 | do | ||
| 1152 | if i == 3 then | ||
| 1153 | x, y = i, i * 2 | ||
| 1154 | break | ||
| 1155 | end | ||
| 1156 | end | ||
| 1157 | end | ||
| 1158 | assert.same(x, 3) | ||
| 1159 | return assert.same(y, 6) | ||
| 1160 | end) | ||
| 1161 | it("should break with multiple values in for nested with", function() | ||
| 1162 | local x, y | ||
| 1163 | for i = 1, 5 do | ||
| 1164 | local _with_0 = { | ||
| 1165 | value = i, | ||
| 1166 | index = i * 2 | ||
| 1167 | } | ||
| 1168 | if _with_0.value == 3 then | ||
| 1169 | x, y = _with_0.value, _with_0.index | ||
| 1170 | break | ||
| 1171 | end | ||
| 1172 | end | ||
| 1173 | assert.same(x, 3) | ||
| 1174 | return assert.same(y, 6) | ||
| 1175 | end) | ||
| 1176 | it("should break with a value in with nested for", function() | ||
| 1177 | local x | ||
| 1178 | do | ||
| 1179 | local _with_0 = { | ||
| 1180 | limit = 5 | ||
| 1181 | } | ||
| 1182 | repeat | ||
| 1183 | for i = 1, _with_0.limit do | ||
| 1184 | if i == 3 then | ||
| 1185 | x = i | ||
| 1186 | break | ||
| 1187 | end | ||
| 1188 | end | ||
| 1189 | break | ||
| 1190 | until true | ||
| 1191 | end | ||
| 1192 | return assert.same(x, 3) | ||
| 1193 | end) | ||
| 1194 | it("should break with multiple values in three-level nested loops", function() | ||
| 1195 | local x, y, z | ||
| 1196 | for i = 1, 3 do | ||
| 1197 | local a1, a2, a3 | ||
| 1198 | for j = 1, 3 do | ||
| 1199 | local b1, b2, b3 | ||
| 1200 | for k = 1, 3 do | ||
| 1201 | if i == 2 and j == 2 and k == 2 then | ||
| 1202 | b1, b2, b3 = i, j, k | ||
| 1203 | break | ||
| 1204 | end | ||
| 1205 | end | ||
| 1206 | if b1 then | ||
| 1207 | a1, a2, a3 = b1, b2, b3 | ||
| 1208 | break | ||
| 1209 | end | ||
| 1210 | end | ||
| 1211 | if a1 then | ||
| 1212 | x, y, z = a1, a2, a3 | ||
| 1213 | break | ||
| 1214 | end | ||
| 1215 | end | ||
| 1216 | assert.same(x, 2) | ||
| 1217 | assert.same(y, 2) | ||
| 1218 | return assert.same(z, 2) | ||
| 1219 | end) | ||
| 1220 | it("should break with multiple values in while nested repeat", function() | ||
| 1221 | local i = 1 | ||
| 1222 | local x, y | ||
| 1223 | do | ||
| 1224 | local _val_0, _val_1 | ||
| 1225 | while i < 10 do | ||
| 1226 | local j = 1 | ||
| 1227 | local a, b | ||
| 1228 | do | ||
| 1229 | local _val_2, _val_3 | ||
| 1230 | repeat | ||
| 1231 | if i == 3 and j == 4 then | ||
| 1232 | _val_2, _val_3 = i, j | ||
| 1233 | break | ||
| 1234 | end | ||
| 1235 | j = j + 1 | ||
| 1236 | until j > 10 | ||
| 1237 | a, b = _val_2, _val_3 | ||
| 1238 | end | ||
| 1239 | i = i + 1 | ||
| 1240 | if a then | ||
| 1241 | _val_0, _val_1 = a, b | ||
| 1242 | break | ||
| 1243 | end | ||
| 1244 | end | ||
| 1245 | x, y = _val_0, _val_1 | ||
| 1246 | end | ||
| 1247 | assert.same(x, 3) | ||
| 1248 | return assert.same(y, 4) | ||
| 1249 | end) | ||
| 1250 | it("should break with multiple values in repeat nested while", function() | ||
| 1251 | local i = 1 | ||
| 1252 | local x, y | ||
| 1253 | do | ||
| 1254 | local _val_0, _val_1 | ||
| 1255 | repeat | ||
| 1256 | local j = 1 | ||
| 1257 | local a, b | ||
| 1258 | do | ||
| 1259 | local _val_2, _val_3 | ||
| 1260 | while j < 10 do | ||
| 1261 | if i == 4 and j == 3 then | ||
| 1262 | _val_2, _val_3 = i, j | ||
| 1263 | break | ||
| 1264 | end | ||
| 1265 | j = j + 1 | ||
| 1266 | end | ||
| 1267 | a, b = _val_2, _val_3 | ||
| 1268 | end | ||
| 1269 | i = i + 1 | ||
| 1270 | if a then | ||
| 1271 | _val_0, _val_1 = a, b | ||
| 1272 | break | ||
| 1273 | end | ||
| 1274 | until i > 10 | ||
| 1275 | x, y = _val_0, _val_1 | ||
| 1276 | end | ||
| 1277 | assert.same(x, 4) | ||
| 1278 | return assert.same(y, 3) | ||
| 1279 | end) | ||
| 1280 | it("should break with multiple values in do nested while", function() | ||
| 1281 | local i = 1 | ||
| 1282 | local x, y | ||
| 1283 | do | ||
| 1284 | repeat | ||
| 1285 | local a, b | ||
| 1286 | do | ||
| 1287 | local _val_0, _val_1 | ||
| 1288 | while i < 10 do | ||
| 1289 | if i == 4 then | ||
| 1290 | _val_0, _val_1 = i, i * 3 | ||
| 1291 | break | ||
| 1292 | end | ||
| 1293 | i = i + 1 | ||
| 1294 | end | ||
| 1295 | a, b = _val_0, _val_1 | ||
| 1296 | end | ||
| 1297 | x, y = a, b | ||
| 1298 | break | ||
| 1299 | until true | ||
| 1300 | end | ||
| 1301 | assert.same(x, 4) | ||
| 1302 | return assert.same(y, 12) | ||
| 1303 | end) | ||
| 1304 | it("should break with multiple values in while nested do", function() | ||
| 1305 | local i = 1 | ||
| 1306 | local x, y | ||
| 1307 | do | ||
| 1308 | local _val_0, _val_1 | ||
| 1309 | while i < 10 do | ||
| 1310 | local a, b | ||
| 1311 | do | ||
| 1312 | repeat | ||
| 1313 | if i == 5 then | ||
| 1314 | a, b = i, i * 4 | ||
| 1315 | break | ||
| 1316 | end | ||
| 1317 | until true | ||
| 1318 | end | ||
| 1319 | i = i + 1 | ||
| 1320 | if a then | ||
| 1321 | _val_0, _val_1 = a, b | ||
| 1322 | break | ||
| 1323 | end | ||
| 1324 | end | ||
| 1325 | x, y = _val_0, _val_1 | ||
| 1326 | end | ||
| 1327 | assert.same(x, 5) | ||
| 1328 | return assert.same(y, 20) | ||
| 1329 | end) | ||
| 1330 | it("should break with multiple values in do nested repeat", function() | ||
| 1331 | local i = 1 | ||
| 1332 | local x, y | ||
| 1333 | do | ||
| 1334 | repeat | ||
| 1335 | local a, b | ||
| 1336 | do | ||
| 1337 | local _val_0, _val_1 | ||
| 1338 | repeat | ||
| 1339 | if i == 3 then | ||
| 1340 | _val_0, _val_1 = i, i * 5 | ||
| 1341 | break | ||
| 1342 | end | ||
| 1343 | i = i + 1 | ||
| 1344 | until i > 10 | ||
| 1345 | a, b = _val_0, _val_1 | ||
| 1346 | end | ||
| 1347 | x, y = a, b | ||
| 1348 | break | ||
| 1349 | until true | ||
| 1350 | end | ||
| 1351 | assert.same(x, 3) | ||
| 1352 | return assert.same(y, 15) | ||
| 1353 | end) | ||
| 1354 | it("should break with multiple values in repeat nested do", function() | ||
| 1355 | local i = 1 | ||
| 1356 | local x, y | ||
| 1357 | do | ||
| 1358 | local _val_0, _val_1 | ||
| 1359 | repeat | ||
| 1360 | local a, b | ||
| 1361 | do | ||
| 1362 | repeat | ||
| 1363 | if i == 2 then | ||
| 1364 | a, b = i, i * 6 | ||
| 1365 | break | ||
| 1366 | end | ||
| 1367 | until true | ||
| 1368 | end | ||
| 1369 | i = i + 1 | ||
| 1370 | if a then | ||
| 1371 | _val_0, _val_1 = a, b | ||
| 1372 | break | ||
| 1373 | end | ||
| 1374 | until i > 10 | ||
| 1375 | x, y = _val_0, _val_1 | ||
| 1376 | end | ||
| 1377 | assert.same(x, 2) | ||
| 1378 | return assert.same(y, 12) | ||
| 1379 | end) | ||
| 1380 | it("should break with multiple values in with nested do", function() | ||
| 1381 | local x, y | ||
| 1382 | do | ||
| 1383 | repeat | ||
| 1384 | local _with_0 = { | ||
| 1385 | value = 10, | ||
| 1386 | factor = 3 | ||
| 1387 | } | ||
| 1388 | local a, b | ||
| 1389 | do | ||
| 1390 | repeat | ||
| 1391 | if _with_0.value > 5 then | ||
| 1392 | a, b = _with_0.value, _with_0.factor | ||
| 1393 | break | ||
| 1394 | end | ||
| 1395 | until true | ||
| 1396 | end | ||
| 1397 | x, y = a, b | ||
| 1398 | break | ||
| 1399 | until true | ||
| 1400 | end | ||
| 1401 | assert.same(x, 10) | ||
| 1402 | return assert.same(y, 3) | ||
| 1403 | end) | ||
| 1404 | it("should break with multiple values in do nested with", function() | ||
| 1405 | local x, y | ||
| 1406 | do | ||
| 1407 | repeat | ||
| 1408 | local _with_0 = { | ||
| 1409 | num = 42, | ||
| 1410 | text = "test" | ||
| 1411 | } | ||
| 1412 | x, y = _with_0.num, _with_0.text | ||
| 1413 | break | ||
| 1414 | until true | ||
| 1415 | end | ||
| 1416 | assert.same(x, 42) | ||
| 1417 | return assert.same(y, "test") | ||
| 1418 | end) | ||
| 1419 | it("should break with multiple values in for nested for with different types", function() | ||
| 1420 | local name, age, active | ||
| 1421 | for i = 1, 5 do | ||
| 1422 | local a, b, c | ||
| 1423 | for j = 1, 5 do | ||
| 1424 | if i == 2 and j == 3 then | ||
| 1425 | a, b, c = "user-" .. tostring(i), j * 10, true | ||
| 1426 | break | ||
| 1427 | end | ||
| 1428 | end | ||
| 1429 | if a then | ||
| 1430 | name, age, active = a, b, c | ||
| 1431 | break | ||
| 1432 | end | ||
| 1433 | end | ||
| 1434 | assert.same(name, "user-2") | ||
| 1435 | assert.same(age, 30) | ||
| 1436 | return assert.is_true(active) | ||
| 1437 | end) | ||
| 1438 | it("should break with multiple values in with nested for with method calls", function() | ||
| 1439 | local obj = { | ||
| 1440 | data = { | ||
| 1441 | 10, | ||
| 1442 | 20, | ||
| 1443 | 30, | ||
| 1444 | 40 | ||
| 1445 | }, | ||
| 1446 | find = function(self) | ||
| 1447 | local x, y | ||
| 1448 | for i = 1, #self.data do | ||
| 1449 | if self.data[i] == 30 then | ||
| 1450 | x, y = self.data[i], i | ||
| 1451 | break | ||
| 1452 | end | ||
| 1453 | end | ||
| 1454 | return x, y | ||
| 1455 | end | ||
| 1456 | } | ||
| 1457 | local value, index = obj:find() | ||
| 1458 | assert.same(value, 30) | ||
| 1459 | return assert.same(index, 3) | ||
| 1460 | end) | ||
| 1461 | it("should break with multiple values in nested loops with early termination", function() | ||
| 1462 | local found, idx | ||
| 1463 | for i = 1, 10 do | ||
| 1464 | if i == 1 then | ||
| 1465 | found, idx = i, true | ||
| 1466 | break | ||
| 1467 | end | ||
| 1468 | local a, b | ||
| 1469 | for j = 1, 10 do | ||
| 1470 | if j == 5 then | ||
| 1471 | a, b = i, false | ||
| 1472 | break | ||
| 1473 | end | ||
| 1474 | end | ||
| 1475 | if a then | ||
| 1476 | found, idx = a, b | ||
| 1477 | break | ||
| 1478 | end | ||
| 1479 | end | ||
| 1480 | assert.same(found, 1) | ||
| 1481 | return assert.is_true(idx) | ||
| 1482 | end) | ||
| 1483 | it("should break with multiple values in nested loops with computed expressions", function() | ||
| 1484 | local sum, product | ||
| 1485 | for i = 1, 5 do | ||
| 1486 | local a, b | ||
| 1487 | for j = 1, 5 do | ||
| 1488 | if i * j == 12 then | ||
| 1489 | a, b = i + j, i * j | ||
| 1490 | break | ||
| 1491 | end | ||
| 1492 | end | ||
| 1493 | if a then | ||
| 1494 | sum, product = a, b | ||
| 1495 | break | ||
| 1496 | end | ||
| 1497 | end | ||
| 1498 | assert.same(sum, 7) | ||
| 1499 | return assert.same(product, 12) | ||
| 1500 | end) | ||
| 1501 | it("should mix break and break with values in same for expression", function() | ||
| 1502 | local x, y | ||
| 1503 | for i = 1, 5 do | ||
| 1504 | if i == 3 then | ||
| 1505 | x, y = i, i * 10 | ||
| 1506 | break | ||
| 1507 | end | ||
| 1508 | if i == 4 then | ||
| 1509 | break | ||
| 1510 | end | ||
| 1511 | end | ||
| 1512 | assert.same(x, 3) | ||
| 1513 | return assert.same(y, 30) | ||
| 1514 | end) | ||
| 1515 | it("should return nils when plain break happens before value break", function() | ||
| 1516 | local x, y | ||
| 1517 | for i = 1, 5 do | ||
| 1518 | if i == 2 then | ||
| 1519 | break | ||
| 1520 | end | ||
| 1521 | if i == 4 then | ||
| 1522 | x, y = i, i * 10 | ||
| 1523 | break | ||
| 1524 | end | ||
| 1525 | end | ||
| 1526 | assert.is_nil(x) | ||
| 1527 | return assert.is_nil(y) | ||
| 1528 | end) | ||
| 1529 | it("should mix plain break and value break across nested loops", function() | ||
| 1530 | local matrix = { | ||
| 1531 | { | ||
| 1532 | 1, | ||
| 1533 | -1, | ||
| 1534 | 9 | ||
| 1535 | }, | ||
| 1536 | { | ||
| 1537 | 4, | ||
| 1538 | 5, | ||
| 1539 | 9 | ||
| 1540 | } | ||
| 1541 | } | ||
| 1542 | local row, col | ||
| 1543 | for r = 1, #matrix do | ||
| 1544 | local a, b | ||
| 1545 | for c = 1, #matrix[r] do | ||
| 1546 | local cell = matrix[r][c] | ||
| 1547 | if cell < 0 then | ||
| 1548 | break | ||
| 1549 | end | ||
| 1550 | if cell == 9 then | ||
| 1551 | a, b = r, c | ||
| 1552 | break | ||
| 1553 | end | ||
| 1554 | end | ||
| 1555 | if a then | ||
| 1556 | row, col = a, b | ||
| 1557 | break | ||
| 1558 | end | ||
| 1559 | end | ||
| 1560 | assert.same(row, 2) | ||
| 1561 | return assert.same(col, 3) | ||
| 1562 | end) | ||
| 1563 | it("should mix break and break with values in while expression", function() | ||
| 1564 | local i = 0 | ||
| 1565 | local x, y | ||
| 1566 | do | ||
| 1567 | local _val_0, _val_1 | ||
| 1568 | while i < 5 do | ||
| 1569 | i = i + 1 | ||
| 1570 | if i == 2 then | ||
| 1571 | break | ||
| 1572 | end | ||
| 1573 | if i == 4 then | ||
| 1574 | _val_0, _val_1 = i, i * 2 | ||
| 1575 | break | ||
| 1576 | end | ||
| 1577 | end | ||
| 1578 | x, y = _val_0, _val_1 | ||
| 1579 | end | ||
| 1580 | assert.is_nil(x) | ||
| 1581 | return assert.is_nil(y) | ||
| 1582 | end) | ||
| 1583 | it("should mix break and break with values in do with with block", function() | ||
| 1584 | local x, y | ||
| 1585 | do | ||
| 1586 | repeat | ||
| 1587 | local _with_0 = { | ||
| 1588 | value = 8, | ||
| 1589 | stop = false | ||
| 1590 | } | ||
| 1591 | if _with_0.stop then | ||
| 1592 | break | ||
| 1593 | end | ||
| 1594 | x, y = _with_0.value, _with_0.value * 2 | ||
| 1595 | break | ||
| 1596 | until true | ||
| 1597 | end | ||
| 1598 | assert.same(x, 8) | ||
| 1599 | return assert.same(y, 16) | ||
| 1600 | end) | ||
| 1601 | it("should fall back to plain break in do with with block", function() | ||
| 1602 | local x, y | ||
| 1603 | do | ||
| 1604 | repeat | ||
| 1605 | do | ||
| 1606 | local _with_0 = { | ||
| 1607 | value = 8, | ||
| 1608 | stop = true | ||
| 1609 | } | ||
| 1610 | if _with_0.stop then | ||
| 1611 | break | ||
| 1612 | end | ||
| 1613 | x, y = _with_0.value, _with_0.value * 2 | ||
| 1614 | break | ||
| 1615 | end | ||
| 1616 | break | ||
| 1617 | until true | ||
| 1618 | end | ||
| 1619 | assert.is_nil(x) | ||
| 1620 | return assert.is_nil(y) | ||
| 1621 | end) | ||
| 1622 | it("should mix continue and break with values in for expression", function() | ||
| 1623 | local x, y | ||
| 1624 | for i = 1, 8 do | ||
| 1625 | if i % 3 ~= 0 then | ||
| 1626 | goto _continue_0 | ||
| 1627 | end | ||
| 1628 | if i == 6 then | ||
| 1629 | x, y = i, i + 100 | ||
| 1630 | break | ||
| 1631 | end | ||
| 1632 | ::_continue_0:: | ||
| 1633 | end | ||
| 1634 | assert.same(x, 6) | ||
| 1635 | return assert.same(y, 106) | ||
| 1636 | end) | ||
| 1637 | it("should mix continue and break with values across nested for expressions", function() | ||
| 1638 | local row, col | ||
| 1639 | for r = 1, 3 do | ||
| 1640 | local a, b | ||
| 1641 | for c = 1, 4 do | ||
| 1642 | if c < 3 then | ||
| 1643 | goto _continue_1 | ||
| 1644 | end | ||
| 1645 | if r == 2 and c == 3 then | ||
| 1646 | a, b = r, c | ||
| 1647 | break | ||
| 1648 | end | ||
| 1649 | ::_continue_1:: | ||
| 1650 | end | ||
| 1651 | if not a then | ||
| 1652 | goto _continue_0 | ||
| 1653 | end | ||
| 1654 | row, col = a, b | ||
| 1655 | break | ||
| 1656 | ::_continue_0:: | ||
| 1657 | end | ||
| 1658 | assert.same(row, 2) | ||
| 1659 | return assert.same(col, 3) | ||
| 1660 | end) | ||
| 1661 | it("should mix break continue and value break with plain break winning", function() | ||
| 1662 | local x, y | ||
| 1663 | for i = 1, 7 do | ||
| 1664 | if i < 3 then | ||
| 1665 | goto _continue_0 | ||
| 1666 | end | ||
| 1667 | if i == 4 then | ||
| 1668 | break | ||
| 1669 | end | ||
| 1670 | if i == 6 then | ||
| 1671 | x, y = i, i * 10 | ||
| 1672 | break | ||
| 1673 | end | ||
| 1674 | ::_continue_0:: | ||
| 1675 | end | ||
| 1676 | assert.is_nil(x) | ||
| 1677 | return assert.is_nil(y) | ||
| 1678 | end) | ||
| 1679 | return it("should mix break continue and value break with value break winning", function() | ||
| 1680 | local x, y | ||
| 1681 | for i = 1, 9 do | ||
| 1682 | if i % 2 == 0 then | ||
| 1683 | goto _continue_0 | ||
| 1684 | end | ||
| 1685 | if i == 5 then | ||
| 1686 | x, y = i, i * 3 | ||
| 1687 | break | ||
| 1688 | end | ||
| 1689 | if i == 9 then | ||
| 1690 | break | ||
| 1691 | end | ||
| 1692 | ::_continue_0:: | ||
| 1693 | end | ||
| 1694 | assert.same(x, 5) | ||
| 1695 | return assert.same(y, 15) | ||
| 1696 | end) | ||
| 1697 | end) | ||
