FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-progressbar/tests/test_color.py at develop · commit-0/python-progressbar · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
commit-0
/
python-progressbar
Public
forked from
wolph/python-progressbar
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-progressbar
/
tests
/
test_color.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
412 lines (354 loc) · 10.9 KB
Breadcrumbs
python-progressbar
/
tests
/
test_color.py
Copy path
File metadata and controls
412 lines (354 loc) · 10.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from
__future__
import
annotations
import
os
from
typing
import
ClassVar
import
pytest
import
progressbar
import
progressbar
.
terminal
from
progressbar
import
env
,
terminal
,
widgets
from
progressbar
.
terminal
import
Color
,
Colors
,
apply_colors
,
colors
ENVIRONMENT_VARIABLES
=
[
'PROGRESSBAR_ENABLE_COLORS'
,
'FORCE_COLOR'
,
'COLORTERM'
,
'TERM'
,
'JUPYTER_COLUMNS'
,
'JUPYTER_LINES'
,
'JPY_PARENT_PID'
,
]
@
pytest
.
fixture
(
autouse
=
True
)
def
clear_env
(
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
# Clear all environment variables that might affect the tests
for
variable
in
ENVIRONMENT_VARIABLES
:
monkeypatch
.
delenv
(
variable
,
raising
=
False
)
monkeypatch
.
setattr
(
env
,
'JUPYTER'
,
False
)
@
pytest
.
mark
.
parametrize
(
'variable'
,
[
'PROGRESSBAR_ENABLE_COLORS'
,
'FORCE_COLOR'
,
],
)
def
test_color_environment_variables
(
monkeypatch
:
pytest
.
MonkeyPatch
,
variable
:
str
,
)
->
None
:
if
os
.
name
==
'nt'
:
# Windows has special handling so we need to disable that to make the
# tests work properly
monkeypatch
.
setattr
(
os
,
'name'
,
'posix'
)
monkeypatch
.
setattr
(
env
,
'COLOR_SUPPORT'
,
env
.
ColorSupport
.
XTERM_256
,
)
monkeypatch
.
setenv
(
variable
,
'true'
)
bar
=
progressbar
.
ProgressBar
()
assert
not
env
.
is_ansi_terminal
(
bar
.
fd
)
assert
not
bar
.
is_ansi_terminal
assert
bar
.
enable_colors
monkeypatch
.
setenv
(
variable
,
'false'
)
bar
=
progressbar
.
ProgressBar
()
assert
not
bar
.
enable_colors
monkeypatch
.
setenv
(
variable
,
''
)
bar
=
progressbar
.
ProgressBar
()
assert
not
bar
.
enable_colors
@
pytest
.
mark
.
parametrize
(
'variable'
,
[
'FORCE_COLOR'
,
'PROGRESSBAR_ENABLE_COLORS'
,
'COLORTERM'
,
'TERM'
,
],
)
@
pytest
.
mark
.
parametrize
(
'value'
,
[
''
,
'truecolor'
,
'24bit'
,
'256'
,
'xterm-256'
,
'xterm'
,
],
)
def
test_color_support_from_env
(
monkeypatch
,
variable
,
value
)
->
None
:
if
os
.
name
==
'nt'
:
# Windows has special handling so we need to disable that to make the
# tests work properly
monkeypatch
.
setattr
(
os
,
'name'
,
'posix'
)
monkeypatch
.
setenv
(
variable
,
value
)
env
.
ColorSupport
.
from_env
()
@
pytest
.
mark
.
parametrize
(
'variable'
,
[
'JUPYTER_COLUMNS'
,
'JUPYTER_LINES'
,
],
)
def
test_color_support_from_env_jupyter
(
monkeypatch
,
variable
)
->
None
:
monkeypatch
.
setattr
(
env
,
'JUPYTER'
,
True
)
assert
env
.
ColorSupport
.
from_env
()
==
env
.
ColorSupport
.
XTERM_TRUECOLOR
# Sanity check
monkeypatch
.
setattr
(
env
,
'JUPYTER'
,
False
)
if
os
.
name
==
'nt'
:
assert
env
.
ColorSupport
.
from_env
()
==
env
.
ColorSupport
.
WINDOWS
else
:
assert
env
.
ColorSupport
.
from_env
()
==
env
.
ColorSupport
.
NONE
def
test_enable_colors_flags
()
->
None
:
bar
=
progressbar
.
ProgressBar
(
enable_colors
=
True
)
assert
bar
.
enable_colors
bar
=
progressbar
.
ProgressBar
(
enable_colors
=
False
)
assert
not
bar
.
enable_colors
bar
=
progressbar
.
ProgressBar
(
enable_colors
=
env
.
ColorSupport
.
XTERM_TRUECOLOR
,
)
assert
bar
.
enable_colors
with
pytest
.
raises
(
ValueError
):
progressbar
.
ProgressBar
(
enable_colors
=
12345
)
class
_TestFixedColorSupport
(
progressbar
.
widgets
.
WidgetBase
):
_fixed_colors
:
ClassVar
[
widgets
.
TFixedColors
]
=
widgets
.
TFixedColors
(
fg_none
=
progressbar
.
widgets
.
colors
.
yellow
,
bg_none
=
None
,
)
def
__call__
(
self
,
*
args
,
**
kwargs
)
->
None
:
pass
class
_TestFixedGradientSupport
(
progressbar
.
widgets
.
WidgetBase
):
_gradient_colors
:
ClassVar
[
widgets
.
TGradientColors
]
=
(
widgets
.
TGradientColors
(
fg
=
progressbar
.
widgets
.
colors
.
gradient
,
bg
=
None
,
)
)
def
__call__
(
self
,
*
args
,
**
kwargs
)
->
None
:
pass
@
pytest
.
mark
.
parametrize
(
'widget'
,
[
progressbar
.
Percentage
,
progressbar
.
SimpleProgress
,
_TestFixedColorSupport
,
_TestFixedGradientSupport
,
],
)
def
test_color_widgets
(
widget
)
->
None
:
assert
widget
().
uses_colors
print
(
f'
{
widget
}
has colors?
{
widget
.
uses_colors
}
'
)
def
test_color_gradient
()
->
None
:
gradient
=
terminal
.
ColorGradient
(
colors
.
red
)
assert
gradient
.
get_color
(
0
)
==
gradient
.
get_color
(
-
1
)
assert
gradient
.
get_color
(
1
)
==
gradient
.
get_color
(
2
)
assert
gradient
.
get_color
(
0.5
)
==
colors
.
red
gradient
=
terminal
.
ColorGradient
(
colors
.
red
,
colors
.
yellow
)
assert
gradient
.
get_color
(
0
)
==
colors
.
red
assert
gradient
.
get_color
(
1
)
==
colors
.
yellow
assert
gradient
.
get_color
(
0.5
)
!=
colors
.
red
assert
gradient
.
get_color
(
0.5
)
!=
colors
.
yellow
gradient
=
terminal
.
ColorGradient
(
colors
.
red
,
colors
.
yellow
,
interpolate
=
False
,
)
assert
gradient
.
get_color
(
0
)
==
colors
.
red
assert
gradient
.
get_color
(
1
)
==
colors
.
yellow
assert
gradient
.
get_color
(
0.5
)
==
colors
.
red
@
pytest
.
mark
.
parametrize
(
'widget'
,
[
progressbar
.
Counter
,
],
)
def
test_no_color_widgets
(
widget
)
->
None
:
assert
not
widget
().
uses_colors
print
(
f'
{
widget
}
has colors?
{
widget
.
uses_colors
}
'
)
assert
widget
(
fixed_colors
=
_TestFixedColorSupport
.
_fixed_colors
,
).
uses_colors
assert
widget
(
gradient_colors
=
_TestFixedGradientSupport
.
_gradient_colors
,
).
uses_colors
def
test_colors
(
monkeypatch
)
->
None
:
for
colors_
in
Colors
.
by_rgb
.
values
():
for
color
in
colors_
:
rgb
=
color
.
rgb
assert
rgb
.
rgb
assert
rgb
.
hex
assert
rgb
.
to_ansi_16
is
not
None
assert
rgb
.
to_ansi_256
is
not
None
assert
rgb
.
to_windows
is
not
None
with
monkeypatch
.
context
()
as
context
:
context
.
setattr
(
env
,
'COLOR_SUPPORT'
,
env
.
ColorSupport
.
XTERM
)
assert
color
.
underline
context
.
setattr
(
env
,
'COLOR_SUPPORT'
,
env
.
ColorSupport
.
WINDOWS
)
assert
color
.
underline
assert
color
.
fg
assert
color
.
bg
assert
str
(
rgb
)
assert
color
(
'test'
)
color_no_name
=
Color
(
rgb
=
color
.
rgb
,
hls
=
color
.
hls
,
name
=
None
,
xterm
=
color
.
xterm
,
)
# Test without name
assert
str
(
color_no_name
)
!=
str
(
color
)
def
test_color
()
->
None
:
color
=
colors
.
red
if
os
.
name
!=
'nt'
:
assert
color
(
'x'
)
==
color
.
fg
(
'x'
)
!=
'x'
assert
color
.
fg
(
'x'
)
!=
color
.
bg
(
'x'
)
!=
'x'
assert
color
.
fg
(
'x'
)
!=
color
.
underline
(
'x'
)
!=
'x'
# Color hashes are based on the RGB value
assert
hash
(
color
)
==
hash
(
terminal
.
Color
(
color
.
rgb
,
None
,
None
,
None
))
Colors
.
register
(
color
.
rgb
)
@
pytest
.
mark
.
parametrize
(
'rgb,hls'
,
[
(
terminal
.
RGB
(
0
,
0
,
0
),
terminal
.
HSL
(
0
,
0
,
0
)),
(
terminal
.
RGB
(
255
,
255
,
255
),
terminal
.
HSL
(
0
,
0
,
100
)),
(
terminal
.
RGB
(
255
,
0
,
0
),
terminal
.
HSL
(
0
,
100
,
50
)),
(
terminal
.
RGB
(
0
,
255
,
0
),
terminal
.
HSL
(
120
,
100
,
50
)),
(
terminal
.
RGB
(
0
,
0
,
255
),
terminal
.
HSL
(
240
,
100
,
50
)),
(
terminal
.
RGB
(
255
,
255
,
0
),
terminal
.
HSL
(
60
,
100
,
50
)),
(
terminal
.
RGB
(
0
,
255
,
255
),
terminal
.
HSL
(
180
,
100
,
50
)),
(
terminal
.
RGB
(
255
,
0
,
255
),
terminal
.
HSL
(
300
,
100
,
50
)),
(
terminal
.
RGB
(
128
,
128
,
128
),
terminal
.
HSL
(
0
,
0
,
50
)),
(
terminal
.
RGB
(
128
,
0
,
0
),
terminal
.
HSL
(
0
,
100
,
25
)),
(
terminal
.
RGB
(
128
,
128
,
0
),
terminal
.
HSL
(
60
,
100
,
25
)),
(
terminal
.
RGB
(
0
,
128
,
0
),
terminal
.
HSL
(
120
,
100
,
25
)),
(
terminal
.
RGB
(
128
,
0
,
128
),
terminal
.
HSL
(
300
,
100
,
25
)),
(
terminal
.
RGB
(
0
,
128
,
128
),
terminal
.
HSL
(
180
,
100
,
25
)),
(
terminal
.
RGB
(
0
,
0
,
128
),
terminal
.
HSL
(
240
,
100
,
25
)),
(
terminal
.
RGB
(
192
,
192
,
192
),
terminal
.
HSL
(
0
,
0
,
75
)),
],
)
def
test_rgb_to_hls
(
rgb
,
hls
)
->
None
:
assert
terminal
.
HSL
.
from_rgb
(
rgb
)
==
hls
@
pytest
.
mark
.
parametrize
(
'text, fg, bg, fg_none, bg_none, percentage, expected'
,
[
(
'test'
,
None
,
None
,
None
,
None
,
None
,
'test'
),
(
'test'
,
None
,
None
,
None
,
None
,
1
,
'test'
),
(
'test'
,
None
,
None
,
None
,
colors
.
red
,
None
,
'
\x1b
[48;5;9mtest
\x1b
[49m'
,
),
(
'test'
,
None
,
colors
.
green
,
None
,
colors
.
red
,
None
,
'
\x1b
[48;5;9mtest
\x1b
[49m'
,
),
(
'test'
,
None
,
colors
.
red
,
None
,
None
,
1
,
'
\x1b
[48;5;9mtest
\x1b
[49m'
),
(
'test'
,
None
,
colors
.
red
,
None
,
None
,
None
,
'test'
),
(
'test'
,
colors
.
green
,
None
,
colors
.
red
,
None
,
None
,
'
\x1b
[38;5;9mtest
\x1b
[39m'
,
),
(
'test'
,
colors
.
green
,
colors
.
red
,
None
,
None
,
1
,
'
\x1b
[48;5;9m
\x1b
[38;5;2mtest
\x1b
[39m
\x1b
[49m'
,
),
(
'test'
,
colors
.
red
,
None
,
None
,
None
,
1
,
'
\x1b
[38;5;9mtest
\x1b
[39m'
),
(
'test'
,
colors
.
red
,
None
,
None
,
None
,
None
,
'test'
),
(
'test'
,
colors
.
red
,
colors
.
red
,
None
,
None
,
None
,
'test'
),
(
'test'
,
colors
.
red
,
colors
.
yellow
,
None
,
None
,
1
,
'
\x1b
[48;5;11m
\x1b
[38;5;9mtest
\x1b
[39m
\x1b
[49m'
,
),
(
'test'
,
colors
.
red
,
colors
.
yellow
,
None
,
None
,
1
,
'
\x1b
[48;5;11m
\x1b
[38;5;9mtest
\x1b
[39m
\x1b
[49m'
,
),
],
)
def
test_apply_colors
(
text
:
str
,
fg
,
bg
,
fg_none
,
bg_none
,
percentage
:
float
|
None
,
expected
,
monkeypatch
,
)
->
None
:
monkeypatch
.
setattr
(
env
,
'COLOR_SUPPORT'
,
env
.
ColorSupport
.
XTERM_256
,
)
assert
(
apply_colors
(
text
,
fg
=
fg
,
bg
=
bg
,
fg_none
=
fg_none
,
bg_none
=
bg_none
,
percentage
=
percentage
,
)
==
expected
)
def
test_windows_colors
(
monkeypatch
)
->
None
:
monkeypatch
.
setattr
(
env
,
'COLOR_SUPPORT'
,
env
.
ColorSupport
.
WINDOWS
)
assert
(
apply_colors
(
'test'
,
fg
=
colors
.
red
,
bg
=
colors
.
red
,
fg_none
=
colors
.
red
,
bg_none
=
colors
.
red
,
percentage
=
1
,
)
==
'test'
)
colors
.
red
.
underline
(
'test'
)
def
test_ansi_color
(
monkeypatch
)
->
None
:
color
=
progressbar
.
terminal
.
Color
(
colors
.
red
.
rgb
,
colors
.
red
.
hls
,
'red-ansi'
,
None
,
)
for
color_support
in
{
env
.
ColorSupport
.
NONE
,
env
.
ColorSupport
.
XTERM
,
env
.
ColorSupport
.
XTERM_256
,
env
.
ColorSupport
.
XTERM_TRUECOLOR
,
}:
monkeypatch
.
setattr
(
env
,
'COLOR_SUPPORT'
,
color_support
,
)
assert
color
.
ansi
is
not
None
or
color_support
==
env
.
ColorSupport
.
NONE
def
test_sgr_call
()
->
None
:
assert
progressbar
.
terminal
.
encircled
(
'test'
)
==
'
\x1b
[52mtest
\x1b
[54m'
Back
|
FazBrowse Home
|
New Git URL