-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDescriptionParserTest.php
More file actions
284 lines (235 loc) · 10.2 KB
/
Copy pathDescriptionParserTest.php
File metadata and controls
284 lines (235 loc) · 10.2 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
<?php
declare(strict_types=1);
namespace TypeLang\PhpDoc\Tests\Parser;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TypeLang\PhpDoc\DocBlock\ComponentInterface;
use TypeLang\PhpDoc\DocBlock\Description\Description;
use TypeLang\PhpDoc\DocBlock\Description\TaggedDescription;
use TypeLang\PhpDoc\DocBlock\Tag\TagInterface;
use TypeLang\PhpDoc\Parser\Description\DescriptionParserInterface;
use TypeLang\PhpDoc\Tests\TestCase;
final class DescriptionParserTest extends TestCase
{
/**
* @return iterable<string, array{DescriptionParserInterface}>
*/
public static function parserDataProvider(): iterable
{
yield 'BalancedBraceAwareParser' => [
self::createDescriptionParser(),
];
}
/**
* @return iterable<string, array{DescriptionParserInterface, string}>
*/
public static function plainDescriptionDataProvider(): iterable
{
$inputs = [
'plain text' => 'Hello world',
'whitespace only' => ' ',
// A bare "@" not wrapped in `{...}` is plain text, not a tag.
'bare at-sign without braces' => 'Text @param int $x',
'literal at-sign in text' => 'contact me@example.com now',
'braces without at-sign' => 'array{int, string}',
'empty inline tag' => '{@}',
'text merged around empty inline tag' => 'a{@}b',
// Unreadable tag name: the whole "{@...}" stays raw text.
'malformed inline tag name' => '{@ foo}',
// Unbalanced "{@..." consumes the rest of the string as text.
'unclosed inline tag' => '{@see Foo',
];
return self::withParser($inputs);
}
/**
* @return iterable<string, array{DescriptionParserInterface, string}>
*/
public static function roundTripInputsDataProvider(): iterable
{
$inputs = [
'plain text' => 'Hello world',
'empty string' => '',
'whitespace only' => ' ',
'braces without at-sign' => 'array{int, string}',
'single inline tag' => '{@see Foo::bar()}',
'inline tag with nested braces' => 'Hello {@see array{int} example} world',
'inline tag with surrounding text' => 'See also {@see Foo::bar()} for details.',
'adjacent inline tags' => '{@see A}{@link B}',
'empty inline tag' => '{@}',
'nested inline tags' => '{@see {@link X}}',
'unclosed inline tag' => '{@see Foo',
];
return self::withParser($inputs);
}
#[Test]
#[DataProvider('parserDataProvider')]
public function tryParseReturnsNullForEmptyString(DescriptionParserInterface $parser): void
{
self::assertNull($parser->tryParse(''));
}
#[Test]
#[DataProvider('parserDataProvider')]
public function tryParseDelegatesToParseForNonEmptyInput(DescriptionParserInterface $parser): void
{
self::assertPlainDescription('Hello world', $parser->tryParse('Hello world'));
self::assertInstanceOf(TaggedDescription::class, $parser->tryParse('{@see Foo}'));
}
#[Test]
#[DataProvider('parserDataProvider')]
public function parseReturnsEmptyDescriptionForEmptyString(DescriptionParserInterface $parser): void
{
self::assertPlainDescription('', $parser->parse(''));
}
#[Test]
#[DataProvider('parserDataProvider')]
public function parseResultIsStringable(DescriptionParserInterface $parser): void
{
self::assertInstanceOf(\Stringable::class, $parser->parse('plain'));
self::assertInstanceOf(\Stringable::class, $parser->parse('{@see Foo}'));
}
#[Test]
#[DataProvider('roundTripInputsDataProvider')]
public function stringCastReproducesOriginalInput(DescriptionParserInterface $parser, string $input): void
{
self::assertSame($input, (string) $parser->parse($input));
}
#[Test]
#[DataProvider('plainDescriptionDataProvider')]
public function inputWithoutRecognizedTagsBecomesPlainDescription(
DescriptionParserInterface $parser,
string $input,
): void {
self::assertPlainDescription($input, $parser->parse($input));
}
#[Test]
#[DataProvider('parserDataProvider')]
public function singleInlineTagBecomesTaggedDescriptionWithOneTag(DescriptionParserInterface $parser): void
{
$result = $parser->parse('{@see Foo::bar()}');
self::assertInstanceOf(TaggedDescription::class, $result);
self::assertCount(1, $result);
self::assertCount(1, $result->tags);
self::assertTagComponent('see', 'Foo::bar()', $result->components[0]);
self::assertSame('{@see Foo::bar()}', (string) $result);
}
#[Test]
#[DataProvider('parserDataProvider')]
public function inlineTagSurroundedByTextKeepsComponentsInOrder(DescriptionParserInterface $parser): void
{
$result = $parser->parse('See also {@see Foo::bar()} for details.');
self::assertInstanceOf(TaggedDescription::class, $result);
// Text before + tag + text after == three ordered components.
self::assertCount(3, $result);
[$before, $tag, $after] = $result->components;
self::assertPlainDescription('See also ', $before);
self::assertTagComponent('see', 'Foo::bar()', $tag);
self::assertPlainDescription(' for details.', $after);
self::assertSame('See also {@see Foo::bar()} for details.', (string) $result);
}
#[Test]
#[DataProvider('parserDataProvider')]
public function adjacentInlineTagsBecomeSeparateTags(DescriptionParserInterface $parser): void
{
$result = $parser->parse('{@see A}{@link B}');
self::assertInstanceOf(TaggedDescription::class, $result);
$names = \array_map(static fn(TagInterface $tag): string => $tag->name, $result->tags);
self::assertSame(['see', 'link'], $names);
self::assertSame('{@see A}{@link B}', (string) $result);
}
#[Test]
#[DataProvider('parserDataProvider')]
public function nestedBracesInsideInlineTagAreRecognized(DescriptionParserInterface $parser): void
{
$result = $parser->parse('Hello {@see array{int} example} world');
self::assertInstanceOf(TaggedDescription::class, $result);
self::assertCount(3, $result);
[$before, $tag, $after] = $result->components;
self::assertPlainDescription('Hello ', $before);
self::assertTagComponent('see', 'array{int} example', $tag);
self::assertPlainDescription(' world', $after);
self::assertSame('Hello {@see array{int} example} world', (string) $result);
}
#[Test]
#[DataProvider('parserDataProvider')]
public function bareAtSignWithoutBracesIsPlainText(DescriptionParserInterface $parser): void
{
self::assertPlainDescription('@param int $x description', $parser->parse('@param int $x description'));
}
#[Test]
#[DataProvider('parserDataProvider')]
public function tagWithoutDescriptionHasNullDescription(DescriptionParserInterface $parser): void
{
$result = $parser->parse('{@see}');
self::assertInstanceOf(TaggedDescription::class, $result);
self::assertCount(1, $result->tags);
self::assertTagComponent('see', null, $result->components[0]);
self::assertSame('{@see}', (string) $result);
}
#[Test]
#[DataProvider('parserDataProvider')]
public function nestedInlineTagsAreParsedRecursively(DescriptionParserInterface $parser): void
{
$result = $parser->parse('{@see {@link X}}');
self::assertInstanceOf(TaggedDescription::class, $result);
self::assertCount(1, $result->tags);
$outer = $result->components[0];
self::assertInstanceOf(TagInterface::class, $outer);
self::assertSame('see', $outer->name);
// The nested `{@link X}` is itself parsed as the outer tag description.
$inner = $outer->description;
self::assertInstanceOf(TaggedDescription::class, $inner);
self::assertCount(1, $inner->tags);
self::assertTagComponent('link', 'X', $inner->components[0]);
self::assertSame('{@see {@link X}}', (string) $result);
}
#[Test]
#[DataProvider('parserDataProvider')]
public function inlineTagWithUnreadableNameBecomesPlainDescription(DescriptionParserInterface $parser): void
{
self::assertPlainDescription('Hello {@!bad} world', $parser->parse('Hello {@!bad} world'));
}
#[Test]
#[DataProvider('parserDataProvider')]
public function unclosedInlineTagAfterValidTagKeepsEarlierTags(DescriptionParserInterface $parser): void
{
$result = $parser->parse('A {@see X} B {@broken C');
self::assertInstanceOf(TaggedDescription::class, $result);
self::assertCount(3, $result);
[$before, $tag, $tail] = $result->components;
self::assertPlainDescription('A ', $before);
self::assertTagComponent('see', 'X', $tag);
self::assertPlainDescription(' B {@broken C', $tail);
}
/**
* @param iterable<string, string> $inputs
* @return iterable<string, array{DescriptionParserInterface, string}>
*/
private static function withParser(iterable $inputs): iterable
{
foreach (self::parserDataProvider() as $parserName => [$parser]) {
foreach ($inputs as $inputName => $input) {
yield $parserName . ': ' . $inputName => [$parser, $input];
}
}
}
private static function assertPlainDescription(string $expected, mixed $actual): void
{
self::assertInstanceOf(Description::class, $actual);
self::assertSame($expected, $actual->value);
self::assertSame($expected, (string) $actual);
}
private static function assertTagComponent(
string $expectedName,
?string $expectedDescription,
ComponentInterface $actual,
): void {
self::assertInstanceOf(TagInterface::class, $actual);
self::assertSame($expectedName, $actual->name);
if ($expectedDescription === null) {
self::assertNull($actual->description);
return;
}
self::assertInstanceOf(Description::class, $actual->description);
self::assertSame($expectedDescription, $actual->description->value);
}
}