Skip to content

Commit f2eed43

Browse files
committed
Add test to ensure, that entities which expands to the XML fragments are parsed
1 parent e49b193 commit f2eed43

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/serde-de-references.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::convert::Infallible;
2+
3+
use quick_xml::de::{Deserializer, EntityResolver};
4+
use quick_xml::events::BytesText;
5+
use serde::Deserialize;
6+
7+
struct TestResolver;
8+
impl EntityResolver for TestResolver {
9+
type Error = Infallible;
10+
11+
fn capture(&mut self, _doctype: BytesText) -> Result<(), Self::Error> {
12+
Ok(())
13+
}
14+
fn resolve(&self, entity: &str) -> Option<&str> {
15+
match dbg!(entity) {
16+
"text" => Some("&#x20;<![CDATA[second text]]>&#32;"),
17+
_ => Some(
18+
"
19+
<child1 attribute = '&lt;attribute value&gt;'>&text;</child1>
20+
<child2/>
21+
",
22+
),
23+
}
24+
}
25+
}
26+
27+
#[derive(Debug, PartialEq, Deserialize)]
28+
struct Root {
29+
child1: Child1,
30+
child2: (),
31+
}
32+
33+
#[derive(Debug, PartialEq, Deserialize)]
34+
struct Child1 {
35+
#[serde(rename = "@attribute")]
36+
attribute: String,
37+
38+
#[serde(rename = "$text")]
39+
text: String,
40+
}
41+
42+
#[test]
43+
fn entities() {
44+
let mut de = Deserializer::from_str_with_resolver("<root>&entity;</root>", TestResolver);
45+
46+
let data = Root::deserialize(&mut de).unwrap();
47+
48+
assert!(de.is_empty(), "the whole XML document should be consumed");
49+
assert_eq!(
50+
data,
51+
Root {
52+
child1: Child1 {
53+
attribute: "<attribute value>".to_string(),
54+
text: " second text ".to_string(),
55+
},
56+
child2: (),
57+
}
58+
);
59+
}

0 commit comments

Comments
 (0)