#![allow( clippy::unneeded_field_pattern, clippy::block_in_if_condition_stmt, clippy::unneeded_field_pattern )] use sardonyx_derive::{EventReader, PrefabData}; use sardonyx_assets::{PrefabData, ProgressCounter}; use sardonyx_core::{ ecs::{Component, DenseVecStorage, Entity, Read, SystemData, World, WriteStorage}, shrev::{EventChannel, ReaderId}, EventReader, }; use sardonyx_error::Error; #[derive(Clone)] pub struct TestEvent1; #[derive(Clone)] pub struct TestEvent2; #[derive(Clone)] pub struct TestEvent3(T); #[derive(Clone, EventReader)] #[reader(TestEventReader)] pub enum TestEvent { One(TestEvent1), Two(TestEvent2), } #[derive(Clone, EventReader)] #[reader(TestEventWithTypeParameterReader)] pub enum TestEventWithTypeParameter where T1: Clone + Send + Sync + 'static, T2: Clone + Send + Sync + 'static, { One(TestEvent1), Two(TestEvent2), Three(TestEvent3), Four(TestEvent3), } #[derive(Clone, PrefabData, Default)] #[prefab(Component)] pub struct Stuff where T: Default + Clone + Send + Sync + 'static, { inner: T, } impl Component for Stuff where T: Clone + Default + Send + Sync + 'static, { type Storage = DenseVecStorage; } #[derive(Clone, PrefabData)] pub struct OuterPrefab where T: Default + Clone + Send + Sync + 'static, { inner: Stuff, } #[derive(Clone)] pub struct External { inner: u64, } impl Component for External { type Storage = DenseVecStorage; } #[derive(PrefabData, Clone)] pub struct Outer { #[prefab(Component)] external: External, } #[derive(PrefabData, Clone)] pub struct OuterTuple(#[prefab(Component)] External); #[derive(PrefabData, Clone)] pub enum EnumPrefab { One { number: Stuff, }, Two { #[prefab(Component)] component: External, }, Three {}, Four, Five(Stuff, #[prefab(Component)] External), } #[cfg(test)] mod tests { use super::*; use sardonyx_assets::{AssetStorage, Loader, Prefab, PrefabLoaderSystemDesc}; use sardonyx_core::ecs::{world::EntitiesRes, Builder, Join, WorldExt}; use sardonyx_test::prelude::*; macro_rules! assert_prefab { ($prefab_type:ident, $prefab:expr, $assertion:expr) => { assert!(sardonyxApplication::blank() .with_system_desc( PrefabLoaderSystemDesc::<$prefab_type>::default(), "test_loader", &[] ) .with_effect(|world| { let handle = { let loader = world.read_resource::(); let storage = world.read_resource::>>(); let mut prefab = Prefab::new(); prefab.main(Some($prefab)); loader.load_from_data(prefab, (), &storage) }; world.create_entity().with(handle).build(); }) .with_assertion($assertion) .run() .is_ok()) }; } #[test] fn instantiate_struct_prefabs() { assert_prefab!( Outer, Outer { external: External { inner: 100 } }, |world| { let entities = world.read_resource::(); let storage = world.read_storage::(); assert_eq!( (&entities, &storage) .join() .map(|(_, ex)| assert_eq!(ex.inner, 100)) .count(), 1 ); } ); } #[test] fn instantiate_struct_variant() { assert_prefab!( EnumPrefab, EnumPrefab::One { number: Stuff { inner: 1 } }, |world| { let entities = world.read_resource::(); let storage = world.read_storage::>(); assert_eq!( (&entities, &storage) .join() .map(|(_, ex)| assert_eq!(ex.inner, 1)) .count(), 1 ); } ); } #[test] fn instantiate_struct_variant_with_component_field() { assert_prefab!( EnumPrefab, EnumPrefab::Two { component: External { inner: 2 } }, |world| { let entities = world.read_resource::(); let storage = world.read_storage::(); assert_eq!( (&entities, &storage) .join() .map(|(_, ex)| assert_eq!(ex.inner, 2)) .count(), 1 ); } ); } #[test] fn instantiate_tuple_variant() { assert_prefab!( EnumPrefab, EnumPrefab::Five( Stuff { inner: "three".to_string() }, External { inner: 4 } ), |world| { let entities = world.read_resource::(); let stuff_storage = world.read_storage::>(); let external_storage = world.read_storage::(); assert_eq!( (&entities, &stuff_storage, &external_storage) .join() .map(|(_, st, ex)| { assert_eq!(st.inner, "three"); assert_eq!(ex.inner, 4); }) .count(), 1 ); } ); } }