generated from 2ndbeam/bevy-template
refactor: Non-breaking refactor
- Everything should work as before - Added blank submodules in layout - Added blank submodules in player - Moved inventory::ui submodule to ui::inventory - Added blank submodules in ui::inventory - Re-exported ui::inventory as inventory::ui for temporary compatibility - Split tests submodules into different files
This commit is contained in:
parent
d65ca6fe97
commit
3094a8af13
14 changed files with 880 additions and 872 deletions
75
src/tests/input.rs
Normal file
75
src/tests/input.rs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
use super::super::*;
|
||||
use leafwing_input_manager::prelude::*;
|
||||
#[derive(Actionlike, Reflect, Clone, Debug, PartialEq, Eq, Hash, Default)]
|
||||
enum Action {
|
||||
#[default]
|
||||
#[actionlike(DualAxis)]
|
||||
DualAxis,
|
||||
#[actionlike(Axis)]
|
||||
SingleAxis,
|
||||
Button,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn asset_from_map() {
|
||||
let mut input_map = InputMap::default();
|
||||
input_map.insert(Action::Button, KeyCode::KeyE);
|
||||
input_map.insert(Action::Button, GamepadButton::East);
|
||||
|
||||
input_map.insert_axis(Action::SingleAxis, GamepadAxis::LeftStickX);
|
||||
input_map.insert_axis(Action::SingleAxis, VirtualAxis::ad());
|
||||
|
||||
input_map.insert_dual_axis(Action::DualAxis, GamepadStick::RIGHT);
|
||||
input_map.insert_dual_axis(Action::DualAxis, VirtualDPad::wasd());
|
||||
|
||||
let mut expected_input_asset = input::InputAsset::default();
|
||||
expected_input_asset.insert(Action::Button, input::MultiInput {
|
||||
keyboard: Some(vec![KeyCode::KeyE]),
|
||||
gamepad: Some(vec![GamepadButton::East]),
|
||||
mouse: None,
|
||||
}.into());
|
||||
expected_input_asset.insert(Action::SingleAxis, input::InputKind::Axis(vec![
|
||||
Box::new(GamepadAxis::LeftStickX),
|
||||
Box::new(VirtualAxis::ad()),
|
||||
]));
|
||||
expected_input_asset.insert(Action::DualAxis, input::InputKind::DualAxis(vec![
|
||||
Box::new(GamepadStick::RIGHT),
|
||||
Box::new(VirtualDPad::wasd()),
|
||||
]));
|
||||
|
||||
let input_asset = input::InputAsset::from(input_map);
|
||||
|
||||
assert_eq!(input_asset, expected_input_asset);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map_from_asset() {
|
||||
let mut input_asset = input::InputAsset::default();
|
||||
input_asset.insert(Action::Button, input::MultiInput {
|
||||
keyboard: Some(vec![KeyCode::KeyE]),
|
||||
gamepad: Some(vec![GamepadButton::East]),
|
||||
mouse: None,
|
||||
}.into());
|
||||
input_asset.insert(Action::SingleAxis, input::InputKind::Axis(vec![
|
||||
Box::new(GamepadAxis::LeftStickX),
|
||||
Box::new(VirtualAxis::ad()),
|
||||
]));
|
||||
input_asset.insert(Action::DualAxis, input::InputKind::DualAxis(vec![
|
||||
Box::new(GamepadStick::RIGHT),
|
||||
Box::new(VirtualDPad::wasd()),
|
||||
]));
|
||||
|
||||
let mut expected_input_map = InputMap::default();
|
||||
expected_input_map.insert(Action::Button, KeyCode::KeyE);
|
||||
expected_input_map.insert(Action::Button, GamepadButton::East);
|
||||
|
||||
expected_input_map.insert_axis(Action::SingleAxis, GamepadAxis::LeftStickX);
|
||||
expected_input_map.insert_axis(Action::SingleAxis, VirtualAxis::ad());
|
||||
|
||||
expected_input_map.insert_dual_axis(Action::DualAxis, GamepadStick::RIGHT);
|
||||
expected_input_map.insert_dual_axis(Action::DualAxis, VirtualDPad::wasd());
|
||||
|
||||
let input_map = InputMap::from(input_asset);
|
||||
|
||||
assert_eq!(input_map, expected_input_map);
|
||||
}
|
||||
313
src/tests/inventory.rs
Normal file
313
src/tests/inventory.rs
Normal file
|
|
@ -0,0 +1,313 @@
|
|||
use bevy::prelude::*;
|
||||
use crate::inventory::{Inventory, item::Item, ui::DraggedItem};
|
||||
|
||||
fn inventory() -> Inventory {
|
||||
Inventory::new(UVec2::splat(4))
|
||||
}
|
||||
|
||||
/// 1x2 0;0
|
||||
fn item_a() -> Item {
|
||||
Item::new_positioned(UVec2::new(1, 2), UVec2::new(0, 0))
|
||||
}
|
||||
|
||||
/// 2x2 0;2
|
||||
fn item_b() -> Item {
|
||||
Item::new_positioned(UVec2::new(2, 2), UVec2::new(0, 2))
|
||||
}
|
||||
|
||||
/// 3x2 1;0
|
||||
fn item_c() -> Item {
|
||||
Item::new_positioned(UVec2::new(3, 2), UVec2::new(1, 0))
|
||||
}
|
||||
|
||||
/// 2x2 2;2
|
||||
fn item_d() -> Item {
|
||||
Item::new_positioned(UVec2::new(2, 2), UVec2::new(2, 2))
|
||||
}
|
||||
|
||||
/// 1x1 0;0
|
||||
fn item_e() -> Item {
|
||||
Item::new_positioned(UVec2::new(1, 1), UVec2::new(0, 0))
|
||||
}
|
||||
|
||||
/// 5x5 0;0
|
||||
fn item_f() -> Item {
|
||||
Item::new_positioned(UVec2::new(5, 5), UVec2::new(0, 0))
|
||||
}
|
||||
|
||||
/// 2x2 3;3
|
||||
fn item_g() -> Item {
|
||||
Item::new_positioned(UVec2::new(2, 2), UVec2::new(3, 3))
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
struct Items(Vec<Item>, usize, bool);
|
||||
|
||||
#[derive(Component, Resource)]
|
||||
struct MovableItem(UVec2, bool);
|
||||
|
||||
#[derive(Component)]
|
||||
struct RotatingMarker(bool);
|
||||
|
||||
fn insert_item(
|
||||
mut commands: Commands,
|
||||
mut items: ResMut<Items>,
|
||||
item_query: Query<&Item>,
|
||||
inventory_query: Query<(Entity, &Inventory, Option<&Children>)>
|
||||
) {
|
||||
let item = &items.0[items.1];
|
||||
let q_size = item.size;
|
||||
let q_pos = item.position.unwrap();
|
||||
let (entity, inventory, children) = inventory_query.single().unwrap();
|
||||
if let Some(children) = children {
|
||||
if items.2 {
|
||||
assert!(inventory.can_fit(item_query, children, q_size, q_pos));
|
||||
} else {
|
||||
assert!(!inventory.can_fit(item_query, children, q_size, q_pos));
|
||||
}
|
||||
}
|
||||
let item_entity = commands.spawn(item.clone()).id();
|
||||
commands.entity(entity).add_child(item_entity);
|
||||
items.1 += 1;
|
||||
}
|
||||
|
||||
fn try_to_move(
|
||||
item_query: Query<&Item>,
|
||||
inventory_query: Query<(&Inventory, &Children)>,
|
||||
movable_item: Query<(Entity, &MovableItem)>,
|
||||
) {
|
||||
let (inventory, children) = inventory_query.single().unwrap();
|
||||
let (movable_item, MovableItem(query_pos, assertion)) = movable_item.single().unwrap();
|
||||
if *assertion {
|
||||
assert!(inventory.can_move(item_query, children, movable_item, *query_pos));
|
||||
} else {
|
||||
assert!(!inventory.can_move(item_query, children, movable_item, *query_pos));
|
||||
}
|
||||
}
|
||||
|
||||
fn find_space(
|
||||
item_query: Query<&Item>,
|
||||
inventory_query: Query<(&Inventory, &Children)>,
|
||||
movable_item: Res<MovableItem>,
|
||||
) {
|
||||
let (inventory, children) = inventory_query.single().unwrap();
|
||||
let MovableItem(query_size, assertion) = *movable_item;
|
||||
if assertion {
|
||||
assert!(inventory.find_free_space(item_query, children, query_size).is_some());
|
||||
} else {
|
||||
assert!(inventory.find_free_space(item_query, children, query_size).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
fn rotate_in_place(
|
||||
item_query: Query<&Item>,
|
||||
rotated_item: Query<(Entity, &RotatingMarker)>,
|
||||
inventory_query: Query<(&Inventory, &Children)>,
|
||||
) {
|
||||
let (inventory, children) = inventory_query.single().unwrap();
|
||||
let (rotatable_id, RotatingMarker(assertion)) = rotated_item.single().unwrap();
|
||||
if *assertion {
|
||||
assert!(inventory.can_rotate(item_query, children, rotatable_id));
|
||||
} else {
|
||||
assert!(!inventory.can_rotate(item_query, children, rotatable_id));
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_item(
|
||||
item_query: Query<&Item>,
|
||||
replacable_item: Query<(Entity, &DraggedItem, &RotatingMarker)>,
|
||||
inventory_query: Query<(&Inventory, &Children)>,
|
||||
) {
|
||||
let (inventory, children) = inventory_query.single().unwrap();
|
||||
let (replacable_id, DraggedItem(replacing_item, _), RotatingMarker(assertion)) = replacable_item.single()
|
||||
.unwrap();
|
||||
if *assertion {
|
||||
assert!(inventory.can_replace(item_query, children, replacable_id, replacing_item));
|
||||
} else {
|
||||
assert!(!inventory.can_replace(item_query, children, replacable_id, replacing_item));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn everything_fits() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(insert_item);
|
||||
|
||||
world.insert_resource(Items(vec![item_a(), item_b(), item_c(), item_d()], 0, true));
|
||||
|
||||
world.spawn(inventory());
|
||||
|
||||
for _ in 0..4 {
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn items_e_f_g_do_not_fit() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(insert_item);
|
||||
|
||||
world.insert_resource(Items(vec![item_e(), item_f(), item_g()], 0, false));
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child(item_a())
|
||||
.with_child(item_b())
|
||||
.with_child(item_c())
|
||||
.with_child(item_d());
|
||||
|
||||
for _ in 0..3 {
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_move_item() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(try_to_move);
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child((item_a(), MovableItem(UVec2::new(3, 2), true)))
|
||||
.with_child(item_b())
|
||||
.with_child(item_c());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_move_item() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(try_to_move);
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child((item_a(), MovableItem(UVec2::new(3, 2), false)))
|
||||
.with_child(item_b())
|
||||
.with_child(item_c())
|
||||
.with_child(item_d());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_item_slightly() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(try_to_move);
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child((item_a(), MovableItem(UVec2::new(0, 1), true)))
|
||||
.with_child(item_c())
|
||||
.with_child(item_d());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_find_space_for_item() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(find_space);
|
||||
|
||||
world.insert_resource(MovableItem(UVec2::new(2, 2), true));
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child(item_a())
|
||||
.with_child(item_b())
|
||||
.with_child(item_c());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_find_space_for_item() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(find_space);
|
||||
|
||||
world.insert_resource(MovableItem(UVec2::new(2, 2), false));
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child(item_a())
|
||||
.with_child(item_b())
|
||||
.with_child(item_c())
|
||||
.with_child(item_d());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn item_fits_if_rotated() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(find_space);
|
||||
|
||||
world.insert_resource(MovableItem(UVec2::new(2, 3), true));
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child(item_a())
|
||||
.with_child(item_b())
|
||||
.with_child(item_d());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotate_item_in_place() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(rotate_in_place);
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child((item_a(), RotatingMarker(true)))
|
||||
.with_child(item_b())
|
||||
.with_child(item_d());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_rotate_item_in_place() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(rotate_in_place);
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child((item_a(), RotatingMarker(false)))
|
||||
.with_child(item_b())
|
||||
.with_child(item_c())
|
||||
.with_child(item_d());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_replace_item() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(replace_item);
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child((item_a(), DraggedItem(item_c(), UVec2::ZERO), RotatingMarker(true)))
|
||||
.with_child(item_b())
|
||||
.with_child(item_d());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_replace_item() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(replace_item);
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child((item_a(), DraggedItem(item_c(), UVec2::ZERO), RotatingMarker(false)))
|
||||
.with_child(item_b())
|
||||
.with_child(item_c())
|
||||
.with_child(item_d());
|
||||
|
||||
world.run_system(system).expect("Error on running system");
|
||||
}
|
||||
2
src/tests/mod.rs
Normal file
2
src/tests/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
mod input;
|
||||
mod inventory;
|
||||
Loading…
Add table
Add a link
Reference in a new issue