@@ 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<WordPath, String> {
- let mut args = env::args();
-
+fn handle_args(args: &mut Vec<String>) -> Result<WordPath, String> {
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<WordPath, String> {
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();
}