Skip to content

Instantly share code, notes, and snippets.

@oceantume
Last active September 18, 2022 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oceantume/c401731182f59ff07d6002f231fb7109 to your computer and use it in GitHub Desktop.
Save oceantume/c401731182f59ff07d6002f231fb7109 to your computer and use it in GitHub Desktop.
Unit test for Inconsistent empty Children component between hierarchy commands
#[cfg(test)]
mod tests {
use super::{BuildChildren, BuildWorldChildren};
use crate::prelude::{Children, Parent};
use smallvec::{smallvec, SmallVec};
use bevy_ecs::{
component::Component,
entity::Entity,
system::{CommandQueue, Commands},
world::World,
};
#[derive(Component)]
struct C(u32);
#[test]
fn children_removed_when_empty_commands() {
let mut world = World::default();
let entities = world
.spawn_batch(vec![(C(1),), (C(2),), (C(3),)])
.collect::<Vec<Entity>>();
let parent1 = entities[0];
let parent2 = entities[1];
let child = entities[2];
let mut queue = CommandQueue::default();
// push child into parent1
{
let mut commands = Commands::new(&mut queue, &world);
commands.entity(parent1).push_children(&[child]);
queue.apply(&mut world);
}
assert_eq!(world.get::<Children>(parent1).unwrap().0.as_slice(), &[child]);
// move child from parent1 with push_children
{
let mut commands = Commands::new(&mut queue, &world);
commands.entity(parent2).push_children(&[child]);
queue.apply(&mut world);
}
assert!(world.get::<Children>(parent1).is_none());
// move child from parent2 with insert_children
{
let mut commands = Commands::new(&mut queue, &world);
commands.entity(parent1).insert_children(0, &[child]);
queue.apply(&mut world);
}
assert!(world.get::<Children>(parent2).is_none());
// move child from parent1 with add_child
{
let mut commands = Commands::new(&mut queue, &world);
commands.entity(parent2).add_child(child);
queue.apply(&mut world);
}
assert!(world.get::<Children>(parent1).is_none());
// remove child from parent2. <------ The last assert for this fails because it's inconsistent
{
let mut commands = Commands::new(&mut queue, &world);
commands.entity(parent2).remove_children(&[child]);
queue.apply(&mut world);
}
// current behavior, which is different from other commands.
assert!(world.get::<Children>(parent2).unwrap().is_empty());
// expected behavior, which would be consistent with other commands.
assert!(world.get::<Children>(parent2).is_none());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment