Skip to content

Commit 242efbd

Browse files
committed
Updating unit tests
1 parent d7eee7d commit 242efbd

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

cmd2/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def align_text(text: str, alignment: TextAlignment, *, fill_char: str = ' ',
667667
if width is None:
668668
width = shutil.get_terminal_size().columns
669669

670-
if width <= 1:
670+
if width < 1:
671671
raise ValueError("width must be at least 1")
672672

673673
# Handle tabs
@@ -822,7 +822,7 @@ def truncate_string(text: str, max_width: int, *, tab_width: int = 4) -> str:
822822
if ansi.style_aware_wcswidth(text) == -1:
823823
raise (ValueError("text contains an unprintable character"))
824824

825-
if max_width <= 1:
825+
if max_width < 1:
826826
raise ValueError("max_width must be at least 1")
827827

828828
if ansi.style_aware_wcswidth(text) > max_width:

tests/test_utils.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,50 @@ def test_context_flag_exit_err(context_flag):
293293
context_flag.__exit__()
294294

295295

296+
def test_truncate_string():
297+
text = 'long'
298+
max_width = 3
299+
truncated = cu.truncate_string(text, max_width)
300+
assert truncated == 'lo\N{HORIZONTAL ELLIPSIS}'
301+
302+
def test_truncate_string_newline_in_text():
303+
text = 'fo\no'
304+
max_width = 2
305+
with pytest.raises(ValueError):
306+
cu.truncate_string(text, max_width)
307+
308+
def test_truncate_string_width_is_too_small():
309+
text = 'foo'
310+
max_width = 0
311+
with pytest.raises(ValueError):
312+
cu.truncate_string(text, max_width)
313+
314+
def test_truncate_string_wide_text():
315+
text = '苹苹other'
316+
max_width = 6
317+
truncated = cu.truncate_string(text, max_width)
318+
assert truncated == '苹苹o\N{HORIZONTAL ELLIPSIS}'
319+
320+
def test_truncate_string_tabs():
321+
text = 'has\ttab'
322+
max_width = 9
323+
truncated = cu.truncate_string(text, max_width)
324+
assert truncated == 'has t\N{HORIZONTAL ELLIPSIS}'
325+
296326
def test_align_text_fill_char_is_tab():
297327
text = 'foo'
298328
fill_char = '\t'
299329
width = 5
300330
aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width)
301331
assert aligned == text + ' '
302332

333+
def test_align_text_width_is_too_small():
334+
text = 'foo'
335+
fill_char = '-'
336+
width = 0
337+
with pytest.raises(ValueError):
338+
cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width)
339+
303340
def test_align_text_fill_char_is_too_long():
304341
text = 'foo'
305342
fill_char = 'fill'
@@ -340,7 +377,7 @@ def test_align_text_wider_than_width_truncate():
340377
fill_char = '-'
341378
width = 8
342379
aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width, truncate=True)
343-
assert aligned == 'long te' + "\N{HORIZONTAL ELLIPSIS}"
380+
assert aligned == 'long te\N{HORIZONTAL ELLIPSIS}'
344381

345382
def test_align_text_has_unprintable():
346383
text = 'foo\x02'

0 commit comments

Comments
 (0)