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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
import pytest
from rich.color import Color, ColorSystem, ColorType
from rich import errors
from rich.style import Style, StyleStack
def test_str():
assert str(Style(bold=False)) == "not bold"
assert str(Style(color="red", bold=False)) == "not bold red"
assert str(Style(color="red", bold=False, italic=True)) == "not bold italic red"
assert str(Style()) == "none"
assert str(Style(bold=True)) == "bold"
assert str(Style(color="red", bold=True)) == "bold red"
assert str(Style(color="red", bgcolor="black", bold=True)) == "bold red on black"
all_styles = Style(
color="red",
bgcolor="black",
bold=True,
dim=True,
italic=True,
underline=True,
blink=True,
blink2=True,
reverse=True,
conceal=True,
strike=True,
underline2=True,
frame=True,
encircle=True,
overline=True,
)
expected = "bold dim italic underline blink blink2 reverse conceal strike underline2 frame encircle overline red on black"
assert str(all_styles) == expected
assert str(Style(link="foo")) == "link foo"
def test_ansi_codes():
all_styles = Style(
color="red",
bgcolor="black",
bold=True,
dim=True,
italic=True,
underline=True,
blink=True,
blink2=True,
reverse=True,
conceal=True,
strike=True,
underline2=True,
frame=True,
encircle=True,
overline=True,
)
expected = "1;2;3;4;5;6;7;8;9;21;51;52;53;31;40"
assert all_styles._make_ansi_codes(ColorSystem.TRUECOLOR) == expected
def test_repr():
assert repr(Style(bold=True, color="red")) == 'Style.parse("bold red")'
def test_eq():
assert Style(bold=True, color="red") == Style(bold=True, color="red")
assert Style(bold=True, color="red") != Style(bold=True, color="green")
assert Style().__eq__("foo") == NotImplemented
def test_hash():
assert isinstance(hash(Style()), int)
def test_empty():
assert Style.null() == Style()
def test_bool():
assert bool(Style()) is False
assert bool(Style(bold=True)) is True
assert bool(Style(color="red")) is True
assert bool(Style.parse("")) is False
def test_color_property():
assert Style(color="red").color == Color("red", ColorType.STANDARD, 1, None)
def test_bgcolor_property():
assert Style(bgcolor="black").bgcolor == Color("black", ColorType.STANDARD, 0, None)
def test_parse():
assert Style.parse("") == Style()
assert Style.parse("red") == Style(color="red")
assert Style.parse("not bold") == Style(bold=False)
assert Style.parse("bold red on black") == Style(
color="red", bgcolor="black", bold=True
)
assert Style.parse("bold link https://example.org") == Style(
bold=True, link="https://example.org"
)
with pytest.raises(errors.StyleSyntaxError):
Style.parse("on")
with pytest.raises(errors.StyleSyntaxError):
Style.parse("on nothing")
with pytest.raises(errors.StyleSyntaxError):
Style.parse("rgb(999,999,999)")
with pytest.raises(errors.StyleSyntaxError):
Style.parse("not monkey")
with pytest.raises(errors.StyleSyntaxError):
Style.parse("link")
def test_link_id():
assert Style().link_id == ""
assert Style.parse("").link_id == ""
assert Style.parse("red").link_id == ""
style = Style.parse("red link https://example.org")
assert isinstance(style.link_id, str)
assert len(style.link_id) > 1
def test_get_html_style():
expected = "color: #7f7fbf; background-color: #800000; font-weight: bold; font-style: italic; text-decoration: underline; text-decoration: line-through; text-decoration: overline"
assert (
Style(
reverse=True,
dim=True,
color="red",
bgcolor="blue",
bold=True,
italic=True,
underline=True,
strike=True,
overline=True,
).get_html_style()
== expected
)
def test_chain():
assert Style.chain(Style(color="red"), Style(bold=True)) == Style(
color="red", bold=True
)
def test_copy():
style = Style(color="red", bgcolor="black", italic=True)
assert style == style.copy()
assert style is not style.copy()
def test_render():
assert Style(color="red").render("foo", color_system=None) == "foo"
assert (
Style(color="red", bgcolor="black", bold=True).render("foo")
== "\x1b[1;31;40mfoo\x1b[0m"
)
assert Style().render("foo") == "foo"
def test_test():
Style(color="red").test("hello")
def test_add():
assert Style(color="red") + None == Style(color="red")
assert Style().__add__("foo") == NotImplemented
def test_iadd():
style = Style(color="red")
style += Style(bold=True)
assert style == Style(color="red", bold=True)
style += None
assert style == Style(color="red", bold=True)
def test_style_stack():
stack = StyleStack(Style(color="red"))
repr(stack)
assert stack.current == Style(color="red")
stack.push(Style(bold=True))
assert stack.current == Style(color="red", bold=True)
stack.pop()
assert stack.current == Style(color="red")
def test_pick_first():
with pytest.raises(ValueError):
Style.pick_first()
def test_background_style():
assert Style(bold=True, color="yellow", bgcolor="red").background_style == Style(
bgcolor="red"
)
def test_without_color():
style = Style(bold=True, color="red", bgcolor="blue")
colorless_style = style.without_color
assert colorless_style.color == None
assert colorless_style.bgcolor == None
assert colorless_style.bold == True
null_style = Style.null()
assert null_style.without_color == null_style
|