Add comprehensive test suite for game mechanics and level handling
- Introduced `test_final_level_flow.py` to validate final level transitions and game end scenarios. - Created `test_game_over_flow.py` to ensure game over conditions trigger correctly based on rat counts. - Implemented `test_keybindings.py` to verify keybinding configurations and their context-specific actions. - Developed `test_level_editor.py` to assess level editor functionalities and layout computations. - Added `test_level_io.py` for testing level data serialization and deserialization. - Established `test_loop_logic_parity.py` to ensure consistent game state across multiple simulation runs. - Created `test_non_regression.py` to simulate game behavior and capture states for future verification. - Implemented `test_verify.py` to compare current game states against a golden master for regression detection.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from engine import maze
|
||||
|
||||
|
||||
class LevelIoTests(unittest.TestCase):
|
||||
def test_dat_round_trip_preserves_all_levels(self):
|
||||
levels = []
|
||||
for level_index in range(maze.DEFAULT_LEVELS_PER_DAT_FILE):
|
||||
level = maze.create_level()
|
||||
level[1][1] = maze.MAP_EMPTY
|
||||
level[1][2] = maze.MAP_EMPTY
|
||||
level[2][1] = maze.MAP_TUNNEL
|
||||
level[2][2] = level_index % 3
|
||||
level[3][3] = (level_index + 1) % 3
|
||||
levels.append(level)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
dat_path = Path(tmp_dir) / "roundtrip.dat"
|
||||
maze.save_dat_levels(dat_path, levels)
|
||||
loaded = maze.load_dat_levels(dat_path)
|
||||
|
||||
self.assertEqual(levels, loaded)
|
||||
|
||||
def test_dat_level_count_is_derived_from_file_size(self):
|
||||
levels = [maze.create_level() for _ in range(3)]
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
dat_path = Path(tmp_dir) / "three-levels.dat"
|
||||
maze.save_dat_levels(dat_path, levels)
|
||||
|
||||
self.assertEqual(maze.get_dat_level_count(dat_path), 3)
|
||||
self.assertEqual(maze.get_level_count(dat_path), 3)
|
||||
self.assertEqual(maze.load_dat_level(dat_path, 4), levels[1])
|
||||
|
||||
def test_load_json_level_supports_legacy_boolean_maps(self):
|
||||
legacy_map = [
|
||||
[True, False, True],
|
||||
[False, False, True],
|
||||
]
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
json_path = Path(tmp_dir) / "legacy.json"
|
||||
json_path.write_text(json.dumps(legacy_map), encoding="utf-8")
|
||||
loaded = maze.load_json_level(json_path)
|
||||
|
||||
self.assertEqual(
|
||||
loaded,
|
||||
[
|
||||
[maze.MAP_WALL, maze.MAP_TUNNEL, maze.MAP_WALL],
|
||||
[maze.MAP_TUNNEL, maze.MAP_TUNNEL, maze.MAP_WALL],
|
||||
],
|
||||
)
|
||||
|
||||
def test_load_json_level_preserves_exact_tile_values(self):
|
||||
tile_map = [
|
||||
[maze.MAP_EMPTY, maze.MAP_WALL, maze.MAP_TUNNEL],
|
||||
[maze.MAP_TUNNEL, maze.MAP_EMPTY, maze.MAP_WALL],
|
||||
]
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
json_path = Path(tmp_dir) / "tiles.json"
|
||||
maze.save_json_level(json_path, tile_map)
|
||||
loaded = maze.load_json_level(json_path)
|
||||
|
||||
self.assertEqual(tile_map, loaded)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user