@@ -30,7 +30,7 @@ pub fn clear_screen() {
30
30
pub fn parse_delay ( s : Option < & str > , t : & str ) -> crate :: Result < Option < Duration > > {
31
31
if let Some ( d) = s. map ( parse_duration) {
32
32
let d =
33
- d. with_context ( || format ! ( "{} had an valid format, allowed is 0ms < XXs <= 5m" , t) ) ?;
33
+ d. with_context ( || format ! ( "{} had an invalid format, allowed is 0ms < XXs <= 5m" , t) ) ?;
34
34
if d > MAX_DELAY {
35
35
anyhow:: bail!( "{} was out of range, allowed is 0ms < XXs <= 5m" , t)
36
36
} else {
@@ -58,4 +58,28 @@ mod tests {
58
58
assert_eq ! ( Duration :: from_micros( 10 ) . as_human_readable( ) , "10us" ) ;
59
59
assert_eq ! ( Duration :: from_nanos( 10 ) . as_human_readable( ) , "10ns" ) ;
60
60
}
61
+
62
+ #[ test]
63
+ fn should_parse_time ( ) -> crate :: Result < ( ) > {
64
+ let s = parse_delay ( Some ( "1m" ) , "foo" ) ?. unwrap ( ) ;
65
+ assert_eq ! ( s, Duration :: from_secs( 60 ) ) ;
66
+ let s = parse_delay ( Some ( "60s" ) , "foo" ) ?. unwrap ( ) ;
67
+ assert_eq ! ( s, Duration :: from_secs( 60 ) ) ;
68
+ let s = parse_delay ( Some ( "500ms" ) , "foo" ) ?. unwrap ( ) ;
69
+ assert_eq ! ( s, Duration :: from_millis( 500 ) ) ;
70
+
71
+ Ok ( ( ) )
72
+ }
73
+
74
+ #[ test]
75
+ #[ should_panic( expected = "foo was out of range, allowed is 0ms < XXs <= 5m" ) ]
76
+ fn should_not_parse_time_that_is_too_long ( ) {
77
+ parse_delay ( Some ( "5m 1s" ) , "foo" ) . unwrap ( ) ;
78
+ }
79
+
80
+ #[ test]
81
+ #[ should_panic( expected = "time unit needed, for example 1sec or 1ms" ) ]
82
+ fn should_not_parse_time_that_is_invalid ( ) {
83
+ parse_delay ( Some ( "1" ) , "foo" ) . unwrap ( ) ;
84
+ }
61
85
}
0 commit comments