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
|
<?php declare(strict_types = 1);
namespace PHPStan\PhpDocParser\Parser;
use Iterator;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\ParserConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
use function file_get_contents;
use function glob;
use function is_dir;
use function mkdir;
use function sprintf;
use function unlink;
/**
* @requires OS ^(?!win)
*/
class FuzzyTest extends TestCase
{
private Lexer $lexer;
private TypeParser $typeParser;
private ConstExprParser $constExprParser;
protected function setUp(): void
{
parent::setUp();
$config = new ParserConfig([]);
$this->lexer = new Lexer($config);
$this->typeParser = new TypeParser($config, new ConstExprParser($config));
$this->constExprParser = new ConstExprParser($config);
}
/**
* @dataProvider provideTypeParserData
*/
#[DataProvider('provideTypeParserData')]
public function testTypeParser(string $input): void
{
$tokens = new TokenIterator($this->lexer->tokenize($input));
$this->typeParser->parse($tokens);
$this->assertSame(
Lexer::TOKEN_END,
$tokens->currentTokenType(),
sprintf('Failed to parse input %s', $input),
);
}
public static function provideTypeParserData(): Iterator
{
return self::provideFuzzyInputsData('Type');
}
/**
* @dataProvider provideConstExprParserData
*/
#[DataProvider('provideConstExprParserData')]
public function testConstExprParser(string $input): void
{
$tokens = new TokenIterator($this->lexer->tokenize($input));
$this->constExprParser->parse($tokens);
$this->assertSame(
Lexer::TOKEN_END,
$tokens->currentTokenType(),
sprintf('Failed to parse input %s', $input),
);
}
public static function provideConstExprParserData(): Iterator
{
return self::provideFuzzyInputsData('ConstantExpr');
}
private static function provideFuzzyInputsData(string $startSymbol): Iterator
{
$inputsDirectory = sprintf('%s/fuzzy/%s', __DIR__ . '/../../../temp', $startSymbol);
if (is_dir($inputsDirectory)) {
$glob = glob(sprintf('%s/*.tst', $inputsDirectory));
if ($glob !== false) {
foreach ($glob as $file) {
unlink($file);
}
}
} else {
mkdir($inputsDirectory, 0777, true);
}
$process = new Process([
__DIR__ . '/../../../tools/abnfgen/abnfgen',
'-lx',
'-n',
'1000',
'-d',
$inputsDirectory,
'-s',
$startSymbol,
__DIR__ . '/../../../doc/grammars/type.abnf',
]);
$process->mustRun();
$glob = glob(sprintf('%s/*.tst', $inputsDirectory));
if ($glob === false) {
return;
}
foreach ($glob as $file) {
$input = file_get_contents($file);
yield [$input];
}
}
}
|