Skip to content

[TASK] Add tests for the selector specificity #1025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions config/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,6 @@ parameters:
count: 1
path: ../src/Property/Import.php

-
message: '#^Parameters should have "string" types as the only types passed to this method$#'
identifier: typePerfect.narrowPublicClassMethodParamType
count: 1
path: ../src/Property/Selector.php

-
message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#'
identifier: method.notFound
Expand Down
24 changes: 0 additions & 24 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,30 +246,6 @@ public function unicodeRangeParsing(): void
public function specificity(): void
{
$document = self::parsedStructureForFile('specificity');
$declarationBlocks = $document->getAllDeclarationBlocks();
$declarationBlock = $declarationBlocks[0];
$selectors = $declarationBlock->getSelectors();
foreach ($selectors as $selector) {
switch ($selector->getSelector()) {
case '#test .help':
self::assertSame(110, $selector->getSpecificity());
break;
case '#file':
self::assertSame(100, $selector->getSpecificity());
break;
case '.help:hover':
self::assertSame(20, $selector->getSpecificity());
break;
case 'ol li::before':
self::assertSame(3, $selector->getSpecificity());
break;
case 'li.green':
self::assertSame(11, $selector->getSpecificity());
break;
default:
self::fail('specificity: untested selector ' . $selector->getSelector());
}
}
self::assertEquals([new Selector('#test .help', true)], $document->getSelectorsBySpecificity('> 100'));
self::assertEquals(
[new Selector('#test .help', true), new Selector('#file', true)],
Expand Down
52 changes: 52 additions & 0 deletions tests/Unit/Property/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,56 @@ public function setSelectorOverwritesSelectorProvidedToConstructor(): void

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],
];
}

/**
* @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());
}
}