test: improve testability of input events
All checks were successful
Rust / build_and_test (push) Successful in 22s

This commit is contained in:
Lukas Wölfer
2026-01-29 20:54:43 +01:00
parent 091fba232b
commit f8b25e81bf
4 changed files with 136 additions and 30 deletions

27
src/event_source.rs Normal file
View File

@@ -0,0 +1,27 @@
use crossterm::event::{self, Event};
use std::error::Error;
use std::time::Duration;
/// An abstraction around event sourcing so we can inject events in tests.
pub trait EventSource {
/// Polls for an event for up to `timeout`, returning `Ok(true)` if an event is
/// available, `Ok(false)` if the timeout elapsed without an event, or an `Err`
/// if polling failed.
fn poll(&mut self, timeout: Duration) -> Result<bool, Box<dyn Error>>;
/// Reads the next available event, or returns an `Err` if reading fails.
fn read(&mut self) -> Result<Event, Box<dyn Error>>;
}
/// Production implementation that delegates to `crossterm::event`.
pub struct CrosstermEventSource;
impl EventSource for CrosstermEventSource {
fn poll(&mut self, timeout: Duration) -> Result<bool, Box<dyn Error>> {
Ok(event::poll(timeout).map_err(Box::new)?)
}
fn read(&mut self) -> Result<Event, Box<dyn Error>> {
Ok(event::read().map_err(Box::new)?)
}
}

View File

