From 903fbc3480ea9605000d642ad3a191cf23a8a63b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Fri, 27 Jun 2025 16:59:31 +0000 Subject: [PATCH] [wip] unit tests for `sharry` module --- src/sharry/ids.rs | 67 ++++++++++++++++++++++++++++++++++++++++++---- src/sharry/json.rs | 28 ++++++++++++++++++- src/sharry/uri.rs | 32 ++++++++++++++++++++++ 3 files changed, 121 insertions(+), 6 deletions(-) diff --git a/src/sharry/ids.rs b/src/sharry/ids.rs index 6f9121d..b6e21d8 100644 --- a/src/sharry/ids.rs +++ b/src/sharry/ids.rs @@ -93,6 +93,64 @@ impl TryFrom for FileID { mod tests { use super::*; + #[test] + fn basic_traits_working() { + let inputs = vec![ + "", + "abcd", + "12345", + "8woeurx09wp", + "6NHNiSVFhZF-dSGbX8iD8ib-Pdb7TbzpsvC-uBBSCyExxb", + ]; + + for input in inputs { + { + // check AliasID + let _ = AliasID(input.to_owned()); + let aid = AliasID::from(input.to_owned()); // "From" trait + assert_eq!( + aid.to_string(), + input, + "`impl Display for AliasID` expected: {:?}, got {:?}", + input, + aid.to_string(), + ); + assert_eq!( + aid.as_ref(), + input.as_bytes(), + "`impl AsRef<[u8]> for AliasID` expected: {:?}, got {:?}", + input.as_bytes(), + aid.as_ref(), + ); + } + + { + // check ShareID + let _ = ShareID(input.to_owned()); + let sid = ShareID::from(input.to_owned()); // "From" trait + assert_eq!( + sid.to_string(), + input, + "`impl Display for ShareID` expected: {:?}, got {:?}", + input, + sid.to_string(), + ); + } + + { + // check FileID + let fid = FileID(input.to_owned()); + assert_eq!( + fid.to_string(), + input, + "`impl Display for FileID` expected: {:?}, got {:?}", + fid.to_string(), + input + ); + } + } + } + #[test] fn valid_urls_produce_expected_file_id() { // a handful of valid‐looking URLs @@ -112,12 +170,11 @@ mod tests { ]; for (good, expected_fid) in cases { - let s = good.to_string(); + let s = good.to_owned(); let file_id = FileID::try_from(s.clone()).expect("URL should parse successfully"); assert_eq!( file_id.0, expected_fid, - "Expected `{}` → FileID({}), got {:?}", - good, expected_fid, file_id + "Expected `{good}` → FileID({expected_fid}), got {file_id:?}", ); } } @@ -138,7 +195,7 @@ mod tests { ]; for bad in bad_inputs { - let err = FileID::try_from(bad.to_string()).expect_err("URL should not parse"); + let err = FileID::try_from(bad.to_owned()).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 } => { @@ -146,7 +203,7 @@ mod tests { expected, ":///api/v2/alias/upload//files/tus/", "Error should output expected format" ); - assert_eq!(actual, bad.to_string(), "Error should echo back the input"); + assert_eq!(actual, bad.to_owned(), "Error should echo back the input"); } _ => panic!("Expected Error::Mismatch for input `{bad}` but got {err:?}"), } diff --git a/src/sharry/json.rs b/src/sharry/json.rs index 2f6f680..5e523d9 100644 --- a/src/sharry/json.rs +++ b/src/sharry/json.rs @@ -1,7 +1,7 @@ +#![allow(non_snake_case)] use serde::{Deserialize, Serialize}; #[derive(Serialize, Debug)] -#[allow(non_snake_case)] pub struct NewShareRequest { name: String, validity: u32, @@ -39,3 +39,29 @@ pub struct NotifyShareResponse { pub success: bool, pub message: String, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn nsreq_new_sets_fields_correctly() { + let name = "myname"; + let desc = "desc"; + let views = 7; + + let req = NewShareRequest::new(name, Some(desc), views); + + assert_eq!(req.name, name); + assert_eq!(req.validity, 0); + assert_eq!(req.description.as_deref(), Some(desc)); + assert_eq!(req.maxViews, views); + assert!(req.password.is_none()); + } + + #[test] + fn nsreq_new_allows_none_description() { + let req = NewShareRequest::new("whatever", None::<&str>, 0); + assert!(req.description.is_none()); + } +} diff --git a/src/sharry/uri.rs b/src/sharry/uri.rs index ea3e647..12a9095 100644 --- a/src/sharry/uri.rs +++ b/src/sharry/uri.rs @@ -45,3 +45,35 @@ impl Uri { self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}")) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn basic_tests() { + let cases = vec![ + ("http", "example.com", "http://example.com"), + ("https", "my-host:8080", "https://my-host:8080"), + ("custom+scheme", "host", "custom+scheme://host"), + ]; + + for (protocol, base_url, display) in cases { + let uri = Uri::new(protocol, base_url); + assert_eq!( + uri.to_string(), + display, + "`impl Display for Uri` expected: {:?}, got {:?}", + display, + uri.to_string(), + ); + assert_eq!( + uri.as_ref(), + display.as_bytes(), + "`impl AsRef<[u8]> for Uri` expected: {:?}, got {:?}", + display.as_bytes(), + uri.as_ref(), + ); + } + } +}