@@ -450,32 +450,71 @@ mod tests {
450
450
}
451
451
452
452
#[ test]
453
- fn remove_tracking ( ) {
453
+ fn removal_tracking ( ) {
454
454
let mut world = World :: new ( ) ;
455
+
456
+ let entity_to_despawn = world. spawn ( ) . insert ( W ( 1 ) ) . id ( ) ;
457
+ let entity_to_remove_w_from = world. spawn ( ) . insert ( W ( 2 ) ) . id ( ) ;
458
+ let spurious_entity = world. spawn ( ) . id ( ) ;
459
+
460
+ // Track which entities we want to operate on
455
461
struct Despawned ( Entity ) ;
456
- let a = world. spawn ( ) . insert_bundle ( ( W ( "abc" ) , W ( 123 ) ) ) . id ( ) ;
457
- world. spawn ( ) . insert_bundle ( ( W ( "abc" ) , W ( 123 ) ) ) ;
458
- world. insert_resource ( false ) ;
459
- world. insert_resource ( Despawned ( a) ) ;
462
+ world. insert_resource ( Despawned ( entity_to_despawn) ) ;
463
+ struct Removed ( Entity ) ;
464
+ world. insert_resource ( Removed ( entity_to_remove_w_from) ) ;
460
465
461
- world. entity_mut ( a) . despawn ( ) ;
466
+ // Verify that all the systems actually ran
467
+ #[ derive( Default ) ]
468
+ struct NSystems ( usize ) ;
469
+ world. insert_resource ( NSystems :: default ( ) ) ;
462
470
463
- fn validate_removed (
471
+ // First, check that removal detection is triggered if and only if we despawn an entity with the correct component
472
+ world. entity_mut ( entity_to_despawn) . despawn ( ) ;
473
+ world. entity_mut ( spurious_entity) . despawn ( ) ;
474
+
475
+ fn validate_despawn (
464
476
removed_i32 : RemovedComponents < W < i32 > > ,
465
477
despawned : Res < Despawned > ,
466
- mut ran : ResMut < bool > ,
478
+ mut n_systems : ResMut < NSystems > ,
467
479
) {
468
480
assert_eq ! (
469
481
removed_i32. iter( ) . collect:: <Vec <_>>( ) ,
470
482
& [ despawned. 0 ] ,
471
- "despawning results in 'removed component' state "
483
+ "despawning causes the correct entity to show up in the 'RemovedComponent' system parameter. "
472
484
) ;
473
485
474
- * ran = true ;
486
+ n_systems . 0 += 1 ;
475
487
}
476
488
477
- run_system ( & mut world, validate_removed) ;
478
- assert ! ( * world. get_resource:: <bool >( ) . unwrap( ) , "system ran" ) ;
489
+ run_system ( & mut world, validate_despawn) ;
490
+
491
+ // Reset the trackers to clear the buffer of removed components
492
+ // Ordinarily, this is done in a system added by MinimalPlugins
493
+ world. clear_trackers ( ) ;
494
+
495
+ // Then, try removing a component
496
+ world. spawn ( ) . insert ( W ( 3 ) ) . id ( ) ;
497
+ world. spawn ( ) . insert ( W ( 4 ) ) . id ( ) ;
498
+ world. entity_mut ( entity_to_remove_w_from) . remove :: < W < i32 > > ( ) ;
499
+
500
+ fn validate_remove (
501
+ removed_i32 : RemovedComponents < W < i32 > > ,
502
+ removed : Res < Removed > ,
503
+ mut n_systems : ResMut < NSystems > ,
504
+ ) {
505
+ assert_eq ! (
506
+ removed_i32. iter( ) . collect:: <Vec <_>>( ) ,
507
+ & [ removed. 0 ] ,
508
+ "removing a component causes the correct entity to show up in the 'RemovedComponent' system parameter."
509
+ ) ;
510
+
511
+ n_systems. 0 += 1 ;
512
+ }
513
+
514
+ run_system ( & mut world, validate_remove) ;
515
+
516
+ // Verify that both systems actually ran
517
+ assert_eq ! ( world. get_resource:: <NSystems >( ) . unwrap( ) . 0 , 2 ) ;
479
518
}
480
519
481
520
#[ test]
0 commit comments