summaryrefslogtreecommitdiff
path: root/src/tests/parsing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/parsing.rs')
-rw-r--r--src/tests/parsing.rs63
1 files changed, 38 insertions, 25 deletions
diff --git a/src/tests/parsing.rs b/src/tests/parsing.rs
index eb98a04..97cf711 100644
--- a/src/tests/parsing.rs
+++ b/src/tests/parsing.rs
@@ -1,28 +1,39 @@
use crate::Configuration;
+use std::collections::HashSet;
-static TEST_DIR: &str = "../tests/parsing";
-
-fn single_test(flat_name: &str, complex_name: &str) -> Result<(), Box<dyn std::error::Error>> {
- println!("Running test {complex_name}...");
- let config1 = Configuration::load_path(format!("{TEST_DIR}/{flat_name}"))?;
- let config2 = Configuration::load_path(format!("{TEST_DIR}/{complex_name}"))?;
- super::check_configs_equal(&config1, &config2)
-}
-
-fn try_parsing() -> Result<(), Box<dyn std::error::Error>> {
- 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?"
+fn check_configs_equal(
+ cfg1: &Configuration,
+ cfg2: &Configuration,
+) -> Result<(), Box<dyn std::error::Error>> {
+ let keys1: HashSet<&str> = cfg1.iter().map(|(k, _)| k).collect();
+ let keys2: HashSet<&str> = cfg2.iter().map(|(k, _)| k).collect();
+ for key in keys1.difference(&keys2) {
+ return Err(format!(
+ "Key {key} appears at {}, but not in other configuration",
+ cfg1.location(&key).unwrap()
)
- })?;
- 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(".flat.pom") {
- single_test(&filename, filename.replace(".flat.pom", ".pom").as_ref())?;
+ .into());
+ }
+ for key in keys2.difference(&keys1) {
+ return Err(format!(
+ "Key {key} appears at {}, but not in other configuration",
+ cfg2.location(&key).unwrap()
+ )
+ .into());
+ }
+ for (key, val1) in cfg1.iter() {
+ let val2 = cfg2.get(&key).unwrap();
+ if val1 != val2 {
+ return Err(format!(
+ "Mismatch between values for key {key}
+defined as {val1:?} at {},
+but defined as {val2:?} at {}",
+ cfg1.location(&key)
+ .map_or("<nowhere>".into(), |x| format!("{x}")),
+ cfg2.location(&key)
+ .map_or("<nowhere>".into(), |x| format!("{x}")),
+ )
+ .into());
}
}
Ok(())
@@ -30,7 +41,9 @@ fn try_parsing() -> Result<(), Box<dyn std::error::Error>> {
#[test]
fn parsing() {
- if let Err(e) = try_parsing() {
- panic!("Error: {e}");
- }
+ super::do_tests_in_dir("parsing", ".flat.pom", |filename| {
+ let config1 = Configuration::load_path(filename)?;
+ let config2 = Configuration::load_path(&filename.replace(".flat.pom", ".pom"))?;
+ check_configs_equal(&config1, &config2)
+ })
}