Completed events unit tests

This commit is contained in:
2023-08-08 10:50:05 -06:00
parent 47da289738
commit e9b37e23ae
2 changed files with 67 additions and 6 deletions
+3 -5
View File
@@ -14,7 +14,6 @@ pub enum InputEvent<T> {
}
pub struct Events {
_tx: Sender<InputEvent<Key>>,
rx: Receiver<InputEvent<Key>>,
}
@@ -23,7 +22,6 @@ impl Events {
let (tx, rx) = mpsc::channel();
let tick_rate: Duration = Duration::from_millis(250);
let event_tx = tx.clone();
thread::spawn(move || {
let mut last_tick = Instant::now();
loop {
@@ -33,18 +31,18 @@ impl Events {
if event::poll(timeout).unwrap() {
if let CrosstermEvent::Key(key_event) = event::read().unwrap() {
let key = Key::from(key_event);
event_tx.send(InputEvent::KeyEvent(key)).unwrap();
tx.send(InputEvent::KeyEvent(key)).unwrap();
}
}
if last_tick.elapsed() >= tick_rate {
event_tx.send(InputEvent::Tick).unwrap();
tx.send(InputEvent::Tick).unwrap();
last_tick = Instant::now();
}
}
});
Events { _tx: tx, rx }
Events { rx }
}
pub fn next(&self) -> Result<InputEvent<Key>, mpsc::RecvError> {
+64 -1
View File
@@ -85,14 +85,77 @@ mod tests {
#[test]
fn test_key_formatter() {
assert_eq!(format!("{}", Key::Esc), "<Esc>");
}
#[test]
fn test_key_formatter_char() {
assert_eq!(format!("{}", Key::Char('q')), "<q>");
}
#[test]
fn test_key_from() {
fn test_key_from_up() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Up)), Key::Up);
}
#[test]
fn test_key_from_down() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Down)), Key::Down);
}
#[test]
fn test_key_from_left() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Left)), Key::Left);
}
#[test]
fn test_key_from_right() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Right)), Key::Right);
}
#[test]
fn test_key_from_backspace() {
assert_eq!(
Key::from(KeyEvent::from(KeyCode::Backspace)),
Key::Backspace
);
}
#[test]
fn test_key_from_home() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Home)), Key::Home);
}
#[test]
fn test_key_from_end() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::End)), Key::End);
}
#[test]
fn test_key_from_delete() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Delete)), Key::Delete);
}
#[test]
fn test_key_from_enter() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Enter)), Key::Enter);
}
#[test]
fn test_key_from_esc() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Esc)), Key::Esc);
}
#[test]
fn test_key_from_char() {
assert_eq!(
Key::from(KeyEvent::from(KeyCode::Char('q'))),
Key::Char('q')
)
}
#[test]
fn test_key_from_unknown() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Pause)), Key::Unknown);
}
}