diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-12-09 16:34:07 -0500 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-12-09 16:34:07 -0500 |
commit | 5ffed1387ddc4e5725f9c6b5b632a611eca1e3ca (patch) | |
tree | a9092d195e5099c62e526adfbf46acc77ef3b84b | |
parent | 6f16e671971a9fac4edfda9c0117ceaeb5e6487e (diff) | |
download | cmd2-git-5ffed1387ddc4e5725f9c6b5b632a611eca1e3ca.tar.gz |
Added more text alignment unit tests
-rw-r--r-- | cmd2/utils.py | 6 | ||||
-rw-r--r-- | tests/test_utils.py | 9 |
2 files changed, 14 insertions, 1 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index d7c9ff19..4235db7d 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -737,6 +737,8 @@ def ljustify_text(text: str, *, fill_char: str = ' ', width: Optional[int] = Non :param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will be converted to a space. :return: left-justified text + :raises: TypeError if fill_char is more than one character + ValueError if text or fill_char contains an unprintable character """ return align_text(text, fill_char=fill_char, width=width, tab_width=tab_width, alignment=TextAlignment.LEFT) @@ -754,6 +756,8 @@ def center_text(text: str, *, fill_char: str = ' ', width: Optional[int] = None, :param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will be converted to a space. :return: centered text + :raises: TypeError if fill_char is more than one character + ValueError if text or fill_char contains an unprintable character """ return align_text(text, fill_char=fill_char, width=width, tab_width=tab_width, alignment=TextAlignment.CENTER) @@ -771,6 +775,8 @@ def rjustify_text(text: str, *, fill_char: str = ' ', width: Optional[int] = Non :param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will be converted to a space. :return: right-justified text + :raises: TypeError if fill_char is more than one character + ValueError if text or fill_char contains an unprintable character """ return align_text(text, fill_char=fill_char, width=width, tab_width=tab_width, alignment=TextAlignment.RIGHT) diff --git a/tests/test_utils.py b/tests/test_utils.py index 1327064f..745e5ff4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -326,7 +326,7 @@ def test_align_text_blank(): fill_char = '-' width = 5 aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) - assert aligned == text + fill_char * width + assert aligned == fill_char * width def test_align_text_wider_than_width(): text = 'long' @@ -335,6 +335,13 @@ def test_align_text_wider_than_width(): aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) assert aligned == text +def test_align_text_has_unprintable(): + text = 'foo\x02' + fill_char = '-' + width = 5 + with pytest.raises(ValueError): + cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) + def test_align_text_term_width(): import shutil from cmd2 import ansi |