Skip to content

Commit

Permalink
test: add unit tests for get_many_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
SOF3 committed Dec 17, 2023
1 parent b2d0aee commit 77f986e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub trait Ref: sealed::Sealed {

/// The underlying entity ID referenced.
fn id(&self) -> <Self::Archetype as Archetype>::RawEntity;

/// Converts this entity reference to a homogeneous temporary.
fn as_ref(&self) -> TempRef<'_, Self::Archetype> { TempRef::new(self.id()) }
}

/// A temporary, non-`'static` reference to an entity.
Expand Down
19 changes: 19 additions & 0 deletions src/system/access/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,25 @@ where
),
}
}

/// Returns mutable references to the component for the specified entities.
///
/// # Panics
/// Panics if `entities` contains duplicate items.
pub fn get_many_mut<const N: usize>(
&mut self,
entities: [impl entity::Ref<Archetype = A>; N],
) -> [&mut C; N] {
match self.try_get_many_mut(entities) {
Some(comps) => comps,
None => panic!(
"Parameter contains duplicate entities, or component {}/{} implements comp::Must \
but is not present",
any::type_name::<A>(),
any::type_name::<C>(),
),
}
}
}

#[derive_trait(pub Set{
Expand Down
34 changes: 33 additions & 1 deletion src/system/access/single/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Tests simple storage access.
use crate::entity::{generation, Ref as _};
use crate::test_util::*;
use crate::{system, system_test, tracer};

Expand All @@ -10,7 +11,7 @@ fn test_simple_fetch() {
mut comp5: system::WriteSimple<TestArch, Simple5RequiredNoInit>,
#[dynec(global)] initials: &InitialEntities,
) {
let ent = initials.strong.as_ref().expect("initials.strong is None");
let ent = initials.strong.as_ref().expect("initials.strong is assigned during init");

let comp = comp5.get_mut(ent);
assert_eq!(comp.0, 7);
Expand All @@ -28,3 +29,34 @@ fn test_simple_fetch() {
let comp = storage.try_get(ent);
assert_eq!(comp, Some(&Simple5RequiredNoInit(20)));
}

#[test]
fn test_get_many() {
#[system(dynec_as(crate))]
fn test_system(
mut comp5: system::WriteSimple<TestArch, Simple5RequiredNoInit>,
#[dynec(global)] initials: &InitialEntities,
) {
let strong = initials.strong.as_ref().expect("initials.stonrg is assigned during init");
let weak = initials.weak.as_ref().expect("initials.weak is assigned during init");

let [strong_comp, weak_comp] = comp5.get_many_mut([strong.as_ref(), weak.as_ref()]);
strong_comp.0 += 11;
weak_comp.0 += 13;
}

let mut world = system_test!(test_system.build(););

let strong = world.create(crate::comps![@(crate) TestArch => Simple5RequiredNoInit(7)]);
world.get_global::<InitialEntities>().strong = Some(strong.clone());

let weak = world.create(crate::comps![@(crate) TestArch => Simple5RequiredNoInit(3)]);
world.get_global::<InitialEntities>().weak =
Some(weak.weak(world.get_global::<generation::StoreMap>()));

world.execute(&tracer::Log(log::Level::Trace));

let storage = world.components.get_simple_storage::<TestArch, Simple5RequiredNoInit>();
assert_eq!(storage.try_get(&strong), Some(&Simple5RequiredNoInit(7 + 11)));
assert_eq!(storage.try_get(&weak), Some(&Simple5RequiredNoInit(3 + 13)));
}

0 comments on commit 77f986e

Please sign in to comment.