test(command): add tests to tui command module

This commit is contained in:
Orhun Parmaksız 2022-01-28 20:01:49 +03:00
parent 2967a5269f
commit 9017aabbad
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 81 additions and 11 deletions

View file

@ -275,7 +275,7 @@ impl<'a> App<'a> {
}
}
Command::Scroll(_, _, _) => {}
Command::EnableSearch => {
Command::Search => {
if self.input_time.is_some() {
self.input_time = None;
}
@ -395,7 +395,7 @@ impl<'a> App<'a> {
self.running = false;
}
}
Command::None => {}
Command::Nothing => {}
}
Ok(())
}

View file

@ -3,7 +3,7 @@ use std::str::FromStr;
use termion::event::Key;
/// Possible application commands.
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum Command {
/// Perform an action based on the selected entry.
Select,
@ -14,7 +14,7 @@ pub enum Command {
/// Move cursor to right/left.
MoveCursor(u8),
/// Enable the search mode.
EnableSearch,
Search,
/// Process the input.
ProcessInput,
/// Update the input buffer.
@ -28,14 +28,14 @@ pub enum Command {
/// Exit the application.
Exit,
/// Do nothing.
None,
Nothing,
}
impl FromStr for Command {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"search" => Ok(Command::EnableSearch),
"search" => Ok(Command::Search),
"select" => Ok(Command::Select),
"copy" => Ok(Command::Copy),
"refresh" => Ok(Command::Refresh),
@ -52,7 +52,7 @@ impl FromStr for Command {
Ok(Command::Scroll(
ScrollArea::try_from(values.next().ok_or(())?)?,
Direction::try_from(values.next().ok_or(())?)?,
1,
values.next().and_then(|v| v.parse().ok()).unwrap_or(1),
))
} else {
Err(())
@ -74,7 +74,7 @@ impl Command {
Key::Left => Command::MoveCursor(1),
Key::Right => Command::MoveCursor(0),
Key::Esc => Command::Exit,
_ => Command::None,
_ => Command::Nothing,
}
} else {
match key {
@ -89,13 +89,83 @@ impl Command {
Key::Char('`') => Command::Scroll(ScrollArea::Section, Direction::Up, 1),
Key::Char('\t') => Command::Scroll(ScrollArea::Section, Direction::Down, 1),
Key::Char(':') => Command::UpdateInput(' '),
Key::Char('/') => Command::EnableSearch,
Key::Char('/') => Command::Search,
Key::Char('\n') => Command::Select,
Key::Char('c') => Command::Copy,
Key::Char('r') => Command::Refresh,
Key::Esc => Command::Exit,
_ => Command::None,
_ => Command::Nothing,
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! assert_command_parser {
(input_mode: $input_mode: expr,
$($key: expr => $command: expr,)+
) => {
$(assert_eq!($command, Command::parse($key, $input_mode)));+
};
}
#[test]
fn test_command() {
for (command, value) in vec![
(Command::Search, "search"),
(Command::Select, "select"),
(Command::Copy, "copy"),
(Command::Refresh, "refresh"),
(Command::Exit, "exit"),
(
Command::Set(String::from("a"), String::from("b")),
"set a b",
),
(
Command::Scroll(ScrollArea::List, Direction::Up, 1),
"scroll list up 1",
),
(
Command::Scroll(ScrollArea::Documentation, Direction::Down, 4),
"scroll docs down 4",
),
] {
assert_eq!(command, Command::from_str(value).unwrap());
}
assert!(Command::from_str("---").is_err());
assert_command_parser! {
input_mode: true,
Key::Char('\n') => Command::ProcessInput,
Key::Char('a') => Command::UpdateInput('a'),
Key::Backspace => Command::ClearInput(false),
Key::Delete => Command::ClearInput(true),
Key::Left => Command::MoveCursor(1),
Key::Right => Command::MoveCursor(0),
Key::Esc => Command::Exit,
}
assert_command_parser! {
input_mode: false,
Key::Up => Command::Scroll(ScrollArea::List, Direction::Up, 1),
Key::Down => Command::Scroll(ScrollArea::List, Direction::Down, 1),
Key::PageUp => Command::Scroll(ScrollArea::List, Direction::Up, 4),
Key::PageDown => Command::Scroll(ScrollArea::List, Direction::Down, 4),
Key::Char('t') => Command::Scroll(ScrollArea::List, Direction::Top, 0),
Key::Char('b') => Command::Scroll(ScrollArea::List, Direction::Bottom, 0),
Key::Left => Command::Scroll(ScrollArea::Documentation, Direction::Up, 1),
Key::Right => Command::Scroll(ScrollArea::Documentation, Direction::Down, 1),
Key::Char('`') => Command::Scroll(ScrollArea::Section, Direction::Up, 1),
Key::Char('\t') => Command::Scroll(ScrollArea::Section, Direction::Down, 1),
Key::Char(':') => Command::UpdateInput(' '),
Key::Char('/') => Command::Search,
Key::Char('\n') => Command::Select,
Key::Char('c') => Command::Copy,
Key::Char('r') => Command::Refresh,
Key::Esc => Command::Exit,
}
assert_eq!(Command::Nothing, Command::parse(Key::PageDown, true));
assert_eq!(Command::Nothing, Command::parse(Key::Char('#'), false));
}
}

View file

@ -58,6 +58,6 @@ generate_option!(
generate_option!(
ScrollArea,
List => "list",
Documentation => "documentation",
Documentation => "docs",
Section => "section",
);