Skip to content

Commit f5dbfc3

Browse files
committed
Make systems in tests fallible
1 parent b59d6e7 commit f5dbfc3

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

crates/bevy_ecs/src/system/system_registry.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,18 @@ mod tests {
651651
assert_eq!(output, NonCopy(3));
652652
}
653653

654+
#[test]
655+
fn fallible_system() {
656+
fn four() -> Result<i32> {
657+
Ok(4)
658+
}
659+
660+
let mut world = World::new();
661+
let fallible_system_id = world.register_system(four);
662+
let output = world.run_system(fallible_system_id);
663+
assert!(matches!(output, Ok(Ok(x)) if x == four().unwrap() ));
664+
}
665+
654666
#[test]
655667
fn exclusive_system() {
656668
let mut world = World::new();
@@ -751,19 +763,53 @@ mod tests {
751763
assert!(matches!(output, Ok(x) if x == four()));
752764
}
753765

766+
#[test]
767+
fn cached_fallible_system() {
768+
fn four() -> Result<i32> {
769+
Ok(4)
770+
}
771+
772+
let mut world = World::new();
773+
let fallible_system_id = world.register_system_cached(four);
774+
let output = world.run_system(fallible_system_id);
775+
assert!(matches!(output, Ok(Ok(x)) if x == four().unwrap()));
776+
let output = world.run_system_cached(four);
777+
assert!(matches!(output, Ok(Ok(x)) if x == four().unwrap()));
778+
let output = world.run_system_cached_with(four, ());
779+
assert!(matches!(output, Ok(Ok(x)) if x == four().unwrap()));
780+
}
781+
754782
#[test]
755783
fn cached_system_commands() {
756-
fn sys(mut counter: ResMut<Counter>) {
757-
counter.0 = 1;
784+
fn increase(mut counter: ResMut<Counter>) {
785+
counter.0 += 1;
758786
}
759787

760788
let mut world = World::new();
761789
world.insert_resource(Counter(0));
762-
763-
world.commands().run_system_cached(sys);
790+
world.commands().run_system_cached(increase);
791+
world.flush_commands();
792+
assert_eq!(world.resource::<Counter>().0, 1);
793+
world.commands().run_system_cached_with(increase, ());
764794
world.flush_commands();
795+
assert_eq!(world.resource::<Counter>().0, 2);
796+
}
765797

798+
#[test]
799+
fn cached_fallible_system_commands() {
800+
fn increase(mut counter: ResMut<Counter>) -> Result {
801+
counter.0 += 1;
802+
Ok(())
803+
}
804+
805+
let mut world = World::new();
806+
world.insert_resource(Counter(0));
807+
world.commands().run_system_cached(increase);
808+
world.flush_commands();
766809
assert_eq!(world.resource::<Counter>().0, 1);
810+
world.commands().run_system_cached_with(increase, ());
811+
world.flush_commands();
812+
assert_eq!(world.resource::<Counter>().0, 2);
767813
}
768814

769815
#[test]

0 commit comments

Comments
 (0)