Skip to content

Commit 3065233

Browse files
committed
Added clearer exception handling to BorderedTable and SimpleTable
1 parent 8e9a21c commit 3065233

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.3.3 (TBD, 2021)
2+
* Enhancements
3+
* Added clearer exception handling to `BorderedTable` and `SimpleTable`.
4+
15
## 2.3.2 (November 22, 2021)
26
* Bug Fixes
37
* Fixed issue where a `ns_provider` could be passed `None` instead of its correct `cmd2.Cmd` or `CommandSet` value.

cmd2/table_creator.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def generate_row(
440440
:param post_line: string to print after each line of a row. This can be used for padding after
441441
the last cell's text and a right row border. (Defaults to blank)
442442
:return: row string
443-
:raises: ValueError if data isn't the same length as self.cols
443+
:raises: ValueError if row_data isn't the same length as self.cols
444444
:raises: TypeError if fill_char is more than one character (not including ANSI style sequences)
445445
:raises: ValueError if fill_char, pre_line, inter_cell, or post_line contains an unprintable
446446
character like a newline
@@ -682,7 +682,11 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
682682
683683
:param row_data: data with an entry for each column in the row
684684
:return: data row string
685+
:raises: ValueError if row_data isn't the same length as self.cols
685686
"""
687+
if len(row_data) != len(self.cols):
688+
raise ValueError("Length of row_data must match length of cols")
689+
686690
fill_char = self.apply_data_bg(SPACE)
687691
inter_cell = self.apply_data_bg(self.column_spacing * SPACE)
688692

@@ -969,7 +973,11 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
969973
970974
:param row_data: data with an entry for each column in the row
971975
:return: data row string
976+
:raises: ValueError if row_data isn't the same length as self.cols
972977
"""
978+
if len(row_data) != len(self.cols):
979+
raise ValueError("Length of row_data must match length of cols")
980+
973981
fill_char = self.apply_data_bg(SPACE)
974982

975983
pre_line = self.apply_border_color('║') + self.apply_data_bg(self.padding * SPACE)

tests/test_table_creator.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def test_generate_row_exceptions():
316316
tc.generate_row(row_data=row_data, is_header=False, **kwargs)
317317
assert "{} contains an unprintable character".format(arg) in str(excinfo.value)
318318

319-
# data with too many columns
319+
# Data with too many columns
320320
row_data = ['Data 1', 'Extra Column']
321321
with pytest.raises(ValueError) as excinfo:
322322
tc.generate_row(row_data=row_data, is_header=False)
@@ -504,6 +504,17 @@ def test_simple_table_width():
504504
assert st.total_width() == 34
505505

506506

507+
def test_simple_generate_data_row_exceptions():
508+
column_1 = Column("Col 1")
509+
tc = SimpleTable([column_1])
510+
511+
# Data with too many columns
512+
row_data = ['Data 1', 'Extra Column']
513+
with pytest.raises(ValueError) as excinfo:
514+
tc.generate_data_row(row_data=row_data)
515+
assert "Length of row_data must match length of cols" in str(excinfo.value)
516+
517+
507518
def test_bordered_table_creation():
508519
column_1 = Column("Col 1", width=15)
509520
column_2 = Column("Col 2", width=15)
@@ -635,6 +646,17 @@ def test_bordered_table_width():
635646
assert bt.total_width() == 37
636647

637648

649+
def test_bordered_generate_data_row_exceptions():
650+
column_1 = Column("Col 1")
651+
tc = BorderedTable([column_1])
652+
653+
# Data with too many columns
654+
row_data = ['Data 1', 'Extra Column']
655+
with pytest.raises(ValueError) as excinfo:
656+
tc.generate_data_row(row_data=row_data)
657+
assert "Length of row_data must match length of cols" in str(excinfo.value)
658+
659+
638660
def test_alternating_table_creation():
639661
column_1 = Column("Col 1", width=15)
640662
column_2 = Column("Col 2", width=15)

0 commit comments

Comments
 (0)