bevy_ecs has a DropTestHelper struct for asserting the expected drop order/panic behavior for components/resource.
This is what a test using it could look like:
let helper = DropTestHelper::new();
let res = panic::catch_unwind(|| {
let mut world = World::new();
world
.spawn()
.insert(helper.make_component(true, 0))
.insert(helper.make_component(false, 1));
println!("Done inserting! Dropping world...");
});
let drop_log = helper.finish(res);
assert_eq!(
&*drop_log,
[
DropLogItem::Create(0),
DropLogItem::Create(1),
DropLogItem::Drop(0),
DropLogItem::Drop(1),
]
);
This test however fails:
#[test]
fn drop_test_helper() {
let drop_test_helper = DropTestHelper::new();
let res = std::panic::catch_unwind(|| {
drop(drop_test_helper.make_component(false, 0));
});
drop_test_helper.finish(res);
}
because the DropTestHelpers expected_panic_flag needs to be defused by dropping a drop_test_helper.make_component(true, _) (note the true parameter):
|
if !expected_panic_flag { |
|
match panic_res { |
|
Ok(()) => panic!("Expected a panic but it didn't happen"), |
|
Err(e) => panic::resume_unwind(e), |
|
} |
|
} |
bevy_ecshas aDropTestHelperstruct for asserting the expected drop order/panic behavior for components/resource.This is what a test using it could look like:
This test however fails:
because the
DropTestHelpersexpected_panic_flagneeds to be defused by dropping adrop_test_helper.make_component(true, _)(note thetrueparameter):bevy/crates/bevy_ecs/src/world/mod.rs
Lines 1691 to 1696 in cd19d27