better error assertions

This commit is contained in:
Jörn-Michael Miehe 2025-07-15 14:54:27 +00:00
parent 9e56779378
commit 147c78377a
4 changed files with 54 additions and 40 deletions

View file

@ -40,7 +40,23 @@ impl_param_from! {
impl Parameter {
fn is_fatal(&self) -> bool {
matches!(self, Self::Uri(_) | Self::AliasID(_))
self.is_uri() || self.is_alias_id()
}
pub fn is_uri(&self) -> bool {
matches!(self, Self::Uri(_))
}
pub fn is_alias_id(&self) -> bool {
matches!(self, Self::AliasID(_))
}
pub fn is_share_id(&self) -> bool {
matches!(self, Self::ShareID(_))
}
pub fn is_file_id(&self) -> bool {
matches!(self, Self::FileID(_))
}
}
@ -123,17 +139,33 @@ impl Error {
})
}
pub fn is_stdio_kind(&self, kind: std::io::ErrorKind) -> bool {
if let Self::StdIo(e) = self {
e.kind() == kind
} else {
false
}
}
pub fn is_mismatch<E, A>(&self, want_expected: E, want_actual: A) -> bool
where
E: AsRef<str>,
A: AsRef<str>,
{
matches!(
self,
Self::Mismatch { expected, actual }
if expected == want_expected.as_ref()
&& actual == want_actual.as_ref()
)
if let Self::Mismatch { expected, actual } = self {
expected == want_expected.as_ref() && actual == want_actual.as_ref()
} else {
false
}
}
#[must_use]
pub fn response_contains(&self, pat: &str) -> bool {
if let Self::Response(r) = self {
r.contains(pat)
} else {
false
}
}
#[must_use]

View file

@ -158,7 +158,7 @@ mod tests {
};
let err = compute_hash(&nex_path, 0, drop).unwrap_err();
assert!(matches!(err, crate::Error::StdIo(e) if e.kind() == std::io::ErrorKind::NotFound));
assert!(err.is_stdio_kind(std::io::ErrorKind::NotFound));
}
#[test]
@ -166,9 +166,7 @@ mod tests {
let dir = TempDir::new().unwrap();
let err = compute_hash(dir.path(), 0, drop).unwrap_err();
assert!(
matches!(err, crate::Error::StdIo(e) if e.kind() == std::io::ErrorKind::IsADirectory)
);
assert!(err.is_stdio_kind(std::io::ErrorKind::IsADirectory));
}
#[test]

View file

@ -224,9 +224,7 @@ mod tests {
for share_id in share_ids.as_ref() {
let res = client.insert_share(&share_id, MockShare::default());
assert!(res.is_err());
assert!(
matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't insert share"))
);
assert!(res.unwrap_err().response_contains("can't insert share"));
}
}
@ -250,9 +248,7 @@ mod tests {
let res = client.get_share(share_id_nex);
assert!(res.is_err());
assert!(
matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't find share"))
);
assert!(res.unwrap_err().response_contains("can't find share"));
}
}
@ -284,7 +280,7 @@ mod tests {
let share_id_nex = ShareID::default();
let res = client.insert_file(&share_id_nex, &FileID::from(true), MockFile::default());
assert!(res.is_err());
assert!(matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't find share")));
assert!(res.unwrap_err().response_contains("can't find share"));
}
#[test]
@ -296,9 +292,7 @@ mod tests {
for file_id in file_ids.as_ref() {
let res = client.insert_file(&share_id, &file_id, MockFile::default());
assert!(res.is_err());
assert!(
matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't insert file"))
);
assert!(res.unwrap_err().response_contains("can't insert file"));
}
}
@ -325,16 +319,12 @@ mod tests {
let res = client.get_file(&share_id_nex, file_id_nex);
assert!(res.is_err());
assert!(
matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't find share"))
);
assert!(res.unwrap_err().response_contains("can't find share"));
let res = client.get_file(&share_id, file_id_nex);
assert!(res.is_err());
assert!(
matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't find file"))
);
assert!(res.unwrap_err().response_contains("can't find file"));
}
}
}

View file

@ -160,23 +160,17 @@ mod tests {
let share_id_i = ShareID::from(false);
let file_id_i = FileID::from(false);
// param checks
let is_uri_i = |p: &Parameter| matches!(p, Parameter::Uri(_));
let is_alias_id_i = |p: &Parameter| matches!(p, Parameter::AliasID(_));
let is_share_id_i = |p: &Parameter| matches!(p, Parameter::ShareID(_));
let is_file_id_i = |p: &Parameter| matches!(p, Parameter::FileID(_));
// uri + alias
test_check((&uri_i, &alias_id_i), is_uri_i);
test_check((&uri_i, &alias_id), is_uri_i);
test_check((&uri, &alias_id_i), is_alias_id_i);
test_check((&uri_i, &alias_id_i), Parameter::is_uri);
test_check((&uri_i, &alias_id), Parameter::is_uri);
test_check((&uri, &alias_id_i), Parameter::is_alias_id);
// share
test_check(&share_id_i, is_share_id_i);
test_check(&share_id_i, Parameter::is_share_id);
// share + file
test_check((&share_id_i, &file_id_i), is_share_id_i);
test_check((&share_id_i, &file_id), is_share_id_i);
test_check((&share_id, &file_id_i), is_file_id_i);
test_check((&share_id_i, &file_id_i), Parameter::is_share_id);
test_check((&share_id_i, &file_id), Parameter::is_share_id);
test_check((&share_id, &file_id_i), Parameter::is_file_id);
}
}