From 6d919a99d64fa6c911b21bf33e844396b8f5fb00 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 13 Feb 2024 18:54:58 +0000 Subject: [PATCH] fix: add errors for invalid cell creation - Raise CellInvalidError when attempting to specify the wrong corners of the cell when creating it. The x and y values should always represent the top left and bottom right corners of the cell. - Raise CellTooSmallError when attempting to specify a cell which is too small to correctly draw its central point. --- cell.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests.py | 1 - 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/cell.py b/cell.py index 60c2956..87a4cd2 100644 --- a/cell.py +++ b/cell.py @@ -23,6 +23,16 @@ class Cell: x2: int, y2: int, window: Window = None, ) -> None: + # Validation + if (x2 < x1) or (y2 < y1): + raise CellInvalidError(x1, y1, x2, y2) + + if (x2 - x1) < 2: + raise CellTooSmallError("horizontal", x2-x1) + + if (y2 - y1) < 2: + raise CellTooSmallError("vertical", y2-y1) + # Define the cell walls top_wall = Line(Point(x1, y1), Point(x2, y1)) bottom_wall = Line(Point(x1, y2), Point(x2, y2)) @@ -103,3 +113,38 @@ class Cell: fill_colour = "grey" line = Line(self.centre(), to_cell.centre()) self._window.draw_line(line, fill_colour) + + +class CellInvalidError(Exception): + """ + CellInvalidError is returned when the program tries to create a Cell whose + values are invalid. The values are invalid when x2 is smaller than x1 + and/or y2 is smaller than y1. When creating a Cell the x and y values + should always represent the top left and the bottom right of the cell's + walls (i.e. x1 < x2 and y1 < y2). + """ + + def __init__(self, x1: int, y1: int, x2: int, y2: int, *args): + super().__init__(args) + self.x1 = x1 + self.x2 = x2 + self.y1 = y1 + self.y2 = y2 + + def __str__(self): + return f"Invalid Cell values received. Please ensure that both: x1 ({self.x1}) < x2 ({self.x2}), and y1 ({self.y1}) < y2 ({self.y2})" + + +class CellTooSmallError(Exception): + """ + CellTooSmallError is returned when the program tries to create a Cell + which is too small to correctly draw it's central point. + """ + + def __init__(self, size_type: str, size: int, *args): + super().__init__(args) + self.size_type = size_type + self.size = size + + def __str__(self): + return f"The {self.size_type} size of the cell ({self.size}) is too small." diff --git a/tests.py b/tests.py index c908586..e5ddcce 100644 --- a/tests.py +++ b/tests.py @@ -55,7 +55,6 @@ class Tests(unittest.TestCase): 2, 2, ) - print(f"entrance: {maze._cells[0][0]._top_wall}") self.assertFalse(maze._cells[0][0]._top_wall.exists) self.assertFalse( maze._cells[number_of_cell_rows - 1][number_of_cells_per_row - 1]._bottom_wall.exists)