mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-12-03 18:07:58 +01:00
aa199120c7
To distinguish array() and [] syntax. The pretty printer respects this attribute. The shortArraySyntax pretty printer option acts as a default in case the attribute is not specified.
115 lines
4.0 KiB
PHP
115 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace PhpParser;
|
|
|
|
use PhpParser\Comment;
|
|
use PhpParser\Node\Expr;
|
|
use PhpParser\Node\Scalar\String_;
|
|
use PhpParser\Node\Stmt;
|
|
use PhpParser\PrettyPrinter\Standard;
|
|
|
|
require_once __DIR__ . '/CodeTestAbstract.php';
|
|
|
|
class PrettyPrinterTest extends CodeTestAbstract
|
|
{
|
|
protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
|
|
$lexer = new Lexer\Emulative;
|
|
$parser5 = new Parser\Php5($lexer);
|
|
$parser7 = new Parser\Php7($lexer);
|
|
|
|
list($version, $options) = $this->parseModeLine($modeLine);
|
|
$prettyPrinter = new Standard($options);
|
|
|
|
try {
|
|
$output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
|
|
} catch (Error $e) {
|
|
$output5 = null;
|
|
if ('php7' !== $version) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
try {
|
|
$output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
|
|
} catch (Error $e) {
|
|
$output7 = null;
|
|
if ('php5' !== $version) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
if ('php5' === $version) {
|
|
$this->assertSame($expected, $output5, $name);
|
|
$this->assertNotSame($expected, $output7, $name);
|
|
} else if ('php7' === $version) {
|
|
$this->assertSame($expected, $output7, $name);
|
|
$this->assertNotSame($expected, $output5, $name);
|
|
} else {
|
|
$this->assertSame($expected, $output5, $name);
|
|
$this->assertSame($expected, $output7, $name);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideTestPrettyPrint
|
|
* @covers PhpParser\PrettyPrinter\Standard<extended>
|
|
*/
|
|
public function testPrettyPrint($name, $code, $expected, $mode) {
|
|
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideTestPrettyPrintFile
|
|
* @covers PhpParser\PrettyPrinter\Standard<extended>
|
|
*/
|
|
public function testPrettyPrintFile($name, $code, $expected, $mode) {
|
|
$this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
|
|
}
|
|
|
|
public function provideTestPrettyPrint() {
|
|
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
|
|
}
|
|
|
|
public function provideTestPrettyPrintFile() {
|
|
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
|
|
}
|
|
|
|
public function testPrettyPrintExpr() {
|
|
$prettyPrinter = new Standard;
|
|
$expr = new Expr\BinaryOp\Mul(
|
|
new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
|
|
new Expr\Variable('c')
|
|
);
|
|
$this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
|
|
|
|
$expr = new Expr\Closure(array(
|
|
'stmts' => array(new Stmt\Return_(new String_("a\nb")))
|
|
));
|
|
$this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
|
|
}
|
|
|
|
public function testCommentBeforeInlineHTML() {
|
|
$prettyPrinter = new PrettyPrinter\Standard;
|
|
$comment = new Comment\Doc("/**\n * This is a comment\n */");
|
|
$stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
|
|
$expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
|
|
$this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
|
|
}
|
|
|
|
private function parseModeLine($modeLine) {
|
|
$parts = explode(' ', $modeLine, 2);
|
|
$version = isset($parts[0]) ? $parts[0] : 'both';
|
|
$options = isset($parts[1]) ? json_decode($parts[1], true) : [];
|
|
return [$version, $options];
|
|
}
|
|
|
|
public function testArraySyntaxDefault() {
|
|
$prettyPrinter = new Standard(['shortArraySyntax' => true]);
|
|
$expr = new Expr\Array_([
|
|
new Expr\ArrayItem(new String_('val'), new String_('key'))
|
|
]);
|
|
$expected = "['key' => 'val']";
|
|
$this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
|
|
}
|
|
}
|