Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Jan 8, 2025
1 parent 35e3d0d commit 1b67f1d
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions crates/bevy_input_focus/src/directional_navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,93 @@ mod tests {
world.run_system_once(navigate_east).unwrap();
assert_eq!(world.resource::<InputFocus>().get(), Some(a));
}

#[test]
fn non_looping_grid_edges() {
let mut world = World::new();
let a = world.spawn_empty().id();
let b = world.spawn_empty().id();
let c = world.spawn_empty().id();
let d = world.spawn_empty().id();

let mut nav_map = DirectionalNavigationMap::default();
// a b
// c d
let mut grid = HashMap::default();
grid.insert(IVec2::new(0, 0), a);
grid.insert(IVec2::new(1, 0), b);
grid.insert(IVec2::new(0, 1), c);
grid.insert(IVec2::new(1, 1), d);

nav_map.add_grid(grid, false);

// Cardinal directions
assert_eq!(nav_map.get_neighbor(a, CompassOctant::East), Some(b));
assert_eq!(nav_map.get_neighbor(b, CompassOctant::West), Some(a));
assert_eq!(nav_map.get_neighbor(a, CompassOctant::South), Some(c));
assert_eq!(nav_map.get_neighbor(c, CompassOctant::North), Some(a));
assert_eq!(nav_map.get_neighbor(b, CompassOctant::South), Some(d));
assert_eq!(nav_map.get_neighbor(d, CompassOctant::North), Some(b));
assert_eq!(nav_map.get_neighbor(c, CompassOctant::East), Some(d));
assert_eq!(nav_map.get_neighbor(d, CompassOctant::West), Some(c));

// Diagonal directions
assert_eq!(nav_map.get_neighbor(a, CompassOctant::SouthEast), Some(d));
assert_eq!(nav_map.get_neighbor(d, CompassOctant::NorthWest), Some(a));
assert_eq!(nav_map.get_neighbor(b, CompassOctant::SouthWest), Some(c));
assert_eq!(nav_map.get_neighbor(c, CompassOctant::NorthEast), Some(b));

// Out of bounds
assert_eq!(nav_map.get_neighbor(a, CompassOctant::North), None);
assert_eq!(nav_map.get_neighbor(a, CompassOctant::West), None);
assert_eq!(nav_map.get_neighbor(a, CompassOctant::NorthEast), None);
assert_eq!(nav_map.get_neighbor(a, CompassOctant::NorthWest), None);
assert_eq!(nav_map.get_neighbor(a, CompassOctant::SouthWest), None);
assert_eq!(nav_map.get_neighbor(d, CompassOctant::East), None);
assert_eq!(nav_map.get_neighbor(d, CompassOctant::South), None);
}

#[test]
fn looping_grid_edges() {
let mut world = World::new();
let a = world.spawn_empty().id();
let b = world.spawn_empty().id();
let c = world.spawn_empty().id();
let d = world.spawn_empty().id();

let mut nav_map = DirectionalNavigationMap::default();
// a b
// c d
let mut grid = HashMap::default();
grid.insert(IVec2::new(0, 0), a);
grid.insert(IVec2::new(1, 0), b);
grid.insert(IVec2::new(0, 1), c);
grid.insert(IVec2::new(1, 1), d);

nav_map.add_grid(grid, false);

// Cardinal directions
assert_eq!(nav_map.get_neighbor(a, CompassOctant::East), Some(b));
assert_eq!(nav_map.get_neighbor(b, CompassOctant::West), Some(a));
assert_eq!(nav_map.get_neighbor(a, CompassOctant::South), Some(c));
assert_eq!(nav_map.get_neighbor(c, CompassOctant::North), Some(a));
assert_eq!(nav_map.get_neighbor(b, CompassOctant::South), Some(d));
assert_eq!(nav_map.get_neighbor(d, CompassOctant::North), Some(b));
assert_eq!(nav_map.get_neighbor(c, CompassOctant::East), Some(d));
assert_eq!(nav_map.get_neighbor(d, CompassOctant::West), Some(c));

// Diagonal directions
assert_eq!(nav_map.get_neighbor(a, CompassOctant::SouthEast), Some(d));
assert_eq!(nav_map.get_neighbor(d, CompassOctant::NorthWest), Some(a));
assert_eq!(nav_map.get_neighbor(b, CompassOctant::SouthWest), Some(c));
assert_eq!(nav_map.get_neighbor(c, CompassOctant::NorthEast), Some(b));

// Out of bounds
assert_eq!(nav_map.get_neighbor(a, CompassOctant::North), Some(c));
assert_eq!(nav_map.get_neighbor(a, CompassOctant::West), Some(b));
assert_eq!(nav_map.get_neighbor(a, CompassOctant::NorthWest), Some(d));
assert_eq!(nav_map.get_neighbor(a, CompassOctant::SouthWest), None);
assert_eq!(nav_map.get_neighbor(d, CompassOctant::East), Some(c));
assert_eq!(nav_map.get_neighbor(d, CompassOctant::South), Some(b));
}
}

0 comments on commit 1b67f1d

Please sign in to comment.