Пример #1
0
def test_style_aware_wcswidth():
    base_str = HELLO_WORLD
    ansi_str = ansi.style(base_str, fg='green')
    assert ansi.style_aware_wcswidth(HELLO_WORLD) == ansi.style_aware_wcswidth(
        ansi_str)

    assert ansi.style_aware_wcswidth('i have a tab\t') == -1
    assert ansi.style_aware_wcswidth('i have a newline\n') == -1
Пример #2
0
def test_widest_line():
    text = ansi.style('i have\n3 lines\nThis is the longest one', fg='green')
    assert ansi.widest_line(text) == ansi.style_aware_wcswidth(
        "This is the longest one")

    text = "I'm just one line"
    assert ansi.widest_line(text) == ansi.style_aware_wcswidth(text)

    assert ansi.widest_line('i have a tab\t') == -1
Пример #3
0
def test_column_creation():
    # No width specified, blank label
    c = Column("")
    assert c.width == 1

    # No width specified, label isn't blank but has no width
    c = Column(ansi.style('', fg=ansi.fg.green))
    assert c.width == 1

    # No width specified, label has width
    c = Column("short\nreally long")
    assert c.width == ansi.style_aware_wcswidth("really long")

    # Width less than 1
    with pytest.raises(ValueError) as excinfo:
        Column("Column 1", width=0)
    assert "Column width cannot be less than 1" in str(excinfo.value)

    # Width specified
    c = Column("header", width=20)
    assert c.width == 20

    # max_data_lines less than 1
    with pytest.raises(ValueError) as excinfo:
        Column("Column 1", max_data_lines=0)
    assert "Max data lines cannot be less than 1" in str(excinfo.value)
Пример #4
0
def test_column_creation():
    # Width less than 1
    with pytest.raises(ValueError) as excinfo:
        Column("Column 1", width=0)
    assert "Column width cannot be less than 1" in str(excinfo.value)

    # Width specified
    c = Column("header", width=20)
    assert c.width == 20

    # max_data_lines less than 1
    with pytest.raises(ValueError) as excinfo:
        Column("Column 1", max_data_lines=0)
    assert "Max data lines cannot be less than 1" in str(excinfo.value)

    # No width specified, blank label
    c = Column("")
    assert c.width is None
    tc = TableCreator([c])
    assert tc.cols[0].width == 1

    # No width specified, label isn't blank but has no width
    c = Column(ansi.style('', fg=ansi.fg.green))
    assert c.width is None
    tc = TableCreator([c])
    assert tc.cols[0].width == 1

    # No width specified, label has width
    c = Column("a line")
    assert c.width is None
    tc = TableCreator([c])
    assert tc.cols[0].width == ansi.style_aware_wcswidth("a line")

    # No width specified, label has width and multiple lines
    c = Column("short\nreally long")
    assert c.width is None
    tc = TableCreator([c])
    assert tc.cols[0].width == ansi.style_aware_wcswidth("really long")

    # No width specified, label has tabs
    c = Column("line\twith\ttabs")
    assert c.width is None
    tc = TableCreator([c])
    assert tc.cols[0].width == ansi.style_aware_wcswidth(
        "line    with    tabs")
Пример #5
0
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
Пример #6
0
def test_style_aware_wcswidth():
    base_str = HELLO_WORLD
    ansi_str = ansi.style(base_str, fg='green')
    assert ansi.style_aware_wcswidth(ansi_str) != len(ansi_str)