2018-03-18 20:27:34 +00:00
|
|
|
package printer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-06-29 21:45:15 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-05-17 19:56:32 +00:00
|
|
|
"github.com/z7zmey/php-parser/pkg/ast"
|
|
|
|
"github.com/z7zmey/php-parser/pkg/token"
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2020-05-17 19:56:32 +00:00
|
|
|
"github.com/z7zmey/php-parser/pkg/printer"
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.Root{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNamespace{
|
|
|
|
NamespaceName: &ast.NameName{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{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")}},
|
|
|
|
ClassName: &ast.NameName{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{Value: []byte("Bar")},
|
2018-04-05 22:12:54 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Extends: &ast.StmtClassExtends{
|
|
|
|
ClassName: &ast.NameName{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{Value: []byte("Baz")},
|
2018-05-12 20:10:01 +00:00
|
|
|
},
|
2018-04-05 22:12:54 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtClassMethod{
|
|
|
|
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
|
|
|
|
MethodName: &ast.Identifier{Value: []byte("greet")},
|
|
|
|
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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.Root{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtInlineHtml{Value: []byte("<div>HTML</div>")},
|
|
|
|
&ast.StmtExpression{
|
|
|
|
Expr: &ast.ExprVariable{
|
|
|
|
Node: ast.Node{
|
|
|
|
Tokens: token.Collection{
|
|
|
|
token.Start: []token.Token{
|
|
|
|
{
|
|
|
|
ID: token.ID('$'),
|
|
|
|
Value: []byte("$"),
|
|
|
|
},
|
2019-02-13 20:18:07 +00:00
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
2018-07-29 08:44:38 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
VarName: &ast.Identifier{
|
|
|
|
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>")},
|
|
|
|
&ast.StmtExpression{
|
|
|
|
Expr: &ast.ExprVariable{
|
|
|
|
Node: ast.Node{
|
|
|
|
Tokens: token.Collection{
|
|
|
|
token.Start: []token.Token{
|
|
|
|
{
|
|
|
|
ID: token.ID('$'),
|
|
|
|
Value: []byte("$"),
|
|
|
|
},
|
2019-02-13 20:18:07 +00:00
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
VarName: &ast.Identifier{
|
|
|
|
Value: []byte("a"),
|
2018-04-05 21:59:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `<div>HTML</div><?php $a;?><div>HTML</div><?php $a;`
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
n := &ast.Identifier{
|
|
|
|
Value: []byte("test"),
|
2018-07-02 17:48:55 +00:00
|
|
|
}
|
|
|
|
p.Print(n)
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.Parameter{
|
|
|
|
Type: &ast.NameFullyQualified{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Foo"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-07-05 19:47:46 +00:00
|
|
|
Var: &ast.Variadic{
|
|
|
|
Var: &ast.ExprVariable{
|
|
|
|
VarName: &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
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.Nullable{
|
|
|
|
Expr: &ast.Parameter{
|
|
|
|
Type: &ast.NameFullyQualified{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Foo"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-07-05 19:47:46 +00:00
|
|
|
Var: &ast.Reference{
|
|
|
|
Var: &ast.ExprVariable{
|
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.Argument{
|
2018-03-18 20:27:34 +00:00
|
|
|
IsReference: false,
|
|
|
|
Variadic: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.Argument{
|
2018-03-18 20:27:34 +00:00
|
|
|
IsReference: true,
|
|
|
|
Variadic: false,
|
2020-05-17 19:56:32 +00:00
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.NameNamePart{
|
|
|
|
Value: []byte("foo"),
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.NameName{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Foo"),
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Bar"),
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.NameFullyQualified{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Foo"),
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Bar"),
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.NameRelative{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Foo"),
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Bar"),
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ScalarLnumber{
|
|
|
|
Value: []byte("1"),
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ScalarDnumber{
|
|
|
|
Value: []byte(".1"),
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ScalarString{
|
|
|
|
Value: []byte("'hello world'"),
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ScalarEncapsedStringPart{
|
|
|
|
Value: []byte("hello world"),
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ScalarEncapsed{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.ScalarEncapsedStringPart{Value: []byte("hello ")},
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ScalarHeredoc{
|
|
|
|
Label: []byte("<<<LBL\n"),
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.ScalarEncapsedStringPart{Value: []byte("hello ")},
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `<<<LBL
|
2018-07-29 08:44:38 +00:00
|
|
|
hello $var world
|
2018-04-05 21:39:04 +00:00
|
|
|
LBL`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarNowdoc(t *testing.T) {
|
2018-04-05 21:39:04 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ScalarHeredoc{
|
|
|
|
Label: []byte("<<<'LBL'\n"),
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.ScalarEncapsedStringPart{Value: []byte("hello world\n")},
|
2018-04-05 21:39:04 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `<<<'LBL'
|
2018-04-05 21:39:04 +00:00
|
|
|
hello world
|
|
|
|
LBL`
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ScalarMagicConstant{
|
|
|
|
Value: []byte("__DIR__"),
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssign{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignReference{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignBitwiseAnd{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignBitwiseOr{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignBitwiseXor{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignCoalesce{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignConcat{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignDiv{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignMinus{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignMod{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignMul{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignPlus{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignPow{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignShiftLeft{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprAssignShiftRight{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryBitwiseAnd{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryBitwiseOr{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryBitwiseXor{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryBooleanAnd{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryBooleanOr{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryCoalesce{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryConcat{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryDiv{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryEqual{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryGreaterOrEqual{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryGreater{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryIdentical{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryLogicalAnd{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryLogicalOr{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryLogicalXor{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryMinus{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryMod{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryMul{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryNotEqual{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryNotIdentical{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryPlus{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryPow{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryShiftLeft{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinaryShiftRight{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinarySmallerOrEqual{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinarySmaller{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBinarySpaceship{
|
|
|
|
Left: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprCastArray{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprCastBool{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(boolean)$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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprCastDouble{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprCastInt{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(integer)$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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprCastObject{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprCastString{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprCastUnset{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprArrayDimFetch{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprArrayItem{
|
|
|
|
Key: &ast.ScalarString{Value: []byte("'Hello'")},
|
|
|
|
Val: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$world")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprArrayItem{
|
|
|
|
Val: &ast.ExprReference{Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$world")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprArrayItem{
|
2019-12-29 20:42:52 +00:00
|
|
|
Unpack: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
Val: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$world")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprArray{
|
|
|
|
Items: []ast.Vertex{
|
|
|
|
&ast.ExprArrayItem{
|
|
|
|
Key: &ast.ScalarString{Value: []byte("'Hello'")},
|
|
|
|
Val: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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")},
|
|
|
|
Val: &ast.ExprReference{Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBitwiseNot{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprBooleanNot{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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 TestPrinterPrintExprClassConstFetch(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprClassConstFetch{
|
|
|
|
Class: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
ConstantName: &ast.Identifier{
|
|
|
|
Value: []byte("CONST"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprClone{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprClosureUse{
|
|
|
|
Uses: []ast.Vertex{
|
|
|
|
&ast.ExprReference{Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$foo")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$bar")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-05-25 06:38:44 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `use(&$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 TestPrinterPrintExprClosure(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprClosure{
|
2018-07-02 17:48:55 +00:00
|
|
|
Static: true,
|
|
|
|
ReturnsRef: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
Params: []ast.Vertex{
|
|
|
|
&ast.Parameter{
|
2020-07-05 19:47:46 +00:00
|
|
|
Var: &ast.Reference{
|
|
|
|
Var: &ast.ExprVariable{
|
|
|
|
VarName: &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
|
|
|
ClosureUse: &ast.ExprClosureUse{
|
|
|
|
Uses: []ast.Vertex{
|
|
|
|
&ast.ExprReference{Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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{
|
|
|
|
Parts: []ast.Vertex{&ast.NameNamePart{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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtExpression{
|
|
|
|
Expr: &ast.ExprArrowFunction{
|
2019-12-28 20:26:54 +00:00
|
|
|
Static: true,
|
|
|
|
ReturnsRef: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
Params: []ast.Vertex{
|
|
|
|
&ast.Parameter{
|
2020-07-05 19:47:46 +00:00
|
|
|
Var: &ast.Reference{
|
|
|
|
Var: &ast.ExprVariable{
|
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
|
|
|
},
|
2019-12-28 20:26:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
ReturnType: &ast.NameFullyQualified{
|
|
|
|
Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}},
|
2019-12-28 20:26:54 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprConstFetch{
|
|
|
|
Const: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("null")}}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprEmpty{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprErrorSuppress{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprEval{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprExit{
|
2018-07-09 21:51:02 +00:00
|
|
|
Die: false,
|
2020-05-17 19:56:32 +00:00
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprExit{
|
2018-07-09 21:51:02 +00:00
|
|
|
Die: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-07-09 21:51:02 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprFunctionCall{
|
|
|
|
Function: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
ArgumentList: &ast.ArgumentList{
|
|
|
|
Arguments: []ast.Vertex{
|
|
|
|
&ast.Argument{
|
2018-04-29 16:58:49 +00:00
|
|
|
IsReference: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.Argument{
|
2018-04-29 16:58:49 +00:00
|
|
|
Variadic: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.Argument{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$c")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprInclude{
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'path'")},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprIncludeOnce{
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'path'")},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprInstanceOf{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Class: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprIsset{
|
|
|
|
Vars: []ast.Vertex{
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprList{
|
|
|
|
Items: []ast.Vertex{
|
|
|
|
&ast.ExprArrayItem{
|
|
|
|
Val: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$c")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprMethodCall{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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")},
|
|
|
|
ArgumentList: &ast.ArgumentList{
|
|
|
|
Arguments: []ast.Vertex{
|
|
|
|
&ast.Argument{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.Argument{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprNew{
|
|
|
|
Class: &ast.NameName{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Foo"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
ArgumentList: &ast.ArgumentList{
|
|
|
|
Arguments: []ast.Vertex{
|
|
|
|
&ast.Argument{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.Argument{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprPostDec{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprPostInc{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprPreDec{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprPreInc{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprPrint{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprPropertyFetch{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$foo")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Property: &ast.Identifier{Value: []byte("bar")},
|
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 TestPrinterPrintExprReference(t *testing.T) {
|
2018-05-14 15:09:11 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprReference{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$foo")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-05-14 15:09:11 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `&$foo`
|
2018-05-14 15:09:11 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprRequire{
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'path'")},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprRequireOnce{
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'path'")},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprShellExec{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.ScalarEncapsedStringPart{Value: []byte("hello ")},
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprShortArray{
|
|
|
|
Items: []ast.Vertex{
|
|
|
|
&ast.ExprArrayItem{
|
|
|
|
Key: &ast.ScalarString{Value: []byte("'Hello'")},
|
|
|
|
Val: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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")},
|
|
|
|
Val: &ast.ExprReference{Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprShortList{
|
|
|
|
Items: []ast.Vertex{
|
|
|
|
&ast.ExprArrayItem{
|
|
|
|
Val: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$c")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprStaticCall{
|
|
|
|
Class: &ast.Identifier{Value: []byte("Foo")},
|
|
|
|
Call: &ast.Identifier{Value: []byte("bar")},
|
|
|
|
ArgumentList: &ast.ArgumentList{
|
|
|
|
Arguments: []ast.Vertex{
|
|
|
|
&ast.Argument{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.Argument{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprStaticPropertyFetch{
|
|
|
|
Class: &ast.Identifier{Value: []byte("Foo")},
|
|
|
|
Property: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$bar")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprTernary{
|
|
|
|
Condition: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprTernary{
|
|
|
|
Condition: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$c")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprUnaryMinus{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprUnaryPlus{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprVariable{
|
|
|
|
VarName: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
})
|
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 TestPrinterPrintYieldFrom(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprYieldFrom{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprYield{
|
|
|
|
Value: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.ExprYield{
|
|
|
|
Key: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$k")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Value: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtAltElseIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtAltElseIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtStmtList{},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtAltElse{
|
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtAltElse{
|
|
|
|
Stmt: &ast.StmtStmtList{},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtAltFor{
|
|
|
|
Init: []ast.Vertex{
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$c")},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$d")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtAltForeach{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$key")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Var: &ast.ExprReference{Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$val")},
|
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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$d")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtAltIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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{
|
|
|
|
&ast.StmtAltElseIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-05-17 19:56:32 +00:00
|
|
|
&ast.StmtAltElseIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$c")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtStmtList{},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Else: &ast.StmtAltElse{
|
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtAltSwitch{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
CaseList: &ast.StmtCaseList{
|
|
|
|
Cases: []ast.Vertex{
|
|
|
|
&ast.StmtCase{
|
|
|
|
Cond: &ast.ScalarString{Value: []byte("'a'")},
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
&ast.StmtCase{
|
|
|
|
Cond: &ast.ScalarString{Value: []byte("'b'")},
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtAltWhile{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtBreak{
|
|
|
|
Expr: &ast.ScalarLnumber{
|
|
|
|
Value: []byte("1"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtCase{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtCase{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmts: []ast.Vertex{},
|
2018-04-02 19:50:36 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtCatch{
|
|
|
|
Types: []ast.Vertex{
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Exception")}}},
|
|
|
|
&ast.NameFullyQualified{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("RuntimeException")}}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtClassMethod{
|
|
|
|
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
|
2018-03-20 17:48:55 +00:00
|
|
|
ReturnsRef: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
MethodName: &ast.Identifier{Value: []byte("foo")},
|
|
|
|
Params: []ast.Vertex{
|
|
|
|
&ast.Parameter{
|
2020-07-29 20:23:44 +00:00
|
|
|
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
|
2020-07-05 19:47:46 +00:00
|
|
|
Var: &ast.Reference{
|
|
|
|
Var: &ast.ExprVariable{
|
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
DefaultValue: &ast.ExprConstFetch{Const: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("null")}}}},
|
2018-03-20 17:48:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.Parameter{
|
2020-07-05 19:47:46 +00:00
|
|
|
Var: &ast.Variadic{
|
|
|
|
Var: &ast.ExprVariable{
|
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-20 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
ReturnType: &ast.NameName{
|
|
|
|
Parts: []ast.Vertex{&ast.NameNamePart{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-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtClassMethod{
|
|
|
|
Modifiers: []ast.Vertex{
|
|
|
|
&ast.Identifier{Value: []byte("public")},
|
|
|
|
&ast.Identifier{Value: []byte("static")},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2018-06-30 21:05:59 +00:00
|
|
|
ReturnsRef: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
MethodName: &ast.Identifier{Value: []byte("foo")},
|
|
|
|
Params: []ast.Vertex{
|
|
|
|
&ast.Parameter{
|
2020-07-29 20:23:44 +00:00
|
|
|
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
|
2020-07-05 19:47:46 +00:00
|
|
|
Var: &ast.Reference{
|
|
|
|
Var: &ast.ExprVariable{
|
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
DefaultValue: &ast.ExprConstFetch{Const: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("null")}}}},
|
2018-06-30 21:05:59 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.Parameter{
|
2020-07-05 19:47:46 +00:00
|
|
|
Var: &ast.Variadic{
|
|
|
|
Var: &ast.ExprVariable{
|
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-06-30 21:05:59 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
ReturnType: &ast.NameName{
|
|
|
|
Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("void")}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtNop{},
|
2018-06-30 21:05:59 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtClass{
|
|
|
|
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("abstract")}},
|
|
|
|
ClassName: &ast.Identifier{Value: []byte("Foo")},
|
|
|
|
Extends: &ast.StmtClassExtends{
|
|
|
|
ClassName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Implements: &ast.StmtClassImplements{
|
|
|
|
InterfaceNames: []ast.Vertex{
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{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{
|
|
|
|
ConstantName: &ast.Identifier{Value: []byte("FOO")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'bar'")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtClass{
|
|
|
|
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("abstract")}},
|
|
|
|
ArgumentList: &ast.ArgumentList{
|
|
|
|
Arguments: []ast.Vertex{
|
|
|
|
&ast.Argument{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.Argument{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
Extends: &ast.StmtClassExtends{
|
|
|
|
ClassName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Implements: &ast.StmtClassImplements{
|
|
|
|
InterfaceNames: []ast.Vertex{
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{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{
|
|
|
|
ConstantName: &ast.Identifier{Value: []byte("FOO")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'bar'")},
|
2018-03-20 18:06:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtClassConstList{
|
|
|
|
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
|
|
|
|
Consts: []ast.Vertex{
|
|
|
|
&ast.StmtConstant{
|
|
|
|
ConstantName: &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{
|
|
|
|
ConstantName: &ast.Identifier{Value: []byte("BAR")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'b'")},
|
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("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtConstList{
|
|
|
|
Consts: []ast.Vertex{
|
|
|
|
&ast.StmtConstant{
|
|
|
|
ConstantName: &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{
|
|
|
|
ConstantName: &ast.Identifier{Value: []byte("BAR")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'b'")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtConstant{
|
|
|
|
ConstantName: &ast.Identifier{Value: []byte("FOO")},
|
|
|
|
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtContinue{
|
|
|
|
Expr: &ast.ScalarLnumber{
|
|
|
|
Value: []byte("1"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtDeclare{
|
|
|
|
Consts: []ast.Vertex{
|
|
|
|
&ast.StmtConstant{
|
|
|
|
ConstantName: &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
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtDeclare{
|
|
|
|
Consts: []ast.Vertex{
|
|
|
|
&ast.StmtConstant{
|
|
|
|
ConstantName: &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'")}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtDeclare{
|
|
|
|
Consts: []ast.Vertex{
|
|
|
|
&ast.StmtConstant{
|
|
|
|
ConstantName: &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{},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtDefault{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtDefault{
|
|
|
|
Stmts: []ast.Vertex{},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtDo{
|
|
|
|
Cond: &ast.ScalarLnumber{Value: []byte("1")},
|
|
|
|
Stmt: &ast.StmtExpression{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2018-03-28 21:04:09 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtDo{
|
|
|
|
Cond: &ast.ScalarLnumber{Value: []byte("1")},
|
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-03-28 21:04:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.Root{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtEcho{
|
|
|
|
Exprs: []ast.Vertex{
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
})
|
|
|
|
|
2019-03-20 19:21:18 +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("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtEcho{
|
|
|
|
Exprs: []ast.Vertex{
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-03-31 11:08:56 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtElseIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtElseIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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'")}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtElseIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtNop{},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtElse{
|
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
2018-03-31 11:08:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtElse{
|
|
|
|
Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}},
|
2018-03-31 11:08:56 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtElse{
|
|
|
|
Stmt: &ast.StmtNop{},
|
2018-03-31 11:08:56 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtExpression{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtFinally{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtNop{},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtFor{
|
|
|
|
Init: []ast.Vertex{
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$c")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$e")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtForeach{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `foreach($a as $k=>$v){;}`
|
2018-03-31 11:49:29 +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 TestPrinterPrintStmtFunction(t *testing.T) {
|
2018-03-31 11:49:29 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtFunction{
|
2018-07-02 17:48:55 +00:00
|
|
|
ReturnsRef: true,
|
2020-05-17 19:56:32 +00:00
|
|
|
FunctionName: &ast.Identifier{Value: []byte("foo")},
|
|
|
|
Params: []ast.Vertex{
|
|
|
|
&ast.Parameter{
|
2020-07-05 19:47:46 +00:00
|
|
|
Var: &ast.Reference{
|
|
|
|
Var: &ast.ExprVariable{
|
|
|
|
VarName: &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{
|
|
|
|
Parts: []ast.Vertex{&ast.NameNamePart{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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtGlobal{
|
|
|
|
Vars: []ast.Vertex{
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-01 12:58:45 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtGoto{
|
|
|
|
Label: &ast.Identifier{Value: []byte("FOO")},
|
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 TestPrinterPrintStmtGroupUse(t *testing.T) {
|
2018-04-01 12:58:45 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtGroupUse{
|
|
|
|
UseType: &ast.Identifier{Value: []byte("function")},
|
|
|
|
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
UseList: []ast.Vertex{
|
|
|
|
&ast.StmtUse{
|
|
|
|
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
|
|
|
Alias: &ast.Identifier{Value: []byte("Baz")},
|
2018-04-01 12:58:45 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.StmtUse{
|
|
|
|
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Quuz")}}},
|
2018-04-01 12:58:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `use function Foo\{Bar as Baz,Quuz};`
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtHaltCompiler{})
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtIf{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmt: &ast.StmtNop{},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.Root{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtInlineHtml{
|
|
|
|
Value: []byte("test"),
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtInterface{
|
|
|
|
InterfaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Extends: &ast.StmtInterfaceExtends{
|
|
|
|
InterfaceNames: []ast.Vertex{
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtClassMethod{
|
|
|
|
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
|
|
|
|
MethodName: &ast.Identifier{Value: []byte("foo")},
|
|
|
|
Params: []ast.Vertex{},
|
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtLabel{
|
|
|
|
LabelName: &ast.Identifier{Value: []byte("FOO")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtNamespace{
|
|
|
|
NamespaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtNamespace{
|
|
|
|
NamespaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtNop{})
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtPropertyList{
|
|
|
|
Modifiers: []ast.Vertex{
|
|
|
|
&ast.Identifier{Value: []byte("public")},
|
|
|
|
&ast.Identifier{Value: []byte("static")},
|
2018-04-01 21:43:27 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Type: &ast.NameName{
|
|
|
|
Parts: []ast.Vertex{
|
|
|
|
&ast.NameNamePart{
|
|
|
|
Value: []byte("Foo"),
|
2019-12-29 14:36:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Properties: []ast.Vertex{
|
|
|
|
&ast.StmtProperty{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-01 21:43:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-12-29 14:36:56 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtProperty{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtReturn{
|
|
|
|
Expr: &ast.ScalarLnumber{Value: []byte("1")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtStaticVar{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtStatic{
|
|
|
|
Vars: []ast.Vertex{
|
|
|
|
&ast.StmtStaticVar{
|
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtSwitch{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
CaseList: &ast.StmtCaseList{
|
|
|
|
Cases: []ast.Vertex{
|
|
|
|
&ast.StmtCase{
|
|
|
|
Cond: &ast.ScalarString{Value: []byte("'a'")},
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
&ast.StmtCase{
|
|
|
|
Cond: &ast.ScalarString{Value: []byte("'b'")},
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
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-12-17 13:24:13 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtThrow{
|
|
|
|
Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$var")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2018-04-01 22:29:24 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +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-10-29 12:47:29 +00:00
|
|
|
func TestPrinterPrintStmtTraitAdaptationList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtTraitAdaptationList{
|
|
|
|
Adaptations: []ast.Vertex{
|
|
|
|
&ast.StmtTraitUseAlias{
|
|
|
|
Ref: &ast.StmtTraitMethodRef{
|
|
|
|
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Method: &ast.Identifier{Value: []byte("a")},
|
2018-10-29 12:47:29 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Alias: &ast.Identifier{Value: []byte("b")},
|
2018-10-29 12:47:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `{Foo::a as b;}`
|
2018-10-29 12:47: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 TestPrinterPrintStmtTraitMethodRef(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtTraitMethodRef{
|
|
|
|
Method: &ast.Identifier{Value: []byte("a")},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expected := `a`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
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 TestPrinterPrintStmtTraitMethodRefFull(t *testing.T) {
|
2018-04-01 22:29:24 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtTraitMethodRef{
|
|
|
|
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Method: &ast.Identifier{Value: []byte("a")},
|
2018-04-01 22:29:24 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::a`
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtTraitUseAlias{
|
|
|
|
Ref: &ast.StmtTraitMethodRef{
|
|
|
|
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Method: &ast.Identifier{Value: []byte("a")},
|
2018-04-01 22:29:24 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Modifier: &ast.Identifier{Value: []byte("public")},
|
|
|
|
Alias: &ast.Identifier{Value: []byte("b")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtTraitUsePrecedence{
|
|
|
|
Ref: &ast.StmtTraitMethodRef{
|
|
|
|
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Method: &ast.Identifier{Value: []byte("a")},
|
2018-04-01 22:29:24 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Insteadof: []ast.Vertex{
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtTraitUse{
|
|
|
|
Traits: []ast.Vertex{
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
2018-04-01 22:29:24 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
TraitAdaptationList: &ast.StmtNop{},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtTraitUse{
|
|
|
|
Traits: []ast.Vertex{
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Bar")}}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
TraitAdaptationList: &ast.StmtTraitAdaptationList{
|
|
|
|
Adaptations: []ast.Vertex{
|
|
|
|
&ast.StmtTraitUseAlias{
|
|
|
|
Ref: &ast.StmtTraitMethodRef{
|
|
|
|
Trait: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Method: &ast.Identifier{Value: []byte("a")},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Alias: &ast.Identifier{Value: []byte("b")},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtTrait{
|
|
|
|
TraitName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtClassMethod{
|
|
|
|
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
|
|
|
|
MethodName: &ast.Identifier{Value: []byte("foo")},
|
|
|
|
Params: []ast.Vertex{},
|
|
|
|
Stmt: &ast.StmtStmtList{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtTry{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtExpression{Expr: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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{
|
|
|
|
&ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Exception")}}},
|
|
|
|
&ast.NameFullyQualified{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("RuntimeException")}}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
Var: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &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
|
|
|
},
|
|
|
|
},
|
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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtUnset{
|
|
|
|
Vars: []ast.Vertex{
|
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$b")},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtUseList(t *testing.T) {
|
2018-04-02 09:41:47 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtUseList{
|
|
|
|
UseType: &ast.Identifier{Value: []byte("function")},
|
|
|
|
Uses: []ast.Vertex{
|
|
|
|
&ast.StmtUse{
|
|
|
|
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Alias: &ast.Identifier{Value: []byte("Bar")},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
2020-05-17 19:56:32 +00:00
|
|
|
&ast.StmtUse{
|
|
|
|
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `use function 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintUse(t *testing.T) {
|
2018-04-01 12:58:45 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtUse{
|
|
|
|
UseType: &ast.Identifier{Value: []byte("function")},
|
|
|
|
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
|
|
|
|
Alias: &ast.Identifier{Value: []byte("Bar")},
|
2018-04-01 12:58:45 +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("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2020-05-17 19:56:32 +00:00
|
|
|
p.Print(&ast.StmtWhile{
|
|
|
|
Cond: &ast.ExprVariable{
|
2020-06-29 21:45:15 +00:00
|
|
|
VarName: &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-06-29 21:45:15 +00:00
|
|
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
2018-10-24 14:04:13 +00:00
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
}
|