Debian Codegraph @

tests/parser.rs

raw symbols SCIP
1 use csscolorparserrust-analyzer cargo csscolorparser 0.8.3 crate/::{parserust-analyzer cargo csscolorparser 0.8.3 parser/parse()., Colorrust-analyzer cargo csscolorparser 0.8.3 color/Color#};
2
3 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
4 fn parserrust-analyzer cargo csscolorparser 0.8.3 parser().() {
5 let test_data = [
6 ("transparent", [0, 0, 0, 0]),
7 ("TRANSPARENT", [0, 0, 0, 0]),
8 ("#ff00ff64", [255, 0, 255, 100]),
9 ("ff00ff64", [255, 0, 255, 100]),
10 ("rgb(247,179,99)", [247, 179, 99, 255]),
11 ("rgb(50% 50% 50%)", [128, 128, 128, 255]),
12 ("rgb(247,179,99,0.37)", [247, 179, 99, 94]),
13 ("hsl(270 0% 50%)", [128, 128, 128, 255]),
14 ("hwb(0 50% 50%)", [128, 128, 128, 255]),
15 ("hsv(0 0% 50%)", [128, 128, 128, 255]),
16 ("hsv(0 0% 100%)", [255, 255, 255, 255]),
17 ("hsv(0 0% 19%)", [48, 48, 48, 255]),
18 ];
19
20 for (s, expected) in test_data {
21 let a = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(s).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().();
22 let b = s.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<Colorrust-analyzer cargo csscolorparser 0.8.3 color/Color#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().();
23 let c = Colorrust-analyzer cargo csscolorparser 0.8.3 color/Color#::from_htmlrust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]from_html().(s).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().();
24 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, a);
25 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, b);
26 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, c);
27 }
28
29 let test_data = [
30 ("lab(0% 0 0)", [0, 0, 0, 255]),
31 ("lab(100% 0 0)", [255, 255, 255, 255]),
32 ("lab(0% 0 0 / 0.5)", [0, 0, 0, 128]),
33 ("lch(0% 0 0)", [0, 0, 0, 255]),
34 ("lch(100% 0 0)", [255, 255, 255, 255]),
35 ("lch(0% 0 0 / 0.5)", [0, 0, 0, 128]),
36 ];
37
38 for (s, expected) in test_data {
39 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(s).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().());
40 }
41 }
42
43 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
44 fn equalrust-analyzer cargo csscolorparser 0.8.3 equal().() {
45 let test_data = [
46 ("transparent", "rgb(0,0,0,0%)"),
47 ("#FF9900", "#f90"),
48 ("#aabbccdd", "#ABCD"),
49 ("#BAD455", "BAD455"),
50 ("rgb(0 255 127 / 75%)", "rgb(0,255,127,0.75)"),
51 ("hwb(180 0% 60%)", "hwb(180,0%,60%)"),
52 ("hwb(290 30% 0%)", "hwb(290 0.3 0)"),
53 ("hsl(180,50%,27%)", "hsl(180,0.5,0.27)"),
54 ("rgb(255, 165, 0)", "hsl(38.824 100% 50%)"),
55 ("#7654CD", "rgb(46.27% 32.94% 80.39%)"),
56 //#[cfg(feature = "lab")]
57 //("#7654CD", "lab(44.36% 36.05 -58.99)"),
58 ];
59
60 for (a, b) in test_data {
61 let c1 = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(a).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
62 let c2 = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(b).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
63 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(c1.to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().(), c2.to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().(), "{:?}", [a, b]);
64 }
65 }
66
67 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
68 fn blackrust-analyzer cargo csscolorparser 0.8.3 black().() {
69 let data = [
70 "#000",
71 "#000f",
72 "#000000",
73 "#000000ff",
74 "000",
75 "000f",
76 "000000",
77 "000000ff",
78 "rgb(0,0,0)",
79 "rgb(0% 0% 0%)",
80 "rgb(0 0 0 100%)",
81 "hsl(270,100%,0%)",
82 "hwb(90 0% 100%)",
83 "hwb(120deg 0% 100% 100%)",
84 "hsv(120 100% 0%)",
85 "oklab(0 0 0)",
86 "oklch(0 0 180)",
87 ];
88
89 let black = [0, 0, 0, 255];
90
91 for s in data {
92 let c = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(s).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().();
93 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(black, c);
94 }
95 }
96
97 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
98 fn redrust-analyzer cargo csscolorparser 0.8.3 red().() {
99 let data = [
100 "#f00",
101 "#f00f",
102 "#ff0000",
103 "#ff0000ff",
104 "f00",
105 "f00f",
106 "ff0000",
107 "ff0000ff",
108 "rgb(255,0,0)",
109 "rgb(255 0 0)",
110 "rgb(700, -99, 0)", // clamp to 0..255
111 "rgb(100% 0% 0%)",
112 "rgb(200% -10% -100%)", // clamp to 0%..100%
113 "rgb(255 0 0 100%)",
114 " RGB ( 255 , 0 , 0 ) ",
115 "RGB( 255 0 0 )",
116 "hsl(0,100%,50%)",
117 "hsl(360 100% 50%)",
118 "hwb(0 0% 0%)",
119 "hwb(360deg 0% 0% 100%)",
120 "hwb(360DEG 0% 0% 100%)",
121 "hsv(0 100% 100%)",
122 "oklab(0.62796, 0.22486, 0.12585)",
123 "oklch(0.62796, 0.25768, 29.23388)",
124 ];
125
126 let red = [255, 0, 0, 255];
127
128 for s in data {
129 let res = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(s);
130 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(res.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().(), "{:?}", s);
131 let c = res.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().();
132 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(red, c);
133 }
134 }
135
136 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
137 fn limerust-analyzer cargo csscolorparser 0.8.3 lime().() {
138 let data = [
139 "#0f0",
140 "#0f0f",
141 "#00ff00",
142 "#00ff00ff",
143 "0f0",
144 "0f0f",
145 "00ff00",
146 "00ff00ff",
147 "rgb(0,255,0)",
148 "rgb(0% 100% 0%)",
149 "rgb(0 255 0 / 100%)",
150 "rgba(0,255,0,1)",
151 "hsl(120,100%,50%)",
152 "hsl(120deg 100% 50%)",
153 "hsl(-240 100% 50%)",
154 "hsl(-240deg 100% 50%)",
155 "hsl(0.3333turn 100% 50%)",
156 "hsl(0.3333TURN 100% 50%)",
157 "hsl(133.333grad 100% 50%)",
158 "hsl(133.333GRAD 100% 50%)",
159 "hsl(2.0944rad 100% 50%)",
160 "hsl(2.0944RAD 100% 50%)",
161 "hsla(120,100%,50%,100%)",
162 "hwb(120 0% 0%)",
163 "hwb(480deg 0% 0% / 100%)",
164 "hsv(120 100% 100%)",
165 "oklab(0.86644, -0.23389, 0.1795)",
166 "oklch(0.86644, 0.29483, 142.49535)",
167 ];
168
169 let lime = [0, 255, 0, 255];
170
171 for s in data {
172 let res = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(s);
173 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(res.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().(), "{:?}", s);
174 let c = res.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().();
175 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(lime, c);
176 }
177 }
178
179 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
180 fn lime_alpharust-analyzer cargo csscolorparser 0.8.3 lime_alpha().() {
181 let data = [
182 "#00ff0080",
183 "00ff0080",
184 "rgb(0,255,0,50%)",
185 "rgb(0% 100% 0% / 0.5)",
186 "rgba(0%,100%,0%,50%)",
187 "hsl(120,100%,50%,0.5)",
188 "hsl(120deg 100% 50% / 50%)",
189 "hsla(120,100%,50%,0.5)",
190 "hwb(120 0% 0% / 50%)",
191 "hsv(120 100% 100% / 50%)",
192 ];
193
194 let lime_alpha = [0, 255, 0, 128];
195
196 for s in data {
197 let c = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(s).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_rgba8rust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_rgba8().();
198 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(lime_alpha, c);
199 }
200 }
201
202 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
203 fn none_valuerust-analyzer cargo csscolorparser 0.8.3 none_value().() {
204 let test_data = [
205 ["rgb(none none none)", "#000000"],
206 ["hwb(none none none)", "#ff0000"],
207 ["hsl(none none none)", "#000000"],
208 ["lab(none none none)", "#000000"],
209 ["lch(none none none)", "#000000"],
210 ["oklab(none none none)", "#000000"],
211 ["oklch(none none none)", "#000000"],
212 ["rgb(255 0 255 / none)", "#ff00ff00"],
213 ["rgb(90 none 210)", "#5a00d2"],
214 ["rgb(255 none 50%)", "#ff0080"],
215 ["rgb(none 50% 127)", "#00807f"],
216 ["rgb(90 35% none)", "#5a5900"],
217 ["hwb(none 60% 0%)", "#ff9999"],
218 ["hwb(137 55% none)", "#8cffad"],
219 ["hsl(none 45% 55%)", "#c05959"],
220 ["lab(50% -75 none)", "#009275"],
221 ["lch(50% 100 none)", "#ff007b"], // xxx
222 ["oklab(60% none -0.17)", "#5a76e4"],
223 ["oklch(60% 0.2 none)", "#d7397b"],
224 ];
225 for [s, hex] in test_data {
226 let c = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(s);
227 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(c.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().(), "{:?}", s);
228 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(c.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_css_hexrust-analyzer cargo csscolorparser 0.8.3 color/impl#[Color]to_css_hex().(), hex, "{:?}", s);
229 }
230 }
231
232 #[cfg(feature = "named-colors")]
233 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
234 fn invalid_formatrust-analyzer cargo csscolorparser 0.8.3 invalid_format().() {
235 let test_data = [
236 "",
237 "bloodred",
238 "#78afzd",
239 "#fffff",
240 "rgb(255,0,0",
241 "rgb(0,255,8s)",
242 "rgb(100%,z9%,75%)",
243 //"rgb(255,0,0%)", // mix format
244 //"rgb(70%,30%,0)", // mix format
245 "rgb(255 0 0 0 0)",
246 "rgb(255 0 0 0 / 0)",
247 "rgb(,255,0,0)",
248 "rgb(255,0,,0)",
249 "rgb(255,0,0,)",
250 "rgb(255,0,0/)",
251 "rgb(255/0,0)",
252 "cmyk(1 0 0)",
253 "rgba(0 0)",
254 "hsl(90',100%,50%)",
255 "hsl(360,70%,50%,90%,100%)",
256 "hsl(deg 100% 50%)",
257 "hsl(Xturn 100% 50%)",
258 "hsl(Zgrad 100% 50%)",
259 "hsl(180 1 x%)",
260 //"hsl(360,0%,0)", // mix format
261 "hsla(360)",
262 "hwb(Xrad,50%,50%)",
263 "hwb(270 0% 0% 0% 0%)",
264 //"hwb(360,0,20%)", // mix format
265 "hsv(120 100% 100% 1 50%)",
266 "hsv(120 XXX 100%)",
267 //"hsv(120,100%,0.5)", //mix format
268 "lab(100%,0)",
269 "lab(100% 0 X)",
270 "lch(100%,0)",
271 "lch(100% 0 X)",
272 "oklab(0,0)",
273 "oklab(0,0,x,0)",
274 "oklch(0,0,0,0,0)",
275 "oklch(0,0,0,x)",
276 "æ",
277 "#ß",
278 "rgb(ß,0,0)",
279 "\u{1F602}",
280 "#\u{1F602}",
281 "rgb(\u{1F602},\u{1F602},\u{1F602})",
282 ];
283
284 for s in test_data {
285 let c = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(s);
286 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(c.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().(), "{:?}", s);
287 }
288
289 #[rustfmt::skip]
290 let test_data = [
291 ("#78afzd", "invalid hex format"),
292 ("rgb(xx,yy,xx)", "invalid rgb format"),
293 ("rgb(255,0)", "invalid rgb format"),
294 ("hsl(0,100%,2o%)", "invalid hsl format"),
295 ("hsv(360)", "invalid hsv format"),
296 ("hwb(270,0%,0%,x)", "invalid hwb format"),
297 ("lab(0%)", "invalid lab format"),
298 ("lch(0%)", "invalid lch format"),
299 ("oklab(9 20)", "invalid oklab format"),
300 ("oklch()", "invalid oklch format"),
301 ("cmyk(0,0,0,0)", "invalid color function"),
302 ("blood", "invalid unknown format"),
303 ("rgb(255,0,0", "invalid unknown format"),
304 ("x£", "invalid unknown format"),
305 ("x£x", "invalid unknown format"),
306 ("xxx£x", "invalid unknown format"),
307 ("xxxxx£x", "invalid unknown format"),
308 ("\u{1F602}", "invalid unknown format"),
309 ];
310
311 for (s, err_msg) in test_data {
312 let c = parserust-analyzer cargo csscolorparser 0.8.3 parser/parse().(s);
313 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(c.unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), err_msg, "{:?}", s);
314 }
315 }