diff --git a/src/file/chunk.rs b/src/file/chunk.rs index d3025b8..72cd32d 100644 --- a/src/file/chunk.rs +++ b/src/file/chunk.rs @@ -55,11 +55,33 @@ impl<'t> Chunk<'t> { #[cfg(test)] mod tests { - // use super::*; + use crate::test_util::{ + check_trait, + data::{DATA_LENGTHS_BAD, cases_with}, + }; - // #[test] - // fn basic_tests() { - // let mut foo = [0u8; 10]; - // let fid = sharry::FileID("fid".to_string()); - // } + use super::*; + + #[test] + fn basic_tests() { + for (data, len, mock_offset) in cases_with(DATA_LENGTHS_BAD) { + let fid = sharry::FileID::default(); + let chunk = Chunk::new_direct(fid, mock_offset, data); + + let repr = format!("{chunk:?}"); + let repr_expect = format!( + "Chunk {{ file_id: {:?}, offset: {:?}, data.len(): {:?}, .. }}", + chunk.file_id, + chunk.offset, + chunk.data.len() + ); + check_trait(repr, repr_expect, "Debug", "Chunk"); + + assert_eq!(chunk.get_file_id().to_string(), "fid"); + assert_eq!(chunk.get_offset(), mock_offset); + assert_eq!(chunk.get_data(), data); + assert_eq!(chunk.get_length(), len); + assert_eq!(chunk.get_behind(), mock_offset + len); + } + } } diff --git a/src/sharry/ids.rs b/src/sharry/ids.rs index 36ed596..24412e0 100644 --- a/src/sharry/ids.rs +++ b/src/sharry/ids.rs @@ -28,9 +28,11 @@ impl From for AliasID { /// /// - impl `From` and `Clone` as this is just a String /// - impl `serde` for cachefile handling +/// - impl `Default` while testing for value generation /// - impl `Display` for formatting compatibility #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct ShareID(pub(super) String); +#[cfg_attr(test, derive(Default))] +pub struct ShareID(String); impl fmt::Display for ShareID { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -48,10 +50,20 @@ impl From for ShareID { /// /// - impl `Clone` as this is just a String /// - impl `serde` for cachefile handling +/// - impl `Default` while testing for value generation /// - impl `Display` for formatting compatibility /// - impl `TryFrom` for extracting from matching a "PATCH" uri #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct FileID(pub(super) String); +#[cfg_attr(test, derive(Default))] +pub struct FileID(String); + +impl FileID { + #[cfg(test)] + /// create a new `FileID` for testing purposes + pub fn new_test(value: impl Into) -> Self { + Self(value.into()) + } +} impl fmt::Display for FileID { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -133,6 +145,9 @@ mod tests { // check FileID let fid = FileID(input.to_string()); check_trait(fid.to_string(), input, "Display", "FileID"); + + let fid_test = FileID::new_test(input); + assert_eq!(fid_test.0, fid.0); } } } diff --git a/src/sharry/uri.rs b/src/sharry/uri.rs index 7745264..16dd27d 100644 --- a/src/sharry/uri.rs +++ b/src/sharry/uri.rs @@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize}; /// - impl `Display` for formatting compatibility /// - impl `AsRef<[u8]>` for hashing with `blake2b_simd` #[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(test, derive(Default))] pub struct Uri(String); impl fmt::Display for Uri { @@ -182,7 +183,7 @@ mod tests { ("12345", "/api/v2/12345"), ]; - let uri = Uri("".to_string()); + let uri = Uri::default(); for (path, expected) in cases { assert_eq!(&expected, &uri.endpoint(format_args!("{path}"))); } @@ -190,9 +191,9 @@ mod tests { #[test] fn test_pub_endpoints() { - let uri = Uri("".to_string()); - let share_id = ShareID("sid".to_string()); - let file_id = FileID("fid".to_string()); + let uri = Uri::default(); + let share_id = ShareID::default(); + let file_id = FileID::default(); assert_eq!("/api/v2/alias/upload/new", uri.share_create()); assert_eq!("/api/v2/alias/mail/notify/sid", uri.share_notify(&share_id));