From 6ff5c445f37083c785111c904426c94b7a01f4b4 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Mon, 7 May 2018 03:47:09 +0200 Subject: Use custom struct/field naming to deserialize spans --- test-suite/tests/spanned.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test-suite/tests/spanned.rs (limited to 'test-suite/tests') diff --git a/test-suite/tests/spanned.rs b/test-suite/tests/spanned.rs new file mode 100644 index 0000000..fec9ea8 --- /dev/null +++ b/test-suite/tests/spanned.rs @@ -0,0 +1,30 @@ +extern crate serde; +extern crate toml; +#[macro_use] +extern crate serde_derive; + +use toml::Spanned; +use std::collections::HashMap; + +#[test] +fn test_spanned_field() { + #[derive(Deserialize)] + struct Foo { + foo: Spanned, + } + + fn good<'de, T>(s: &'de str, expected: &str) where T: serde::Deserialize<'de> { + let foo: Foo = 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::("foo = \"foo\"", "\"foo\""); + good::("foo = 42", "42"); + good::>( + "foo = {\"foo\" = 42, \"bar\" = 42}", + "{\"foo\" = 42, \"bar\" = 42}" + ); +} -- cgit v1.2.3 From 6c067202e061910d5018c4f96e9a7595c4291a08 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Mon, 7 May 2018 04:05:05 +0200 Subject: Adjust spans for more complex types --- test-suite/tests/spanned.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test-suite/tests') diff --git a/test-suite/tests/spanned.rs b/test-suite/tests/spanned.rs index fec9ea8..c5dc28e 100644 --- a/test-suite/tests/spanned.rs +++ b/test-suite/tests/spanned.rs @@ -23,8 +23,21 @@ fn test_spanned_field() { good::("foo = \"foo\"", "\"foo\""); good::("foo = 42", "42"); + // leading plus + good::("foo = +42", "+42"); + // table good::>( "foo = {\"foo\" = 42, \"bar\" = 42}", "{\"foo\" = 42, \"bar\" = 42}" ); + // array + good::>( + "foo = [0, 1, 2, 3, 4]", + "[0, 1, 2, 3, 4]" + ); + // datetime + good::( + "foo = \"1997-09-09T09:09:09Z\"", + "\"1997-09-09T09:09:09Z\"" + ); } -- cgit v1.2.3 From 2b40d542a0ea645c5e3c132aabc0082dffa2f554 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Mon, 7 May 2018 05:36:09 +0200 Subject: Fix incorrectly reported spans for Datetime --- test-suite/tests/spanned.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'test-suite/tests') diff --git a/test-suite/tests/spanned.rs b/test-suite/tests/spanned.rs index c5dc28e..4339a0c 100644 --- a/test-suite/tests/spanned.rs +++ b/test-suite/tests/spanned.rs @@ -4,8 +4,26 @@ extern crate toml; 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)] @@ -40,4 +58,9 @@ fn test_spanned_field() { "foo = \"1997-09-09T09:09:09Z\"", "\"1997-09-09T09:09:09Z\"" ); + + for expected in good_datetimes() { + let s = format!("foo = {}", expected); + good::(&s, expected); + } } -- cgit v1.2.3 From 0a5fe3fff9a11b9684eb41be302606de0770f22d Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Wed, 9 May 2018 21:26:59 +0200 Subject: Make spanned module private and hide internals --- test-suite/tests/spanned.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test-suite/tests') diff --git a/test-suite/tests/spanned.rs b/test-suite/tests/spanned.rs index 4339a0c..1b110f0 100644 --- a/test-suite/tests/spanned.rs +++ b/test-suite/tests/spanned.rs @@ -34,9 +34,9 @@ fn test_spanned_field() { fn good<'de, T>(s: &'de str, expected: &str) where T: serde::Deserialize<'de> { let foo: Foo = 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]); + assert_eq!(6, foo.foo.start()); + assert_eq!(s.len(), foo.foo.end()); + assert_eq!(expected, &s[foo.foo.start()..foo.foo.end()]); } good::("foo = \"foo\"", "\"foo\""); -- cgit v1.2.3