2020-12-28 19:13:08 +00:00
|
|
|
package printer_test
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-12-11 07:54:26 +00:00
|
|
|
"github.com/z7zmey/php-parser/pkg/token"
|
2020-12-28 19:13:08 +00:00
|
|
|
"github.com/z7zmey/php-parser/pkg/visitor/printer"
|
2020-06-29 21:45:15 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-05-17 19:56:32 +00:00
|
|
|
"github.com/z7zmey/php-parser/pkg/ast"
|
2018-03-18 20:27:34 +00:00
|
|
|
)
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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{
|
2020-12-28 08:10:15 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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>")},
|
2020-09-10 20:11:08 +00:00
|
|
|
&ast.StmtEcho{
|
|
|
|
Exprs: []ast.Vertex{
|
|
|
|
&ast.ScalarString{
|
|
|
|
Value: []byte(`"a"`),
|
2018-07-29 08:44:38 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.StmtInlineHtml{Value: []byte("<div>HTML</div>")},
|
2020-09-10 20:11:08 +00:00
|
|
|
&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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:27:34 +00:00
|
|
|
// node
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintIdentifier(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
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"),
|
2018-07-02 17:48:55 +00:00
|
|
|
}
|
2020-12-11 07:54:26 +00:00
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `test`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintParameter(t *testing.T) {
|
2018-03-18 20:27:34 +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.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"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
DefaultValue: &ast.ScalarString{
|
|
|
|
Value: []byte("'default'"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "\\Foo...$var='default'"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNullable(t *testing.T) {
|
2018-03-18 20:27:34 +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.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"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
DefaultValue: &ast.ScalarString{
|
|
|
|
Value: []byte("'default'"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "?\\Foo&$var='default'"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintArgument(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "...$var"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintArgumentByRef(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "&$var"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// name
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNameNamePart(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
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)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "foo"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNameName(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
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"),
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
&ast.NamePart{
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: []byte("Bar"),
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "Foo\\Bar"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNameFullyQualified(t *testing.T) {
|
2018-03-18 20:27:34 +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.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"),
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
&ast.NamePart{
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: []byte("Bar"),
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "\\Foo\\Bar"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNameRelative(t *testing.T) {
|
2018-03-18 20:27:34 +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.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"),
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
&ast.NamePart{
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: []byte("Bar"),
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "namespace\\Foo\\Bar"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scalar
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarLNumber(t *testing.T) {
|
2018-03-18 20:27:34 +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.ScalarLnumber{
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: []byte("1"),
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "1"
|
2018-07-02 17:48:55 +00:00
|
|
|
actual := o.String()
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarDNumber(t *testing.T) {
|
2018-03-18 20:27:34 +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.ScalarDnumber{
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: []byte(".1"),
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := ".1"
|
2018-07-02 17:48:55 +00:00
|
|
|
actual := o.String()
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarString(t *testing.T) {
|
2018-03-18 20:27:34 +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.ScalarString{
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: []byte("'hello world'"),
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `'hello world'`
|
2018-04-03 16:59:20 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarEncapsedStringPart(t *testing.T) {
|
2018-03-18 20:27:34 +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.ScalarEncapsedStringPart{
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: []byte("hello world"),
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `hello world`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarEncapsed(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ScalarEncapsedStringPart{Value: []byte(" world")},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `"hello $var world"`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarMagicConstant(t *testing.T) {
|
2018-03-18 20:27:34 +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.ScalarMagicConstant{
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: []byte("__DIR__"),
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
if o.String() != `__DIR__` {
|
2018-03-18 20:27:34 +00:00
|
|
|
t.Errorf("TestPrintScalarMagicConstant is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssign(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintReference(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a=&$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignBitwiseAnd(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a&=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignBitwiseOr(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a|=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignBitwiseXor(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a^=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:26:54 +00:00
|
|
|
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")},
|
2019-12-28 20:26:54 +00:00
|
|
|
},
|
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")},
|
2019-12-28 20:26:54 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2019-12-28 20:26:54 +00:00
|
|
|
|
|
|
|
expected := `$a??=$b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignConcat(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a.=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignDiv(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a/=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignMinus(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a-=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignMod(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a%=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignMul(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a*=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignPlus(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a+=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignPow(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a**=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignShiftLeft(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<<=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignShiftRight(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a>>=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// binary
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBitwiseAnd(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a&$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBitwiseOr(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a|$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBitwiseXor(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a^$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBooleanAnd(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-10-24 14:04:13 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a&&$b`
|
2018-10-24 14:04:13 +00:00
|
|
|
actual := o.String()
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBooleanOr(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a||$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryCoalesce(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a??$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryConcat(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a.$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryDiv(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a/$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryEqual(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a==$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryGreaterOrEqual(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a>=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryGreater(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a>$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryIdentical(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a===$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryLogicalAnd(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `$a and$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryLogicalOr(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `$a or$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryLogicalXor(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `$a xor$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryMinus(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a-$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryMod(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a%$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryMul(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a*$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryNotEqual(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a!=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryNotIdentical(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a!==$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryPlus(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a+$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryPow(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a**$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryShiftLeft(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<<$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryShiftRight(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a>>$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinarySmallerOrEqual(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinarySmaller(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinarySpaceship(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<=>$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cast
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintArray(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(array)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBool(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `(bool)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintDouble(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(float)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintInt(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-26 20:48:02 +00:00
|
|
|
expected := `(int)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintObject(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(object)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintString(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(string)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintUnset(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(unset)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// expr
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprArrayDimFetch(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var[1]`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprArrayItemWithKey(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `'Hello'=>$world`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprArrayItem(t *testing.T) {
|
2018-03-18 20:27:34 +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.ExprArrayItem{
|
2020-12-26 18:13:50 +00:00
|
|
|
Val: &ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$world")},
|
2020-12-26 18:13:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
2020-12-26 18:13:50 +00:00
|
|
|
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-26 18:13:50 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprArray(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
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
|
|
|
},
|
2018-03-18 20:27:34 +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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `array('Hello'=>$world,2=>&$var,$var)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprBitwiseNot(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `~$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprBooleanNot(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `!$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 22:02:14 +00:00
|
|
|
func TestPrinterPrintExprBracket(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2020-12-28 19:13:08 +00:00
|
|
|
p := printer.NewPrinter(o).WithState(printer.PrinterStatePHP)
|
2020-12-27 22:02:14 +00:00
|
|
|
n := &ast.ExprBooleanNot{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$var")},
|
2020-12-27 22:02:14 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
n.Accept(p)
|
|
|
|
|
|
|
|
expected := `!$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprClassConstFetch(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
Const: &ast.Identifier{
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: []byte("CONST"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var::CONST`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprClone(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `clone$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprClosureUse(t *testing.T) {
|
2018-03-18 20:27:34 +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.ExprClosureUse{
|
2020-12-26 16:20:10 +00:00
|
|
|
Var: &ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$foo")},
|
2018-05-25 06:38:44 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-26 16:20:10 +00:00
|
|
|
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)
|
2020-12-26 16:20:10 +00:00
|
|
|
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")},
|
2020-12-26 16:20:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
n.Accept(p)
|
|
|
|
|
|
|
|
expected := `&$foo`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprClosure(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
Uses: []ast.Vertex{
|
2020-12-26 16:20:10 +00:00
|
|
|
&ast.ExprClosureUse{
|
|
|
|
AmpersandTkn: &token.Token{
|
|
|
|
Value: []byte("&"),
|
|
|
|
},
|
|
|
|
Var: &ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$a")},
|
2020-12-26 16:20:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&ast.ExprClosureUse{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
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")}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-07-02 17:48:55 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `static function&($var)use(&$a,$b):\Foo{$a;}`
|
2018-07-02 17:48:55 +00:00
|
|
|
actual := o.String()
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:26:54 +00:00
|
|
|
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")},
|
2019-12-28 20:26:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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")}},
|
2019-12-28 20:26:54 +00:00
|
|
|
},
|
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")},
|
2019-12-28 20:26:54 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2019-12-28 20:26:54 +00:00
|
|
|
|
|
|
|
expected := `static fn&(&$var):\Foo=>$a;`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprConstFetch(t *testing.T) {
|
2018-03-20 17:48: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.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)
|
2018-03-20 17:48:55 +00:00
|
|
|
|
|
|
|
expected := "null"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintEmpty(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `empty($var)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrettyPrinterrorSuppress(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `@$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintEval(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `eval($var)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExit(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `exit$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-07-09 21:51:02 +00:00
|
|
|
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")},
|
2018-07-09 21:51:02 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-07-09 21:51:02 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `die$var`
|
2018-07-09 21:51:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-03-18 20:27:34 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintFunctionCall(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var(&$a,...$b,$c)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintInclude(t *testing.T) {
|
2018-03-18 20:27:34 +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.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)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `include'path'`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintIncludeOnce(t *testing.T) {
|
2018-03-18 20:27:34 +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.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)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `include_once'path'`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintInstanceOf(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var instanceof Foo`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintIsset(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `isset($a,$b)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintList(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +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("$c")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `list($a,list($b,$c))`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintMethodCall(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$foo->bar($a,$b)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNew(t *testing.T) {
|
2018-03-18 20:27:34 +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.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"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `new Foo($a,$b)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPostDec(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var--`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPostInc(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var++`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPreDec(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `--$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPreInc(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `++$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPrint(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `print$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPropertyFetch(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$foo->bar`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintRequire(t *testing.T) {
|
2018-03-18 20:27:34 +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.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)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `require'path'`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintRequireOnce(t *testing.T) {
|
2018-03-18 20:27:34 +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.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)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `require_once'path'`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintShellExec(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ScalarEncapsedStringPart{Value: []byte("!")},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "`hello $world!`"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprShortArray(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
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
|
|
|
},
|
2018-03-18 20:27:34 +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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `['Hello'=>$world,2=>&$var,$var]`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintShortList(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +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("$c")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
CloseBracketTkn: &token.Token{
|
|
|
|
Value: []byte("]"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `[$a,list($b,$c)]`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStaticCall(t *testing.T) {
|
2018-03-18 20:27:34 +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.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
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::bar($a,$b)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStaticPropertyFetch(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::$bar`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintTernary(t *testing.T) {
|
2018-03-18 20:27:34 +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.ExprTernary{
|
2020-12-28 21:01:02 +00:00
|
|
|
Cond: &ast.ExprVariable{
|
|
|
|
Name: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a?:$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintTernaryFull(t *testing.T) {
|
2018-03-18 20:27:34 +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.ExprTernary{
|
2020-12-28 21:01:02 +00:00
|
|
|
Cond: &ast.ExprVariable{
|
|
|
|
Name: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a?$b:$c`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintUnaryMinus(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `-$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintUnaryPlus(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `+$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintVariable(t *testing.T) {
|
2018-03-18 20:27:34 +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.ExprVariable{
|
|
|
|
DollarTkn: &token.Token{
|
|
|
|
Value: []byte("$"),
|
|
|
|
},
|
2020-12-27 19:55:36 +00:00
|
|
|
OpenCurlyBracketTkn: &token.Token{
|
|
|
|
Value: []byte("{"),
|
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.ExprVariable{
|
|
|
|
Name: &ast.Identifier{Value: []byte("$var")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-27 19:55:36 +00:00
|
|
|
CloseCurlyBracketTkn: &token.Token{
|
|
|
|
Value: []byte("}"),
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-27 19:55:36 +00:00
|
|
|
expected := `${$var}`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintYieldFrom(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `yield from$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintYield(t *testing.T) {
|
2018-03-18 20:27:34 +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.ExprYield{
|
2020-12-28 21:01:02 +00:00
|
|
|
Val: &ast.ExprVariable{
|
|
|
|
Name: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `yield$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintYieldFull(t *testing.T) {
|
2018-03-18 20:27:34 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
Val: &ast.ExprVariable{
|
|
|
|
Name: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `yield$k=>$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 20:50:07 +00:00
|
|
|
|
|
|
|
// stmt
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltElseIf(t *testing.T) {
|
2018-03-19 20:50: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.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")},
|
2018-10-24 14:04:13 +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("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-19 20:50:07 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a):$b;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltElseIfEmpty(t *testing.T) {
|
2018-04-02 19:50:36 +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.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")},
|
2018-10-24 14:04:13 +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{},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-02 19:50:36 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a):`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltElse(t *testing.T) {
|
2018-03-19 20:50: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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-19 20:50:07 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `else:$b;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltElseEmpty(t *testing.T) {
|
2018-04-02 19:50:36 +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.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)
|
2018-04-02 19:50:36 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `else:`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltFor(t *testing.T) {
|
2018-03-19 20:50: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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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")},
|
2018-12-17 13:24:13 +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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-19 20:50:07 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `for($a;$b;$c):$d;endfor;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltForeach(t *testing.T) {
|
2018-03-19 20:50: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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-19 20:50:07 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `foreach($var as$key=>&$val):$d;endforeach;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltIf(t *testing.T) {
|
2018-03-19 20:50: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")},
|
2018-10-24 14:04:13 +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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
ElseIf: []ast.Vertex{
|
2020-09-03 19:18:06 +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("$b")},
|
2018-10-24 14:04:13 +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("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-09-03 19:18:06 +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")},
|
2018-10-24 14:04:13 +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{},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-09-03 19:18:06 +00:00
|
|
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-19 20:50:07 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `if($a):$d;elseif($b):$b;elseif($c):else:$b;endif;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtAltSwitch(t *testing.T) {
|
2018-03-19 20:50: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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
ColonTkn: &token.Token{
|
|
|
|
Value: []byte(":"),
|
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
Cases: []ast.Vertex{
|
2020-09-06 09:45:08 +00:00
|
|
|
&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")},
|
2020-09-06 09:45:08 +00:00
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-09-06 09:45:08 +00:00
|
|
|
},
|
|
|
|
&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-09-06 09:45:08 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-19 20:50:07 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `switch($var):case'a':$a;case'b':$b;endswitch;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltWhile(t *testing.T) {
|
2018-03-19 20:50: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.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")},
|
2018-10-24 14:04:13 +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("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-19 20:50:07 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `while($a):$b;endwhile;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtBreak(t *testing.T) {
|
2018-03-20 10:21:21 +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.StmtBreak{
|
2020-05-17 19:56:32 +00:00
|
|
|
Expr: &ast.ScalarLnumber{
|
|
|
|
Value: []byte("1"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-20 10:21:21 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "break 1;"
|
2018-03-20 10:21:21 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtCase(t *testing.T) {
|
2018-03-19 20:50: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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-19 20:50:07 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `case$a:$a;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtCaseEmpty(t *testing.T) {
|
2018-04-02 19:50:36 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmts: []ast.Vertex{},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-02 19:50:36 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := "case$a:"
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtCatch(t *testing.T) {
|
2018-03-20 10:21:21 +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.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")}}},
|
2018-07-02 17:48: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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-20 10:21:21 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-20 10:21:21 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `catch(Exception|\RuntimeException$e){$a;}`
|
2018-03-20 10:21:21 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtClassMethod(t *testing.T) {
|
2018-03-20 17:48: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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
DefaultValue: &ast.ExprConstFetch{Const: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("null")}}}},
|
2018-03-20 17:48:55 +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-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-20 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
ReturnType: &ast.Name{
|
|
|
|
Parts: []ast.Vertex{&ast.NamePart{Value: []byte("void")}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-06-03 06:35:44 +00:00
|
|
|
},
|
2018-03-20 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-20 17:48:55 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `public function&foo(?int&$a=null,...$b):void{$a;}`
|
2018-03-20 17:48:55 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
2018-06-30 21:05:59 +00:00
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-07-29 08:44:38 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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-10-24 14:04:13 +00:00
|
|
|
},
|
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")}},
|
2018-07-02 17:48:55 +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-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 {
|
2018-03-20 17:48:55 +00:00
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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")}}},
|
2020-12-28 08:10:15 +00:00
|
|
|
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")}}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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")},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Consts: []ast.Vertex{
|
|
|
|
&ast.StmtConstant{
|
2020-08-24 20:28:44 +00:00
|
|
|
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
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
Extends: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Bar")}}},
|
2020-12-28 08:10:15 +00:00
|
|
|
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")}}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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{
|
2020-08-24 20:28:44 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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{
|
2020-08-24 20:28:44 +00:00
|
|
|
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{
|
2020-08-24 20:28:44 +00:00
|
|
|
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
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `public const FOO='a',BAR='b';`
|
2018-07-29 08:44:38 +00:00
|
|
|
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{
|
2020-08-24 20:28:44 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("FOO")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'a'")},
|
2018-07-29 08:44:38 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.StmtConstant{
|
2020-08-24 20:28:44 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("BAR")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'b'")},
|
2018-07-29 08:44:38 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-07-29 08:44:38 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtConstant(t *testing.T) {
|
2018-03-20 10:21:21 +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.StmtConstant{
|
2020-08-24 20:28:44 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("FOO")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-20 10:21:21 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "FOO='BAR'"
|
2018-03-20 10:21:21 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-20 18:37:55 +00:00
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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{
|
2020-08-24 20:28:44 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("FOO")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'bar'")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
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
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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{
|
2020-08-24 20:28:44 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("FOO")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'bar'")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
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
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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{
|
2020-08-24 20:28:44 +00:00
|
|
|
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
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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-10-24 14:04:13 +00:00
|
|
|
}},
|
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
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `default:$a;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtDefalutEmpty(t *testing.T) {
|
2018-04-02 19:50:36 +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{},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-02 19:50:36 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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-10-24 14:04:13 +00:00
|
|
|
}},
|
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()
|
|
|
|
|
2018-03-31 11:08:56 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
func TestPrinterPrintStmtEchoHtmlState(t *testing.T) {
|
2018-03-31 11:08:56 +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.StmtEcho{
|
|
|
|
Exprs: []ast.Vertex{
|
|
|
|
&ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$a")},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$b")},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-29 08:44:38 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-12-17 13:24:13 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `<?php echo$a,$b;`
|
2018-12-17 13:24:13 +00:00
|
|
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-31 11:08:56 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-31 11:08:56 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `echo$a,$b;`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseIfStmts(t *testing.T) {
|
2018-03-31 11:08: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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
2018-03-31 11:08:56 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-31 11:08:56 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a){;}`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseIfExpr(t *testing.T) {
|
2018-03-31 11:08: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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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-31 11:08:56 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a)'bar';`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseIfNop(t *testing.T) {
|
2018-03-31 11:08: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.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")},
|
2018-10-24 14:04:13 +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-31 11:08:56 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a);`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseStmts(t *testing.T) {
|
2018-03-31 11:08: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.StmtElse{
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
2018-03-31 11:08:56 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-31 11:08:56 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `else{;}`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseExpr(t *testing.T) {
|
2018-03-31 11:08: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.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)
|
2018-03-31 11:08:56 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `else'bar';`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseNop(t *testing.T) {
|
2018-03-31 11:08: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.StmtElse{
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtNop{},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-31 11:08:56 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `else;`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-03-28 20:44:02 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExpression(t *testing.T) {
|
2018-03-19 20:50: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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-19 20:50:07 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtFinally(t *testing.T) {
|
2018-03-31 11:49:29 +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.StmtFinally{
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-31 11:49:29 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `finally{;}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtFor(t *testing.T) {
|
2018-03-31 11:49:29 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$d")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$f")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-31 11:49:29 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `for($a,$b;$c,$d;$e,$f){;}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtForeach(t *testing.T) {
|
2018-03-31 11:49:29 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +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("$v")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-31 11:49:29 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `foreach($a as$k=>$v){;}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
2020-12-26 17:38:45 +00:00
|
|
|
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)
|
2020-12-26 17:38:45 +00:00
|
|
|
n := &ast.StmtForeach{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$a")},
|
2020-12-26 17:38:45 +00:00
|
|
|
},
|
|
|
|
Key: &ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$k")},
|
2020-12-26 17:38:45 +00:00
|
|
|
},
|
|
|
|
AmpersandTkn: &token.Token{
|
|
|
|
Value: []byte("&"),
|
|
|
|
},
|
|
|
|
Var: &ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$v")},
|
2020-12-26 17:38:45 +00:00
|
|
|
},
|
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
n.Accept(p)
|
|
|
|
|
|
|
|
expected := `foreach($a as$k=>&$v){;}`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
2018-03-31 11:49:29 +00:00
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtFunction(t *testing.T) {
|
2018-03-31 11:49:29 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
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")}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-03-31 11:49:29 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `function&foo(&$var):\Foo{;}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtGlobal(t *testing.T) {
|
2018-04-01 12:58:45 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-01 12:58:45 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `global$a,$b;`
|
2018-04-01 12:58:45 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtGoto(t *testing.T) {
|
2018-04-01 12:58:45 +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.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)
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `goto FOO;`
|
2018-04-01 12:58:45 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintHaltCompiler(t *testing.T) {
|
2018-04-01 12:58:45 +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.StmtHaltCompiler{}
|
|
|
|
n.Accept(p)
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `__halt_compiler();`
|
2018-04-01 12:58:45 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtNop{},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
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-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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-10-24 14:04:13 +00:00
|
|
|
},
|
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
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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")},
|
2018-10-24 14:04:13 +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-04-01 14:07:07 +00:00
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 14:07:07 +00:00
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintInterface(t *testing.T) {
|
2018-04-01 21:28:01 +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.StmtInterface{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
|
2020-12-28 08:10:15 +00:00
|
|
|
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")}}},
|
2018-07-02 17:48:55 +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("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-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-04-01 21:28:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 21:28:01 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `interface Foo extends Bar,Baz{public function foo(){$a;}}`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintLabel(t *testing.T) {
|
2018-04-01 21:28:01 +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.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)
|
2018-04-01 21:28:01 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `FOO:`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNamespace(t *testing.T) {
|
2018-04-01 21:28:01 +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.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)
|
2018-04-01 21:28:01 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `namespace Foo;`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNamespaceWithStmts(t *testing.T) {
|
2018-04-01 21:28:01 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-04-01 21:28:01 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 21:28:01 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `namespace Foo{$a;}`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPropertyList(t *testing.T) {
|
2018-04-01 21:43:27 +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.StmtPropertyList{
|
2020-05-17 19:56:32 +00:00
|
|
|
Modifiers: []ast.Vertex{
|
|
|
|
&ast.Identifier{Value: []byte("public")},
|
|
|
|
&ast.Identifier{Value: []byte("static")},
|
2018-04-01 21:43:27 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Expr: &ast.ScalarString{Value: []byte("'a'")},
|
2018-04-01 21:43:27 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-01 21:43:27 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 21:43:27 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `public static Foo$a='a',$b;`
|
2018-04-01 21:43:27 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintProperty(t *testing.T) {
|
2018-04-01 21:43:27 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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-04-01 21:43:27 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a=1`
|
2018-04-01 21:43:27 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintReturn(t *testing.T) {
|
2018-04-01 21:43:27 +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.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)
|
2018-04-01 21:43:27 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `return 1;`
|
2018-04-01 21:43:27 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStaticVar(t *testing.T) {
|
2018-04-01 21:59:31 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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-04-01 21:59:31 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a=1`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStatic(t *testing.T) {
|
2018-04-01 21:59:31 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 21:59:31 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `static$a,$b;`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtList(t *testing.T) {
|
2018-04-01 21:59:31 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 21:59:31 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `{$a;$b;}`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtListNested(t *testing.T) {
|
2018-04-01 21:59:31 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:59:31 +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
|
|
|
|
2018-12-17 13:24:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
Cases: []ast.Vertex{
|
2020-09-06 09:45:08 +00:00
|
|
|
&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")},
|
2020-09-06 09:45:08 +00:00
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-09-06 09:45:08 +00:00
|
|
|
},
|
|
|
|
&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-09-06 09:45:08 +00:00
|
|
|
}},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 21:59:31 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `switch($var){case'a':$a;case'b':$b;}`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtThrow(t *testing.T) {
|
2018-04-01 22:29:24 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 22:29:24 +00:00
|
|
|
|
2020-12-11 07:54:26 +00:00
|
|
|
expected := `throw$var;`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitUseAlias(t *testing.T) {
|
2018-04-01 22:29:24 +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.StmtTraitUseAlias{
|
2020-12-28 21:01:02 +00:00
|
|
|
Trait: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
|
2020-12-28 08:47:09 +00:00
|
|
|
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)
|
2018-04-01 22:29:24 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::a as public b;`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitUsePrecedence(t *testing.T) {
|
2018-04-01 22:29:24 +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.StmtTraitUsePrecedence{
|
2020-12-28 21:01:02 +00:00
|
|
|
Trait: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
|
2020-12-28 08:47:09 +00:00
|
|
|
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")}}},
|
2018-04-01 22:29:24 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 22:29:24 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::a insteadof Bar,Baz;`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitUse(t *testing.T) {
|
2018-04-01 22:29:24 +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.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")}}},
|
2018-04-01 22:29:24 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 22:29:24 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `use Foo,Bar;`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitAdaptations(t *testing.T) {
|
2018-04-01 22:29:24 +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.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")}}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-12-20 21:04:23 +00:00
|
|
|
Adaptations: []ast.Vertex{
|
|
|
|
&ast.StmtTraitUseAlias{
|
2020-12-28 21:01:02 +00:00
|
|
|
Trait: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Foo")}}},
|
2020-12-28 08:47:09 +00:00
|
|
|
Method: &ast.Identifier{Value: []byte("a")},
|
|
|
|
Alias: &ast.Identifier{Value: []byte("b")},
|
2018-04-01 22:29:24 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-01 22:29:24 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `use Foo,Bar{Foo::a as b;}`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintTrait(t *testing.T) {
|
2018-04-02 09:41:47 +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.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-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-02 09:41:47 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `trait Foo{public function foo(){$a;}}`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTry(t *testing.T) {
|
2018-04-02 09:41:47 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Finally: &ast.StmtFinally{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-02 09:41:47 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `try{$a;}catch(Exception|\RuntimeException$e){$b;}finally{;}`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtUnset(t *testing.T) {
|
2018-04-02 09:41:47 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-12-28 21:01:02 +00:00
|
|
|
Name: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-02 09:41:47 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `unset($a,$b);`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 19:22:53 +00:00
|
|
|
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{
|
2020-08-22 13:59:26 +00:00
|
|
|
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")}}},
|
2020-08-22 13:59:26 +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-08-03 19:22:53 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2020-08-03 19:22:53 +00:00
|
|
|
|
2020-08-22 13:59:26 +00:00
|
|
|
expected := `use function Foo as Bar,Baz;`
|
2020-08-03 19:22:53 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 13:59:26 +00:00
|
|
|
func TestPrinterPrintStmtGroupUse(t *testing.T) {
|
2020-08-03 19:22:53 +00:00
|
|
|
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{
|
2020-08-22 13:59:26 +00:00
|
|
|
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")},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
2020-12-28 21:01:02 +00:00
|
|
|
&ast.StmtUse{
|
|
|
|
Use: &ast.Name{Parts: []ast.Vertex{&ast.NamePart{Value: []byte("Baz")}}},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-02 09:41:47 +00:00
|
|
|
|
2020-08-22 13:59:26 +00:00
|
|
|
expected := `use function Foo\{Foo as Bar,Baz};`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 19:22:53 +00:00
|
|
|
func TestPrinterPrintUseDeclaration(t *testing.T) {
|
2018-04-01 12:58:45 +00:00
|
|
|
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{
|
2020-08-22 13:59:26 +00:00
|
|
|
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")}}},
|
2020-08-03 19:22:53 +00:00
|
|
|
Alias: &ast.Identifier{Value: []byte("Bar")},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2020-08-03 19:22:53 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `function Foo as Bar`
|
2018-04-01 12:58:45 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 09:41:47 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintWhileStmtList(t *testing.T) {
|
2018-04-02 09:41:47 +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.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")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 07:54:26 +00:00
|
|
|
}
|
|
|
|
n.Accept(p)
|
2018-04-02 09:41:47 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `while($a){$a;}`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|