use snapbox::assert_data_eq; use snapbox::prelude::*; use snapbox::str; macro_rules! bad { ($toml:expr, $msg:expr) => { match $toml.parse::() { Ok(s) => panic!("parsed to: {:#?}", s), Err(e) => assert_data_eq!(e.to_string(), $msg.raw()), } }; } #[test] fn times() { fn dogood(s: &str, serialized: &str) { let to_parse = format!("foo = {}", s); let document = to_parse.parse::().unwrap(); assert_eq!( document["foo"].as_datetime().unwrap().to_string(), serialized ); } fn good(s: &str) { dogood(s, s); dogood(&s.replace('T', " "), s); dogood(&s.replace('T', "t"), s); dogood(&s.replace('Z', "z"), s); } good("1997-09-09T09:09:09Z"); good("1997-09-09T09:09:09+09:09"); good("1997-09-09T09:09:09-09:09"); good("1997-09-09T09:09:09"); good("1997-09-09"); dogood("1997-09-09 ", "1997-09-09"); dogood("1997-09-09 # comment", "1997-09-09"); good("09:09:09"); good("1997-09-09T09:09:09.09Z"); good("1997-09-09T09:09:09.09+09:09"); good("1997-09-09T09:09:09.09-09:09"); good("1997-09-09T09:09:09.09"); good("09:09:09.09"); } #[test] fn bad_times() { bad!( "foo = 199-09-09", str![[r#" TOML parse error at line 1, column 10 | 1 | foo = 199-09-09 | ^ expected newline, `#` "#]] ); bad!( "foo = 199709-09", str![[r#" TOML parse error at line 1, column 13 | 1 | foo = 199709-09 | ^ expected newline, `#` "#]] ); bad!( "foo = 1997-9-09", str![[r#" TOML parse error at line 1, column 12 | 1 | foo = 1997-9-09 | ^ invalid date-time "#]] ); bad!( "foo = 1997-09-9", str![[r#" TOML parse error at line 1, column 15 | 1 | foo = 1997-09-9 | ^ invalid date-time "#]] ); bad!( "foo = 1997-09-0909:09:09", str![[r#" TOML parse error at line 1, column 17 | 1 | foo = 1997-09-0909:09:09 | ^ expected newline, `#` "#]] ); bad!( "foo = 1997-09-09T09:09:09.", str![[r#" TOML parse error at line 1, column 26 | 1 | foo = 1997-09-09T09:09:09. | ^ expected newline, `#` "#]] ); bad!( "foo = T", str![[r#" TOML parse error at line 1, column 7 | 1 | foo = T | ^ invalid string expected `"`, `'` "#]] ); bad!( "foo = T.", str![[r#" TOML parse error at line 1, column 7 | 1 | foo = T. | ^ invalid string expected `"`, `'` "#]] ); bad!( "foo = TZ", str![[r#" TOML parse error at line 1, column 7 | 1 | foo = TZ | ^ invalid string expected `"`, `'` "#]] ); bad!( "foo = 1997-09-09T09:09:09.09+", str![[r#" TOML parse error at line 1, column 30 | 1 | foo = 1997-09-09T09:09:09.09+ | ^ invalid time offset "#]] ); bad!( "foo = 1997-09-09T09:09:09.09+09", str![[r#" TOML parse error at line 1, column 32 | 1 | foo = 1997-09-09T09:09:09.09+09 | ^ invalid time offset "#]] ); bad!( "foo = 1997-09-09T09:09:09.09+09:9", str![[r#" TOML parse error at line 1, column 33 | 1 | foo = 1997-09-09T09:09:09.09+09:9 | ^ invalid time offset "#]] ); bad!( "foo = 1997-09-09T09:09:09.09+0909", str![[r#" TOML parse error at line 1, column 32 | 1 | foo = 1997-09-09T09:09:09.09+0909 | ^ invalid time offset "#]] ); bad!( "foo = 1997-09-09T09:09:09.09-", str![[r#" TOML parse error at line 1, column 30 | 1 | foo = 1997-09-09T09:09:09.09- | ^ invalid time offset "#]] ); bad!( "foo = 1997-09-09T09:09:09.09-09", str![[r#" TOML parse error at line 1, column 32 | 1 | foo = 1997-09-09T09:09:09.09-09 | ^ invalid time offset "#]] ); bad!( "foo = 1997-09-09T09:09:09.09-09:9", str![[r#" TOML parse error at line 1, column 33 | 1 | foo = 1997-09-09T09:09:09.09-09:9 | ^ invalid time offset "#]] ); bad!( "foo = 1997-09-09T09:09:09.09-0909", str![[r#" TOML parse error at line 1, column 32 | 1 | foo = 1997-09-09T09:09:09.09-0909 | ^ invalid time offset "#]] ); bad!( "foo = 1997-00-09T09:09:09.09Z", str![[r#" TOML parse error at line 1, column 12 | 1 | foo = 1997-00-09T09:09:09.09Z | ^ invalid date-time value is out of range "#]] ); bad!( "foo = 1997-09-00T09:09:09.09Z", str![[r#" TOML parse error at line 1, column 15 | 1 | foo = 1997-09-00T09:09:09.09Z | ^ invalid date-time value is out of range "#]] ); bad!( "foo = 1997-09-09T30:09:09.09Z", str![[r#" TOML parse error at line 1, column 17 | 1 | foo = 1997-09-09T30:09:09.09Z | ^ expected newline, `#` "#]] ); bad!( "foo = 1997-09-09T12:69:09.09Z", str![[r#" TOML parse error at line 1, column 21 | 1 | foo = 1997-09-09T12:69:09.09Z | ^ invalid date-time value is out of range "#]] ); bad!( "foo = 1997-09-09T12:09:69.09Z", str![[r#" TOML parse error at line 1, column 24 | 1 | foo = 1997-09-09T12:09:69.09Z | ^ invalid date-time value is out of range "#]] ); }