summaryrefslogtreecommitdiff
path: root/tests/test_colors.py
blob: e586520ef0bf0711a5ebcad76fc480db46b7d655 (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
98
99
100
101
102
from ansicolor import Colors
from ansicolor import blue
from ansicolor import colordiff
from ansicolor import colorize
from ansicolor import get_code
from ansicolor import get_highlighter
from ansicolor import justify_formatted
from ansicolor import red
from ansicolor import strip_escapes
from ansicolor import wrap_string


def test_codes():
    # reset code
    assert '\033[0;0m' == get_code(None)

    # plain color codes
    assert '\033[0;0;30m' == get_code(Colors.Black)
    assert '\033[0;0;31m' == get_code(Colors.Red)
    assert '\033[0;0;32m' == get_code(Colors.Green)
    assert '\033[0;0;33m' == get_code(Colors.Yellow)
    assert '\033[0;0;34m' == get_code(Colors.Blue)
    assert '\033[0;0;35m' == get_code(Colors.Magenta)
    assert '\033[0;0;36m' == get_code(Colors.Cyan)
    assert '\033[0;0;37m' == get_code(Colors.White)

    # bold color
    assert '\033[0;1;31m' == get_code(Colors.Red, bold=True)

    # reverse color
    assert '\033[0;7;31m' == get_code(Colors.Red, reverse=True)

    # bold + reverse color
    assert '\033[1;7;31m' == get_code(Colors.Red, bold=True, reverse=True)


def test_coloring():
    assert '\033[0;0;31m' + 'hi' + '\033[0;0m' == red('hi')


def test_highlights():
    # can I get a highlighter?
    assert Colors.Green == get_highlighter(0)
    assert Colors.Yellow == get_highlighter(1)


def test_colorize1():
    assert (
        get_code(Colors.Red)
        + "Hi there"
        + get_code(None)
    ) == colorize("Hi there", Colors.Red)

def test_colorize2():
    assert (
        "H"
        + get_code(Colors.Red)
        + "i ther"
        + get_code(None)
        + "e"
    ) == colorize("Hi there", Colors.Red, start=1, end=7)


def test_wrap_string():
    assert (
        get_code(Colors.Red)
        + "Hi "
        + get_code(None)
        + "there"
    ) == wrap_string("Hi there", 3, Colors.Red)


def test_strip_escapes():
    assert "Hi there" == strip_escapes(
        colorize("Hi there", Colors.Red, start=3)
    )

    assert strip_escapes(
        colorize("Hi", None, bold=True) +
        " there, " +
        colorize("stranger", Colors.Green, bold=True)
    ) == "Hi there, stranger"


def test_colordiff():
    x, y = colordiff("hi bob", "hi there",
                     color_x=Colors.Red, color_y=Colors.Blue)

    fx = lambda s: red(s, reverse=True)
    fy = lambda s: blue(s, reverse=True)

    assert x == "hi " + fx("b") + fx("o") + fx("b")
    assert y == "hi " + fy("t") + fy("h") + fy("e") + fy("r") + fy("e")


def test_justify_formatted():
    def rjust(s, width):
        return s.rjust(width)

    assert justify_formatted(
        red("hi"), rjust, 10
    ) == "        " + red("hi")