Skip to content

Commit

Permalink
fix: reorganize ECS tests (#470)
Browse files Browse the repository at this point in the history
Follow-up on #468.

## Changes

*This PR makes changes exclusively within `*::tests` modules, there are
no changes to lib code.*

- Move tests out of the `entities` module that don't belong
- Add more edge cases to help capture the similar but very different
logic for all of the ways you can iterate component stores & bitsets
  • Loading branch information
nelson137 authored Sep 24, 2024
1 parent e6cc5da commit e2a58ae
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 360 deletions.
65 changes: 64 additions & 1 deletion framework_crates/bones_ecs/src/components/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ impl<'a> Iterator for UntypedComponentBitsetIteratorMut<'a> {
}

#[cfg(test)]
mod test {
mod tests {
#![allow(non_snake_case)]

use super::*;

#[derive(Clone, HasSchema, Default)]
Expand Down Expand Up @@ -337,4 +339,65 @@ mod test {
assert_eq!(count, 2);
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, HasSchema, Default)]
struct X(u32);

#[derive(Debug, Clone, Copy, PartialEq, Eq, HasSchema, Default)]
struct Y(u32);

fn entity(index: u32) -> Entity {
Entity::new(index, 0)
}

fn store<C: HasSchema>(entities: &[u32], ctor: fn(u32) -> C) -> ComponentStore<C> {
let mut store = ComponentStore::default();
for &i in entities {
store.insert(entity(i), ctor(i));
}
store
}

fn bitset(enabled: &[usize]) -> BitSetVec {
let mut bitset = BitSetVec::default();
for &i in enabled {
bitset.bit_set(i);
}
bitset
}

#[test]
fn get_single_with_bitset__multiple_required() {
{
let bitset = bitset(&[]);
let (store_x, store_y) = (store(&[], X), store(&[], Y));
let query = (&Ref::new(&store_x), &Ref::new(&store_y));

let result = query.get_single_with_bitset(Rc::new(bitset));

assert_eq!(result, Err(QuerySingleError::NoEntities));
}

{
let bitset = bitset(&[1]);
let store_x = store(&[1], X);
let store_y = store(&[1], Y);
let query = (&Ref::new(&store_x), &Ref::new(&store_y));

let result = query.get_single_with_bitset(Rc::new(bitset));

assert_eq!(result, Ok((&X(1), &Y(1))));
}

{
let bitset = bitset(&[1, 2]);
let store_x = store(&[1, 2], X);
let store_y = store(&[1, 2], Y);
let query = (&Ref::new(&store_x), &Ref::new(&store_y));

let result = query.get_single_with_bitset(Rc::new(bitset));

assert_eq!(result, Err(QuerySingleError::MultipleEntities));
}
}
}
Loading

0 comments on commit e2a58ae

Please sign in to comment.