Skip to content

Commit a9c79f9

Browse files
authored
feat: Add invalid column width error (#83)
1 parent 58c886a commit a9c79f9

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

docs/source/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Exceptions
5757

5858
.. autoexception:: table2ascii.exceptions.ColumnWidthTooSmallError
5959

60+
.. autoexception:: table2ascii.exceptions.InvalidColumnWidthError
61+
6062
.. autoexception:: table2ascii.exceptions.InvalidAlignmentError
6163

6264
.. autoexception:: table2ascii.exceptions.TableStyleTooLongError

table2ascii/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .table_style import TableStyle
99
from .table_to_ascii import table2ascii
1010

11-
__version__ = "1.0.1"
11+
__version__ = "1.0.2"
1212

1313
__all__ = [
1414
"Alignment",

table2ascii/exceptions.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ def __init__(self, padding: int):
156156
super().__init__(self._message())
157157

158158
def _message(self) -> str:
159-
return f"Invalid cell padding: {self.padding} is not a positive integer."
159+
return (
160+
f"Invalid cell padding: The cell padding provided was {self.padding} "
161+
f"but it must be a non-negative integer."
162+
)
160163

161164

162165
class ColumnWidthTooSmallError(TableOptionError):
@@ -171,20 +174,34 @@ class ColumnWidthTooSmallError(TableOptionError):
171174
min_width (int): The minimum width that is allowed
172175
"""
173176

174-
def __init__(self, column_index: int, column_width: int, min_width: int):
177+
def __init__(self, column_index: int, column_width: int, min_width: int | None = None):
175178
self.column_index = column_index
176179
self.column_width = column_width
177180
self.min_width = min_width
178181
super().__init__(self._message())
179182

180183
def _message(self) -> str:
181184
return (
182-
f"Column width too small: The column width for column index {self.column_index} "
183-
f" of `column_widths` is {self.column_width}, but the minimum width "
185+
f"Column width too small: The column width for index {self.column_index} "
186+
f"of `column_widths` is {self.column_width}, but the minimum width "
184187
f"required to display the content is {self.min_width}."
185188
)
186189

187190

191+
class InvalidColumnWidthError(ColumnWidthTooSmallError):
192+
"""Exception raised when the column width is invalid
193+
194+
This class is a subclass of :class:`ColumnWidthTooSmallError`.
195+
"""
196+
197+
def _message(self) -> str:
198+
return (
199+
f"Invalid column width: The column width for index {self.column_index} "
200+
f"of `column_widths` is {self.column_width}, but the column width "
201+
f"must be a positive integer."
202+
)
203+
204+
188205
class InvalidAlignmentError(TableOptionError):
189206
"""Exception raised when an invalid value is passed for an :class:`Alignment`
190207

table2ascii/table_to_ascii.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
FooterColumnCountMismatchError,
1717
InvalidAlignmentError,
1818
InvalidCellPaddingError,
19+
InvalidColumnWidthError,
1920
NoHeaderBodyOrFooterError,
2021
)
2122
from .merge import Merge
@@ -150,6 +151,8 @@ def __calculate_column_widths(
150151
minimum = column_widths[i]
151152
if option is None:
152153
option = minimum
154+
elif option < 0:
155+
raise InvalidColumnWidthError(i, option)
153156
elif option < minimum:
154157
raise ColumnWidthTooSmallError(i, option, minimum)
155158
column_widths[i] = option

tests/test_column_widths.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import pytest
22

33
from table2ascii import table2ascii as t2a
4-
from table2ascii.exceptions import ColumnWidthsCountMismatchError, ColumnWidthTooSmallError
4+
from table2ascii.exceptions import (
5+
ColumnWidthsCountMismatchError,
6+
ColumnWidthTooSmallError,
7+
InvalidColumnWidthError,
8+
)
59

610

711
def test_column_widths():
@@ -83,7 +87,7 @@ def test_wrong_number_column_widths():
8387

8488

8589
def test_negative_column_widths():
86-
with pytest.raises(ColumnWidthTooSmallError):
90+
with pytest.raises(InvalidColumnWidthError):
8791
t2a(
8892
header=["#", "G", "H", "R", "S"],
8993
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],

0 commit comments

Comments
 (0)