Compare commits

...

2 commits

Author SHA1 Message Date
fcc163c210 [wip] unit tests for file module
- test value generation for `sharry::{id, uri}`
- testing for `Chunk`
2025-07-05 23:02:08 +00:00
4ff1f34b08 refactor check_trait function 2025-07-05 23:00:54 +00:00
4 changed files with 65 additions and 28 deletions

View file

@ -55,11 +55,33 @@ impl<'t> Chunk<'t> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// use super::*; use crate::test_util::{
check_trait,
data::{DATA_LENGTHS_BAD, cases_with},
};
// #[test] use super::*;
// fn basic_tests() {
// let mut foo = [0u8; 10]; #[test]
// let fid = sharry::FileID("fid".to_string()); 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);
}
}
} }

View file

@ -28,9 +28,11 @@ impl From<String> for AliasID {
/// ///
/// - impl `From<String>` and `Clone` as this is just a String /// - impl `From<String>` and `Clone` as this is just a String
/// - impl `serde` for cachefile handling /// - impl `serde` for cachefile handling
/// - impl `Default` while testing for value generation
/// - impl `Display` for formatting compatibility /// - impl `Display` for formatting compatibility
#[derive(Debug, Clone, Serialize, Deserialize)] #[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 { impl fmt::Display for ShareID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -48,10 +50,20 @@ impl From<String> for ShareID {
/// ///
/// - impl `Clone` as this is just a String /// - impl `Clone` as this is just a String
/// - impl `serde` for cachefile handling /// - impl `serde` for cachefile handling
/// - impl `Default` while testing for value generation
/// - impl `Display` for formatting compatibility /// - impl `Display` for formatting compatibility
/// - impl `TryFrom<String>` for extracting from matching a "PATCH" uri /// - impl `TryFrom<String>` for extracting from matching a "PATCH" uri
#[derive(Debug, Clone, Serialize, Deserialize)] #[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<String>) -> Self {
Self(value.into())
}
}
impl fmt::Display for FileID { impl fmt::Display for FileID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -117,24 +129,25 @@ mod tests {
for input in inputs { for input in inputs {
{ {
// check AliasID // check AliasID
let _ = AliasID(input.to_string()); // direct creation
let aid = AliasID::from(input.to_string()); let aid = AliasID::from(input.to_string());
check_trait(&input, &aid.0, "From<String>", "AliasID"); check_trait(&aid.0, input, "From<String>", "AliasID");
check_trait(&input, &aid.as_ref(), "AsRef<str>", "AliasID"); check_trait(aid.as_ref(), input, "AsRef<str>", "AliasID");
} }
{ {
// check ShareID // check ShareID
let _ = ShareID(input.to_string()); // direct creation
let sid = ShareID::from(input.to_string()); let sid = ShareID::from(input.to_string());
check_trait(&input, &sid.0, "From<String>", "ShareID"); check_trait(&sid.0, input, "From<String>", "ShareID");
check_trait(&input, &sid.to_string(), "Display", "ShareID"); check_trait(sid.to_string(), input, "Display", "ShareID");
} }
{ {
// check FileID // check FileID
let fid = FileID(input.to_string()); // direct creation let fid = FileID(input.to_string());
check_trait(&input, &fid.to_string(), "Display", "FileID"); check_trait(fid.to_string(), input, "Display", "FileID");
let fid_test = FileID::new_test(input);
assert_eq!(fid_test.0, fid.0);
} }
} }
} }

View file

@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
/// - impl `Display` for formatting compatibility /// - impl `Display` for formatting compatibility
/// - impl `AsRef<[u8]>` for hashing with `blake2b_simd` /// - impl `AsRef<[u8]>` for hashing with `blake2b_simd`
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(Default))]
pub struct Uri(String); pub struct Uri(String);
impl fmt::Display for Uri { impl fmt::Display for Uri {
@ -115,8 +116,8 @@ mod tests {
for uri_data in cases { for uri_data in cases {
let uri = Uri(uri_data.to_string()); let uri = Uri(uri_data.to_string());
check_trait(&uri_data, &uri.to_string(), "Display", "Uri"); check_trait(uri.to_string(), uri_data, "Display", "Uri");
check_trait(&uri_data.as_bytes(), &uri.as_ref(), "AsRef<[u8]>", "Uri"); check_trait(uri.as_ref(), uri_data.as_bytes(), "AsRef<[u8]>", "Uri");
} }
} }
@ -137,7 +138,7 @@ mod tests {
for (good, expected) in cases { for (good, expected) in cases {
let uri = Uri::from(good.to_string()); let uri = Uri::from(good.to_string());
check_trait(&expected, &uri.0, "From<String>", "Uri"); check_trait(uri.0, expected, "From<String>", "Uri");
} }
} }
@ -159,7 +160,7 @@ mod tests {
for bad in cases { for bad in cases {
let uri = Uri::from(bad.to_string()); let uri = Uri::from(bad.to_string());
check_trait(&bad, &uri.0, "From<String>", "Uri"); check_trait(uri.0, bad, "From<String>", "Uri");
} }
} }
@ -182,7 +183,7 @@ mod tests {
("12345", "/api/v2/12345"), ("12345", "/api/v2/12345"),
]; ];
let uri = Uri("".to_string()); let uri = Uri::default();
for (path, expected) in cases { for (path, expected) in cases {
assert_eq!(&expected, &uri.endpoint(format_args!("{path}"))); assert_eq!(&expected, &uri.endpoint(format_args!("{path}")));
} }
@ -190,9 +191,9 @@ mod tests {
#[test] #[test]
fn test_pub_endpoints() { fn test_pub_endpoints() {
let uri = Uri("".to_string()); let uri = Uri::default();
let share_id = ShareID("sid".to_string()); let share_id = ShareID::default();
let file_id = FileID("fid".to_string()); let file_id = FileID::default();
assert_eq!("/api/v2/alias/upload/new", uri.share_create()); assert_eq!("/api/v2/alias/upload/new", uri.share_create());
assert_eq!("/api/v2/alias/mail/notify/sid", uri.share_notify(&share_id)); assert_eq!("/api/v2/alias/mail/notify/sid", uri.share_notify(&share_id));

View file

@ -6,14 +6,15 @@ use std::{fmt, io::Write};
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
pub fn check_trait<E, A>(expected: &E, actual: &A, tr: &'static str, ty: &'static str) pub fn check_trait<A, E>(actual: A, expected: E, tr: &str, ty: &str)
where where
E: fmt::Debug + PartialEq<A>, A: fmt::Debug + PartialEq<E>,
A: fmt::Debug, E: fmt::Debug,
{ {
assert_eq!( assert_eq!(
actual, expected,
"`impl {tr} for {ty}` expected: {:?}, actual: {:?}",
expected, actual, expected, actual,
"`impl {tr} for {ty}` expected: {expected:?}, actual: {actual:?}",
); );
} }