From 7c9b0a39db0c4c235db372f88d725fae6e82889f Mon Sep 17 00:00:00 2001 From: est31 Date: Mon, 16 Sep 2019 23:32:45 +0200 Subject: Support deserializing spanned keys (#333) * Store key spans in the deserializer * Support deserializing spanned keys * Store key spans of the table header as well * Support nested table key spans as well --- test-suite/tests/spanned.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'test-suite/tests') diff --git a/test-suite/tests/spanned.rs b/test-suite/tests/spanned.rs index 5130a72..1186645 100644 --- a/test-suite/tests/spanned.rs +++ b/test-suite/tests/spanned.rs @@ -85,3 +85,76 @@ fn test_spanned_field() { // ending at something other than the absolute end good::("foo = 42\nnoise = true", "42", Some(8)); } + +#[test] +fn test_spanned_table() { + #[derive(Deserialize)] + struct Foo { + foo: HashMap, Spanned>, + } + + 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, HashMap, Spanned>>, + } + + 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' } + ", + ); +} -- cgit v1.2.3