@@ -1,3 +1,4 @@
mod event_source;
mod terminal_guard; mod terminal_guard;
/// UI components (todo list) /// UI components (todo list)
mod todo_list; mod todo_list;
@@ -6,7 +7,9 @@ use core::time::Duration;
use crossterm::event::{self, EnableMouseCapture, Event, KeyCode}; use crossterm::event::{self, EnableMouseCapture, Event, KeyCode};
use crossterm::execute; use crossterm::execute;
use crossterm::terminal::{EnterAlternateScreen, enable_raw_mode}; use crossterm::terminal::{EnterAlternateScreen, enable_raw_mode};
use event_source::{CrosstermEventSource, EventSource};
use ratatui::Terminal; use ratatui::Terminal;
use ratatui::backend::Backend;
use ratatui::backend::CrosstermBackend; use ratatui::backend::CrosstermBackend;
use std::io::{self, Read as _}; use std::io::{self, Read as _};
use terminal_guard::TerminalModeGuard; use terminal_guard::TerminalModeGuard;
@@ -37,13 +40,30 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let backend = CrosstermBackend::new(stdout); let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?; let mut terminal = Terminal::new(backend)?;
run_app(&mut terminal, &mut CrosstermEventSource, &mut todo)?;
drop(mode_guard);
terminal.show_cursor()?;
Ok(())
}
/// Run the app loop using an abstract `EventSource` so tests can inject events.
pub fn run_app<B: Backend, E: EventSource>(
terminal: &mut Terminal<B>,
event_source: &mut E,
todo: &mut TodoList,
) -> Result<(), Box<dyn std::error::Error>>
where
<B as Backend>::Error: 'static,
{
loop { loop {
terminal.draw(|f| { terminal.draw(|f| {
todo.draw(f); todo.draw(f);
})?; })?;
if event::poll(Duration::from_millis(100))? { if event_source.poll(Duration::from_millis(100))? {
match event::read()? { match event_source.read()? {
Event::Key(key) => match key.code { Event::Key(key) => match key.code {
KeyCode::Char('q') => break, KeyCode::Char('q') => break,
KeyCode::Up => { KeyCode::Up => {
@@ -93,8 +113,63 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
} }
drop(mode_guard);
terminal.show_cursor()?;
Ok(()) Ok(())
} }
#[cfg(test)]
mod tests {
#![allow(clippy::panic_in_result_fn)]
use super::*;
use crate::todo_list::TodoList;
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use std::collections::VecDeque;
use std::time::Duration;
struct MockEventSource {
events: VecDeque<Event>,
}
impl MockEventSource {
fn new(events: Vec<Event>) -> Self {
Self {
events: VecDeque::from(events),
}
}
}
impl crate::event_source::EventSource for MockEventSource {
fn poll(&mut self, _timeout: Duration) -> Result<bool, Box<dyn std::error::Error>> {
Ok(!self.events.is_empty())
}
fn read(&mut self) -> Result<Event, Box<dyn std::error::Error>> {
Ok(self.events.pop_front().expect("no events left"))
}
}
#[test]
fn pressing_down_moves_selection() -> Result<(), Box<dyn std::error::Error>> {
let lines = vec!["one".to_owned(), "two".to_owned(), "three".to_owned()];
let mut todo = TodoList::with_lines(lines);
// prepare events: Down then 'q' to exit
let down = Event::Key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
let quit = Event::Key(KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE));
let mut source = MockEventSource::new(vec![down, quit]);
let backend = TestBackend::new(80, 24);
let mut terminal = Terminal::new(backend)?;
// initially selection is Some(0)
assert_eq!(todo.state.selected(), Some(0));
run_app(&mut terminal, &mut source, &mut todo)?;
// after pressing Down we expect selection to move to 1
assert_eq!(todo.state.selected(), Some(1));
Ok(())
}
}

View File

@@ -4,35 +4,17 @@ use ratatui::widgets::{Block, Borders, List, ListItem, ListState};
/// A simple todo/list view that owns the lines, marked flags and selection state. /// A simple todo/list view that owns the lines, marked flags and selection state.
pub struct TodoList { pub struct TodoList {
/// The text lines in the todo list.
pub lines: Vec<String>, pub lines: Vec<String>,
/// Flags indicating whether each corresponding line is marked.
pub marked: Vec<bool>, pub marked: Vec<bool>,
/// Stateful selection for the list widget.
pub state: ListState, pub state: ListState,
/// Title displayed above the list widget.
pub title: String, pub title: String,
} }
impl TodoList { impl TodoList {
/// Create a new `TodoList` from lines.
pub fn new(lines: Vec<String>) -> Self {
let mut state = ListState::default();
if !lines.is_empty() {
state.select(Some(0));
}
Self {
lines,
marked: vec![false; state.selected().map(|_| 0).unwrap_or(0)],
state,
title: "Lines (space to mark, q to quit)".to_string(),
}
}
/// Convenience constructor that ensures marked has same length as lines.
pub fn with_lines(lines: Vec<String>) -> Self {
let mut s = Self::new(lines);
s.marked = vec![false; s.lines.len()];
s
}
/// Draw the list into a `ratatui` frame. /// Draw the list into a `ratatui` frame.
pub fn draw(&mut self, f: &mut ratatui::Frame<'_>) { pub fn draw(&mut self, f: &mut ratatui::Frame<'_>) {
let size = f.area(); let size = f.area();
@@ -53,4 +35,26 @@ impl TodoList {
f.render_stateful_widget(list, size, &mut self.state); f.render_stateful_widget(list, size, &mut self.state);
} }
/// Create a new `TodoList` from lines.
pub fn new(lines: Vec<String>) -> Self {
let mut state = ListState::default();
if !lines.is_empty() {
state.select(Some(0));
}
Self {
lines,
marked: vec![false; state.selected().map_or(0, |_| 0)],
state,
title: "Lines (space to mark, q to quit)".to_owned(),
}
}
/// Convenience constructor that ensures marked has same length as lines.
pub fn with_lines(lines: Vec<String>) -> Self {
let mut s = Self::new(lines);
s.marked = vec![false; s.lines.len()];
s
}
} }

View File

@@ -12,7 +12,7 @@ fn render_list_into_buffer(
width: u16, width: u16,
height: u16, height: u16,
) -> Buffer { ) -> Buffer {
let backend = TestBackend::new(width.into(), height.into()); let backend = TestBackend::new(width, height);
let mut terminal = Terminal::new(backend).expect("create terminal"); let mut terminal = Terminal::new(backend).expect("create terminal");
let lines_vec: Vec<ListItem> = lines let lines_vec: Vec<ListItem> = lines
@@ -45,8 +45,8 @@ fn render_list_into_buffer(
// Extract the underlying backend buffer (clone to own it) // Extract the underlying backend buffer (clone to own it)
let backend = terminal.backend(); let backend = terminal.backend();
let buf = backend.buffer().clone();
buf backend.buffer().clone()
} }
#[test] #[test]