diff --git a/docs/source/api.rst b/docs/source/api.rst index 6b16e63..170e480 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -57,6 +57,8 @@ Exceptions .. autoexception:: table2ascii.exceptions.ColumnWidthTooSmallError +.. autoexception:: table2ascii.exceptions.InvalidColumnWidthError + .. autoexception:: table2ascii.exceptions.InvalidAlignmentError .. autoexception:: table2ascii.exceptions.TableStyleTooLongError diff --git a/table2ascii/__init__.py b/table2ascii/__init__.py index c70aad5..ebd9744 100644 --- a/table2ascii/__init__.py +++ b/table2ascii/__init__.py @@ -8,7 +8,7 @@ from .table_style import TableStyle from .table_to_ascii import table2ascii -__version__ = "1.0.1" +__version__ = "1.0.2" __all__ = [ "Alignment", diff --git a/table2ascii/exceptions.py b/table2ascii/exceptions.py index 86be92a..0c57134 100644 --- a/table2ascii/exceptions.py +++ b/table2ascii/exceptions.py @@ -156,7 +156,10 @@ def __init__(self, padding: int): super().__init__(self._message()) def _message(self) -> str: - return f"Invalid cell padding: {self.padding} is not a positive integer." + return ( + f"Invalid cell padding: The cell padding provided was {self.padding} " + f"but it must be a non-negative integer." + ) class ColumnWidthTooSmallError(TableOptionError): @@ -171,7 +174,7 @@ class ColumnWidthTooSmallError(TableOptionError): min_width (int): The minimum width that is allowed """ - def __init__(self, column_index: int, column_width: int, min_width: int): + def __init__(self, column_index: int, column_width: int, min_width: int | None = None): self.column_index = column_index self.column_width = column_width self.min_width = min_width @@ -179,12 +182,26 @@ def __init__(self, column_index: int, column_width: int, min_width: int): def _message(self) -> str: return ( - f"Column width too small: The column width for column index {self.column_index} " - f" of `column_widths` is {self.column_width}, but the minimum width " + f"Column width too small: The column width for index {self.column_index} " + f"of `column_widths` is {self.column_width}, but the minimum width " f"required to display the content is {self.min_width}." ) +class InvalidColumnWidthError(ColumnWidthTooSmallError): + """Exception raised when the column width is invalid + + This class is a subclass of :class:`ColumnWidthTooSmallError`. + """ + + def _message(self) -> str: + return ( + f"Invalid column width: The column width for index {self.column_index} " + f"of `column_widths` is {self.column_width}, but the column width " + f"must be a positive integer." + ) + + class InvalidAlignmentError(TableOptionError): """Exception raised when an invalid value is passed for an :class:`Alignment` diff --git a/table2ascii/table_to_ascii.py b/table2ascii/table_to_ascii.py index 1b3a523..8116467 100644 --- a/table2ascii/table_to_ascii.py +++ b/table2ascii/table_to_ascii.py @@ -16,6 +16,7 @@ FooterColumnCountMismatchError, InvalidAlignmentError, InvalidCellPaddingError, + InvalidColumnWidthError, NoHeaderBodyOrFooterError, ) from .merge import Merge @@ -150,6 +151,8 @@ def __calculate_column_widths( minimum = column_widths[i] if option is None: option = minimum + elif option < 0: + raise InvalidColumnWidthError(i, option) elif option < minimum: raise ColumnWidthTooSmallError(i, option, minimum) column_widths[i] = option diff --git a/tests/test_column_widths.py b/tests/test_column_widths.py index 5a3f1ba..55840eb 100644 --- a/tests/test_column_widths.py +++ b/tests/test_column_widths.py @@ -1,7 +1,11 @@ import pytest from table2ascii import table2ascii as t2a -from table2ascii.exceptions import ColumnWidthsCountMismatchError, ColumnWidthTooSmallError +from table2ascii.exceptions import ( + ColumnWidthsCountMismatchError, + ColumnWidthTooSmallError, + InvalidColumnWidthError, +) def test_column_widths(): @@ -83,7 +87,7 @@ def test_wrong_number_column_widths(): def test_negative_column_widths(): - with pytest.raises(ColumnWidthTooSmallError): + with pytest.raises(InvalidColumnWidthError): t2a( header=["#", "G", "H", "R", "S"], body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],