From 1227fc88eef3ec07b53221abaa34a20c913b845d Mon Sep 17 00:00:00 2001 From: Integral Date: Sat, 14 Dec 2024 20:30:17 +0800 Subject: [PATCH] test: make handle_args() testable & add test_handle_args() (codeberg #5) --- core/src/args.rs | 69 ++++++++++++++++++++++++++++++++++++++++++------ core/src/main.rs | 3 ++- 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/core/src/args.rs b/core/src/args.rs index aeb2e44..a7c9000 100644 --- a/core/src/args.rs +++ b/core/src/args.rs @@ -7,23 +7,23 @@ pub enum Status { Error, } -pub fn handle_args() -> Status { - use Status::*; - let args = std::env::args().collect::>(); +pub fn handle_args(args: impl IntoIterator) -> Status { + let args: Vec<_> = args.into_iter().collect(); if args.len() <= 1 { - return Continue; + return Status::Continue; } + let mut init = Init::new(); let mut index = 1; while index < args.len() { match args[index].as_str() { "-h" | "--help" => { print_help(); - return Exit; + return Status::Exit; } "-v" | "--version" => { print_version(); - return Exit; + return Status::Exit; } "-a" | "--alias" => { if args.len() > index + 1 { @@ -52,13 +52,13 @@ pub fn handle_args() -> Status { if init.shell.is_empty() { eprintln!("{}", t!("no-shell")); - return Error; + return Status::Error; } init.binary_path = args[0].clone(); initialization(&mut init); - Exit + Status::Exit } fn print_help() { @@ -92,3 +92,56 @@ fn print_version() { println!("Default lib directory: {}", lib.unwrap()); } } + +#[cfg(test)] +mod tests { + use super::{handle_args, Status}; + + #[test] + fn test_handle_args() { + assert!(matches!( + handle_args([String::from("pay-respects")]), + Status::Continue + )); + + for args in [ + [String::new(), String::from("-h")], + [String::new(), String::from("--help")], + [String::new(), String::from("-v")], + [String::new(), String::from("--version")], + [String::new(), String::from("zsh")], + ] { + println!("Arguments {:?} should return Exit", args); + assert!(matches!(handle_args(args), Status::Exit)); + } + + for args in [ + [String::new(), String::from("fish"), String::from("--alias")], + [String::new(), String::from("bash"), String::from("--nocnf")], + ] { + println!("Arguments {:?} should return Exit", args); + assert!(matches!(handle_args(args), Status::Exit)); + } + + for args in [ + [String::new(), String::from("-a")], + [String::new(), String::from("--alias")], + [String::new(), String::from("--nocnf")], + ] { + println!("Arguments {:?} should return Error", args); + assert!(matches!(handle_args(args), Status::Error)); + } + + for args in [ + [String::new(), String::from("-a"), String::from("--nocnf")], + [ + String::new(), + String::from("--alias"), + String::from("--nocnf"), + ], + ] { + println!("Argument {:?} should return Error", args); + assert!(matches!(handle_args(args), Status::Error)); + } + } +} diff --git a/core/src/main.rs b/core/src/main.rs index 736f345..8feb5e9 100644 --- a/core/src/main.rs +++ b/core/src/main.rs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +use std::env; use sys_locale::get_locale; mod args; @@ -71,7 +72,7 @@ fn init() -> Result { }; rust_i18n::set_locale(&locale[0..2]); - let status = args::handle_args(); + let status = args::handle_args(env::args()); match status { args::Status::Exit => { return Err(status);