From 2202ee5d66974e3a18c7b66db6d7d798b09c7fa1 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 2 Nov 2020 12:54:45 +0200 Subject: [PATCH] tests: add tests for continue/break in last statement of an `or{}` --- vlib/v/tests/option_in_loop_test.v | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 vlib/v/tests/option_in_loop_test.v diff --git a/vlib/v/tests/option_in_loop_test.v b/vlib/v/tests/option_in_loop_test.v new file mode 100644 index 0000000000..733c39046b --- /dev/null +++ b/vlib/v/tests/option_in_loop_test.v @@ -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 +}