Skip to content

Commit

Permalink
Add test to ensure, that entities which expands to the XML fragments …
Browse files Browse the repository at this point in the history
…are parsed
  • Loading branch information
Mingun committed Jun 21, 2024
1 parent e49b193 commit f2eed43
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/serde-de-references.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::convert::Infallible;

use quick_xml::de::{Deserializer, EntityResolver};
use quick_xml::events::BytesText;
use serde::Deserialize;

struct TestResolver;
impl EntityResolver for TestResolver {
type Error = Infallible;

fn capture(&mut self, _doctype: BytesText) -> Result<(), Self::Error> {
Ok(())
}
fn resolve(&self, entity: &str) -> Option<&str> {
match dbg!(entity) {
"text" => Some("&#x20;<![CDATA[second text]]>&#32;"),
_ => Some(
"
<child1 attribute = '&lt;attribute value&gt;'>&text;</child1>
<child2/>
",
),
}
}
}

#[derive(Debug, PartialEq, Deserialize)]
struct Root {
child1: Child1,
child2: (),
}

#[derive(Debug, PartialEq, Deserialize)]
struct Child1 {
#[serde(rename = "@attribute")]
attribute: String,

#[serde(rename = "$text")]
text: String,
}

#[test]
fn entities() {
let mut de = Deserializer::from_str_with_resolver("<root>&entity;</root>", TestResolver);

let data = Root::deserialize(&mut de).unwrap();

assert!(de.is_empty(), "the whole XML document should be consumed");
assert_eq!(
data,
Root {
child1: Child1 {
attribute: "<attribute value>".to_string(),
text: " second text ".to_string(),
},
child2: (),
}
);
}

0 comments on commit f2eed43

Please sign in to comment.