Skip to content

[TASK] Add unit tests for selector parsing #1322

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 4 commits into from
Jul 9, 2025
Merged
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
78 changes: 78 additions & 0 deletions tests/Unit/RuleSet/DeclarationBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
use Sabberworm\CSS\CSSElement;
use Sabberworm\CSS\CSSList\CSSListItem;
use Sabberworm\CSS\Position\Positionable;
use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Property\Selector;
use Sabberworm\CSS\RuleSet\DeclarationBlock;
use Sabberworm\CSS\Settings;
use TRegx\PhpUnit\DataProviders\DataProvider;

/**
* @covers \Sabberworm\CSS\RuleSet\DeclarationBlock
Expand Down Expand Up @@ -48,4 +52,78 @@ public function implementsPositionable(): void
{
self::assertInstanceOf(Positionable::class, $this->subject);
}

/**
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideSelector(): array
{
return [
'type' => ['body'],
'class' => ['.teapot'],
'type & class' => ['img.teapot'],
'id' => ['#my-mug'],
'type & id' => ['h2#my-mug'],
'pseudo-class' => [':hover'],
'type & pseudo-class' => ['a:hover'],
'`not`' => [':not(#your-mug)'],
'pseudo-element' => ['::before'],
'attribute with `"`' => ['[alt="{}()[]\\"\'"]'],
'attribute with `\'`' => ['[alt=\'{}()[]"\\\'\']'],
];
}

/**
* @test
*
* @param non-empty-string $selector
*
* @dataProvider provideSelector
*/
public function parsesSingleSelector(string $selector): void
{
$subject = DeclarationBlock::parse(new ParserState($selector . ' {}', Settings::create()));

self::assertInstanceOf(DeclarationBlock::class, $subject);
self::assertSame([$selector], self::getSelectorsAsStrings($subject));
}

/**
* @return DataProvider<non-empty-string, array{0: non-empty-string, 1: non-empty-string}>
*/
public static function provideTwoSelectors(): DataProvider
{
return DataProvider::cross(self::provideSelector(), self::provideSelector());
}

/**
* @test
*
* @param non-empty-string $firstSelector
* @param non-empty-string $secondSelector
*
* @dataProvider provideTwoSelectors
*/
public function parsesTwoCommaSeparatedSelectors(string $firstSelector, string $secondSelector): void
{
$joinedSelectors = $firstSelector . ', ' . $secondSelector;

$subject = DeclarationBlock::parse(new ParserState($joinedSelectors . ' {}', Settings::create()));

self::assertInstanceOf(DeclarationBlock::class, $subject);
self::assertSame([$firstSelector, $secondSelector], self::getSelectorsAsStrings($subject));
}

/**
* @return array<string>
*/
private static function getSelectorsAsStrings(DeclarationBlock $declarationBlock): array
{
return \array_map(
static function (Selector $selectorObject): string {
return $selectorObject->getSelector();
},
$declarationBlock->getSelectors()
);
}
}