mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-16 22:58:15 +01:00
Respect version mode line in pretty printer tests
By default parser uses newest supported and pretty printer uses its default version. If version is specified, it's used for both parser and printer. Additionally, parserVersion can be used to specify a different version for parser and printer.
This commit is contained in:
parent
beba9c528f
commit
d3ae2ed679
@ -10,18 +10,24 @@ use PhpParser\Node\InterpolatedStringPart;
|
||||
use PhpParser\Node\Scalar\Int_;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Parser\Php7;
|
||||
use PhpParser\PrettyPrinter\Standard;
|
||||
|
||||
class PrettyPrinterTest extends CodeTestAbstract {
|
||||
/** @return array{0: Parser, 1: PrettyPrinter} */
|
||||
private function createParserAndPrinter(array $options): array {
|
||||
$parserVersion = $options['parserVersion'] ?? $options['version'] ?? null;
|
||||
$printerVersion = $options['version'] ?? null;
|
||||
$factory = new ParserFactory();
|
||||
$parser = $factory->createForVersion($parserVersion !== null
|
||||
? PhpVersion::fromString($parserVersion) : PhpVersion::getNewestSupported());
|
||||
$prettyPrinter = new Standard([
|
||||
'phpVersion' => $printerVersion !== null ? PhpVersion::fromString($printerVersion) : null
|
||||
]);
|
||||
return [$parser, $prettyPrinter];
|
||||
}
|
||||
|
||||
protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
|
||||
$lexer = new Lexer\Emulative();
|
||||
$parser = new Parser\Php7($lexer);
|
||||
|
||||
$options = $this->parseModeLine($modeLine);
|
||||
$version = isset($options['version']) ? PhpVersion::fromString($options['version']) : null;
|
||||
$prettyPrinter = new Standard(['phpVersion' => $version]);
|
||||
|
||||
[$parser, $prettyPrinter] = $this->createParserAndPrinter($this->parseModeLine($modeLine));
|
||||
$output = canonicalize($prettyPrinter->$method($parser->parse($code)));
|
||||
$this->assertSame($expected, $output, $name);
|
||||
}
|
||||
@ -181,12 +187,9 @@ class PrettyPrinterTest extends CodeTestAbstract {
|
||||
* @dataProvider provideTestFormatPreservingPrint
|
||||
*/
|
||||
public function testFormatPreservingPrint($name, $code, $modification, $expected, $modeLine): void {
|
||||
$lexer = new Lexer\Emulative();
|
||||
$parser = new Parser\Php7($lexer);
|
||||
[$parser, $printer] = $this->createParserAndPrinter($this->parseModeLine($modeLine));
|
||||
$traverser = new NodeTraverser(new NodeVisitor\CloningVisitor());
|
||||
|
||||
$printer = new PrettyPrinter\Standard();
|
||||
|
||||
$oldStmts = $parser->parse($code);
|
||||
$oldTokens = $parser->getTokens();
|
||||
|
||||
@ -222,14 +225,9 @@ CODE
|
||||
* the pretty printer tests (i.e. returns the input if no changes occurred).
|
||||
*/
|
||||
|
||||
$lexer = new Lexer\Emulative();
|
||||
|
||||
$parser = new Php7($lexer);
|
||||
|
||||
[$parser, $printer] = $this->createParserAndPrinter($this->parseModeLine($modeLine));
|
||||
$traverser = new NodeTraverser(new NodeVisitor\CloningVisitor());
|
||||
|
||||
$printer = new PrettyPrinter\Standard();
|
||||
|
||||
try {
|
||||
$oldStmts = $parser->parse($code);
|
||||
} catch (Error $e) {
|
||||
|
@ -5,7 +5,7 @@ Pretty printer generates least-parentheses output
|
||||
echo 'abc' . 'cde' . 'fgh';
|
||||
echo 'abc' . ('cde' . 'fgh');
|
||||
|
||||
echo 'abc' . 1 + 2 . 'fgh';
|
||||
echo ('abc' . 1) + 2 . 'fgh';
|
||||
echo 'abc' . (1 + 2) . 'fgh';
|
||||
|
||||
echo 1 * 2 + 3 / 4 % 5 . 6;
|
||||
|
@ -79,7 +79,7 @@ function gen()
|
||||
yield * $a;
|
||||
}
|
||||
-----
|
||||
!!version=5.6
|
||||
!!version=5.6,parserVersion=8.0
|
||||
function gen()
|
||||
{
|
||||
yield;
|
||||
|
Loading…
x
Reference in New Issue
Block a user