summaryrefslogtreecommitdiff
path: root/src/tests/mod.rs
blob: 644c131e5631e2afc55ac90f0e08fbcf9ed8f7e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
mod errors;
mod locations;
mod parsing;

use crate::Configuration;

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?"
			)
		})?;
		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(
		"<test configuration>",
		"
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"]);
}