summaryrefslogtreecommitdiff
path: root/tests/test_ansi.py
blob: b32bdddfbc344eab4ba34baa7e08fc0ada0d1821 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# coding=utf-8
# flake8: noqa E302
"""
Unit testing for cmd2/ansi.py module
"""
import pytest
from colorama import Fore, Back, Style

import cmd2.ansi as ansi

HELLO_WORLD = 'Hello, world!'


def test_strip_ansi():
    base_str = HELLO_WORLD
    ansi_str = Fore.GREEN + base_str + Fore.RESET
    assert base_str != ansi_str
    assert base_str == ansi.strip_ansi(ansi_str)


def test_ansi_safe_wcswidth():
    base_str = HELLO_WORLD
    ansi_str = Fore.GREEN + base_str + Fore.RESET
    assert ansi.ansi_safe_wcswidth(ansi_str) != len(ansi_str)


def test_style_none():
    base_str = HELLO_WORLD
    ansi_str = base_str
    assert ansi.style(base_str) == ansi_str


def test_style_fg():
    base_str = HELLO_WORLD
    ansi_str = Fore.BLUE + base_str + Fore.RESET
    assert ansi.style(base_str, fg='blue') == ansi_str


def test_style_bg():
    base_str = HELLO_WORLD
    ansi_str = Back.GREEN + base_str + Back.RESET
    assert ansi.style(base_str, bg='green') == ansi_str


def test_style_bold():
    base_str = HELLO_WORLD
    ansi_str = Style.BRIGHT + base_str + Style.NORMAL
    assert ansi.style(base_str, bold=True) == ansi_str


def test_style_underline():
    base_str = HELLO_WORLD
    ansi_str = ansi.UNDERLINE_ENABLE + base_str + ansi.UNDERLINE_DISABLE
    assert ansi.style(base_str, underline=True) == ansi_str


def test_style_multi():
    base_str = HELLO_WORLD
    ansi_str = Fore.BLUE + Back.GREEN + Style.BRIGHT + ansi.UNDERLINE_ENABLE + \
               base_str + Fore.RESET + Back.RESET + Style.NORMAL + ansi.UNDERLINE_DISABLE
    assert ansi.style(base_str, fg='blue', bg='green', bold=True, underline=True) == ansi_str


def test_style_color_not_exist():
    base_str = HELLO_WORLD

    with pytest.raises(ValueError):
        ansi.style(base_str, fg='fake', bg='green')

    with pytest.raises(ValueError):
        ansi.style(base_str, fg='blue', bg='fake')


def test_fg_lookup_dict():
    assert ansi.fg_lookup('gray') == Fore.LIGHTBLACK_EX


def test_fg_lookup_colorama():
    assert ansi.fg_lookup('green') == Fore.GREEN


def test_fg_lookup_nonexist():
    with pytest.raises(ValueError):
        ansi.fg_lookup('foo')


def test_bg_lookup_dict():
    assert ansi.bg_lookup('gray') == Back.LIGHTBLACK_EX


def test_bg_lookup_colorama():
    assert ansi.bg_lookup('green') == Back.GREEN


def test_bg_lookup_nonexist():
    with pytest.raises(ValueError):
        ansi.bg_lookup('bar')