Skip to content

Commit

Permalink
Simplify tests by using ContextInstances
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Nov 1, 2024
1 parent aacb172 commit a2f092d
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 645 deletions.
43 changes: 16 additions & 27 deletions tests/accumulation.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
mod action_recorder;

use bevy::{input::InputPlugin, prelude::*};
use bevy_enhanced_input::prelude::*;

use action_recorder::{ActionRecorderPlugin, AppTriggeredExt, RecordedActions};

#[test]
fn max_abs() {
let mut app = App::new();
app.add_plugins((
MinimalPlugins,
InputPlugin,
EnhancedInputPlugin,
ActionRecorderPlugin,
))
.add_input_context::<DummyContext>()
.record_action::<MaxAbs>();
app.add_plugins((MinimalPlugins, InputPlugin, EnhancedInputPlugin))
.add_input_context::<DummyContext>();

let entity = app.world_mut().spawn(DummyContext).id();

Expand All @@ -27,23 +17,17 @@ fn max_abs() {

app.update();

let recorded = app.world().resource::<RecordedActions>();
let events = recorded.get::<MaxAbs>(entity).unwrap();
let event = events.last().unwrap();
assert_eq!(event.value, Vec2::Y.into());
let instances = app.world().resource::<ContextInstances>();
let ctx = instances.get::<DummyContext>(entity).unwrap();
let action = ctx.action::<MaxAbs>().unwrap();
assert_eq!(action.value(), Vec2::Y.into());
}

#[test]
fn cumulative() {
let mut app = App::new();
app.add_plugins((
MinimalPlugins,
InputPlugin,
EnhancedInputPlugin,
ActionRecorderPlugin,
))
.add_input_context::<DummyContext>()
.record_action::<Cumulative>();
app.add_plugins((MinimalPlugins, InputPlugin, EnhancedInputPlugin))
.add_input_context::<DummyContext>();

let entity = app.world_mut().spawn(DummyContext).id();

Expand All @@ -55,9 +39,14 @@ fn cumulative() {

app.update();

let recorded = app.world().resource::<RecordedActions>();
let events = recorded.get::<Cumulative>(entity).unwrap();
assert!(events.is_empty(), "up and down should cancel each other");
let instances = app.world().resource::<ContextInstances>();
let ctx = instances.get::<DummyContext>(entity).unwrap();
let action = ctx.action::<Cumulative>().unwrap();
assert_eq!(
action.value(),
Vec2::ZERO.into(),
"up and down should cancel each other"
);
}

#[derive(Debug, Component)]
Expand Down
69 changes: 0 additions & 69 deletions tests/action_recorder/mod.rs

This file was deleted.

58 changes: 18 additions & 40 deletions tests/bind_presets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod action_recorder;

use bevy::{
input::{
gamepad::{GamepadConnection, GamepadConnectionEvent, GamepadInfo},
Expand All @@ -9,19 +7,11 @@ use bevy::{
};
use bevy_enhanced_input::prelude::*;

use action_recorder::{ActionRecorderPlugin, AppTriggeredExt, RecordedActions};

#[test]
fn wasd_and_arrows() {
let mut app = App::new();
app.add_plugins((
MinimalPlugins,
InputPlugin,
EnhancedInputPlugin,
ActionRecorderPlugin,
))
.add_input_context::<DummyContext>()
.record_action::<DummyAction>();
app.add_plugins((MinimalPlugins, InputPlugin, EnhancedInputPlugin))
.add_input_context::<DummyContext>();

let entity = app.world_mut().spawn(DummyContext).id();

Expand All @@ -43,11 +33,11 @@ fn wasd_and_arrows() {

app.update();

let recorded = app.world().resource::<RecordedActions>();
let events = recorded.get::<DummyAction>(entity).unwrap();
let event = events.last().unwrap();
let instances = app.world().resource::<ContextInstances>();
let ctx = instances.get::<DummyContext>(entity).unwrap();
let action = ctx.action::<DummyAction>().unwrap();
assert_eq!(
event.value,
action.value(),
dir.into(),
"`{key:?}` should result in `{dir}`"
);
Expand All @@ -63,14 +53,8 @@ fn wasd_and_arrows() {
#[test]
fn dpad() {
let mut app = App::new();
app.add_plugins((
MinimalPlugins,
InputPlugin,
EnhancedInputPlugin,
ActionRecorderPlugin,
))
.add_input_context::<DummyContext>()
.record_action::<DummyAction>();
app.add_plugins((MinimalPlugins, InputPlugin, EnhancedInputPlugin))
.add_input_context::<DummyContext>();

let gamepad = Gamepad::new(0);
app.world_mut().send_event(GamepadConnectionEvent {
Expand Down Expand Up @@ -101,11 +85,11 @@ fn dpad() {

app.update();

let recorded = app.world().resource::<RecordedActions>();
let events = recorded.get::<DummyAction>(entity).unwrap();
let event = events.last().unwrap();
let instances = app.world().resource::<ContextInstances>();
let ctx = instances.get::<DummyContext>(entity).unwrap();
let action = ctx.action::<DummyAction>().unwrap();
assert_eq!(
event.value,
action.value(),
dir.into(),
"`{button:?}` should result in `{dir}`"
);
Expand All @@ -121,14 +105,8 @@ fn dpad() {
#[test]
fn sticks() {
let mut app = App::new();
app.add_plugins((
MinimalPlugins,
InputPlugin,
EnhancedInputPlugin,
ActionRecorderPlugin,
))
.add_input_context::<DummyContext>()
.record_action::<DummyAction>();
app.add_plugins((MinimalPlugins, InputPlugin, EnhancedInputPlugin))
.add_input_context::<DummyContext>();

let gamepad = Gamepad::new(0);
app.world_mut().send_event(GamepadConnectionEvent {
Expand Down Expand Up @@ -160,11 +138,11 @@ fn sticks() {

app.update();

let recorded = app.world().resource::<RecordedActions>();
let events = recorded.get::<DummyAction>(entity).unwrap();
let event = events.last().unwrap();
let instances = app.world().resource::<ContextInstances>();
let ctx = instances.get::<DummyContext>(entity).unwrap();
let action = ctx.action::<DummyAction>().unwrap();
assert_eq!(
event.value,
action.value(),
dir.into(),
"`{axis:?}` should result in `{dir}`"
);
Expand Down
Loading

0 comments on commit a2f092d

Please sign in to comment.