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
|
use crate::Configuration;
use std::error::Error;
use std::fmt::Display;
fn do_list_test(_cfg: &Configuration) -> Result<(), Box<dyn Error>> {
todo!()
}
fn do_goodbad_test<I: PartialEq + Display>(
cfg: &Configuration,
name: &str,
parser: impl Fn(&Configuration, &str) -> Option<crate::Result<I>>,
) -> Result<(), Box<dyn Error>> {
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}")
}
})
}
|