Skip to content

Commit 7f76e23

Browse files
committed
Added more validation to TableCreator
1 parent 2055dfc commit 7f76e23

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

cmd2/table_creator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ def __init__(self, cols: Sequence[Column], *, tab_width: int = 4) -> None:
132132
:param cols: column definitions for this table
133133
:param tab_width: all tabs will be replaced with this many spaces. If a row's fill_char is a tab,
134134
then it will be converted to one space.
135+
:raises: ValueError if tab_width is less than 1
135136
"""
137+
if tab_width < 1:
138+
raise ValueError("Tab width cannot be less than 1")
139+
136140
self.cols = copy.copy(cols)
137141
self.tab_width = tab_width
138142

tests/test_table_creator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ def test_tabs():
259259
inter_cell='\t', post_line='\t')
260260
assert row == ' Col 1 Col 2 '
261261

262+
with pytest.raises(ValueError) as excinfo:
263+
TableCreator([column_1, column_2], tab_width=0)
264+
assert "Tab width cannot be less than 1" in str(excinfo.value)
265+
262266

263267
def test_simple_table_creation():
264268
column_1 = Column("Col 1", width=16)

0 commit comments

Comments
 (0)