use std::borrow::Cow;
use similar_asserts::assert_eq;
use instant_xml::{from_str, to_string, FromXml, ToXml};
#[derive(Debug, PartialEq, Eq, FromXml, ToXml)]
#[xml(ns("URI"))]
struct StructSpecialEntities<'a> {
string: String,
#[xml(borrow)]
cow: Cow<'a, str>,
}
#[test]
fn escape_back() {
assert_eq!(
from_str(
"<StructSpecialEntities xmlns=\"URI\"><string><>&"'adsad"</string><cow>str&</cow></StructSpecialEntities>"
),
Ok(StructSpecialEntities {
string: String::from("<>&\"'adsad\""),
cow: Cow::Owned("str&".to_string()),
})
);
let escape_back = from_str::<StructSpecialEntities>(
"<StructSpecialEntities xmlns=\"URI\"><string><>&"'adsad"</string><cow>str</cow></StructSpecialEntities>"
)
.unwrap();
if let Cow::Owned(_) = escape_back.cow {
panic!("Should be Borrowed")
}
let escape_back = from_str::<StructSpecialEntities>(
"<StructSpecialEntities xmlns=\"URI\"><string><>&"'adsad"</string><cow>str&</cow></StructSpecialEntities>"
)
.unwrap();
if let Cow::Borrowed(_) = escape_back.cow {
panic!("Should be Owned")
}
}
#[test]
fn special_entities() {
assert_eq!(
to_string(&StructSpecialEntities{
string: "&\"<>\'aa".to_string(),
cow: Cow::from("&\"<>\'cc"),
}).unwrap(),
"<StructSpecialEntities xmlns=\"URI\"><string>&"<>'aa</string><cow>&"<>'cc</cow></StructSpecialEntities>",
);
}
#[derive(Debug, PartialEq, Eq, FromXml, ToXml)]
struct SimpleCData<'a> {
#[xml(borrow)]
foo: Cow<'a, str>,
}
#[test]
fn simple_cdata() {
assert_eq!(
from_str::<SimpleCData>("<SimpleCData><foo><![CDATA[<fo&o>]]></foo></SimpleCData>")
.unwrap(),
SimpleCData {
foo: Cow::Borrowed("<fo&o>")
}
);
assert_eq!(
to_string(&SimpleCData {
foo: Cow::Borrowed("<foo>")
})
.unwrap(),
"<SimpleCData><foo><foo></foo></SimpleCData>",
);
}