From e82e5c5e0bcc124e5a6b92171223dfdaca1d4180 Mon Sep 17 00:00:00 2001 From: Mingun Date: Wed, 19 Jun 2024 01:48:52 +0500 Subject: [PATCH] Replace test issue_default_namespace_on_outermost_element with more formal test in reader-namespaces.rs (Review in whitespace changes ignored mode) --- tests/reader-namespaces.rs | 238 +++++++++++++++++++++--------------- tests/xmlrs_reader_tests.rs | 13 -- 2 files changed, 139 insertions(+), 112 deletions(-) diff --git a/tests/reader-namespaces.rs b/tests/reader-namespaces.rs index dc3d0185..4c4e5581 100644 --- a/tests/reader-namespaces.rs +++ b/tests/reader-namespaces.rs @@ -87,117 +87,157 @@ fn namespace() { ); } -#[test] -fn default_namespace() { - let mut r = NsReader::from_str(r#""#); +mod default_namespace { + use super::*; + use pretty_assertions::assert_eq; - // - match r.read_resolved_event() { - Ok((ns, Start(_))) => assert_eq!(ns, Unbound), - e => panic!( - "expecting outer start element with no namespace, got {:?}", - e - ), - } - let it = r.prefixes(); - assert_eq!(it.size_hint(), (0, Some(0))); - assert_eq!(it.collect::>(), vec![]); + #[test] + fn event_empty() { + let mut r = NsReader::from_str(""); - // - match r.read_resolved_event() { - Ok((ns, Start(_))) => assert_eq!(ns, Bound(Namespace(b"www1"))), - e => panic!( - "expecting inner start element with to resolve to 'www1', got {:?}", - e - ), - } - let it = r.prefixes(); - assert_eq!(it.size_hint(), (0, Some(1))); - assert_eq!( - it.collect::>(), - vec![(PrefixDeclaration::Default, Namespace(b"www1"))] - ); + let e = match r.read_resolved_event() { + Ok((ns, Empty(e))) => { + assert_eq!(ns, Bound(Namespace(b"ns"))); + e + } + e => panic!("Expecting Empty event, got {:?}", e), + }; - // - match r.read_resolved_event() { - Ok((ns, End(_))) => assert_eq!(ns, Bound(Namespace(b"www1"))), - e => panic!( - "expecting inner end element with to resolve to 'www1', got {:?}", - e - ), - } - let it = r.prefixes(); - assert_eq!(it.size_hint(), (0, Some(1))); - assert_eq!( - it.collect::>(), - vec![(PrefixDeclaration::Default, Namespace(b"www1"))] - ); + let mut attrs = e + .attributes() + .map(|ar| ar.expect("Expecting attribute parsing to succeed.")) + // we don't care about xmlns attributes for this test + .filter(|kv| kv.key.as_namespace_binding().is_none()) + .map(|Attribute { key: name, value }| { + let (opt_ns, local_name) = r.resolve_attribute(name); + (opt_ns, local_name.into_inner(), value) + }); + assert_eq!( + attrs.next(), + Some((Unbound, &b"attr"[..], Cow::Borrowed(&b"val"[..]))) + ); + assert_eq!(attrs.next(), None); - // very important: a should not be in any namespace. The default namespace only applies to - // the sub-document it is defined on. - match r.read_resolved_event() { - Ok((ns, End(_))) => assert_eq!(ns, Unbound), - e => panic!("expecting outer end element with no namespace, got {:?}", e), + let it = r.prefixes(); + assert_eq!(it.size_hint(), (0, Some(1))); + assert_eq!( + it.collect::>(), + vec![(PrefixDeclaration::Default, Namespace(b"ns"))] + ); } - let it = r.prefixes(); - assert_eq!(it.size_hint(), (0, Some(0))); - assert_eq!(it.collect::>(), vec![]); -} -#[test] -fn default_namespace_reset() { - let mut r = NsReader::from_str(r#""#); + #[test] + fn event_start_end() { + let mut r = NsReader::from_str(r#""#); - // - match r.read_resolved_event() { - Ok((ns, Start(_))) => assert_eq!(ns, Bound(Namespace(b"www1"))), - e => panic!( - "expecting outer start element with to resolve to 'www1', got {:?}", - e - ), - } - let it = r.prefixes(); - assert_eq!(it.size_hint(), (0, Some(1))); - assert_eq!( - it.collect::>(), - vec![(PrefixDeclaration::Default, Namespace(b"www1"))] - ); + // + match r.read_resolved_event() { + Ok((ns, Start(_))) => assert_eq!(ns, Unbound), + e => panic!( + "expecting outer start element with no namespace, got {:?}", + e + ), + } + let it = r.prefixes(); + assert_eq!(it.size_hint(), (0, Some(0))); + assert_eq!(it.collect::>(), vec![]); - // - match r.read_resolved_event() { - Ok((ns, Start(_))) => assert_eq!(ns, Unbound), - e => panic!( - "expecting inner start element with no namespace, got {:?}", - e - ), - } - let it = r.prefixes(); - assert_eq!(it.size_hint(), (0, Some(2))); - assert_eq!(it.collect::>(), vec![]); + // + match r.read_resolved_event() { + Ok((ns, Start(_))) => assert_eq!(ns, Bound(Namespace(b"www1"))), + e => panic!( + "expecting inner start element with to resolve to 'www1', got {:?}", + e + ), + } + let it = r.prefixes(); + assert_eq!(it.size_hint(), (0, Some(1))); + assert_eq!( + it.collect::>(), + vec![(PrefixDeclaration::Default, Namespace(b"www1"))] + ); - // - match r.read_resolved_event() { - Ok((ns, End(_))) => assert_eq!(ns, Unbound), - e => panic!("expecting inner end element with no namespace, got {:?}", e), + // + match r.read_resolved_event() { + Ok((ns, End(_))) => assert_eq!(ns, Bound(Namespace(b"www1"))), + e => panic!( + "expecting inner end element with to resolve to 'www1', got {:?}", + e + ), + } + let it = r.prefixes(); + assert_eq!(it.size_hint(), (0, Some(1))); + assert_eq!( + it.collect::>(), + vec![(PrefixDeclaration::Default, Namespace(b"www1"))] + ); + + // very important: a should not be in any namespace. The default namespace only applies to + // the sub-document it is defined on. + match r.read_resolved_event() { + Ok((ns, End(_))) => assert_eq!(ns, Unbound), + e => panic!("expecting outer end element with no namespace, got {:?}", e), + } + let it = r.prefixes(); + assert_eq!(it.size_hint(), (0, Some(0))); + assert_eq!(it.collect::>(), vec![]); } - let it = r.prefixes(); - assert_eq!(it.size_hint(), (0, Some(2))); - assert_eq!(it.collect::>(), vec![]); - // - match r.read_resolved_event() { - Ok((ns, End(_))) => assert_eq!(ns, Bound(Namespace(b"www1"))), - e => panic!( - "expecting outer end element with to resolve to 'www1', got {:?}", - e - ), + #[test] + fn reset() { + let mut r = NsReader::from_str(r#""#); + + // + match r.read_resolved_event() { + Ok((ns, Start(_))) => assert_eq!(ns, Bound(Namespace(b"www1"))), + e => panic!( + "expecting outer start element with to resolve to 'www1', got {:?}", + e + ), + } + let it = r.prefixes(); + assert_eq!(it.size_hint(), (0, Some(1))); + assert_eq!( + it.collect::>(), + vec![(PrefixDeclaration::Default, Namespace(b"www1"))] + ); + + // + match r.read_resolved_event() { + Ok((ns, Start(_))) => assert_eq!(ns, Unbound), + e => panic!( + "expecting inner start element with no namespace, got {:?}", + e + ), + } + let it = r.prefixes(); + assert_eq!(it.size_hint(), (0, Some(2))); + assert_eq!(it.collect::>(), vec![]); + + // + match r.read_resolved_event() { + Ok((ns, End(_))) => assert_eq!(ns, Unbound), + e => panic!("expecting inner end element with no namespace, got {:?}", e), + } + let it = r.prefixes(); + assert_eq!(it.size_hint(), (0, Some(2))); + assert_eq!(it.collect::>(), vec![]); + + // + match r.read_resolved_event() { + Ok((ns, End(_))) => assert_eq!(ns, Bound(Namespace(b"www1"))), + e => panic!( + "expecting outer end element with to resolve to 'www1', got {:?}", + e + ), + } + let it = r.prefixes(); + assert_eq!(it.size_hint(), (0, Some(1))); + assert_eq!( + it.collect::>(), + vec![(PrefixDeclaration::Default, Namespace(b"www1"))] + ); } - let it = r.prefixes(); - assert_eq!(it.size_hint(), (0, Some(1))); - assert_eq!( - it.collect::>(), - vec![(PrefixDeclaration::Default, Namespace(b"www1"))] - ); } /// Single empty element with qualified attributes. diff --git a/tests/xmlrs_reader_tests.rs b/tests/xmlrs_reader_tests.rs index 4f1f259a..6469a028 100644 --- a/tests/xmlrs_reader_tests.rs +++ b/tests/xmlrs_reader_tests.rs @@ -267,19 +267,6 @@ fn issue_105_unexpected_double_dash() { ); } -#[test] -fn issue_default_namespace_on_outermost_element() { - // Regression test - test( - r#""#, - r#" - |EmptyElement({urn:foo}hello) - |EndDocument - "#, - true, - ); -} - #[track_caller] fn test(input: &str, output: &str, trim: bool) { test_bytes(input.as_bytes(), output.as_bytes(), trim);