-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
243 lines (206 loc) · 6.14 KB
/
Copy pathtest.js
File metadata and controls
243 lines (206 loc) · 6.14 KB
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
import test from 'ava';
import ColorPalette from './index.js';
// --- Helpers ---
function round(value, decimals = 2) {
return Math.round(value * (10 ** decimals)) / (10 ** decimals);
}
function assertColorComponents(t, color, expected, decimals = 2) {
t.is(round(color.red, decimals), expected.red);
t.is(round(color.green, decimals), expected.green);
t.is(round(color.blue, decimals), expected.blue);
if (expected.opacity !== undefined) {
t.is(round(color.opacity, decimals), expected.opacity);
}
}
const {Color} = ColorPalette;
// --- Tests ---
test('color creation and component access', t => {
const color = new Color({
red: 0.5,
green: 0.7,
blue: 0.3,
opacity: 0.8,
name: 'Test',
});
t.is(color.name, 'Test');
t.is(color.opacity, 0.8);
assertColorComponents(t, color, {red: 0.5, green: 0.7, blue: 0.3});
});
test('color creation with linear values', t => {
const color = new Color({
red: 0.5,
green: 0.7,
blue: 0.3,
isLinear: true,
});
const linear = color.linearComponents;
assertColorComponents(t, linear, {red: 0.5, green: 0.7, blue: 0.3});
});
test('color component validation', t => {
t.throws(() => {
// eslint-disable-next-line no-new
new Color({red: 'invalid', green: 0, blue: 0});
}, {message: /must be a number/});
});
test('color opacity handling', t => {
const color = new Color({red: 0, green: 0, blue: 0});
color.opacity = 1.5;
t.is(color.opacity, 1);
color.opacity = -0.5;
t.is(color.opacity, 0);
t.throws(() => {
color.opacity = 'invalid';
}, {message: /must be a number/});
});
test('palette creation and validation', t => {
const red = new Color({red: 1, green: 0, blue: 0});
const green = new Color({red: 0, green: 1, blue: 0});
const palette = new ColorPalette({colors: [red, green], name: 'Test'});
t.is(palette.colors.length, 2);
t.is(palette.name, 'Test');
t.throws(() => {
// eslint-disable-next-line no-new
new ColorPalette({colors: [{}]});
}, {message: /must be an instance of Color/});
});
test('serialization roundtrip', t => {
const original = new ColorPalette({
colors: [
new Color({
red: 1, green: 0, blue: 0, name: 'Red',
}),
new Color({
red: 0, green: 1, blue: 0, opacity: 0.5, name: 'Green',
}),
],
name: 'Test',
});
const serialized = original.serialize();
const deserialized = ColorPalette.deserialize(serialized);
t.is(deserialized.name, original.name);
t.is(deserialized.colors.length, original.colors.length);
for (let i = 0; i < original.colors.length; i++) {
const originalColor = original.colors[i];
const deserializedColor = deserialized.colors[i];
t.is(deserializedColor.name, originalColor.name);
t.is(deserializedColor.opacity, originalColor.opacity);
assertColorComponents(
t,
deserializedColor.linearComponents,
originalColor.linearComponents,
5,
);
}
});
test('deserialization validation', t => {
t.throws(() => {
ColorPalette.deserialize('/');
}, {message: /not valid JSON/});
t.throws(() => {
ColorPalette.deserialize('{}');
}, {message: 'Colors must be an array'});
t.throws(() => {
ColorPalette.deserialize(JSON.stringify({
colors: [{components: 'invalid'}],
}));
}, {message: 'Components must be an array'});
t.throws(() => {
ColorPalette.deserialize(JSON.stringify({
colors: [{components: [1, 2]}],
}));
}, {message: 'Components must have 3 or 4 values'});
t.throws(() => {
ColorPalette.deserialize(JSON.stringify({
colors: [{components: [-1, 0, 0]}],
}));
}, {message: 'Component values must be numbers'});
});
test('color component modification', t => {
const color = new Color({red: 0.5, green: 0.5, blue: 0.5});
color.red = 1;
color.green = 0;
color.blue = 0;
assertColorComponents(t, color, {red: 1, green: 0, blue: 0});
t.throws(() => {
color.green = 'invalid';
}, {message: /number/});
});
test('precision rounding', t => {
const color = new Color({
red: 0.123_456,
green: 0.123_45,
blue: 0.123_444,
opacity: 0.123_449,
isLinear: true,
});
const linear = color.linearComponents;
t.is(linear.red, 0.123_46);
t.is(linear.green, 0.123_45);
t.is(linear.blue, 0.123_44);
t.is(linear.opacity, 0.123_45);
});
test('precision roundtrip', t => {
const color = new Color({
red: 0.123_456,
green: 0.123_45,
blue: 0.123_444,
opacity: 0.123_449,
isLinear: true,
});
// eslint-disable-next-line unicorn/prefer-structured-clone
const parsed = JSON.parse(JSON.stringify(color));
t.deepEqual(parsed.components, [0.123_46, 0.123_45, 0.123_44, 0.123_45]);
});
// --- Parameterized hex string and number tests ---
const hexStringCases = [
{hexString: '#FF0000', expected: {red: 1, green: 0, blue: 0}},
{hexString: 'F00', expected: {red: 1, green: 0, blue: 0}},
{
hexString: '#FF000080', expected: {
red: 1, green: 0, blue: 0, opacity: 0.5,
},
},
{
hexString: 'F008', expected: {
red: 1, green: 0, blue: 0, opacity: 0.53,
},
},
];
for (const {hexString, expected} of hexStringCases) {
test(`hex string initialization: ${hexString}`, t => {
const color = Color.fromHexString(hexString);
assertColorComponents(t, color, expected);
});
}
test('hex string initialization: invalid formats', t => {
const invalidHexStrings = ['', '#', '#F', '#FF', '#FFFFF', '#FFFFFFF', '#GG0000'];
for (const invalidHexString of invalidHexStrings) {
t.throws(() => {
Color.fromHexString(invalidHexString);
}, {message: /Invalid hex color format/});
}
});
const hexNumberCases = [
{hexNumber: 0xFF_00_00, expected: {red: 1, green: 0, blue: 0}},
{hexNumber: 0xF_00, expected: {red: 1, green: 0, blue: 0}},
{hexNumber: 0x12_34_56, expected: {red: 0.0706, green: 0.2039, blue: 0.3373}, decimals: 4},
{
hexNumber: 0xFF_00_00_80, expected: {
red: 1, green: 0, blue: 0, opacity: 0.502,
}, decimals: 4,
},
];
for (const {hexNumber, expected, decimals = 2} of hexNumberCases) {
test(`hex number initialization: ${hexNumber.toString(16)}`, t => {
const color = Color.fromHexNumber(hexNumber);
assertColorComponents(t, color, expected, decimals);
});
}
test('hex number initialization: invalid values', t => {
t.throws(() => {
Color.fromHexNumber(-1);
}, {message: /Invalid hex value/});
t.throws(() => {
Color.fromHexNumber(0x1_00_00_00_00);
}, {message: /Invalid hex value/});
});