Skip to content

Commit

Permalink
Add test for reading invalid XML to the end
Browse files Browse the repository at this point in the history
(Review this commit with "ignore whitespace changes" option)

failures (1):
    de::tests::read_to_end::invalid_xml
  • Loading branch information
Mingun authored and dralley committed Dec 26, 2022
1 parent 85eeb2e commit 75ae6c7
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3079,40 +3079,58 @@ mod tests {
}
}

#[test]
fn read_to_end() {
mod read_to_end {
use super::*;
use crate::de::DeEvent::*;
use pretty_assertions::assert_eq;

let mut de = Deserializer::from_str(
r#"
<root>
<tag a="1"><tag>text</tag>content</tag>
<tag a="2"><![CDATA[cdata content]]></tag>
<self-closed/>
</root>
"#,
);
#[test]
fn complex() {
let mut de = Deserializer::from_str(
r#"
<root>
<tag a="1"><tag>text</tag>content</tag>
<tag a="2"><![CDATA[cdata content]]></tag>
<self-closed/>
</root>
"#,
);

assert_eq!(de.next().unwrap(), Start(BytesStart::new("root")));
assert_eq!(de.next().unwrap(), Start(BytesStart::new("root")));

assert_eq!(
de.next().unwrap(),
Start(BytesStart::from_content(r#"tag a="1""#, 3))
);
assert_eq!(de.read_to_end(QName(b"tag")).unwrap(), ());
assert_eq!(
de.next().unwrap(),
Start(BytesStart::from_content(r#"tag a="1""#, 3))
);
assert_eq!(de.read_to_end(QName(b"tag")).unwrap(), ());

assert_eq!(
de.next().unwrap(),
Start(BytesStart::from_content(r#"tag a="2""#, 3))
);
assert_eq!(de.next().unwrap(), CData(BytesCData::new("cdata content")));
assert_eq!(de.next().unwrap(), End(BytesEnd::new("tag")));
assert_eq!(
de.next().unwrap(),
Start(BytesStart::from_content(r#"tag a="2""#, 3))
);
assert_eq!(de.next().unwrap(), CData(BytesCData::new("cdata content")));
assert_eq!(de.next().unwrap(), End(BytesEnd::new("tag")));

assert_eq!(de.next().unwrap(), Start(BytesStart::new("self-closed")));
assert_eq!(de.read_to_end(QName(b"self-closed")).unwrap(), ());
assert_eq!(de.next().unwrap(), Start(BytesStart::new("self-closed")));
assert_eq!(de.read_to_end(QName(b"self-closed")).unwrap(), ());

assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
assert_eq!(de.next().unwrap(), Eof);
}

assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
assert_eq!(de.next().unwrap(), Eof);
#[test]
fn invalid_xml() {
let mut de = Deserializer::from_str("<tag><tag></tag>");

assert_eq!(de.next().unwrap(), Start(BytesStart::new("tag")));
assert_eq!(de.peek().unwrap(), &Start(BytesStart::new("tag")));

match de.read_to_end(QName(b"tag")) {
Err(DeError::UnexpectedEof) => (),
x => panic!("Expected `Err(UnexpectedEof)`, but found {:?}", x),
}
assert_eq!(de.next().unwrap(), Eof);
}
}

#[test]
Expand Down

0 comments on commit 75ae6c7

Please sign in to comment.