Skip to content

Commit

Permalink
Add first tests for InputReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 13, 2024
1 parent 24201a3 commit fec3275
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions src/input_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl InputReader<'_, '_> {
/// Returns the [`ActionValue`] for the given [`Input`] if exists.
///
/// See also [`Self::set_consume_input`] and [`Self::set_gamepad`].
pub(super) fn value(&mut self, input: Input) -> ActionValue {
match input {
pub(super) fn value(&mut self, input: impl Into<Input>) -> ActionValue {
match input.into() {
Input::Keyboard {
key_code,
modifiers,
Expand Down Expand Up @@ -375,3 +375,51 @@ impl From<usize> for GamepadDevice {
Gamepad::new(value).into()
}
}

#[cfg(test)]
mod tests {
use bevy::ecs::system::SystemState;

use super::*;

#[test]
fn key_code() {
let (mut world, mut state) = init_world();

world
.resource_mut::<ButtonInput<KeyCode>>()
.press(KeyCode::Space);

let mut reader = state.get(&world);
assert_eq!(reader.value(KeyCode::Space), ActionValue::Bool(true));
assert_eq!(reader.value(KeyCode::Space), ActionValue::Bool(true));
}

#[test]
fn key_code_consumed() {
let (mut world, mut state) = init_world();

world
.resource_mut::<ButtonInput<KeyCode>>()
.press(KeyCode::Space);

let mut reader = state.get(&world);
assert_eq!(reader.value(KeyCode::Space), ActionValue::Bool(true));
assert_eq!(reader.value(KeyCode::Space), ActionValue::Bool(false));
}

fn init_world<'w, 's>() -> (World, SystemState<InputReader<'w, 's>>) {
let mut world = World::new();
world.init_resource::<ButtonInput<KeyCode>>();
world.init_resource::<ButtonInput<MouseButton>>();
world.init_resource::<Events<MouseMotion>>();
world.init_resource::<Events<MouseWheel>>();
world.init_resource::<ButtonInput<GamepadButton>>();
world.init_resource::<Axis<GamepadAxis>>();
world.init_resource::<Gamepads>();

let state = SystemState::<InputReader>::new(&mut world);

(world, state)
}
}

0 comments on commit fec3275

Please sign in to comment.