Skip to content

feat: Add invalid column width error #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Exceptions

.. autoexception:: table2ascii.exceptions.ColumnWidthTooSmallError

.. autoexception:: table2ascii.exceptions.InvalidColumnWidthError

.. autoexception:: table2ascii.exceptions.InvalidAlignmentError

.. autoexception:: table2ascii.exceptions.TableStyleTooLongError
Expand Down
2 changes: 1 addition & 1 deletion table2ascii/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 21 additions & 4 deletions table2ascii/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -171,20 +174,34 @@ 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
super().__init__(self._message())

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`

Expand Down
3 changes: 3 additions & 0 deletions table2ascii/table_to_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
FooterColumnCountMismatchError,
InvalidAlignmentError,
InvalidCellPaddingError,
InvalidColumnWidthError,
NoHeaderBodyOrFooterError,
)
from .merge import Merge
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions tests/test_column_widths.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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"]],
Expand Down