diff options
author | Alex Crichton <alex@alexcrichton.com> | 2018-05-09 16:48:58 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-09 16:48:58 -0500 |
commit | cb49d599924f2c78a8ea1172cd21f05dc6fbfa93 (patch) | |
tree | 4506e53b64daa801a2b58c9fbba56343f623278c /test-suite | |
parent | 8a54e5e0b6808a3192ec342b60e36b4325c47f02 (diff) | |
parent | 0a5fe3fff9a11b9684eb41be302606de0770f22d (diff) | |
download | milf-rs-cb49d599924f2c78a8ea1172cd21f05dc6fbfa93.tar.gz milf-rs-cb49d599924f2c78a8ea1172cd21f05dc6fbfa93.zip |
Merge pull request #239 from udoprog/spans
Support spans when deserializing serde structures
Diffstat (limited to 'test-suite')
-rw-r--r-- | test-suite/tests/spanned.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/test-suite/tests/spanned.rs b/test-suite/tests/spanned.rs new file mode 100644 index 0000000..1b110f0 --- /dev/null +++ b/test-suite/tests/spanned.rs @@ -0,0 +1,66 @@ +extern crate serde; +extern crate toml; +#[macro_use] +extern crate serde_derive; + +use toml::Spanned; +use toml::value::Datetime; +use std::collections::HashMap; + +/// 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 +} + +#[test] +fn test_spanned_field() { + #[derive(Deserialize)] + struct Foo<T> { + foo: Spanned<T>, + } + + fn good<'de, T>(s: &'de str, expected: &str) where T: serde::Deserialize<'de> { + let foo: Foo<T> = toml::from_str(s).unwrap(); + + assert_eq!(6, foo.foo.start()); + assert_eq!(s.len(), foo.foo.end()); + assert_eq!(expected, &s[foo.foo.start()..foo.foo.end()]); + } + + good::<String>("foo = \"foo\"", "\"foo\""); + good::<u32>("foo = 42", "42"); + // leading plus + good::<u32>("foo = +42", "+42"); + // table + good::<HashMap<String, u32>>( + "foo = {\"foo\" = 42, \"bar\" = 42}", + "{\"foo\" = 42, \"bar\" = 42}" + ); + // array + good::<Vec<u32>>( + "foo = [0, 1, 2, 3, 4]", + "[0, 1, 2, 3, 4]" + ); + // datetime + good::<String>( + "foo = \"1997-09-09T09:09:09Z\"", + "\"1997-09-09T09:09:09Z\"" + ); + + for expected in good_datetimes() { + let s = format!("foo = {}", expected); + good::<Datetime>(&s, expected); + } +} |