Skip to content

Commit 4d33faf

Browse files
authored
[TASK] Add TestCase for Value\Color (#793)
This covers expected behaviour for CSS level 3 (some of which is also covered by more general tests). Also included are some commented-out tests for CSS Color Module Level 4, support for which is yet to be implemented, as well as for some syntaxes that should be rejected (but currently are not). Precursor to resolving #755 and supporting CSS Color Module Level 4. Co-authored-by: Jake Hotson <[email protected]>
1 parent 49ddae6 commit 4d33faf

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

tests/Unit/Value/ColorTest.php

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit\Value;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sabberworm\CSS\Parsing\ParserState;
9+
use Sabberworm\CSS\Parsing\SourceException;
10+
use Sabberworm\CSS\Settings;
11+
use Sabberworm\CSS\Value\Color;
12+
13+
/**
14+
* Note: some test data is currently commented-out.
15+
* These cover
16+
* - CSS Color Module Level 4 syntaxes that are not yet supported;
17+
* - Some invalid syntaxes that should be rejected but currently are not.
18+
*
19+
* @covers \Sabberworm\CSS\Value\Color
20+
*/
21+
final class ColorTest extends TestCase
22+
{
23+
/**
24+
* @return array<string, array{0: string, 1: string}>
25+
*/
26+
public static function provideValidColorAndExpectedRendering(): array
27+
{
28+
return [
29+
'3-digit hex color' => [
30+
'#070',
31+
'#070',
32+
],
33+
'6-digit hex color that can be represented as 3-digit' => [
34+
'#007700',
35+
'#070',
36+
],
37+
'6-digit hex color that cannot be represented as 3-digit' => [
38+
'#007600',
39+
'#007600',
40+
],
41+
'4-digit hex color (with alpha)' => [
42+
'#0707',
43+
'rgba(0,119,0,.47)',
44+
],
45+
'8-digit hex color (with alpha)' => [
46+
'#0077007F',
47+
'rgba(0,119,0,.5)',
48+
],
49+
'legacy rgb that can be represented as 3-digit hex' => [
50+
'rgb(0, 119, 0)',
51+
'#070',
52+
],
53+
'legacy rgb that cannot be represented as 3-digit hex' => [
54+
'rgb(0, 118, 0)',
55+
'#007600',
56+
],
57+
'legacy rgba with fractional alpha' => [
58+
'rgba(0, 119, 0, 0.5)',
59+
'rgba(0,119,0,.5)',
60+
],
61+
'legacy rgba with percentage alpha' => [
62+
'rgba(0, 119, 0, 50%)',
63+
'rgba(0,119,0,50%)',
64+
],
65+
/*
66+
'legacy rgb as rgba' => [
67+
'rgba(0, 119, 0)',
68+
'rgb(0,119,0)',
69+
],
70+
'legacy rgba as rgb' => [
71+
'rgb(0, 119, 0, 0.5)',
72+
'rgba(0,119,0,.5)',
73+
],
74+
'modern rgb' => [
75+
'rgb(0 119 0)',
76+
'rgb(0,119,0)',
77+
],
78+
'modern rgb with none' => [
79+
'rgb(none 119 0)',
80+
'rgb(none 119 0)',
81+
],
82+
'modern rgba' => [
83+
'rgb(0 119 0 / 0.5)',
84+
'rgba(0,119,0,.5)',
85+
],
86+
//*/
87+
'legacy hsl' => [
88+
'hsl(120, 100%, 25%)',
89+
'hsl(120,100%,25%)',
90+
],
91+
'legacy hsl with deg' => [
92+
'hsl(120deg, 100%, 25%)',
93+
'hsl(120deg,100%,25%)',
94+
],
95+
'legacy hsl with grad' => [
96+
'hsl(133grad, 100%, 25%)',
97+
'hsl(133grad,100%,25%)',
98+
],
99+
'legacy hsl with rad' => [
100+
'hsl(2.094rad, 100%, 25%)',
101+
'hsl(2.094rad,100%,25%)',
102+
],
103+
'legacy hsl with turn' => [
104+
'hsl(0.333turn, 100%, 25%)',
105+
'hsl(.333turn,100%,25%)',
106+
],
107+
'legacy hsla with fractional alpha' => [
108+
'hsla(120, 100%, 25%, 0.5)',
109+
'hsla(120,100%,25%,.5)',
110+
],
111+
'legacy hsla with percentage alpha' => [
112+
'hsla(120, 100%, 25%, 50%)',
113+
'hsla(120,100%,25%,50%)',
114+
],
115+
/*
116+
'legacy hsl as hsla' => [
117+
'hsla(120, 100%, 25%)',
118+
'hsl(120,100%,25%)',
119+
],
120+
'legacy hsla as hsl' => [
121+
'hsl(120, 100%, 25%, 0.5)',
122+
'hsla(120,100%,25%,0.5)',
123+
],
124+
//*/
125+
];
126+
}
127+
128+
/**
129+
* @test
130+
*
131+
* @dataProvider provideValidColorAndExpectedRendering
132+
*/
133+
public function parsesAndRendersValidColor(string $color, string $expectedRendering): void
134+
{
135+
$subject = Color::parse(new ParserState($color, Settings::create()));
136+
137+
$renderedResult = (string) $subject;
138+
139+
self::assertSame($expectedRendering, $renderedResult);
140+
}
141+
142+
/**
143+
* Browsers reject all these, thus so should the parser.
144+
*
145+
* @return array<string, array{0: string}>
146+
*/
147+
public static function provideInvalidColor(): array
148+
{
149+
return [
150+
'hex color with 0 digits' => [
151+
'#',
152+
],
153+
'hex color with 1 digit' => [
154+
'#f',
155+
],
156+
'hex color with 2 digits' => [
157+
'#f0',
158+
],
159+
'hex color with 5 digits' => [
160+
'#ff000',
161+
],
162+
'hex color with 7 digits' => [
163+
'#ff00000',
164+
],
165+
'hex color with 9 digits' => [
166+
'#ff0000000',
167+
],
168+
'rgb color with 0 arguments' => [
169+
'rgb()',
170+
],
171+
'rgb color with 1 argument' => [
172+
'rgb(255)',
173+
],
174+
'legacy rgb color with 2 arguments' => [
175+
'rgb(255, 0)',
176+
],
177+
'legacy rgb color with 5 arguments' => [
178+
'rgb(255, 0, 0, 0.5, 0)',
179+
],
180+
/*
181+
'legacy rgb color with invalid unit' => [
182+
'rgb(255, 0px, 0)',
183+
],
184+
//*/
185+
'hsl color with 0 arguments' => [
186+
'hsl()',
187+
],
188+
'hsl color with 1 argument' => [
189+
'hsl(0)',
190+
],
191+
'legacy hsl color with 2 arguments' => [
192+
'hsl(0, 100%)',
193+
],
194+
'legacy hsl color with 5 arguments' => [
195+
'hsl(0, 100%, 50%, 0.5, 0)',
196+
],
197+
/*
198+
'legacy hsl color without % for S/L units' => [
199+
'hsl(0, 1, 0.5)'
200+
],
201+
'legacy hsl color with invalid unit for H' => [
202+
'hsl(0px, 100%, 50%)'
203+
],
204+
//*/
205+
];
206+
}
207+
208+
/**
209+
* @test
210+
*
211+
* @dataProvider provideInvalidColor
212+
*/
213+
public function throwsExceptionWithInvalidColor(string $color): void
214+
{
215+
$this->expectException(SourceException::class);
216+
217+
Color::parse(new ParserState($color, Settings::create()));
218+
}
219+
}

0 commit comments

Comments
 (0)