|
8 | 8 | use Sabberworm\CSS\Comment\Comment; |
9 | 9 | use Sabberworm\CSS\Parsing\ParserState; |
10 | 10 | use Sabberworm\CSS\Settings; |
| 11 | +use TRegx\PhpUnit\DataProviders\DataProvider; |
11 | 12 |
|
12 | 13 | /** |
13 | 14 | * @covers \Sabberworm\CSS\Parsing\ParserState |
@@ -157,4 +158,149 @@ public function consumeIfComesUpdatesLineNumber(): void |
157 | 158 |
|
158 | 159 | self::assertSame(2, $subject->currentLine()); |
159 | 160 | } |
| 161 | + |
| 162 | + /** |
| 163 | + * @return array<non-empty-string, array{0: string, 1: string}> |
| 164 | + */ |
| 165 | + public static function provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption(): array |
| 166 | + { |
| 167 | + return [ |
| 168 | + 'nothing' => ['', ''], |
| 169 | + 'space' => [' ', ' '], |
| 170 | + 'tab' => ["\t", "\t"], |
| 171 | + 'line feed' => ["\n", "\n"], |
| 172 | + 'carriage return' => ["\r", "\r"], |
| 173 | + 'two spaces' => [' ', ' '], |
| 174 | + 'comment' => ['/*hello*/', ''], |
| 175 | + 'comment with space to the left' => [' /*hello*/', ' '], |
| 176 | + 'comment with space to the right' => ['/*hello*/ ', ' '], |
| 177 | + 'two comments' => ['/*hello*//*bye*/', ''], |
| 178 | + 'two comments with space between' => ['/*hello*/ /*bye*/', ' '], |
| 179 | + 'two comments with line feed between' => ["/*hello*/\n/*bye*/", "\n"], |
| 180 | + ]; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @test |
| 185 | + * |
| 186 | + * @dataProvider provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption |
| 187 | + */ |
| 188 | + public function consumeWhiteSpaceReturnsTheConsumed( |
| 189 | + string $whitespaceMaybeWithComments, |
| 190 | + string $expectedConsumption |
| 191 | + ): void { |
| 192 | + $subject = new ParserState($whitespaceMaybeWithComments, Settings::create()); |
| 193 | + |
| 194 | + $result = $subject->consumeWhiteSpace(); |
| 195 | + |
| 196 | + self::assertSame($expectedConsumption, $result); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * @test |
| 201 | + */ |
| 202 | + public function consumeWhiteSpaceExtractsComment(): void |
| 203 | + { |
| 204 | + $commentText = 'Did they get you to trade your heroes for ghosts?'; |
| 205 | + $subject = new ParserState('/*' . $commentText . '*/', Settings::create()); |
| 206 | + |
| 207 | + $result = []; |
| 208 | + $subject->consumeWhiteSpace($result); |
| 209 | + |
| 210 | + self::assertInstanceOf(Comment::class, $result[0]); |
| 211 | + self::assertSame($commentText, $result[0]->getComment()); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * @test |
| 216 | + */ |
| 217 | + public function consumeWhiteSpaceExtractsTwoComments(): void |
| 218 | + { |
| 219 | + $commentText1 = 'Hot ashes for trees? Hot air for a cool breeze?'; |
| 220 | + $commentText2 = 'Cold comfort for change? Did you exchange'; |
| 221 | + $subject = new ParserState('/*' . $commentText1 . '*//*' . $commentText2 . '*/', Settings::create()); |
| 222 | + |
| 223 | + $result = []; |
| 224 | + $subject->consumeWhiteSpace($result); |
| 225 | + |
| 226 | + self::assertInstanceOf(Comment::class, $result[0]); |
| 227 | + self::assertSame($commentText1, $result[0]->getComment()); |
| 228 | + self::assertInstanceOf(Comment::class, $result[1]); |
| 229 | + self::assertSame($commentText2, $result[1]->getComment()); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * @return array<non-empty-string, array{0: non-empty-string}> |
| 234 | + */ |
| 235 | + public static function provideWhitespace(): array |
| 236 | + { |
| 237 | + return [ |
| 238 | + 'space' => [' '], |
| 239 | + 'tab' => ["\t"], |
| 240 | + 'line feed' => ["\n"], |
| 241 | + 'carriage return' => ["\r"], |
| 242 | + 'two spaces' => [' '], |
| 243 | + ]; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * @test |
| 248 | + * |
| 249 | + * @param non-empty-string $whitespace |
| 250 | + * |
| 251 | + * @dataProvider provideWhitespace |
| 252 | + */ |
| 253 | + public function consumeWhiteSpaceExtractsCommentWithSurroundingWhitespace(string $whitespace): void |
| 254 | + { |
| 255 | + $commentText = 'A walk-on part in the war for a lead role in a cage?'; |
| 256 | + $subject = new ParserState($whitespace . '/*' . $commentText . '*/' . $whitespace, Settings::create()); |
| 257 | + |
| 258 | + $result = []; |
| 259 | + $subject->consumeWhiteSpace($result); |
| 260 | + |
| 261 | + self::assertInstanceOf(Comment::class, $result[0]); |
| 262 | + self::assertSame($commentText, $result[0]->getComment()); |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * @return array<non-empty-string, array{0: non-empty-string}> |
| 267 | + */ |
| 268 | + public static function provideNonWhitespace(): array |
| 269 | + { |
| 270 | + return [ |
| 271 | + 'number' => ['7'], |
| 272 | + 'uppercase letter' => ['B'], |
| 273 | + 'lowercase letter' => ['c'], |
| 274 | + 'symbol' => ['@'], |
| 275 | + 'sequence of non-whitespace characters' => ['Hello!'], |
| 276 | + 'sequence of characters including space which isn\'t first' => ['Oh no!'], |
| 277 | + ]; |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * @return DataProvider<non-empty-string, array{0: non-empty-string, 1: string}> |
| 282 | + */ |
| 283 | + public function provideNonWhitespaceAndContentWithPossibleWhitespaceOrComments(): DataProvider |
| 284 | + { |
| 285 | + return DataProvider::cross( |
| 286 | + self::provideNonWhitespace(), |
| 287 | + self::provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption() |
| 288 | + ); |
| 289 | + } |
| 290 | + |
| 291 | + /** |
| 292 | + * @test |
| 293 | + * |
| 294 | + * @param non-empty-string $nonWhitespace |
| 295 | + * |
| 296 | + * @dataProvider provideNonWhitespaceAndContentWithPossibleWhitespaceOrComments |
| 297 | + */ |
| 298 | + public function consumeWhiteSpaceStopsAtNonWhitespace(string $nonWhitespace, string $whitespace): void |
| 299 | + { |
| 300 | + $subject = new ParserState($whitespace . $nonWhitespace, Settings::create()); |
| 301 | + |
| 302 | + $subject->consumeWhiteSpace(); |
| 303 | + |
| 304 | + self::assertTrue($subject->comes($nonWhitespace)); |
| 305 | + } |
160 | 306 | } |
0 commit comments