Skip to main content

ixtargets/
ixtargets.rs

1//! Per-target check of the Inline XBRL conformance suite's multi-output variations: each
2//! expected `<instance target="...">` is compared with the extracted instance of that
3//! target (canonical facts, without a DTS on either side).
4//!
5//! usage: cargo run --release -p crossfoot-inline --example ixtargets -- <suite tests dir>
6
7use std::path::Path;
8use std::sync::Arc;
9
10use crossfoot_core::{canonical, xml, Dts};
11
12fn parse(p: &Path) -> Option<Arc<xml::Document>> {
13    let bytes = std::fs::read(p).ok()?;
14    xml::parse(
15        &crossfoot_core::resolve::path_to_uri(p),
16        &bytes,
17        &Default::default(),
18    )
19    .ok()
20    .map(Arc::new)
21}
22
23fn main() {
24    let dir = std::env::args().nth(1).expect("tests dir");
25    let mut checked = 0;
26    let mut failed = 0;
27    let mut cases: Vec<_> = Vec::new();
28    for area in std::fs::read_dir(&dir).unwrap().flatten() {
29        if !area.path().is_dir() {
30            continue;
31        }
32        for f in std::fs::read_dir(area.path()).unwrap().flatten() {
33            let p = f.path();
34            let name = p.file_name().unwrap().to_string_lossy().to_string();
35            if name.ends_with(".xml") && !name.contains("predictedOutput") && name != "index.xml" {
36                cases.push(p);
37            }
38        }
39    }
40    cases.sort();
41    let dts = Dts::empty();
42    for tc in cases {
43        let Some(doc) = parse(&tc) else { continue };
44        let root = doc.document_element().unwrap();
45        let tdir = tc.parent().unwrap();
46        for v in root.elements().filter(|e| e.local() == "variation") {
47            let Some(result) = v.elements().find(|e| e.local() == "result") else {
48                continue;
49            };
50            let exp: Vec<(Option<String>, String)> = result
51                .elements()
52                .filter(|e| e.local() == "instance")
53                .map(|e| {
54                    (
55                        e.att("target").map(|s| s.to_string()),
56                        e.string_value().trim().to_string(),
57                    )
58                })
59                .collect();
60            if exp.len() < 2 && exp.iter().all(|(t, _)| t.is_none()) {
61                continue;
62            }
63            let data = v.elements().find(|e| e.local() == "data").unwrap();
64            let docs: Vec<Arc<xml::Document>> = data
65                .elements()
66                .filter(|e| e.local() == "instance")
67                .filter_map(|e| parse(&tdir.join(e.string_value().trim())))
68                .collect();
69            let set = crossfoot_inline::load_document_set(docs);
70            for (target, file) in &exp {
71                checked += 1;
72                let Some(edoc) = parse(&tdir.join(file)) else {
73                    continue;
74                };
75                let (einst, _) = crossfoot_instance::load_instance(edoc, &dts);
76                let want = canonical::dump(&dts, &einst);
77                let got = set
78                    .instances
79                    .iter()
80                    .find(|i| matches!(&i.source, crossfoot_core::InstanceSource::Inline { target: t, .. } if t.as_deref() == target.as_deref()))
81                    .map(|i| canonical::dump(&dts, i))
82                    .unwrap_or_default();
83                if want != got {
84                    failed += 1;
85                    println!("DIFF {} target={:?}", tc.display(), target);
86                    for f in want.iter().filter(|f| !got.contains(f)) {
87                        println!("   - {:?}", f);
88                    }
89                    for f in got.iter().filter(|f| !want.contains(f)) {
90                        println!("   + {:?}", f);
91                    }
92                }
93            }
94        }
95    }
96    println!("{} target outputs checked, {} differ", checked, failed);
97}