//! Unit tests of the document-set extraction.
use super::*;
const HEAD: &str = r#"<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ix="http://www.xbrl.org/2013/inlineXBRL" xmlns:ixt="http://www.xbrl.org/inlineXBRL/transformation/2020-02-12" xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:link="http://www.xbrl.org/2003/linkbase" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:iso4217="http://www.xbrl.org/2003/iso4217" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:e="http://example.com/e"><head><title>t</title></head><body xml:lang="en"><div style="display:none"><ix:header><ix:references><link:schemaRef xlink:type="simple" xlink:href="e.xsd"/></ix:references><ix:resources><xbrli:context id="c"><xbrli:entity><xbrli:identifier scheme="s">X</xbrli:identifier></xbrli:entity><xbrli:period><xbrli:instant>2024-12-31</xbrli:instant></xbrli:period></xbrli:context><xbrli:unit id="u"><xbrli:measure>iso4217:GBP</xbrli:measure></xbrli:unit></ix:resources></ix:header></div>"#;
fn load_text(text: &str) -> InlineSet {
let doc = crossfoot_core::xml::parse_str("file:///t.html", text).expect("well-formed");
load(vec![Arc::new(doc)])
}
fn load_body(body: &str) -> InlineSet {
load_text(&format!("{}{}</body></html>", HEAD, body))
}
#[test]
fn nested_non_fractions_may_differ_in_sign() {
// basic EPS positive, diluted EPS negative over one displayed number (Maltese ESEF filing)
let s = load_body(
r#"<p>(<ix:nonFraction name="e:basic" contextRef="c" unitRef="u" decimals="2" format="ixt:num-dot-decimal"><ix:nonFraction name="e:diluted" contextRef="c" unitRef="u" decimals="2" sign="-" format="ixt:num-dot-decimal">0.04</ix:nonFraction></ix:nonFraction>)</p>"#,
);
let c = codes(&s);
assert!(
!c.iter().any(|x| x.contains("NestedAttributeMismatch")),
"{:?}",
c
);
assert_eq!(value(&s, "basic").as_deref(), Some("0.04"));
assert_eq!(value(&s, "diluted").as_deref(), Some("-0.04"));
// a scale mismatch is still an error
let bad = load_body(
r#"<p><ix:nonFraction name="e:a" contextRef="c" unitRef="u" decimals="0" scale="3"><ix:nonFraction name="e:b" contextRef="c" unitRef="u" decimals="0">1</ix:nonFraction></ix:nonFraction></p>"#,
);
assert!(codes(&bad)
.iter()
.any(|x| x.contains("NonFractionNestedAttributeMismatch")));
}
#[test]
fn list_and_row_content_models_are_checked() {
// <ol> directly inside <ol> (an ESEF filing; Arelle: lxml SCHEMAV_ELEMENT_CONTENT)
let s = load_body(r#"<ol><li>a</li><ol><li>b</li></ol></ol><ul></ul>"#);
let c = codes(&s);
assert!(
c.iter().any(|x| x.ends_with("cvc-complex-type_2_4_a")),
"{:?}",
c
);
assert!(
c.iter().any(|x| x.ends_with("cvc-complex-type_2_4_b")),
"{:?}",
c
);
let ok = load_body(
r#"<ol><li>a<ol><li>b</li></ol></li></ol><dl><dt>x</dt><dd>y</dd></dl><table><tr><th>h</th><td>d</td></tr></table>"#,
);
let c = codes(&ok);
assert!(
!c.iter().any(|x| x.contains("cvc-complex-type_2_4")),
"{:?}",
c
);
}
#[test]
fn xhtml_attributes_outside_the_schema_are_reported() {
// ESEF filings carry tool attributes such as <span x-id="..."> (Arelle: lxml cvc 3.2.1)
let s = load_body(
r#"<p><span x-id="id-1" class="c">a</span></p><table><tr><td width="5">b</td></tr></table>"#,
);
let bad: Vec<_> = s
.diags
.iter()
.filter(|d| d.code.ends_with("cvc-complex-type_3_2_2"))
.map(|d| d.message.clone())
.collect();
assert_eq!(bad.len(), 2, "{:?}", bad);
assert!(bad[0].contains("x-id") && bad[1].contains("width"));
// declared attributes are fine
let ok = load_body(
r#"<p class="a" id="p1" style="x" title="t" dir="ltr"><span class="c">a</span></p><table border="0" cellpadding="1" width="100%"><tr valign="top"><td colspan="2" align="right">b</td></tr></table>"#,
);
assert!(
!ok.diags
.iter()
.any(|d| d.code.ends_with("cvc-complex-type_3_2_2")),
"{:?}",
ok.diags
);
}
fn codes(s: &InlineSet) -> Vec<String> {
s.diags
.iter()
.map(|d| d.code.rsplit(':').next().unwrap().to_string())
.collect()
}
fn value(s: &InlineSet, local: &str) -> Option<String> {
s.instances[0]
.facts
.iter()
.find(|f| &*f.concept.local == local)
.and_then(|f| f.value.clone())
}
#[test]
fn numeric_scale_sign_format() {
let s = load_body(
r#"<p><ix:nonFraction name="e:a" contextRef="c" unitRef="u" decimals="-3" scale="3" sign="-" format="ixt:num-dot-decimal">1,234.5</ix:nonFraction></p>"#,
);
assert!(s.diags.is_empty(), "{:?}", codes(&s));
assert_eq!(value(&s, "a").as_deref(), Some("-1234500"));
let f = &s.instances[0].facts[0];
assert_eq!(f.decimals, Some(Decimals::N(-3)));
assert_eq!(f.inline.as_ref().unwrap().display, "1,234.5");
}
#[test]
fn continuation_and_exclude() {
let s = load_body(
r#"<p><ix:nonNumeric name="e:t" contextRef="c" continuedAt="k1">One <ix:exclude>skip</ix:exclude>two</ix:nonNumeric></p><p><ix:continuation id="k1"> three</ix:continuation></p>"#,
);
assert!(s.diags.is_empty(), "{:?}", codes(&s));
assert_eq!(value(&s, "t").as_deref(), Some("One two three"));
assert!(s.instances[0].facts[0].inline.as_ref().unwrap().continued);
}
#[test]
fn escaped_content_keeps_markup() {
let s = load_body(
r#"<div><ix:nonNumeric name="e:t" contextRef="c" escape="true"><p class="x">A & <b>B</b><br/></p><ix:exclude>no</ix:exclude></ix:nonNumeric></div>"#,
);
assert_eq!(
value(&s, "t").as_deref(),
Some(r#"<p class="x">A & <b>B</b><br/></p>"#)
);
}
#[test]
fn nested_facts_and_lang() {
let s = load_body(
r#"<div><ix:nonNumeric name="e:outer" contextRef="c">Total <ix:nonFraction name="e:n" contextRef="c" unitRef="u" decimals="0">12</ix:nonFraction> units</ix:nonNumeric></div>"#,
);
assert_eq!(value(&s, "outer").as_deref(), Some("Total 12 units"));
assert_eq!(value(&s, "n").as_deref(), Some("12"));
// the output document has no xml:lang unless copied from ix:references
assert!(s.instances[0].facts.iter().all(|f| f.lang.is_none()));
}
#[test]
fn tuple_order_and_duplicates() {
let s = load_body(
r#"<div><ix:tuple name="e:tp" tupleID="t"><ix:nonNumeric name="e:b" contextRef="c" order="2">B</ix:nonNumeric><ix:nonNumeric name="e:a" contextRef="c" order="1">A</ix:nonNumeric></ix:tuple><ix:nonNumeric name="e:a" contextRef="c" order="1" tupleRef="t">A</ix:nonNumeric></div>"#,
);
assert!(s.diags.is_empty(), "{:?}", codes(&s));
let inst = &s.instances[0];
assert_eq!(inst.facts.len(), 3, "the duplicate member is output once");
let tuple = &inst.facts[inst.top_level[0].0 as usize];
let kids: Vec<&str> = tuple
.children
.iter()
.map(|c| &*inst.facts[c.0 as usize].concept.local)
.collect();
assert_eq!(kids, vec!["a", "b"]);
}
#[test]
fn spec_errors() {
let s = load_body(
r#"<p><ix:nonFraction name="e:a" contextRef="nope" unitRef="u">-1</ix:nonFraction><ix:nonNumeric name="e:b" contextRef="c" continuedAt="missing">x</ix:nonNumeric><ix:continuation id="orphan">y</ix:continuation></p>"#,
);
let c = codes(&s);
for want in [
"UnknownContext",
"PrecisionAndDecimalsAbsent",
"FormatAbsentNegativeNumber",
"DanglingContinuation",
"UnreferencedContinuation",
] {
assert!(c.iter().any(|x| x == want), "missing {} in {:?}", want, c);
}
}
#[test]
fn unbound_format_prefix_is_undefined() {
let s = load_body(
r#"<p><ix:nonFraction name="e:a" contextRef="c" unitRef="u" decimals="0" format="nope:num-dot-decimal">1</ix:nonFraction></p>"#,
);
assert_eq!(codes(&s), vec!["FormatUndefined"]);
}
#[test]
fn fraction_terms() {
let s = load_body(
r#"<p><ix:fraction name="e:f" contextRef="c" unitRef="u"><ix:numerator scale="1">3</ix:numerator>/<ix:denominator>4</ix:denominator></ix:fraction></p>"#,
);
assert!(s.diags.is_empty(), "{:?}", codes(&s));
assert_eq!(
s.instances[0].facts[0].fraction,
Some(("30".to_string(), "4".to_string()))
);
}
#[test]
fn targets_get_their_own_instance() {
let head = HEAD.replace("</ix:references>", "</ix:references><ix:references target=\"T\" xml:lang=\"fr\"><link:schemaRef xlink:type=\"simple\" xlink:href=\"t.xsd\"/></ix:references>");
let s = load_text(&format!(
"{}{}</body></html>",
head,
r#"<p><ix:nonNumeric name="e:a" contextRef="c">d</ix:nonNumeric><ix:nonNumeric name="e:b" contextRef="c" target="T">t</ix:nonNumeric></p>"#
));
assert!(s.diags.is_empty(), "{:?}", codes(&s));
assert_eq!(s.instances.len(), 2);
assert_eq!(s.instances[0].facts.len(), 1);
assert_eq!(&*s.instances[1].facts[0].concept.local, "b");
assert_eq!(s.instances[1].facts[0].lang.as_deref(), Some("fr"));
assert!(s.instances[1].schema_refs[0].ends_with("t.xsd"));
}
#[test]
fn schema_level_checks() {
let s = load_body(
r#"<p><ix:nonNumeric name="e:a" contextRef="c" foo="x">a</ix:nonNumeric><ix:hidden><ix:nonNumeric name="e:b" contextRef="c">b</ix:nonNumeric></ix:hidden></p>"#,
);
let c = codes(&s);
assert!(
c.contains(&"xbrl.core.xml.SchemaValidationError.cvc-complex-type_3_2_2".to_string()),
"{:?}",
c
);
assert!(
c.contains(&"xbrl.core.xml.SchemaValidationError.cvc-complex-type_2_4_a".to_string()),
"{:?}",
c
);
}