Skip to content

DropTestHelper cannot be used when not testing for at least one panic #5615

@jakobhellermann

Description

@jakobhellermann

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),
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-TestingA change that impacts how we test Bevy or how users test their apps

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions