2018-07-02 17:48:55 +00:00
|
|
|
package printer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
|
|
"github.com/z7zmey/php-parser/node/name"
|
|
|
|
"github.com/z7zmey/php-parser/node/stmt"
|
|
|
|
"github.com/z7zmey/php-parser/php7"
|
|
|
|
"github.com/z7zmey/php-parser/printer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExamplePrinter() {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
abstract class Bar extends Baz
|
|
|
|
{
|
|
|
|
public function greet()
|
|
|
|
{
|
|
|
|
echo "Hello";
|
|
|
|
// some comment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
// parse
|
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
php7parser := php7.NewParser([]byte(src), "7.4")
|
2019-02-25 14:52:47 +00:00
|
|
|
php7parser.WithFreeFloating()
|
2018-07-02 17:48:55 +00:00
|
|
|
php7parser.Parse()
|
|
|
|
|
|
|
|
rootNode := php7parser.GetRootNode()
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
// change namespace
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
parts := &rootNode.(*node.Root).Stmts[0].(*stmt.Namespace).NamespaceName.(*name.Name).Parts
|
|
|
|
*parts = append(*parts, &name.NamePart{Value: "Quuz"})
|
|
|
|
|
|
|
|
// print
|
|
|
|
|
|
|
|
p := printer.NewPrinter(os.Stdout)
|
|
|
|
p.Print(rootNode)
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
//<?php
|
|
|
|
//
|
|
|
|
// namespace Foo\Quuz;
|
|
|
|
//
|
|
|
|
// abstract class Bar extends Baz
|
|
|
|
// {
|
|
|
|
// public function greet()
|
|
|
|
// {
|
|
|
|
// echo "Hello";
|
|
|
|
// // some comment
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
func parse(src string) node.Node {
|
2019-12-26 15:57:56 +00:00
|
|
|
php7parser := php7.NewParser([]byte(src), "7.4")
|
2019-02-25 14:52:47 +00:00
|
|
|
php7parser.WithFreeFloating()
|
2018-07-02 17:48:55 +00:00
|
|
|
php7parser.Parse()
|
|
|
|
|
|
|
|
return php7parser.GetRootNode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func print(n node.Node) string {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(n)
|
|
|
|
|
|
|
|
return o.String()
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
// test node
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestParseAndPrintRoot(t *testing.T) {
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
src := ` <div>Hello</div>
|
|
|
|
<?php
|
|
|
|
$a;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintIdentifier(t *testing.T) {
|
|
|
|
|
|
|
|
src := `<?
|
|
|
|
/* Foo */
|
|
|
|
Foo ( ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintParameter(t *testing.T) {
|
|
|
|
|
|
|
|
src := `<?php
|
|
|
|
function & foo (
|
|
|
|
? int $a , & $b = null
|
|
|
|
, \ Foo ...$c
|
2018-10-25 10:32:18 +00:00
|
|
|
) : namespace \ Bar \ baz \ quuz{
|
2018-07-29 08:44:38 +00:00
|
|
|
;
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintNullable(t *testing.T) {
|
|
|
|
|
|
|
|
src := `<?php
|
|
|
|
function & foo ( ? int $a ) {
|
|
|
|
/* do nothing */
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintArgument(t *testing.T) {
|
|
|
|
|
|
|
|
src := `<?php
|
|
|
|
foo ( $a , $b
|
|
|
|
, ... $c ,
|
|
|
|
) ; `
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test name
|
|
|
|
|
|
|
|
func TestParseAndPrintNames(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
foo ( ) ;
|
|
|
|
\ foo ( ) ;
|
|
|
|
namespace \ foo ( ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test scalar
|
|
|
|
|
|
|
|
func TestParseAndPrintMagicConstant(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
__CLASS__ ;
|
|
|
|
__DIR__ ;
|
|
|
|
__FILE__ ;
|
|
|
|
__FUNCTION__ ;
|
|
|
|
__LINE__ ;
|
|
|
|
__NAMESPACE__ ;
|
|
|
|
__METHOD__ ;
|
|
|
|
__TRAIT__ ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintNumber(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
// LNumber
|
|
|
|
1234567890123456789 ;
|
|
|
|
|
|
|
|
// DNumber
|
|
|
|
12345678901234567890 ;
|
|
|
|
0. ;
|
|
|
|
.2 ;
|
|
|
|
0.2 ;
|
|
|
|
|
|
|
|
// binary LNumber
|
|
|
|
0b0111111111111111111111111111111111111111111111111111111111111111 ;
|
|
|
|
|
|
|
|
// binary DNumber
|
|
|
|
0b1111111111111111111111111111111111111111111111111111111111111111 ;
|
|
|
|
|
|
|
|
// HLNumber
|
|
|
|
0x007111111111111111 ;
|
|
|
|
|
|
|
|
// HDNumber
|
|
|
|
0x8111111111111111 ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintString(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
'Hello' ;
|
|
|
|
"Hello {$world } " ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintHeredoc(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
foo(<<<EAP
|
|
|
|
test
|
|
|
|
EAP
|
|
|
|
, 'test'
|
|
|
|
);
|
|
|
|
|
|
|
|
<<<EAP
|
|
|
|
test
|
|
|
|
EAP;
|
|
|
|
|
|
|
|
<<<'EAP'
|
|
|
|
test
|
|
|
|
EAP;
|
|
|
|
|
|
|
|
<<<"EAP"
|
|
|
|
test
|
|
|
|
EAP;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test assign
|
|
|
|
|
|
|
|
func TestParseAndPrintAssign(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a = $b ;
|
|
|
|
$a = & $b ;
|
|
|
|
$a &= $b ;
|
|
|
|
$a |= $b ;
|
|
|
|
$a ^= $b ;
|
2019-12-28 20:26:54 +00:00
|
|
|
$a ??= $b ;
|
2018-07-29 08:44:38 +00:00
|
|
|
$a .= $b ;
|
|
|
|
$a /= $b ;
|
|
|
|
$a -= $b ;
|
|
|
|
$a %= $b ;
|
|
|
|
$a *= $b ;
|
|
|
|
$a += $b ;
|
|
|
|
$a **= $b ;
|
|
|
|
$a <<= $b ;
|
|
|
|
$a >>= $b ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test binary
|
|
|
|
|
|
|
|
func TestParseAndPrintBinary(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$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 or $b ;
|
|
|
|
$a xor $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 ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test cast
|
|
|
|
|
|
|
|
func TestParseAndPrintCast(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
( array ) $a ;
|
|
|
|
( bool ) $a ;
|
|
|
|
( boolean ) $a ;
|
|
|
|
( real ) $a ;
|
|
|
|
( double ) $a ;
|
|
|
|
( float ) $a ;
|
|
|
|
( int ) $a ;
|
|
|
|
( integer ) $a ;
|
|
|
|
( object ) $a ;
|
|
|
|
( string ) $a ;
|
|
|
|
( binary ) $a ;
|
|
|
|
( unset ) $a ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test expr
|
|
|
|
|
|
|
|
func TestParseAndPrintArrayDimFetch(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
FOO [ ] ;
|
|
|
|
FOO [ 1 ] ;
|
|
|
|
$a [ ] ;
|
|
|
|
$a [ 1 ] ;
|
|
|
|
$a { 1 } ;
|
|
|
|
new $a [ ] ;
|
|
|
|
new $a [ 1 ] ;
|
|
|
|
new $a { 1 } ;
|
|
|
|
"$a[1]test" ;
|
|
|
|
"${ a [ 1 ] }test" ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintArrayItem(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$foo = [
|
|
|
|
$world ,
|
|
|
|
& $world ,
|
|
|
|
'Hello' => $world ,
|
2019-12-29 20:42:52 +00:00
|
|
|
... $unpack
|
2018-07-29 08:44:38 +00:00
|
|
|
] ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintArray(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
array ( /* empty array */ ) ;
|
|
|
|
array ( 0 , 2 => 2 ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintBitwiseNot(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
~ $var ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintBooleanNot(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
! $var ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintClassConstFetch(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
$var :: CONST ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
actual := print(parse(src))
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintClone(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
clone $var ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintClosureUse(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
$a = function ( ) use ( $a , & $b ) {
|
|
|
|
// do nothing
|
|
|
|
} ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
actual := print(parse(src))
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
2018-07-02 17:48:55 +00:00
|
|
|
}
|
2018-07-29 08:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintClosure(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a = static function & ( ) : void {
|
|
|
|
// do nothing
|
|
|
|
} ;
|
2018-07-02 17:48:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:26:54 +00:00
|
|
|
func TestParseAndPrintArrowFunction(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a = static fn & ( $b ) : void => $c ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintConstFetch(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
null ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintEmpty(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
empty ( $a ) ;`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintErrorSuppress(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
@ foo ( ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintEval(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
eval ( " " ) ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintExit(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
exit ;
|
|
|
|
exit ( ) ;
|
2019-02-13 20:18:07 +00:00
|
|
|
exit (1) ;
|
2018-07-29 08:44:38 +00:00
|
|
|
exit ( 1 );
|
|
|
|
die ;
|
|
|
|
die ( ) ;
|
2019-02-13 20:18:07 +00:00
|
|
|
die (1) ;
|
2018-07-29 08:44:38 +00:00
|
|
|
die ( 1 );
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintFunctionCall(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
foo ( ) ;
|
|
|
|
$var ( $a , ... $b , $c ) ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintInclude(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
src := `<?php
|
|
|
|
include 'foo' ;
|
2018-07-29 08:44:38 +00:00
|
|
|
include_once 'bar' ;`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintInstanceOf(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
$a instanceof Foo ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintIsset(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
isset ( $a , $b [ 2 ] , ) ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintList(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
list( , $var , ) = $b ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintMethodCall(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a -> bar ( $arg , ) ;`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintNew(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
new Foo ;
|
|
|
|
|
|
|
|
new Foo ( $a, $b ) ;
|
|
|
|
|
|
|
|
new class ( $c ) extends Foo implements Bar , Baz {
|
|
|
|
|
|
|
|
} ; `
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintIncDec(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
++ $a ;
|
|
|
|
-- $a ;
|
|
|
|
$a ++ ;
|
|
|
|
$a -- ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintPrint(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
print $a ;
|
2018-07-02 17:48:55 +00:00
|
|
|
print ( $a ) ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintPropertyFetch(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
$a -> b ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintReference(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a = & $b ;
|
|
|
|
$a = [ & $b ] ;
|
|
|
|
$a = [ $b => & $c ] ;
|
|
|
|
|
|
|
|
$a = function ( ) use ( & $b ) {
|
|
|
|
// do nothing
|
|
|
|
} ;
|
|
|
|
|
|
|
|
foreach ( $a as & $b ) {
|
|
|
|
// do nothing
|
2018-07-02 17:48:55 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintRequire(t *testing.T) {
|
|
|
|
|
|
|
|
src := `<?php
|
|
|
|
require __DIR__ . '/folder' ;
|
|
|
|
require_once $a ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintShellExec(t *testing.T) {
|
|
|
|
src := "<?php ` {$v} cmd ` ; "
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintShortArray(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a = [ ] ;
|
|
|
|
$a = [ 0 ] ;
|
|
|
|
$a = [
|
|
|
|
1 => & $b , // one
|
|
|
|
$c , /* two */
|
|
|
|
] ;
|
|
|
|
$a = [0, 1, 2] ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintShortList(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
[
|
|
|
|
/* skip */,
|
|
|
|
$b
|
|
|
|
/* skip */,
|
|
|
|
] = $a ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintStaticCall(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
Foo :: bar ( $a , $b ) ;`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintStaticPropertyFetch(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
|
|
|
Foo :: $bar ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintTernary(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a ? $b : $c ;
|
|
|
|
$a ? : $c ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintUnary(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
- $a ;
|
|
|
|
+ $a ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintVariable(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
$ /* variable variable comment */ $var ; `
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintYield(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
yield $a ;
|
|
|
|
yield $k => $v ;
|
|
|
|
yield from $a ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
// test stmt
|
|
|
|
|
2018-07-09 21:51:02 +00:00
|
|
|
func TestParseAndPrintAltIf(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
|
|
|
if ( 1 ) :
|
2018-07-29 08:44:38 +00:00
|
|
|
// do nothing
|
2018-07-02 17:48:55 +00:00
|
|
|
elseif ( 2 ) :
|
2018-07-29 08:44:38 +00:00
|
|
|
elseif ( 3 ) :
|
|
|
|
;
|
2018-07-02 17:48:55 +00:00
|
|
|
else :
|
|
|
|
;
|
|
|
|
endif ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintAltFor(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
for ( $a ; $b ; $c ) :
|
|
|
|
endfor ;
|
|
|
|
|
|
|
|
for ( ; ; ) :
|
|
|
|
endfor ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintAltForeach(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
foreach ( $a as $k => & $v ) :
|
|
|
|
echo $v ;
|
|
|
|
endforeach ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintAltSwitch(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
switch ( $a ) :
|
2018-07-02 17:48:55 +00:00
|
|
|
case 1 :
|
2018-07-29 08:44:38 +00:00
|
|
|
;
|
|
|
|
case 2 : ;
|
2018-07-02 17:48:55 +00:00
|
|
|
case 3 :
|
2018-07-29 08:44:38 +00:00
|
|
|
;
|
2018-07-02 17:48:55 +00:00
|
|
|
default :
|
2018-07-29 08:44:38 +00:00
|
|
|
;
|
|
|
|
endswitch ;
|
|
|
|
|
|
|
|
|
|
|
|
switch ( $a ) : ;
|
|
|
|
case 1 ; ;
|
|
|
|
default ; ;
|
|
|
|
endswitch ;
|
|
|
|
|
|
|
|
switch ( $a ) :
|
|
|
|
endswitch ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintAltWhile(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
while ( $a ) :
|
2018-07-29 08:44:38 +00:00
|
|
|
// do nothing
|
2018-07-02 17:48:55 +00:00
|
|
|
endwhile ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintBreak(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
break ;
|
|
|
|
break 1 ;
|
|
|
|
break ( 2 ) ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
actual := print(parse(src))
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintClassMethod(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* abstract method
|
|
|
|
*/
|
|
|
|
public static function & greet ( ? Foo $a ) : void ;
|
|
|
|
|
|
|
|
function greet ( string $a )
|
|
|
|
{
|
|
|
|
return 'hello' ;
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintClass(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
abstract final class Foo extends Bar implements Baz , Quuz {
|
|
|
|
|
|
|
|
}
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
new class ( $c ) extends Foo implements Bar , Baz {
|
|
|
|
|
|
|
|
} ;`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintClassConstList(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
class Foo {
|
|
|
|
public const FOO = 'f' , BAR = 'b' ;
|
2018-07-02 17:48:55 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintConstList(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
const FOO = 1 , BAR = 2 ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintContinue(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
continue ;
|
|
|
|
continue 1 ;
|
|
|
|
continue ( 2 ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintDeclare(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
declare ( FOO = 'bar' ) ;
|
|
|
|
declare ( FOO = 'bar' ) $a ;
|
|
|
|
declare ( FOO = 'bar' ) { }
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
declare ( FOO = 'bar' ) : enddeclare ;
|
|
|
|
declare ( FOO = 'bar' ) :
|
|
|
|
;
|
|
|
|
enddeclare ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintDoWhile(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
do {
|
2018-07-02 17:48:55 +00:00
|
|
|
;
|
2018-07-29 08:44:38 +00:00
|
|
|
} while ( $a ) ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintEcho(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
echo '' ;
|
|
|
|
echo $a , ' ' , PHP_EOL;
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
<?= $a, $b ?>
|
|
|
|
<?= $c ;
|
|
|
|
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintIfExpression(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
$a ; `
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintFor(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
for ( $i = 0 ; $i < 3 ; $i ++ )
|
|
|
|
echo $i . PHP_EOL;
|
|
|
|
|
|
|
|
for ( ; ; ) {
|
|
|
|
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintForeach(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
foreach ( $a as $k => & $v ) {
|
|
|
|
;
|
|
|
|
}`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintFunction(t *testing.T) {
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
function & foo ( ) : void {
|
|
|
|
;
|
|
|
|
}`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintGlobal(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
global $a , $b ;`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintGoto(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
goto Foo ;`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintGroupUse(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
use function Foo \ { Bar as Baz , Quuz , } ;
|
|
|
|
use Foo \ { function Bar as Baz , const Quuz } ;
|
|
|
|
use \ Foo \ { function Bar as Baz , const Quuz , } ;
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintHaltCompiler(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-09-13 17:07:50 +00:00
|
|
|
__halt_compiler ( ) ;
|
|
|
|
this text is ignored by parser
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintIfElseIfElse(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
if ( 1 ) ;
|
|
|
|
elseif ( 2 ) {
|
|
|
|
;
|
2018-07-02 17:48:55 +00:00
|
|
|
}
|
2018-07-29 08:44:38 +00:00
|
|
|
else if ( 3 ) $a;
|
|
|
|
else { }`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintInlineHtml(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
$a;?>test<? `
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:20:20 +00:00
|
|
|
func TestParseAndPrintShebang(t *testing.T) {
|
|
|
|
src := `#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
$a;?>test<? `
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintInterface(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
interface Foo extends Bar , Baz {
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintGotoLabel(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
Foo : $b ; `
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintNamespace(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
namespace Foo \ Bar ;
|
|
|
|
namespace Baz {
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
}
|
2018-07-29 08:44:38 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
}
|
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintNop(t *testing.T) {
|
|
|
|
src := `<?php ; `
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintPropertyList(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
class Foo {
|
|
|
|
var $a = '' , $b = null ;
|
|
|
|
private $c ;
|
2019-12-29 14:36:56 +00:00
|
|
|
public static Bar $d ;
|
2018-07-29 08:44:38 +00:00
|
|
|
|
|
|
|
}`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintReturn(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
class Foo {
|
|
|
|
function bar ( )
|
|
|
|
{
|
|
|
|
return null ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
function foo ( )
|
2018-07-29 08:44:38 +00:00
|
|
|
{
|
|
|
|
return $a ;
|
|
|
|
}
|
2019-02-13 20:18:07 +00:00
|
|
|
|
|
|
|
function bar ( )
|
|
|
|
{
|
|
|
|
return ;
|
|
|
|
}
|
2018-07-29 08:44:38 +00:00
|
|
|
`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintStaticVar(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
static $a , $b = foo ( ) ;
|
2018-07-02 17:48:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintStmtList(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
2018-07-02 17:48:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintSwitch(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
switch ( $a ) {
|
|
|
|
case 1 : ;
|
|
|
|
default : ;
|
|
|
|
}
|
|
|
|
switch ( $a ) { ;
|
|
|
|
case 1 ; ;
|
|
|
|
default ; ;
|
|
|
|
}`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintThrow(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
throw new \ Exception ( "msg" ) ;`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintTraitUse(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
class foo {
|
|
|
|
use \ foo , bar ;
|
|
|
|
use foo , \ bar { }
|
|
|
|
use \ foo , \ bar {
|
|
|
|
foo :: a as b ;
|
|
|
|
bar :: a insteadof foo ;
|
|
|
|
foo :: c as public ;
|
|
|
|
foo :: d as public e;
|
|
|
|
f as g ;
|
|
|
|
}
|
|
|
|
}`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintTrait(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
trait foo {
|
|
|
|
function bar ( ) { }
|
|
|
|
}`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintTryCatchFinally(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
try {
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
} catch ( \ Exception | \ Foo \ Bar $e) {
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
} finally {
|
|
|
|
|
|
|
|
}`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintUnset(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
unset ( $a ) ;
|
|
|
|
unset ( $a , $b , ) ;`
|
2018-07-02 17:48:55 +00:00
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintUseList(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
use Foo ;
|
|
|
|
use \ Foo as Bar ;
|
|
|
|
use function \ Foo as Bar ;
|
|
|
|
use const Foo as Bar, baz ;
|
2018-07-02 17:48:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintWhile(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
while ( $a ) echo '' ;
|
|
|
|
while ( $a ) { }
|
|
|
|
while ( $a ) ;
|
2018-07-02 17:48:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
// other
|
|
|
|
|
|
|
|
func TestParseAndPrintParentheses(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
$b = (($a));
|
|
|
|
$b = ( ($a) );
|
|
|
|
$b = ( ( $a ) );
|
|
|
|
$b = ( ($a ));
|
|
|
|
$b = (( $a) );
|
|
|
|
|
|
|
|
( $a + $b ) * 2 ;
|
|
|
|
( $ $foo . 'foo' ) :: { $bar . 'bar' } ( ) ;
|
|
|
|
( $ $foo ) [ 'bar' ] ;
|
|
|
|
$ { $a . 'b' } -> call ( ) ;
|
|
|
|
$a -> { $b . 'b' } ;
|
2018-07-02 17:48:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintComplexString1(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
// "test $foo" ;
|
|
|
|
"test $foo[1]" ;
|
2018-10-29 13:54:04 +00:00
|
|
|
"test $foo[-1]" ;
|
|
|
|
"test $foo[112345678901234567890] " ;
|
|
|
|
"test $foo[-112345678901234567890] " ;
|
2018-07-29 08:44:38 +00:00
|
|
|
"test $foo[a]" ;
|
|
|
|
"test $foo[$bar]" ;
|
|
|
|
"test $foo->bar" ;
|
2018-07-02 17:48:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintComplexString2(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
"test ${ foo }" ;
|
|
|
|
"test ${ foo . 'bar' }" ;
|
|
|
|
"test ${ foo [ ] }" ;
|
2018-10-29 13:39:15 +00:00
|
|
|
"test ${ foo [ $b ] }" ;
|
2018-07-29 08:44:38 +00:00
|
|
|
"test ${ foo [ 1 ] }" ;
|
|
|
|
"test ${ foo [ 'expr' . $bar ] }" ;
|
|
|
|
"test ${ $foo }" ;
|
|
|
|
"test ${ $foo -> bar }" ;
|
|
|
|
"test ${ $foo -> bar ( ) }" ;
|
|
|
|
"test ${ $a . '' }" ;
|
2018-07-02 17:48:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintComplexString3(t *testing.T) {
|
2018-10-29 13:39:15 +00:00
|
|
|
src := `<?php
|
|
|
|
"test ${foo}" ;
|
|
|
|
"test ${foo[0]}";
|
2018-12-17 13:24:13 +00:00
|
|
|
"test ${foo::$bar}";
|
2018-10-29 13:39:15 +00:00
|
|
|
"test ${foo }" ;
|
|
|
|
"test ${foo . 'bar' }" ;
|
|
|
|
"test ${foo [ ] }" ;
|
|
|
|
"test ${foo [ $b ] }" ;
|
|
|
|
"test ${foo [ 1 ] }" ;
|
|
|
|
"test ${foo [ 'expr' . $bar ] }" ;
|
|
|
|
"test ${$foo }" ;
|
|
|
|
"test ${$foo -> bar }" ;
|
|
|
|
"test ${$foo -> bar ( ) }" ;
|
|
|
|
"test ${$a . '' }" ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintComplexString4(t *testing.T) {
|
2018-07-02 17:48:55 +00:00
|
|
|
src := `<?php
|
2018-07-29 08:44:38 +00:00
|
|
|
"test {$foo }" ;
|
|
|
|
"test {$foo [ ] }" ;
|
|
|
|
"test {$foo [ 1 ] }" ;
|
|
|
|
"test {$foo -> bar }" ;
|
|
|
|
"test {$foo -> bar ( ) }" ;
|
2018-07-02 17:48:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
actual := print(parse(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|