Skip to content

Commit

Permalink
Unit tests for derived prefab loading
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-deason committed May 26, 2019
1 parent 918007c commit 304a3d1
Showing 1 changed file with 57 additions and 47 deletions.
104 changes: 57 additions & 47 deletions amethyst_derive/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,52 +96,52 @@ pub enum EnumPrefab {
#[cfg(test)]
mod tests {
use super::*;
use amethyst_core::ecs::{
Entities, Join, Builder,
};
use amethyst_assets::{Prefab, AssetStorage, Loader, PrefabLoaderSystem};
use amethyst_assets::{AssetStorage, Loader, Prefab, PrefabLoaderSystem};
use amethyst_core::ecs::{world::EntitiesRes, Builder, Join};
use amethyst_test::prelude::*;

macro_rules! assert_prefab {
($prefab_type:ident, $prefab:expr, $assertion:expr) => {
assert!(
AmethystApplication::ui_base::<String, String>()
.with_effect(|world| {
let handle = {
let loader = world.read_resource::<Loader>();
let storage = world.read_resource::<AssetStorage<Prefab<$prefab_type>>>();
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()
)
}
assert!(AmethystApplication::blank()
.with_system(
PrefabLoaderSystem::<$prefab_type>::default(),
"test_loader",
&[]
)
.with_effect(|world| {
let handle = {
let loader = world.read_resource::<Loader>();
let storage = world.read_resource::<AssetStorage<Prefab<$prefab_type>>>();
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,
}
external: External { inner: 100 }
},
|world| {
let entities = world.read_resource::<Entities>();
let entities = world.read_resource::<EntitiesRes>();
let storage = world.read_storage::<External>();

assert_eq!(
(&entities, &storage).join().map(|(_, ex)| assert_eq!(ex.inner, 100)).count(),
(&entities, &storage)
.join()
.map(|(_, ex)| assert_eq!(ex.inner, 100))
.count(),
1
);
);
}
);
}
Expand All @@ -151,18 +151,19 @@ mod tests {
assert_prefab!(
EnumPrefab,
EnumPrefab::One {
number: Stuff {
inner: 1,
}
number: Stuff { inner: 1 }
},
|world| {
let entities = world.read_resource::<Entities>();
let entities = world.read_resource::<EntitiesRes>();
let storage = world.read_storage::<Stuff<usize>>();

assert_eq!(
(&entities, &storage).join().map(|(_, ex)| assert_eq!(ex.inner, 1)).count(),
(&entities, &storage)
.join()
.map(|(_, ex)| assert_eq!(ex.inner, 1))
.count(),
1
);
);
}
);
}
Expand All @@ -172,18 +173,19 @@ mod tests {
assert_prefab!(
EnumPrefab,
EnumPrefab::Two {
component: External {
inner: 2,
}
component: External { inner: 2 }
},
|world| {
let entities = world.read_resource::<Entities>();
let entities = world.read_resource::<EntitiesRes>();
let storage = world.read_storage::<External>();

assert_eq!(
(&entities, &storage).join().map(|(_, ex)| assert_eq!(ex.inner, 2)).count(),
(&entities, &storage)
.join()
.map(|(_, ex)| assert_eq!(ex.inner, 2))
.count(),
1
);
);
}
);
}
Expand All @@ -192,17 +194,25 @@ mod tests {
fn instantiate_tuple_variant() {
assert_prefab!(
EnumPrefab,
EnumPrefab::Five(Stuff { inner: "three".to_string() }, External { inner: 4 }),
EnumPrefab::Five(
Stuff {
inner: "three".to_string()
},
External { inner: 4 }
),
|world| {
let entities = world.read_resource::<Entities>();
let entities = world.read_resource::<EntitiesRes>();
let stuff_storage = world.read_storage::<Stuff<String>>();
let external_storage = world.read_storage::<External>();

assert_eq!(
(&entities, &stuff_storage, &external_storage).join().map(|(_, st, ex)| {
assert_eq!(st.inner, "threeis");
assert_eq!(ex.inner, 4);
}).count(),
(&entities, &stuff_storage, &external_storage)
.join()
.map(|(_, st, ex)| {
assert_eq!(st.inner, "three");
assert_eq!(ex.inner, 4);
})
.count(),
1
);
}
Expand Down

0 comments on commit 304a3d1

Please sign in to comment.