mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-27 04:14:44 +01:00
Add some tests for pretty printing expressions
This commit is contained in:
parent
54275ad181
commit
6930feac43
@ -9,7 +9,7 @@ Version 1.1.0 (2015-01-18)
|
||||
* Methods that do not specify an explicit visibility (e.g. `function method()`) will now have the `MODIFIER_PUBLIC`
|
||||
flag set. This also means that their `isPublic()` method will return true.
|
||||
|
||||
* Declaring a property as abstract or static is now an error.
|
||||
* Declaring a property as abstract or final is now an error.
|
||||
|
||||
* The `Lexer` and `Lexer\Emulative` classes now accept an `$options` array in their constructors. Currently only the
|
||||
`usedAttributes` option is supported, which determines which attributes will be added to AST nodes. In particular
|
||||
|
@ -2,13 +2,18 @@
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
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, $dump) {
|
||||
$parser = new Parser(new Lexer\Emulative);
|
||||
$prettyPrinter = new PrettyPrinter\Standard;
|
||||
$prettyPrinter = new Standard;
|
||||
|
||||
$stmts = $parser->parse($code);
|
||||
$this->assertSame(
|
||||
@ -41,4 +46,18 @@ class PrettyPrinterTest extends CodeTestAbstract
|
||||
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));
|
||||
}
|
||||
}
|
19
test/code/prettyPrinter/list.test
Normal file
19
test/code/prettyPrinter/list.test
Normal file
@ -0,0 +1,19 @@
|
||||
list()
|
||||
-----
|
||||
<?php
|
||||
|
||||
list() = $a;
|
||||
list($a) = $b;
|
||||
list($a, $b, $c) = $d;
|
||||
list(, $a) = $b;
|
||||
list(, , $a, , $b) = $c;
|
||||
list(list($a)) = $b;
|
||||
list(, list(, list(, $a), $b)) = $c;
|
||||
-----
|
||||
list() = $a;
|
||||
list($a) = $b;
|
||||
list($a, $b, $c) = $d;
|
||||
list(, $a) = $b;
|
||||
list(, , $a, , $b) = $c;
|
||||
list(list($a)) = $b;
|
||||
list(, list(, list(, $a), $b)) = $c;
|
135
test/code/prettyPrinter/literals.test
Normal file
135
test/code/prettyPrinter/literals.test
Normal file
@ -0,0 +1,135 @@
|
||||
Literals
|
||||
-----
|
||||
<?php
|
||||
|
||||
// magic constants
|
||||
__LINE__;
|
||||
__FILE__;
|
||||
__DIR__;
|
||||
__FUNCTION__;
|
||||
__CLASS__;
|
||||
__TRAIT__;
|
||||
__METHOD__;
|
||||
__NAMESPACE__;
|
||||
|
||||
// not actually literals, but close
|
||||
null;
|
||||
true;
|
||||
false;
|
||||
NULL;
|
||||
TRUE;
|
||||
FALSE;
|
||||
|
||||
// integers (normalized to decimal)
|
||||
0;
|
||||
11;
|
||||
011;
|
||||
0x11;
|
||||
0b11;
|
||||
|
||||
// floats (normalized to ... something)
|
||||
0.;
|
||||
.0;
|
||||
0.0;
|
||||
0e1000;
|
||||
1.0;
|
||||
1e100;
|
||||
1e1000;
|
||||
1E-100;
|
||||
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
|
||||
|
||||
// strings (normalized to single quoted)
|
||||
'a';
|
||||
'a
|
||||
b';
|
||||
"a";
|
||||
"a\nb";
|
||||
'a\'b';
|
||||
"a'b";
|
||||
"a\b";
|
||||
<<<'STR'
|
||||
a\nb$a
|
||||
{$b}
|
||||
STR;
|
||||
|
||||
// strings (normalized to double quoted)
|
||||
"$a";
|
||||
"a$b";
|
||||
"a${b}c";
|
||||
"a{$b}c";
|
||||
"a$a[b]c";
|
||||
<<<STR
|
||||
a\nb$a\n{$b}
|
||||
STR;
|
||||
|
||||
// make sure indentation doesn't mess anything up
|
||||
function foo()
|
||||
{
|
||||
"a\nb";
|
||||
'a
|
||||
b';
|
||||
'a
|
||||
b';
|
||||
}
|
||||
-----
|
||||
// magic constants
|
||||
__LINE__;
|
||||
__FILE__;
|
||||
__DIR__;
|
||||
__FUNCTION__;
|
||||
__CLASS__;
|
||||
__TRAIT__;
|
||||
__METHOD__;
|
||||
__NAMESPACE__;
|
||||
// not actually literals, but close
|
||||
null;
|
||||
true;
|
||||
false;
|
||||
NULL;
|
||||
TRUE;
|
||||
FALSE;
|
||||
// integers (normalized to decimal)
|
||||
0;
|
||||
11;
|
||||
9;
|
||||
17;
|
||||
3;
|
||||
// floats (normalized to ... something)
|
||||
0.0;
|
||||
0.0;
|
||||
0.0;
|
||||
0.0;
|
||||
1.0;
|
||||
1.0E+100;
|
||||
INF;
|
||||
1.0E-100;
|
||||
1.0E+84;
|
||||
// strings (normalized to single quoted)
|
||||
'a';
|
||||
'a
|
||||
b';
|
||||
'a';
|
||||
'a
|
||||
b';
|
||||
'a\'b';
|
||||
'a\'b';
|
||||
'a\\b';
|
||||
'a\\nb$a
|
||||
{$b}';
|
||||
// strings (normalized to double quoted)
|
||||
"{$a}";
|
||||
"a{$b}";
|
||||
"a{$b}c";
|
||||
"a{$b}c";
|
||||
"a{$a['b']}c";
|
||||
"a\nb{$a}\n{$b}";
|
||||
// make sure indentation doesn't mess anything up
|
||||
function foo()
|
||||
{
|
||||
'a
|
||||
b';
|
||||
'a
|
||||
b';
|
||||
'a
|
||||
b';
|
||||
}
|
140
test/code/prettyPrinter/operators.test
Normal file
140
test/code/prettyPrinter/operators.test
Normal file
@ -0,0 +1,140 @@
|
||||
Basic operators
|
||||
-----
|
||||
<?php
|
||||
|
||||
$a ** $b;
|
||||
|
||||
++$a;
|
||||
--$a;
|
||||
$a++;
|
||||
$a--;
|
||||
|
||||
@$a;
|
||||
~$a;
|
||||
-$a;
|
||||
+$a;
|
||||
|
||||
(int) $a;
|
||||
(integer) $a;
|
||||
(float) $a;
|
||||
(double) $a;
|
||||
(real) $a;
|
||||
(string) $a;
|
||||
(binary) $a;
|
||||
(array) $a;
|
||||
(object) $a;
|
||||
(bool) $a;
|
||||
(boolean) $a;
|
||||
(unset) $a;
|
||||
|
||||
$a * $b;
|
||||
$a / $b;
|
||||
$a % $b;
|
||||
$a + $b;
|
||||
$a - $b;
|
||||
$a . $b;
|
||||
$a << $b;
|
||||
$a >> $b;
|
||||
$a < $b;
|
||||
$a <= $b;
|
||||
$a > $b;
|
||||
$a >= $b;
|
||||
$a == $b;
|
||||
$a != $b;
|
||||
$a <> $b;
|
||||
$a === $b;
|
||||
$a !== $b;
|
||||
$a & $b;
|
||||
$a ^ $b;
|
||||
$a | $b;
|
||||
$a && $b;
|
||||
$a || $b;
|
||||
$a ? $b : $c;
|
||||
$a ?: $c;
|
||||
|
||||
$a = $b;
|
||||
$a **= $b;
|
||||
$a *= $b;
|
||||
$a /= $b;
|
||||
$a %= $b;
|
||||
$a += $b;
|
||||
$a -= $b;
|
||||
$a .= $b;
|
||||
$a <<= $b;
|
||||
$a >>= $b;
|
||||
$a &= $b;
|
||||
$a ^= $b;
|
||||
$a |= $b;
|
||||
$a =& $b;
|
||||
|
||||
$a and $b;
|
||||
$a xor $b;
|
||||
$a or $b;
|
||||
|
||||
$a instanceof Foo;
|
||||
$a instanceof $b;
|
||||
-----
|
||||
$a ** $b;
|
||||
++$a;
|
||||
--$a;
|
||||
$a++;
|
||||
$a--;
|
||||
@$a;
|
||||
~$a;
|
||||
-$a;
|
||||
+$a;
|
||||
(int) $a;
|
||||
(int) $a;
|
||||
(double) $a;
|
||||
(double) $a;
|
||||
(double) $a;
|
||||
(string) $a;
|
||||
(string) $a;
|
||||
(array) $a;
|
||||
(object) $a;
|
||||
(bool) $a;
|
||||
(bool) $a;
|
||||
(unset) $a;
|
||||
$a * $b;
|
||||
$a / $b;
|
||||
$a % $b;
|
||||
$a + $b;
|
||||
$a - $b;
|
||||
$a . $b;
|
||||
$a << $b;
|
||||
$a >> $b;
|
||||
$a < $b;
|
||||
$a <= $b;
|
||||
$a > $b;
|
||||
$a >= $b;
|
||||
$a == $b;
|
||||
$a != $b;
|
||||
$a != $b;
|
||||
$a === $b;
|
||||
$a !== $b;
|
||||
$a & $b;
|
||||
$a ^ $b;
|
||||
$a | $b;
|
||||
$a && $b;
|
||||
$a || $b;
|
||||
$a ? $b : $c;
|
||||
$a ?: $c;
|
||||
$a = $b;
|
||||
$a **= $b;
|
||||
$a *= $b;
|
||||
$a /= $b;
|
||||
$a %= $b;
|
||||
$a += $b;
|
||||
$a -= $b;
|
||||
$a .= $b;
|
||||
$a <<= $b;
|
||||
$a >>= $b;
|
||||
$a &= $b;
|
||||
$a ^= $b;
|
||||
$a |= $b;
|
||||
$a =& $b;
|
||||
$a and $b;
|
||||
$a xor $b;
|
||||
$a or $b;
|
||||
$a instanceof Foo;
|
||||
$a instanceof $b;
|
@ -21,13 +21,18 @@ $a ? $b : ($c ? $d : ($e ? $f : $g));
|
||||
$a ? $b ? $c : $d : $f;
|
||||
|
||||
(1 > 0) > (1 < 0);
|
||||
++$a + $b;
|
||||
$a + $b++;
|
||||
|
||||
$a ** $b ** $c;
|
||||
($a ** $b) ** $c;
|
||||
-1 ** 2;
|
||||
|
||||
// this will currently unnecessarily print !($a = $b). This is correct as far as operator precedence is concerned, but
|
||||
// the parentheses are not really necessary, because = takes only variables as the left hand side operator.
|
||||
// The following will currently add unnecessary parentheses, because the pretty printer is not aware that assignment
|
||||
// and incdec only work on variables.
|
||||
!$a = $b;
|
||||
++$a ** $b;
|
||||
$a ** $b++;
|
||||
-----
|
||||
echo 'abc' . 'cde' . 'fgh';
|
||||
echo 'abc' . ('cde' . 'fgh');
|
||||
@ -43,8 +48,13 @@ $a ? $b : $c ? $d : $e ? $f : $g;
|
||||
$a ? $b : ($c ? $d : ($e ? $f : $g));
|
||||
$a ? $b ? $c : $d : $f;
|
||||
(1 > 0) > (1 < 0);
|
||||
++$a + $b;
|
||||
$a + $b++;
|
||||
$a ** $b ** $c;
|
||||
($a ** $b) ** $c;
|
||||
// this will currently unnecessarily print !($a = $b). This is correct as far as operator precedence is concerned, but
|
||||
// the parentheses are not really necessary, because = takes only variables as the left hand side operator.
|
||||
-1 ** 2;
|
||||
// The following will currently add unnecessary parentheses, because the pretty printer is not aware that assignment
|
||||
// and incdec only work on variables.
|
||||
!($a = $b);
|
||||
(++$a) ** $b;
|
||||
$a ** ($b++);
|
||||
|
70
test/code/prettyPrinter/variables.test
Normal file
70
test/code/prettyPrinter/variables.test
Normal file
@ -0,0 +1,70 @@
|
||||
Variables
|
||||
-----
|
||||
<?php
|
||||
|
||||
$a;
|
||||
$$a;
|
||||
${$a};
|
||||
$a->b;
|
||||
$a->b();
|
||||
$a->b($c);
|
||||
$a->$b();
|
||||
$a->{$b}();
|
||||
$a->$b[$c]();
|
||||
$$a->b;
|
||||
$a[$b];
|
||||
$a[$b]();
|
||||
$$a[$b];
|
||||
$a::B;
|
||||
$a::$b;
|
||||
$a::b();
|
||||
$a::b($c);
|
||||
$a::$b();
|
||||
$a::$b[$c];
|
||||
$a::$b[$c]($d);
|
||||
$a::{$b[$c]}($d);
|
||||
$a::{$b->c}();
|
||||
a();
|
||||
$a();
|
||||
$a()[$b];
|
||||
$a->b()[$c];
|
||||
$a::$b()[$c];
|
||||
(new A)->b;
|
||||
(new A())->b();
|
||||
(new $$a)[$b];
|
||||
(new $a->b)->c;
|
||||
|
||||
global $a, $$a, $$a[$b], $$a->b;
|
||||
-----
|
||||
$a;
|
||||
${$a};
|
||||
${$a};
|
||||
$a->b;
|
||||
$a->b();
|
||||
$a->b($c);
|
||||
$a->{$b}();
|
||||
$a->{$b}();
|
||||
$a->{$b[$c]}();
|
||||
${$a}->b;
|
||||
$a[$b];
|
||||
$a[$b]();
|
||||
${$a[$b]};
|
||||
$a::B;
|
||||
$a::$b;
|
||||
$a::b();
|
||||
$a::b($c);
|
||||
$a::$b();
|
||||
$a::$b[$c];
|
||||
$a::$b[$c]($d);
|
||||
$a::$b[$c]($d);
|
||||
$a::{$b->c}();
|
||||
a();
|
||||
$a();
|
||||
$a()[$b];
|
||||
$a->b()[$c];
|
||||
$a::$b()[$c];
|
||||
(new A())->b;
|
||||
(new A())->b();
|
||||
(new ${$a}())[$b];
|
||||
(new $a->b())->c;
|
||||
global $a, ${$a}, ${$a[$b]}, ${$a->b};
|
Loading…
Reference in New Issue
Block a user