From 61d62d731e34de660bc6b474c326ce40df903be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 3 Jul 2025 17:20:56 +0000 Subject: [PATCH] [wip] unit tests for `file` module - testing for `compute_hash` and `check_hash` --- src/error.rs | 12 +++++++++++ src/file/mod.rs | 51 +++++++++++++++++++++++++---------------------- src/sharry/ids.rs | 14 ++++--------- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/error.rs b/src/error.rs index 400776f..58211b7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -121,6 +121,18 @@ impl Error { } } + pub fn is_mismatch(&self, has_expected: E, has_actual: A) -> bool + where + String: PartialEq + PartialEq, + { + matches!( + self, + Self::Mismatch { expected, actual } + if *expected == has_expected + && *actual == has_actual + ) + } + #[must_use] pub fn get_invalid_param(&self) -> Option<&Parameter> { if let Self::InvalidParameter(p) = self { diff --git a/src/file/mod.rs b/src/file/mod.rs index 9ff13e0..9340892 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -154,26 +154,35 @@ mod tests { let file = create_file(content); let callback = drop; - let err_callback = |err| { + { + let err = compute_hash(file.path(), bad_size, callback) + .expect_err("compute_hash should report a mismatch"); + // check error - match err { - crate::Error::Mismatch { expected, actual } => { - assert_eq!(expected, bad_size.to_string()); - assert_eq!(actual, good_size.to_string()); - } - _ => panic!( - "Expected Error::Mismatch for input size `{bad_size}` but got {err:?}" - ), - }; - }; + assert!(err.is_mismatch(bad_size.to_string(), good_size.to_string())); + } - compute_hash(file.path(), bad_size, callback) - .map_err(err_callback) - .expect_err("compute_hash should report a mismatch"); + { + let err = check_hash(file.path(), bad_size, Some("foobar"), callback) + .expect_err("check_hash should report a mismatch"); - check_hash(file.path(), bad_size, Some("foobar"), callback) - .map_err(err_callback) + // check error + assert!(err.is_mismatch(bad_size.to_string(), good_size.to_string())); + } + } + } + + #[test] + fn hash_value_none() { + for (content, size) in CASES { + let file = create_file(content); + let callback = drop; + + let err = check_hash(file.path(), size, None, callback) .expect_err("check_hash should report a mismatch"); + + // check error + assert!(err.is_mismatch("hash", file.path().display().to_string())); } } @@ -190,17 +199,11 @@ mod tests { let file = create_file(content); let callback = drop; - let err = check_hash(file.path(), size, Some(&bad_hash.to_string()), callback) + let err = check_hash(file.path(), size, Some(bad_hash), callback) .expect_err("check_hash should report a mismatch"); // check error - match err { - crate::Error::Mismatch { expected, actual } => { - assert_eq!(expected, bad_hash); - assert_eq!(actual, good_hash); - } - _ => panic!("Expected Error::Mismatch but got {err:?}"), - } + assert!(err.is_mismatch(bad_hash, good_hash)); } } } diff --git a/src/sharry/ids.rs b/src/sharry/ids.rs index 3fc3dfd..e9eae98 100644 --- a/src/sharry/ids.rs +++ b/src/sharry/ids.rs @@ -185,16 +185,10 @@ mod tests { for bad in bad_inputs { let err = FileID::try_from(bad.to_string()).expect_err("URL should not parse"); // make sure it's the Mismatch variant, and that it contains the original input - match err { - crate::Error::Mismatch { expected, actual } => { - assert_eq!( - expected, ":///api/v2/alias/upload//files/tus/", - "Error should output expected format" - ); - assert_eq!(actual, bad, "Error should echo back the input"); - } - _ => panic!("Expected Error::Mismatch for input `{bad}` but got {err:?}"), - } + assert!(err.is_mismatch( + ":///api/v2/alias/upload//files/tus/", + bad + )); } } }