From f2eed43f3e80dc9b59c8ba25fd442507345895a8 Mon Sep 17 00:00:00 2001 From: Mingun Date: Thu, 20 Jun 2024 00:29:24 +0500 Subject: [PATCH] Add test to ensure, that entities which expands to the XML fragments are parsed --- tests/serde-de-references.rs | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/serde-de-references.rs diff --git a/tests/serde-de-references.rs b/tests/serde-de-references.rs new file mode 100644 index 00000000..88020d9b --- /dev/null +++ b/tests/serde-de-references.rs @@ -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(" "), + _ => Some( + " + &text; + + ", + ), + } + } +} + +#[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("&entity;", 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: "".to_string(), + text: " second text ".to_string(), + }, + child2: (), + } + ); +}