tests: add tests for continue/break in last statement of an `or{}`

pull/6713/head
Delyan Angelov 2020-11-02 12:54:45 +02:00
parent 4e760c703e
commit 2202ee5d66
1 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,36 @@
fn opt_0_10_20(x int) ?int {
if x < 0 || (x >= 10 && x <= 20) {
return error('invalid')
}
return x
}
fn test_options_in_for_loop_break() {
mut sum := 0
mut nbreaks := 0
for i := 5; i < 15; i++ {
x := opt_0_10_20(i) or {
nbreaks++
break
}
sum += x
// println('i: ${i:3} | sum: ${sum:3}')
}
assert nbreaks == 1
assert sum == 35
}
fn test_options_in_for_loop_continue() {
mut sum := 0
mut ncontinue := 0
for i := -5; i < 30; i++ {
x := opt_0_10_20(i) or {
ncontinue++
continue
}
sum += x
// println('i: ${i:3} | sum: ${sum:3}')
}
assert ncontinue == 16
assert sum == 270
}