Files
kiln/kiln-core/src/tests/mod.rs
T

69 lines
2.0 KiB
Rust
Raw Normal View History

2026-06-07 00:46:38 -05:00
mod actions;
mod collision;
2026-06-13 16:24:29 -05:00
mod game_portals;
2026-06-07 00:46:38 -05:00
mod map_file;
mod movement;
mod scripting;
use crate::archetype::Archetype;
use crate::board::Board;
2026-07-10 23:16:28 -05:00
use crate::floor::Floor;
2026-06-15 23:35:18 -05:00
use crate::game::GameState;
use crate::glyph::Glyph;
2026-06-07 00:46:38 -05:00
use crate::object_def::ObjectDef;
2026-06-25 00:04:42 -05:00
use crate::utils::PlayerPos;
2026-06-15 23:35:18 -05:00
use std::collections::{BTreeMap, HashMap};
2026-06-07 00:46:38 -05:00
2026-06-13 01:25:58 -05:00
/// Builds a 1×1 board with a single object that optionally references a script,
/// and returns both the board and the `(name, source)` entries as a script map.
///
/// Pass the returned scripts to [`GameState::with_scripts`] so they are compiled.
2026-06-15 23:35:18 -05:00
fn board_with_object(
object_script: Option<&str>,
scripts: &[(&str, &str)],
) -> (Board, HashMap<String, String>) {
2026-06-07 00:46:38 -05:00
let mut object = ObjectDef::new(0, 0);
2026-06-07 01:26:18 -05:00
object.id = 1;
2026-06-07 00:46:38 -05:00
object.script_name = object_script.map(str::to_string);
2026-06-13 01:25:58 -05:00
let board = Board {
2026-06-07 00:46:38 -05:00
name: "test".into(),
width: 1,
height: 1,
2026-07-10 23:16:28 -05:00
grid: vec![(Glyph::transparent(), Archetype::Empty)],
floor: Floor::Blank,
decorations: Vec::new(),
2026-06-25 00:04:42 -05:00
player: PlayerPos { x: 0, y: 0 },
2026-06-07 00:46:38 -05:00
objects: BTreeMap::from([(1, object)]),
next_object_id: 2,
portals: Vec::new(),
board_script_name: None,
load_errors: Vec::new(),
2026-06-13 17:58:04 -05:00
registry: HashMap::new(),
2026-06-13 01:25:58 -05:00
};
(board, scripts_from(scripts))
}
/// Converts a `(name, source)` slice into a `HashMap` suitable for
/// [`GameState::with_scripts`].
fn scripts_from(pairs: &[(&str, &str)]) -> HashMap<String, String> {
2026-06-15 23:35:18 -05:00
pairs
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect()
2026-06-07 00:46:38 -05:00
}
/// Returns an `ObjectDef` at `(x, y)` bound to the named script.
fn scripted_object(x: usize, y: usize, script: &str) -> ObjectDef {
let mut o = ObjectDef::new(x, y);
o.script_name = Some(script.to_string());
o
}
/// Flattens each log line into a single string for easy assertions.
fn log_texts(game: &GameState) -> Vec<String> {
game.log
.iter()
.map(|line| line.spans.iter().map(|s| s.text.as_str()).collect())
.collect()
}