move tests
This commit is contained in:
@@ -308,4 +308,179 @@ impl Board {
|
||||
self.objects.insert(id, object);
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use color::Rgba8;
|
||||
use crate::archetype::Archetype;
|
||||
use crate::glyph::Glyph;
|
||||
use crate::object_def::ObjectDef;
|
||||
use crate::script::Direction;
|
||||
use crate::utils::{ObjectId, Player, Solid};
|
||||
use super::Board;
|
||||
|
||||
/// Builds an all-empty `w×h` board with the given player position, objects,
|
||||
/// and `(name, source)` scripts. Assigns sequential ids (1..=n) to objects.
|
||||
pub(crate) fn open_board(
|
||||
w: usize,
|
||||
h: usize,
|
||||
player: (i32, i32),
|
||||
objects: Vec<ObjectDef>,
|
||||
scripts: &[(&str, &str)],
|
||||
) -> Board {
|
||||
let mut object_map: BTreeMap<ObjectId, ObjectDef> = BTreeMap::new();
|
||||
let mut next_object_id: ObjectId = 1;
|
||||
for o in objects {
|
||||
object_map.insert(next_object_id, o);
|
||||
next_object_id += 1;
|
||||
}
|
||||
Board {
|
||||
name: "test".into(),
|
||||
width: w,
|
||||
height: h,
|
||||
cells: vec![(Archetype::Empty.default_glyph(), Archetype::Empty); w * h],
|
||||
floor: vec![Archetype::Empty.default_glyph(); w * h],
|
||||
floor_spec: None,
|
||||
player: Player { x: player.0, y: player.1 },
|
||||
objects: object_map,
|
||||
next_object_id,
|
||||
portals: Vec::new(),
|
||||
font: None,
|
||||
zoom: 1,
|
||||
scripts: scripts.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect::<HashMap<_,_>>(),
|
||||
board_script_name: None,
|
||||
load_errors: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Stamps a crate cell onto a board.
|
||||
pub(crate) fn crate_at(board: &mut Board, x: usize, y: usize) {
|
||||
*board.get_mut(x, y) = (Archetype::Crate.default_glyph(), Archetype::Crate);
|
||||
}
|
||||
|
||||
/// Stamps a wall cell onto a board.
|
||||
pub(crate) fn wall_at(board: &mut Board, x: usize, y: usize) {
|
||||
*board.get_mut(x, y) = (Archetype::Wall.default_glyph(), Archetype::Wall);
|
||||
}
|
||||
|
||||
/// Stamps an arbitrary archetype cell onto a board.
|
||||
pub(crate) fn stamp(board: &mut Board, x: usize, y: usize, arch: Archetype) {
|
||||
*board.get_mut(x, y) = (arch.default_glyph(), arch);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solid_at_reports_wall_object_and_empty() {
|
||||
let mut board = open_board(4, 1, (3, 0), vec![], &[]);
|
||||
board.cells[1] = (Archetype::Wall.default_glyph(), Archetype::Wall);
|
||||
board.add_object(ObjectDef::new(2, 0));
|
||||
|
||||
assert!(board.solid_at(0, 0).is_none());
|
||||
assert!(board.is_passable(0, 0));
|
||||
|
||||
match board.solid_at(1, 0) {
|
||||
Some(Solid::Cell(Archetype::Wall)) => {}
|
||||
other => panic!("expected Solid::Cell(Wall), got {:?}", other.is_some()),
|
||||
}
|
||||
assert!(!board.is_passable(1, 0));
|
||||
|
||||
match board.solid_at(2, 0) {
|
||||
Some(Solid::Object(id)) => assert_eq!((board.objects[&id].x, board.objects[&id].y), (2, 0)),
|
||||
_ => panic!("expected Solid::Object"),
|
||||
}
|
||||
assert!(!board.is_passable(2, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_solid_object_does_not_block() {
|
||||
let mut obj = ObjectDef::new(1, 0);
|
||||
obj.solid = false;
|
||||
let board = open_board(3, 1, (0, 0), vec![obj], &[]);
|
||||
assert!(board.solid_at(1, 0).is_none());
|
||||
assert!(board.is_passable(1, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solid_at_reports_player() {
|
||||
let board = open_board(3, 1, (1, 0), vec![], &[]);
|
||||
match board.solid_at(1, 0) {
|
||||
Some(Solid::Player) => {}
|
||||
_ => panic!("expected Solid::Player at the player's cell"),
|
||||
}
|
||||
assert!(!board.is_passable(1, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn in_bounds_checks_grid_boundaries() {
|
||||
let board = open_board(3, 2, (0, 0), vec![], &[]);
|
||||
assert!(board.in_bounds((0, 0)));
|
||||
assert!(board.in_bounds((2, 1)));
|
||||
assert!(!board.in_bounds((-1, 0)));
|
||||
assert!(!board.in_bounds((0, -1)));
|
||||
assert!(!board.in_bounds((3, 0)));
|
||||
assert!(!board.in_bounds((0, 2)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_push_is_read_only_and_correct() {
|
||||
let mut board = open_board(3, 1, (0, 0), vec![], &[]);
|
||||
crate_at(&mut board, 1, 0);
|
||||
assert!(board.can_push(1, 0, Direction::East));
|
||||
assert_eq!(board.get(1, 0).1, Archetype::Crate); // no mutation
|
||||
assert_eq!(board.get(2, 0).1, Archetype::Empty);
|
||||
|
||||
let mut board = open_board(3, 1, (0, 0), vec![], &[]);
|
||||
crate_at(&mut board, 1, 0);
|
||||
wall_at(&mut board, 2, 0);
|
||||
assert!(!board.can_push(1, 0, Direction::East));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn push_into_player_pushes_player() {
|
||||
// Crate shoved east into the player slides the player along into open space.
|
||||
let mut board = open_board(4, 1, (2, 0), vec![], &[]);
|
||||
crate_at(&mut board, 1, 0);
|
||||
assert!(board.can_push(1, 0, Direction::East));
|
||||
board.push(1, 0, Direction::East);
|
||||
assert_eq!(board.get(1, 0).1, Archetype::Empty);
|
||||
assert_eq!(board.get(2, 0).1, Archetype::Crate);
|
||||
assert_eq!((board.player.x, board.player.y), (3, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn push_into_player_blocked_by_wall() {
|
||||
// Player backed against a wall: push has nowhere to go, nothing moves.
|
||||
let mut board = open_board(4, 1, (2, 0), vec![], &[]);
|
||||
crate_at(&mut board, 1, 0);
|
||||
wall_at(&mut board, 3, 0);
|
||||
assert!(!board.can_push(1, 0, Direction::East));
|
||||
board.push(1, 0, Direction::East); // no-op
|
||||
assert_eq!(board.get(1, 0).1, Archetype::Crate);
|
||||
assert_eq!((board.player.x, board.player.y), (2, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glyph_at_uses_floor_for_empty_and_grid_for_solid() {
|
||||
// Player parked at (2,0) so it doesn't overlap either asserted cell.
|
||||
let mut board = open_board(3, 1, (2, 0), vec![], &[]);
|
||||
let floor_glyph = Glyph {
|
||||
tile: '.' as u32,
|
||||
fg: Rgba8 { r: 10, g: 20, b: 30, a: 255 },
|
||||
bg: Rgba8 { r: 1, g: 2, b: 3, a: 255 },
|
||||
};
|
||||
board.floor = vec![floor_glyph; 3];
|
||||
wall_at(&mut board, 0, 0);
|
||||
assert_eq!(board.glyph_at(0, 0), Archetype::Wall.default_glyph());
|
||||
assert_eq!(board.glyph_at(1, 0), floor_glyph);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fresh_board_is_valid_and_reports_errors() {
|
||||
let mut board = open_board(1, 1, (0, 0), vec![], &[]);
|
||||
assert!(board.is_valid());
|
||||
board.report_error("something went wrong");
|
||||
assert!(!board.is_valid());
|
||||
assert_eq!(board.load_errors.len(), 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user