2017-01-29 18:53:20 -06:00
|
|
|
extern crate toml;
|
|
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2019-07-28 11:56:22 -05:00
|
|
|
macro_rules! bad {
|
|
|
|
($toml:expr, $msg:expr) => {
|
|
|
|
match $toml.parse::<toml::Value>() {
|
|
|
|
Ok(s) => panic!("parsed to: {:#?}", s),
|
|
|
|
Err(e) => assert_eq!(e.to_string(), $msg),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn times() {
|
2018-07-10 19:14:16 -05:00
|
|
|
fn dogood(s: &str, serialized: &str) {
|
2017-01-29 18:53:20 -06:00
|
|
|
let to_parse = format!("foo = {}", s);
|
2019-07-28 11:56:22 -05:00
|
|
|
let value = toml::Value::from_str(&to_parse).unwrap();
|
2018-07-10 19:14:16 -05:00
|
|
|
assert_eq!(value["foo"].as_datetime().unwrap().to_string(), serialized);
|
|
|
|
}
|
|
|
|
fn good(s: &str) {
|
|
|
|
dogood(s, s);
|
|
|
|
dogood(&s.replace("T", " "), s);
|
2019-02-19 12:59:42 -06:00
|
|
|
dogood(&s.replace("T", "t"), s);
|
|
|
|
dogood(&s.replace("Z", "z"), s);
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2018-11-21 11:35:50 -06:00
|
|
|
dogood("1997-09-09 ", "1997-09-09");
|
|
|
|
dogood("1997-09-09 # comment", "1997-09-09");
|
2017-01-29 18:53:20 -06:00
|
|
|
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() {
|
2019-07-30 12:20:18 -05:00
|
|
|
bad!(
|
|
|
|
"foo = 199-09-09",
|
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 199709-09",
|
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-9-09",
|
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-9",
|
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
|
|
|
);
|
2019-07-28 11:56:22 -05:00
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-0909:09:09",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
2019-07-28 12:40:49 -05:00
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T09:09:09.",
|
|
|
|
"invalid date at line 1 column 7"
|
|
|
|
);
|
2019-07-30 12:20:18 -05:00
|
|
|
bad!(
|
|
|
|
"foo = T",
|
2020-10-11 12:30:55 -05:00
|
|
|
"invalid TOML value, did you mean to use a quoted string? at line 1 column 7"
|
2019-07-30 12:20:18 -05:00
|
|
|
);
|
2019-07-28 12:40:49 -05:00
|
|
|
bad!(
|
|
|
|
"foo = T.",
|
2020-10-11 12:30:55 -05:00
|
|
|
"invalid TOML value, did you mean to use a quoted string? at line 1 column 7"
|
2019-07-28 12:40:49 -05:00
|
|
|
);
|
2019-07-30 12:20:18 -05:00
|
|
|
bad!(
|
|
|
|
"foo = TZ",
|
2020-10-11 12:30:55 -05:00
|
|
|
"invalid TOML value, did you mean to use a quoted string? at line 1 column 7"
|
2019-07-30 12:20:18 -05:00
|
|
|
);
|
2019-07-28 12:40:49 -05:00
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T09:09:09.09+",
|
|
|
|
"invalid date at line 1 column 7"
|
|
|
|
);
|
2019-07-28 11:56:22 -05:00
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T09:09:09.09+09",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T09:09:09.09+09:9",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T09:09:09.09+0909",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T09:09:09.09-",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T09:09:09.09-09",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T09:09:09.09-09:9",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T09:09:09.09-0909",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
2017-01-29 18:53:20 -06:00
|
|
|
|
2019-07-28 11:56:22 -05:00
|
|
|
bad!(
|
|
|
|
"foo = 1997-00-09T09:09:09.09Z",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-00T09:09:09.09Z",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T30:09:09.09Z",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T12:69:09.09Z",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
|
|
|
bad!(
|
|
|
|
"foo = 1997-09-09T12:09:69.09Z",
|
2019-07-30 12:20:18 -05:00
|
|
|
"failed to parse datetime for key `foo` at line 1 column 7"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|