2018-05-06 20:47:09 -05:00
|
|
|
extern crate serde;
|
|
|
|
extern crate toml;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2019-09-05 08:38:54 -05:00
|
|
|
use std::fmt::Debug;
|
2019-05-08 19:45:39 -05:00
|
|
|
use toml::value::Datetime;
|
|
|
|
use toml::Spanned;
|
2018-05-06 20:47:09 -05:00
|
|
|
|
2018-05-06 22:36:09 -05:00
|
|
|
/// A set of good datetimes.
|
|
|
|
pub fn good_datetimes() -> Vec<&'static str> {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
v.push("1997-09-09T09:09:09Z");
|
|
|
|
v.push("1997-09-09T09:09:09+09:09");
|
|
|
|
v.push("1997-09-09T09:09:09-09:09");
|
|
|
|
v.push("1997-09-09T09:09:09");
|
|
|
|
v.push("1997-09-09");
|
|
|
|
v.push("09:09:09");
|
|
|
|
v.push("1997-09-09T09:09:09.09Z");
|
|
|
|
v.push("1997-09-09T09:09:09.09+09:09");
|
|
|
|
v.push("1997-09-09T09:09:09.09-09:09");
|
|
|
|
v.push("1997-09-09T09:09:09.09");
|
|
|
|
v.push("09:09:09.09");
|
|
|
|
v
|
|
|
|
}
|
|
|
|
|
2018-05-06 20:47:09 -05:00
|
|
|
#[test]
|
|
|
|
fn test_spanned_field() {
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Foo<T> {
|
|
|
|
foo: Spanned<T>,
|
|
|
|
}
|
|
|
|
|
2019-09-05 08:38:54 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct BareFoo<T> {
|
|
|
|
foo: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn good<'de, T>(s: &'de str, expected: &str, end: Option<usize>)
|
2019-05-08 19:45:39 -05:00
|
|
|
where
|
2019-09-05 08:38:54 -05:00
|
|
|
T: serde::Deserialize<'de> + Debug + PartialEq,
|
2019-05-08 19:45:39 -05:00
|
|
|
{
|
2018-05-06 20:47:09 -05:00
|
|
|
let foo: Foo<T> = toml::from_str(s).unwrap();
|
|
|
|
|
2018-05-09 14:26:59 -05:00
|
|
|
assert_eq!(6, foo.foo.start());
|
2019-09-05 08:38:54 -05:00
|
|
|
if let Some(end) = end {
|
|
|
|
assert_eq!(end, foo.foo.end());
|
|
|
|
} else {
|
|
|
|
assert_eq!(s.len(), foo.foo.end());
|
|
|
|
}
|
2018-05-09 14:26:59 -05:00
|
|
|
assert_eq!(expected, &s[foo.foo.start()..foo.foo.end()]);
|
2019-09-05 08:38:54 -05:00
|
|
|
|
|
|
|
// Test for Spanned<> at the top level
|
|
|
|
let foo_outer: Spanned<BareFoo<T>> = toml::from_str(s).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(0, foo_outer.start());
|
|
|
|
assert_eq!(s.len(), foo_outer.end());
|
|
|
|
assert_eq!(foo.foo.into_inner(), foo_outer.into_inner().foo);
|
2018-05-06 20:47:09 -05:00
|
|
|
}
|
|
|
|
|
2019-09-05 08:38:54 -05:00
|
|
|
good::<String>("foo = \"foo\"", "\"foo\"", None);
|
|
|
|
good::<u32>("foo = 42", "42", None);
|
2018-05-06 21:05:05 -05:00
|
|
|
// leading plus
|
2019-09-05 08:38:54 -05:00
|
|
|
good::<u32>("foo = +42", "+42", None);
|
2018-05-06 21:05:05 -05:00
|
|
|
// table
|
2018-05-06 20:47:09 -05:00
|
|
|
good::<HashMap<String, u32>>(
|
|
|
|
"foo = {\"foo\" = 42, \"bar\" = 42}",
|
2019-05-08 19:45:39 -05:00
|
|
|
"{\"foo\" = 42, \"bar\" = 42}",
|
2019-09-05 08:38:54 -05:00
|
|
|
None,
|
2018-05-06 20:47:09 -05:00
|
|
|
);
|
2018-05-06 21:05:05 -05:00
|
|
|
// array
|
2019-09-05 08:38:54 -05:00
|
|
|
good::<Vec<u32>>("foo = [0, 1, 2, 3, 4]", "[0, 1, 2, 3, 4]", None);
|
2018-05-06 21:05:05 -05:00
|
|
|
// datetime
|
2019-09-05 08:38:54 -05:00
|
|
|
good::<String>(
|
|
|
|
"foo = \"1997-09-09T09:09:09Z\"",
|
|
|
|
"\"1997-09-09T09:09:09Z\"",
|
|
|
|
None,
|
|
|
|
);
|
2018-05-06 22:36:09 -05:00
|
|
|
|
|
|
|
for expected in good_datetimes() {
|
|
|
|
let s = format!("foo = {}", expected);
|
2019-09-05 08:38:54 -05:00
|
|
|
good::<Datetime>(&s, expected, None);
|
2018-05-06 22:36:09 -05:00
|
|
|
}
|
2019-09-05 08:38:54 -05:00
|
|
|
// ending at something other than the absolute end
|
|
|
|
good::<u32>("foo = 42\nnoise = true", "42", Some(8));
|
2018-05-06 20:47:09 -05:00
|
|
|
}
|
2019-09-16 16:32:45 -05:00
|
|
|
|
|
|
|
#[test]
|
2019-10-28 09:01:23 -05:00
|
|
|
fn test_inner_spanned_table() {
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Foo {
|
|
|
|
foo: Spanned<HashMap<Spanned<String>, Spanned<String>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn good(s: &str, zero: bool) {
|
|
|
|
let foo: Foo = toml::from_str(s).unwrap();
|
|
|
|
|
|
|
|
if zero {
|
|
|
|
assert_eq!(foo.foo.start(), 0);
|
|
|
|
// We'd actually have to assert equality with s.len() here,
|
|
|
|
// but the current implementation doesn't support that,
|
|
|
|
// and it's not possible with toml's data format to support it
|
|
|
|
// in the general case as spans aren't always well-defined.
|
|
|
|
// So this check mainly serves as a reminder that this test should
|
|
|
|
// be updated *if* one day there is support for emitting the actual span.
|
|
|
|
assert_eq!(foo.foo.end(), 0);
|
|
|
|
} else {
|
|
|
|
assert_eq!(foo.foo.start(), s.find("{").unwrap());
|
|
|
|
assert_eq!(foo.foo.end(), s.find("}").unwrap() + 1);
|
|
|
|
}
|
|
|
|
for (k, v) in foo.foo.get_ref().iter() {
|
|
|
|
assert_eq!(&s[k.start()..k.end()], k.get_ref());
|
|
|
|
assert_eq!(&s[(v.start() + 1)..(v.end() - 1)], v.get_ref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
good(
|
|
|
|
"
|
|
|
|
[foo]
|
|
|
|
a = 'b'
|
|
|
|
bar = 'baz'
|
|
|
|
c = 'd'
|
|
|
|
e = \"f\"
|
|
|
|
",
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
good(
|
|
|
|
"
|
|
|
|
foo = { a = 'b', bar = 'baz', c = 'd', e = \"f\" }",
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_outer_spanned_table() {
|
2019-09-16 16:32:45 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Foo {
|
|
|
|
foo: HashMap<Spanned<String>, Spanned<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn good(s: &str) {
|
|
|
|
let foo: Foo = toml::from_str(s).unwrap();
|
|
|
|
|
|
|
|
for (k, v) in foo.foo.iter() {
|
|
|
|
assert_eq!(&s[k.start()..k.end()], k.get_ref());
|
|
|
|
assert_eq!(&s[(v.start() + 1)..(v.end() - 1)], v.get_ref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
good(
|
|
|
|
"
|
|
|
|
[foo]
|
|
|
|
a = 'b'
|
|
|
|
bar = 'baz'
|
|
|
|
c = 'd'
|
|
|
|
e = \"f\"
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
good(
|
|
|
|
"
|
|
|
|
foo = { a = 'b', bar = 'baz', c = 'd', e = \"f\" }
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_spanned_nested() {
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Foo {
|
|
|
|
foo: HashMap<Spanned<String>, HashMap<Spanned<String>, Spanned<String>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn good(s: &str) {
|
|
|
|
let foo: Foo = toml::from_str(s).unwrap();
|
|
|
|
|
|
|
|
for (k, v) in foo.foo.iter() {
|
|
|
|
assert_eq!(&s[k.start()..k.end()], k.get_ref());
|
|
|
|
for (n_k, n_v) in v.iter() {
|
|
|
|
assert_eq!(&s[n_k.start()..n_k.end()], n_k.get_ref());
|
|
|
|
assert_eq!(&s[(n_v.start() + 1)..(n_v.end() - 1)], n_v.get_ref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
good(
|
|
|
|
"
|
|
|
|
[foo.a]
|
|
|
|
a = 'b'
|
|
|
|
c = 'd'
|
|
|
|
e = \"f\"
|
|
|
|
[foo.bar]
|
|
|
|
baz = 'true'
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
good(
|
|
|
|
"
|
|
|
|
[foo]
|
|
|
|
foo = { a = 'b', bar = 'baz', c = 'd', e = \"f\" }
|
|
|
|
bazz = {}
|
|
|
|
g = { h = 'i' }
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-10-28 09:01:23 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_spanned_array() {
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Foo {
|
|
|
|
foo: Vec<Spanned<HashMap<Spanned<String>, Spanned<String>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn good(s: &str) {
|
|
|
|
let foo_list: Foo = toml::from_str(s).unwrap();
|
|
|
|
|
|
|
|
for foo in foo_list.foo.iter() {
|
|
|
|
assert_eq!(foo.start(), 0);
|
|
|
|
// We'd actually have to assert equality with s.len() here,
|
|
|
|
// but the current implementation doesn't support that,
|
|
|
|
// and it's not possible with toml's data format to support it
|
|
|
|
// in the general case as spans aren't always well-defined.
|
|
|
|
// So this check mainly serves as a reminder that this test should
|
|
|
|
// be updated *if* one day there is support for emitting the actual span.
|
|
|
|
assert_eq!(foo.end(), 0);
|
|
|
|
for (k, v) in foo.get_ref().iter() {
|
|
|
|
assert_eq!(&s[k.start()..k.end()], k.get_ref());
|
|
|
|
assert_eq!(&s[(v.start() + 1)..(v.end() - 1)], v.get_ref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
good(
|
|
|
|
"
|
|
|
|
[[foo]]
|
|
|
|
a = 'b'
|
|
|
|
bar = 'baz'
|
|
|
|
c = 'd'
|
|
|
|
e = \"f\"
|
|
|
|
[[foo]]
|
|
|
|
a = 'c'
|
|
|
|
bar = 'baz'
|
|
|
|
c = 'g'
|
|
|
|
e = \"h\"
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|