Skip to content

Commit 5ffed13

Browse files
committed
Added more text alignment unit tests
1 parent 6f16e67 commit 5ffed13

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

cmd2/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ def ljustify_text(text: str, *, fill_char: str = ' ', width: Optional[int] = Non
737737
:param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will
738738
be converted to a space.
739739
:return: left-justified text
740+
:raises: TypeError if fill_char is more than one character
741+
ValueError if text or fill_char contains an unprintable character
740742
"""
741743
return align_text(text, fill_char=fill_char, width=width,
742744
tab_width=tab_width, alignment=TextAlignment.LEFT)
@@ -754,6 +756,8 @@ def center_text(text: str, *, fill_char: str = ' ', width: Optional[int] = None,
754756
:param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will
755757
be converted to a space.
756758
:return: centered text
759+
:raises: TypeError if fill_char is more than one character
760+
ValueError if text or fill_char contains an unprintable character
757761
"""
758762
return align_text(text, fill_char=fill_char, width=width,
759763
tab_width=tab_width, alignment=TextAlignment.CENTER)
@@ -771,6 +775,8 @@ def rjustify_text(text: str, *, fill_char: str = ' ', width: Optional[int] = Non
771775
:param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will
772776
be converted to a space.
773777
:return: right-justified text
778+
:raises: TypeError if fill_char is more than one character
779+
ValueError if text or fill_char contains an unprintable character
774780
"""
775781
return align_text(text, fill_char=fill_char, width=width,
776782
tab_width=tab_width, alignment=TextAlignment.RIGHT)

tests/test_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def test_align_text_blank():
326326
fill_char = '-'
327327
width = 5
328328
aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT)
329-
assert aligned == text + fill_char * width
329+
assert aligned == fill_char * width
330330

331331
def test_align_text_wider_than_width():
332332
text = 'long'
@@ -335,6 +335,13 @@ def test_align_text_wider_than_width():
335335
aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT)
336336
assert aligned == text
337337

338+
def test_align_text_has_unprintable():
339+
text = 'foo\x02'
340+
fill_char = '-'
341+
width = 5
342+
with pytest.raises(ValueError):
343+
cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT)
344+
338345
def test_align_text_term_width():
339346
import shutil
340347
from cmd2 import ansi

0 commit comments

Comments
 (0)