aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/inputs/test/break_multiple_values_spec.yue840
-rw-r--r--spec/outputs/test/break_multiple_values_spec.lua1697
2 files changed, 2537 insertions, 0 deletions
diff --git a/spec/inputs/test/break_multiple_values_spec.yue b/spec/inputs/test/break_multiple_values_spec.yue
new file mode 100644
index 0000000..846be00
--- /dev/null
+++ b/spec/inputs/test/break_multiple_values_spec.yue
@@ -0,0 +1,840 @@
1describe "break with multiple values", ->
2 it "should break with multiple values from numeric for loop", ->
3 x, y = for i = 1, 10
4 if i > 5
5 break i, i * 2
6 assert.same x, 6
7 assert.same y, 12
8
9 it "should break with multiple values from ipairs iterator", ->
10 x, y = for _, v in ipairs [1, 2, 3]
11 if v > 2
12 break v, v * 2
13 assert.same x, 3
14 assert.same y, 6
15
16 it "should break with multiple values from destructuring iterator", ->
17 x, y = for [a, b] in *{[10, 20], [30, 40], [40, 50]}
18 if b > 20
19 break a, b * 2
20 assert.same x, 30
21 assert.same y, 80
22
23 it "should break with multiple values from do block", ->
24 x, y = do
25 break 1, 2
26 assert.same x, 1
27 assert.same y, 2
28
29 it "should break with multiple values from with statement", ->
30 x = with abc: 99, flag: true
31 break .abc if .flag
32 break 0
33 assert.same x, 99
34
35 it "should work with continue in comprehension", ->
36 input = {1,2,3,4,5,6}
37 output = for x in *input
38 continue if x % 2 == 1
39 x
40 assert.same output, {2, 4, 6}
41
42 it "should break in comprehension with index", ->
43 f1 = ->
44 tb = [true, true, false]
45 index = for i = 1, #tb
46 break i if tb[i]
47 index
48 assert.same f1!, 1
49
50 it "should work with continue in indexed comprehension", ->
51 input = {1,2,3,4,5,6}
52 output = for i = 1, #input
53 x = input[i]
54 continue if x % 2 == 1
55 x
56 assert.same output, {2, 4, 6}
57
58 it "should break with multiple values in simple for loop", ->
59 a, b = for i = 1, 5
60 if i == 2
61 break i, i * 10
62 assert.same a, 2
63 assert.same b, 20
64
65 it "should break with expression values", ->
66 sum, product = for i = 1, 3
67 if i == 2
68 break i + i, i * i
69 assert.same sum, 4
70 assert.same product, 4
71
72 it "should break with table iteration values", ->
73 key, value = for k, v in pairs {a: 1, b: 2, c: 3}
74 if k == "b"
75 break k, v * 10
76 assert.same key, "b"
77 assert.same value, 20
78
79 it "should break in for loop with multiple values", ->
80 count, total = for i = 1, 10
81 if i == 5
82 break i, i * 10
83 assert.same count, 5
84 assert.same total, 50
85
86 it "should break in repeat loop with multiple values", ->
87 i = 1
88 i, doubled = repeat
89 if i > 3
90 break i, i * 100
91 i += 1
92 until false
93 assert.same i, 4
94 assert.same doubled, 400
95
96 it "should work with with? and break", ->
97 dummy = (x) -> x
98 result = with? value: 1
99 dummy with? nil
100 break .value
101 break .value
102 assert.same result, 1
103
104 it "should break with multiple values in do block", ->
105 x, y = do
106 break 10, 30
107 assert.same x, 10
108 assert.same y, 30
109
110 it "should break with string and number values", ->
111 name, age = for k, v in pairs {name: "Alice", age: 30, city: "NYC"}
112 if k == "name"
113 break v, 25
114 assert.same name, "Alice"
115 assert.same age, 25
116
117 it "should break with nil values", ->
118 a, b, c = for i = 1, 5
119 if i == 3
120 break i, nil, "test"
121 assert.same a, 3
122 assert.same b, nil
123 assert.same c, "test"
124
125 it "should break with boolean values", ->
126 found, value = for i = 1, 5
127 if i == 3
128 break true, "found"
129 assert.is_true found
130 assert.same value, "found"
131
132 it "should break with function call values", ->
133 fn = -> 42
134 val, dbl = for i = 1, 3
135 if i == 2
136 break fn!, fn! * 2
137 assert.same val, 42
138 assert.same dbl, 84
139
140 it "should break with multiple values for destructuring", ->
141 x, y = for [a, b] in *{[1, 2], [3, 4], [5, 6]}
142 if a > 2
143 break b, a
144 assert.same x, 4
145 assert.same y, 3
146
147 it "should break in switch-like logic", ->
148 status, message = for i = 1, 3
149 do
150 if i == 1
151 break "ok", "one"
152 if i == 2
153 break "ok", "two"
154 assert.same status, "ok"
155 assert.same message, "one"
156
157 it "should handle multiple break conditions", ->
158 idx, val = for i = 1, 10
159 if i == 3
160 break i, "first"
161 if i == 7
162 break i, "second"
163 assert.same idx, 3
164 assert.same val, "first"
165
166 it "should break with computed values", ->
167 sum, count = for i = 1, 5
168 if i == 4
169 break 10 + i, i
170 assert.same sum, 14
171 assert.same count, 4
172
173 it "should break with table literal", ->
174 name, data = for i = 1, 3
175 if i == 2
176 break "item", {value: i, index: i}
177 assert.same name, "item"
178 assert.same data.value, 2
179 assert.same data.index, 2
180
181 it "should break with string concatenation", ->
182 prefix, suffix = for i = 1, 3
183 if i == 2
184 break "pre", "fix"
185 assert.same prefix .. suffix, "prefix"
186
187 it "should break with continue in loop", ->
188 x, y = for i = 1, 5
189 if i == 3
190 break i, i * 2
191 assert.same x, 3
192 assert.same y, 6
193
194 it "should break with ternary-like expression", ->
195 result, is_valid = for i = 1, 5
196 if i == 3
197 break i, i > 2
198 assert.same result, 3
199 assert.is_true is_valid
200
201 it "should break with arithmetic operations", ->
202 sum, diff = for i = 1, 5
203 if i == 3
204 break i + 10, 10 - i
205 assert.same sum, 13
206 assert.same diff, 7
207
208 it "should break with table access", ->
209 data = {a: 1, b: 2}
210 first, second = for i = 1, 3
211 if i == 2
212 break data.a, data.b
213 assert.same first, 1
214 assert.same second, 2
215
216 it "should break with literal values", ->
217 a, b = for i = 1, 3
218 if i == 2
219 break "x", "y"
220 assert.same a, "x"
221 assert.same b, "y"
222
223 it "should break with conditional expression", ->
224 status, msg = for i = 1, 5
225 if i == 3
226 break "ok", "done"
227 assert.same status, "ok"
228 assert.same msg, "done"
229
230 it "should handle multiple breaks in sequence", ->
231 val1, val2 = for i = 1, 10
232 if i == 3
233 break i, i * 2
234 if i == 5
235 break i, i * 3
236 if i == 7
237 break i, i * 4
238 assert.same val1, 3
239 assert.same val2, 6
240
241 it "should handle multiple breaks in switch", ->
242 val1, val2 = for i = 1, 10 do switch i
243 when 3 then break i, i * 2
244 when 5 then break i, i * 3
245 when 7 then break i, i * 4
246 assert.same val1, 3
247 assert.same val2, 6
248
249 it "should break with nil and default", ->
250 a, b = for i = 1, 5
251 if i == 2
252 break i, nil
253 assert.same a, 2
254 assert.is_nil b
255
256 it "should break with false and value", ->
257 found, data = for i = 1, 5
258 if i == 3
259 break false, "item"
260 assert.is_false found
261 assert.same data, "item"
262
263 it "should break with three values", ->
264 a, b, c = for i = 1, 5
265 if i == 3
266 break i, i * 2, i * 3
267 assert.same a, 3
268 assert.same b, 6
269 assert.same c, 9
270
271 it "should break in while loop", ->
272 i = 1
273 count, total = while i < 10
274 if i == 5
275 break i, i * 10
276 i += 1
277 assert.same count, 5
278 assert.same total, 50
279
280 it "should handle early break in loop", ->
281 a, b = for i = 1, 100
282 if i == 1
283 break "first", "second"
284 assert.same a, "first"
285 assert.same b, "second"
286
287 it "should break with multiple function calls", ->
288 add = (a, b) -> a + b
289 mul = (a, b) -> a * b
290 sum, product = for i = 1, 5
291 if i == 3
292 break add(i, i), mul(i, i)
293 assert.same sum, 6
294 assert.same product, 9
295
296 it "should work with chained comparisons", ->
297 x, y = for i = 1, 10
298 if 3 < i and i < 7
299 break i, i * i
300 assert.same x, 4
301 assert.same y, 16
302
303 it "should work with logical expressions", ->
304 flag, value = for i = 1, 5
305 if i > 2 and i < 4
306 break true, i
307 assert.is_true flag
308 assert.same value, 3
309
310 it "should work with table unpacking in break", ->
311 tbl = {10, 20}
312 a, b = for i = 1, 5
313 if i == 3
314 break tbl[1], tbl[2]
315 assert.same a, 10
316 assert.same b, 20
317
318 it "should work with nested conditions", ->
319 result, flag = for i = 1, 10
320 if i > 2
321 if i < 5
322 if i == 3
323 break i, true
324 assert.same result, 3
325 assert.is_true flag
326
327 it "should work with class methods", ->
328 class MyClass
329 find_pair: =>
330 result = [
331 for i = 1, 10
332 if i == 5
333 break i, i * 10
334 ]
335 result
336 obj = MyClass!
337 result = obj\find_pair!
338 assert.same result[1], 5
339 assert.same result[2], 50
340
341 it "should work with string interpolation", ->
342 a, b = for i = 1, 5
343 if i == 3
344 break "item-#{i}", "value-#{i * 10}"
345 assert.same a, "item-3"
346 assert.same b, "value-30"
347
348 it "should work with try-catch", ->
349 try
350 result, error_msg = for i = 1, 5
351 if i == 3
352 break i, "found"
353 assert.same result, 3
354 assert.same error_msg, "found"
355 catch e
356 error "Should not reach here"
357
358 it "should work with const attribute", ->
359 const MAX = 10
360 first, second = for i = 1, MAX
361 if i == 5
362 break i, i * 2
363 assert.same first, 5
364 assert.same second, 10
365
366 it "should work with backcall style processing", ->
367 a, b = for i = 1, 5
368 if i == 3
369 break i, i * 2
370 assert.same a, 3
371 assert.same b, 6
372
373 it "should work with varargs", ->
374 process = (...) ->
375 items = {...}
376 a, b = for i = 1, #items
377 if items[i] == 3
378 break items[i], items[i] * 2
379 a, b
380 result1, result2 = process 1, 2, 3, 4, 5
381 assert.same result1, 3
382 assert.same result2, 6
383
384 it "should work with table slicing", ->
385 matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
386 sub, val = for i = 1, #matrix
387 if matrix[i][1] == 4
388 break matrix[i][1], matrix[i][2]
389 assert.same sub, 4
390 assert.same val, 5
391
392 it "should work with chaining comparison", ->
393 result, flag = for i = 1, 10
394 if 1 < i <= 5 < 10
395 break i, i * i
396 assert.same result, 2
397 assert.same flag, 4
398
399 it "should work with implicit return", ->
400 fn = ->
401 result = [
402 for i = 1, 5
403 if i == 3
404 break i, i * 10
405 ]
406 result
407 result = fn!
408 assert.same result[1], 3
409 assert.same result[2], 30
410
411 it "should work with nil coalescing", ->
412 data = nil
413 first, second = for i = 1, 5
414 if i == 3
415 break i, i * 2
416 assert.same first, 3
417 assert.same second, 6
418
419 it "should work with table literal shorthand", ->
420 a, b = for i = 1, 5
421 if i == 2
422 break "first", "second"
423 assert.same a, "first"
424 assert.same b, "second"
425
426 it "should work with method calls", ->
427 obj = {
428 start: 10
429 end_val: 20
430 find: =>
431 result = [
432 for i = 1, 10
433 if i == 4
434 break @start + i, @end_val + i
435 ]
436 result
437 }
438 result = obj\find!
439 assert.same result[1], 14
440 assert.same result[2], 24
441
442 it "should work with do-end assignment", ->
443 a, b = do
444 x, y = for i = 1, 5
445 if i == 2
446 break i, i * 5
447 x, y
448 assert.same a, 2
449 assert.same b, 10
450
451 it "should work with unary operators", ->
452 result, negated = for i = 1, 5
453 if i == 3
454 break i, -i
455 assert.same result, 3
456 assert.same negated, -3
457
458 it "should work with bitwise operators", ->
459 a, b = for i = 1, 10
460 if i == 4
461 break i ~ 1, i | 8
462 assert.same a, 5
463 assert.same b, 12
464
465 it "should work with modulo operation", ->
466 quotient, remainder = for i = 1, 10
467 if i == 7
468 break i // 3, i % 3
469 assert.same quotient, 2
470 assert.same remainder, 1
471
472 it "should work with exponentiation", ->
473 base, squared = for i = 1, 5
474 if i == 3
475 break i, i ^ 2
476 assert.same base, 3
477 assert.same squared, 9
478
479 it "should work with length operator", ->
480 str, len = for i = 1, 5
481 if i == 3
482 break "hello", #"hello"
483 assert.same str, "hello"
484 assert.same len, 5
485
486 it "should work with table concatenation", ->
487 part1, part2 = for i = 1, 5
488 if i == 2
489 break {1, 2}, {3, 4}
490 assert.same part1, {1, 2}
491 assert.same part2, {3, 4}
492
493 it "should work with nested do blocks", ->
494 result = do
495 temp = {
496 for i = 1, 5
497 do
498 if i == 3
499 break i, i * 2
500 }
501 temp[1] + temp[2]
502 assert.same result, 9
503
504 it "should work with table.insert", ->
505 results = {}
506 item, index = for i = 1, 5
507 if i == 3
508 break i, i * 2
509 table.insert results, i
510 assert.same results, {1, 2}
511
512 it "should break with multiple values in nested for loops", ->
513 x, y = for i = 1, 5
514 a, b = for j = 1, 5
515 if i == 2 and j == 3
516 break i, j
517 break a, b if a
518 assert.same x, 2
519 assert.same y, 3
520
521 it "should break with multiple values in for nested while", ->
522 x, y = for i = 1, 10
523 j = 1
524 a, b = while j < 10
525 if i == 3 and j == 5
526 break i, j
527 j += 1
528 break a, b if a and b
529 assert.same x, 3
530 assert.same y, 5
531
532 it "should break with multiple values in while nested for", ->
533 i = 1
534 x, y = while i < 10
535 a, b = for j = 1, 10
536 if i == 3 and j == 4
537 break i, j
538 i += 1
539 break a, b if a and b
540 assert.same x, 3
541 assert.same y, 4
542
543 it "should break with multiple values in for nested repeat", ->
544 x, y = for i = 1, 5
545 j = 1
546 a, b = repeat
547 if i == 2 and j == 3
548 break i, j
549 j += 1
550 until j > 10
551 break a, b if a and b
552 assert.same x, 2
553 assert.same y, 3
554
555 it "should break with multiple values in repeat nested for", ->
556 i = 1
557 x, y = repeat
558 a, b = for j = 1, 5
559 if i == 3 and j == 2
560 break i, j
561 i += 1
562 break a, b if a and b
563 until i > 10
564 assert.same x, 3
565 assert.same y, 2
566
567 it "should break with multiple values in do nested for", ->
568 x, y = do
569 a1, b1 = for i = 1, 5
570 a, b = for j = 1, 5
571 if i == 2 and j == 3
572 break i, j
573 break a, b if a and b
574 break a1, b1
575 assert.same x, 2
576 assert.same y, 3
577
578 it "should break with multiple values in for nested do", ->
579 x, y = for i = 1, 5
580 do
581 if i == 3
582 break i, i * 2
583 assert.same x, 3
584 assert.same y, 6
585
586 it "should break with multiple values in for nested with", ->
587 x, y = for i = 1, 5
588 with value: i, index: i * 2
589 if .value == 3
590 break .value, .index
591 assert.same x, 3
592 assert.same y, 6
593
594 it "should break with a value in with nested for", ->
595 x = with {limit: 5}
596 break for i = 1, .limit
597 if i == 3
598 break i
599 assert.same x, 3
600
601 it "should break with multiple values in three-level nested loops", ->
602 x, y, z = for i = 1, 3
603 a1, a2, a3 = for j = 1, 3
604 b1, b2, b3 = for k = 1, 3
605 if i == 2 and j == 2 and k == 2
606 break i, j, k
607 break b1, b2, b3 if b1
608 break a1, a2, a3 if a1
609 assert.same x, 2
610 assert.same y, 2
611 assert.same z, 2
612
613 it "should break with multiple values in while nested repeat", ->
614 i = 1
615 x, y = while i < 10
616 j = 1
617 a, b = repeat
618 if i == 3 and j == 4
619 break i, j
620 j += 1
621 until j > 10
622 i += 1
623 break a, b if a
624 assert.same x, 3
625 assert.same y, 4
626
627 it "should break with multiple values in repeat nested while", ->
628 i = 1
629 x, y = repeat
630 j = 1
631 a, b = while j < 10
632 if i == 4 and j == 3
633 break i, j
634 j += 1
635 i += 1
636 break a, b if a
637 until i > 10
638 assert.same x, 4
639 assert.same y, 3
640
641 it "should break with multiple values in do nested while", ->
642 i = 1
643 x, y = do
644 a, b = while i < 10
645 if i == 4
646 break i, i * 3
647 i += 1
648 break a, b
649 assert.same x, 4
650 assert.same y, 12
651
652 it "should break with multiple values in while nested do", ->
653 i = 1
654 x, y = while i < 10
655 a, b = do
656 if i == 5
657 break i, i * 4
658 i += 1
659 break a, b if a
660 assert.same x, 5
661 assert.same y, 20
662
663 it "should break with multiple values in do nested repeat", ->
664 i = 1
665 x, y = do
666 a, b = repeat
667 if i == 3
668 break i, i * 5
669 i += 1
670 until i > 10
671 break a, b
672 assert.same x, 3
673 assert.same y, 15
674
675 it "should break with multiple values in repeat nested do", ->
676 i = 1
677 x, y = repeat
678 a, b = do
679 if i == 2
680 break i, i * 6
681 i += 1
682 break a, b if a
683 until i > 10
684 assert.same x, 2
685 assert.same y, 12
686
687 it "should break with multiple values in with nested do", ->
688 x, y = do
689 with value: 10, factor: 3
690 a, b = do
691 if .value > 5
692 break .value, .factor
693 break a, b
694 assert.same x, 10
695 assert.same y, 3
696
697 it "should break with multiple values in do nested with", ->
698 x, y = do
699 with num: 42, text: "test"
700 break .num, .text
701 assert.same x, 42
702 assert.same y, "test"
703
704 it "should break with multiple values in for nested for with different types", ->
705 name, age, active = for i = 1, 5
706 a, b, c = for j = 1, 5
707 if i == 2 and j == 3
708 break "user-#{i}", j * 10, true
709 break a, b, c if a
710 assert.same name, "user-2"
711 assert.same age, 30
712 assert.is_true active
713
714 it "should break with multiple values in with nested for with method calls", ->
715 obj =
716 data: [10, 20, 30, 40]
717 find: =>
718 x, y = for i = 1, #@data
719 if @data[i] == 30
720 break @data[i], i
721 x, y
722 value, index = obj\find!
723 assert.same value, 30
724 assert.same index, 3
725
726 it "should break with multiple values in nested loops with early termination", ->
727 found, idx = for i = 1, 10
728 if i == 1
729 break i, true
730 a, b = for j = 1, 10
731 break i, false if j == 5
732 break a, b if a
733 assert.same found, 1
734 assert.is_true idx
735
736 it "should break with multiple values in nested loops with computed expressions", ->
737 sum, product = for i = 1, 5
738 a, b = for j = 1, 5
739 if i * j == 12
740 break i + j, i * j
741 break a, b if a
742 assert.same sum, 7
743 assert.same product, 12
744
745 it "should mix break and break with values in same for expression", ->
746 x, y = for i = 1, 5
747 if i == 3
748 break i, i * 10
749 if i == 4
750 break
751 assert.same x, 3
752 assert.same y, 30
753
754 it "should return nils when plain break happens before value break", ->
755 x, y = for i = 1, 5
756 if i == 2
757 break
758 if i == 4
759 break i, i * 10
760 assert.is_nil x
761 assert.is_nil y
762
763 it "should mix plain break and value break across nested loops", ->
764 matrix = {
765 {1, -1, 9}
766 {4, 5, 9}
767 }
768 row, col = for r = 1, #matrix
769 a, b = for c = 1, #matrix[r]
770 cell = matrix[r][c]
771 break if cell < 0
772 break r, c if cell == 9
773 break a, b if a
774 assert.same row, 2
775 assert.same col, 3
776
777 it "should mix break and break with values in while expression", ->
778 i = 0
779 x, y = while i < 5
780 i += 1
781 if i == 2
782 break
783 if i == 4
784 break i, i * 2
785 assert.is_nil x
786 assert.is_nil y
787
788 it "should mix break and break with values in do with with block", ->
789 x, y = do
790 with value: 8, stop: false
791 break if .stop
792 break .value, .value * 2
793 assert.same x, 8
794 assert.same y, 16
795
796 it "should fall back to plain break in do with with block", ->
797 x, y = do
798 with value: 8, stop: true
799 break if .stop
800 break .value, .value * 2
801 break
802 assert.is_nil x
803 assert.is_nil y
804
805 it "should mix continue and break with values in for expression", ->
806 x, y = for i = 1, 8
807 continue if i % 3 ~= 0
808 break i, i + 100 if i == 6
809 assert.same x, 6
810 assert.same y, 106
811
812 it "should mix continue and break with values across nested for expressions", ->
813 row, col = for r = 1, 3
814 a, b = for c = 1, 4
815 continue if c < 3
816 break r, c if r == 2 and c == 3
817 continue if not a
818 break a, b
819 assert.same row, 2
820 assert.same col, 3
821
822 it "should mix break continue and value break with plain break winning", ->
823 x, y = for i = 1, 7
824 continue if i < 3
825 if i == 4
826 break
827 if i == 6
828 break i, i * 10
829 assert.is_nil x
830 assert.is_nil y
831
832 it "should mix break continue and value break with value break winning", ->
833 x, y = for i = 1, 9
834 continue if i % 2 == 0
835 if i == 5
836 break i, i * 3
837 if i == 9
838 break
839 assert.same x, 5
840 assert.same y, 15
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 @@
1local _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
11end
12local _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
21end
22local _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
31end
32local _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
41end
42local _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
53end
54return 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)
1697end)