From 023d5afadabfba19d77dcc7ec6c7f8a5c8bd1651 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 11 Feb 2020 12:39:38 -0500 Subject: Fixed bug where ANSI style sequences were not correctly handled in utils.truncate_line() --- tests/test_utils.py | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'tests/test_utils.py') 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' -- cgit v1.2.1