Skip to content

Commit 6873717

Browse files
committed
Added unit tests.
1 parent d0df69a commit 6873717

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

cmd2/cmd2.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4133,15 +4133,18 @@ def print_topics(self, header: str, cmds: list[str] | None, cmdlen: int, maxcol:
41334133
if not cmds:
41344134
return
41354135

4136-
# Add a row that looks like a table header.
4137-
header_grid = Table.grid()
4138-
header_grid.add_row(header, style=Cmd2Style.HELP_HEADER)
4139-
header_grid.add_row(Rule(characters=self.ruler, style=Cmd2Style.TABLE_BORDER))
4140-
self.poutput(ru.indent(header_grid, 1))
4136+
# Print a row that looks like a table header.
4137+
if header:
4138+
header_grid = Table.grid()
4139+
header_grid.add_row(header, style=Cmd2Style.HELP_HEADER)
4140+
header_grid.add_row(Rule(characters=self.ruler, style=Cmd2Style.TABLE_BORDER))
4141+
self.poutput(ru.indent(header_grid, 1))
4142+
4143+
# Subtract 2 from the max column width to account for the
4144+
# one-space indentation and a one-space right margin.
4145+
maxcol = min(maxcol, ru.console_width()) - 2
41414146

41424147
# Print the topics in columns.
4143-
# Subtract 1 from maxcol to account for indentation.
4144-
maxcol = min(maxcol, ru.console_width()) - 1
41454148
columnized_cmds = self.render_columns(cmds, maxcol)
41464149
self.poutput(ru.indent(columnized_cmds, 1))
41474150
self.poutput()
@@ -5523,7 +5526,7 @@ class TestMyAppCase(Cmd2TestCase):
55235526
num_transcripts = len(transcripts_expanded)
55245527
plural = '' if len(transcripts_expanded) == 1 else 's'
55255528
self.poutput(
5526-
Rule("cmd2 transcript test", style=Style.null()),
5529+
Rule("cmd2 transcript test", characters=self.ruler, style=Style.null()),
55275530
style=Style(bold=True),
55285531
)
55295532
self.poutput(f'platform {sys.platform} -- Python {verinfo}, cmd2-{cmd2.__version__}, readline-{rl_type}')
@@ -5542,7 +5545,7 @@ class TestMyAppCase(Cmd2TestCase):
55425545
if test_results.wasSuccessful():
55435546
self.perror(stream.read(), end="", style=None)
55445547
finish_msg = f'{num_transcripts} transcript{plural} passed in {execution_time:.3f} seconds'
5545-
self.psuccess(Rule(finish_msg, style=Style.null()))
5548+
self.psuccess(Rule(finish_msg, characters=self.ruler, style=Style.null()))
55465549
else:
55475550
# Strip off the initial traceback which isn't particularly useful for end users
55485551
error_str = stream.read()

tests/test_rich_utils.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Unit testing for cmd2/rich_utils.py module"""
22

33
import pytest
4+
import rich.box
45
from rich.style import Style
6+
from rich.table import Table
57
from rich.text import Text
68

79
from cmd2 import (
@@ -59,6 +61,45 @@ def test_string_to_rich_text() -> None:
5961
assert ru.string_to_rich_text(input_string).plain == input_string
6062

6163

64+
def test_indented_text() -> None:
65+
console = ru.Cmd2GeneralConsole()
66+
67+
# With an indention of 10, text will be evenly split across two lines.
68+
console.width = 20
69+
text = "A" * 20
70+
level = 10
71+
indented_text = ru.indent(text, level)
72+
73+
with console.capture() as capture:
74+
console.print(indented_text)
75+
result = capture.get().splitlines()
76+
77+
padding = " " * level
78+
expected_line = padding + ("A" * 10)
79+
assert result[0] == expected_line
80+
assert result[1] == expected_line
81+
82+
83+
def test_indented_table() -> None:
84+
console = ru.Cmd2GeneralConsole()
85+
86+
level = 2
87+
table = Table("Column", box=rich.box.ASCII)
88+
table.add_row("Some Data")
89+
indented_table = ru.indent(table, level)
90+
91+
with console.capture() as capture:
92+
console.print(indented_table)
93+
result = capture.get().splitlines()
94+
95+
padding = " " * level
96+
assert result[0].startswith(padding + "+-----------+")
97+
assert result[1].startswith(padding + "| Column |")
98+
assert result[2].startswith(padding + "|-----------|")
99+
assert result[3].startswith(padding + "| Some Data |")
100+
assert result[4].startswith(padding + "+-----------+")
101+
102+
62103
@pytest.mark.parametrize(
63104
('rich_text', 'string'),
64105
[

0 commit comments

Comments
 (0)