forked from MyIntervals/PHP-CSS-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectorTest.php
More file actions
191 lines (160 loc) · 4.78 KB
/
Copy pathSelectorTest.php
File metadata and controls
191 lines (160 loc) · 4.78 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
<?php
declare(strict_types=1);
namespace Sabberworm\CSS\Tests\Unit\Property;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Property\Selector;
use Sabberworm\CSS\Renderable;
/**
* @covers \Sabberworm\CSS\Property\Selector
*/
final class SelectorTest extends TestCase
{
/**
* @test
*/
public function implementsRenderable(): void
{
$subject = new Selector('a');
self::assertInstanceOf(Renderable::class, $subject);
}
/**
* @test
*/
public function getSelectorByDefaultReturnsSelectorProvidedToConstructor(): void
{
$selector = 'a';
$subject = new Selector($selector);
self::assertSame($selector, $subject->getSelector());
}
/**
* @test
*/
public function setSelectorOverwritesSelectorProvidedToConstructor(): void
{
$subject = new Selector('a');
$selector = 'input';
$subject->setSelector($selector);
self::assertSame($selector, $subject->getSelector());
}
/**
* @return array<string, array{0: non-empty-string, 1: int<0, max>}>
*/
public static function provideSelectorsAndSpecificities(): array
{
return [
'element' => ['a', 1],
'element and descendant with pseudo-selector' => ['ol li::before', 3],
'class' => ['.highlighted', 10],
'element with class' => ['li.green', 11],
'class with pseudo-selector' => ['.help:hover', 20],
'ID' => ['#file', 100],
'ID and descendant class' => ['#test .help', 110],
'`not`' => [':not(#your-mug)', 100],
// TODO, broken: The specificity should be the highest of the `:not` arguments, not the sum.
'`not` with multiple arguments' => [':not(#your-mug, .their-mug)', 110],
];
}
/**
* @test
*
* @param non-empty-string $selector
* @param int<0, max> $expectedSpecificity
*
* @dataProvider provideSelectorsAndSpecificities
*/
public function getSpecificityByDefaultReturnsSpecificityOfSelectorProvidedToConstructor(
string $selector,
int $expectedSpecificity
): void {
$subject = new Selector($selector);
self::assertSame($expectedSpecificity, $subject->getSpecificity());
}
/**
* @test
*
* @param non-empty-string $selector
* @param int<0, max> $expectedSpecificity
*
* @dataProvider provideSelectorsAndSpecificities
*/
public function getSpecificityReturnsSpecificityOfSelectorLastProvidedViaSetSelector(
string $selector,
int $expectedSpecificity
): void {
$subject = new Selector('p');
$subject->setSelector($selector);
self::assertSame($expectedSpecificity, $subject->getSpecificity());
}
/**
* @test
*
* @dataProvider provideSelectorsAndSpecificities
*/
public function isValidForValidSelectorReturnsTrue(string $selector): void
{
self::assertTrue(Selector::isValid($selector));
}
/**
* @return array<string, array{0: string}>
*/
public static function provideInvalidSelectors(): array
{
return [
// This is currently broken.
// 'empty string' => [''],
'percent sign' => ['%'],
// This is currently broken.
// 'hash only' => ['#'],
// This is currently broken.
// 'dot only' => ['.'],
'slash' => ['/'],
'less-than sign' => ['<'],
// This is currently broken.
// 'whitespace only' => [" \t\n\r"],
];
}
/**
* @test
*
* @dataProvider provideInvalidSelectors
*/
public function isValidForInvalidSelectorReturnsFalse(string $selector): void
{
self::assertFalse(Selector::isValid($selector));
}
/**
* @test
*/
public function cleansUpSpacesWithinSelector(): void
{
$selector = 'p > small';
$subject = new Selector($selector);
self::assertSame('p > small', $subject->getSelector());
}
/**
* @test
*/
public function cleansUpTabsWithinSelector(): void
{
$selector = "p\t>\tsmall";
$subject = new Selector($selector);
self::assertSame('p > small', $subject->getSelector());
}
/**
* @test
*/
public function cleansUpNewLineWithinSelector(): void
{
$selector = "p\n>\nsmall";
$subject = new Selector($selector);
self::assertSame('p > small', $subject->getSelector());
}
/**
* @test
*/
public function doesNotCleanupSpacesWithinAttributeSelector(): void
{
$subject = new Selector('a[title="extra space"]');
self::assertSame('a[title="extra space"]', $subject->getSelector());
}
}