mod errors; mod interpretation; mod locations; mod parsing; use crate::Configuration; fn do_tests_in_dir( dir: &str, ext: &str, test: impl Fn(&str) -> Result<(), Box>, ) { let result = (|| -> Result<(), Box> { 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?" ) })?; 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}")) .map_err(|e| format!("{dir}/{filename}: {e}"))?; } } Ok(()) })(); if let Err(e) = result { panic!("Error: {e}"); } } #[test] fn misc() { let conf = Configuration::load( "", " a = yes [foo] x = 5 bar.y = 6 " .as_bytes(), ) .unwrap(); // test section assert_eq!(conf.section("foo").get("x"), Some("5")); assert_eq!(conf.section("foo").get("bar.y"), Some("6")); assert_eq!(conf.section("foo.bar").get("y"), Some("6")); assert_eq!(conf.section("fob").get("x"), None); // test get_or_default assert_eq!(conf.get_or_default("foo.x", ""), "5"); assert_eq!(conf.get_or_default("foo.y", ""), ""); assert_eq!(conf.get_int_or_default("foo.x", 0).unwrap(), 5); assert_eq!(conf.get_int_or_default("foo.y", 0).unwrap(), 0); assert_eq!(conf.get_float_or_default("foo.x", 0.0).unwrap(), 5.0); assert_eq!(conf.get_float_or_default("foo.y", 0.0).unwrap(), 0.0); assert!(conf.get_bool_or_default("a", false).unwrap()); assert!(!conf.get_bool_or_default("aa", false).unwrap()); assert_eq!(conf.get_list_or_default("a", ["a"]), ["yes"]); assert_eq!(conf.get_list_or_default("aa", ["a"]), ["a"]); assert_eq!(conf.get_list_or_empty("a"), ["yes"]); assert!(conf.get_list_or_empty("aa").is_empty()); let mut keys: Vec<_> = conf.keys().collect(); keys.sort(); assert_eq!(keys, ["a", "foo"]); }