Skip to content

Commit

Permalink
Add more testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
titaneric committed Dec 27, 2024
1 parent 41ba73b commit 093e503
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/stdlib/parse_duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ fn parse_duration(bytes: Value, unit: Value) -> Resolved {
static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?ix) # i: case-insensitive, x: ignore whitespace + comments
\A
(?P<value>[0-9]*\.?[0-9]+) # value: integer or float
\s? # optional space between value and unit
(?P<unit>[µa-z]{1,2}) # unit: one or two letters",
Expand Down Expand Up @@ -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(),
}

Expand Down Expand Up @@ -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(),
}
];
}

0 comments on commit 093e503

Please sign in to comment.