toml-rs/test-suite/tests/datetime.rs

135 lines
3.8 KiB
Rust
Raw Normal View History

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),
}
};
}
#[test]
fn times() {
fn dogood(s: &str, serialized: &str) {
let to_parse = format!("foo = {}", s);
2019-07-28 11:56:22 -05:00
let value = toml::Value::from_str(&to_parse).unwrap();
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);
}
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() {
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",
"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.",
"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",
"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
);
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
);
}