Repository: bevyengine/bevy
File: crates/bevy_asset/src/assets.rs (Rust)

Code before:
    use bevy_reflect::TypePath;

    #[test]
    fn asset_index_round_trip() {
        let asset_index = AssetIndex {
            generation: 42,
            index: 1337,
        };
        let roundtripped = AssetIndex::from_bits(asset_index.to_bits());
        assert_eq!(asset_index, roundtripped);
    }

    #[test]
    fn assets_mut_change_detection() {
        #[derive(Asset, TypePath, Default)]
        struct TestAsset {
            value: u32,
        }

        #[derive(Resource, Default)]
        struct TestState {
            asset_target_value: u32,
            asset_modified_counter: u32,
        }

        let mut app = App::new();
        app.add_plugins(AssetPlugin::default());
        app.init_asset::<TestAsset>();
        app.insert_resource(TestState::default());

        let mut assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
        let my_asset_handle = assets.add(TestAsset::default());
        let my_asset_id = my_asset_handle.id();

        app.add_systems(
            Update,
            move |mut assets: ResMut<Assets<TestAsset>>, state: Res<TestState>| {
                let mut asset = assets.get_mut(my_asset_id).unwrap();

                if asset.value != state.asset_target_value {
                    asset.value = state.asset_target_value;
                }
            },
        );
        app.add_systems(
            Last,
            move |mut reader: MessageReader<AssetEvent<TestAsset>>,
                  mut state: ResMut<TestState>| {
                for event in reader.read() {
                    if event.is_modified(my_asset_id) {
                        state.asset_modified_counter += 1;

Review comment (suggestion):
Please use the `create_app` function from `crates/bevy_asset/src/lib.rs`. It prevents you from doing bad stuff accidentally, and hides away some setup code we don't care about.

Code after:
        current_generation: u32,
    },
    #[error("AssetIndex {index:?} has been removed")]
    Removed { index: AssetIndex },
}

#[cfg(test)]
mod test {
    use crate::tests::create_app;
    use crate::{Asset, AssetApp, AssetEvent, AssetIndex, Assets};
    use bevy_ecs::prelude::Messages;
    use bevy_reflect::TypePath;

    #[test]
    fn asset_index_round_trip() {
        let asset_index = AssetIndex {
            generation: 42,
            index: 1337,
        };
        let roundtripped = AssetIndex::from_bits(asset_index.to_bits());
        assert_eq!(asset_index, roundtripped);
    }

    #[test]
    fn assets_mut_change_detection() {
        #[derive(Asset, TypePath, Default)]
        struct TestAsset {
            value: u32,
        }

        let mut app = create_app().0;
        app.init_asset::<TestAsset>();

        let mut assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
        let my_asset_handle = assets.add(TestAsset::default());
        let my_asset_id = my_asset_handle.id();

        // check a few times just in case there are some unexpected leftover events from previous runs
        for _ in 0..3 {
            // check that modifying the asset value triggers an event
            {
                let mut assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
                let mut asset = assets.get_mut(my_asset_id).unwrap();
                asset.value += 1;
            }

            app.update();

            let modified_count = app
                .world_mut()
                .resource_mut::<Messages<AssetEvent<TestAsset>>>()