75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
#!/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() |