2018-03-18 20:27:34 +00:00
|
|
|
package printer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
|
|
"github.com/z7zmey/php-parser/node/expr"
|
|
|
|
"github.com/z7zmey/php-parser/node/expr/assign"
|
|
|
|
"github.com/z7zmey/php-parser/node/expr/binary"
|
|
|
|
"github.com/z7zmey/php-parser/node/expr/cast"
|
|
|
|
"github.com/z7zmey/php-parser/node/name"
|
|
|
|
"github.com/z7zmey/php-parser/node/scalar"
|
2018-03-19 20:50:07 +00:00
|
|
|
"github.com/z7zmey/php-parser/node/stmt"
|
2018-03-18 20:27:34 +00:00
|
|
|
"github.com/z7zmey/php-parser/printer"
|
|
|
|
)
|
|
|
|
|
2018-04-05 21:59:22 +00:00
|
|
|
func TestPrintFile(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-05 22:12:54 +00:00
|
|
|
p := printer.NewPrinter(o, "\t")
|
2018-04-05 21:59:22 +00:00
|
|
|
p.PrintFile(&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-04-05 22:12:54 +00:00
|
|
|
&stmt.Namespace{
|
|
|
|
NamespaceName: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "Foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&stmt.Class{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
|
|
|
|
ClassName: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "Bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Extends: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "Baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassMethod{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
MethodName: &node.Identifier{Value: "greet"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Echo{
|
|
|
|
Exprs: []node.Node{
|
|
|
|
&scalar.String{Value: "'Hello world'"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-04-05 21:59:22 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `<?php
|
2018-04-05 22:12:54 +00:00
|
|
|
namespace Foo;
|
|
|
|
abstract class Bar extends Baz
|
|
|
|
{
|
|
|
|
public function greet()
|
|
|
|
{
|
|
|
|
echo 'Hello world';
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 21:59:22 +00:00
|
|
|
`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintFileInlineHtml(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.PrintFile(&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.InlineHtml{Value: "<div>HTML</div>"},
|
|
|
|
&stmt.Expression{
|
|
|
|
Expr: &scalar.Heredoc{
|
|
|
|
Label: "\"LBL\"",
|
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello world\n"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `<div>HTML</div><?php
|
|
|
|
<<<"LBL"
|
|
|
|
hello world
|
|
|
|
LBL;
|
|
|
|
`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:27:34 +00:00
|
|
|
// node
|
|
|
|
|
|
|
|
func TestPrintIdentifier(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&node.Identifier{Value: "test"})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
if o.String() != `test` {
|
|
|
|
t.Errorf("TestPrintIdentifier is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintParameter(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&node.Parameter{
|
2018-03-18 20:27:34 +00:00
|
|
|
ByRef: false,
|
|
|
|
Variadic: true,
|
|
|
|
VariableType: &name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
2018-04-03 16:59:20 +00:00
|
|
|
DefaultValue: &scalar.String{Value: "'default'"},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expected := "\\Foo ...$var = 'default'"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintNullable(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&node.Nullable{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &node.Parameter{
|
|
|
|
ByRef: false,
|
|
|
|
Variadic: true,
|
|
|
|
VariableType: &name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
2018-04-03 16:59:20 +00:00
|
|
|
DefaultValue: &scalar.String{Value: "'default'"},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := "?\\Foo ...$var = 'default'"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintArgument(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&node.Argument{
|
2018-03-18 20:27:34 +00:00
|
|
|
IsReference: false,
|
|
|
|
Variadic: true,
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := "...$var"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestPrintArgumentByRef(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&node.Argument{
|
2018-03-18 20:27:34 +00:00
|
|
|
IsReference: true,
|
|
|
|
Variadic: false,
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := "&$var"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// name
|
|
|
|
|
|
|
|
func TestPrintNameNamePart(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&name.NamePart{
|
2018-03-18 20:27:34 +00:00
|
|
|
Value: "foo",
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := "foo"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintNameName(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&name.Name{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := "Foo\\Bar"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintNameFullyQualified(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&name.FullyQualified{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := "\\Foo\\Bar"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintNameRelative(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&name.Relative{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := "namespace\\Foo\\Bar"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scalar
|
|
|
|
|
|
|
|
func TestPrintScalarLNumber(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&scalar.Lnumber{Value: "1"})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
if o.String() != `1` {
|
|
|
|
t.Errorf("TestPrintScalarLNumber is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintScalarDNumber(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&scalar.Dnumber{Value: ".1"})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
if o.String() != `.1` {
|
|
|
|
t.Errorf("TestPrintScalarDNumber is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintScalarString(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:59:20 +00:00
|
|
|
p.Print(&scalar.String{Value: "'hello world'"})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-04-03 16:59:20 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintScalarEncapsedStringPart(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&scalar.EncapsedStringPart{Value: "hello world"})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
if o.String() != `hello world` {
|
|
|
|
t.Errorf("TestPrintScalarEncapsedStringPart is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintScalarEncapsed(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&scalar.Encapsed{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello "},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
&scalar.EncapsedStringPart{Value: " world"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if o.String() != `"hello $var world"` {
|
|
|
|
t.Errorf("TestPrintScalarEncapsed is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:39:04 +00:00
|
|
|
func TestPrintScalarHeredoc(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&scalar.Heredoc{
|
|
|
|
Label: "LBL",
|
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello "},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
&scalar.EncapsedStringPart{Value: " world\n"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `<<<LBL
|
|
|
|
hello $var world
|
|
|
|
LBL`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintScalarNowdoc(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&scalar.Heredoc{
|
|
|
|
Label: "'LBL'",
|
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello world\n"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `<<<'LBL'
|
|
|
|
hello world
|
|
|
|
LBL`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:27:34 +00:00
|
|
|
func TestPrintScalarMagicConstant(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&scalar.MagicConstant{Value: "__DIR__"})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
if o.String() != `__DIR__` {
|
|
|
|
t.Errorf("TestPrintScalarMagicConstant is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign
|
|
|
|
|
|
|
|
func TestPrintAssign(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.Assign{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a = $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:03:32 +00:00
|
|
|
func TestPrintReference(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-05 09:03:32 +00:00
|
|
|
p.Print(&assign.Reference{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a =& $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignBitwiseAnd(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.BitwiseAnd{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a &= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignBitwiseOr(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.BitwiseOr{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a |= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignBitwiseXor(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.BitwiseXor{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a ^= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignConcat(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.Concat{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a .= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignDiv(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.Div{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a /= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignMinus(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.Minus{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a -= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignMod(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.Mod{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a %= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignMul(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.Mul{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a *= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignPlus(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.Plus{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a += $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignPow(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.Pow{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a **= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignShiftLeft(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.ShiftLeft{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a <<= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAssignShiftRight(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&assign.ShiftRight{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a >>= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// binary
|
|
|
|
|
|
|
|
func TestPrintBinaryBitwiseAnd(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.BitwiseAnd{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a & $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryBitwiseOr(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.BitwiseOr{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a | $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryBitwiseXor(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.BitwiseXor{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a ^ $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryBooleanAnd(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.BooleanAnd{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a && $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryBooleanOr(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.BooleanOr{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a || $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryCoalesce(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Coalesce{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a ?? $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryConcat(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Concat{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a . $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryDiv(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Div{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a / $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryEqual(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Equal{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a == $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryGreaterOrEqual(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.GreaterOrEqual{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a >= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryGreater(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Greater{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a > $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryIdentical(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Identical{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a === $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryLogicalAnd(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.LogicalAnd{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a and $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryLogicalOr(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.LogicalOr{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a or $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryLogicalXor(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.LogicalXor{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a xor $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryMinus(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Minus{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a - $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryMod(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Mod{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a % $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryMul(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Mul{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a * $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryNotEqual(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.NotEqual{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a != $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryNotIdentical(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.NotIdentical{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a !== $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryPlus(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Plus{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a + $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryPow(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Pow{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a ** $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryShiftLeft(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.ShiftLeft{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a << $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinaryShiftRight(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.ShiftRight{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a >> $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinarySmallerOrEqual(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.SmallerOrEqual{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a <= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinarySmaller(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Smaller{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a < $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBinarySpaceship(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&binary.Spaceship{
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a <=> $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cast
|
|
|
|
|
2018-04-05 08:59:29 +00:00
|
|
|
func TestPrintArray(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Array{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `(array)$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:59:29 +00:00
|
|
|
func TestPrintBool(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Bool{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `(bool)$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:59:29 +00:00
|
|
|
func TestPrintDouble(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Double{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `(float)$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:59:29 +00:00
|
|
|
func TestPrintInt(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Int{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `(int)$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:59:29 +00:00
|
|
|
func TestPrintObject(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Object{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `(object)$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:59:29 +00:00
|
|
|
func TestPrintString(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.String{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `(string)$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:59:29 +00:00
|
|
|
func TestPrintUnset(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Unset{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `(unset)$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// expr
|
|
|
|
|
|
|
|
func TestPrintExprArrayDimFetch(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ArrayDimFetch{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
Dim: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$var[1]`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprArrayItemWithKey(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ArrayItem{
|
2018-03-18 20:27:34 +00:00
|
|
|
ByRef: false,
|
2018-04-03 16:59:20 +00:00
|
|
|
Key: &scalar.String{Value: "'Hello'"},
|
2018-03-18 20:27:34 +00:00
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "world"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `'Hello' => $world`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprArrayItem(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ArrayItem{
|
2018-03-18 20:27:34 +00:00
|
|
|
ByRef: true,
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "world"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `&$world`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprArray(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Array{
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
|
|
|
ByRef: false,
|
2018-04-03 16:59:20 +00:00
|
|
|
Key: &scalar.String{Value: "'Hello'"},
|
2018-03-18 20:27:34 +00:00
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "world"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
ByRef: true,
|
|
|
|
Key: &scalar.Lnumber{Value: "2"},
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
ByRef: false,
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `array('Hello' => $world, 2 => &$var, $var)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprBitwiseNot(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.BitwiseNot{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `~$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprBooleanNot(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.BooleanNot{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `!$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprClassConstFetch(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ClassConstFetch{
|
2018-03-18 20:27:34 +00:00
|
|
|
Class: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
ConstantName: &node.Identifier{Value: "CONST"},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$var::CONST`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprClone(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Clone{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `clone $var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprClosureUse(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ClosureUse{
|
2018-03-18 20:27:34 +00:00
|
|
|
ByRef: true,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `&$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprClosure(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-04-02 20:57:22 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&expr.Closure{
|
|
|
|
Static: true,
|
|
|
|
ReturnsRef: true,
|
|
|
|
Params: []node.Node{
|
|
|
|
&node.Parameter{
|
|
|
|
ByRef: true,
|
|
|
|
Variadic: false,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Uses: []node.Node{
|
|
|
|
&expr.ClosureUse{
|
|
|
|
ByRef: true,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&expr.ClosureUse{
|
|
|
|
ByRef: false,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReturnType: &name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 20:57:22 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
static function &(&$var) use (&$a, $b): \Foo {
|
|
|
|
$a;
|
|
|
|
}
|
2018-04-02 20:57:22 +00:00
|
|
|
}`
|
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-20 17:48:55 +00:00
|
|
|
func TestPrintExprConstFetch(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ConstFetch{
|
2018-03-20 17:48:55 +00:00
|
|
|
Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := "null"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:27:34 +00:00
|
|
|
func TestPrintDie(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Die{Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `die($var)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintEmpty(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Empty{Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `empty($var)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintErrorSuppress(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ErrorSuppress{Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `@$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintEval(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Eval{Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `eval($var)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExit(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Exit{Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `exit($var)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintFunctionCall(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.FunctionCall{
|
2018-03-18 20:27:34 +00:00
|
|
|
Function: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
2018-04-29 16:58:49 +00:00
|
|
|
ArgumentList: &node.ArgumentList{
|
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
IsReference: true,
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Variadic: true,
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$var(&$a, ...$b, $c)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintInclude(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:59:20 +00:00
|
|
|
p.Print(&expr.Include{Expr: &scalar.String{Value: "'path'"}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `include 'path'`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintIncludeOnce(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:59:20 +00:00
|
|
|
p.Print(&expr.IncludeOnce{Expr: &scalar.String{Value: "'path'"}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `include_once 'path'`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintInstanceOf(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.InstanceOf{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$var instanceof Foo`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintIsset(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Isset{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variables: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `isset($a, $b)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.List{
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.List{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `list($a, list($b, $c))`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintMethodCall(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.MethodCall{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
|
|
|
|
Method: &node.Identifier{Value: "bar"},
|
2018-04-29 16:58:49 +00:00
|
|
|
ArgumentList: &node.ArgumentList{
|
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$foo->bar($a, $b)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintNew(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.New{
|
2018-03-18 20:27:34 +00:00
|
|
|
Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
2018-04-29 16:58:49 +00:00
|
|
|
ArgumentList: &node.ArgumentList{
|
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `new Foo($a, $b)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintPostDec(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.PostDec{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$var--`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintPostInc(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.PostInc{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$var++`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintPreDec(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.PreDec{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `--$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintPreInc(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.PreInc{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `++$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintPrint(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Print{Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `print($var)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintPropertyFetch(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.PropertyFetch{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
|
|
|
|
Property: &node.Identifier{Value: "bar"},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$foo->bar`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintRequire(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:59:20 +00:00
|
|
|
p.Print(&expr.Require{Expr: &scalar.String{Value: "'path'"}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `require 'path'`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintRequireOnce(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:59:20 +00:00
|
|
|
p.Print(&expr.RequireOnce{Expr: &scalar.String{Value: "'path'"}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `require_once 'path'`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintShellExec(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ShellExec{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello "},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "world"}},
|
|
|
|
&scalar.EncapsedStringPart{Value: "!"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := "`hello $world!`"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintExprShortArray(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ShortArray{
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
2018-04-03 16:59:20 +00:00
|
|
|
Key: &scalar.String{Value: "'Hello'"},
|
2018-03-18 20:27:34 +00:00
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "world"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
ByRef: true,
|
|
|
|
Key: &scalar.Lnumber{Value: "2"},
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `['Hello' => $world, 2 => &$var, $var]`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintShortList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.ShortList{
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.List{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `[$a, list($b, $c)]`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStaticCall(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.StaticCall{
|
2018-03-18 20:27:34 +00:00
|
|
|
Class: &node.Identifier{Value: "Foo"},
|
|
|
|
Call: &node.Identifier{Value: "bar"},
|
2018-04-29 16:58:49 +00:00
|
|
|
ArgumentList: &node.ArgumentList{
|
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `Foo::bar($a, $b)`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStaticPropertyFetch(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.StaticPropertyFetch{
|
2018-03-18 20:27:34 +00:00
|
|
|
Class: &node.Identifier{Value: "Foo"},
|
|
|
|
Property: &expr.Variable{VarName: &node.Identifier{Value: "bar"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `Foo::$bar`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintTernary(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Ternary{
|
2018-03-18 20:27:34 +00:00
|
|
|
Condition: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-04-03 16:59:20 +00:00
|
|
|
IfFalse: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a ?: $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintTernaryFull(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Ternary{
|
2018-03-18 20:27:34 +00:00
|
|
|
Condition: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-04-03 16:59:20 +00:00
|
|
|
IfTrue: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
IfFalse: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a ? $b : $c`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintUnaryMinus(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.UnaryMinus{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `-$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintUnaryPlus(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.UnaryPlus{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `+$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintVariable(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Variable{VarName: &expr.Variable{VarName: &node.Identifier{Value: "var"}}})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
expected := `$$var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintYieldFrom(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.YieldFrom{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `yield from $var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintYield(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Yield{
|
2018-03-18 20:27:34 +00:00
|
|
|
Value: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `yield $var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintYieldFull(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&expr.Yield{
|
2018-03-18 20:27:34 +00:00
|
|
|
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
|
|
|
|
Value: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `yield $k => $var`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 20:50:07 +00:00
|
|
|
|
|
|
|
// stmt
|
|
|
|
|
|
|
|
func TestPrintAltElseIf(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.AltElseIf{
|
2018-03-19 20:50:07 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `elseif ($a) :
|
2018-04-03 16:20:55 +00:00
|
|
|
$b;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAltElseIfEmpty(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.AltElseIf{
|
2018-04-02 19:50:36 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{},
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAltElse(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.AltElse{
|
2018-03-19 20:50:07 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `else :
|
2018-04-03 16:20:55 +00:00
|
|
|
$b;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAltElseEmpty(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.AltElse{
|
2018-04-02 19:50:36 +00:00
|
|
|
Stmt: &stmt.StmtList{},
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAltFor(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.AltFor{
|
|
|
|
Init: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
Cond: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
Loop: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAltForeach(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.AltForeach{
|
|
|
|
ByRef: true,
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
Key: &expr.Variable{VarName: &node.Identifier{Value: "key"}},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "val"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAltIf(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.AltIf{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-03-19 20:50:07 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-03 16:20:55 +00:00
|
|
|
ElseIf: []node.Node{
|
|
|
|
&stmt.AltElseIf{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&stmt.AltElseIf{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
Stmt: &stmt.StmtList{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Else: &stmt.AltElse{
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtAltSwitch(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.AltSwitch{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
2018-04-03 16:59:20 +00:00
|
|
|
Cond: &scalar.String{Value: "'a'"},
|
2018-04-03 16:20:55 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&stmt.Case{
|
2018-04-03 16:59:20 +00:00
|
|
|
Cond: &scalar.String{Value: "'b'"},
|
2018-04-03 16:20:55 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintAltWhile(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.AltWhile{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
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-03-20 10:21:21 +00:00
|
|
|
func TestPrintStmtBreak(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Break{
|
2018-03-20 10:21:21 +00:00
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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-03-19 20:50:07 +00:00
|
|
|
func TestPrintStmtCase(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Case{
|
2018-03-19 20:50:07 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `case $a:
|
2018-04-03 16:20:55 +00:00
|
|
|
$a;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtCaseEmpty(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Case{
|
2018-04-02 19:50:36 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
})
|
|
|
|
|
|
|
|
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-03-20 10:21:21 +00:00
|
|
|
func TestPrintStmtCatch(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-03-20 10:21:21 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Catch{
|
|
|
|
Types: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Exception"}}},
|
|
|
|
&name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "RuntimeException"}}},
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-03-20 10:21:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
catch (Exception | \RuntimeException $e) {
|
|
|
|
$a;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-20 10:21:21 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 17:48:55 +00:00
|
|
|
func TestPrintStmtClassMethod(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.ClassMethod{
|
2018-03-20 17:48:55 +00:00
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
ReturnsRef: true,
|
|
|
|
MethodName: &node.Identifier{Value: "foo"},
|
|
|
|
Params: []node.Node{
|
|
|
|
&node.Parameter{
|
|
|
|
ByRef: true,
|
|
|
|
VariableType: &node.Nullable{Expr: &name.Name{Parts: []node.Node{&name.NamePart{Value: "int"}}}},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
DefaultValue: &expr.ConstFetch{Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}}},
|
|
|
|
},
|
|
|
|
&node.Parameter{
|
|
|
|
Variadic: true,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReturnType: &name.Name{Parts: []node.Node{&name.NamePart{Value: "void"}}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `public function &foo(?int &$a = null, ...$b): void
|
|
|
|
{
|
2018-04-03 16:20:55 +00:00
|
|
|
$a;
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-20 17:48:55 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:06:56 +00:00
|
|
|
func TestPrintStmtClass(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-03-20 18:06:56 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Class{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
|
|
|
|
ClassName: &node.Identifier{Value: "Foo"},
|
|
|
|
Extends: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
Implements: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}},
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Quuz"}}},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassConstList{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
2018-04-03 16:59:20 +00:00
|
|
|
Expr: &scalar.String{Value: "'bar'"},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-20 18:06:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
abstract class Foo extends Bar implements Baz, Quuz
|
|
|
|
{
|
|
|
|
public const FOO = 'bar';
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-20 18:06:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtAnonymousClass(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-03-20 18:06:56 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Class{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
|
2018-04-29 16:58:49 +00:00
|
|
|
ArgumentList: &node.ArgumentList{
|
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Extends: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
Implements: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}},
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Quuz"}}},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassConstList{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
2018-04-03 16:59:20 +00:00
|
|
|
Expr: &scalar.String{Value: "'bar'"},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-20 18:06:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
abstract class($a, $b) extends Bar implements Baz, Quuz
|
|
|
|
{
|
|
|
|
public const FOO = 'bar';
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-20 18:06:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:37:55 +00:00
|
|
|
func TestPrintStmtClassConstList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.ClassConstList{
|
2018-03-20 18:37:55 +00:00
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
2018-04-03 16:59:20 +00:00
|
|
|
Expr: &scalar.String{Value: "'a'"},
|
2018-03-20 18:37:55 +00:00
|
|
|
},
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "BAR"},
|
2018-04-03 16:59:20 +00:00
|
|
|
Expr: &scalar.String{Value: "'b'"},
|
2018-03-20 18:37:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `public 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-03-20 10:21:21 +00:00
|
|
|
func TestPrintStmtConstant(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Constant{
|
2018-03-20 10:21:21 +00:00
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
2018-04-03 16:59:20 +00:00
|
|
|
Expr: &scalar.String{Value: "'BAR'"},
|
2018-03-20 10:21:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expected := "FOO = 'BAR'"
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:37:55 +00:00
|
|
|
func TestPrintStmtContinue(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Continue{
|
2018-03-20 18:37:55 +00:00
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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-03-28 20:44:02 +00:00
|
|
|
func TestPrintStmtDeclareStmts(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Declare{
|
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
2018-04-03 16:59:20 +00:00
|
|
|
Expr: &scalar.String{Value: "'bar'"},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
2018-03-28 20:44:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `{
|
|
|
|
declare(FOO = 'bar') {
|
|
|
|
;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-28 20:44:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtDeclareExpr(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Declare{
|
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
2018-04-03 16:59:20 +00:00
|
|
|
Expr: &scalar.String{Value: "'bar'"},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-03 16:59:20 +00:00
|
|
|
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "'bar'"}},
|
2018-03-28 20:44:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `{
|
|
|
|
declare(FOO = 'bar')
|
|
|
|
'bar';
|
|
|
|
}`
|
2018-03-28 20:44:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtDeclareNop(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Declare{
|
2018-03-28 20:44:02 +00:00
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
2018-04-03 16:59:20 +00:00
|
|
|
Expr: &scalar.String{Value: "'bar'"},
|
2018-03-28 20:44:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `declare(FOO = 'bar');`
|
2018-03-28 20:44:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-03-28 21:04:09 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtDefalut(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Default{
|
2018-03-28 21:04:09 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `default:
|
2018-04-03 16:20:55 +00:00
|
|
|
$a;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtDefalutEmpty(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Default{
|
2018-04-02 19:50:36 +00:00
|
|
|
Stmts: []node.Node{},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `default:`
|
2018-03-28 21:04:09 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtDo_Expression(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Do{
|
|
|
|
Cond: &scalar.Lnumber{Value: "1"},
|
|
|
|
Stmt: &stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
},
|
2018-03-28 21:04:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
do
|
|
|
|
$a;
|
|
|
|
while (1);
|
|
|
|
}`
|
2018-03-28 21:04:09 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtDo_StmtList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Do{
|
|
|
|
Cond: &scalar.Lnumber{Value: "1"},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-03-28 21:04:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtEcho(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Echo{
|
2018-03-31 11:08:56 +00:00
|
|
|
Exprs: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtElseIfStmts(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.ElseIf{
|
2018-03-31 11:08:56 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `elseif ($a) {
|
2018-04-03 16:20:55 +00:00
|
|
|
;
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtElseIfExpr(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.ElseIf{
|
2018-03-31 11:08:56 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-04-03 16:59:20 +00:00
|
|
|
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "'bar'"}},
|
2018-03-31 11:08:56 +00:00
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `elseif ($a)
|
2018-04-03 16:20:55 +00:00
|
|
|
'bar';`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtElseIfNop(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.ElseIf{
|
2018-03-31 11:08:56 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtElseStmts(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Else{
|
2018-03-31 11:08:56 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `else {
|
2018-04-03 16:20:55 +00:00
|
|
|
;
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtElseExpr(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Else{
|
2018-04-03 16:59:20 +00:00
|
|
|
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "'bar'"}},
|
2018-03-31 11:08:56 +00:00
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `else
|
2018-04-03 16:20:55 +00:00
|
|
|
'bar';`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtElseNop(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Else{
|
2018-03-31 11:08:56 +00:00
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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-03-31 11:17:05 +00:00
|
|
|
func TestPrintExpression(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}})
|
2018-03-31 11:17:05 +00:00
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `$a;`
|
2018-03-31 11:17:05 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtFinally(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-03-19 20:50:07 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Finally{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
finally {
|
|
|
|
;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 11:49:29 +00:00
|
|
|
func TestPrintStmtForStmts(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.For{
|
|
|
|
Init: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
Cond: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "d"}},
|
|
|
|
},
|
|
|
|
Loop: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "e"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "f"}},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
for ($a, $b; $c, $d; $e, $f) {
|
|
|
|
;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtForExpr(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.For{
|
|
|
|
Init: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
Cond: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
Loop: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
2018-04-03 16:59:20 +00:00
|
|
|
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "'bar'"}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
for ($a; $b; $c)
|
|
|
|
'bar';
|
|
|
|
}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtForNop(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.For{
|
2018-03-31 11:49:29 +00:00
|
|
|
Init: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
Cond: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
Loop: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `for ($a; $b; $c);`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtForeachStmts(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Foreach{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
foreach ($a as $b) {
|
|
|
|
;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtForeachExpr(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Foreach{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "v"}},
|
2018-04-03 16:59:20 +00:00
|
|
|
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "'bar'"}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
foreach ($a as $k => $v)
|
|
|
|
'bar';
|
|
|
|
}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtForeachNop(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Foreach{
|
2018-03-31 11:49:29 +00:00
|
|
|
ByRef: true,
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "v"}},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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-03-31 11:49:29 +00:00
|
|
|
func TestPrintStmtFunction(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-03-31 11:49:29 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Function{
|
|
|
|
ReturnsRef: true,
|
|
|
|
FunctionName: &node.Identifier{Value: "foo"},
|
|
|
|
Params: []node.Node{
|
|
|
|
&node.Parameter{
|
|
|
|
ByRef: true,
|
|
|
|
Variadic: false,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReturnType: &name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
function &foo(&$var): \Foo {
|
|
|
|
;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
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
|
|
|
func TestPrintStmtGlobal(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Global{
|
2018-04-01 12:58:45 +00:00
|
|
|
Vars: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtGoto(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Goto{
|
2018-04-01 12:58:45 +00:00
|
|
|
Label: &node.Identifier{Value: "FOO"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtGroupUse(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.GroupUse{
|
2018-04-01 12:58:45 +00:00
|
|
|
UseType: &node.Identifier{Value: "function"},
|
|
|
|
Prefix: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
UseList: []node.Node{
|
|
|
|
&stmt.Use{
|
|
|
|
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
Alias: &node.Identifier{Value: "Baz"},
|
|
|
|
},
|
|
|
|
&stmt.Use{
|
|
|
|
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Quuz"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintHaltCompiler(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.HaltCompiler{})
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2018-04-02 19:50:36 +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-04-01 14:07:07 +00:00
|
|
|
func TestPrintIfExpression(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.If{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
ElseIf: []node.Node{
|
|
|
|
&stmt.ElseIf{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.ElseIf{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Else: &stmt.Else{
|
|
|
|
Stmt: &stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "f"}},
|
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
if ($a)
|
|
|
|
$b;
|
|
|
|
elseif ($c) {
|
|
|
|
$d;
|
|
|
|
}
|
|
|
|
elseif ($e);
|
|
|
|
else
|
|
|
|
$f;
|
|
|
|
}`
|
2018-04-01 14:07:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintIfStmtList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.If{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
if ($a) {
|
|
|
|
$b;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-04-01 14:07:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintIfNop(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.If{
|
2018-04-01 14:07:07 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `if ($a);`
|
2018-04-01 14:07:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintInlineHtml(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.InlineHtml{
|
2018-04-01 14:07:07 +00:00
|
|
|
Value: "test",
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `?>test<?php`
|
2018-04-01 14:07:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 21:28:01 +00:00
|
|
|
func TestPrintInterface(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-04-01 21:28:01 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Interface{
|
|
|
|
InterfaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Extends: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}},
|
|
|
|
},
|
2018-04-01 21:28:01 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.ClassMethod{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
MethodName: &node.Identifier{Value: "foo"},
|
|
|
|
Params: []node.Node{},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:28:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
interface Foo extends Bar, Baz
|
|
|
|
{
|
|
|
|
public function foo()
|
|
|
|
{
|
|
|
|
$a;
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintLabel(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Label{
|
2018-04-01 21:28:01 +00:00
|
|
|
LabelName: &node.Identifier{Value: "FOO"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintNamespace(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Namespace{
|
2018-04-01 21:28:01 +00:00
|
|
|
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintNamespaceWithStmts(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.StmtList{
|
2018-04-01 21:28:01 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Namespace{
|
|
|
|
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:28:01 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `{
|
|
|
|
namespace Foo {
|
|
|
|
$a;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 20:44:02 +00:00
|
|
|
func TestPrintNop(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Nop{})
|
2018-03-28 20:44:02 +00:00
|
|
|
|
2018-04-02 19:50:36 +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-04-01 21:43:27 +00:00
|
|
|
func TestPrintPropertyList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.PropertyList{
|
2018-04-01 21:43:27 +00:00
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "public"},
|
|
|
|
&node.Identifier{Value: "static"},
|
|
|
|
},
|
|
|
|
Properties: []node.Node{
|
|
|
|
&stmt.Property{
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&stmt.Property{
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `public static $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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintProperty(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Property{
|
2018-04-01 21:43:27 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintReturn(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Return{
|
2018-04-01 21:43:27 +00:00
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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-04-01 21:59:31 +00:00
|
|
|
func TestPrintStaticVar(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.StaticVar{
|
2018-04-01 21:59:31 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStatic(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Static{
|
2018-04-01 21:59:31 +00:00
|
|
|
Vars: []node.Node{
|
|
|
|
&stmt.StaticVar{
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&stmt.StaticVar{
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.StmtList{
|
2018-04-01 21:59:31 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:59:55 +00:00
|
|
|
expected := `{
|
2018-04-03 16:20:55 +00:00
|
|
|
$a;
|
|
|
|
$b;
|
2018-04-02 19:59:55 +00:00
|
|
|
}`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
func TestPrintStmtListNested(t *testing.T) {
|
2018-04-01 21:59:31 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
&stmt.StmtList{
|
2018-04-01 21:59:31 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "c"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `{
|
|
|
|
$a;
|
|
|
|
{
|
|
|
|
$b;
|
|
|
|
{
|
|
|
|
$c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtSwitch(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Switch{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
2018-04-03 16:59:20 +00:00
|
|
|
Cond: &scalar.String{Value: "'a'"},
|
2018-04-03 16:20:55 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&stmt.Case{
|
2018-04-03 16:59:20 +00:00
|
|
|
Cond: &scalar.String{Value: "'b'"},
|
2018-04-03 16:20:55 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `{
|
|
|
|
switch ($var) {
|
|
|
|
case 'a':
|
|
|
|
$a;
|
|
|
|
case 'b':
|
|
|
|
$b;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 22:29:24 +00:00
|
|
|
func TestPrintStmtThrow(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Throw{
|
2018-04-01 22:29:24 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtTraitMethodRef(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.TraitMethodRef{
|
2018-04-01 22:29:24 +00:00
|
|
|
Trait: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Method: &node.Identifier{Value: "a"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtTraitUseAlias(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.TraitUseAlias{
|
2018-04-01 22:29:24 +00:00
|
|
|
Ref: &stmt.TraitMethodRef{
|
|
|
|
Trait: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Method: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Modifier: &node.Identifier{Value: "public"},
|
|
|
|
Alias: &node.Identifier{Value: "b"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtTraitUsePrecedence(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.TraitUsePrecedence{
|
2018-04-01 22:29:24 +00:00
|
|
|
Ref: &stmt.TraitMethodRef{
|
|
|
|
Trait: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Method: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Insteadof: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtTraitUse(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.TraitUse{
|
2018-04-01 22:29:24 +00:00
|
|
|
Traits: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtTraitAdaptations(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.TraitUse{
|
|
|
|
Traits: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
},
|
2018-04-29 19:34:24 +00:00
|
|
|
TraitAdaptationList: &stmt.TraitAdaptationList{
|
|
|
|
Adaptations: []node.Node{
|
|
|
|
&stmt.TraitUseAlias{
|
|
|
|
Ref: &stmt.TraitMethodRef{
|
|
|
|
Trait: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Method: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Alias: &node.Identifier{Value: "b"},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-01 22:29:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
use Foo, Bar {
|
|
|
|
Foo::a as b;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-04-01 22:29:24 +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
|
|
|
func TestPrintTrait(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-04-02 09:41:47 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Trait{
|
|
|
|
TraitName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
2018-04-02 09:41:47 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.ClassMethod{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
MethodName: &node.Identifier{Value: "foo"},
|
|
|
|
Params: []node.Node{},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
trait Foo
|
|
|
|
{
|
|
|
|
public function foo()
|
|
|
|
{
|
|
|
|
$a;
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtTry(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-04-02 09:41:47 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Try{
|
2018-04-02 09:41:47 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
Catches: []node.Node{
|
|
|
|
&stmt.Catch{
|
|
|
|
Types: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Exception"}}},
|
|
|
|
&name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "RuntimeException"}}},
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Finally: &stmt.Finally{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
try {
|
|
|
|
$a;
|
|
|
|
}
|
|
|
|
catch (Exception | \RuntimeException $e) {
|
|
|
|
$b;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtUset(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Unset{
|
2018-04-02 09:41:47 +00:00
|
|
|
Vars: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintStmtUseList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.UseList{
|
2018-04-02 09:41:47 +00:00
|
|
|
UseType: &node.Identifier{Value: "function"},
|
|
|
|
Uses: []node.Node{
|
|
|
|
&stmt.Use{
|
|
|
|
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Alias: &node.Identifier{Value: "Bar"},
|
|
|
|
},
|
|
|
|
&stmt.Use{
|
|
|
|
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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-04-01 12:58:45 +00:00
|
|
|
func TestPrintUse(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.Use{
|
2018-04-01 12:58:45 +00:00
|
|
|
UseType: &node.Identifier{Value: "function"},
|
|
|
|
Use: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Alias: &node.Identifier{Value: "Bar"},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +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
|
|
|
|
|
|
|
func TestPrintWhileStmtList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.While{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
while ($a) {
|
|
|
|
$a;
|
|
|
|
}
|
2018-04-02 19:50:36 +00:00
|
|
|
}`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintWhileExpression(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.While{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
})
|
|
|
|
|
2018-04-03 16:20:55 +00:00
|
|
|
expected := `namespace {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintWhileNop(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-04-02 20:57:22 +00:00
|
|
|
p := printer.NewPrinter(o, " ")
|
|
|
|
p.Print(&stmt.While{
|
2018-04-02 09:41:47 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-04-02 19:50:36 +00:00
|
|
|
expected := `while ($a);`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|