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 Sep 24, 2024
1 parent efda7f3 commit 5ddb32f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 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::NonZero};

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
Expand Down
55 changes: 53 additions & 2 deletions crates/bevy_scene/src/dynamic_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,16 @@ where

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

use crate::dynamic_scene_builder::DynamicSceneBuilder;
Expand Down Expand Up @@ -348,4 +351,52 @@ 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 entities 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 mut scene_world = World::new();
scene_world.insert_resource(app.world().resource::<AppTypeRegistry>().clone());

for _ in 0..5 {
scene_world.spawn_empty();
}

let scene_child = scene_world.spawn_empty().id();
let scene_parent = scene_world.spawn_empty().add_children(&[scene_child]).id();

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

assert_ne!(parent.get(), scene_parent);
},
);

let scene = DynamicSceneBuilder::from_world(&scene_world)
.allow_component::<Parent>()
.allow_component::<Children>()
.extract_entities([scene_parent, scene_child].into_iter())
.build();

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

scene
.write_to_world(app.world_mut(), &mut entity_map)
.expect("write scene to world");
}
}
57 changes: 57 additions & 0 deletions crates/bevy_scene/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,60 @@ impl Scene {
Ok(())
}
}

#[cfg(test)]
mod tests {
use bevy_app::App;
use bevy_ecs::{
entity::{Entity, EntityHashMap},
observer::Trigger,
reflect::AppTypeRegistry,
system::Query,
world::{OnAdd, World},
};
use bevy_hierarchy::{BuildChildren, Children, Parent};

use crate::Scene;

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

let type_registry = AppTypeRegistry::default();
{
let mut registry_write = type_registry.write();
registry_write.register::<Parent>();
registry_write.register::<Children>();
}

let mut app = App::new();
app.insert_resource(type_registry.clone());

let mut scene_world = World::new();

for _ in 0..5 {
scene_world.spawn_empty();
}

let scene_child = scene_world.spawn_empty().id();
let scene_parent = scene_world.spawn_empty().add_children(&[scene_child]).id();

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

assert_ne!(parent.get(), scene_parent);
},
);

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

let scene = Scene { world: scene_world };

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

0 comments on commit 5ddb32f

Please sign in to comment.