added some tests

This commit is contained in:
epi
2021-12-19 11:12:51 -06:00
parent 1a26e6a992
commit 1c48f754a2
3 changed files with 82 additions and 31 deletions

View File

@@ -33,7 +33,7 @@ pub(super) struct Menu {
footer: String,
/// target for output
term: Term,
pub(super) term: Term,
}
/// Implementation of Menu
@@ -175,39 +175,35 @@ impl Menu {
}
/// get input from the user and translate it to a `MenuCmd`
pub(super) fn get_command_input_from_user(&self) -> Option<MenuCmd> {
if let Ok(line) = self.term.read_line() {
let line = line.trim(); // normalize input if there are leading spaces
pub(super) fn get_command_input_from_user(&self, line: &str) -> Option<MenuCmd> {
let line = line.trim(); // normalize input if there are leading spaces
match line.chars().next().unwrap_or('_').to_ascii_lowercase() {
'c' => {
// cancel command; start by determining if -f was used
let force = line.contains("-f");
match line.chars().next().unwrap_or('_').to_ascii_lowercase() {
'c' => {
// cancel command; start by determining if -f was used
let force = line.contains("-f");
// then remove c[ancel] from the command so it can be passed to the number
// splitter
let re = Regex::new(r"^[cC][ancelANCEL]*").unwrap();
let line = line.replace("-f", "");
let line = re.replace(&line, "").to_string();
// then remove c[ancel] from the command so it can be passed to the number
// splitter
let re = Regex::new(r"^[cC][ancelANCEL]*").unwrap();
let line = line.replace("-f", "");
let line = re.replace(&line, "").to_string();
Some(MenuCmd::Cancel(self.split_to_nums(&line), force))
}
'a' => {
// add command
// similar to cancel, we need to remove the a[dd] substring, the rest should be
// a url
let re = Regex::new(r"^[aA][dD]*").unwrap();
let line = re.replace(line, "").to_string().trim().to_string();
Some(MenuCmd::Add(line))
}
_ => {
// invalid input
None
}
Some(MenuCmd::Cancel(self.split_to_nums(&line), force))
}
'a' => {
// add command
// similar to cancel, we need to remove the a[dd] substring, the rest should be
// a url
let re = Regex::new(r"^[aA][dD]*").unwrap();
let line = re.replace(line, "").to_string().trim().to_string();
Some(MenuCmd::Add(line))
}
_ => {
// invalid input
None
}
} else {
None
}
}

View File

@@ -308,7 +308,11 @@ impl FeroxScans {
self.display_scans().await;
self.menu.print_footer();
let menu_cmd = self.menu.get_command_input_from_user();
let menu_cmd = if let Ok(line) = self.menu.term.read_line() {
self.menu.get_command_input_from_user(&line)
} else {
None
};
let result = match menu_cmd {
Some(MenuCmd::Cancel(indices, should_force)) => {

View File

@@ -572,6 +572,9 @@ async fn ferox_scan_abort() {
/// and their correctness can be verified easily manually; just calling for now
fn menu_print_header_and_footer() {
let menu = Menu::new();
let menu_cmd_res_1 = MenuCmdResult::Url(String::from("http://localhost"));
let menu_cmd_res_2 = MenuCmdResult::NumCancelled(2);
println!("{:?}{:?}", menu_cmd_res_1, menu_cmd_res_2);
menu.clear_screen();
menu.print_header();
menu.print_footer();
@@ -579,6 +582,54 @@ fn menu_print_header_and_footer() {
menu.show_progress_bars();
}
#[test]
/// ensure command parsing from user input results int he correct MenuCmd returned
fn menu_get_command_input_from_user_returns_cancel() {
let menu = Menu::new();
for (idx, cmd) in ["cancel", "Cancel", "c", "C"].iter().enumerate() {
let force = idx % 2 == 0;
let full_cmd = if force {
format!("{} -f {}\n", cmd, idx)
} else {
format!("{} {}\n", cmd, idx)
};
let result = menu.get_command_input_from_user(&full_cmd).unwrap();
assert!(matches!(result, MenuCmd::Cancel(_, _)));
if let MenuCmd::Cancel(canx_list, ret_force) = result {
if idx == 0 {
assert!(canx_list.is_empty());
} else {
assert_eq!(canx_list, vec![idx]);
}
assert_eq!(force, ret_force);
}
}
}
#[test]
/// ensure command parsing from user input results int he correct MenuCmd returned
fn menu_get_command_input_from_user_returns_add() {
let menu = Menu::new();
for cmd in &["add", "Addd", "a", "A"] {
let test_url = "http://happyfuntimes.commmm";
let full_cmd = format!("{} {}\n", cmd, test_url);
let result = menu.get_command_input_from_user(&full_cmd).unwrap();
assert!(matches!(result, MenuCmd::Add(_)));
if let MenuCmd::Add(url) = result {
assert_eq!(url, test_url);
}
}
}
#[test]
/// ensure spaces are trimmed and numbers are returned from split_to_nums
fn split_to_nums_is_correct() {