-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_termcolor.py
114 lines (95 loc) · 3.06 KB
/
test_termcolor.py
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
103
104
105
106
107
108
109
110
111
112
113
114
import os
import pytest
from termcolor import ATTRIBUTES, COLORS, HIGHLIGHTS, colored, cprint
ALL_COLORS = list(COLORS) + [None]
ALL_HIGHLIGHTS = list(HIGHLIGHTS) + [None]
ALL_ATTRIBUTES = list(ATTRIBUTES) + [None]
def setup_module():
# By default, make sure no env vars already set for tests
try:
del os.environ["ANSI_COLORS_DISABLED"]
except KeyError: # pragma: no cover
pass
def test_basic():
assert colored("text") == "text\x1b[0m"
def test_sanity():
for color in ALL_COLORS:
for highlight in ALL_HIGHLIGHTS:
for attribute in ALL_ATTRIBUTES:
attrs = None if attribute is None else [attribute]
colored("text", color, highlight, attrs)
cprint("text", color, highlight, attrs)
def assert_cprint(
capsys, expected, text, color=None, on_color=None, attrs=None, **kwargs
):
cprint(text, color, on_color, attrs, **kwargs)
captured = capsys.readouterr()
print(captured.out)
assert captured.out == expected + "\n"
@pytest.mark.parametrize(
"color, expected",
[
("grey", "\x1b[30mtext\x1b[0m"),
("red", "\x1b[31mtext\x1b[0m"),
("green", "\x1b[32mtext\x1b[0m"),
("yellow", "\x1b[33mtext\x1b[0m"),
("blue", "\x1b[34mtext\x1b[0m"),
("magenta", "\x1b[35mtext\x1b[0m"),
("cyan", "\x1b[36mtext\x1b[0m"),
("white", "\x1b[37mtext\x1b[0m"),
],
)
def test_color(capsys, color, expected):
assert colored("text", color=color) == expected
assert_cprint(capsys, expected, "text", color=color)
@pytest.mark.parametrize(
"on_color, expected",
[
("on_grey", "\x1b[40mtext\x1b[0m"),
("on_red", "\x1b[41mtext\x1b[0m"),
("on_green", "\x1b[42mtext\x1b[0m"),
("on_yellow", "\x1b[43mtext\x1b[0m"),
("on_blue", "\x1b[44mtext\x1b[0m"),
("on_magenta", "\x1b[45mtext\x1b[0m"),
("on_cyan", "\x1b[46mtext\x1b[0m"),
("on_white", "\x1b[47mtext\x1b[0m"),
],
)
def test_on_color(capsys, on_color, expected):
assert colored("text", on_color=on_color) == expected
assert_cprint(capsys, expected, "text", on_color=on_color)
@pytest.mark.parametrize(
"attr, expected",
[
("bold", "\x1b[1mtext\x1b[0m"),
("dark", "\x1b[2mtext\x1b[0m"),
("underline", "\x1b[4mtext\x1b[0m"),
("blink", "\x1b[5mtext\x1b[0m"),
("reverse", "\x1b[7mtext\x1b[0m"),
("concealed", "\x1b[8mtext\x1b[0m"),
],
)
def test_attrs(capsys, attr, expected):
assert colored("text", attrs=[attr]) == expected
assert_cprint(capsys, expected, "text", attrs=[attr])
@pytest.mark.parametrize(
"test_env_var",
[
"ANSI_COLORS_DISABLED",
"NO_COLOR",
],
)
@pytest.mark.parametrize(
"test_value",
[
"true",
"false",
"1",
"0",
"",
],
)
def test_env_var(monkeypatch, test_env_var, test_value):
"""Assert nothing applied when this env var set, regardless of value."""
monkeypatch.setenv(test_env_var, test_value)
assert colored("text", color="red") == "text"