crossfoot-inline 0.1.0

Inline XBRL 1.1 document sets and the transformation registry (Crossfoot)
Documentation
//! 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))
}

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 &amp; <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 &amp; <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
    );
}