2018-07-29 08:44:38 +00:00
|
|
|
package printer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-29 20:00:56 +00:00
|
|
|
"github.com/z7zmey/php-parser/pkg/ast"
|
|
|
|
|
2020-05-17 19:56:32 +00:00
|
|
|
"github.com/z7zmey/php-parser/internal/php5"
|
2020-06-29 20:00:56 +00:00
|
|
|
"github.com/z7zmey/php-parser/internal/scanner"
|
2020-05-17 19:56:32 +00:00
|
|
|
"github.com/z7zmey/php-parser/pkg/printer"
|
2018-07-29 08:44:38 +00:00
|
|
|
)
|
|
|
|
|
2020-05-17 19:56:32 +00:00
|
|
|
func parsePhp5(src string) ast.Vertex {
|
2020-08-17 17:31:04 +00:00
|
|
|
lexer := scanner.NewLexer([]byte(src), "5.6", nil)
|
2020-07-02 21:20:32 +00:00
|
|
|
php5parser := php5.NewParser(lexer, nil)
|
2018-07-29 08:44:38 +00:00
|
|
|
php5parser.Parse()
|
|
|
|
|
|
|
|
return php5parser.GetRootNode()
|
|
|
|
}
|
|
|
|
|
2020-05-17 19:56:32 +00:00
|
|
|
func printPhp5(n ast.Vertex) string {
|
2018-07-29 08:44:38 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(n)
|
|
|
|
|
|
|
|
return o.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// test node
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Root(t *testing.T) {
|
|
|
|
|
|
|
|
src := ` <div>Hello</div>
|
|
|
|
<?php
|
|
|
|
$a;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Identifier(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<? ;
|
2018-07-29 08:44:38 +00:00
|
|
|
/* Foo */
|
|
|
|
Foo ( ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Parameter(t *testing.T) {
|
|
|
|
|
|
|
|
src := `<?php
|
|
|
|
function & foo ( $a , & $b = null , ... $c ) {
|
|
|
|
;
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Argument(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
foo ( $a , $b
|
|
|
|
, $c
|
|
|
|
) ; `
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test name
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Names(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
foo ( ) ;
|
|
|
|
\ foo ( ) ;
|
|
|
|
namespace \ foo ( ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test scalar
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5MagicConstant(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
__CLASS__ ;
|
|
|
|
__DIR__ ;
|
|
|
|
__FILE__ ;
|
|
|
|
__FUNCTION__ ;
|
|
|
|
__LINE__ ;
|
|
|
|
__NAMESPACE__ ;
|
|
|
|
__METHOD__ ;
|
|
|
|
__TRAIT__ ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Number(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
// LNumber
|
|
|
|
1234567890123456789 ;
|
|
|
|
|
|
|
|
// DNumber
|
|
|
|
12345678901234567890 ;
|
|
|
|
0. ;
|
|
|
|
.2 ;
|
|
|
|
0.2 ;
|
|
|
|
|
|
|
|
// binary LNumber
|
|
|
|
0b0111111111111111111111111111111111111111111111111111111111111111 ;
|
|
|
|
|
|
|
|
// binary DNumber
|
|
|
|
0b1111111111111111111111111111111111111111111111111111111111111111 ;
|
|
|
|
|
|
|
|
// HLNumber
|
|
|
|
0x007111111111111111 ;
|
|
|
|
|
|
|
|
// HDNumber
|
|
|
|
0x8111111111111111 ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5String(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
'Hello' ;
|
|
|
|
"Hello {$world } " ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Heredoc(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
foo(<<<EAP
|
|
|
|
test
|
|
|
|
EAP
|
|
|
|
, 'test'
|
|
|
|
);
|
|
|
|
|
|
|
|
<<<EAP
|
|
|
|
test
|
|
|
|
EAP;
|
|
|
|
|
|
|
|
<<<'EAP'
|
|
|
|
test
|
|
|
|
EAP;
|
|
|
|
|
|
|
|
<<<"EAP"
|
|
|
|
test
|
|
|
|
EAP;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test assign
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Assign(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 <<= $b ;
|
|
|
|
$a >>= $b ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test binary
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Binary(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 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 ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test cast
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Cast(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 := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test expr
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ArrayDimFetch(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
FOO [ ] ;
|
|
|
|
FOO [ 1 ] ;
|
|
|
|
$a [ ] ;
|
|
|
|
$a [ 1 ] ;
|
|
|
|
$a { 1 } ;
|
|
|
|
new $a [ ] ;
|
|
|
|
new $a [ 1 ] ;
|
|
|
|
new $a { 1 } ;
|
|
|
|
"$a[1]test" ;
|
|
|
|
"${ a [ 1 ] }test" ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ArrayItem(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$foo = [
|
|
|
|
$world ,
|
|
|
|
& $world ,
|
|
|
|
'Hello' => $world ,
|
|
|
|
] ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Array(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
array ( /* empty array */ ) ;
|
|
|
|
array ( 0 , 2 => 2 ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5BitwiseNot(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
~ $var ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5BooleanNot(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
! $var ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ClassConstFetch(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$var :: CONSTANT ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Clone(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
clone $var ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ClosureUse(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a = function ( ) use ( $a , & $b ) {
|
|
|
|
// do nothing
|
|
|
|
} ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Closure(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a = static function & ( ) {
|
|
|
|
// do nothing
|
|
|
|
} ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ConstFetch(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
null ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Empty(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
empty ( $a ) ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ErrorSuppress(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
@ foo ( ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Eval(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
eval ( " " ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Exit(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 := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5FunctionCall(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
foo ( ) ;
|
|
|
|
$var ( $a , ... $b , $c ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Include(t *testing.T) {
|
|
|
|
|
|
|
|
src := `<?php
|
|
|
|
include 'foo' ;
|
|
|
|
include_once 'bar' ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5InstanceOf(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a instanceof Foo ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Isset(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
isset ( $a , $b [ 2 ] ) ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5List(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
list( , $var , ) = $b ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5MethodCall(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a -> bar ( $arg ) ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5New(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
|
|
|
|
new Foo ;
|
|
|
|
|
|
|
|
new Foo ( $a, $b ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5IncDec(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
++ $a ;
|
|
|
|
-- $a ;
|
|
|
|
$a ++ ;
|
|
|
|
$a -- ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Print(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
print $a ;
|
|
|
|
print ( $a ) ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5PropertyFetch(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a -> b ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Reference(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a = & $b ;
|
|
|
|
$a = [ & $b ] ;
|
|
|
|
$a = [ $b => & $c ] ;
|
|
|
|
|
|
|
|
$a = function ( ) use ( & $b ) {
|
|
|
|
// do nothing
|
|
|
|
} ;
|
|
|
|
|
|
|
|
foreach ( $a as & $b ) {
|
|
|
|
// do nothing
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Require(t *testing.T) {
|
|
|
|
|
|
|
|
src := `<?php
|
|
|
|
require __DIR__ . '/folder' ;
|
|
|
|
require_once $a ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ShellExec(t *testing.T) {
|
|
|
|
src := "<?php ` {$v} cmd ` ; "
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ShortArray(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a = [ ] ;
|
|
|
|
$a = [ 0 ] ;
|
|
|
|
$a = [
|
|
|
|
1 => & $b , // one
|
|
|
|
$c , /* two */
|
|
|
|
] ;
|
|
|
|
$a = [0, 1, 2] ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5StaticCall(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
Foo :: bar ( $a , $b ) ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5StaticPropertyFetch(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
Foo :: $bar ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Ternary(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a ? $b : $c ;
|
|
|
|
$a ? : $c ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Unary(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
- $a ;
|
|
|
|
+ $a ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Variable(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$ /* variable variable comment */ $var ; `
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Yield(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
yield $a ;
|
|
|
|
yield $k => $v ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test stmt
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5AltIf(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
if ( 1 ) :
|
|
|
|
// do nothing
|
|
|
|
elseif ( 2 ) :
|
|
|
|
elseif ( 3 ) :
|
|
|
|
;
|
|
|
|
else :
|
|
|
|
;
|
|
|
|
endif ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5AltFor(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
for ( $a ; $b ; $c ) :
|
|
|
|
endfor ;
|
|
|
|
|
|
|
|
for ( ; ; ) :
|
|
|
|
endfor ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5AltForeach(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
foreach ( $a as $k => & $v ) :
|
|
|
|
echo $v ;
|
|
|
|
endforeach ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5AltSwitch(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
switch ( $a ) :
|
|
|
|
case 1 :
|
|
|
|
;
|
|
|
|
case 2 : ;
|
|
|
|
case 3 :
|
|
|
|
;
|
|
|
|
default :
|
|
|
|
;
|
|
|
|
endswitch ;
|
|
|
|
|
|
|
|
|
|
|
|
switch ( $a ) : ;
|
|
|
|
case 1 ; ;
|
|
|
|
default ; ;
|
|
|
|
endswitch ;
|
|
|
|
|
|
|
|
switch ( $a ) :
|
|
|
|
endswitch ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5AltWhile(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
while ( $a ) :
|
|
|
|
// do nothing
|
|
|
|
endwhile ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Break(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
break ;
|
|
|
|
break 1 ;
|
|
|
|
break ( 2 ) ;
|
|
|
|
`
|
|
|
|
|
2020-07-02 21:20:32 +00:00
|
|
|
root := parsePhp5(src)
|
|
|
|
actual := printPhp5(root)
|
2018-07-29 08:44:38 +00:00
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ClassMethod(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* abstract method
|
|
|
|
*/
|
|
|
|
public static function & greet ( $a ) ;
|
|
|
|
|
|
|
|
function greet ( $a )
|
|
|
|
{
|
|
|
|
return 'hello' ;
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Class(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
final class Foo extends Bar implements Baz , Quuz {
|
|
|
|
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ClassConstList(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
class Foo {
|
|
|
|
const FOO = 'f' , BAR = 'b' ;
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ConstList(t *testing.T) {
|
2020-08-24 20:28:44 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
const FOO = 1 , BAR = 2 ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Continue(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
continue ;
|
|
|
|
continue 1 ;
|
|
|
|
continue ( 2 ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Declare(t *testing.T) {
|
|
|
|
src := `<?php
|
2020-08-24 20:28:44 +00:00
|
|
|
declare ( FOO = 'bar' , BAR = 'foo' ) ;
|
2018-07-29 08:44:38 +00:00
|
|
|
declare ( FOO = 'bar' ) $a ;
|
|
|
|
declare ( FOO = 'bar' ) { }
|
|
|
|
|
|
|
|
declare ( FOO = 'bar' ) : enddeclare ;
|
|
|
|
declare ( FOO = 'bar' ) :
|
|
|
|
;
|
|
|
|
enddeclare ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5DoWhile(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
do {
|
|
|
|
;
|
|
|
|
} while ( $a ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Echo(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
echo '' ;
|
|
|
|
echo $a , ' ' , PHP_EOL;
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
<?= $a, $b ?>
|
|
|
|
<?= $c ; `
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5IfExpression(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a ; `
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5For(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
for ( $i = 0 ; $i < 3 ; $i ++ )
|
|
|
|
echo $i . PHP_EOL;
|
|
|
|
|
|
|
|
for ( ; ; ) {
|
|
|
|
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Foreach(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
foreach ( $a as $k => & $v ) {
|
|
|
|
;
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Function(t *testing.T) {
|
|
|
|
|
|
|
|
src := `<?php
|
|
|
|
function & foo ( ) {
|
|
|
|
;
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Global(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
global $a , $b ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Goto(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
goto Foo ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5HaltCompiler(t *testing.T) {
|
2020-08-24 18:41:06 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-09-13 17:07:50 +00:00
|
|
|
__halt_compiler ( ) ;
|
|
|
|
this text is ignored by parser
|
|
|
|
`
|
2018-07-29 08:44:38 +00:00
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5IfElseIfElse(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
if ( 1 ) ;
|
|
|
|
elseif ( 2 ) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
else if ( 3 ) $a;
|
|
|
|
else { }`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5InlineHtml(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
$a;?>test<? `
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:20:20 +00:00
|
|
|
func TestParseAndPrintPhp5Shebang(t *testing.T) {
|
|
|
|
src := `#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
$a;?>test<? `
|
|
|
|
|
2020-06-29 21:45:15 +00:00
|
|
|
actual := printPhp5(parsePhp5(src))
|
2019-12-29 22:20:20 +00:00
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func TestParseAndPrintPhp5Interface(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
interface Foo extends Bar , Baz {
|
|
|
|
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5GotoLabel(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
Foo : $b ; `
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Namespace(t *testing.T) {
|
2020-08-24 11:20:20 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
src := `<?php ;
|
2018-07-29 08:44:38 +00:00
|
|
|
namespace Foo \ Bar ;
|
|
|
|
namespace Baz {
|
|
|
|
|
|
|
|
}
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Nop(t *testing.T) {
|
|
|
|
src := `<?php ; `
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5PropertyList(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
class Foo {
|
|
|
|
var $a = '' , $b = null ;
|
|
|
|
private $c ;
|
|
|
|
public static $d ;
|
|
|
|
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Return(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
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
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5StaticVar(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
static $a , $b = ' ' ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5StmtList(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Switch(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
switch ( $a ) {
|
|
|
|
case 1 : ;
|
|
|
|
default : ;
|
|
|
|
}
|
|
|
|
switch ( $a ) { ;
|
|
|
|
case 1 ; ;
|
|
|
|
default ; ;
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Throw(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
throw new \ Exception ( "msg" ) ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5TraitUse(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
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 ;
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Trait(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
trait foo {
|
|
|
|
function bar ( ) { }
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5TryCatchFinally(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
} catch ( \ Exception $e) {
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
}`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Unset(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
unset ( $a ) ;
|
|
|
|
unset ( $a , $b ) ;`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5UseList(t *testing.T) {
|
2020-08-22 13:59:26 +00:00
|
|
|
// TODO: remove ; after <?php
|
|
|
|
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 ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5While(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
while ( $a ) echo '' ;
|
|
|
|
while ( $a ) { }
|
|
|
|
while ( $a ) ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// other
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5Parentheses(t *testing.T) {
|
|
|
|
src := `<?php
|
2018-10-24 19:32:35 +00:00
|
|
|
global $ { $b } ;
|
2018-07-29 08:44:38 +00:00
|
|
|
$b = (($a));
|
|
|
|
$b = ( ($a) );
|
|
|
|
$b = ( ( $a ) );
|
|
|
|
$b = ( ($a ));
|
|
|
|
$b = (( $a) );
|
|
|
|
|
|
|
|
( $a + $b ) * 2 ;
|
|
|
|
$ { $a . 'b' } -> call ( ) ;
|
|
|
|
$a -> { $b . 'b' } ;
|
|
|
|
$a -> $b ( ) -> { $c . 'c' } ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ComplexString1(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
// "test $foo" ;
|
|
|
|
"test $foo[1]" ;
|
2018-10-29 13:54:04 +00:00
|
|
|
"test $foo[112345678901234567890] " ;
|
2018-07-29 08:44:38 +00:00
|
|
|
"test $foo[a]" ;
|
|
|
|
"test $foo[$bar]" ;
|
|
|
|
"test $foo->bar" ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ComplexString2(t *testing.T) {
|
|
|
|
src := `<?php
|
|
|
|
"test ${ foo }" ;
|
|
|
|
"test ${ foo . 'bar' }" ;
|
|
|
|
"test ${ foo [ ] }" ;
|
|
|
|
"test ${ foo [ 1 ] }" ;
|
|
|
|
"test ${ foo [ 'expr' . $bar ] }" ;
|
|
|
|
"test ${ $foo }" ;
|
|
|
|
"test ${ $foo -> bar }" ;
|
|
|
|
"test ${ $foo -> bar ( ) }" ;
|
|
|
|
"test ${ $a . '' }" ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ComplexString3(t *testing.T) {
|
2018-10-29 13:39:15 +00:00
|
|
|
src := `<?php
|
|
|
|
"test ${foo }" ;
|
|
|
|
"test ${foo . 'bar' }" ;
|
|
|
|
"test ${foo [ ] }" ;
|
|
|
|
"test ${foo [ 1 ] }" ;
|
|
|
|
"test ${foo [ 'expr' . $bar ] }" ;
|
|
|
|
"test ${$foo }" ;
|
|
|
|
"test ${$foo -> bar }" ;
|
|
|
|
"test ${$foo -> bar ( ) }" ;
|
|
|
|
"test ${$a . '' }" ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAndPrintPhp5ComplexString4(t *testing.T) {
|
2018-07-29 08:44:38 +00:00
|
|
|
src := `<?php
|
|
|
|
"test {$foo }" ;
|
|
|
|
"test {$foo [ ] }" ;
|
|
|
|
"test {$foo [ 1 ] }" ;
|
|
|
|
"test {$foo -> bar }" ;
|
|
|
|
"test {$foo -> bar ( ) }" ;
|
|
|
|
`
|
|
|
|
|
|
|
|
actual := printPhp5(parsePhp5(src))
|
|
|
|
|
|
|
|
if src != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
|
|
|
|
}
|
|
|
|
}
|