From ec21d604f892a06999a9d38d38c903e083ad1f08 Mon Sep 17 00:00:00 2001 From: est31 Date: Mon, 28 Oct 2019 15:01:23 +0100 Subject: Support for dotted table spans (#340) * "Support" spans for maps In toml you can declare maps via {} and via [name]. We can't obtain spans for [] maps but at least we can emit fake spans to make SpannedValue work. We also add a regression test. * Don't regress the inline table case * Also support arrays --- test-suite/tests/spanned.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) (limited to 'test-suite') diff --git a/test-suite/tests/spanned.rs b/test-suite/tests/spanned.rs index 1186645..d8f7a12 100644 --- a/test-suite/tests/spanned.rs +++ b/test-suite/tests/spanned.rs @@ -87,7 +87,54 @@ fn test_spanned_field() { } #[test] -fn test_spanned_table() { +fn test_inner_spanned_table() { + #[derive(Deserialize)] + struct Foo { + foo: Spanned, Spanned>>, + } + + 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() { #[derive(Deserialize)] struct Foo { foo: HashMap, Spanned>, @@ -158,3 +205,45 @@ fn test_spanned_nested() { ", ); } + +#[test] +fn test_spanned_array() { + #[derive(Deserialize)] + struct Foo { + foo: Vec, Spanned>>>, + } + + 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\" + ", + ); +} -- cgit v1.2.3