2018-03-18 20:27:34 +00:00
|
|
|
package printer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
"github.com/z7zmey/php-parser/freefloating"
|
2018-03-18 20:27:34 +00:00
|
|
|
"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-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintFile(t *testing.T) {
|
2018-04-05 21:59:22 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-05-02 09:14:24 +00:00
|
|
|
p.Print(&node.Root{
|
2018-04-05 21:59:22 +00:00
|
|
|
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"},
|
|
|
|
},
|
|
|
|
},
|
2018-05-12 20:10:01 +00:00
|
|
|
Extends: &stmt.ClassExtends{
|
|
|
|
ClassName: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "Baz"},
|
|
|
|
},
|
2018-04-05 22:12:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassMethod{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
MethodName: &node.Identifier{Value: "greet"},
|
2018-06-03 06:35:44 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Echo{
|
|
|
|
Exprs: []node.Node{
|
|
|
|
&scalar.String{Value: "'Hello world'"},
|
|
|
|
},
|
2018-04-05 22:12:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-04-05 21:59:22 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `<?php namespace Foo;abstract class Bar extends Baz{public function greet(){echo 'Hello world';}}`
|
2018-04-05 21:59:22 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintFileInlineHtml(t *testing.T) {
|
2018-04-05 21:59:22 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-05-02 09:14:24 +00:00
|
|
|
p.Print(&node.Root{
|
2018-04-05 21:59:22 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.InlineHtml{Value: "<div>HTML</div>"},
|
|
|
|
&stmt.Expression{
|
2018-12-17 13:24:13 +00:00
|
|
|
Expr: &expr.Variable{
|
2019-02-13 20:18:07 +00:00
|
|
|
FreeFloating: freefloating.Collection{
|
|
|
|
freefloating.Start: []freefloating.String{
|
|
|
|
{
|
|
|
|
StringType: freefloating.TokenType,
|
|
|
|
Value: "$",
|
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
2018-07-29 08:44:38 +00:00
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
VarName: &node.Identifier{
|
|
|
|
Value: "a",
|
2018-07-29 08:44:38 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
|
|
|
&stmt.InlineHtml{Value: "<div>HTML</div>"},
|
|
|
|
&stmt.Expression{
|
2018-07-02 17:48:55 +00:00
|
|
|
Expr: &expr.Variable{
|
2019-02-13 20:18:07 +00:00
|
|
|
FreeFloating: freefloating.Collection{
|
|
|
|
freefloating.Start: []freefloating.String{
|
|
|
|
{
|
|
|
|
StringType: freefloating.TokenType,
|
|
|
|
Value: "$",
|
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
VarName: &node.Identifier{
|
|
|
|
Value: "a",
|
2018-04-05 21:59:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `<div>HTML</div><?php $a;?><div>HTML</div><?php $a;`
|
2018-04-05 21:59:22 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:27:34 +00:00
|
|
|
// node
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintIdentifier(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
n := &node.Identifier{
|
|
|
|
Value: "test",
|
|
|
|
}
|
|
|
|
p.Print(n)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `test`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintParameter(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&node.Parameter{
|
2018-07-02 17:48:55 +00:00
|
|
|
ByRef: false,
|
|
|
|
Variadic: true,
|
|
|
|
VariableType: &name.FullyQualified{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
DefaultValue: &scalar.String{
|
|
|
|
Value: "'default'",
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "\\Foo...$var='default'"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNullable(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&node.Nullable{
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &node.Parameter{
|
2018-07-02 17:48:55 +00:00
|
|
|
ByRef: true,
|
|
|
|
Variadic: false,
|
|
|
|
VariableType: &name.FullyQualified{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Value: "var",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DefaultValue: &scalar.String{
|
|
|
|
Value: "'default'",
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "?\\Foo&$var='default'"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintArgument(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&node.Argument{
|
2018-03-18 20:27:34 +00:00
|
|
|
IsReference: false,
|
|
|
|
Variadic: true,
|
2018-07-02 17:48:55 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Value: "var",
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "...$var"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintArgumentByRef(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&node.Argument{
|
2018-03-18 20:27:34 +00:00
|
|
|
IsReference: true,
|
|
|
|
Variadic: false,
|
2018-07-02 17:48:55 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Value: "var",
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "&$var"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// name
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNameNamePart(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&name.NamePart{
|
2018-03-18 20:27:34 +00:00
|
|
|
Value: "foo",
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "foo"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNameName(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&name.Name{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "Foo\\Bar"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNameFullyQualified(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&name.FullyQualified{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "\\Foo\\Bar"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNameRelative(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&name.Relative{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "namespace\\Foo\\Bar"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scalar
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarLNumber(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&scalar.Lnumber{
|
|
|
|
Value: "1",
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "1"
|
2018-07-02 17:48:55 +00:00
|
|
|
actual := o.String()
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarDNumber(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&scalar.Dnumber{
|
|
|
|
Value: ".1",
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := ".1"
|
2018-07-02 17:48:55 +00:00
|
|
|
actual := o.String()
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarString(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&scalar.String{
|
|
|
|
Value: "'hello world'",
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `'hello world'`
|
2018-04-03 16:59:20 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarEncapsedStringPart(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&scalar.EncapsedStringPart{
|
|
|
|
Value: "hello world",
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `hello world`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarEncapsed(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&scalar.Encapsed{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello "},
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
&scalar.EncapsedStringPart{Value: " world"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `"hello $var world"`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
2018-03-18 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarHeredoc(t *testing.T) {
|
2018-04-05 21:39:04 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 21:39:04 +00:00
|
|
|
p.Print(&scalar.Heredoc{
|
2019-03-10 21:37:01 +00:00
|
|
|
Label: "<<<LBL\n",
|
2018-04-05 21:39:04 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello "},
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2019-03-10 21:37:01 +00:00
|
|
|
&scalar.EncapsedStringPart{Value: " world\n"},
|
2018-04-05 21:39:04 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `<<<LBL
|
2018-07-29 08:44:38 +00:00
|
|
|
hello $var world
|
2018-04-05 21:39:04 +00:00
|
|
|
LBL`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarNowdoc(t *testing.T) {
|
2018-04-05 21:39:04 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 21:39:04 +00:00
|
|
|
p.Print(&scalar.Heredoc{
|
2019-03-10 21:37:01 +00:00
|
|
|
Label: "<<<'LBL'\n",
|
2018-04-05 21:39:04 +00:00
|
|
|
Parts: []node.Node{
|
2019-03-10 21:37:01 +00:00
|
|
|
&scalar.EncapsedStringPart{Value: "hello world\n"},
|
2018-04-05 21:39:04 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `<<<'LBL'
|
2018-04-05 21:39:04 +00:00
|
|
|
hello world
|
|
|
|
LBL`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintScalarMagicConstant(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&scalar.MagicConstant{
|
|
|
|
Value: "__DIR__",
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
if o.String() != `__DIR__` {
|
2018-03-18 20:27:34 +00:00
|
|
|
t.Errorf("TestPrintScalarMagicConstant is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssign(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.Assign{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintReference(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 09:03:32 +00:00
|
|
|
p.Print(&assign.Reference{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a=&$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignBitwiseAnd(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.BitwiseAnd{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a&=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignBitwiseOr(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.BitwiseOr{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a|=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignBitwiseXor(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.BitwiseXor{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a^=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:26:54 +00:00
|
|
|
func TestPrinterPrintAssignCoalesce(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&assign.Coalesce{
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignConcat(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.Concat{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a.=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignDiv(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.Div{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a/=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignMinus(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.Minus{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a-=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignMod(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.Mod{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a%=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignMul(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.Mul{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a*=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignPlus(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.Plus{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a+=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignPow(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.Pow{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a**=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignShiftLeft(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.ShiftLeft{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<<=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAssignShiftRight(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&assign.ShiftRight{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a>>=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// binary
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBitwiseAnd(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.BitwiseAnd{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a&$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBitwiseOr(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.BitwiseOr{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a|$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBitwiseXor(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.BitwiseXor{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a^$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBooleanAnd(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.BooleanAnd{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a&&$b`
|
2018-10-24 14:04:13 +00:00
|
|
|
actual := o.String()
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryBooleanOr(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.BooleanOr{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a||$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryCoalesce(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Coalesce{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a??$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryConcat(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Concat{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a.$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryDiv(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Div{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a/$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryEqual(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Equal{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a==$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryGreaterOrEqual(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.GreaterOrEqual{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a>=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryGreater(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Greater{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a>$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryIdentical(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Identical{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a===$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryLogicalAnd(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.LogicalAnd{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a and $b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryLogicalOr(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.LogicalOr{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a or $b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryLogicalXor(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.LogicalXor{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a xor $b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryMinus(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Minus{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a-$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryMod(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Mod{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a%$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryMul(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Mul{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a*$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryNotEqual(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.NotEqual{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a!=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryNotIdentical(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.NotIdentical{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a!==$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryPlus(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Plus{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
2018-10-24 14:04:13 +00:00
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a+$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryPow(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Pow{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a**$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryShiftLeft(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.ShiftLeft{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<<$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinaryShiftRight(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.ShiftRight{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a>>$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinarySmallerOrEqual(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.SmallerOrEqual{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<=$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinarySmaller(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Smaller{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBinarySpaceship(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&binary.Spaceship{
|
2018-10-24 14:04:13 +00:00
|
|
|
Left: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Right: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a<=>$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cast
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintArray(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Array{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(array)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintBool(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Bool{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(boolean)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintDouble(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Double{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(float)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintInt(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Int{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(integer)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintObject(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Object{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(object)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintString(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.String{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(string)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintUnset(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-05 08:59:29 +00:00
|
|
|
p.Print(&cast.Unset{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `(unset)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// expr
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprArrayDimFetch(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.ArrayDimFetch{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
Dim: &scalar.Lnumber{Value: "1"},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var[1]`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprArrayItemWithKey(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.ArrayItem{
|
2018-05-14 15:09:11 +00:00
|
|
|
Key: &scalar.String{Value: "'Hello'"},
|
2018-07-02 17:48:55 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "world"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `'Hello'=>$world`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprArrayItem(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.ArrayItem{
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Reference{Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "world"},
|
|
|
|
}},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expected := `&$world`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 20:42:52 +00:00
|
|
|
func TestPrinterPrintExprArrayItemUnpack(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.ArrayItem{
|
|
|
|
Unpack: 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprArray(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.Array{
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
2018-05-14 15:09:11 +00:00
|
|
|
Key: &scalar.String{Value: "'Hello'"},
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "world"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
2018-05-14 15:09:11 +00:00
|
|
|
Key: &scalar.Lnumber{Value: "2"},
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Reference{Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
}},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `array('Hello'=>$world,2=>&$var,$var)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprBitwiseNot(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.BitwiseNot{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `~$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprBooleanNot(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.BooleanNot{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `!$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprClassConstFetch(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.ClassConstFetch{
|
2018-10-24 14:04:13 +00:00
|
|
|
Class: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
ConstantName: &node.Identifier{
|
|
|
|
Value: "CONST",
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var::CONST`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprClone(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.Clone{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `clone $var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprClosureUse(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.ClosureUse{
|
2018-05-25 06:38:44 +00:00
|
|
|
Uses: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Reference{Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "foo"},
|
|
|
|
}},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "bar"},
|
|
|
|
},
|
2018-05-25 06:38:44 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `use(&$foo,$bar)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprClosure(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.Closure{
|
|
|
|
Static: true,
|
|
|
|
ReturnsRef: true,
|
|
|
|
Params: []node.Node{
|
|
|
|
&node.Parameter{
|
|
|
|
ByRef: true,
|
|
|
|
Variadic: false,
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ClosureUse: &expr.ClosureUse{
|
|
|
|
Uses: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Reference{Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ReturnType: &name.FullyQualified{
|
|
|
|
Parts: []node.Node{&name.NamePart{Value: "Foo"}},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `static function&(&$var)use(&$a,$b):\Foo{$a;}`
|
2018-07-02 17:48:55 +00:00
|
|
|
actual := o.String()
|
2018-03-18 20:27:34 +00:00
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:26:54 +00:00
|
|
|
func TestPrinterPrintExprArrowFunction(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Expression{
|
|
|
|
Expr: &expr.ArrowFunction{
|
|
|
|
Static: true,
|
|
|
|
ReturnsRef: true,
|
|
|
|
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"}},
|
|
|
|
},
|
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `static fn&(&$var):\Foo=>$a;`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprConstFetch(t *testing.T) {
|
2018-03-20 17:48:55 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintEmpty(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.Empty{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `empty($var)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrettyPrinterrorSuppress(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.ErrorSuppress{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `@$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintEval(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.Eval{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `eval($var)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExit(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.Exit{
|
2018-07-09 21:51:02 +00:00
|
|
|
Die: false,
|
2018-07-02 17:48:55 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `exit $var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-07-09 21:51:02 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrinterPrintDie(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.Exit{
|
|
|
|
Die: true,
|
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `die $var`
|
2018-07-09 21:51:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-03-18 20:27:34 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintFunctionCall(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.FunctionCall{
|
2018-10-24 14:04:13 +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,
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Variadic: true,
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
|
|
|
&node.Argument{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "c"},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var(&$a,...$b,$c)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintInclude(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.Include{
|
|
|
|
Expr: &scalar.String{Value: "'path'"},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `include 'path'`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintIncludeOnce(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.IncludeOnce{
|
2018-12-17 13:24:13 +00:00
|
|
|
Expr: &scalar.String{Value: "'path'"},
|
2018-07-02 17:48:55 +00:00
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `include_once 'path'`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintInstanceOf(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.InstanceOf{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var instanceof Foo`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintIsset(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.Isset{
|
2018-03-18 20:27:34 +00:00
|
|
|
Variables: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `isset($a,$b)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintList(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.List{
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.List{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "c"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `list($a,list($b,$c))`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintMethodCall(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.MethodCall{
|
2018-10-24 14:04:13 +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{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
|
|
|
&node.Argument{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$foo->bar($a,$b)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNew(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.New{
|
2018-07-02 17:48:55 +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{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
|
|
|
&node.Argument{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `new Foo($a,$b)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPostDec(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.PostDec{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var--`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPostInc(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.PostInc{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$var++`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPreDec(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.PreDec{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `--$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPreInc(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.PreInc{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `++$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPrint(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.Print{
|
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `print $var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPropertyFetch(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.PropertyFetch{
|
|
|
|
Variable: &expr.Variable{
|
2018-10-24 14:04:13 +00:00
|
|
|
VarName: &node.Identifier{Value: "foo"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Property: &node.Identifier{Value: "bar"},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$foo->bar`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprReference(t *testing.T) {
|
2018-05-14 15:09:11 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-05-14 15:09:11 +00:00
|
|
|
p.Print(&expr.Reference{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "foo"},
|
|
|
|
},
|
2018-05-14 15:09:11 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `&$foo`
|
2018-05-14 15:09:11 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintRequire(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.Require{
|
|
|
|
Expr: &scalar.String{Value: "'path'"},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `require 'path'`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintRequireOnce(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.RequireOnce{
|
|
|
|
Expr: &scalar.String{Value: "'path'"},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `require_once 'path'`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintShellExec(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.ShellExec{
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello "},
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "world"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
&scalar.EncapsedStringPart{Value: "!"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "`hello $world!`"
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExprShortArray(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "world"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
2018-05-14 15:09:11 +00:00
|
|
|
Key: &scalar.Lnumber{Value: "2"},
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Reference{Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
}},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `['Hello'=>$world,2=>&$var,$var]`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintShortList(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.ShortList{
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.List{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
2018-10-24 14:04:13 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "c"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `[$a,list($b,$c)]`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStaticCall(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
|
|
|
&node.Argument{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::bar($a,$b)`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStaticPropertyFetch(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.StaticPropertyFetch{
|
2018-10-24 14:04:13 +00:00
|
|
|
Class: &node.Identifier{Value: "Foo"},
|
|
|
|
Property: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "bar"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::$bar`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintTernary(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.Ternary{
|
2018-10-24 14:04:13 +00:00
|
|
|
Condition: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
IfFalse: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a?:$b`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintTernaryFull(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.Ternary{
|
2018-10-24 14:04:13 +00:00
|
|
|
Condition: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
IfTrue: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
|
|
|
IfFalse: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "c"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a?$b:$c`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintUnaryMinus(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.UnaryMinus{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `-$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintUnaryPlus(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.UnaryPlus{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `+$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintVariable(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&expr.Variable{
|
|
|
|
VarName: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintYieldFrom(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.YieldFrom{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `yield from $var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintYield(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.Yield{
|
2018-10-24 14:04:13 +00:00
|
|
|
Value: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `yield $var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintYieldFull(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.Yield{
|
2018-10-24 14:04:13 +00:00
|
|
|
Key: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "k"},
|
|
|
|
},
|
|
|
|
Value: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `yield $k=>$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 20:50:07 +00:00
|
|
|
|
|
|
|
// stmt
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltElseIf(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.AltElseIf{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a):$b;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltElseIfEmpty(t *testing.T) {
|
2018-04-02 19:50:36 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.AltElseIf{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-04-02 19:50:36 +00:00
|
|
|
Stmt: &stmt.StmtList{},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a):`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltElse(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.AltElse{
|
2018-03-19 20:50:07 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `else:$b;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltElseEmpty(t *testing.T) {
|
2018-04-02 19:50:36 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.AltElse{
|
2018-04-02 19:50:36 +00:00
|
|
|
Stmt: &stmt.StmtList{},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `else:`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltFor(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.AltFor{
|
|
|
|
Init: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
Cond: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
Loop: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
2018-12-17 13:24:13 +00:00
|
|
|
VarName: &node.Identifier{Value: "c"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
2018-10-24 14:04:13 +00:00
|
|
|
VarName: &node.Identifier{Value: "d"},
|
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `for($a;$b;$c):$d;endfor;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltForeach(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.AltForeach{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
Key: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "key"},
|
|
|
|
},
|
|
|
|
Variable: &expr.Reference{Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "val"},
|
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "d"},
|
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `foreach($var as $key=>&$val):$d;endforeach;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltIf(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.AltIf{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "d"},
|
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ElseIf: []node.Node{
|
|
|
|
&stmt.AltElseIf{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
&stmt.AltElseIf{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "c"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.StmtList{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Else: &stmt.AltElse{
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `if($a):$d;elseif($b):$b;elseif($c):else:$b;endif;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtAltSwitch(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.AltSwitch{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
CaseList: &stmt.CaseList{
|
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
|
|
|
Cond: &scalar.String{Value: "'a'"},
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&stmt.Case{
|
|
|
|
Cond: &scalar.String{Value: "'b'"},
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `switch($var):case 'a':$a;case 'b':$b;endswitch;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintAltWhile(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.AltWhile{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `while($a):$b;endwhile;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtBreak(t *testing.T) {
|
2018-03-20 10:21:21 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Break{
|
2018-07-02 17:48:55 +00:00
|
|
|
Expr: &scalar.Lnumber{
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-03-20 10:21:21 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "break 1;"
|
2018-03-20 10:21:21 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtCase(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Case{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `case $a:$a;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtCaseEmpty(t *testing.T) {
|
2018-04-02 19:50:36 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Case{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-04-02 19:50:36 +00:00
|
|
|
Stmts: []node.Node{},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "case $a:"
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtCatch(t *testing.T) {
|
2018-03-20 10:21:21 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Catch{
|
|
|
|
Types: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Exception"}}},
|
|
|
|
&name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "RuntimeException"}}},
|
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "e"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-03-20 10:21:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `catch(Exception|\RuntimeException$e){$a;}`
|
2018-03-20 10:21:21 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtClassMethod(t *testing.T) {
|
2018-03-20 17:48:55 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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"}}}},
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-03-20 17:48:55 +00:00
|
|
|
DefaultValue: &expr.ConstFetch{Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}}},
|
|
|
|
},
|
|
|
|
&node.Parameter{
|
|
|
|
Variadic: true,
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-20 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
ReturnType: &name.Name{
|
|
|
|
Parts: []node.Node{&name.NamePart{Value: "void"}},
|
|
|
|
},
|
2018-06-03 06:35:44 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-06-03 06:35:44 +00:00
|
|
|
},
|
2018-03-20 17:48:55 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `public function &foo(?int&$a=null,...$b):void{$a;}`
|
2018-03-20 17:48:55 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
2018-06-30 21:05:59 +00:00
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-07-29 08:44:38 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtAbstractClassMethod(t *testing.T) {
|
2018-06-30 21:05:59 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-06-30 21:05:59 +00:00
|
|
|
p.Print(&stmt.ClassMethod{
|
2018-12-17 13:24:13 +00:00
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "public"},
|
|
|
|
&node.Identifier{Value: "static"},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2018-06-30 21:05:59 +00:00
|
|
|
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"}}}},
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-06-30 21:05:59 +00:00
|
|
|
DefaultValue: &expr.ConstFetch{Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}}},
|
|
|
|
},
|
|
|
|
&node.Parameter{
|
|
|
|
Variadic: true,
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-06-30 21:05:59 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
ReturnType: &name.Name{
|
|
|
|
Parts: []node.Node{&name.NamePart{Value: "void"}},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.Nop{},
|
2018-06-30 21:05:59 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `public static function &foo(?int&$a=null,...$b):void;`
|
2018-06-30 21:05:59 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
2018-03-20 17:48:55 +00:00
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtClass(t *testing.T) {
|
2018-03-20 18:06:56 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Class{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
|
|
|
|
ClassName: &node.Identifier{Value: "Foo"},
|
|
|
|
Extends: &stmt.ClassExtends{
|
|
|
|
ClassName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
},
|
|
|
|
Implements: &stmt.ClassImplements{
|
|
|
|
InterfaceNames: []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{
|
2018-12-17 13:24:13 +00:00
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "public"},
|
|
|
|
&node.Identifier{Value: "static"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
|
|
|
Expr: &scalar.String{Value: "'bar'"},
|
2018-03-20 18:06:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `abstract class Foo extends Bar implements Baz,Quuz{public static const FOO='bar';}`
|
2018-03-20 18:06:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtAnonymousClass(t *testing.T) {
|
2018-03-20 18:06:56 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Class{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
|
|
|
|
ArgumentList: &node.ArgumentList{
|
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
&node.Argument{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Extends: &stmt.ClassExtends{
|
|
|
|
ClassName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
},
|
|
|
|
Implements: &stmt.ClassImplements{
|
|
|
|
InterfaceNames: []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"},
|
|
|
|
Expr: &scalar.String{Value: "'bar'"},
|
2018-03-20 18:06:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `abstract class($a,$b) extends Bar implements Baz,Quuz{public const FOO='bar';}`
|
2018-03-20 18:06:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtClassConstList(t *testing.T) {
|
2018-03-20 18:37:55 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
expected := `public const FOO='a',BAR='b';`
|
2018-07-29 08:44:38 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrinterPrintStmtConstList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.ConstList{
|
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
|
|
|
Expr: &scalar.String{Value: "'a'"},
|
|
|
|
},
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "BAR"},
|
|
|
|
Expr: &scalar.String{Value: "'b'"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `const FOO='a',BAR='b';`
|
2018-03-20 18:37:55 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtConstant(t *testing.T) {
|
2018-03-20 10:21:21 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := "FOO='BAR'"
|
2018-03-20 10:21:21 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtContinue(t *testing.T) {
|
2018-03-20 18:37:55 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Continue{
|
2018-07-02 17:48:55 +00:00
|
|
|
Expr: &scalar.Lnumber{
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-03-20 18:37:55 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `continue 1;`
|
2018-03-20 18:37:55 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtDeclareStmts(t *testing.T) {
|
2018-03-28 20:44:02 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Declare{
|
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
|
|
|
Expr: &scalar.String{Value: "'bar'"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
2018-03-28 20:44:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `declare(FOO='bar'){;}`
|
2018-03-28 20:44:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtDeclareExpr(t *testing.T) {
|
2018-03-28 20:44:02 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Declare{
|
|
|
|
Consts: []node.Node{
|
|
|
|
&stmt.Constant{
|
|
|
|
ConstantName: &node.Identifier{Value: "FOO"},
|
|
|
|
Expr: &scalar.String{Value: "'bar'"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "'bar'"}},
|
2018-03-28 20:44:02 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `declare(FOO='bar')'bar';`
|
2018-03-28 20:44:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtDeclareNop(t *testing.T) {
|
2018-03-28 20:44:02 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
expected := `declare(FOO='bar');`
|
2018-03-28 20:44:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-03-28 21:04:09 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtDefalut(t *testing.T) {
|
2018-03-28 21:04:09 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Default{
|
2018-03-28 21:04:09 +00:00
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-03-28 21:04:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `default:$a;`
|
2018-04-02 19:50:36 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtDefalutEmpty(t *testing.T) {
|
2018-04-02 19:50:36 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Default{
|
2018-04-02 19:50:36 +00:00
|
|
|
Stmts: []node.Node{},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `default:`
|
2018-03-28 21:04:09 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtDo_Expression(t *testing.T) {
|
2018-03-28 21:04:09 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Do{
|
|
|
|
Cond: &scalar.Lnumber{Value: "1"},
|
|
|
|
Stmt: &stmt.Expression{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2018-03-28 21:04:09 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `do $a;while(1);`
|
2018-03-28 21:04:09 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtDo_StmtList(t *testing.T) {
|
2018-03-28 21:04:09 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Do{
|
|
|
|
Cond: &scalar.Lnumber{Value: "1"},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-03-28 21:04:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `do{$a;}while(1);`
|
2018-03-28 21:04:09 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-03-31 11:08:56 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
func TestPrinterPrintStmtEchoHtmlState(t *testing.T) {
|
2018-03-31 11:08:56 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-12-17 13:24:13 +00:00
|
|
|
p.Print(&node.Root{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Echo{
|
|
|
|
Exprs: []node.Node{
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
|
|
|
},
|
2018-07-29 08:44:38 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
})
|
|
|
|
|
2019-03-20 19:21:18 +00:00
|
|
|
expected := `<?php echo $a,$b;`
|
2018-12-17 13:24:13 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrinterPrintStmtEchoPhpState(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Echo{
|
2018-03-31 11:08:56 +00:00
|
|
|
Exprs: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-31 11:08:56 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `echo $a,$b;`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseIfStmts(t *testing.T) {
|
2018-03-31 11:08:56 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.ElseIf{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-03-31 11:08:56 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a){;}`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseIfExpr(t *testing.T) {
|
2018-03-31 11:08:56 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.ElseIf{
|
2018-10-24 14:04:13 +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-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a)'bar';`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseIfNop(t *testing.T) {
|
2018-03-31 11:08:56 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.ElseIf{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-03-31 11:08:56 +00:00
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `elseif($a);`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseStmts(t *testing.T) {
|
2018-03-31 11:08:56 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Else{
|
2018-03-31 11:08:56 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `else{;}`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseExpr(t *testing.T) {
|
2018-03-31 11:08:56 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
expected := `else 'bar';`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtElseNop(t *testing.T) {
|
2018-03-31 11:08:56 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Else{
|
2018-03-31 11:08:56 +00:00
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `else ;`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-03-28 20:44:02 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintExpression(t *testing.T) {
|
2018-03-19 20:50:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Expression{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a;`
|
2018-03-19 20:50:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtFinally(t *testing.T) {
|
2018-03-31 11:49:29 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Finally{
|
2018-04-03 16:20:55 +00:00
|
|
|
Stmts: []node.Node{
|
2018-07-02 17:48:55 +00:00
|
|
|
&stmt.Nop{},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `finally{;}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtFor(t *testing.T) {
|
2018-03-31 11:49:29 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.For{
|
2018-03-31 11:49:29 +00:00
|
|
|
Init: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
Cond: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "c"},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "d"},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
Loop: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "e"},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "f"},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `for($a,$b;$c,$d;$e,$f){;}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtForeach(t *testing.T) {
|
2018-03-31 11:49:29 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Foreach{
|
2018-10-24 14:04:13 +00:00
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `foreach($a as $k=>$v){;}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtFunction(t *testing.T) {
|
2018-03-31 11:49:29 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Function{
|
|
|
|
ReturnsRef: true,
|
|
|
|
FunctionName: &node.Identifier{Value: "foo"},
|
|
|
|
Params: []node.Node{
|
|
|
|
&node.Parameter{
|
|
|
|
ByRef: true,
|
|
|
|
Variadic: false,
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ReturnType: &name.FullyQualified{
|
|
|
|
Parts: []node.Node{&name.NamePart{Value: "Foo"}},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `function &foo(&$var):\Foo{;}`
|
2018-03-31 11:49:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtGlobal(t *testing.T) {
|
2018-04-01 12:58:45 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Global{
|
2018-04-01 12:58:45 +00:00
|
|
|
Vars: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-04-01 12:58:45 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `global$a,$b;`
|
2018-04-01 12:58:45 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtGoto(t *testing.T) {
|
2018-04-01 12:58:45 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Goto{
|
2018-04-01 12:58:45 +00:00
|
|
|
Label: &node.Identifier{Value: "FOO"},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `goto FOO;`
|
2018-04-01 12:58:45 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtGroupUse(t *testing.T) {
|
2018-04-01 12:58:45 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
expected := `use function Foo\{Bar as Baz,Quuz};`
|
2018-04-01 12:58:45 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintHaltCompiler(t *testing.T) {
|
2018-04-01 12:58:45 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-12-17 13:24:13 +00:00
|
|
|
p.Print(&stmt.HaltCompiler{})
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `__halt_compiler();`
|
2018-04-01 12:58:45 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintIfExpression(t *testing.T) {
|
2018-04-01 14:07:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.If{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.Expression{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
},
|
|
|
|
ElseIf: []node.Node{
|
|
|
|
&stmt.ElseIf{
|
|
|
|
Cond: &expr.Variable{
|
2018-10-24 14:04:13 +00:00
|
|
|
VarName: &node.Identifier{Value: "c"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "d"},
|
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
&stmt.ElseIf{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "e"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Else: &stmt.Else{
|
|
|
|
Stmt: &stmt.Expression{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "f"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `if($a)$b;elseif($c){$d;}elseif($e);else $f;`
|
2018-04-01 14:07:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintIfStmtList(t *testing.T) {
|
2018-04-01 14:07:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.If{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `if($a){$b;}`
|
2018-04-01 14:07:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintIfNop(t *testing.T) {
|
2018-04-01 14:07:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.If{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `if($a);`
|
2018-04-01 14:07:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintInlineHtml(t *testing.T) {
|
2018-04-01 14:07:07 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-12-17 13:24:13 +00:00
|
|
|
p.Print(&node.Root{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.InlineHtml{
|
|
|
|
Value: "test",
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `test`
|
2018-04-01 14:07:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintInterface(t *testing.T) {
|
2018-04-01 21:28:01 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Interface{
|
|
|
|
InterfaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Extends: &stmt.InterfaceExtends{
|
|
|
|
InterfaceNames: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
&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: "foo"},
|
|
|
|
Params: []node.Node{},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-04-01 21:28:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `interface Foo extends Bar,Baz{public function foo(){$a;}}`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintLabel(t *testing.T) {
|
2018-04-01 21:28:01 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Label{
|
2018-04-01 21:28:01 +00:00
|
|
|
LabelName: &node.Identifier{Value: "FOO"},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `FOO:`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNamespace(t *testing.T) {
|
2018-04-01 21:28:01 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Namespace{
|
2018-04-01 21:28:01 +00:00
|
|
|
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `namespace Foo;`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNamespaceWithStmts(t *testing.T) {
|
2018-04-01 21:28:01 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Namespace{
|
|
|
|
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-04-01 21:28:01 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `namespace Foo{$a;}`
|
2018-04-01 21:28:01 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintNop(t *testing.T) {
|
2018-03-28 20:44:02 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-12-17 13:24:13 +00:00
|
|
|
p.Print(&stmt.Nop{})
|
2018-03-28 20:44:02 +00:00
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `;`
|
2018-03-28 20:44:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintPropertyList(t *testing.T) {
|
2018-04-01 21:43:27 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.PropertyList{
|
2018-04-01 21:43:27 +00:00
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "public"},
|
|
|
|
&node.Identifier{Value: "static"},
|
|
|
|
},
|
2019-12-29 14:36:56 +00:00
|
|
|
Type: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:43:27 +00:00
|
|
|
Properties: []node.Node{
|
|
|
|
&stmt.Property{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-12-17 13:24:13 +00:00
|
|
|
Expr: &scalar.String{Value: "'a'"},
|
2018-04-01 21:43:27 +00:00
|
|
|
},
|
|
|
|
&stmt.Property{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-04-01 21:43:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-12-29 14:36:56 +00:00
|
|
|
expected := `public static Foo $a='a',$b;`
|
2018-04-01 21:43:27 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintProperty(t *testing.T) {
|
2018-04-01 21:43:27 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Property{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
2019-02-13 20:18:07 +00:00
|
|
|
FreeFloating: freefloating.Collection{
|
|
|
|
freefloating.Start: []freefloating.String{
|
|
|
|
{
|
|
|
|
StringType: freefloating.TokenType,
|
|
|
|
Value: "$",
|
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
2018-04-01 21:43:27 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a=1`
|
2018-04-01 21:43:27 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintReturn(t *testing.T) {
|
2018-04-01 21:43:27 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Return{
|
2018-04-01 21:43:27 +00:00
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `return 1;`
|
2018-04-01 21:43:27 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStaticVar(t *testing.T) {
|
2018-04-01 21:59:31 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.StaticVar{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
2018-04-01 21:59:31 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `$a=1`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStatic(t *testing.T) {
|
2018-04-01 21:59:31 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Static{
|
2018-04-01 21:59:31 +00:00
|
|
|
Vars: []node.Node{
|
|
|
|
&stmt.StaticVar{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
&stmt.StaticVar{
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `static$a,$b;`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtList(t *testing.T) {
|
2018-04-01 21:59:31 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.StmtList{
|
2018-04-01 21:59:31 +00:00
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `{$a;$b;}`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtListNested(t *testing.T) {
|
2018-04-01 21:59:31 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-03 16:20:55 +00:00
|
|
|
p.Print(&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.StmtList{
|
2018-04-01 21:59:31 +00:00
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "c"},
|
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `{$a;{$b;{$c;}}}`
|
2018-04-03 16:20:55 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtSwitch(t *testing.T) {
|
2018-04-03 16:20:55 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Switch{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
CaseList: &stmt.CaseList{
|
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
|
|
|
Cond: &scalar.String{Value: "'a'"},
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&stmt.Case{
|
|
|
|
Cond: &scalar.String{Value: "'b'"},
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `switch($var){case 'a':$a;case 'b':$b;}`
|
2018-04-01 21:59:31 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtThrow(t *testing.T) {
|
2018-04-01 22:29:24 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Throw{
|
2018-10-24 14:04:13 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
2018-04-01 22:29:24 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `throw $var;`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 12:47:29 +00:00
|
|
|
func TestPrinterPrintStmtTraitAdaptationList(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&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-12-17 13:24:13 +00:00
|
|
|
expected := `{Foo::a as b;}`
|
2018-10-29 12:47:29 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitMethodRef(t *testing.T) {
|
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.TraitMethodRef{
|
|
|
|
Method: &node.Identifier{Value: "a"},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := `a`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-07-29 08:44:38 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitMethodRefFull(t *testing.T) {
|
2018-04-01 22:29:24 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::a`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitUseAlias(t *testing.T) {
|
2018-04-01 22:29:24 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::a as public b;`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitUsePrecedence(t *testing.T) {
|
2018-04-01 22:29:24 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
expected := `Foo::a insteadof Bar,Baz;`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitUse(t *testing.T) {
|
2018-04-01 22:29:24 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
TraitAdaptationList: &stmt.Nop{},
|
2018-04-01 22:29:24 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `use Foo,Bar;`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTraitAdaptations(t *testing.T) {
|
2018-04-01 22:29:24 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.TraitUse{
|
|
|
|
Traits: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
},
|
|
|
|
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"},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Alias: &node.Identifier{Value: "b"},
|
2018-04-01 22:29:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `use Foo,Bar{Foo::a as b;}`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintTrait(t *testing.T) {
|
2018-04-02 09:41:47 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Trait{
|
|
|
|
TraitName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
2018-04-02 09:41:47 +00:00
|
|
|
Stmts: []node.Node{
|
2018-07-02 17:48:55 +00:00
|
|
|
&stmt.ClassMethod{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
MethodName: &node.Identifier{Value: "foo"},
|
|
|
|
Params: []node.Node{},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `trait Foo{public function foo(){$a;}}`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtTry(t *testing.T) {
|
2018-04-02 09:41:47 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.Try{
|
2018-04-02 09:41:47 +00:00
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
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"}}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-10-24 14:04:13 +00:00
|
|
|
Variable: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "e"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
}},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Finally: &stmt.Finally{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `try{$a;}catch(Exception|\RuntimeException$e){$b;}finally{;}`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtUnset(t *testing.T) {
|
2018-04-02 09:41:47 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.Unset{
|
2018-04-02 09:41:47 +00:00
|
|
|
Vars: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `unset($a,$b);`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtUseList(t *testing.T) {
|
2018-04-02 09:41:47 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
expected := `use function Foo as Bar,Baz;`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintUse(t *testing.T) {
|
2018-04-01 12:58:45 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
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-12-17 13:24:13 +00:00
|
|
|
expected := `function Foo as Bar`
|
2018-04-01 12:58:45 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 09:41:47 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintWhileStmtList(t *testing.T) {
|
2018-04-02 09:41:47 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
|
|
|
p.Print(&stmt.While{
|
2018-10-24 14:04:13 +00:00
|
|
|
Cond: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-10-24 14:04:13 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "a"},
|
|
|
|
}},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 13:24:13 +00:00
|
|
|
expected := `while($a){$a;}`
|
2018-04-02 09:41:47 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|