Skip to content
Snippets Groups Projects
Commit 9fdb7d97 authored by Nick Mathewson's avatar Nick Mathewson :fire:
Browse files

More tests for tor-config.

parent 54de7f5c
No related branches found
Tags tor-0.3.3.4-alpha
No related merge requests found
......@@ -57,3 +57,57 @@ impl ConfigBuildError {
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn within() {
let e1 = ConfigBuildError::MissingField {
field: "lettuce".to_owned(),
};
let e2 = ConfigBuildError::Invalid {
field: "tomato".to_owned(),
problem: "too crunchy".to_owned(),
};
let e3 = ConfigBuildError::Inconsistent {
fields: vec!["mayo".to_owned(), "avocado".to_owned()],
problem: "pick one".to_owned(),
};
assert_eq!(
&e1.within("sandwich").to_string(),
"Field was not provided: sandwich.lettuce"
);
assert_eq!(
&e2.within("sandwich").to_string(),
"Value of sandwich.tomato was incorrect: too crunchy"
);
assert_eq!(
&e3.within("sandwich").to_string(),
r#"Fields ["sandwich.mayo", "sandwich.avocado"] are inconsistent: pick one"#
);
}
#[derive(derive_builder::Builder, Debug)]
#[builder(build_fn(error = "ConfigBuildError"))]
struct Cephalopod {
// arms have suction cups for their whole length
arms: u8,
// Tentacles have suction cups at the ends
tentacles: u8,
}
#[test]
fn builderr() {
let squid = CephalopodBuilder::default().arms(8).tentacles(2).build();
let octopus = CephalopodBuilder::default().arms(8).build();
assert!(squid.is_ok());
assert!(octopus.is_err());
assert_eq!(
&octopus.unwrap_err().to_string(),
"Field was not provided: tentacles"
);
}
}
......@@ -130,7 +130,7 @@ fn get_env(var: &str) -> Result<Option<&'static str>, CfgPathError> {
impl std::fmt::Display for CfgPath {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0 {
PathInner::Literal(p) => write!(fmt, "{:?}", p),
PathInner::Literal(p) => write!(fmt, "{:?} [exactly]", p),
PathInner::Shell(s) => s.fmt(fmt),
}
}
......@@ -200,6 +200,21 @@ mod test {
let p = CfgPath::new("${ARTI_WOMBAT}/example".to_string());
assert_eq!(p.to_string(), "${ARTI_WOMBAT}/example".to_string());
assert!(p.path().is_err());
assert!(matches!(p.path(), Err(CfgPathError::UnknownVar(_))));
assert_eq!(
&p.path().unwrap_err().to_string(),
"unrecognized variable ARTI_WOMBAT"
);
}
#[test]
fn literal() {
let p = CfgPath::from_path(PathBuf::from("${ARTI_CACHE}/literally"));
// This doesn't get expanded, since we're using a literal path.
assert_eq!(
p.path().unwrap().to_str().unwrap(),
"${ARTI_CACHE}/literally"
);
assert_eq!(p.to_string(), "\"${ARTI_CACHE}/literally\" [exactly]");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment