Skip to content

Commit b9779d5

Browse files
author
Brian Merchant
committed
Simple tests for AddedOne/ChangedOne
1 parent 3603ced commit b9779d5

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

src/tests.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,106 @@ fn age_up_not(mut q: Query<&mut dyn Person, Without<Fem>>) {
188188
}
189189
}
190190

191+
#[test]
192+
fn added_filter() {
193+
let mut world = World::new();
194+
world.init_resource::<Output>();
195+
world
196+
.register_component_as::<dyn Person, Human>()
197+
.register_component_as::<dyn Person, Dolphin>();
198+
199+
world.spawn(Human("Henry".to_owned(), 22));
200+
201+
let mut stage = SystemStage::parallel();
202+
stage
203+
.add_system(print_added_info)
204+
.add_system(age_up_fem.after(print_added_info))
205+
.add_system(age_up_not.after(print_added_info));
206+
207+
stage.run(&mut world);
208+
209+
world.spawn((Dolphin(27), Fem));
210+
211+
stage.run(&mut world);
212+
213+
stage.run(&mut world);
214+
215+
assert_eq!(
216+
world.resource::<Output>().0,
217+
&[
218+
"Added people:",
219+
"Henry: 22",
220+
"",
221+
"Added people:",
222+
"Reginald: 27",
223+
"",
224+
"Added people:",
225+
"",
226+
]
227+
);
228+
}
229+
230+
// Prints the name and age of every newly added `Person`.
231+
fn print_added_info(people: Query<AddedOne<&dyn Person>>, mut output: ResMut<Output>) {
232+
output.0.push("Added people:".to_string());
233+
for person in (&people).into_iter().flatten() {
234+
output
235+
.0
236+
.push(format!("{}: {}", person.name(), person.age()));
237+
}
238+
output.0.push(default());
239+
}
240+
241+
#[test]
242+
fn changed_filter() {
243+
let mut world = World::new();
244+
world.init_resource::<Output>();
245+
world
246+
.register_component_as::<dyn Person, Human>()
247+
.register_component_as::<dyn Person, Dolphin>();
248+
249+
world.spawn(Human("Henry".to_owned(), 22));
250+
251+
let mut stage = SystemStage::parallel();
252+
stage
253+
.add_system(print_changed_info)
254+
.add_system(age_up_fem.after(print_changed_info));
255+
256+
stage.run(&mut world);
257+
258+
world.spawn((Dolphin(27), Fem));
259+
260+
stage.run(&mut world);
261+
262+
stage.run(&mut world);
263+
264+
assert_eq!(
265+
world.resource::<Output>().0,
266+
&[
267+
"Changed people:",
268+
"Henry: 22",
269+
"",
270+
"Changed people:",
271+
"Reginald: 27",
272+
"",
273+
"Changed people:",
274+
"Reginald: 28",
275+
""
276+
]
277+
);
278+
}
279+
280+
// Prints the name and age of every `Person` whose info has changed in some way
281+
fn print_changed_info(people: Query<ChangedOne<&dyn Person>>, mut output: ResMut<Output>) {
282+
output.0.push("Changed people:".to_string());
283+
for person in (&people).into_iter().flatten() {
284+
output
285+
.0
286+
.push(format!("{}: {}", person.name(), person.age()));
287+
}
288+
output.0.push(default());
289+
}
290+
191291
#[queryable]
192292
pub trait Messages {
193293
fn send(&mut self, _: &dyn Display);

0 commit comments

Comments
 (0)