php-parser/pkg/visitor/printer/printer_test.go

4738 lines
104 KiB
Go
Raw Normal View History

2020-12-28 19:13:08 +00:00
package printer_test
import (
"bytes"
"github.com/VKCOM/php-parser/pkg/token"
"github.com/VKCOM/php-parser/pkg/visitor/printer"
"testing"
"github.com/VKCOM/php-parser/pkg/ast"
)
func TestPrinterPrintFile(t *testing.T) {
2018-04-05 21:59:22 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o)
2020-12-11 07:54:26 +00:00
n := &ast.Root{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtNamespace{
2020-12-28 21:01:02 +00:00
Name: &ast.Name{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{Value: []byte("Foo")},
2018-04-05 22:12:54 +00:00
},
},
},
2020-05-17 19:56:32 +00:00
&ast.StmtClass{
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("abstract")}},
2020-12-28 21:01:02 +00:00
Name: &ast.Name{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{Value: []byte("Bar")},
2018-04-05 22:12:54 +00:00
},
},
2020-12-28 21:01:02 +00:00
Extends: &ast.Name{
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{Value: []byte("Baz")},
2018-04-05 22:12:54 +00:00
},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtClassMethod{
2020-12-28 21:01:02 +00:00
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Name: &ast.Identifier{Value: []byte("greet")},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtEcho{
Exprs: []ast.Vertex{
&ast.ScalarString{Value: []byte("'Hello world'")},
2018-06-03 06:35:44 +00:00
},
2018-04-05 22:12:54 +00:00
},
},
},
},
},
},
2018-04-05 21:59:22 +00:00
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-04-05 21:59:22 +00:00
2020-12-11 07:54:26 +00:00
expected := `<?php namespace Foo;abstract class Bar extends Baz{public function greet(){echo'Hello world';}}`
2018-04-05 21:59:22 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintFileInlineHtml(t *testing.T) {
2018-04-05 21:59:22 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.Root{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtInlineHtml{Value: []byte("<div>HTML</div>")},
&ast.StmtEcho{
Exprs: []ast.Vertex{
&ast.ScalarString{
Value: []byte(`"a"`),
},
},
},
2020-05-17 19:56:32 +00:00
&ast.StmtInlineHtml{Value: []byte("<div>HTML</div>")},
&ast.StmtEcho{
Exprs: []ast.Vertex{
&ast.ScalarString{
Value: []byte(`"b"`),
2018-04-05 21:59:22 +00:00
},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-04-05 21:59:22 +00:00
2020-12-11 07:54:26 +00:00
expected := `<div>HTML</div><?php echo"a";?><div>HTML</div><?php echo"b";`
2018-04-05 21:59:22 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
// node
func TestPrinterPrintIdentifier(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-05-17 19:56:32 +00:00
n := &ast.Identifier{
Value: []byte("test"),
}
2020-12-11 07:54:26 +00:00
n.Accept(p)
expected := `test`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintParameter(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.Parameter{
2020-05-17 19:56:32 +00:00
Type: &ast.NameFullyQualified{
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Foo"),
},
},
},
2020-12-11 07:54:26 +00:00
VariadicTkn: &token.Token{
Value: []byte("..."),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-05-17 19:56:32 +00:00
DefaultValue: &ast.ScalarString{
Value: []byte("'default'"),
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "\\Foo...$var='default'"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintNullable(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.Nullable{
2020-05-17 19:56:32 +00:00
Expr: &ast.Parameter{
Type: &ast.NameFullyQualified{
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Foo"),
},
},
},
2020-12-11 07:54:26 +00:00
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-05-17 19:56:32 +00:00
DefaultValue: &ast.ScalarString{
Value: []byte("'default'"),
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "?\\Foo&$var='default'"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintArgument(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.Argument{
VariadicTkn: &token.Token{
Value: []byte("..."),
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "...$var"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintArgumentByRef(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.Argument{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "&$var"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
// name
func TestPrinterPrintNameNamePart(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-28 21:01:02 +00:00
n := &ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("foo"),
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "foo"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintNameName(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-28 21:01:02 +00:00
n := &ast.Name{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Foo"),
},
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Bar"),
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "Foo\\Bar"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintNameFullyQualified(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.NameFullyQualified{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Foo"),
},
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Bar"),
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "\\Foo\\Bar"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintNameRelative(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.NameRelative{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Foo"),
},
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Bar"),
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "namespace\\Foo\\Bar"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
// scalar
func TestPrinterPrintScalarLNumber(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ScalarLnumber{
2020-05-17 19:56:32 +00:00
Value: []byte("1"),
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "1"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintScalarDNumber(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ScalarDnumber{
2020-05-17 19:56:32 +00:00
Value: []byte(".1"),
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := ".1"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintScalarString(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ScalarString{
2020-05-17 19:56:32 +00:00
Value: []byte("'hello world'"),
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `'hello world'`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintScalarEncapsedStringPart(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ScalarEncapsedStringPart{
2020-05-17 19:56:32 +00:00
Value: []byte("hello world"),
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `hello world`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintScalarEncapsed(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ScalarEncapsed{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
&ast.ScalarEncapsedStringPart{Value: []byte("hello ")},
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-05-17 19:56:32 +00:00
&ast.ScalarEncapsedStringPart{Value: []byte(" world")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `"hello $var world"`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintScalarHeredoc(t *testing.T) {
2018-04-05 21:39:04 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ScalarHeredoc{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
&ast.ScalarEncapsedStringPart{Value: []byte("hello ")},
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-05-17 19:56:32 +00:00
&ast.ScalarEncapsedStringPart{Value: []byte(" world\n")},
2018-04-05 21:39:04 +00:00
},
}
2020-12-11 07:54:26 +00:00
n.Accept(p)
2018-04-05 21:39:04 +00:00
2020-12-11 07:54:26 +00:00
expected := `<<<EOT
hello $var world
EOT`
2018-04-05 21:39:04 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintScalarMagicConstant(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ScalarMagicConstant{
2020-05-17 19:56:32 +00:00
Value: []byte("__DIR__"),
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
if o.String() != `__DIR__` {
t.Errorf("TestPrintScalarMagicConstant is failed\n")
}
}
// assign
func TestPrinterPrintAssign(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssign{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintReference(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignReference{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a=&$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignBitwiseAnd(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignBitwiseAnd{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a&=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignBitwiseOr(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignBitwiseOr{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a|=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignBitwiseXor(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignBitwiseXor{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a^=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignCoalesce(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignCoalesce{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a??=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignConcat(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignConcat{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a.=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignDiv(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignDiv{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a/=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignMinus(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignMinus{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a-=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignMod(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignMod{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a%=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignMul(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignMul{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a*=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignPlus(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignPlus{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a+=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignPow(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignPow{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a**=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignShiftLeft(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignShiftLeft{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a<<=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignShiftRight(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprAssignShiftRight{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a>>=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
// binary
func TestPrinterPrintBinaryBitwiseAnd(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryBitwiseAnd{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a&$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryBitwiseOr(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryBitwiseOr{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a|$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryBitwiseXor(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryBitwiseXor{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a^$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryBooleanAnd(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryBooleanAnd{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a&&$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryBooleanOr(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryBooleanOr{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a||$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryCoalesce(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryCoalesce{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a??$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryConcat(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryConcat{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a.$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryDiv(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryDiv{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a/$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryEqual(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryEqual{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a==$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryGreaterOrEqual(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryGreaterOrEqual{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a>=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryGreater(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryGreater{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a>$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryIdentical(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryIdentical{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a===$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryLogicalAnd(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryLogicalAnd{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `$a and$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryLogicalOr(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryLogicalOr{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `$a or$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryLogicalXor(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryLogicalXor{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `$a xor$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryMinus(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryMinus{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a-$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryMod(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryMod{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a%$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryMul(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryMul{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a*$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryNotEqual(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryNotEqual{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a!=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryNotIdentical(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryNotIdentical{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a!==$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryPlus(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryPlus{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a+$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryPow(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryPow{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a**$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryShiftLeft(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryShiftLeft{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a<<$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinaryShiftRight(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinaryShiftRight{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a>>$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinarySmallerOrEqual(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinarySmallerOrEqual{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a<=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinarySmaller(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinarySmaller{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a<$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBinarySpaceship(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBinarySpaceship{
2020-05-17 19:56:32 +00:00
Left: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Right: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a<=>$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
// cast
func TestPrinterPrintArray(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprCastArray{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `(array)$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintBool(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprCastBool{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `(bool)$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintDouble(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprCastDouble{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `(float)$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintInt(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprCastInt{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-26 20:48:02 +00:00
expected := `(int)$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintObject(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprCastObject{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `(object)$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintString(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprCastString{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `(string)$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintUnset(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprCastUnset{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `(unset)$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
// expr
func TestPrinterPrintExprArrayDimFetch(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprArrayDimFetch{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-05-17 19:56:32 +00:00
Dim: &ast.ScalarLnumber{Value: []byte("1")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$var[1]`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprArrayItemWithKey(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprArrayItem{
2020-05-17 19:56:32 +00:00
Key: &ast.ScalarString{Value: []byte("'Hello'")},
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$world")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `'Hello'=>$world`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprArrayItem(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprArrayItem{
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$world")},
},
}
n.Accept(p)
expected := `$world`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprArrayItem_Reference(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
n := &ast.ExprArrayItem{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$world")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `&$world`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
2019-12-29 20:42:52 +00:00
func TestPrinterPrintExprArrayItemUnpack(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprArrayItem{
EllipsisTkn: &token.Token{
Value: []byte("..."),
},
2020-05-17 19:56:32 +00:00
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$world")},
2019-12-29 20:42:52 +00:00
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2019-12-29 20:42:52 +00:00
expected := `...$world`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprArray(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprArray{
ArrayTkn: &token.Token{
Value: []byte("array"),
},
2020-05-17 19:56:32 +00:00
Items: []ast.Vertex{
&ast.ExprArrayItem{
Key: &ast.ScalarString{Value: []byte("'Hello'")},
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$world")},
},
},
2020-05-17 19:56:32 +00:00
&ast.ExprArrayItem{
Key: &ast.ScalarLnumber{Value: []byte("2")},
2020-12-26 20:38:35 +00:00
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
2020-12-26 20:38:35 +00:00
},
},
2020-05-17 19:56:32 +00:00
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `array('Hello'=>$world,2=>&$var,$var)`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprBitwiseNot(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBitwiseNot{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `~$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprBooleanNot(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprBooleanNot{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `!$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprBracket(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
n := &ast.ExprBooleanNot{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
}
n.Accept(p)
expected := `!$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprClassConstFetch(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprClassConstFetch{
2020-05-17 19:56:32 +00:00
Class: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-28 21:01:02 +00:00
Const: &ast.Identifier{
2020-05-17 19:56:32 +00:00
Value: []byte("CONST"),
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$var::CONST`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprClone(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprClone{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `clone$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprClosureUse(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprClosureUse{
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$foo")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$foo`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprClosureUse_Reference(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
n := &ast.ExprClosureUse{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$foo")},
},
}
n.Accept(p)
expected := `&$foo`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprClosure(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprClosure{
StaticTkn: &token.Token{
Value: []byte("static"),
},
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
2020-05-17 19:56:32 +00:00
Params: []ast.Vertex{
&ast.Parameter{
2020-12-11 07:54:26 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
},
},
2020-12-28 21:01:02 +00:00
Uses: []ast.Vertex{
&ast.ExprClosureUse{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
},
&ast.ExprClosureUse{
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
},
2020-05-17 19:56:32 +00:00
ReturnType: &ast.NameFullyQualified{
2020-12-28 21:01:02 +00:00
Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `static function&($var)use(&$a,$b):\Foo{$a;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprArrowFunction(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtExpression{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprArrowFunction{
2020-12-11 07:54:26 +00:00
StaticTkn: &token.Token{
Value: []byte("static"),
},
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
2020-05-17 19:56:32 +00:00
Params: []ast.Vertex{
&ast.Parameter{
2020-12-11 07:54:26 +00:00
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
},
},
2020-05-17 19:56:32 +00:00
ReturnType: &ast.NameFullyQualified{
2020-12-28 21:01:02 +00:00
Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `static fn&(&$var):\Foo=>$a;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprConstFetch(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprConstFetch{
2020-12-28 21:01:02 +00:00
Const: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("null")}}},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "null"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintEmpty(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprEmpty{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `empty($var)`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrettyPrinterrorSuppress(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprErrorSuppress{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `@$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintEval(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprEval{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `eval($var)`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExit(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprExit{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `exit$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintDie(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprExit{
2020-12-26 20:41:25 +00:00
ExitTkn: &token.Token{
2020-12-11 07:54:26 +00:00
Value: []byte("die"),
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `die$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintFunctionCall(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprFunctionCall{
2020-05-17 19:56:32 +00:00
Function: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-28 21:01:02 +00:00
Args: []ast.Vertex{
2020-12-11 07:54:26 +00:00
&ast.Argument{
AmpersandTkn: &token.Token{
Value: []byte("&"),
2018-04-29 16:58:49 +00:00
},
2020-12-11 07:54:26 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
2018-04-29 16:58:49 +00:00
},
2020-12-11 07:54:26 +00:00
},
&ast.Argument{
VariadicTkn: &token.Token{
Value: []byte("..."),
},
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
2020-12-11 07:54:26 +00:00
},
},
&ast.Argument{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$c")},
2018-04-29 16:58:49 +00:00
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$var(&$a,...$b,$c)`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintInclude(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprInclude{
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarString{Value: []byte("'path'")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `include'path'`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintIncludeOnce(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprIncludeOnce{
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarString{Value: []byte("'path'")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `include_once'path'`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintInstanceOf(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprInstanceOf{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-28 21:01:02 +00:00
Class: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$var instanceof Foo`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintIsset(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprIsset{
2020-05-17 19:56:32 +00:00
Vars: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `isset($a,$b)`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintList(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprList{
2020-05-17 19:56:32 +00:00
Items: []ast.Vertex{
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
},
2020-05-17 19:56:32 +00:00
&ast.ExprArrayItem{
Val: &ast.ExprList{
Items: []ast.Vertex{
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
2020-05-17 19:56:32 +00:00
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$c")},
},
},
},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `list($a,list($b,$c))`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintMethodCall(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprMethodCall{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$foo")},
},
2020-05-17 19:56:32 +00:00
Method: &ast.Identifier{Value: []byte("bar")},
2020-12-28 21:01:02 +00:00
Args: []ast.Vertex{
2020-12-11 07:54:26 +00:00
&ast.Argument{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
2018-04-29 16:58:49 +00:00
},
2020-12-11 07:54:26 +00:00
},
&ast.Argument{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
2018-04-29 16:58:49 +00:00
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$foo->bar($a,$b)`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintNew(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprNew{
2020-12-28 21:01:02 +00:00
Class: &ast.Name{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Foo"),
},
},
},
2020-12-28 21:01:02 +00:00
Args: []ast.Vertex{
2020-12-11 07:54:26 +00:00
&ast.Argument{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
2018-04-29 16:58:49 +00:00
},
2020-12-11 07:54:26 +00:00
},
&ast.Argument{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
2018-04-29 16:58:49 +00:00
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `new Foo($a,$b)`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintPostDec(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprPostDec{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$var--`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintPostInc(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprPostInc{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$var++`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintPreDec(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprPreDec{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `--$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintPreInc(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprPreInc{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `++$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintPrint(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprPrint{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `print$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintPropertyFetch(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprPropertyFetch{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$foo")},
},
2020-12-28 21:01:02 +00:00
Prop: &ast.Identifier{Value: []byte("bar")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$foo->bar`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintRequire(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprRequire{
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarString{Value: []byte("'path'")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `require'path'`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintRequireOnce(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprRequireOnce{
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarString{Value: []byte("'path'")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `require_once'path'`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintShellExec(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprShellExec{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
&ast.ScalarEncapsedStringPart{Value: []byte("hello ")},
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$world")},
},
2020-05-17 19:56:32 +00:00
&ast.ScalarEncapsedStringPart{Value: []byte("!")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "`hello $world!`"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprShortArray(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprArray{
2020-05-17 19:56:32 +00:00
Items: []ast.Vertex{
&ast.ExprArrayItem{
Key: &ast.ScalarString{Value: []byte("'Hello'")},
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$world")},
},
},
2020-05-17 19:56:32 +00:00
&ast.ExprArrayItem{
Key: &ast.ScalarLnumber{Value: []byte("2")},
2020-12-26 20:38:35 +00:00
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
2020-12-26 20:38:35 +00:00
},
},
2020-05-17 19:56:32 +00:00
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `['Hello'=>$world,2=>&$var,$var]`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintShortList(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprList{
OpenBracketTkn: &token.Token{
Value: []byte("["),
},
2020-05-17 19:56:32 +00:00
Items: []ast.Vertex{
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
},
2020-05-17 19:56:32 +00:00
&ast.ExprArrayItem{
Val: &ast.ExprList{
Items: []ast.Vertex{
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
2020-05-17 19:56:32 +00:00
&ast.ExprArrayItem{
Val: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$c")},
},
},
},
},
},
},
2020-12-11 07:54:26 +00:00
CloseBracketTkn: &token.Token{
Value: []byte("]"),
},
}
n.Accept(p)
expected := `[$a,list($b,$c)]`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStaticCall(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprStaticCall{
2020-05-17 19:56:32 +00:00
Class: &ast.Identifier{Value: []byte("Foo")},
Call: &ast.Identifier{Value: []byte("bar")},
2020-12-28 21:01:02 +00:00
Args: []ast.Vertex{
2020-12-11 07:54:26 +00:00
&ast.Argument{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
2018-04-29 16:58:49 +00:00
},
2020-12-11 07:54:26 +00:00
},
&ast.Argument{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
2018-04-29 16:58:49 +00:00
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `Foo::bar($a,$b)`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStaticPropertyFetch(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprStaticPropertyFetch{
2020-05-17 19:56:32 +00:00
Class: &ast.Identifier{Value: []byte("Foo")},
2020-12-28 21:01:02 +00:00
Prop: &ast.ExprVariable{
Name: &ast.Identifier{Value: []byte("$bar")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `Foo::$bar`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintTernary(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprTernary{
2020-12-28 21:01:02 +00:00
Cond: &ast.ExprVariable{
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
IfFalse: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a?:$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintTernaryFull(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprTernary{
2020-12-28 21:01:02 +00:00
Cond: &ast.ExprVariable{
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
IfTrue: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-05-17 19:56:32 +00:00
IfFalse: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$c")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a?$b:$c`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintUnaryMinus(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprUnaryMinus{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `-$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintUnaryPlus(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprUnaryPlus{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `+$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintVariable(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprVariable{
DollarTkn: &token.Token{
Value: []byte("$"),
},
OpenCurlyBracketTkn: &token.Token{
Value: []byte("{"),
},
2020-12-28 21:01:02 +00:00
Name: &ast.ExprVariable{
Name: &ast.Identifier{Value: []byte("$var")},
},
CloseCurlyBracketTkn: &token.Token{
Value: []byte("}"),
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `${$var}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintYieldFrom(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprYieldFrom{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `yield from$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintYield(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprYield{
2020-12-28 21:01:02 +00:00
Val: &ast.ExprVariable{
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `yield$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintYieldFull(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.ExprYield{
2020-05-17 19:56:32 +00:00
Key: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$k")},
},
2020-12-28 21:01:02 +00:00
Val: &ast.ExprVariable{
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `yield$k=>$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
// stmt
func TestPrinterPrintAltElseIf(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElseIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `elseif($a):$b;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltElseIfEmpty(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElseIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `elseif($a):`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltElse(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElse{
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `else:$b;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltElseEmpty(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElse{
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `else:`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltFor(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtFor{
2020-05-17 19:56:32 +00:00
Init: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
},
2020-05-17 19:56:32 +00:00
Cond: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
2020-05-17 19:56:32 +00:00
Loop: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$c")},
},
},
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$d")},
}},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `for($a;$b;$c):$d;endfor;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltForeach(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtForeach{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-05-17 19:56:32 +00:00
Key: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$key")},
},
2020-12-26 20:38:35 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$val")},
2020-12-26 20:38:35 +00:00
},
ColonTkn: &token.Token{
Value: []byte(":"),
},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$d")},
2020-12-26 20:38:35 +00:00
}},
2020-12-11 07:54:26 +00:00
},
},
2020-12-26 20:38:35 +00:00
}
n.Accept(p)
expected := `foreach($var as$key=>$val):$d;endforeach;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltForeach_Reference(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-26 20:38:35 +00:00
n := &ast.StmtForeach{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
2020-12-26 20:38:35 +00:00
},
Key: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$key")},
2020-12-26 20:38:35 +00:00
},
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$val")},
2020-12-26 20:38:35 +00:00
},
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$d")},
}},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `foreach($var as$key=>&$val):$d;endforeach;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltIf(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$d")},
}},
},
},
2020-05-17 19:56:32 +00:00
ElseIf: []ast.Vertex{
&ast.StmtElseIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
2018-04-03 16:20:55 +00:00
},
},
},
&ast.StmtElseIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$c")},
},
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{},
},
},
Else: &ast.StmtElse{
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `if($a):$d;elseif($b):$b;elseif($c):else:$b;endif;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtAltSwitch(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtSwitch{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-12-28 21:01:02 +00:00
Cases: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `switch($var):case'a':$a;case'b':$b;endswitch;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltWhile(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtWhile{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-12-11 07:54:26 +00:00
ColonTkn: &token.Token{
Value: []byte(":"),
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `while($a):$b;endwhile;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtBreak(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtBreak{
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarLnumber{
Value: []byte("1"),
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "break 1;"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtCase(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtCase{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `case$a:$a;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtCaseEmpty(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtCase{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := "case$a:"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtCatch(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtCatch{
2020-05-17 19:56:32 +00:00
Types: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Exception")}}},
&ast.NameFullyQualified{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("RuntimeException")}}},
},
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$e")},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `catch(Exception|\RuntimeException$e){$a;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtClassMethod(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtClassMethod{
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("foo")},
2020-05-17 19:56:32 +00:00
Params: []ast.Vertex{
&ast.Parameter{
2020-12-28 21:01:02 +00:00
Type: &ast.Nullable{Expr: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("int")}}}},
2020-12-11 07:54:26 +00:00
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-12-28 21:01:02 +00:00
DefaultValue: &ast.ExprConstFetch{Const: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("null")}}}},
},
2020-05-17 19:56:32 +00:00
&ast.Parameter{
2020-12-11 07:54:26 +00:00
VariadicTkn: &token.Token{
Value: []byte("..."),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
},
2020-12-28 21:01:02 +00:00
ReturnType: &ast.Name{
Parts: []ast.Vertex{&ast.NamePart{Value: []byte("void")}},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
2018-06-03 06:35:44 +00:00
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `public function&foo(?int&$a=null,...$b):void{$a;}`
actual := o.String()
if expected != actual {
2018-06-30 21:05:59 +00:00
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtAbstractClassMethod(t *testing.T) {
2018-06-30 21:05:59 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtClassMethod{
2020-05-17 19:56:32 +00:00
Modifiers: []ast.Vertex{
&ast.Identifier{Value: []byte("public")},
&ast.Identifier{Value: []byte("static")},
},
2020-12-11 07:54:26 +00:00
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("foo")},
2020-05-17 19:56:32 +00:00
Params: []ast.Vertex{
&ast.Parameter{
2020-12-28 21:01:02 +00:00
Type: &ast.Nullable{Expr: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("int")}}}},
2020-12-11 07:54:26 +00:00
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-12-28 21:01:02 +00:00
DefaultValue: &ast.ExprConstFetch{Const: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("null")}}}},
2018-06-30 21:05:59 +00:00
},
2020-05-17 19:56:32 +00:00
&ast.Parameter{
2020-12-11 07:54:26 +00:00
VariadicTkn: &token.Token{
Value: []byte("..."),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2018-06-30 21:05:59 +00:00
},
},
2020-12-28 21:01:02 +00:00
ReturnType: &ast.Name{
Parts: []ast.Vertex{&ast.NamePart{Value: []byte("void")}},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtNop{},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-06-30 21:05:59 +00:00
2020-12-11 07:54:26 +00:00
expected := `public static function&foo(?int&$a=null,...$b):void;`
2018-06-30 21:05:59 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtClass(t *testing.T) {
2018-03-20 18:06:56 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtClass{
2020-05-17 19:56:32 +00:00
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("abstract")}},
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("Foo")},
Extends: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Bar")}}},
Implements: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Baz")}}},
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Quuz")}}},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtClassConstList{
Modifiers: []ast.Vertex{
&ast.Identifier{Value: []byte("public")},
&ast.Identifier{Value: []byte("static")},
},
2020-05-17 19:56:32 +00:00
Consts: []ast.Vertex{
&ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
2018-03-20 18:06:56 +00:00
},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-20 18:06:56 +00:00
expected := `abstract class Foo extends Bar implements Baz,Quuz{public static const FOO='bar';}`
2018-03-20 18:06:56 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtAnonymousClass(t *testing.T) {
2018-03-20 18:06:56 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtClass{
2020-05-17 19:56:32 +00:00
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("abstract")}},
2020-12-28 21:01:02 +00:00
Args: []ast.Vertex{
2020-12-11 07:54:26 +00:00
&ast.Argument{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
2018-04-03 16:20:55 +00:00
},
2020-12-11 07:54:26 +00:00
},
&ast.Argument{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
},
2020-12-28 21:01:02 +00:00
Extends: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Bar")}}},
Implements: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Baz")}}},
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Quuz")}}},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtClassConstList{
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
2018-03-20 18:06:56 +00:00
},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-20 18:06:56 +00:00
2020-12-11 07:54:26 +00:00
expected := `abstract class($a,$b)extends Bar implements Baz,Quuz{public const FOO='bar';}`
2018-03-20 18:06:56 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtClassConstList(t *testing.T) {
2018-03-20 18:37:55 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtClassConstList{
2020-05-17 19:56:32 +00:00
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
2018-03-20 18:37:55 +00:00
},
2020-05-17 19:56:32 +00:00
&ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
2018-03-20 18:37:55 +00:00
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-20 18:37:55 +00:00
expected := `public const FOO='a',BAR='b';`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtConstList(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtConstList{
2020-05-17 19:56:32 +00:00
Consts: []ast.Vertex{
&ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
},
2020-05-17 19:56:32 +00:00
&ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `const FOO='a',BAR='b';`
2018-03-20 18:37:55 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtConstant(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := "FOO='BAR'"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtContinue(t *testing.T) {
2018-03-20 18:37:55 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtContinue{
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarLnumber{
Value: []byte("1"),
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-20 18:37:55 +00:00
expected := `continue 1;`
2018-03-20 18:37:55 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtDeclareStmts(t *testing.T) {
2018-03-28 20:44:02 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtDeclare{
2020-05-17 19:56:32 +00:00
Consts: []ast.Vertex{
&ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNop{},
2018-03-28 20:44:02 +00:00
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-28 20:44:02 +00:00
expected := `declare(FOO='bar'){;}`
2018-03-28 20:44:02 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtDeclareExpr(t *testing.T) {
2018-03-28 20:44:02 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtDeclare{
2020-05-17 19:56:32 +00:00
Consts: []ast.Vertex{
&ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-28 20:44:02 +00:00
expected := `declare(FOO='bar')'bar';`
2018-03-28 20:44:02 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtDeclareNop(t *testing.T) {
2018-03-28 20:44:02 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtDeclare{
2020-05-17 19:56:32 +00:00
Consts: []ast.Vertex{
&ast.StmtConstant{
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
2018-03-28 20:44:02 +00:00
},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtNop{},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-28 20:44:02 +00:00
expected := `declare(FOO='bar');`
2018-03-28 20:44:02 +00:00
actual := o.String()
2018-03-28 21:04:09 +00:00
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtDefalut(t *testing.T) {
2018-03-28 21:04:09 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtDefault{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
2018-03-28 21:04:09 +00:00
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-28 21:04:09 +00:00
expected := `default:$a;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtDefalutEmpty(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtDefault{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `default:`
2018-03-28 21:04:09 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtDo_Expression(t *testing.T) {
2018-03-28 21:04:09 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtDo{
2020-05-17 19:56:32 +00:00
Cond: &ast.ScalarLnumber{Value: []byte("1")},
Stmt: &ast.StmtExpression{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-28 21:04:09 +00:00
2020-12-11 07:54:26 +00:00
expected := `do$a;while(1);`
2018-03-28 21:04:09 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtDo_StmtList(t *testing.T) {
2018-03-28 21:04:09 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtDo{
2020-05-17 19:56:32 +00:00
Cond: &ast.ScalarLnumber{Value: []byte("1")},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
2018-03-28 21:04:09 +00:00
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-03-28 21:04:09 +00:00
2020-12-11 07:54:26 +00:00
expected := `do{$a;}while(1);`
2018-03-28 21:04:09 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtEchoHtmlState(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o)
2020-12-11 07:54:26 +00:00
n := &ast.Root{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtEcho{
Exprs: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `<?php echo$a,$b;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtEchoPhpState(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtEcho{
2020-05-17 19:56:32 +00:00
Exprs: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `echo$a,$b;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtElseIfStmts(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElseIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `elseif($a){;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtElseIfExpr(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElseIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `elseif($a)'bar';`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtElseIfNop(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElseIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtNop{},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `elseif($a);`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtElseStmts(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElse{
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `else{;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtElseExpr(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElse{
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `else'bar';`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtElseNop(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtElse{
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtNop{},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `else;`
actual := o.String()
2018-03-28 20:44:02 +00:00
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExpression(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtExpression{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtFinally(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtFinally{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `finally{;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtFor(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtFor{
2020-05-17 19:56:32 +00:00
Init: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
2020-05-17 19:56:32 +00:00
Cond: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$c")},
},
2020-05-17 19:56:32 +00:00
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$d")},
},
},
2020-05-17 19:56:32 +00:00
Loop: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$e")},
},
2020-05-17 19:56:32 +00:00
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$f")},
},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `for($a,$b;$c,$d;$e,$f){;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtForeach(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtForeach{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Key: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$k")},
},
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$v")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `foreach($a as$k=>$v){;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtForeach_Reference(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
n := &ast.StmtForeach{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
Key: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$k")},
},
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$v")},
},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
},
}
n.Accept(p)
expected := `foreach($a as$k=>&$v){;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtFunction(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtFunction{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("foo")},
2020-05-17 19:56:32 +00:00
Params: []ast.Vertex{
&ast.Parameter{
2020-12-11 07:54:26 +00:00
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
},
},
2020-05-17 19:56:32 +00:00
ReturnType: &ast.NameFullyQualified{
2020-12-28 21:01:02 +00:00
Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `function&foo(&$var):\Foo{;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtGlobal(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtGlobal{
2020-05-17 19:56:32 +00:00
Vars: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `global$a,$b;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtGoto(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtGoto{
2020-05-17 19:56:32 +00:00
Label: &ast.Identifier{Value: []byte("FOO")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `goto FOO;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintHaltCompiler(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtHaltCompiler{}
n.Accept(p)
expected := `__halt_compiler();`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintIfExpression(t *testing.T) {
2018-04-01 14:07:07 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtExpression{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
2020-05-17 19:56:32 +00:00
ElseIf: []ast.Vertex{
&ast.StmtElseIf{
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$c")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$d")},
},
2018-04-01 14:07:07 +00:00
},
},
},
},
2020-05-17 19:56:32 +00:00
&ast.StmtElseIf{
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$e")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtNop{},
},
},
2020-05-17 19:56:32 +00:00
Else: &ast.StmtElse{
Stmt: &ast.StmtExpression{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$f")},
},
},
2018-04-01 14:07:07 +00:00
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-04-01 14:07:07 +00:00
2020-12-11 07:54:26 +00:00
expected := `if($a)$b;elseif($c){$d;}elseif($e);else$f;`
2018-04-01 14:07:07 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintIfStmtList(t *testing.T) {
2018-04-01 14:07:07 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
2018-04-01 14:07:07 +00:00
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-04-01 14:07:07 +00:00
expected := `if($a){$b;}`
2018-04-01 14:07:07 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintIfNop(t *testing.T) {
2018-04-01 14:07:07 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtIf{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtNop{},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-04-01 14:07:07 +00:00
expected := `if($a);`
2018-04-01 14:07:07 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintInlineHtml(t *testing.T) {
2018-04-01 14:07:07 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.Root{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtInlineHtml{
Value: []byte("test"),
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-04-01 14:07:07 +00:00
expected := `test`
2018-04-01 14:07:07 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintInterface(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtInterface{
2020-12-28 21:01:02 +00:00
Name: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
Extends: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Bar")}}},
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Baz")}}},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtClassMethod{
2020-12-28 21:01:02 +00:00
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Name: &ast.Identifier{Value: []byte("foo")},
Params: []ast.Vertex{},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
2018-04-03 16:20:55 +00:00
},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `interface Foo extends Bar,Baz{public function foo(){$a;}}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintLabel(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtLabel{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("FOO")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `FOO:`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintNamespace(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtNamespace{
2020-12-28 21:01:02 +00:00
Name: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `namespace Foo;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintNamespaceWithStmts(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtNamespace{
2020-12-28 21:01:02 +00:00
Name: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `namespace Foo{$a;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintNop(t *testing.T) {
2018-03-28 20:44:02 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtNop{}
n.Accept(p)
2018-03-28 20:44:02 +00:00
expected := `;`
2018-03-28 20:44:02 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintPropertyList(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtPropertyList{
2020-05-17 19:56:32 +00:00
Modifiers: []ast.Vertex{
&ast.Identifier{Value: []byte("public")},
&ast.Identifier{Value: []byte("static")},
},
2020-12-28 21:01:02 +00:00
Type: &ast.Name{
2020-05-17 19:56:32 +00:00
Parts: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.NamePart{
2020-05-17 19:56:32 +00:00
Value: []byte("Foo"),
2019-12-29 14:36:56 +00:00
},
},
},
2020-12-28 21:01:02 +00:00
Props: []ast.Vertex{
2020-05-17 19:56:32 +00:00
&ast.StmtProperty{
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarString{Value: []byte("'a'")},
},
2020-05-17 19:56:32 +00:00
&ast.StmtProperty{
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `public static Foo$a='a',$b;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintProperty(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtProperty{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarLnumber{Value: []byte("1")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a=1`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintReturn(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtReturn{
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarLnumber{Value: []byte("1")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `return 1;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStaticVar(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtStaticVar{
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Expr: &ast.ScalarLnumber{Value: []byte("1")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `$a=1`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStatic(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtStatic{
2020-05-17 19:56:32 +00:00
Vars: []ast.Vertex{
&ast.StmtStaticVar{
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
},
2020-05-17 19:56:32 +00:00
&ast.StmtStaticVar{
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `static$a,$b;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtList(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtStmtList{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
2020-05-17 19:56:32 +00:00
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `{$a;$b;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtListNested(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtStmtList{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
2020-05-17 19:56:32 +00:00
&ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
2020-05-17 19:56:32 +00:00
&ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$c")},
}},
2018-04-03 16:20:55 +00:00
},
},
},
},
2018-04-03 16:20:55 +00:00
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2018-04-03 16:20:55 +00:00
expected := `{$a;{$b;{$c;}}}`
2018-04-03 16:20:55 +00:00
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtSwitch(t *testing.T) {
2018-04-03 16:20:55 +00:00
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtSwitch{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-28 21:01:02 +00:00
Cases: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
},
},
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'b'")},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `switch($var){case'a':$a;case'b':$b;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtThrow(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtThrow{
2020-05-17 19:56:32 +00:00
Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$var")},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
2020-12-11 07:54:26 +00:00
expected := `throw$var;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtTraitUseAlias(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtTraitUseAlias{
2020-12-28 21:01:02 +00:00
Trait: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
2020-05-17 19:56:32 +00:00
Modifier: &ast.Identifier{Value: []byte("public")},
Alias: &ast.Identifier{Value: []byte("b")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `Foo::a as public b;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtTraitUsePrecedence(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtTraitUsePrecedence{
2020-12-28 21:01:02 +00:00
Trait: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
2020-05-17 19:56:32 +00:00
Insteadof: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Bar")}}},
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Baz")}}},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `Foo::a insteadof Bar,Baz;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtTraitUse(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtTraitUse{
2020-05-17 19:56:32 +00:00
Traits: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Bar")}}},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `use Foo,Bar;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtTraitAdaptations(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtTraitUse{
2020-05-17 19:56:32 +00:00
Traits: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Bar")}}},
},
Adaptations: []ast.Vertex{
&ast.StmtTraitUseAlias{
2020-12-28 21:01:02 +00:00
Trait: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
Method: &ast.Identifier{Value: []byte("a")},
Alias: &ast.Identifier{Value: []byte("b")},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `use Foo,Bar{Foo::a as b;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintTrait(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtTrait{
2020-12-28 21:01:02 +00:00
Name: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtClassMethod{
2020-12-28 21:01:02 +00:00
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Name: &ast.Identifier{Value: []byte("foo")},
Params: []ast.Vertex{},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
2018-04-03 16:20:55 +00:00
},
},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `trait Foo{public function foo(){$a;}}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtTry(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtTry{
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
},
2020-05-17 19:56:32 +00:00
Catches: []ast.Vertex{
&ast.StmtCatch{
Types: []ast.Vertex{
2020-12-28 21:01:02 +00:00
&ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Exception")}}},
&ast.NameFullyQualified{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("RuntimeException")}}},
2018-04-03 16:20:55 +00:00
},
2020-05-17 19:56:32 +00:00
Var: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$e")},
},
2020-05-17 19:56:32 +00:00
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
}},
},
},
},
2020-05-17 19:56:32 +00:00
Finally: &ast.StmtFinally{
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `try{$a;}catch(Exception|\RuntimeException$e){$b;}finally{;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtUnset(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtUnset{
2020-05-17 19:56:32 +00:00
Vars: []ast.Vertex{
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
&ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$b")},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `unset($a,$b);`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintUse(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-28 21:01:02 +00:00
n := &ast.StmtUseList{
Type: &ast.Identifier{Value: []byte("function")},
2020-12-28 21:01:02 +00:00
Uses: []ast.Vertex{
&ast.StmtUse{
Use: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
2020-12-28 21:01:02 +00:00
&ast.StmtUse{
Use: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Baz")}}},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `use function Foo as Bar,Baz;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtGroupUse(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-28 21:01:02 +00:00
n := &ast.StmtGroupUseList{
Type: &ast.Identifier{Value: []byte("function")},
2020-12-28 21:01:02 +00:00
Prefix: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
Uses: []ast.Vertex{
&ast.StmtUse{
Use: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
2020-05-17 19:56:32 +00:00
Alias: &ast.Identifier{Value: []byte("Bar")},
},
2020-12-28 21:01:02 +00:00
&ast.StmtUse{
Use: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Baz")}}},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `use function Foo\{Foo as Bar,Baz};`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintUseDeclaration(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-28 21:01:02 +00:00
n := &ast.StmtUse{
Type: &ast.Identifier{Value: []byte("function")},
2020-12-28 21:01:02 +00:00
Use: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `function Foo as Bar`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintWhileStmtList(t *testing.T) {
o := bytes.NewBufferString("")
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
2020-12-11 07:54:26 +00:00
n := &ast.StmtWhile{
2020-05-17 19:56:32 +00:00
Cond: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
},
2020-05-17 19:56:32 +00:00
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
2020-12-28 21:01:02 +00:00
Name: &ast.Identifier{Value: []byte("$a")},
}},
},
},
2020-12-11 07:54:26 +00:00
}
n.Accept(p)
expected := `while($a){$a;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}