2017-02-08 23:36:38 -06:00
|
|
|
extern crate serde;
|
2019-05-08 19:45:39 -05:00
|
|
|
extern crate toml;
|
2017-02-08 23:36:38 -06:00
|
|
|
|
|
|
|
use serde::de::Deserialize;
|
|
|
|
|
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-02-08 23:36:38 -06:00
|
|
|
#[test]
|
2019-01-07 11:06:04 -06:00
|
|
|
fn newlines_after_tables() {
|
2017-03-31 20:45:00 -05:00
|
|
|
let s = "
|
|
|
|
[a] foo = 1
|
|
|
|
[[b]] foo = 1
|
|
|
|
";
|
2019-07-28 12:40:49 -05:00
|
|
|
bad!(
|
|
|
|
s,
|
|
|
|
"expected newline, found an identifier at line 2 column 13"
|
|
|
|
);
|
2017-02-08 23:36:38 -06:00
|
|
|
|
2017-03-31 20:45:00 -05:00
|
|
|
let mut d = toml::de::Deserializer::new(s);
|
2017-02-08 23:36:38 -06:00
|
|
|
d.set_require_newline_after_table(false);
|
|
|
|
let value = toml::Value::deserialize(&mut d).unwrap();
|
|
|
|
assert_eq!(value["a"]["foo"].as_integer(), Some(1));
|
2017-03-31 20:45:00 -05:00
|
|
|
assert_eq!(value["b"][0]["foo"].as_integer(), Some(1));
|
2017-02-08 23:36:38 -06:00
|
|
|
}
|
2019-01-07 11:06:04 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn allow_duplicate_after_longer() {
|
|
|
|
let s = "
|
|
|
|
[dependencies.openssl-sys]
|
|
|
|
version = 1
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
libc = 1
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
bitflags = 1
|
|
|
|
";
|
2019-07-28 11:56:22 -05:00
|
|
|
bad!(
|
|
|
|
s,
|
2019-07-28 12:40:49 -05:00
|
|
|
"redefinition of table `dependencies` for key `dependencies` at line 8 column 9"
|
2019-07-28 11:56:22 -05:00
|
|
|
);
|
2019-01-07 11:06:04 -06:00
|
|
|
|
|
|
|
let mut d = toml::de::Deserializer::new(s);
|
|
|
|
d.set_allow_duplicate_after_longer_table(true);
|
|
|
|
let value = toml::Value::deserialize(&mut d).unwrap();
|
2019-05-08 19:45:39 -05:00
|
|
|
assert_eq!(
|
|
|
|
value["dependencies"]["openssl-sys"]["version"].as_integer(),
|
|
|
|
Some(1)
|
|
|
|
);
|
2019-01-07 11:06:04 -06:00
|
|
|
assert_eq!(value["dependencies"]["libc"].as_integer(), Some(1));
|
|
|
|
assert_eq!(value["dependencies"]["bitflags"].as_integer(), Some(1));
|
|
|
|
}
|