use std::fs;
use std::path::Path;
use stepq::info::Info;
use stepq::model::{Graph, Placement, ProductStructure};
use stepq::p21::parse;
fn structure_of(relative: &str) -> Option<(ProductStructure, Info)> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures")
.join(relative);
let Ok(src) = fs::read(&path) else {
eprintln!(
"skipping: {} not fetched (run tools/fetch-fixtures.sh)",
path.display()
);
return None;
};
let graph = Graph::new(parse(&src).unwrap()).unwrap();
Some((ProductStructure::new(&graph), Info::new(&graph)))
}
#[test]
fn as1_has_the_same_structure_from_every_exporter() {
for file in [
"steptools/as1-ac-214.stp",
"steptools/as1-ec-214.stp",
"steptools/as1-md-214.stp",
"steptools/as1-tc-214.stp",
"steptools/as1-ug-214.stp",
] {
let Some((structure, _)) = structure_of(file) else {
continue;
};
assert_eq!(structure.usages().len(), 13, "{file}");
let roots: Vec<usize> = structure
.roots()
.iter()
.copied()
.filter(|&root| structure.children(root).len() > 0)
.collect();
assert_eq!(roots.len(), 1, "{file}: one top-level assembly");
let mut quantities: Vec<f64> = structure
.bill_of_materials(roots[0])
.iter()
.map(|line| line.quantity)
.collect();
quantities.sort_by(f64::total_cmp);
let quantities: Vec<String> = quantities.iter().map(ToString::to_string).collect();
assert_eq!(
quantities,
["1", "1", "1", "2", "2", "6", "6", "8"],
"{file}"
);
assert!(
structure.usages().iter().all(|u| u.placement.is_some()),
"{file}: every usage is placed"
);
}
}
#[test]
fn assembly_fixtures_are_consistent_with_info() {
for file in [
"steptools/as1-ac-214.stp",
"steptools/as1-ec-214.stp",
"steptools/as1-md-214.stp",
"steptools/as1-tc-214.stp",
"steptools/as1-ug-214.stp",
"nist-edm/as1_pe.stp",
"nist-edm/moon_buggy_asm.stp",
"nist-edm/vaccase_asm_solid.stp",
"nist-edm/weldment_asm_solid.stp",
] {
let Some((structure, info)) = structure_of(file) else {
continue;
};
assert_eq!(structure.usages().len(), info.assembly_usages, "{file}");
assert!(
structure.usages().iter().all(|u| u.parent != u.child),
"{file}"
);
let reversed = structure
.usages()
.iter()
.filter(|u| u.placement.as_ref().and_then(Placement::reversed) == Some(true))
.count();
let placed = structure
.usages()
.iter()
.filter(|u| u.placement.is_some())
.count();
let components: usize = structure
.roots()
.iter()
.map(|&root| structure.bill_of_materials(root).len())
.sum();
eprintln!(
"{file}: {} definitions, {} usages ({placed} placed, {reversed} reversed), {} top-level, {components} BOM lines",
structure.definitions().len(),
structure.usages().len(),
structure.roots().len(),
);
assert!(components > 0, "{file}");
if file.ends_with("moon_buggy_asm.stp") {
assert!(
structure
.usages()
.iter()
.all(|u| matches!(u.placement, Some(Placement::MappedItem { .. }))),
"{file}: every usage is placed through a mapped item"
);
}
}
}