174 lines
4.9 KiB
Python
174 lines
4.9 KiB
Python
"""Tests for core Maze and Cell classes."""
|
|
|
|
import pytest
|
|
from src.core.maze import Maze
|
|
from src.core.cell import Cell
|
|
|
|
|
|
class TestCell:
|
|
"""Test Cell class functionality."""
|
|
|
|
def test_cell_initialization(self):
|
|
"""Test cell is initialized with all walls."""
|
|
cell = Cell(0, 0)
|
|
assert cell.row == 0
|
|
assert cell.col == 0
|
|
assert all(cell.walls.values())
|
|
assert not cell.visited
|
|
|
|
def test_remove_wall(self):
|
|
"""Test removing walls from a cell."""
|
|
cell = Cell(0, 0)
|
|
cell.remove_wall('north')
|
|
assert not cell.has_wall('north')
|
|
assert cell.has_wall('south')
|
|
|
|
def test_cell_reset(self):
|
|
"""Test resetting a cell."""
|
|
cell = Cell(0, 0)
|
|
cell.remove_wall('north')
|
|
cell.visited = True
|
|
cell.reset()
|
|
assert cell.has_wall('north')
|
|
assert not cell.visited
|
|
|
|
def test_cell_serialization(self):
|
|
"""Test cell to_dict and from_dict."""
|
|
cell = Cell(2, 3)
|
|
cell.remove_wall('east')
|
|
cell.visited = True
|
|
|
|
data = cell.to_dict()
|
|
restored = Cell.from_dict(data)
|
|
|
|
assert restored.row == cell.row
|
|
assert restored.col == cell.col
|
|
assert restored.walls == cell.walls
|
|
assert restored.visited == cell.visited
|
|
|
|
def test_cell_equality(self):
|
|
"""Test cell equality."""
|
|
cell1 = Cell(0, 0)
|
|
cell2 = Cell(0, 0)
|
|
cell3 = Cell(1, 1)
|
|
|
|
assert cell1 == cell2
|
|
assert cell1 != cell3
|
|
|
|
|
|
class TestMaze:
|
|
"""Test Maze class functionality."""
|
|
|
|
def test_maze_initialization(self):
|
|
"""Test maze is initialized correctly."""
|
|
maze = Maze(10, 10, seed=42)
|
|
assert maze.rows == 10
|
|
assert maze.cols == 10
|
|
assert maze.seed == 42
|
|
assert len(maze.grid) == 10
|
|
assert len(maze.grid[0]) == 10
|
|
|
|
def test_maze_dimensions_validation(self):
|
|
"""Test maze dimension validation."""
|
|
with pytest.raises(ValueError):
|
|
Maze(3, 10) # Too small
|
|
with pytest.raises(ValueError):
|
|
Maze(10, 60) # Too large
|
|
|
|
def test_get_cell(self):
|
|
"""Test getting cells from maze."""
|
|
maze = Maze(10, 10)
|
|
cell = maze.get_cell(5, 5)
|
|
assert cell is not None
|
|
assert cell.row == 5
|
|
assert cell.col == 5
|
|
|
|
# Out of bounds
|
|
assert maze.get_cell(-1, 0) is None
|
|
assert maze.get_cell(0, 100) is None
|
|
|
|
def test_get_neighbors(self):
|
|
"""Test getting neighbors of a cell."""
|
|
maze = Maze(10, 10)
|
|
|
|
# Corner cell
|
|
cell = maze.get_cell(0, 0)
|
|
neighbors = maze.get_neighbors(cell)
|
|
assert len(neighbors) == 2 # Only south and east
|
|
|
|
# Middle cell
|
|
cell = maze.get_cell(5, 5)
|
|
neighbors = maze.get_neighbors(cell)
|
|
assert len(neighbors) == 4 # All directions
|
|
|
|
def test_remove_wall_between(self):
|
|
"""Test removing walls between cells."""
|
|
maze = Maze(10, 10)
|
|
cell1 = maze.get_cell(0, 0)
|
|
cell2 = maze.get_cell(0, 1)
|
|
|
|
maze.remove_wall_between(cell1, cell2)
|
|
|
|
assert not cell1.has_wall('east')
|
|
assert not cell2.has_wall('west')
|
|
|
|
def test_reset_visited(self):
|
|
"""Test resetting visited flags."""
|
|
maze = Maze(10, 10)
|
|
|
|
# Mark some cells as visited
|
|
for row in maze.grid[:5]:
|
|
for cell in row:
|
|
cell.visited = True
|
|
|
|
maze.reset_visited()
|
|
|
|
# Check all cells are unvisited
|
|
for row in maze.grid:
|
|
for cell in row:
|
|
assert not cell.visited
|
|
|
|
def test_maze_serialization(self):
|
|
"""Test maze to_dict and from_dict."""
|
|
maze = Maze(5, 5, seed=42)
|
|
maze.algorithm_used = "Test Algorithm"
|
|
maze.generation_time_ms = 10.5
|
|
|
|
# Modify some walls
|
|
cell1 = maze.get_cell(0, 0)
|
|
cell2 = maze.get_cell(0, 1)
|
|
maze.remove_wall_between(cell1, cell2)
|
|
|
|
# Serialize and deserialize
|
|
data = maze.to_dict()
|
|
restored = Maze.from_dict(data)
|
|
|
|
assert restored.rows == maze.rows
|
|
assert restored.cols == maze.cols
|
|
assert restored.seed == maze.seed
|
|
assert restored.algorithm_used == maze.algorithm_used
|
|
|
|
# Check walls are preserved
|
|
restored_cell1 = restored.get_cell(0, 0)
|
|
assert not restored_cell1.has_wall('east')
|
|
|
|
def test_maze_json(self):
|
|
"""Test JSON serialization."""
|
|
maze = Maze(5, 5, seed=42)
|
|
json_str = maze.to_json()
|
|
|
|
restored = Maze.from_json(json_str)
|
|
|
|
assert restored.rows == maze.rows
|
|
assert restored.cols == maze.cols
|
|
assert restored.seed == maze.seed
|
|
|
|
def test_is_valid_position(self):
|
|
"""Test position validation."""
|
|
maze = Maze(10, 10)
|
|
|
|
assert maze.is_valid_position(0, 0)
|
|
assert maze.is_valid_position(9, 9)
|
|
assert not maze.is_valid_position(-1, 0)
|
|
assert not maze.is_valid_position(0, 10)
|