use sup_xml::{parse_str, xpath_count, xpath_str, ParseOptions};
fn doc(xml: &str) -> sup_xml::Document {
let opts = ParseOptions { namespace_aware: true, ..ParseOptions::default() };
parse_str(xml, &opts).expect("test document must parse")
}
#[test]
fn xml_id_is_lookable_without_dtd() {
let d = doc(r#"<r><a xml:id="alpha"/><a xml:id="beta"/></r>"#);
assert_eq!(xpath_count(&d, "id('alpha')").unwrap(), 1);
assert_eq!(xpath_count(&d, "id('beta')").unwrap(), 1);
assert_eq!(xpath_count(&d, "id('gamma')").unwrap(), 0);
}
#[test]
fn xml_id_value_is_whitespace_normalized() {
let d = doc(r#"<r><a xml:id=" foo "/></r>"#);
assert_eq!(xpath_count(&d, "id('foo')").unwrap(), 1);
assert_eq!(xpath_str(&d, "string(/r/a/@xml:id)").unwrap(), "foo");
}
#[test]
fn xml_id_normalizes_internal_whitespace_runs() {
let d = doc("<r><a xml:id=\"foo\t\tbar\"/></r>");
assert_eq!(xpath_str(&d, "string(/r/a/@xml:id)").unwrap(), "foo bar");
}
#[test]
fn xml_id_coexists_with_dtd_attlist_typing() {
let d = doc(r#"<!DOCTYPE r [
<!ELEMENT r (a*)>
<!ELEMENT a EMPTY>
<!ATTLIST a key ID #IMPLIED>
]>
<r>
<a key="legacy"/>
<a xml:id="modern"/>
</r>"#);
assert_eq!(xpath_count(&d, "id('legacy')").unwrap(), 1);
assert_eq!(xpath_count(&d, "id('modern')").unwrap(), 1);
}
#[test]
fn xml_id_with_other_attributes_isnt_confused() {
let d = doc(r#"<r><a xml:id="alpha" desc="foo bar"/></r>"#);
assert_eq!(xpath_str(&d, "string(/r/a/@desc)").unwrap(), "foo bar");
assert_eq!(xpath_str(&d, "string(/r/a/@xml:id)").unwrap(), "alpha");
assert_eq!(xpath_count(&d, "id('alpha')").unwrap(), 1);
}