test: add tests for the Maze class
Add some tests to test the Maze constructor.
This commit is contained in:
parent
2ebb555bac
commit
70175b3afd
2 changed files with 42 additions and 13 deletions
26
maze.py
26
maze.py
|
@ -13,31 +13,31 @@ class Maze:
|
||||||
self,
|
self,
|
||||||
x_position: int,
|
x_position: int,
|
||||||
y_position: int,
|
y_position: int,
|
||||||
num_rows: int,
|
num_cell_rows: int,
|
||||||
num_columns: int,
|
num_cells_per_row: int,
|
||||||
cell_size_x: int,
|
cell_size_x: int,
|
||||||
cell_size_y: int,
|
cell_size_y: int,
|
||||||
window: Window = None,
|
window: Window = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self._x_position = x_position
|
self._x_position = x_position
|
||||||
self._y_position = y_position
|
self._y_position = y_position
|
||||||
self._num_rows = num_rows
|
self._num_cell_rows = num_cell_rows
|
||||||
self._num_columns = num_columns
|
self._num_cells_per_row = num_cells_per_row
|
||||||
self._cell_size_x = cell_size_x
|
self._cell_size_x = cell_size_x
|
||||||
self._cell_size_y = cell_size_y
|
self._cell_size_y = cell_size_y
|
||||||
self._window = window
|
self._window = window
|
||||||
|
|
||||||
# Create the Maze's cells
|
# Create the Maze's cells
|
||||||
self._cells: List[List[Cell]] = [None for i in range(self._num_rows)]
|
self._cells: List[List[Cell]] = [None for i in range(self._num_cell_rows)]
|
||||||
self._create_cells()
|
self._create_cells()
|
||||||
|
|
||||||
def _create_cells(self):
|
def _create_cells(self):
|
||||||
cursor_x = self._x_position
|
cursor_x = self._x_position
|
||||||
cursor_y = self._y_position
|
cursor_y = self._y_position
|
||||||
|
|
||||||
for i in range(self._num_rows):
|
for i in range(self._num_cell_rows):
|
||||||
column: List[Cell] = [None for j in range(self._num_columns)]
|
cells: List[Cell] = [None for j in range(self._num_cells_per_row)]
|
||||||
for j in range(self._num_columns):
|
for j in range(self._num_cells_per_row):
|
||||||
cell = Cell(
|
cell = Cell(
|
||||||
cursor_x,
|
cursor_x,
|
||||||
cursor_y,
|
cursor_y,
|
||||||
|
@ -45,20 +45,20 @@ class Maze:
|
||||||
(cursor_y + self._cell_size_y),
|
(cursor_y + self._cell_size_y),
|
||||||
self._window
|
self._window
|
||||||
)
|
)
|
||||||
column[j] = cell
|
cells[j] = cell
|
||||||
if j == self._num_columns - 1:
|
if j == self._num_cells_per_row - 1:
|
||||||
cursor_x = self._x_position
|
cursor_x = self._x_position
|
||||||
else:
|
else:
|
||||||
cursor_x += self._cell_size_x
|
cursor_x += self._cell_size_x
|
||||||
self._cells[i] = column
|
self._cells[i] = cells
|
||||||
cursor_y += self._cell_size_y
|
cursor_y += self._cell_size_y
|
||||||
|
|
||||||
if self._window:
|
if self._window:
|
||||||
self._draw_cells()
|
self._draw_cells()
|
||||||
|
|
||||||
def _draw_cells(self):
|
def _draw_cells(self):
|
||||||
for i in range(self._num_rows):
|
for i in range(self._num_cell_rows):
|
||||||
for j in range(self._num_columns):
|
for j in range(self._num_cells_per_row):
|
||||||
self._cells[i][j].draw()
|
self._cells[i][j].draw()
|
||||||
self._animate()
|
self._animate()
|
||||||
|
|
||||||
|
|
29
tests.py
Normal file
29
tests.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import unittest
|
||||||
|
from maze import Maze
|
||||||
|
|
||||||
|
|
||||||
|
class Tests(unittest.TestCase):
|
||||||
|
def test_maze_create_cell_grid(self):
|
||||||
|
cases = [
|
||||||
|
{
|
||||||
|
"number_of_cell_rows": 12,
|
||||||
|
"number_of_cells_per_row": 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"number_of_cell_rows": 50,
|
||||||
|
"number_of_cells_per_row": 120,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"number_of_cell_rows": 4,
|
||||||
|
"number_of_cells_per_row": 4,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
for case in cases:
|
||||||
|
maze = Maze(0, 0, case["number_of_cell_rows"], case["number_of_cells_per_row"], 2, 2)
|
||||||
|
self.assertEqual(len(maze._cells), case["number_of_cell_rows"])
|
||||||
|
self.assertEqual(len(maze._cells[0]), case["number_of_cells_per_row"])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
Loading…
Reference in a new issue