Skip to content

Commit da794bf

Browse files
committed
Refactor tests for related entities
Spawn entities directly with ChildOf/OwnedBy. Reads easier.
1 parent 0036199 commit da794bf

1 file changed

Lines changed: 53 additions & 77 deletions

File tree

src/server/related_entities.rs

Lines changed: 53 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,9 @@ mod tests {
337337
.init_resource::<RelatedEntities>()
338338
.sync_related_entities::<ChildOf>();
339339

340-
let child1 = app.world_mut().spawn(Replicated).id();
341-
let child2 = app.world_mut().spawn(Replicated).id();
342-
let root = app
343-
.world_mut()
344-
.spawn(Replicated)
345-
.add_children(&[child1, child2])
346-
.id();
340+
let root = app.world_mut().spawn(Replicated).id();
341+
let child1 = app.world_mut().spawn((Replicated, ChildOf(root))).id();
342+
let child2 = app.world_mut().spawn((Replicated, ChildOf(root))).id();
347343

348344
let mut related = app.world_mut().resource_mut::<RelatedEntities>();
349345
related.rebuild_graphs();
@@ -361,10 +357,10 @@ mod tests {
361357
.init_resource::<RelatedEntities>()
362358
.sync_related_entities::<ChildOf>();
363359

364-
let child1 = app.world_mut().spawn(Replicated).id();
365-
let root1 = app.world_mut().spawn(Replicated).add_child(child1).id();
366-
let child2 = app.world_mut().spawn(Replicated).id();
367-
let root2 = app.world_mut().spawn(Replicated).add_child(child2).id();
360+
let root1 = app.world_mut().spawn(Replicated).id();
361+
let child1 = app.world_mut().spawn((Replicated, ChildOf(root1))).id();
362+
let root2 = app.world_mut().spawn(Replicated).id();
363+
let child2 = app.world_mut().spawn((Replicated, ChildOf(root2))).id();
368364

369365
let mut related = app.world_mut().resource_mut::<RelatedEntities>();
370366
related.rebuild_graphs();
@@ -383,9 +379,9 @@ mod tests {
383379
.init_resource::<RelatedEntities>()
384380
.sync_related_entities::<ChildOf>();
385381

386-
let grandchild = app.world_mut().spawn(Replicated).id();
387-
let child = app.world_mut().spawn(Replicated).add_child(grandchild).id();
388-
let root = app.world_mut().spawn(Replicated).add_child(child).id();
382+
let root = app.world_mut().spawn(Replicated).id();
383+
let child = app.world_mut().spawn((Replicated, ChildOf(root))).id();
384+
let grandchild = app.world_mut().spawn((Replicated, ChildOf(child))).id();
389385

390386
let mut related = app.world_mut().resource_mut::<RelatedEntities>();
391387
related.rebuild_graphs();
@@ -403,24 +399,23 @@ mod tests {
403399
.init_resource::<RelatedEntities>()
404400
.sync_related_entities::<ChildOf>();
405401

406-
let grandgrandchild = app.world_mut().spawn(Replicated).id();
407-
let grandchild = app
402+
let root = app.world_mut().spawn(Replicated).id();
403+
let child = app.world_mut().spawn((Replicated, ChildOf(root))).id();
404+
let grandchild = app.world_mut().spawn((Replicated, ChildOf(child))).id();
405+
let grandgrandchild = app
408406
.world_mut()
409-
.spawn(Replicated)
410-
.add_child(grandgrandchild)
407+
.spawn((Replicated, ChildOf(grandchild)))
411408
.id();
412-
let child = app.world_mut().spawn(Replicated).add_child(grandchild).id();
413-
let root = app.world_mut().spawn(Replicated).add_child(child).id();
414409

415410
app.world_mut().entity_mut(grandchild).remove::<ChildOf>();
416411

417412
let mut related = app.world_mut().resource_mut::<RelatedEntities>();
418413
related.rebuild_graphs();
419414
assert_eq!(related.graphs_count(), 2);
420-
assert_eq!(related.graph_index(root), Some(1));
421-
assert_eq!(related.graph_index(child), Some(1));
422-
assert_eq!(related.graph_index(grandchild), Some(0));
423-
assert_eq!(related.graph_index(grandgrandchild), Some(0));
415+
assert_eq!(related.graph_index(root), Some(0));
416+
assert_eq!(related.graph_index(child), Some(0));
417+
assert_eq!(related.graph_index(grandchild), Some(1));
418+
assert_eq!(related.graph_index(grandgrandchild), Some(1));
424419
}
425420

426421
#[test]
@@ -431,10 +426,10 @@ mod tests {
431426
.init_resource::<RelatedEntities>()
432427
.sync_related_entities::<ChildOf>();
433428

434-
let child1 = app.world_mut().spawn(Replicated).id();
435-
let root1 = app.world_mut().spawn(Replicated).add_child(child1).id();
436-
let child2 = app.world_mut().spawn(Replicated).id();
437-
let root2 = app.world_mut().spawn(Replicated).add_child(child2).id();
429+
let root1 = app.world_mut().spawn(Replicated).id();
430+
let child1 = app.world_mut().spawn((Replicated, ChildOf(root1))).id();
431+
let root2 = app.world_mut().spawn(Replicated).id();
432+
let child2 = app.world_mut().spawn((Replicated, ChildOf(root2))).id();
438433

439434
app.world_mut().entity_mut(child1).add_child(root2);
440435

@@ -455,10 +450,10 @@ mod tests {
455450
.init_resource::<RelatedEntities>()
456451
.sync_related_entities::<ChildOf>();
457452

458-
let child1 = app.world_mut().spawn(Replicated).id();
459-
let root1 = app.world_mut().spawn(Replicated).add_child(child1).id();
460-
let child2 = app.world_mut().spawn(Replicated).id();
461-
let root2 = app.world_mut().spawn(Replicated).add_child(child2).id();
453+
let root1 = app.world_mut().spawn(Replicated).id();
454+
let child1 = app.world_mut().spawn((Replicated, ChildOf(root1))).id();
455+
let root2 = app.world_mut().spawn(Replicated).id();
456+
let child2 = app.world_mut().spawn((Replicated, ChildOf(root2))).id();
462457

463458
app.world_mut().entity_mut(child1).insert(ChildOf(root2));
464459

@@ -479,8 +474,8 @@ mod tests {
479474
.init_resource::<RelatedEntities>()
480475
.sync_related_entities::<ChildOf>();
481476

482-
let child = app.world_mut().spawn(Replicated).id();
483-
let root = app.world_mut().spawn(Replicated).add_child(child).id();
477+
let root = app.world_mut().spawn(Replicated).id();
478+
let child = app.world_mut().spawn((Replicated, ChildOf(root))).id();
484479

485480
app.world_mut().entity_mut(child).remove::<ChildOf>();
486481

@@ -499,13 +494,9 @@ mod tests {
499494
.init_resource::<RelatedEntities>()
500495
.sync_related_entities::<ChildOf>();
501496

502-
let child1 = app.world_mut().spawn(Replicated).id();
503-
let child2 = app.world_mut().spawn(Replicated).id();
504-
let root = app
505-
.world_mut()
506-
.spawn(Replicated)
507-
.add_children(&[child1, child2])
508-
.id();
497+
let root = app.world_mut().spawn(Replicated).id();
498+
let child1 = app.world_mut().spawn((Replicated, ChildOf(root))).id();
499+
let child2 = app.world_mut().spawn((Replicated, ChildOf(root))).id();
509500

510501
app.world_mut().despawn(root);
511502

@@ -526,12 +517,11 @@ mod tests {
526517
.sync_related_entities::<ChildOf>()
527518
.sync_related_entities::<OwnedBy>();
528519

529-
let child = app.world_mut().spawn(Replicated).id();
530-
let root1 = app.world_mut().spawn(Replicated).add_child(child).id();
531-
let root2 = app
520+
let root1 = app.world_mut().spawn(Replicated).id();
521+
let root2 = app.world_mut().spawn(Replicated).id();
522+
let child = app
532523
.world_mut()
533-
.spawn(Replicated)
534-
.add_one_related::<OwnedBy>(child)
524+
.spawn((Replicated, ChildOf(root1), OwnedBy(root2)))
535525
.id();
536526

537527
let mut related = app.world_mut().resource_mut::<RelatedEntities>();
@@ -551,12 +541,10 @@ mod tests {
551541
.sync_related_entities::<ChildOf>()
552542
.sync_related_entities::<OwnedBy>();
553543

554-
let child = app.world_mut().spawn(Replicated).id();
555-
let root = app
544+
let root = app.world_mut().spawn(Replicated).id();
545+
let child = app
556546
.world_mut()
557-
.spawn(Replicated)
558-
.add_child(child)
559-
.add_one_related::<OwnedBy>(child)
547+
.spawn((Replicated, ChildOf(root), OwnedBy(root)))
560548
.id();
561549

562550
let mut related = app.world_mut().resource_mut::<RelatedEntities>();
@@ -575,12 +563,10 @@ mod tests {
575563
.sync_related_entities::<ChildOf>()
576564
.sync_related_entities::<OwnedBy>();
577565

578-
let child = app.world_mut().spawn(Replicated).id();
579-
let root = app
566+
let root = app.world_mut().spawn(Replicated).id();
567+
let child = app
580568
.world_mut()
581-
.spawn(Replicated)
582-
.add_child(child)
583-
.add_one_related::<OwnedBy>(child)
569+
.spawn((Replicated, ChildOf(root), OwnedBy(root)))
584570
.id();
585571

586572
app.world_mut().entity_mut(child).remove::<ChildOf>();
@@ -601,13 +587,9 @@ mod tests {
601587
.sync_related_entities::<ChildOf>()
602588
.sync_related_entities::<OwnedBy>();
603589

604-
let grandchild = app.world_mut().spawn(Replicated).id();
605-
let child = app.world_mut().spawn(Replicated).add_child(grandchild).id();
606-
let root = app
607-
.world_mut()
608-
.spawn(Replicated)
609-
.add_one_related::<OwnedBy>(child)
610-
.id();
590+
let root = app.world_mut().spawn(Replicated).id();
591+
let child = app.world_mut().spawn((Replicated, ChildOf(root))).id();
592+
let grandchild = app.world_mut().spawn((Replicated, OwnedBy(child))).id();
611593

612594
let mut related = app.world_mut().resource_mut::<RelatedEntities>();
613595
related.rebuild_graphs();
@@ -626,8 +608,8 @@ mod tests {
626608
.sync_related_entities::<ChildOf>()
627609
.sync_related_entities::<OwnedBy>();
628610

629-
let child = app.world_mut().spawn_empty().id();
630-
let root = app.world_mut().spawn_empty().add_child(child).id();
611+
let root = app.world_mut().spawn_empty().id();
612+
let child = app.world_mut().spawn(ChildOf(root)).id();
631613

632614
app.world_mut().entity_mut(child).insert(Replicated);
633615
app.world_mut().entity_mut(root).insert(Replicated);
@@ -648,12 +630,10 @@ mod tests {
648630
.sync_related_entities::<ChildOf>()
649631
.sync_related_entities::<OwnedBy>();
650632

651-
let child = app.world_mut().spawn(Replicated).id();
652-
let root = app
633+
let root = app.world_mut().spawn(Replicated).id();
634+
let child = app
653635
.world_mut()
654-
.spawn(Replicated)
655-
.add_child(child)
656-
.add_one_related::<OwnedBy>(child)
636+
.spawn((Replicated, ChildOf(root), OwnedBy(root)))
657637
.id();
658638

659639
app.world_mut().entity_mut(child).remove::<Replicated>();
@@ -673,13 +653,9 @@ mod tests {
673653
.init_resource::<RelatedEntities>()
674654
.sync_related_entities::<ChildOf>();
675655

676-
let child1 = app.world_mut().spawn(Replicated).id();
677-
let child2 = app.world_mut().spawn(Replicated).id();
678-
let root = app
679-
.world_mut()
680-
.spawn(Replicated)
681-
.add_children(&[child1, child2])
682-
.id();
656+
let root = app.world_mut().spawn(Replicated).id();
657+
let child1 = app.world_mut().spawn((Replicated, ChildOf(root))).id();
658+
let child2 = app.world_mut().spawn((Replicated, ChildOf(root))).id();
683659

684660
let mut related = app.world_mut().resource_mut::<RelatedEntities>();
685661
related.rebuild_graphs();

0 commit comments

Comments
 (0)