-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathParserStateTest.php
More file actions
369 lines (320 loc) · 11.4 KB
/
ParserStateTest.php
File metadata and controls
369 lines (320 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
declare(strict_types=1);
namespace Sabberworm\CSS\Tests\Unit\Parsing;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Comment\Comment;
use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Settings;
use TRegx\PhpUnit\DataProviders\DataProvider;
/**
* @covers \Sabberworm\CSS\Parsing\ParserState
*/
final class ParserStateTest extends TestCase
{
/**
* @return array<
* string,
* array{
* text: non-empty-string,
* stopCharacter: non-empty-string,
* expectedConsumedText: non-empty-string,
* expectedComments: non-empty-list<non-empty-string>
* }
* >
*/
public static function provideTextForConsumptionWithComments(): array
{
return [
'comment at start' => [
'text' => '/*comment*/hello{',
'stopCharacter' => '{',
'expectedConsumedText' => 'hello',
'expectedComments' => ['comment'],
],
'comment at end' => [
'text' => 'hello/*comment*/{',
'stopCharacter' => '{',
'expectedConsumedText' => 'hello',
'expectedComments' => ['comment'],
],
'comment in middle' => [
'text' => 'hell/*comment*/o{',
'stopCharacter' => '{',
'expectedConsumedText' => 'hello',
'expectedComments' => ['comment'],
],
'two comments at start' => [
'text' => '/*comment1*//*comment2*/hello{',
'stopCharacter' => '{',
'expectedConsumedText' => 'hello',
'expectedComments' => ['comment1', 'comment2'],
],
'two comments at end' => [
'text' => 'hello/*comment1*//*comment2*/{',
'stopCharacter' => '{',
'expectedConsumedText' => 'hello',
'expectedComments' => ['comment1', 'comment2'],
],
'two comments interspersed' => [
'text' => 'he/*comment1*/ll/*comment2*/o{',
'stopCharacter' => '{',
'expectedConsumedText' => 'hello',
'expectedComments' => ['comment1', 'comment2'],
],
];
}
/**
* @test
*
* @param non-empty-string $text
* @param non-empty-string $stopCharacter
* @param non-empty-string $expectedConsumedText
* @param non-empty-list<non-empty-string> $expectedComments
*
* @dataProvider provideTextForConsumptionWithComments
*/
public function consumeUntilExtractsComments(
string $text,
string $stopCharacter,
string $expectedConsumedText,
array $expectedComments
): void {
$subject = new ParserState($text, Settings::create());
$comments = [];
$result = $subject->consumeUntil($stopCharacter, false, false, $comments);
self::assertSame($expectedConsumedText, $result);
$commentsAsText = \array_map(
static function (Comment $comment): string {
return $comment->getComment();
},
$comments
);
self::assertSame($expectedComments, $commentsAsText);
}
/**
* @test
*/
public function consumeIfComesComsumesMatchingContent(): void
{
$subject = new ParserState('abc', Settings::create());
$subject->consumeIfComes('ab');
self::assertSame('c', $subject->peek());
}
/**
* @test
*/
public function consumeIfComesDoesNotComsumeNonMatchingContent(): void
{
$subject = new ParserState('a', Settings::create());
$subject->consumeIfComes('x');
self::assertSame('a', $subject->peek());
}
/**
* @test
*/
public function consumeIfComesReturnsTrueIfContentConsumed(): void
{
$subject = new ParserState('abc', Settings::create());
$result = $subject->consumeIfComes('ab');
self::assertTrue($result);
}
/**
* @test
*/
public function consumeIfComesReturnsFalseIfContentNotConsumed(): void
{
$subject = new ParserState('a', Settings::create());
$result = $subject->consumeIfComes('x');
self::assertFalse($result);
}
/**
* @test
*/
public function consumeIfComesUpdatesLineNumber(): void
{
$subject = new ParserState("\n", Settings::create());
$subject->consumeIfComes("\n");
self::assertSame(2, $subject->currentLine());
}
/**
* @return array<non-empty-string, array{0: string, 1: string}>
*/
public static function provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption(): array
{
return [
'nothing' => ['', ''],
'space' => [' ', ' '],
'tab' => ["\t", "\t"],
'line feed' => ["\n", "\n"],
'carriage return' => ["\r", "\r"],
'two spaces' => [' ', ' '],
'comment' => ['/*hello*/', ''],
'comment with space to the left' => [' /*hello*/', ' '],
'comment with space to the right' => ['/*hello*/ ', ' '],
'two comments' => ['/*hello*//*bye*/', ''],
'two comments with space between' => ['/*hello*/ /*bye*/', ' '],
'two comments with line feed between' => ["/*hello*/\n/*bye*/", "\n"],
];
}
/**
* @test
*
* @dataProvider provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption
*/
public function consumeWhiteSpaceReturnsTheConsumed(
string $whitespaceMaybeWithComments,
string $expectedConsumption
): void {
$subject = new ParserState($whitespaceMaybeWithComments, Settings::create());
$result = $subject->consumeWhiteSpace();
self::assertSame($expectedConsumption, $result);
}
/**
* @test
*/
public function consumeWhiteSpaceExtractsComment(): void
{
$commentText = 'Did they get you to trade your heroes for ghosts?';
$subject = new ParserState('/*' . $commentText . '*/', Settings::create());
$result = [];
$subject->consumeWhiteSpace($result);
self::assertInstanceOf(Comment::class, $result[0]);
self::assertSame($commentText, $result[0]->getComment());
}
/**
* @test
*/
public function consumeWhiteSpaceExtractsTwoComments(): void
{
$commentText1 = 'Hot ashes for trees? Hot air for a cool breeze?';
$commentText2 = 'Cold comfort for change? Did you exchange';
$subject = new ParserState('/*' . $commentText1 . '*//*' . $commentText2 . '*/', Settings::create());
$result = [];
$subject->consumeWhiteSpace($result);
self::assertInstanceOf(Comment::class, $result[0]);
self::assertSame($commentText1, $result[0]->getComment());
self::assertInstanceOf(Comment::class, $result[1]);
self::assertSame($commentText2, $result[1]->getComment());
}
/**
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideWhitespace(): array
{
return [
'space' => [' '],
'tab' => ["\t"],
'line feed' => ["\n"],
'carriage return' => ["\r"],
'two spaces' => [' '],
];
}
/**
* @test
*
* @param non-empty-string $whitespace
*
* @dataProvider provideWhitespace
*/
public function consumeWhiteSpaceExtractsCommentWithSurroundingWhitespace(string $whitespace): void
{
$commentText = 'A walk-on part in the war for a lead role in a cage?';
$subject = new ParserState($whitespace . '/*' . $commentText . '*/' . $whitespace, Settings::create());
$result = [];
$subject->consumeWhiteSpace($result);
self::assertInstanceOf(Comment::class, $result[0]);
self::assertSame($commentText, $result[0]->getComment());
}
/**
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideNonWhitespace(): array
{
return [
'number' => ['7'],
'uppercase letter' => ['B'],
'lowercase letter' => ['c'],
'symbol' => ['@'],
'sequence of non-whitespace characters' => ['Hello!'],
'sequence of characters including space which isn\'t first' => ['Oh no!'],
];
}
/**
* @return DataProvider<non-empty-string, array{0: non-empty-string, 1: string}>
*/
public function provideNonWhitespaceAndContentWithPossibleWhitespaceOrComments(): DataProvider
{
return DataProvider::cross(
self::provideNonWhitespace(),
self::provideContentWhichMayHaveWhitespaceOrCommentsAndExpectedConsumption()
);
}
/**
* @test
*
* @param non-empty-string $nonWhitespace
*
* @dataProvider provideNonWhitespaceAndContentWithPossibleWhitespaceOrComments
*/
public function consumeWhiteSpaceStopsAtNonWhitespace(string $nonWhitespace, string $whitespace): void
{
$subject = new ParserState($whitespace . $nonWhitespace, Settings::create());
$subject->consumeWhiteSpace();
self::assertTrue($subject->comes($nonWhitespace));
}
/**
* @return array<non-empty-string, array{0: string}>
*/
public static function providePossibleWhitespaceWithoutLineFeedPossiblyIncludingComments(): array
{
return [
'nothing' => [''],
'space' => [' '],
'tab' => ["\t"],
'carriage return' => ["\r"], // inclusion of this is debatable, but Mac line endings are not generally seen
'two spaces' => [' '],
'comment' => ['/* Time for bed */'],
'comment with spacing around' => [' /* I am so tired */ '],
];
}
/**
* @return DataProvider<non-empty-string, array{0: string, 1: string}>
*/
public function providePossibleWhitespaceWithoutLineFeedPossiblyIncludingCommentsAndTheSameAgain(): DataProvider
{
return DataProvider::cross(
self::providePossibleWhitespaceWithoutLineFeedPossiblyIncludingComments(),
self::providePossibleWhitespaceWithoutLineFeedPossiblyIncludingComments()
);
}
/**
* @test
*
* @dataProvider providePossibleWhitespaceWithoutLineFeedPossiblyIncludingCommentsAndTheSameAgain
*/
public function consumeWhiteSpaceStopsAfterLineFeedWhenRequested(string $firstLine, string $secondLine): void
{
$subject = new ParserState($firstLine . "\n" . $secondLine, Settings::create());
$comments = [];
$subject->consumeWhiteSpace($comments, true);
if ($secondLine === '') {
self::assertTrue($subject->isEnd());
} else {
self::assertTrue($subject->comes($secondLine));
}
}
/**
* @test
*
* @dataProvider providePossibleWhitespaceWithoutLineFeedPossiblyIncludingCommentsAndTheSameAgain
*/
public function consumeWhiteSpaceDoesNotStopAfterLineFeedWhenNotRequested(
string $firstLine,
string $secondLine
): void {
$subject = new ParserState($firstLine . "\n" . $secondLine, Settings::create());
$comments = [];
$subject->consumeWhiteSpace($comments, false);
self::assertTrue($subject->isEnd());
}
}