Skip to content

Commit

Permalink
Add test for scene/observer/mapped entity interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobsonchase committed Jul 31, 2024
1 parent e579622 commit d61c067
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
5 changes: 4 additions & 1 deletion crates/bevy_ecs/src/identifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;

use self::{error::IdentifierError, kinds::IdKind, masks::IdentifierMask};
use self::masks::IdentifierMask;
use std::{hash::Hash, num::NonZeroU32};

pub mod error;
pub(crate) mod kinds;
pub(crate) mod masks;

pub use self::error::IdentifierError;
pub use self::kinds::IdKind;

/// A unified identifier for all entity and similar IDs.
/// Has the same size as a `u64` integer, but the layout is split between a 32-bit low
/// segment, a 31-bit high segment, and the significant bit reserved as type flags to denote
Expand Down
48 changes: 47 additions & 1 deletion crates/bevy_scene/src/dynamic_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,19 @@ where

#[cfg(test)]
mod tests {
use bevy_app::App;
use bevy_ecs::entity::{Entity, EntityHashMap, EntityMapper, MapEntities};
use bevy_ecs::identifier::{IdKind, Identifier};
use bevy_ecs::observer::Trigger;
use bevy_ecs::reflect::{ReflectMapEntitiesResource, ReflectResource};
use bevy_ecs::system::Resource;
use bevy_ecs::system::{Query, Resource};
use bevy_ecs::world::{FromWorld, OnAdd};
use bevy_ecs::{reflect::AppTypeRegistry, world::Command, world::World};
use bevy_hierarchy::{Parent, PushChild};
use bevy_reflect::Reflect;

use crate::dynamic_scene_builder::DynamicSceneBuilder;
use crate::{DynamicEntity, DynamicScene};

#[derive(Resource, Reflect, Debug)]
#[reflect(Resource, MapEntitiesResource)]
Expand Down Expand Up @@ -343,4 +348,45 @@ mod tests {
"something is wrong with the this test or the code reloading scenes since the relationship between scene entities is broken"
);
}

#[test]
fn observers_should_see_mapped_entities() {
// Testing that Observers don't see the entites from the scene, but rather from the world itself.

let mut app = App::new();
app.init_resource::<AppTypeRegistry>()
.register_type::<Parent>()
.observe(|trigger: Trigger<OnAdd, Parent>, query: Query<&Parent>| {
let parent = query
.get(trigger.entity())
.expect("entity should have a parent");

assert_ne!(parent.get(), Entity::PLACEHOLDER);
});

let parent = DynamicEntity {
entity: Entity::PLACEHOLDER,
components: vec![],
};

let child = DynamicEntity {
entity: Entity::try_from(
Identifier::new(42, 7, IdKind::Entity).expect("Identifier should be valid"),
)
.expect("Entity should be valid"),
// Bit of a hack to construct a Parent - its FromWorld implementation uses Entity::PLACEHOLDER.
components: vec![Box::new(Parent::from_world(app.world_mut()))],
};

let scene = DynamicScene {
entities: vec![parent, child],
..Default::default()
};

let mut entity_map = EntityHashMap::<Entity>::default();

scene
.write_to_world(app.world_mut(), &mut entity_map)
.expect("write scene to world");
}
}

0 comments on commit d61c067

Please sign in to comment.