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
|
#!/usr/bin/env python3
# *****************************************************************************
# Copyright (C) 2019-2021 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the thcolor project, which is MIT-licensed.
# *****************************************************************************
""" Unit tests for the thcolor color decoding and management module. """
import pytest
from thcolor.angles import * # NOQA
from thcolor.colors import * # NOQA
from thcolor.decoders import * # NOQA
from thcolor.errors import * # NOQA
class TestBaseDecoder:
@pytest.fixture
def decoder(self):
class StrictDecoder(MetaColorDecoder):
__ncol_support__ = False
__defaults_to_netscape_color__ = False
B20 = 12.34
heLLo = 56.78
return StrictDecoder()
@pytest.mark.parametrize('test_input,expected', (
('1 / 2 , 3', (
1, 2, 3.0,
)),
('20deg 4turn 3,, 3. 3.0 .2 50%', (
DegreesAngle(20), TurnsAngle(4), 3, None, 3.0, 3.0, 0.2, 0.5,
)),
('#12345F', (
SRGBColor.frombytes(18, 52, 95, 1.00),
)),
))
def test_syntax(self, decoder, test_input, expected):
""" Test basic syntax.
Tests syntaxes that are always available, with separators.
"""
result = decoder.decode(test_input)
assert result == expected
@pytest.mark.parametrize('test_input,expected', (
('20.5, 20 / 19.5deg', (
DegreesAngle(20.5), DegreesAngle(20), DegreesAngle(19.5),
)),
))
def test_coersce_into_angles(self, decoder, test_input, expected):
""" Test the decoding while indicating that we want angles. """
result = decoder.decode(test_input, prefer_angles=True)
assert result == expected
def test_existing_variable(self, decoder):
""" Test getting an existing variable. """
result = decoder.decode('hello')
assert result == (56.78,)
def test_non_existing_variable(self, decoder):
""" Test getting a non-existing variable. """
with pytest.raises(ColorExpressionSyntaxError, match=r'unknown value'):
decoder.decode('nonexisting')
def test_existing_variable_ncol_format(self, decoder):
""" Test decoding an expression using an NCol-like variable.
We want to avoid the NCol subsytem from shadowing
existing variables when NCol support is disabled.
"""
result = decoder.decode('B20')
assert result == (12.34,)
def test_non_existing_variable_ncol_format(self, decoder):
""" Test decoding an expression using an NCol-like variable.
We want to avoid the NCol subsystem intercepting errors
in case of non-existing variables.
"""
with pytest.raises(ColorExpressionSyntaxError, match=r'unknown value'):
decoder.decode('Y40')
class TestNColDecoder:
""" Test base decoder with ncol support. """
@pytest.fixture
def decoder(self):
class Decoder(MetaColorDecoder):
__ncol_support__ = True
__defaults_to_netscape_color__ = False
return Decoder()
@pytest.mark.parametrize('test_input,expected', (
(
'B20 / 50% 32%',
HWBColor(DegreesAngle(252), .5, .32, 1.00),
),
(
'Y40, 33%, 55%',
HWBColor(DegreesAngle(84), 0.33, 0.55, 1.00),
),
))
def test_ncol(self, decoder, test_input, expected):
""" Test natural colors. """
result = decoder.decode(test_input)
assert result == (expected,)
class TestNetscapeDecoder:
""" Test base decoder with netscape color support. """
@pytest.fixture
def decoder(self):
class Decoder(MetaColorDecoder):
__ncol_support__ = False
__defaults_to_netscape_color__ = True
return Decoder()
@pytest.mark.parametrize('test_input,expected', (
('chucknorris', SRGBColor.frombytes(192, 0, 0, 1.00)),
))
def test_netscape_colors(self, decoder, test_input, expected):
""" Test decoding using Netscape colors. """
result = decoder.decode(test_input, prefer_colors=True)
assert result == (expected,)
@pytest.mark.parametrize('test_input,expected', (
('#123', SRGBColor.frombytes(17, 34, 51, 1.00)),
('123', SRGBColor.frombytes(1, 2, 3, 1.00)),
('123.0', SRGBColor.frombytes(18, 48, 0, 1.00)),
))
def test_coersce_into_colors(self, decoder, test_input, expected):
""" Test the decoding while indicating that we want colors.
It is expected from the decoder to use Netscape color name
style decoding in this scenario.
"""
result = decoder.decode(test_input, prefer_colors=True)
assert result == (expected,)
class TestDefaultDecoder:
""" Test the default decoder with all features. """
@pytest.fixture
def decoder(self):
return DefaultColorDecoder()
@pytest.mark.parametrize('test_input,expected', (
('blue', SRGBColor.frombytes(0, 0, 255, 1.00)),
('rgb(1, 22,242)', SRGBColor.frombytes(1, 22, 242, 1.00)),
(' rgb (1,22, 242 , 50.0% )', SRGBColor.frombytes(1, 22, 242, 0.50)),
(' rgb (1 22/ 242,50.0%,/)', SRGBColor.frombytes(1, 22, 242, 0.50)),
('rgba(1,22,242,0.500)', SRGBColor.frombytes(1, 22, 242, 0.50)),
('rbga(5, 7)', SRGBColor.frombytes(5, 0, 7, 1.00)),
('hsl(0, 1,50.0%)', HSLColor(DegreesAngle(0.0), 1.0, 0.5, 1.00)),
(
'hls(0 / 1 0.5 , 0.2)',
HSLColor(DegreesAngle(0.0), 0.5, 1.0, 0.20),
),
('hwb(0 0% 0)', HWBColor(DegreesAngle(0), 0.0, 0.0, 1.00)),
('hbw(127 .5)', HWBColor(DegreesAngle(127), 0.0, 0.5)),
('gray(100)', SRGBColor.frombytes(100, 100, 100, 1.00)),
('gray(100 / 55 %)', SRGBColor.frombytes(100, 100, 100, 0.55)),
('gray(red( #123456 )/0.2/)', SRGBColor.frombytes(18, 18, 18, 0.20)),
('B20 50% 32%', HWBColor(DegreesAngle(252), .5, .32, 1.00)),
('ncol(B20 / 50% 32%)', HWBColor(DegreesAngle(252), .5, .32, 1.00)),
('cmyk(0% 37% 0.13 .78)', CMYKColor(0, .37, .13, .78, 1.00)),
(
'darker(10%, hsl(0, 1, 50.0%))',
HSLColor(DegreesAngle(0), 1.00, 0.40, 1.00),
),
(
'lighter(50%, hsl(0, 1, 60.0%))',
HSLColor(DegreesAngle(0), 1.00, 1.00, 1.00),
),
(
'saturate(10%, hls(0, 1, 85.0%))',
HSLColor(DegreesAngle(0), 0.95, 1.00, 1.00),
),
(
'desaturate(10%, hls(0turn, 1, 5%, 0.2))',
HSLColor(DegreesAngle(0), 0.00, 1.00, 0.20),
),
('rgba(255, 0, 0, 20 %)', HSLColor(DegreesAngle(0), 1.00, 0.50, 0.20)),
(
'Y40, 33%, 55%',
HWBColor(DegreesAngle(84), 0.33, 0.55, 1.00),
),
('cmyk(0% 37% 0.13 .78)', CMYKColor(0.00, 0.37, 0.13, 0.78, 1.00)),
))
def test_decoding_colors(self, decoder, test_input, expected):
result = decoder.decode(test_input, prefer_colors=True)
assert result[0] == expected
# End of file.
|