From 093e50351a4e39a95ec36da9e3d9316ce0b0c130 Mon Sep 17 00:00:00 2001 From: "eric.cy.huang" Date: Sat, 28 Dec 2024 02:54:31 +0800 Subject: [PATCH] Add more testcases --- src/stdlib/parse_duration.rs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/stdlib/parse_duration.rs b/src/stdlib/parse_duration.rs index 84dc3b53f..631627428 100644 --- a/src/stdlib/parse_duration.rs +++ b/src/stdlib/parse_duration.rs @@ -41,7 +41,6 @@ fn parse_duration(bytes: Value, unit: Value) -> Resolved { static RE: Lazy = Lazy::new(|| { Regex::new( r"(?ix) # i: case-insensitive, x: ignore whitespace + comments - \A (?P[0-9]*\.?[0-9]+) # value: integer or float \s? # optional space between value and unit (?P[µa-z]{1,2}) # unit: one or two letters", @@ -179,16 +178,25 @@ mod tests { want: Ok(86401.0), tdef: TypeDef::float().fallible(), } - error_multiple_units_space_not_allowed { - args: func_args![value: "1d 1s", - unit: "s"], - want: Err("unable to parse duration: ' 1s'"), + + s_space_ms_ms { + args: func_args![value: "1s 1ms", + unit: "ms"], + want: Ok(1001.0), tdef: TypeDef::float().fallible(), } - error_multiple_units { - args: func_args![value: "1d foo", - unit: "s"], - want: Err("unable to parse duration: ' foo'"), + + ms_space_us_ms { + args: func_args![value: "1ms1 µs", + unit: "ms"], + want: Ok(1.001), + tdef: TypeDef::float().fallible(), + } + + s_space_m_ms_order_agnostic { + args: func_args![value: "1s1m", + unit: "ms"], + want: Ok(61000.0), tdef: TypeDef::float().fallible(), } @@ -233,5 +241,12 @@ mod tests { want: Err("unknown unit format: 'w'"), tdef: TypeDef::float().fallible(), } + + error_failed_2nd_unit { + args: func_args![value: "1d foo", + unit: "s"], + want: Err("unable to parse duration: ' foo'"), + tdef: TypeDef::float().fallible(), + } ]; }