-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathParserStateTest.php
More file actions
160 lines (138 loc) · 4.53 KB
/
Copy pathParserStateTest.php
File metadata and controls
160 lines (138 loc) · 4.53 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
<?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;
/**
* @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());
}
}