use std::fs;
use std::path::{Path, PathBuf};
use rucc_rules::{Matcher, parse};
use rucc_verify::{Model, query};
const DIRECTORIES: &[&str] = &["../../crates/rucc-codegen/rules", "../../crates/rucc-opt/rules"];
fn rules_dir(at: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join(at).canonicalize().expect("the rule directory")
}
fn files() -> Vec<PathBuf> {
let mut out: Vec<PathBuf> = DIRECTORIES
.iter()
.flat_map(|at| fs::read_dir(rules_dir(at)).expect("the rule directory is readable"))
.map(|entry| entry.expect("an entry").path())
.filter(|path| path.extension().is_some_and(|kind| kind == "rules"))
.collect();
out.sort();
out
}
#[test]
fn there_is_a_rule_set_to_check() {
assert!(
files().len() >= DIRECTORIES.len(),
"a rule directory has no rule file in it, and this test is about them"
);
}
#[test]
fn every_shipped_rule_has_a_question_to_ask() {
for file in files() {
let shown = file.display().to_string();
let text = fs::read_to_string(&file).expect("the rule file is readable");
let rules = match parse(&shown, &text) {
Ok(rules) => rules,
Err(errors) => panic!("{}", errors[0]),
};
if let Err(errors) = Matcher::build(&shown, &rules) {
panic!("{}", errors[0]);
}
let model_path = file.with_extension("model");
let model = match Model::open(&model_path) {
Ok(model) => model,
Err(errors) => panic!("{}", errors[0]),
};
for rule in &rules {
if let Err(problem) = query(&shown, rule, &model) {
panic!("{problem}");
}
}
}
}