|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sabberworm\CSS\Tests\Unit\Parsing; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Sabberworm\CSS\Comment\Comment; |
| 9 | +use Sabberworm\CSS\Parsing\ParserState; |
| 10 | +use Sabberworm\CSS\Settings; |
| 11 | + |
| 12 | +/** |
| 13 | + * @covers \Sabberworm\CSS\Parsing\ParserState |
| 14 | + */ |
| 15 | +final class ParserStateTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @return array< |
| 19 | + * string, |
| 20 | + * array{ |
| 21 | + * text: non-empty-string, |
| 22 | + * stopCharacter: non-empty-string, |
| 23 | + * expectedConsumedText: non-empty-string, |
| 24 | + * expectedComments: non-empty-list<non-empty-string> |
| 25 | + * } |
| 26 | + * > |
| 27 | + */ |
| 28 | + public static function provideTextForConsumptionWithComments(): array |
| 29 | + { |
| 30 | + return [ |
| 31 | + 'comment at start' => [ |
| 32 | + 'text' => '/*comment*/hello{', |
| 33 | + 'stopCharacter' => '{', |
| 34 | + 'expectedConsumedText' => 'hello', |
| 35 | + 'expectedComments' => ['comment'], |
| 36 | + ], |
| 37 | + 'comment at end' => [ |
| 38 | + 'text' => 'hello/*comment*/{', |
| 39 | + 'stopCharacter' => '{', |
| 40 | + 'expectedConsumedText' => 'hello', |
| 41 | + 'expectedComments' => ['comment'], |
| 42 | + ], |
| 43 | + 'comment in middle' => [ |
| 44 | + 'text' => 'hell/*comment*/o{', |
| 45 | + 'stopCharacter' => '{', |
| 46 | + 'expectedConsumedText' => 'hello', |
| 47 | + 'expectedComments' => ['comment'], |
| 48 | + ], |
| 49 | + 'two comments at start' => [ |
| 50 | + 'text' => '/*comment1*//*comment2*/hello{', |
| 51 | + 'stopCharacter' => '{', |
| 52 | + 'expectedConsumedText' => 'hello', |
| 53 | + 'expectedComments' => ['comment1', 'comment2'], |
| 54 | + ], |
| 55 | + 'two comments at end' => [ |
| 56 | + 'text' => 'hello/*comment1*//*comment2*/{', |
| 57 | + 'stopCharacter' => '{', |
| 58 | + 'expectedConsumedText' => 'hello', |
| 59 | + 'expectedComments' => ['comment1', 'comment2'], |
| 60 | + ], |
| 61 | + 'two comments interspersed' => [ |
| 62 | + 'text' => 'he/*comment1*/ll/*comment2*/o{', |
| 63 | + 'stopCharacter' => '{', |
| 64 | + 'expectedConsumedText' => 'hello', |
| 65 | + 'expectedComments' => ['comment1', 'comment2'], |
| 66 | + ], |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @test |
| 72 | + * |
| 73 | + * @param non-empty-string $text |
| 74 | + * @param non-empty-string $stopCharacter |
| 75 | + * @param non-empty-string $expectedConsumedText |
| 76 | + * @param non-empty-list<non-empty-string> $expectedComments |
| 77 | + * |
| 78 | + * @dataProvider provideTextForConsumptionWithComments |
| 79 | + */ |
| 80 | + public function consumeUntilExtractsComments( |
| 81 | + string $text, |
| 82 | + string $stopCharacter, |
| 83 | + string $expectedConsumedText, |
| 84 | + array $expectedComments |
| 85 | + ): void { |
| 86 | + $subject = new ParserState($text, Settings::create()); |
| 87 | + |
| 88 | + $comments = []; |
| 89 | + $result = $subject->consumeUntil($stopCharacter, false, false, $comments); |
| 90 | + $commentsAsText = \array_map( |
| 91 | + static function (Comment $comment): string { |
| 92 | + return $comment->getComment(); |
| 93 | + }, |
| 94 | + $comments |
| 95 | + ); |
| 96 | + |
| 97 | + self::assertSame($expectedConsumedText, $result); |
| 98 | + self::assertSame($expectedComments, $commentsAsText); |
| 99 | + } |
| 100 | +} |
0 commit comments