Use macro per-test-file

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-12 18:15:20 +01:00
parent 605798674f
commit 06816567ff
49 changed files with 213 additions and 271 deletions

View file

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::path::Path;
use nomo::Context;
@ -8,57 +9,55 @@ struct Info {
context: HashMap<String, serde_json::Value>,
}
#[test]
fn check_cases() {
insta::glob!("cases/*.nomo", |path| {
let mut settings = insta::Settings::clone_current();
settings.set_snapshot_path("cases");
settings.set_snapshot_suffix(path.file_stem().unwrap().display().to_string());
settings.set_prepend_module_to_snapshot(false);
test_each_file::test_each_path! { for ["nomo"] in "./tests/cases/" => check_for_input }
fn check_for_input([path]: [&Path; 1]) {
let mut settings = insta::Settings::clone_current();
settings.set_snapshot_path("cases");
settings.set_prepend_module_to_snapshot(false);
let input = std::fs::read_to_string(path).unwrap();
let basename = path.file_stem().unwrap().to_string_lossy();
let input = std::fs::read_to_string(path).unwrap();
let (context, input) = input.split_once("\n---\n").unwrap_or_else(|| ("", &input));
let (context, input) = input.split_once("\n---\n").unwrap_or_else(|| ("", &input));
let map = if !context.is_empty() {
serde_json::from_str::<HashMap<String, serde_json::Value>>(context).unwrap()
} else {
HashMap::new()
};
let map = if !context.is_empty() {
serde_json::from_str::<HashMap<String, serde_json::Value>>(context).unwrap()
} else {
HashMap::new()
};
settings.set_info(&Info {
input: input.to_string(),
context: map.clone(),
});
let mut context = Context::new();
for (k, v) in map {
context.try_insert(k, v).unwrap();
}
let parsed = nomo::parser::parse(input.into()).unwrap();
let _guard = settings.bind_to_scope();
insta::assert_debug_snapshot!("1-parsed", parsed);
let ast = match nomo::ast::parse(parsed.tokens()) {
Ok(ast) => ast,
Err(err) => {
eprintln!("{}", err.to_report(input));
panic!("Could not evaluate ast");
}
};
insta::assert_debug_snapshot!("2-ast", ast);
let emit = nomo::emit::emit_machine(ast);
insta::assert_debug_snapshot!("3-instructions", emit);
let output = nomo::eval::execute(&emit, &context).unwrap();
insta::assert_debug_snapshot!("4-output", output);
settings.set_info(&Info {
input: input.to_string(),
context: map.clone(),
});
let mut context = Context::new();
for (k, v) in map {
context.try_insert(k, v).unwrap();
}
let parsed = nomo::parser::parse(input.into()).unwrap();
let _guard = settings.bind_to_scope();
insta::assert_debug_snapshot!(format!("{basename}.1-parsed"), parsed);
let ast = match nomo::ast::parse(parsed.tokens()) {
Ok(ast) => ast,
Err(err) => {
eprintln!("{}", err.to_report(input));
panic!("Could not evaluate ast");
}
};
insta::assert_debug_snapshot!(format!("{basename}.2-ast"), ast);
let emit = nomo::emit::emit_machine(ast);
insta::assert_debug_snapshot!(format!("{basename}.3-instructions"), emit);
let output = nomo::eval::execute(&emit, &context).unwrap();
insta::assert_debug_snapshot!(format!("{basename}.4-output"), output);
}