Skip to content

Commit ff90a0b

Browse files
committed
add more tests to printing and conversions
1 parent 0109654 commit ff90a0b

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

crates/bevy_mod_scripting_core/src/bindings/pretty_print.rs

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl DisplayWithWorld for ReflectBaseType {
366366
impl DisplayWithWorld for TypeId {
367367
fn display_with_world(&self, world: WorldGuard) -> String {
368368
if *self == TypeId::of::<FakeType>() {
369-
return "Dynamic Type".to_owned();
369+
return "Unknown Type".to_owned();
370370
} else if *self == TypeId::of::<World>() {
371371
// does not implement Reflect, so we do this manually
372372
return "World".to_owned();
@@ -485,3 +485,113 @@ impl<T: DisplayWithWorld> DisplayWithWorld for Vec<T> {
485485
string
486486
}
487487
}
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+
}

crates/bevy_mod_scripting_core/src/bindings/script_value.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,35 @@ impl TryFrom<ScriptValue> for ParsedPath {
182182
})
183183
}
184184
}
185+
186+
#[cfg(test)]
187+
mod test {
188+
use super::*;
189+
190+
#[test]
191+
fn test_script_value_to_parsed_path() {
192+
let value = ScriptValue::String("test".into());
193+
let parsed_path = ParsedPath::from(vec![OffsetAccess {
194+
access: bevy::reflect::Access::Field("test".to_owned().into()),
195+
offset: Some(4),
196+
}]);
197+
assert_eq!(parsed_path, ParsedPath::try_from(value).unwrap());
198+
199+
let value = ScriptValue::String("_0".into());
200+
let parsed_path = ParsedPath::from(vec![OffsetAccess {
201+
access: bevy::reflect::Access::TupleIndex(0),
202+
offset: Some(1),
203+
}]);
204+
assert_eq!(parsed_path, ParsedPath::try_from(value).unwrap());
205+
206+
let value = ScriptValue::Integer(0);
207+
let parsed_path = ParsedPath::from(vec![OffsetAccess {
208+
access: bevy::reflect::Access::ListIndex(0),
209+
offset: Some(1),
210+
}]);
211+
assert_eq!(parsed_path, ParsedPath::try_from(value).unwrap());
212+
213+
let value = ScriptValue::Float(0.0);
214+
assert!(ParsedPath::try_from(value).is_err());
215+
}
216+
}

0 commit comments

Comments
 (0)