diff options
Diffstat (limited to 'src/tests/mod.rs')
-rw-r--r-- | src/tests/mod.rs | 72 |
1 files changed, 24 insertions, 48 deletions
diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 58cd2b0..83873dc 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,56 +1,32 @@ -use crate::Configuration; -use std::collections::HashSet; - +mod locations; mod parsing; -fn check_configs_equal( - cfg1: &Configuration, - cfg2: &Configuration, -) -> Result<(), Box<dyn std::error::Error>> { - fn check_configs_equal_at( - cfg1: &Configuration, - cfg2: &Configuration, - prefix: &str, - ) -> Result<(), Box<dyn std::error::Error>> { - let (keys1, keys2): (HashSet<&str>, HashSet<&str>) = - if let Some(k) = prefix.strip_suffix('.') { - (cfg1.subkeys(k).collect(), cfg2.subkeys(k).collect()) - } else { - (cfg1.keys().collect(), cfg2.keys().collect()) - }; - for key in keys1.difference(&keys2) { - return Err(format!( - "Key {prefix}{key} appears at {}, but not in other configuration", - cfg1.location(&format!("{prefix}{key}")).unwrap() - ) - .into()); - } - for key in keys2.difference(&keys1) { - return Err(format!( - "Key {prefix}{key} appears at {}, but not in other configuration", - cfg2.location(&format!("{prefix}{key}")).unwrap() +fn do_tests_in_dir( + dir: &str, + ext: &str, + test: impl Fn(&str) -> Result<(), Box<dyn std::error::Error>>, +) { + let result = (|| -> Result<(), Box<dyn std::error::Error>> { + let test_dir = format!("../tests/{dir}"); + let tests = std::fs::read_dir(&test_dir).map_err(|e| { + format!( + "error reading {test_dir}: {e} — try moving pom-rs to inside the main pom repository?" ) - .into()); - } - for key in &keys1 { - let full_key = format!("{prefix}{key}"); - let val1 = cfg1.get(&full_key); - let val2 = cfg2.get(&full_key); - if val1 != val2 { - return Err(format!( - "Mismatch between values for key {full_key} - defined as {val1:?} at {}, -but defined as {val2:?} at {}", - cfg1.location(&full_key) - .map_or("<nowhere>".into(), |x| format!("{x}")), - cfg2.location(&full_key) - .map_or("<nowhere>".into(), |x| format!("{x}")), - ) - .into()); + })?; + for test_entry in tests { + let filename = test_entry + .map_err(|e| format!("error reading tests directory {test_dir}: {e}"))? + .file_name() + .into_string() + .expect("bad UTF-8 in test name (file in {test_dir})"); + if filename.ends_with(ext) { + println!("Running test {dir}/{filename}..."); + test(&format!("{test_dir}/{filename}"))? } - check_configs_equal_at(cfg1, cfg2, &format!("{full_key}."))?; } Ok(()) + })(); + if let Err(e) = result { + panic!("Error: {e}"); } - check_configs_equal_at(cfg1, cfg2, "") } |