Skip to content

Commit

Permalink
Added clearer exception handling to BorderedTable and SimpleTable
Browse files Browse the repository at this point in the history
  • Loading branch information
kmvanbrunt committed Nov 24, 2021
1 parent 8e9a21c commit 3065233
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.3.3 (TBD, 2021)
* Enhancements
* Added clearer exception handling to `BorderedTable` and `SimpleTable`.

## 2.3.2 (November 22, 2021)
* Bug Fixes
* Fixed issue where a `ns_provider` could be passed `None` instead of its correct `cmd2.Cmd` or `CommandSet` value.
Expand Down
10 changes: 9 additions & 1 deletion cmd2/table_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def generate_row(
:param post_line: string to print after each line of a row. This can be used for padding after
the last cell's text and a right row border. (Defaults to blank)
:return: row string
:raises: ValueError if data isn't the same length as self.cols
:raises: ValueError if row_data isn't the same length as self.cols
:raises: TypeError if fill_char is more than one character (not including ANSI style sequences)
:raises: ValueError if fill_char, pre_line, inter_cell, or post_line contains an unprintable
character like a newline
Expand Down Expand Up @@ -682,7 +682,11 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
:param row_data: data with an entry for each column in the row
:return: data row string
:raises: ValueError if row_data isn't the same length as self.cols
"""
if len(row_data) != len(self.cols):
raise ValueError("Length of row_data must match length of cols")

fill_char = self.apply_data_bg(SPACE)
inter_cell = self.apply_data_bg(self.column_spacing * SPACE)

Expand Down Expand Up @@ -969,7 +973,11 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
:param row_data: data with an entry for each column in the row
:return: data row string
:raises: ValueError if row_data isn't the same length as self.cols
"""
if len(row_data) != len(self.cols):
raise ValueError("Length of row_data must match length of cols")

fill_char = self.apply_data_bg(SPACE)

pre_line = self.apply_border_color('║') + self.apply_data_bg(self.padding * SPACE)
Expand Down
24 changes: 23 additions & 1 deletion tests/test_table_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def test_generate_row_exceptions():
tc.generate_row(row_data=row_data, is_header=False, **kwargs)
assert "{} contains an unprintable character".format(arg) in str(excinfo.value)

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


def test_simple_generate_data_row_exceptions():
column_1 = Column("Col 1")
tc = SimpleTable([column_1])

# Data with too many columns
row_data = ['Data 1', 'Extra Column']
with pytest.raises(ValueError) as excinfo:
tc.generate_data_row(row_data=row_data)
assert "Length of row_data must match length of cols" in str(excinfo.value)


def test_bordered_table_creation():
column_1 = Column("Col 1", width=15)
column_2 = Column("Col 2", width=15)
Expand Down Expand Up @@ -635,6 +646,17 @@ def test_bordered_table_width():
assert bt.total_width() == 37


def test_bordered_generate_data_row_exceptions():
column_1 = Column("Col 1")
tc = BorderedTable([column_1])

# Data with too many columns
row_data = ['Data 1', 'Extra Column']
with pytest.raises(ValueError) as excinfo:
tc.generate_data_row(row_data=row_data)
assert "Length of row_data must match length of cols" in str(excinfo.value)


def test_alternating_table_creation():
column_1 = Column("Col 1", width=15)
column_2 = Column("Col 2", width=15)
Expand Down

0 comments on commit 3065233

Please sign in to comment.