def test_align_text_width_is_too_small(): text = 'foo' fill_char = '-' width = 0 with pytest.raises(ValueError): cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width)
def test_align_text_has_unprintable(): text = 'foo\x02' fill_char = '-' width = 5 with pytest.raises(ValueError): cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width)
def test_align_text_fill_char_is_too_long(): text = 'foo' fill_char = 'fill' width = 5 with pytest.raises(TypeError): cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width)
def test_align_text_fill_char_is_unprintable(): text = 'foo' fill_char = '\n' width = 5 with pytest.raises(ValueError): cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT)
def test_align_text_fill_char_is_newline(): text = 'foo' fill_char = '\n' width = 5 with pytest.raises(ValueError): cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width)
def test_align_text_with_style(): from cmd2 import ( ansi, ) # Single line with only left fill text = ansi.style('line1', fg=ansi.fg.bright_blue) fill_char = ansi.style('-', fg=ansi.fg.bright_yellow) width = 6 aligned = cu.align_text(text, cu.TextAlignment.RIGHT, fill_char=fill_char, width=width) left_fill = ansi.RESET_ALL + fill_char + ansi.RESET_ALL right_fill = ansi.RESET_ALL line_1_text = ansi.fg.bright_blue + 'line1' + ansi.FG_RESET assert aligned == (left_fill + line_1_text + right_fill) # Single line with only right fill text = ansi.style('line1', fg=ansi.fg.bright_blue) fill_char = ansi.style('-', fg=ansi.fg.bright_yellow) width = 6 aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width) left_fill = ansi.RESET_ALL right_fill = ansi.RESET_ALL + fill_char + ansi.RESET_ALL line_1_text = ansi.fg.bright_blue + 'line1' + ansi.FG_RESET assert aligned == (left_fill + line_1_text + right_fill) # Multiple lines to show that style is preserved across all lines. Also has left and right fill. text = ansi.style('line1\nline2', fg=ansi.fg.bright_blue) fill_char = ansi.style('-', fg=ansi.fg.bright_yellow) width = 7 aligned = cu.align_text(text, cu.TextAlignment.CENTER, fill_char=fill_char, width=width) left_fill = ansi.RESET_ALL + fill_char + ansi.RESET_ALL right_fill = ansi.RESET_ALL + fill_char + ansi.RESET_ALL line_1_text = ansi.fg.bright_blue + 'line1' line_2_text = ansi.fg.bright_blue + 'line2' + ansi.FG_RESET assert aligned == (left_fill + line_1_text + right_fill + '\n' + left_fill + line_2_text + right_fill)
def test_align_text_wider_than_width(): text = 'long text field' fill_char = '-' width = 8 aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width) assert aligned == text
def test_align_text_blank(): text = '' fill_char = '-' width = 5 aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width) assert aligned == fill_char * width
def test_align_text_fill_char_is_tab(): text = 'foo' fill_char = '\t' width = 5 aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width) assert aligned == text + ' '
def test_align_text_wider_than_width(): text = 'long' fill_char = '-' width = 3 aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) assert aligned == text
def test_align_text_wider_than_width_truncate(): text = 'long text field' 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}'
def test_align_text_has_tabs(): text = '\t\tfoo' fill_char = '-' width = 10 aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width, tab_width=2) assert aligned == ' ' + 'foo' + '---'
def test_align_text_term_width(): import shutil from cmd2 import ansi text = 'foo' fill_char = ' ' term_width = shutil.get_terminal_size().columns expected_fill = (term_width - ansi.style_aware_wcswidth(text)) * fill_char aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char) assert aligned == text + expected_fill
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""" text = '1苹2苹' 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}-'