2012-05-11 16:18:14 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
2012-05-11 16:18:14 +02:00
|
|
|
|
2015-01-18 19:57:09 +01:00
|
|
|
use PhpParser\Node\Expr;
|
2015-03-20 21:47:20 +01:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
2015-01-18 19:57:09 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\PrettyPrinter\Standard;
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
require_once __DIR__ . '/CodeTestAbstract.php';
|
|
|
|
|
|
|
|
class PrettyPrinterTest extends CodeTestAbstract
|
2012-05-11 16:18:14 +02:00
|
|
|
{
|
2015-09-21 09:12:59 +02:00
|
|
|
protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
|
2015-06-13 19:46:01 +02:00
|
|
|
$lexer = new Lexer\Emulative;
|
|
|
|
$parser5 = new Parser\Php5($lexer);
|
|
|
|
$parser7 = new Parser\Php7($lexer);
|
2015-09-21 09:12:59 +02:00
|
|
|
|
|
|
|
list($version, $options) = $this->parseModeLine($modeLine);
|
|
|
|
$prettyPrinter = new Standard($options);
|
2012-05-11 16:18:14 +02:00
|
|
|
|
2015-06-13 19:46:01 +02:00
|
|
|
try {
|
2015-06-20 11:43:16 +02:00
|
|
|
$output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
|
2015-06-13 19:46:01 +02:00
|
|
|
} catch (Error $e) {
|
|
|
|
$output5 = null;
|
2015-09-21 09:12:59 +02:00
|
|
|
$this->assertEquals('php7', $version);
|
2015-06-13 19:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-06-20 11:43:16 +02:00
|
|
|
$output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
|
2015-06-13 19:46:01 +02:00
|
|
|
} catch (Error $e) {
|
|
|
|
$output7 = null;
|
2015-09-21 09:12:59 +02:00
|
|
|
$this->assertEquals('php5', $version);
|
2015-06-13 19:46:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-21 09:12:59 +02:00
|
|
|
if ('php5' === $version) {
|
2015-06-13 19:46:01 +02:00
|
|
|
$this->assertSame($expected, $output5, $name);
|
|
|
|
$this->assertNotSame($expected, $output7, $name);
|
2015-09-21 09:12:59 +02:00
|
|
|
} else if ('php7' === $version) {
|
2015-06-13 19:46:01 +02:00
|
|
|
$this->assertSame($expected, $output7, $name);
|
|
|
|
$this->assertNotSame($expected, $output5, $name);
|
|
|
|
} else {
|
|
|
|
$this->assertSame($expected, $output5, $name);
|
|
|
|
$this->assertSame($expected, $output7, $name);
|
|
|
|
}
|
2012-05-11 16:18:14 +02:00
|
|
|
}
|
|
|
|
|
2013-04-15 20:53:23 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestPrettyPrint
|
2014-09-28 12:48:55 +02:00
|
|
|
* @covers PhpParser\PrettyPrinter\Standard<extended>
|
2013-04-15 20:53:23 +02:00
|
|
|
*/
|
2015-06-13 19:46:01 +02:00
|
|
|
public function testPrettyPrint($name, $code, $expected, $mode) {
|
|
|
|
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
|
2013-04-15 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestPrettyPrintFile
|
2014-09-28 12:48:55 +02:00
|
|
|
* @covers PhpParser\PrettyPrinter\Standard<extended>
|
2013-04-15 20:53:23 +02:00
|
|
|
*/
|
2015-06-13 19:46:01 +02:00
|
|
|
public function testPrettyPrintFile($name, $code, $expected, $mode) {
|
|
|
|
$this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
|
2013-04-15 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
2012-05-11 16:18:14 +02:00
|
|
|
public function provideTestPrettyPrint() {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
|
2012-05-11 16:18:14 +02:00
|
|
|
}
|
2013-04-15 20:53:23 +02:00
|
|
|
|
|
|
|
public function provideTestPrettyPrintFile() {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
|
2013-04-15 20:53:23 +02:00
|
|
|
}
|
2015-01-18 19:57:09 +01:00
|
|
|
|
|
|
|
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(
|
2015-03-20 21:47:20 +01:00
|
|
|
'stmts' => array(new Stmt\Return_(new String_("a\nb")))
|
2015-01-18 19:57:09 +01:00
|
|
|
));
|
|
|
|
$this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
|
|
|
|
}
|
2015-09-21 09:12:59 +02:00
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2015-01-18 19:57:09 +01:00
|
|
|
}
|