From ca96da8381da62cea1b01fd1f1d0363b7e5d1f9b Mon Sep 17 00:00:00 2001 From: Adam Perkowski Date: Wed, 27 Nov 2024 17:02:56 +0100 Subject: [PATCH] test: `Package` default(), new() & tests Signed-off-by: Adam Perkowski --- src/config.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index a894bb3..0baff72 100644 --- a/src/config.rs +++ b/src/config.rs @@ -62,8 +62,50 @@ pub struct Package { pub prefix: String, } -// TODO: Package defaults & tests impl Package { + pub fn new( + source: String, + target: String, + use_max_tag: bool, + prefix: String, + ) -> error::Result { + let mut package = Package::default(); + + match source.as_ref() { + "aur" => { + package.aur = target; + Ok(()) + } + "github" => { + package.github = target; + Ok(()) + } + "gitlab" => { + package.gitlab = target; + Ok(()) + } + _ => Err(error::Error::SourceNotFound(source.clone())), + }?; + + package.source = source; + package.use_max_tag = Some(use_max_tag); + package.prefix = prefix; + + Ok(package) + } + + fn default() -> Self { + Package { + source: String::new(), + host: String::new(), + aur: String::new(), + github: String::new(), + gitlab: String::new(), + use_max_tag: None, + prefix: String::new(), + } + } + /// global function to get various API-specific agrs for a package /// /// # example @@ -156,8 +198,24 @@ mod tests { async fn loading() { let config = load(None).await.unwrap(); - // TODO: here, ref L47 - assert_eq!(config.1, PathBuf::from("nvrs.toml")); } + + #[tokio::test] + async fn manual_package() { + assert!(Package::new( + "non_existing_source".to_string(), + "non_existing".to_string(), + false, + String::new() + ) + .is_err()); + assert!(Package::new( + "github".to_string(), + "orhun/git-cliff".to_string(), + false, + "v".to_string() + ) + .is_ok()); + } }