Skip to content

Commit 90c8040

Browse files
committed
Bring in @payload's tests from #1076
1 parent 32ed97d commit 90c8040

File tree

2 files changed

+89
-79
lines changed

2 files changed

+89
-79
lines changed

crates/bevy_transform/src/components/children.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy_utils::HashMap;
44
use smallvec::SmallVec;
55
use std::ops::Deref;
66

7-
#[derive(Default, Clone, Debug, Reflect)]
7+
#[derive(Default, Clone, Debug, Reflect, PartialEq, Eq)]
88
#[reflect(Component, MapEntities)]
99
pub struct Children {
1010
order: SmallVec<[Entity; 8]>,
@@ -86,8 +86,11 @@ impl Children {
8686
}
8787

8888
pub(crate) fn insert(&mut self, index: usize, entities: &[Entity]) {
89+
let initial_count = self.order.len();
8990
self.extend(entities.iter().cloned());
90-
let mut desired_index = index;
91+
let actually_inserted = self.order.len() - initial_count;
92+
let mut desired_index =
93+
(index as i32 - (entities.len() as i32 - actually_inserted as i32)).max(0) as usize;
9194
for entity in entities {
9295
let current_index = self.uniqueness[entity];
9396
if current_index != desired_index {

crates/bevy_transform/src/hierarchy/child_builder.rs

Lines changed: 84 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ mod tests {
226226
let child1 = child1.expect("child1 should exist");
227227
let child2 = child2.expect("child2 should exist");
228228
let child3 = child3.expect("child3 should exist");
229-
let expected_children: SmallVec<[Entity; 8]> = smallvec![child1, child2, child3];
229+
let expected_children = vec![child1, child2, child3];
230230

231231
assert_eq!(
232232
world
233233
.get::<Children>(parent)
234234
.unwrap()
235235
.iter()
236-
.cloned()
237-
.collect::<SmallVec<[Entity; 8]>>(),
236+
.copied()
237+
.collect::<Vec<_>>(),
238238
expected_children
239239
);
240240
assert_eq!(*world.get::<Parent>(child1).unwrap(), Parent(parent));
@@ -250,110 +250,117 @@ mod tests {
250250
);
251251
}
252252

253-
#[test]
254-
fn push_and_insert_children() {
253+
fn setup() -> (World, Resources, Commands, Vec<Entity>, Entity) {
255254
let mut world = World::default();
256-
let mut resources = Resources::default();
257-
let mut commands = Commands::default();
255+
let resources = Resources::default();
256+
let commands = Commands::default();
258257
let entities = world
259-
.spawn_batch(vec![(1,), (2,), (3,), (4,), (5,)])
258+
.spawn_batch(vec![(0,), (1,), (2,), (3,), (4,)])
260259
.collect::<Vec<Entity>>();
260+
let parent = entities[0];
261+
(world, resources, commands, entities, parent)
262+
}
261263

262-
commands.push_children(entities[0], &entities[1..3]);
264+
#[test]
265+
fn push_children_adds_parent_component() {
266+
let (mut world, mut resources, mut commands, child, parent) = setup();
267+
commands.push_children(parent, &child[1..=2]);
263268
commands.apply(&mut world, &mut resources);
269+
assert_eq!(world.get::<Parent>(child[2]).unwrap(), &Parent(parent));
270+
}
264271

265-
let parent = entities[0];
266-
let child1 = entities[1];
267-
let child2 = entities[2];
268-
let child3 = entities[3];
269-
let child4 = entities[4];
270-
271-
let expected_children: SmallVec<[Entity; 8]> = smallvec![child1, child2];
272+
#[test]
273+
fn push_children_adds_previous_parent_component() {
274+
let (mut world, mut resources, mut commands, child, parent) = setup();
275+
commands.push_children(parent, &child[1..=2]);
276+
commands.apply(&mut world, &mut resources);
272277
assert_eq!(
273-
world
274-
.get::<Children>(parent)
275-
.unwrap()
276-
.iter()
277-
.cloned()
278-
.collect::<SmallVec<[Entity; 8]>>(),
279-
expected_children
278+
world.get::<PreviousParent>(child[2]).unwrap(),
279+
&PreviousParent(parent)
280280
);
281-
assert_eq!(*world.get::<Parent>(child1).unwrap(), Parent(parent));
282-
assert_eq!(*world.get::<Parent>(child2).unwrap(), Parent(parent));
281+
}
283282

283+
#[test]
284+
fn push_children_adds_children_component() {
285+
let (mut world, mut resources, mut commands, child, parent) = setup();
286+
commands.push_children(parent, &child[1..=2]);
287+
commands.apply(&mut world, &mut resources);
284288
assert_eq!(
285-
*world.get::<PreviousParent>(child1).unwrap(),
286-
PreviousParent(parent)
289+
world.get::<Children>(parent).unwrap(),
290+
&Children::with(&child[1..=2])
287291
);
292+
}
293+
294+
#[test]
295+
fn push_children_keeps_children_unique() {
296+
let (mut world, mut resources, mut commands, child, parent) = setup();
297+
commands.push_children(parent, &child[1..=2]);
298+
commands.apply(&mut world, &mut resources);
299+
commands.push_children(parent, &child[1..=2]);
300+
commands.apply(&mut world, &mut resources);
288301
assert_eq!(
289-
*world.get::<PreviousParent>(child2).unwrap(),
290-
PreviousParent(parent)
302+
world.get::<Children>(parent).unwrap(),
303+
&Children::with(&child[1..=2])
291304
);
305+
}
292306

293-
commands.insert_children(parent, 1, &entities[3..]);
307+
#[test]
308+
fn insert_children_adds_parent_component() {
309+
let (mut world, mut resources, mut commands, child, parent) = setup();
310+
commands.insert_children(parent, 0, &child[1..=2]);
294311
commands.apply(&mut world, &mut resources);
312+
assert_eq!(world.get::<Parent>(child[2]).unwrap(), &Parent(parent));
313+
}
295314

296-
let expected_children: SmallVec<[Entity; 8]> = smallvec![child1, child3, child4, child2];
315+
#[test]
316+
fn insert_children_adds_previous_parent_component() {
317+
let (mut world, mut resources, mut commands, child, parent) = setup();
318+
commands.insert_children(parent, 0, &child[1..=2]);
319+
commands.apply(&mut world, &mut resources);
297320
assert_eq!(
298-
world
299-
.get::<Children>(parent)
300-
.unwrap()
301-
.iter()
302-
.cloned()
303-
.collect::<SmallVec<[Entity; 8]>>(),
304-
expected_children
321+
world.get::<PreviousParent>(child[2]).unwrap(),
322+
&PreviousParent(parent)
305323
);
306-
assert_eq!(*world.get::<Parent>(child3).unwrap(), Parent(parent));
307-
assert_eq!(*world.get::<Parent>(child4).unwrap(), Parent(parent));
324+
}
325+
326+
#[test]
327+
fn insert_children_adds_children_component() {
328+
let (mut world, mut resources, mut commands, child, parent) = setup();
329+
commands.insert_children(parent, 0, &child[1..=2]);
330+
commands.apply(&mut world, &mut resources);
308331
assert_eq!(
309-
*world.get::<PreviousParent>(child3).unwrap(),
310-
PreviousParent(parent)
332+
world.get::<Children>(parent).unwrap(),
333+
&Children::with(&child[1..=2])
311334
);
335+
}
336+
337+
#[test]
338+
fn insert_children_keeps_children_unique() {
339+
let (mut world, mut resources, mut commands, child, parent) = setup();
340+
commands.insert_children(parent, 0, &child[1..=2]);
341+
commands.apply(&mut world, &mut resources);
342+
commands.insert_children(parent, 1, &child[1..=2]);
343+
commands.apply(&mut world, &mut resources);
312344
assert_eq!(
313-
*world.get::<PreviousParent>(child4).unwrap(),
314-
PreviousParent(parent)
345+
world.get::<Children>(parent).unwrap(),
346+
&Children::with(&child[1..=2])
315347
);
316348
}
317349

318350
#[test]
319-
fn push_duplicate_children() {
320-
let mut world = World::default();
321-
let mut resources = Resources::default();
322-
let mut commands = Commands::default();
323-
let entities = world
324-
.spawn_batch(vec![(1,), (2,), (3,)])
325-
.collect::<Vec<Entity>>();
326-
327-
let parent = entities[0];
328-
let child1 = entities[1];
329-
let child2 = entities[2];
330-
331-
commands.push_children(parent, &[child1, child1, child2, child1]);
351+
fn insert_children_keeps_children_order() {
352+
let (mut world, mut resources, mut commands, child, parent) = setup();
353+
commands.insert_children(parent, 0, &child[1..=2]);
332354
commands.apply(&mut world, &mut resources);
333-
334-
let expected_children: SmallVec<[Entity; 8]> = smallvec![child2, child1];
335355
assert_eq!(
336-
world
337-
.get::<Children>(parent)
338-
.unwrap()
339-
.iter()
340-
.cloned()
341-
.collect::<SmallVec<[Entity; 8]>>(),
342-
expected_children
356+
world.get::<Children>(parent).unwrap(),
357+
&Children::with(&[child[1], child[2]])
343358
);
344-
345-
commands.push_children(parent, &[child2]);
359+
commands.insert_children(parent, 1, &child[3..=4]);
346360
commands.apply(&mut world, &mut resources);
347-
348-
let expected_children: SmallVec<[Entity; 8]> = smallvec![child1, child2];
349361
assert_eq!(
350-
world
351-
.get::<Children>(parent)
352-
.unwrap()
353-
.iter()
354-
.cloned()
355-
.collect::<SmallVec<[Entity; 8]>>(),
356-
expected_children
362+
world.get::<Children>(parent).unwrap(),
363+
&Children::with(&[child[1], child[3], child[4], child[2]])
357364
);
358365
}
359366
}

0 commit comments

Comments
 (0)