Skip to content

Commit

Permalink
Added tests for key and operation parsing #39
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzrehde committed Jan 11, 2023
1 parent 0f73c2a commit c206cf8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/config/keybindings/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{bail, Result};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::str::FromStr;

#[cfg_attr(test, derive(Debug))]
#[derive(Hash, Eq, PartialEq)]
pub struct Key(KeyEvent);

Expand Down Expand Up @@ -71,3 +72,51 @@ fn parse_code(s: &str) -> Result<KeyEvent> {
};
Ok(KeyEvent::from(code))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_lowercase_key() -> Result<()> {
assert_eq!("k".parse::<Key>()?, Key(KeyCode::Char('k').into()));
Ok(())
}

#[test]
fn test_parse_uppercase_key() -> Result<()> {
assert_eq!("G".parse::<Key>()?, Key(KeyCode::Char('G').into()));
Ok(())
}

#[test]
fn test_parse_ctrl_modifier() -> Result<()> {
assert_eq!(
"ctrl+c".parse::<Key>()?,
Key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL))
);
// TODO: passes test, but doesn't practically work in all terminals
assert_eq!(
"ctrl+S".parse::<Key>()?,
Key(KeyEvent::new(KeyCode::Char('S'), KeyModifiers::CONTROL))
);
Ok(())
}

#[test]
fn test_parse_alt_modifier() -> Result<()> {
assert_eq!(
"alt+z".parse::<Key>()?,
Key(KeyEvent::new(KeyCode::Char('z'), KeyModifiers::ALT))
);
Ok(())
}

#[test]
fn test_parse_invalid_modifiers() {
assert!("shift+a".parse::<Key>().is_err());
assert!("super+a".parse::<Key>().is_err());
assert!("alt+shift+a".parse::<Key>().is_err());
assert!("alt+ctrl+a".parse::<Key>().is_err());
}
}
4 changes: 2 additions & 2 deletions src/config/keybindings/operations/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mod tests {
use super::*;

#[test]
fn test_move_cursor() {
fn test_parse_move_cursor() {
assert!(matches!(
"down 42".parse(),
Ok(Operation::MoveCursor(MoveCursor::Down(42)))
Expand All @@ -106,7 +106,7 @@ mod tests {
}

#[test]
fn test_move_cursor_invalid_step_size() {
fn test_parse_move_cursor_invalid_step_size() {
assert!("down -42".parse::<Operation>().is_err());
assert!("up -24".parse::<Operation>().is_err());
}
Expand Down

0 comments on commit c206cf8

Please sign in to comment.