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_truncate_line_tabs(): line = 'has\ttab' max_width = 9 truncated = cu.truncate_line(line, max_width) assert truncated == 'has t\N{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}'
def test_truncate_line_wide_text(): line = '苹苹other' max_width = 6 truncated = cu.truncate_line(line, max_width) assert truncated == '苹苹o\N{HORIZONTAL ELLIPSIS}'
def test_truncate_line_width_is_too_small(): line = 'foo' max_width = 0 with pytest.raises(ValueError): cu.truncate_line(line, max_width)
def test_truncate_line_with_newline(): line = 'fo\no' max_width = 2 with pytest.raises(ValueError): cu.truncate_line(line, max_width)
def test_truncate_line(): line = 'long' max_width = 3 truncated = cu.truncate_line(line, max_width) assert truncated == 'lo\N{HORIZONTAL ELLIPSIS}'
def test_truncate_line_already_fits(): line = 'long' max_width = 4 truncated = cu.truncate_line(line, max_width) assert truncated == line