From 4591912ad315700b198238b8a3d3ba3c68d3bccf Mon Sep 17 00:00:00 2001 From: onielfa Date: Fri, 31 Jul 2020 00:43:57 +0200 Subject: Add basic tests --- src/command.rs | 162 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 133 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/command.rs b/src/command.rs index ae5be66..d474e35 100644 --- a/src/command.rs +++ b/src/command.rs @@ -104,7 +104,7 @@ fn call_on_app(s: &mut Cursive, input: &str) { } } -#[derive(PartialEq)] +#[derive(PartialEq, Debug)] pub enum Command { Add(String, Option, bool), MonthPrev, @@ -219,50 +219,154 @@ mod tests { use super::*; #[test] - fn parse_add_command_with_goal() { - let command: Vec = "eat healthy 3" - .trim() - .split(' ') - .into_iter() - .map(|s| s.to_string()) - .collect(); + fn parse_add_command() { + let result = Command::from_string("add eat 2"); - let verb = "add".to_owned(); - let auto = false; + assert!(result.is_ok()); + match result.unwrap() { + Command::Add(name, goal, auto) => { + assert_eq!(name, "eat"); + assert_eq!(goal.unwrap(), 2); + assert_eq!(auto, false); + } + _ => panic!(), + } + } - let result = parse_add(verb, command, auto); + #[test] + fn parse_add_command_without_goal() { + let result = Command::from_string("add eat"); + assert!(result.is_ok()); match result.unwrap() { - Command::Add(name, goal, a) => { - assert_eq!(name, "eat healthy".to_owned()); - assert_eq!(goal.unwrap(), 3); - assert_eq!(a, auto); + Command::Add(name, goal, auto) => { + assert_eq!(name, "eat"); + assert!(goal.is_none()); + assert_eq!(auto, false); + } + _ => panic!(), + } + } + + // #[test] + fn parse_add_command_with_long_name() { + let result = Command::from_string("add \"eat healthy\" 5"); + + assert!(result.is_ok()); + match result.unwrap() { + Command::Add(name, goal, auto) => { + assert_eq!(name, "eat healthy"); + assert_eq!(goal.unwrap(), 5); + assert_eq!(auto, false); } _ => panic!(), } } #[test] - fn parse_add_command_without_goal() { - let command: Vec = "eat healthy" - .trim() - .split(' ') - .into_iter() - .map(|s| s.to_string()) - .collect(); + fn parse_add_auto_command() { + let result = Command::from_string("add-auto eat 2"); + + assert!(result.is_ok()); + match result.unwrap() { + Command::Add(name, goal, auto) => { + assert_eq!(name, "eat"); + assert_eq!(goal.unwrap(), 2); + assert_eq!(auto, true); + } + _ => panic!(), + } + } - let verb = "add".to_owned(); - let auto = false; + #[test] + fn parse_delete_command() { + let result = Command::from_string("delete eat"); - let result = parse_add(verb, command, auto); + assert!(result.is_ok()); + match result.unwrap() { + Command::Delete(name) => { + assert_eq!(name, "eat"); + } + _ => panic!(), + } + } + + #[test] + fn parse_track_up_command() { + let result = Command::from_string("track-up eat"); + assert!(result.is_ok()); match result.unwrap() { - Command::Add(name, goal, a) => { - assert_eq!(name, "eat healthy".to_owned()); - assert!(goal.is_none()); - assert_eq!(a, auto); + Command::TrackUp(name) => { + assert_eq!(name, "eat"); } _ => panic!(), } } + + #[test] + fn parse_track_down_command() { + let result = Command::from_string("track-down eat"); + + assert!(result.is_ok()); + match result.unwrap() { + Command::TrackDown(name) => { + assert_eq!(name, "eat"); + } + _ => panic!(), + } + } + + #[test] + fn parse_help_command() { + let result = Command::from_string("help add"); + + assert!(result.is_ok()); + match result.unwrap() { + Command::Help(name) => { + assert_eq!(name.unwrap(), "add"); + } + _ => panic!(), + } + } + + #[test] + fn parse_month_prev_command() { + let result = Command::from_string("mprev"); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), Command::MonthPrev); + } + + #[test] + fn parse_month_next_command() { + let result = Command::from_string("mnext"); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), Command::MonthNext); + } + + #[test] + fn parse_quit_command() { + let result = Command::from_string("q"); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), Command::Quit); + } + + #[test] + fn parse_write_command() { + let result = Command::from_string("w"); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), Command::Write); + } + + #[test] + fn parse_no_command() { + let result = Command::from_string(""); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), Command::Blank); + } } -- cgit v1.2.3