diff --git a/src/cron/expression.v b/src/cron/expression.v index 71ee9a10..60e1b747 100644 --- a/src/cron/expression.v +++ b/src/cron/expression.v @@ -147,6 +147,18 @@ fn parse_range(s string, min int, max int, mut bitv []bool) ? { } } +fn bitv_to_ints(bitv []bool, min int) []int { + mut out := []int{} + + for i in 0..bitv.len { + if bitv[i] { + out << min + i + } + } + + return out +} + fn parse_part(s string, min int, max int) ?[]int { mut bitv := []bool{init: false, len: max - min + 1} @@ -154,15 +166,7 @@ fn parse_part(s string, min int, max int) ?[]int { parse_range(range, min, max, mut bitv) ? } - mut out := []int{} - - for i in 0..max + 1 { - if bitv[i] { - out << min + i - } - } - - return out + return bitv_to_ints(bitv, min) } // min hour day month day-of-week diff --git a/src/cron/expression_parse_test.v b/src/cron/expression_parse_test.v new file mode 100644 index 00000000..7a4974dd --- /dev/null +++ b/src/cron/expression_parse_test.v @@ -0,0 +1,65 @@ +module cron + +// parse_range_error returns the returned error message. If the result is '', +// that means the function didn't error. +fn parse_range_error(s string, min int, max int) string { + mut bitv := []bool{init: false, len: max - min + 1} + + parse_range(s, min, max, mut bitv) or { + return err.msg + } + + return '' +} + +// =====parse_range===== +fn test_parse_star_range() ? { + mut bitv := []bool{init: false, len: 6} + parse_range('*', 0, 5, mut bitv) ? + + assert bitv == [true, true, true, true, true, true] +} + +fn test_parse_number() ? { + mut bitv := []bool{init: false, len: 6} + parse_range('4', 0, 5, mut bitv) ? + + assert bitv_to_ints(bitv, 0) == [4] +} + +fn test_parse_number_too_large() ? { + assert parse_range_error('10', 0, 6) == 'Out of range.' +} + +fn test_parse_number_too_small() ? { + assert parse_range_error('0', 2, 6) == 'Out of range.' +} + +fn test_parse_step_star() ? { + mut bitv := []bool{init: false, len: 21} + parse_range('*/4', 0, 20, mut bitv) ? + + assert bitv_to_ints(bitv, 0) == [0, 4, 8, 12, 16, 20] +} + +fn test_parse_step_star_too_large() ? { + assert parse_range_error('*/21', 0, 20) == 'Step too large.' +} + +fn test_parse_step_zero() ? { + assert parse_range_error('*/0', 0, 20) == 'Step size zero not allowed.' +} + +fn test_parse_step_number() ? { + mut bitv := []bool{init: false, len: 21} + parse_range('5/4', 0, 20, mut bitv) ? + assert bitv_to_ints(bitv, 0) == [5, 9, 13, 17] +} + +fn test_parse_step_number_too_large() ? { + assert parse_range_error('10/4', 0, 5) == 'Out of range.' +} + +fn test_parse_step_number_too_small() ? { + assert parse_range_error('2/4', 5, 10) == 'Out of range.' +} diff --git a/src/cron/expression_test.v b/src/cron/expression_test.v deleted file mode 100644 index 2d58b15c..00000000 --- a/src/cron/expression_test.v +++ /dev/null @@ -1,42 +0,0 @@ -module cron - -// =====parse_range===== -fn test_parse_star_range() ? { - assert parse_range('*', 0, 5) ? == [0, 1, 2, 3, 4, 5] -} - -fn test_parse_number() ? { - assert parse_range('4', 0, 5) ? == [4] -} - -fn test_parse_number_too_large() ? { - assert parse_range('10', 0, 6) ? == [6] -} - -fn test_parse_number_too_small() ? { - assert parse_range('0', 2, 6) ? == [2] -} - -fn test_parse_step_star() ? { - assert parse_range('*/4', 0, 20) ? == [0, 4, 8, 12, 16, 20] -} - -fn test_parse_step_star_too_large() ? { - assert parse_range('*/21', 0, 20) ? == [0] -} - -fn test_parse_step_zero() ? { - assert parse_range('*/0', 0, 20) ? == [] -} - -fn test_parse_step_number() ? { - assert parse_range('5/4', 0, 20) ? == [5, 9, 13, 17] -} - -fn test_parse_step_number_too_large() ? { - assert parse_range('10/4', 0, 5) ? == [5] -} - -fn test_parse_step_number_too_small() ? { - assert parse_range('2/4', 5, 10) ? == [5, 9] -}