Skip to content

Commit 07f4074

Browse files
committed
examples on how to tests systems (#1714)
well... those are examples on how to tests systems despawning entities, modifying components, accessing resources, spawning entities
1 parent 6ed51c2 commit 07f4074

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

examples/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ git checkout v0.4.0
4747
- [Reflection](#reflection)
4848
- [Scene](#scene)
4949
- [Shaders](#shaders)
50+
- [Tests](#tests)
5051
- [Tools](#tools)
5152
- [UI (User Interface)](#ui-user-interface)
5253
- [Window](#window)
@@ -130,8 +131,8 @@ Example | File | Description
130131

131132
Example | File | Description
132133
--- | --- | ---
133-
`log_diagnostics` | [`diagnostics/log_diagnostics.rs`](./diagnostics/log_diagnostics.rs) | Add a plugin that logs diagnostics to the console
134134
`custom_diagnostic` | [`diagnostics/custom_diagnostic.rs`](./diagnostics/custom_diagnostic.rs) | Shows how to create a custom diagnostic
135+
`log_diagnostics` | [`diagnostics/log_diagnostics.rs`](./diagnostics/log_diagnostics.rs) | Add a plugin that logs diagnostics to the console
135136

136137
## ECS (Entity Component System)
137138

@@ -197,6 +198,12 @@ Example | File | Description
197198
`shader_custom_material` | [`shader/shader_custom_material.rs`](./shader/shader_custom_material.rs) | Illustrates creating a custom material and a shader that uses it
198199
`shader_defs` | [`shader/shader_defs.rs`](./shader/shader_defs.rs) | Demonstrates creating a custom material that uses "shaders defs" (a tool to selectively toggle parts of a shader)
199200

201+
## Tests
202+
203+
Example | File | Description
204+
--- | --- | ---
205+
`how_to_test_systems` | [`../tests/how_to_test_systems.rs`](../tests/how_to_test_systems.rs) | How to test systems with commands, queries or resources
206+
200207
## Tools
201208

202209
Example | File | Description

tests/how_to_test_systems.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use bevy::prelude::*;
2+
3+
#[derive(Default)]
4+
struct Enemy {
5+
hit_points: u32,
6+
}
7+
8+
fn despawn_dead_enemies(mut commands: Commands, enemies: Query<(Entity, &Enemy)>) {
9+
for (entity, enemy) in enemies.iter() {
10+
if enemy.hit_points == 0 {
11+
commands.entity(entity).despawn_recursive();
12+
}
13+
}
14+
}
15+
16+
fn hurt_enemies(mut enemies: Query<&mut Enemy>) {
17+
for mut enemy in enemies.iter_mut() {
18+
enemy.hit_points -= 1;
19+
}
20+
}
21+
22+
fn spawn_enemy(mut commands: Commands, keyboard_input: Res<Input<KeyCode>>) {
23+
if keyboard_input.just_pressed(KeyCode::Space) {
24+
commands.spawn().insert(Enemy { hit_points: 5 });
25+
}
26+
}
27+
28+
#[test]
29+
fn did_hurt_enemy() {
30+
// Setup world
31+
let mut world = World::default();
32+
33+
// Setup stage with our two systems
34+
let mut update_stage = SystemStage::parallel();
35+
update_stage.add_system(hurt_enemies.system().before("death"));
36+
update_stage.add_system(despawn_dead_enemies.system().label("death"));
37+
38+
// Setup test entities
39+
let enemy_id = world.spawn().insert(Enemy { hit_points: 5 }).id();
40+
41+
// Run systems
42+
update_stage.run(&mut world);
43+
44+
// Check resulting changes
45+
assert!(world.get::<Enemy>(enemy_id).is_some());
46+
assert_eq!(world.get::<Enemy>(enemy_id).unwrap().hit_points, 4);
47+
}
48+
49+
#[test]
50+
fn did_despawn_enemy() {
51+
// Setup world
52+
let mut world = World::default();
53+
54+
// Setup stage with our two systems
55+
let mut update_stage = SystemStage::parallel();
56+
update_stage.add_system(hurt_enemies.system().before("death"));
57+
update_stage.add_system(despawn_dead_enemies.system().label("death"));
58+
59+
// Setup test entities
60+
let enemy_id = world.spawn().insert(Enemy { hit_points: 1 }).id();
61+
62+
// Run systems
63+
update_stage.run(&mut world);
64+
65+
// Check resulting changes
66+
assert!(world.get::<Enemy>(enemy_id).is_none());
67+
}
68+
69+
#[test]
70+
fn spawn_enemy_using_input_resource() {
71+
// Setup world
72+
let mut world = World::default();
73+
74+
// Setup stage with a system
75+
let mut update_stage = SystemStage::parallel();
76+
update_stage.add_system(spawn_enemy.system());
77+
78+
// Setup test resource
79+
let mut input = Input::<KeyCode>::default();
80+
input.press(KeyCode::Space);
81+
world.insert_resource(input);
82+
83+
// Run systems
84+
update_stage.run(&mut world);
85+
86+
// Check resulting changes, one entity has been spawned with `Enemy` component
87+
assert_eq!(world.query::<&Enemy>().iter(&world).len(), 1);
88+
89+
// Clear the `just_pressed` status for all `KeyCode`s
90+
world.get_resource_mut::<Input<KeyCode>>().unwrap().update();
91+
92+
// Run systems
93+
update_stage.run(&mut world);
94+
95+
// Check resulting changes, no new entity has been spawned
96+
assert_eq!(world.query::<&Enemy>().iter(&world).len(), 1);
97+
}

0 commit comments

Comments
 (0)