-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
214 lines (167 loc) · 5.02 KB
/
test.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
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
from prev_gen import (
Config, Reverser, Previewer, Palette, Settings, Color
)
from prev_gen.util import Raises, Filters
from os.path import exists
from math import isclose
from os import remove
def test_helper_raises():
r = Raises(ValueError)
with r:
int('1')
assert not r.raised
with r:
int('raise')
assert r.raised
# a non-specified error should still be raised
try:
with r:
_ = int.__dict__['wrong']
assert False
except KeyError:
assert True
def test_input_modes():
"""simply should not raise an error"""
Color('f00')
Color('crimson')
Color((1., 0., 0.))
Color((1., 0., 0.), model='hsl')
def test_invalid_input():
r = Raises(ValueError, NotImplementedError)
# tuple value not long enough
with r:
Color((0, 0))
assert r.raised
# not implemented for dict
with r:
Color({})
assert r.raised
def test_color_can_be_compared():
assert Color('crimson') == 'crimson'
assert Color('crimson') == 'dc143c'
assert Color('crimson') != Color('beige')
assert Color('beige') != 'invalidColor'
def test_color_modes_work():
c = (0.2, 0.5, 0.3)
assert (
Color(c, model='rgb')
!= Color(c, model='hsl')
!= Color(c, model='hsv')
!= Color(c, model='cmy')
)
def test_hex_input_types():
assert (
Color('0000ff')
== Color('#0000fF')
)
def test_hex_ignores_mode():
c = 'f00'
assert (
Color(c, model='rgb').rgb
== Color(c, model='hsv').rgb
== Color(c, model='hsl').rgb
== Color(c, model='cmyk').rgb
)
def test_colors_infer_properties():
c = Color('ff0000', alpha=0.93)
assert c.hexadecimal == '#ff0000'
assert c.rgb == [1., 0., 0.]
assert c.hsv == [0., 1., 1.]
assert c.hsl == [0., 1., 0.5]
assert c.dark is False
assert all(isclose(x, y, rel_tol=0.001) for x, y in zip(c.oklch, (0.6279, 0.2577, 0.0812)))
def test_color_serializable():
c = Color('000000', 'name', 'descLeft', 'descRight')
des = Color('000000').deserialize_text(c.serialize_text())
assert c.name == des.name
assert c.desc_left == des.desc_left
assert c.desc_right == des.desc_right
def test_settings_serialize():
assert Settings().serialize() == 'gAR9lC4='
s = Settings(file_name='nonDefault')
assert Settings.deserialize(s.serialize()) == s
def test_table_init():
c = [Color('f00') for _ in range(5)]
p = Palette(c)
assert p.colors == c
assert p.settings == Settings()
# the size is calculated as the rectangle closest to a square whose width >= height
assert p.width == 3
assert p.height == 2
def test_table_iterable():
c = [Color('ff0000') for _ in range(3)]
c.insert(2, Color('000000', alpha=0.))
p = Palette(c)
it = 0
for _ in p:
it += 1
assert it == 4
def test_generate_png():
assert str(type(Previewer([Color('f00')], show=False))) == '<class \'PIL.Image.Image\'>'
def test_save_png():
Previewer([Settings(file_name='testSavePNG'), Color('f00')], show=False, save=True)
assert exists('testSavePNG.png')
remove('testSavePNG.png')
def test_reverse_png():
a = Previewer([Settings(file_name='testReversePNG'), Color('f00')], show=False, save=True)
Reverser('testReversePNG.png')
remove('testReversePNG.png')
Reverser(a)
def test_generate_svg():
assert str(type(Previewer([Color('f00')], show=False, output='svg'))) == (
'<class \'xml.etree.ElementTree.ElementTree\'>'
)
def test_save_svg():
Previewer([Settings(file_name='testSaveSVG'), Color('f00')], show=False, save=True, output='svg')
assert exists('testSaveSVG.svg')
remove('testSaveSVG.svg')
def test_reverse_svg():
a = Previewer([Settings(file_name='testReverseSVG'), Color('f00')], show=False, save=True, output='svg')
Reverser('testReverseSVG.svg')
remove('testReverseSVG.svg')
Reverser(a)
def test_yaml():
c = """
palette:
- - color: '#000000'
""".removeprefix('\n')
assert str(Config.read(c, output='yml')) == str(Config([[Color('000000')]], output='yml')) == c
def test_json():
c = """
{
"palette": [
[
{
"color": "#000000"
}
]
]
}
""".removeprefix('\n')
assert str(Config.read(c, output='json')) == str(Config([[Color('000000')]], output='json')) == c
def test_toml():
c = """
palette = [
[
{ color = "#000000" },
],
]
""".removeprefix('\n')
assert str(Config.read(c, output='toml')) == str(Config([[Color('000000')]], output='toml')) == c
def test_python():
c = """
palette = [
[
{'color': '#000000'},
],
]
if __name__ == '__main__':
from prev_gen import Previewer
Previewer(palette)
""".removeprefix('\n')
assert str(Config.read(c, output='py')) == str(Config([[Color('000000')]], output='py')) == c
def test_filter_monochrome():
assert Filters(Previewer([
Settings(grid_width=3, grid_height=3, bar_height=1),
Color('ff0000')
], show=False)).monochrome().img.getpixel((0, 0)) == (62, 62, 62, 255)