diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-02-11 12:39:38 -0500 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-02-11 12:39:38 -0500 |
commit | 023d5afadabfba19d77dcc7ec6c7f8a5c8bd1651 (patch) | |
tree | 5f37b4f4e8d5d4aa8be3f56624a4138e8b7bc582 /tests | |
parent | 8d9f97bdcaacf8465e8fe61f64e5d22753cc415c (diff) | |
download | cmd2-git-023d5afadabfba19d77dcc7ec6c7f8a5c8bd1651.tar.gz |
Fixed bug where ANSI style sequences were not correctly handled in utils.truncate_line()
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_utils.py | 43 |
1 files changed, 37 insertions, 6 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py index 5030ce0e..db432286 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -10,6 +10,7 @@ import time import pytest import cmd2.utils as cu +from cmd2.constants import HORIZONTAL_ELLIPSIS HELLO_WORLD = 'Hello, world!' @@ -297,7 +298,13 @@ def test_truncate_line(): line = 'long' max_width = 3 truncated = cu.truncate_line(line, max_width) - assert truncated == 'lo\N{HORIZONTAL ELLIPSIS}' + assert truncated == 'lo' + HORIZONTAL_ELLIPSIS + +def test_truncate_line_already_fits(): + line = 'long' + max_width = 4 + truncated = cu.truncate_line(line, max_width) + assert truncated == line def test_truncate_line_with_newline(): line = 'fo\no' @@ -315,20 +322,44 @@ def test_truncate_line_wide_text(): line = '苹苹other' max_width = 6 truncated = cu.truncate_line(line, max_width) - assert truncated == '苹苹o\N{HORIZONTAL ELLIPSIS}' + assert truncated == '苹苹o' + HORIZONTAL_ELLIPSIS def test_truncate_line_split_wide_text(): """Test when truncation results in a string which is shorter than max_width""" line = '1苹2苹' max_width = 3 truncated = cu.truncate_line(line, max_width) - assert truncated == '1\N{HORIZONTAL ELLIPSIS}' + assert truncated == '1' + HORIZONTAL_ELLIPSIS def test_truncate_line_tabs(): line = 'has\ttab' max_width = 9 truncated = cu.truncate_line(line, max_width) - assert truncated == 'has t\N{HORIZONTAL ELLIPSIS}' + assert truncated == 'has t' + HORIZONTAL_ELLIPSIS + +def test_truncate_with_style(): + from cmd2 import ansi + + before_style = ansi.fg.blue + ansi.UNDERLINE_ENABLE + after_style = ansi.fg.reset + ansi.UNDERLINE_DISABLE + + # Style only before truncated text + line = before_style + 'long' + max_width = 3 + truncated = cu.truncate_line(line, max_width) + assert truncated == before_style + 'lo' + HORIZONTAL_ELLIPSIS + + # Style before and after truncated text + line = before_style + 'long' + after_style + max_width = 3 + truncated = cu.truncate_line(line, max_width) + assert truncated == before_style + 'lo' + HORIZONTAL_ELLIPSIS + after_style + + # Style only after truncated text + line = 'long' + after_style + max_width = 3 + truncated = cu.truncate_line(line, max_width) + assert truncated == 'lo' + HORIZONTAL_ELLIPSIS + after_style def test_align_text_fill_char_is_tab(): text = 'foo' @@ -384,7 +415,7 @@ def test_align_text_wider_than_width_truncate(): fill_char = '-' width = 8 aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width, truncate=True) - assert aligned == 'long te\N{HORIZONTAL ELLIPSIS}' + assert aligned == 'long te' + HORIZONTAL_ELLIPSIS def test_align_text_wider_than_width_truncate_add_fill(): """Test when truncation results in a string which is shorter than width and align_text adds filler""" @@ -392,7 +423,7 @@ def test_align_text_wider_than_width_truncate_add_fill(): fill_char = '-' width = 3 aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width, truncate=True) - assert aligned == '1\N{HORIZONTAL ELLIPSIS}-' + assert aligned == '1' + HORIZONTAL_ELLIPSIS + fill_char def test_align_text_has_unprintable(): text = 'foo\x02' |