use crate::Configuration; use std::error::Error; use std::fmt::Display; fn do_list_test(_cfg: &Configuration) -> Result<(), Box> { todo!() } fn do_goodbad_test( cfg: &Configuration, name: &str, parser: impl Fn(&Configuration, &str) -> Option>, ) -> Result<(), Box> { let good = cfg.section("good"); for key in good.keys() { let [a_value, b_value] = ["a", "b"].map(|c| { parser(&good, &format!("{key}.{c}")) .ok_or_else(|| format!("expected to find key good.{key}.{c}"))? .map_err(|e| format!("parsing good.{key}.{c} failed: {e}")) }); let a_value = a_value?; let b_value = b_value?; if a_value != b_value { Err(format!( "{name} incorrectly parsed: good.{key}.a = {a_value}, but good.{key}.b = {b_value}" ))?; } } let bad = cfg.section("bad"); for (key, val) in bad.iter() { if parser(&bad, key) .ok_or_else(|| format!("parsing bad.{key} failed: {key} not found"))? .is_ok() { Err(format!( "bad.{key} value {val:?} should be rejected as a(n) {name} but it wasn't" ))?; } } Ok(()) } #[test] fn interpretation() { super::do_tests_in_dir("interpretation", ".pom", |filename| { let cfg = Configuration::load_path(filename)?; if filename.ends_with("list.pom") { do_list_test(&cfg) } else if filename.ends_with("uint.pom") { do_goodbad_test(&cfg, "uint", Configuration::get_uint) } else if filename.ends_with("int.pom") { do_goodbad_test(&cfg, "int", Configuration::get_int) } else if filename.ends_with("float.pom") { do_goodbad_test(&cfg, "float", Configuration::get_float) } else if filename.ends_with("bool.pom") { do_goodbad_test(&cfg, "bool", Configuration::get_bool) } else { panic!("unrecognized test: {filename}") } }) }