-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test to ensure, that entities which expands to the XML fragments …
…are parsed
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" <![CDATA[second text]]> "), | ||
_ => Some( | ||
" | ||
<child1 attribute = '<attribute value>'>&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: (), | ||
} | ||
); | ||
} |