@@ -366,7 +366,7 @@ impl DisplayWithWorld for ReflectBaseType {
366
366
impl DisplayWithWorld for TypeId {
367
367
fn display_with_world ( & self , world : WorldGuard ) -> String {
368
368
if * self == TypeId :: of :: < FakeType > ( ) {
369
- return "Dynamic Type" . to_owned ( ) ;
369
+ return "Unknown Type" . to_owned ( ) ;
370
370
} else if * self == TypeId :: of :: < World > ( ) {
371
371
// does not implement Reflect, so we do this manually
372
372
return "World" . to_owned ( ) ;
@@ -485,3 +485,113 @@ impl<T: DisplayWithWorld> DisplayWithWorld for Vec<T> {
485
485
string
486
486
}
487
487
}
488
+
489
+ #[ cfg( test) ]
490
+ mod test {
491
+ use bevy:: prelude:: AppTypeRegistry ;
492
+
493
+ use crate :: bindings:: {
494
+ function:: script_function:: AppScriptFunctionRegistry , AppReflectAllocator ,
495
+ ReflectAllocationId , WorldAccessGuard ,
496
+ } ;
497
+
498
+ use super :: * ;
499
+
500
+ fn setup_world ( ) -> World {
501
+ let mut world = World :: default ( ) ;
502
+
503
+ let type_registry = AppTypeRegistry :: default ( ) ;
504
+ world. insert_resource ( type_registry) ;
505
+
506
+ let allocator = AppReflectAllocator :: default ( ) ;
507
+ world. insert_resource ( allocator) ;
508
+
509
+ let script_function_registry = AppScriptFunctionRegistry :: default ( ) ;
510
+ world. insert_resource ( script_function_registry) ;
511
+
512
+ world
513
+ }
514
+
515
+ #[ test]
516
+ fn test_type_id ( ) {
517
+ let mut world = setup_world ( ) ;
518
+ let world = WorldGuard :: new ( WorldAccessGuard :: new ( & mut world) ) ;
519
+
520
+ let type_id = TypeId :: of :: < usize > ( ) ;
521
+ assert_eq ! ( type_id. display_with_world( world. clone( ) ) , "usize" ) ;
522
+ assert_eq ! ( type_id. display_value_with_world( world. clone( ) ) , "usize" ) ;
523
+ assert_eq ! ( type_id. display_without_world( ) , format!( "{:?}" , type_id) ) ;
524
+
525
+ let type_id = TypeId :: of :: < FakeType > ( ) ;
526
+ assert_eq ! ( type_id. display_with_world( world. clone( ) ) , "Unknown Type" ) ;
527
+ assert_eq ! (
528
+ type_id. display_value_with_world( world. clone( ) ) ,
529
+ "Unknown Type"
530
+ ) ;
531
+ assert_eq ! ( type_id. display_without_world( ) , format!( "{:?}" , type_id) ) ;
532
+ }
533
+
534
+ #[ test]
535
+ fn test_reflect_base_type ( ) {
536
+ let mut world = setup_world ( ) ;
537
+ let world = WorldGuard :: new ( WorldAccessGuard :: new ( & mut world) ) ;
538
+
539
+ let type_id = TypeId :: of :: < usize > ( ) ;
540
+
541
+ assert_eq ! (
542
+ ReflectBaseType {
543
+ base_id: ReflectBase :: Owned ( ReflectAllocationId :: new( 0 ) ) ,
544
+ type_id,
545
+ }
546
+ . display_with_world( world. clone( ) ) ,
547
+ "Allocation(0)(usize)"
548
+ ) ;
549
+
550
+ assert_eq ! (
551
+ ReflectBaseType {
552
+ base_id: ReflectBase :: Owned ( ReflectAllocationId :: new( 0 ) ) ,
553
+ type_id,
554
+ }
555
+ . display_value_with_world( world. clone( ) ) ,
556
+ "Allocation(0)(usize)"
557
+ ) ;
558
+
559
+ assert_eq ! (
560
+ ReflectBaseType {
561
+ base_id: ReflectBase :: Owned ( ReflectAllocationId :: new( 0 ) ) ,
562
+ type_id,
563
+ }
564
+ . display_without_world( ) ,
565
+ format!( "Allocation(0)({:?})" , type_id)
566
+ ) ;
567
+ }
568
+
569
+ #[ test]
570
+ fn test_reflect_reference ( ) {
571
+ let mut world = setup_world ( ) ;
572
+
573
+ let world = WorldGuard :: new ( WorldAccessGuard :: new ( & mut world) ) ;
574
+
575
+ let type_id = TypeId :: of :: < usize > ( ) ;
576
+
577
+ let allocator = world. allocator ( ) ;
578
+ let mut allocator_write = allocator. write ( ) ;
579
+ let reflect_reference = ReflectReference :: new_allocated ( 2usize , & mut allocator_write) ;
580
+ drop ( allocator_write) ;
581
+
582
+ assert_eq ! (
583
+ reflect_reference. display_with_world( world. clone( ) ) ,
584
+ "<Reference to Allocation(0)(usize) -> usize>"
585
+ ) ;
586
+
587
+ assert_eq ! (
588
+ reflect_reference. display_value_with_world( world. clone( ) ) ,
589
+ "Reflect(usize(2))"
590
+ ) ;
591
+
592
+ assert_eq ! (
593
+ reflect_reference. display_without_world( ) ,
594
+ format!( "<Reference to Allocation(0)({:?})>" , type_id)
595
+ ) ;
596
+ }
597
+ }
0 commit comments