From 5109457fcd193e94e11363ac0bd9a27db46334e0 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 19 Aug 2025 09:40:59 -0500 Subject: [PATCH] test: add input tests --- tests/input.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/input.rs diff --git a/tests/input.rs b/tests/input.rs new file mode 100644 index 0000000..8a55ec7 --- /dev/null +++ b/tests/input.rs @@ -0,0 +1,38 @@ +use pacman::events::{GameCommand, GameEvent}; +use pacman::map::direction::Direction; +use pacman::systems::input::{process_simple_key_events, Bindings, SimpleKeyEvent}; +use sdl2::keyboard::Keycode; + +#[test] +fn resumes_previous_direction_when_secondary_key_released() { + let mut bindings = Bindings::default(); + + // Frame 1: Press W (Up) => emits Move Up + let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::W)]); + assert!(events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Up)))); + + // Frame 2: Press D (Right) => emits Move Right + let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::D)]); + assert!(events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Right)))); + + // Frame 3: Release D, no new key this frame => should continue previous key W (Up) + let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyUp(Keycode::D)]); + assert!(events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Up)))); +} + +#[test] +fn holds_last_pressed_key_across_frames_when_no_new_input() { + let mut bindings = Bindings::default(); + + // Frame 1: Press Left + let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::Left)]); + assert!(events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Left)))); + + // Frame 2: No input => continues Left + let events = process_simple_key_events(&mut bindings, &[]); + assert!(events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Left)))); + + // Frame 3: Release Left, no input remains => nothing emitted + let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyUp(Keycode::Left)]); + assert!(events.is_empty()); +}