Skip to content

Commit 9bc6622

Browse files
authored
[TASK] Add tests for RuleSet::addRule() without sibling argument (#1261)
Some are currently skipped, pending some minor bug fixes.
1 parent 042515c commit 9bc6622

File tree

1 file changed

+135
-14
lines changed

1 file changed

+135
-14
lines changed

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 135 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Sabberworm\CSS\Rule\Rule;
1111
use Sabberworm\CSS\RuleSet\RuleContainer;
1212
use Sabberworm\CSS\Tests\Unit\RuleSet\Fixtures\ConcreteRuleSet;
13+
use TRegx\PhpUnit\DataProviders\DataProvider;
1314

1415
/**
1516
* @covers \Sabberworm\CSS\RuleSet\RuleSet
@@ -50,6 +51,139 @@ public function implementsRuleContainer(): void
5051
self::assertInstanceOf(RuleContainer::class, $this->subject);
5152
}
5253

54+
/**
55+
* @return array<string, array{0: list<non-empty-string>}>
56+
*/
57+
public static function providePropertyNamesToBeSetInitially(): array
58+
{
59+
return [
60+
'no properties' => [[]],
61+
'one property' => [['color']],
62+
'two different properties' => [['color', 'display']],
63+
'two of the same property' => [['color', 'color']],
64+
];
65+
}
66+
67+
/**
68+
* @return array<string, array{0: non-empty-string}>
69+
*/
70+
public static function providePropertyNameToAdd(): array
71+
{
72+
return [
73+
'property name `color` maybe matching that of existing declaration' => ['color'],
74+
'property name `display` maybe matching that of existing declaration' => ['display'],
75+
'property name `width` not matching that of existing declaration' => ['width'],
76+
];
77+
}
78+
79+
/**
80+
* @return DataProvider<string, array{0: list<string>, 1: string}>
81+
*/
82+
public static function provideInitialPropertyNamesAndPropertyNameToAdd(): DataProvider
83+
{
84+
return DataProvider::cross(self::providePropertyNamesToBeSetInitially(), self::providePropertyNameToAdd());
85+
}
86+
87+
/**
88+
* @test
89+
*
90+
* @param list<string> $initialPropertyNames
91+
*
92+
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
93+
*/
94+
public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAndColumnNumbers(
95+
array $initialPropertyNames,
96+
string $propertyNameToAdd
97+
): void {
98+
if ($initialPropertyNames === []) {
99+
self::markTestSkipped('currently broken - first rule added does not have valid line number set');
100+
}
101+
102+
$ruleToAdd = new Rule($propertyNameToAdd);
103+
$this->setRulesFromPropertyNames($initialPropertyNames);
104+
105+
$this->subject->addRule($ruleToAdd);
106+
107+
$rules = $this->subject->getRules();
108+
self::assertSame($ruleToAdd, \end($rules));
109+
self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
110+
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
111+
self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
112+
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
113+
}
114+
115+
/**
116+
* @test
117+
*
118+
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
119+
*
120+
* @param list<string> $initialPropertyNames
121+
*/
122+
public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLineNumber(
123+
array $initialPropertyNames,
124+
string $propertyNameToAdd
125+
): void {
126+
self::markTestSkipped('currently broken - does not set column number');
127+
128+
$ruleToAdd = new Rule($propertyNameToAdd);
129+
$ruleToAdd->setPosition(42);
130+
$this->setRulesFromPropertyNames($initialPropertyNames);
131+
132+
$this->subject->addRule($ruleToAdd);
133+
134+
self::assertContains($ruleToAdd, $this->subject->getRules());
135+
self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
136+
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
137+
self::assertSame(42, $ruleToAdd->getLineNumber(), 'line number not preserved');
138+
}
139+
140+
/**
141+
* @test
142+
*
143+
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
144+
*
145+
* @param list<string> $initialPropertyNames
146+
*/
147+
public function addRuleWithOnlyColumnNumberAddsRuleAndSetsLineNumberPreservingColumnNumber(
148+
array $initialPropertyNames,
149+
string $propertyNameToAdd
150+
): void {
151+
self::markTestSkipped('currently broken - does not preserve column number');
152+
153+
$ruleToAdd = new Rule($propertyNameToAdd);
154+
$ruleToAdd->setPosition(null, 42);
155+
$this->setRulesFromPropertyNames($initialPropertyNames);
156+
157+
$this->subject->addRule($ruleToAdd);
158+
159+
self::assertContains($ruleToAdd, $this->subject->getRules());
160+
self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
161+
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
162+
self::assertSame(42, $ruleToAdd->getColumnNumber(), 'column number not preserved');
163+
}
164+
165+
/**
166+
* @test
167+
*
168+
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
169+
*
170+
* @param list<string> $initialPropertyNames
171+
*/
172+
public function addRuleWithCompletePositionAddsRuleAndPreservesPosition(
173+
array $initialPropertyNames,
174+
string $propertyNameToAdd
175+
): void {
176+
$ruleToAdd = new Rule($propertyNameToAdd);
177+
$ruleToAdd->setPosition(42, 64);
178+
$this->setRulesFromPropertyNames($initialPropertyNames);
179+
180+
$this->subject->addRule($ruleToAdd);
181+
182+
self::assertContains($ruleToAdd, $this->subject->getRules());
183+
self::assertSame(42, $ruleToAdd->getLineNumber(), 'line number not preserved');
184+
self::assertSame(64, $ruleToAdd->getColumnNumber(), 'column number not preserved');
185+
}
186+
53187
/**
54188
* @return array<string, array{0: list<string>, 1: string, 2: list<string>}>
55189
*/
@@ -190,25 +324,12 @@ public function removeMatchingRulesRemovesRulesByPropertyNamePrefixAndKeepsOther
190324
}
191325
}
192326

193-
/**
194-
* @return array<string, array{0: list<string>}>
195-
*/
196-
public static function providePropertyNamesToRemove(): array
197-
{
198-
return [
199-
'no properties' => [[]],
200-
'one property' => [['color']],
201-
'two different properties' => [['color', 'display']],
202-
'two of the same property' => [['color', 'color']],
203-
];
204-
}
205-
206327
/**
207328
* @test
208329
*
209330
* @param list<string> $propertyNamesToRemove
210331
*
211-
* @dataProvider providePropertyNamesToRemove
332+
* @dataProvider providePropertyNamesToBeSetInitially
212333
*/
213334
public function removeAllRulesRemovesAllRules(array $propertyNamesToRemove): void
214335
{

0 commit comments

Comments
 (0)