use std::path::Path;
use std::sync::Arc;
use crossfoot_core::{canonical, xml, Dts};
fn parse(p: &Path) -> Option<Arc<xml::Document>> {
let bytes = std::fs::read(p).ok()?;
xml::parse(
&crossfoot_core::resolve::path_to_uri(p),
&bytes,
&Default::default(),
)
.ok()
.map(Arc::new)
}
fn main() {
let dir = std::env::args().nth(1).expect("tests dir");
let mut checked = 0;
let mut failed = 0;
let mut cases: Vec<_> = Vec::new();
for area in std::fs::read_dir(&dir).unwrap().flatten() {
if !area.path().is_dir() {
continue;
}
for f in std::fs::read_dir(area.path()).unwrap().flatten() {
let p = f.path();
let name = p.file_name().unwrap().to_string_lossy().to_string();
if name.ends_with(".xml") && !name.contains("predictedOutput") && name != "index.xml" {
cases.push(p);
}
}
}
cases.sort();
let dts = Dts::empty();
for tc in cases {
let Some(doc) = parse(&tc) else { continue };
let root = doc.document_element().unwrap();
let tdir = tc.parent().unwrap();
for v in root.elements().filter(|e| e.local() == "variation") {
let Some(result) = v.elements().find(|e| e.local() == "result") else {
continue;
};
let exp: Vec<(Option<String>, String)> = result
.elements()
.filter(|e| e.local() == "instance")
.map(|e| {
(
e.att("target").map(|s| s.to_string()),
e.string_value().trim().to_string(),
)
})
.collect();
if exp.len() < 2 && exp.iter().all(|(t, _)| t.is_none()) {
continue;
}
let data = v.elements().find(|e| e.local() == "data").unwrap();
let docs: Vec<Arc<xml::Document>> = data
.elements()
.filter(|e| e.local() == "instance")
.filter_map(|e| parse(&tdir.join(e.string_value().trim())))
.collect();
let set = crossfoot_inline::load_document_set(docs);
for (target, file) in &exp {
checked += 1;
let Some(edoc) = parse(&tdir.join(file)) else {
continue;
};
let (einst, _) = crossfoot_instance::load_instance(edoc, &dts);
let want = canonical::dump(&dts, &einst);
let got = set
.instances
.iter()
.find(|i| matches!(&i.source, crossfoot_core::InstanceSource::Inline { target: t, .. } if t.as_deref() == target.as_deref()))
.map(|i| canonical::dump(&dts, i))
.unwrap_or_default();
if want != got {
failed += 1;
println!("DIFF {} target={:?}", tc.display(), target);
for f in want.iter().filter(|f| !got.contains(f)) {
println!(" - {:?}", f);
}
for f in got.iter().filter(|f| !want.contains(f)) {
println!(" + {:?}", f);
}
}
}
}
}
println!("{} target outputs checked, {} differ", checked, failed);
}