generated from 2ndbeam/bevy-template
tests: Added more tests
- Added 2 more tests related to item moving - Refactored inventory tests internals
This commit is contained in:
parent
fee774dddd
commit
13dd936044
1 changed files with 46 additions and 29 deletions
69
src/tests.rs
69
src/tests.rs
|
|
@ -113,10 +113,10 @@ mod inventory {
|
|||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
struct Items(Vec<Item>, usize);
|
||||
struct Items(Vec<Item>, usize, bool);
|
||||
|
||||
#[derive(Component)]
|
||||
struct MovableItem(UVec2);
|
||||
struct MovableItem(UVec2, bool);
|
||||
|
||||
fn insert_item(
|
||||
mut commands: Commands,
|
||||
|
|
@ -129,28 +129,12 @@ mod inventory {
|
|||
let q_pos = item.position.unwrap();
|
||||
let (entity, inventory, children) = inventory_query.single().unwrap();
|
||||
if let Some(children) = children {
|
||||
println!("{q_size} {q_pos}");
|
||||
if items.2 {
|
||||
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 failed_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 {
|
||||
println!("{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;
|
||||
|
|
@ -162,8 +146,12 @@ mod inventory {
|
|||
movable_item: Query<(Entity, &MovableItem)>,
|
||||
) {
|
||||
let (inventory, children) = inventory_query.single().unwrap();
|
||||
let (movable_item, MovableItem(query_pos)) = movable_item.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));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -172,7 +160,7 @@ mod inventory {
|
|||
|
||||
let system = world.register_system(insert_item);
|
||||
|
||||
world.insert_resource(Items(vec![item_a(), item_b(), item_c(), item_d()], 0));
|
||||
world.insert_resource(Items(vec![item_a(), item_b(), item_c(), item_d()], 0, true));
|
||||
|
||||
world.spawn(inventory());
|
||||
|
||||
|
|
@ -185,9 +173,9 @@ mod inventory {
|
|||
fn items_e_f_g_do_not_fit() {
|
||||
let mut world = World::new();
|
||||
|
||||
let system = world.register_system(failed_insert_item);
|
||||
let system = world.register_system(insert_item);
|
||||
|
||||
world.insert_resource(Items(vec![item_e(), item_f(), item_g()], 0));
|
||||
world.insert_resource(Items(vec![item_e(), item_f(), item_g()], 0, false));
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child(item_a())
|
||||
|
|
@ -207,10 +195,39 @@ mod inventory {
|
|||
let system = world.register_system(try_to_move);
|
||||
|
||||
world.spawn(inventory())
|
||||
.with_child((item_a(), MovableItem(UVec2::new(3, 2))))
|
||||
.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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue