File: test_terminal_utils.py

package info (click to toggle)
cmd2 3.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,664 kB
  • sloc: python: 17,488; makefile: 114; sh: 39; javascript: 7
file content (81 lines) | stat: -rw-r--r-- 2,346 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Unit testing for cmd2/terminal_utils.py module"""

import pytest

from cmd2 import (
    Color,
)
from cmd2 import string_utils as su
from cmd2 import terminal_utils as tu


def test_set_title() -> None:
    title = "Hello, world!"
    assert tu.set_title_str(title) == tu.OSC + '2;' + title + tu.BEL


@pytest.mark.parametrize(
    ('cols', 'prompt', 'line', 'cursor', 'msg', 'expected'),
    [
        (
            127,
            '(Cmd) ',
            'help his',
            12,
            su.stylize('Hello World!', style=Color.MAGENTA),
            '\x1b[2K\r\x1b[35mHello World!\x1b[0m',
        ),
        (127, '\n(Cmd) ', 'help ', 5, 'foo', '\x1b[2K\x1b[1A\x1b[2K\rfoo'),
        (
            10,
            '(Cmd) ',
            'help history of the american republic',
            4,
            'boo',
            '\x1b[3B\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\rboo',
        ),
    ],
)
def test_async_alert_str(cols, prompt, line, cursor, msg, expected) -> None:
    alert_str = tu.async_alert_str(terminal_columns=cols, prompt=prompt, line=line, cursor_offset=cursor, alert_msg=msg)
    assert alert_str == expected


def test_clear_screen() -> None:
    clear_type = 2
    assert tu.clear_screen_str(clear_type) == f"{tu.CSI}{clear_type}J"

    clear_type = -1
    expected_err = "clear_type must in an integer from 0 to 3"
    with pytest.raises(ValueError, match=expected_err):
        tu.clear_screen_str(clear_type)

    clear_type = 4
    with pytest.raises(ValueError, match=expected_err):
        tu.clear_screen_str(clear_type)


def test_clear_line() -> None:
    clear_type = 2
    assert tu.clear_line_str(clear_type) == f"{tu.CSI}{clear_type}K"

    clear_type = -1
    expected_err = "clear_type must in an integer from 0 to 2"
    with pytest.raises(ValueError, match=expected_err):
        tu.clear_line_str(clear_type)

    clear_type = 3
    with pytest.raises(ValueError, match=expected_err):
        tu.clear_line_str(clear_type)


def test_cursor() -> None:
    count = 1
    assert tu.Cursor.UP(count) == f"{tu.CSI}{count}A"
    assert tu.Cursor.DOWN(count) == f"{tu.CSI}{count}B"
    assert tu.Cursor.FORWARD(count) == f"{tu.CSI}{count}C"
    assert tu.Cursor.BACK(count) == f"{tu.CSI}{count}D"

    x = 4
    y = 5
    assert tu.Cursor.SET_POS(x, y) == f"{tu.CSI}{y};{x}H"