diff --git a/src/cron/expression.v b/src/cron/expression.v index 2938dd9..8781900 100644 --- a/src/cron/expression.v +++ b/src/cron/expression.v @@ -17,17 +17,15 @@ fn parse_range(s string, min u32, max u32) ?[]u32 { if s != '*' { exps := s.split('/') + start = math.min(max, math.max(exps[0].u32(), min)) + if exps.len > 1 { interval = exps[1].u32() } // Here, s solely consists of a number, so that's the only value we // should return. else { - return [exps[0].u32()] - } - - if exps[0] != '*' { - start = math.max(exps[0].u32(), min) + return [start] } } diff --git a/src/cron/expression_test.v b/src/cron/expression_test.v index 562ced2..6d293c5 100644 --- a/src/cron/expression_test.v +++ b/src/cron/expression_test.v @@ -7,3 +7,31 @@ fn test_parse_star_range() ? { fn test_parse_number() ? { assert parse_range('4', 0, 5) ? == [u32(4)] } + +fn test_parse_number_too_large() ? { + assert parse_range('10', 0, 6) ? == [u32(6)] +} + +fn test_parse_number_too_small() ? { + assert parse_range('0', 2, 6) ? == [u32(2)] +} + +fn test_parse_step_star() ? { + assert parse_range('*/4', 0, 20) ? == [u32(0), 4, 8, 12, 16, 20] +} + +fn test_parse_step_star_too_large() ? { + assert parse_range('*/21', 0, 20) ? == [u32(0)] +} + +fn test_parse_step_number() ? { + assert parse_range('5/4', 0, 20) ? == [u32(5), 9, 13, 17] +} + +fn test_parse_step_number_too_large() ? { + assert parse_range('10/4', 0, 5) ? == [u32(5)] +} + +fn test_parse_step_number_too_small() ? { + assert parse_range('2/4', 5, 10) ? == [u32(5), 9] +}