diff options
Diffstat (limited to '')
| -rw-r--r-- | spec/inputs/test/while_assignment_spec.yue | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/spec/inputs/test/while_assignment_spec.yue b/spec/inputs/test/while_assignment_spec.yue new file mode 100644 index 0000000..1c98e58 --- /dev/null +++ b/spec/inputs/test/while_assignment_spec.yue | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | describe "while assignment", -> | ||
| 2 | it "should loop while value is truthy", -> | ||
| 3 | counter = 0 | ||
| 4 | get_next = -> | ||
| 5 | if counter < 3 | ||
| 6 | counter += 1 | ||
| 7 | counter | ||
| 8 | else | ||
| 9 | nil | ||
| 10 | results = {} | ||
| 11 | while val := get_next! | ||
| 12 | table.insert results, val | ||
| 13 | assert.same results, {1, 2, 3} | ||
| 14 | |||
| 15 | it "should work with function results", -> | ||
| 16 | counter = 0 | ||
| 17 | fn = -> | ||
| 18 | counter += 1 | ||
| 19 | if counter <= 3 | ||
| 20 | counter * 10 | ||
| 21 | else | ||
| 22 | nil | ||
| 23 | |||
| 24 | sum = 0 | ||
| 25 | while val := fn! | ||
| 26 | sum += val | ||
| 27 | assert.same sum, 60 -- (10+20+30) | ||
| 28 | |||
| 29 | it "should exit immediately on nil", -> | ||
| 30 | get_val = -> nil | ||
| 31 | counter = 0 | ||
| 32 | while val := get_val! | ||
| 33 | counter += 1 | ||
| 34 | assert.same counter, 0 | ||
| 35 | |||
| 36 | it "should support break in loop", -> | ||
| 37 | items = {1, 2, 3, 4, 5} | ||
| 38 | sum = 0 | ||
| 39 | for item in *items | ||
| 40 | sum += item | ||
| 41 | break if sum > 6 | ||
| 42 | assert.same sum, 10 | ||
