From 7bc0fb7ada6c5c503266e9d8ed6dfba9431fdbfc Mon Sep 17 00:00:00 2001 From: Vamist Date: Fri, 10 Feb 2023 22:55:09 +0000 Subject: [PATCH] [dev] Unit tests --- src/main.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 95 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 44c864f..436273c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,7 +154,8 @@ impl WordPath { } fn main() { - let wordpath = fetch_args(); + let mut args = env::args().collect(); + let wordpath = handle_args(&mut args); if let Err(why) = wordpath { panic!("{why}"); @@ -171,15 +172,15 @@ fn main() { println!("{}", result.unwrap()); } -fn fetch_args() -> Result { - let mut args = env::args(); - +fn handle_args(args: &mut Vec) -> Result { if args.len() < 4 { return Err( "Not enough args supplied, example: wpaths /usr/share/dict/words cat dog".to_string(), ); } + let mut args = args.iter_mut(); + // Skip the first arg, as that is our exe args.next().unwrap(); @@ -187,20 +188,101 @@ fn fetch_args() -> Result { let start = args.next().unwrap().to_lowercase(); let end = args.next().unwrap().to_lowercase(); - // Process all args - let file = PathBuf::from(file); + let file = PathBuf::from(&file); if start.len() != end.len() { return Err("Word lengths are not equal, this is not supported".to_string()); } - if !start.chars().all(char::is_alphabetic) { - return Err("Start word must only contain alphabetic characters".to_string()); - } + Ok(WordPath { file, start, end }) +} - if !end.chars().all(char::is_alphabetic) { - return Err("End word must only contain alphabetic characters".to_string()); - } +/// TESTS +/// Could be in a different file, but it requires more effort then its worth for a small project - Ok(WordPath { file, start, end }) +#[test] +fn arg_valid() { + let mut args = vec![ + "wpath".to_string(), + "/usr/share/dict/words".to_string(), + "cat".to_string(), + "dog".to_string(), + ]; + + handle_args(&mut args).unwrap(); +} + +#[test] +#[should_panic] +fn arg_invalid_len_start() { + let mut args = vec![ + "wpath".to_string(), + "/usr/share/dict/words".to_string(), + "cats".to_string(), + "dog".to_string(), + ]; + + handle_args(&mut args).unwrap(); +} + +#[test] +#[should_panic] +fn arg_invalid_len_end() { + let mut args = vec![ + "wpath".to_string(), + "/usr/share/dict/words".to_string(), + "cat".to_string(), + "dogs".to_string(), + ]; + + handle_args(&mut args).unwrap(); +} + +#[test] +#[should_panic] +fn invalid_file() { + let mut args = vec![ + "wpath".to_string(), + "/usr/share/dict/wordle".to_string(), + "cat".to_string(), + "dog".to_string(), + ]; + + handle_args(&mut args).unwrap().read().unwrap(); +} + +#[test] +fn valid_file() { + let mut args = vec![ + "wpath".to_string(), + "/usr/share/dict/words".to_string(), + "cat".to_string(), + "dog".to_string(), + ]; + + handle_args(&mut args).unwrap().read().unwrap(); +} + +#[test] +fn valid_word_path() { + let mut args = vec![ + "wpath".to_string(), + "/usr/share/dict/words".to_string(), + "cat".to_string(), + "dog".to_string(), + ]; + + handle_args(&mut args).unwrap().run().unwrap(); +} + +#[test] +fn invalid_word_path() { + let mut args = vec![ + "wpath".to_string(), + "/usr/share/dict/words".to_string(), + "cat".to_string(), + "lol".to_string(), + ]; + + handle_args(&mut args).unwrap().run().unwrap(); } -- 2.45.2