2018-03-18 20:27:34 +00:00
|
|
|
package printer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
"github.com/z7zmey/php-parser/meta"
|
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
expected := `<?phpnamespaceFoo;abstractclassBarextendsBaz{publicfunctiongreet(){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-07-02 17:48:55 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Value: "a",
|
2018-04-05 21:59:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := `<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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IdentifierToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "test",
|
|
|
|
}
|
|
|
|
p.Print(n)
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
if o.String() != ` test` {
|
2018-03-18 20:27:34 +00:00
|
|
|
t.Errorf("TestPrintIdentifier is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EllipsisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EqualToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ByRef: false,
|
|
|
|
Variadic: true,
|
|
|
|
VariableType: &name.FullyQualified{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NsSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StringToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
DefaultValue: &scalar.String{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ConstantEncapsedStringToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "'default'",
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.QuestionMarkToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &node.Parameter{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AmpersandToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EqualToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ByRef: true,
|
|
|
|
Variadic: false,
|
|
|
|
VariableType: &name.FullyQualified{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NsSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.VariableToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Value: "var",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DefaultValue: &scalar.String{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ConstantEncapsedStringToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "'default'",
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EllipsisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
IsReference: false,
|
|
|
|
Variadic: true,
|
2018-07-02 17:48:55 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.VariableToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Value: "var",
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AmpersandToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
IsReference: true,
|
|
|
|
Variadic: false,
|
2018-07-02 17:48:55 +00:00
|
|
|
Expr: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.VariableToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Value: "var",
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StringToken,
|
|
|
|
},
|
|
|
|
&meta.Comment{
|
|
|
|
Value: "/*comment*/",
|
|
|
|
TokenName: meta.StringToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StringToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NsSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Value: "foo",
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := " /*comment*/ 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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StringToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.Comment{
|
|
|
|
Value: "/*comment*/",
|
|
|
|
TokenName: meta.StringToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StringToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NsSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := " /*comment*/ 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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NsSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NamespaceToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NsSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
&name.NamePart{
|
|
|
|
Value: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.LnumberToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := " 1"
|
|
|
|
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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DnumberToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: ".1",
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := " .1"
|
|
|
|
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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ConstantEncapsedStringToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "'hello world'",
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EncapsedAndWhitespaceToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "hello world",
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
if o.String() != ` hello world` {
|
2018-03-18 20:27:34 +00:00
|
|
|
t.Errorf("TestPrintScalarEncapsedStringPart is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DoubleQuoteToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello "},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
&scalar.EncapsedStringPart{Value: " world"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
if o.String() != ` "hello $var world"` {
|
2018-03-18 20:27:34 +00:00
|
|
|
t.Errorf("TestPrintScalarEncapsed is failed\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StartHeredocToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-05 21:39:04 +00:00
|
|
|
Label: "LBL",
|
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello "},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
&scalar.EncapsedStringPart{Value: " world\n"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` <<<LBL
|
2018-04-05 21:39:04 +00:00
|
|
|
hello $var 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 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{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StartHeredocToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-05 21:39:04 +00:00
|
|
|
Label: "'LBL'",
|
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello world\n"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.MagicConstantToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "__DIR__",
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EqualToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.VariableToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a = $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EqualToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AmpersandToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.VariableToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AndEqualToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.VariableToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{Value: "b"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expected := `$a &= $b`
|
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OrEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.XorEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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 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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ConcatEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DivEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.MinusEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ModEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.MulEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PlusEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PowEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SlEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SrEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expression: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AmpersandToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.VerticalBarToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CaretToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.BooleanAndToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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 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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.BooleanOrToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CoalesceToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DotToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SlashToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IsEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IsGreaterOrEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.GreaterToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IsIdenticalToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.LogicalAndToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.LogicalOrToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.LogicalXorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.MinusToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PercentToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AsteriskToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IsNotEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IsNotIdenticalToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PlusToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PowToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SlToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SrToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IsSmallerOrEqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.LessToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SpaceshipToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Right: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ArrayCastToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.BoolCastToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` (bool)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintDouble(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DoubleCastToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IntCastToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` (int)$var`
|
2018-03-18 20:27:34 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintObject(t *testing.T) {
|
2018-03-18 20:27:34 +00:00
|
|
|
o := bytes.NewBufferString("")
|
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ObjectCastToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StringCastToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.UnsetCastToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenSquareBracket,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseSquareBracket,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
Dim: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DoubleArrowToken,
|
|
|
|
},
|
|
|
|
},
|
2018-05-14 15:09:11 +00:00
|
|
|
Key: &scalar.String{Value: "'Hello'"},
|
2018-07-02 17:48:55 +00:00
|
|
|
Val: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.VariableToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{Value: "world"},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expected := `'Hello' => $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 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-05-14 15:09:11 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ArrayToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
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'"},
|
|
|
|
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"},
|
|
|
|
Val: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}}},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
2018-05-14 15:09:11 +00:00
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.TildeToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ExclamationMarkToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PaamayimNekudotayimToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Class: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
ConstantName: &node.Identifier{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IdentifierToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "CONST",
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloneToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.UseToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-05-25 06:38:44 +00:00
|
|
|
Uses: []node.Node{
|
|
|
|
&expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "bar"}},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StaticToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.FunctionToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AmpersandToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-04-02 20:57:22 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Static: true,
|
|
|
|
ReturnsRef: true,
|
|
|
|
Params: []node.Node{
|
|
|
|
&node.Parameter{
|
|
|
|
ByRef: true,
|
|
|
|
Variadic: false,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ClosureUse: &expr.ClosureUse{
|
|
|
|
Uses: []node.Node{
|
|
|
|
&expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReturnType: &name.FullyQualified{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Parts: []node.Node{&name.NamePart{Value: "Foo"}},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expected := ` static function & (&$var )use(&$a,$b) :\Foo {$a; }`
|
|
|
|
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 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 TestPrinterPrintDie(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.Die{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ExitToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` die ($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 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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EmptyToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AtToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EvalToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ExitToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` exit ($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 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-03-18 20:27:34 +00:00
|
|
|
Function: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
2018-04-29 16:58:49 +00:00
|
|
|
ArgumentList: &node.ArgumentList{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
IsReference: true,
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Variadic: true,
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IncludeToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &scalar.String{Value: "'path'"},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IncludeOnceToken,
|
|
|
|
},
|
|
|
|
}, Expr: &scalar.String{Value: "'path'"},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.InstanceofToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := `$var instanceofFoo`
|
2018-03-18 20:27:34 +00:00
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IssetToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variables: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ListToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.List{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ObjectOperatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
|
|
|
|
Method: &node.Identifier{Value: "bar"},
|
2018-04-29 16:58:49 +00:00
|
|
|
ArgumentList: &node.ArgumentList{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NewToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Class: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StringToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "Foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
ArgumentList: &node.ArgumentList{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DecToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IncToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DecToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IncToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PrintToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &expr.Variable{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
p := printer.NewPrinter(o)
|
2018-04-02 20:57:22 +00:00
|
|
|
p.Print(&expr.PropertyFetch{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ObjectOperatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
|
|
|
|
Property: &node.Identifier{Value: "bar"},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AmpersandToken,
|
|
|
|
},
|
|
|
|
},
|
2018-05-14 15:09:11 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.RequireToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &scalar.String{Value: "'path'"},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.RequireOnceToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &scalar.String{Value: "'path'"},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.BackquoteToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{Value: "hello "},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "world"}},
|
|
|
|
&scalar.EncapsedStringPart{Value: "!"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenSquareBracket,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseSquareBracket,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
2018-04-03 16:59:20 +00:00
|
|
|
Key: &scalar.String{Value: "'Hello'"},
|
2018-03-18 20:27:34 +00:00
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "world"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
2018-05-14 15:09:11 +00:00
|
|
|
Key: &scalar.Lnumber{Value: "2"},
|
|
|
|
Val: &expr.Reference{Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}}},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenSquareBracket,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseSquareBracket,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.List{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
&expr.ArrayItem{
|
|
|
|
Val: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PaamayimNekudotayimToken,
|
|
|
|
},
|
|
|
|
},
|
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{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&node.Argument{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PaamayimNekudotayimToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Class: &node.Identifier{Value: "Foo"},
|
|
|
|
Property: &expr.Variable{VarName: &node.Identifier{Value: "bar"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.QuestionMarkToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Condition: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-04-03 16:59:20 +00:00
|
|
|
IfFalse: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.QuestionMarkToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Condition: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-04-03 16:59:20 +00:00
|
|
|
IfTrue: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
IfFalse: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
2018-03-18 20:27:34 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.MinusToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PlusToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.VariableToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VarName: &expr.Variable{
|
|
|
|
VarName: &node.Identifier{Value: "var"},
|
|
|
|
},
|
|
|
|
})
|
2018-03-18 20:27:34 +00:00
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.YieldFromToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.YieldToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Value: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.YieldToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DoubleArrowToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-18 20:27:34 +00:00
|
|
|
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
|
|
|
|
Value: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ElseifToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ElseifToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 19:50:36 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ElseToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ElseToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 19:50:36 +00:00
|
|
|
Stmt: &stmt.StmtList{},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ForToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ForInitSemicolonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ForCondSemicolonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EndforToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Init: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
Cond: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
Loop: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ForeachToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AsToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DoubleArrowToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EndforeachToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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"}}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IfToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EndifToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ElseIf: []node.Node{
|
|
|
|
&stmt.AltElseIf{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
2018-03-19 20:50:07 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
2018-07-02 17:48:55 +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{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
Stmt: &stmt.StmtList{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Else: &stmt.AltElse{
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SwitchToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SwitchSemicolonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EndswitchToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
CaseList: &stmt.CaseList{
|
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
|
|
|
Cond: &scalar.String{Value: "'a'"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&stmt.Case{
|
|
|
|
Cond: &scalar.String{Value: "'b'"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&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-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.WhileToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EndwhileToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.BreakToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &scalar.Lnumber{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-03-20 10:21:21 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CaseToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CaseSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CaseToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CaseSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 19:50:36 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CatchToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Types: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Exception"}}},
|
|
|
|
&name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "RuntimeException"}}},
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
2018-03-20 10:21:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.FunctionToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AmpersandToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-20 17:48:55 +00:00
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
ReturnsRef: true,
|
|
|
|
MethodName: &node.Identifier{Value: "foo"},
|
|
|
|
Params: []node.Node{
|
|
|
|
&node.Parameter{
|
|
|
|
ByRef: true,
|
|
|
|
VariableType: &node.Nullable{Expr: &name.Name{Parts: []node.Node{&name.NamePart{Value: "int"}}}},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
DefaultValue: &expr.ConstFetch{Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}}},
|
|
|
|
},
|
|
|
|
&node.Parameter{
|
|
|
|
Variadic: true,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
ReturnType: &name.Name{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Parts: []node.Node{&name.NamePart{Value: "void"}},
|
|
|
|
},
|
2018-06-03 06:35:44 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
2018-06-03 06:35:44 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
2018-03-20 17:48:55 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.FunctionToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AmpersandToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-06-30 21:05:59 +00:00
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
|
|
|
|
ReturnsRef: true,
|
|
|
|
MethodName: &node.Identifier{Value: "foo"},
|
|
|
|
Params: []node.Node{
|
|
|
|
&node.Parameter{
|
|
|
|
ByRef: true,
|
|
|
|
VariableType: &node.Nullable{Expr: &name.Name{Parts: []node.Node{&name.NamePart{Value: "int"}}}},
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
DefaultValue: &expr.ConstFetch{Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}}},
|
|
|
|
},
|
|
|
|
&node.Parameter{
|
|
|
|
Variadic: true,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
ReturnType: &name.Name{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Parts: []node.Node{&name.NamePart{Value: "void"}},
|
|
|
|
},
|
|
|
|
Stmt: &stmt.Nop{},
|
2018-06-30 21:05:59 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := `public 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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ClassToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
|
|
|
|
ClassName: &node.Identifier{Value: "Foo"},
|
|
|
|
Extends: &stmt.ClassExtends{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ExtendsToken,
|
2018-05-12 20:10:01 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
ClassName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
},
|
|
|
|
Implements: &stmt.ClassImplements{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ImplementsToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
expected := `abstract classFoo extendsBar implementsBaz,Quuz {publicconstFOO='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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ClassToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
|
|
|
|
ArgumentList: &node.ArgumentList{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
2018-05-12 20:10:01 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{
|
|
|
|
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{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Extends: &stmt.ClassExtends{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ExtendsToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ClassName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
},
|
|
|
|
Implements: &stmt.ClassImplements{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ImplementsToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
expected := `abstract class ($a,$b ) extendsBar implementsBaz,Quuz {publicconstFOO='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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ConstToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +00:00
|
|
|
expected := `public constFOO='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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EqualToken,
|
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +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
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ContinueToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expr: &scalar.Lnumber{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-03-20 18:37:55 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` continue (1 ) ;`
|
2018-03-20 18:37:55 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DeclareToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DeclareToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
2018-03-28 20:44:02 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
expected := ` declare (FOO='bar' )'bar';`
|
2018-03-28 20:44:02 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DefaultToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CaseSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-28 21:04:09 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DefaultToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CaseSeparatorToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 19:50:36 +00:00
|
|
|
Stmts: []node.Node{},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DoToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.WhileToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-03-28 21:04:09 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Cond: &scalar.Lnumber{Value: "1"},
|
|
|
|
Stmt: &stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
2018-03-28 21:04:09 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DoToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.WhileToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{Value: "1"},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
2018-03-28 21:04:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
func TestPrinterPrintStmtEcho(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.Echo{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EchoToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:08:56 +00:00
|
|
|
Exprs: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ElseifToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:08:56 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ElseifToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:08:56 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-04-03 16:59:20 +00:00
|
|
|
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "'bar'"}},
|
2018-03-31 11:08:56 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-03-31 11:08:56 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ElseToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:08:56 +00:00
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ElseToken,
|
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +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-04-02 19:50:36 +00:00
|
|
|
expected := `else;`
|
2018-03-31 11:08:56 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
2018-03-28 20:44:02 +00:00
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-03-19 20:50:07 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-03-19 20:50:07 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.FinallyToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ForToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ForInitSemicolonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ForCondSemicolonToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
Init: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-07-02 17:48:55 +00:00
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
Cond: []node.Node{
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +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-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ForeachToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AsToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.DoubleArrowToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Key: &expr.Variable{VarName: &node.Identifier{Value: "k"}},
|
2018-07-02 17:48:55 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "v"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
2018-03-31 11:49:29 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.FunctionToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AmpersandToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReturnsRef: true,
|
|
|
|
FunctionName: &node.Identifier{Value: "foo"},
|
|
|
|
Params: []node.Node{
|
|
|
|
&node.Parameter{
|
|
|
|
ByRef: true,
|
|
|
|
Variadic: false,
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReturnType: &name.FullyQualified{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Parts: []node.Node{&name.NamePart{Value: "Foo"}},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Nop{},
|
2018-03-31 11:49:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.GlobalToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 12:58:45 +00:00
|
|
|
Vars: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.GotoToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 12:58:45 +00:00
|
|
|
Label: &node.Identifier{Value: "FOO"},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` gotoFOO ;`
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.UseToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NsSeparatorToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +00:00
|
|
|
expected := ` usefunctionFoo \ {BarasBaz,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)
|
|
|
|
p.Print(&stmt.HaltCompiler{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.HaltCompilerToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2018-04-01 12:58:45 +00:00
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IfToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
ElseIf: []node.Node{
|
|
|
|
&stmt.ElseIf{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "d"}},
|
2018-04-01 14:07:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
&stmt.ElseIf{
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Else: &stmt.Else{
|
|
|
|
Stmt: &stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "f"}},
|
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.IfToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{
|
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
2018-04-01 14:07:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-04-01 14:07:07 +00:00
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Stmt: &stmt.Nop{},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-04-02 20:57:22 +00:00
|
|
|
p.Print(&stmt.InlineHtml{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.InlineHTMLToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 14:07:07 +00:00
|
|
|
Value: "test",
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` ?>test<?php`
|
2018-04-01 14:07:07 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.InterfaceToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InterfaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Extends: &stmt.InterfaceExtends{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ExtendsToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
},
|
|
|
|
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{
|
|
|
|
&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-07-02 17:48:55 +00:00
|
|
|
expected := ` interfaceFoo extendsBar,Baz {publicfunctionfoo(){$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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:28:01 +00:00
|
|
|
LabelName: &node.Identifier{Value: "FOO"},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NamespaceToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:28:01 +00:00
|
|
|
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` namespaceFoo ;`
|
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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.NamespaceToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
2018-04-01 21:28:01 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` namespaceFoo {$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)
|
|
|
|
p.Print(&stmt.Nop{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2018-03-28 20:44:02 +00:00
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:43:27 +00:00
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "public"},
|
|
|
|
&node.Identifier{Value: "static"},
|
|
|
|
},
|
|
|
|
Properties: []node.Node{
|
|
|
|
&stmt.Property{
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&stmt.Property{
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := `publicstatic$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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:43:27 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ReturnToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:43:27 +00:00
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` return1 ;`
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.EqualToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Expr: &scalar.Lnumber{Value: "1"},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.StaticToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
Vars: []node.Node{
|
|
|
|
&stmt.StaticVar{
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
&stmt.StaticVar{
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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{
|
2018-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-03 16:20:55 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
&stmt.StmtList{
|
2018-04-01 21:59:31 +00:00
|
|
|
Stmts: []node.Node{
|
2018-04-03 16:20:55 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
|
|
|
|
&stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "c"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 21:59:31 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` {$a;{$b;{$c;}} }`
|
2018-04-03 16:20:55 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SwitchToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
CaseList: &stmt.CaseList{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
|
|
|
Cond: &scalar.String{Value: "'a'"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&stmt.Case{
|
|
|
|
Cond: &scalar.String{Value: "'b'"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&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-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.ThrowToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 22:29:24 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` throw$var ;`
|
2018-04-01 22:29:24 +00:00
|
|
|
actual := o.String()
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
func 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.PaamayimNekudotayimToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 22:29:24 +00:00
|
|
|
Trait: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
Method: &node.Identifier{Value: "a"},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AsToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +00:00
|
|
|
expected := `Foo::a aspublicb ;`
|
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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.InsteadofToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +00:00
|
|
|
expected := `Foo::a insteadofBar,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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.UseToken,
|
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +00:00
|
|
|
TraitAdaptationList: &stmt.Nop{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-04-01 22:29:24 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +00:00
|
|
|
expected := ` useFoo,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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.UseToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Traits: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
|
|
|
|
},
|
|
|
|
TraitAdaptationList: &stmt.TraitAdaptationList{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-07-02 17:48:55 +00:00
|
|
|
expected := ` useFoo,Bar {Foo::aasb; }`
|
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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.TraitToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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{
|
|
|
|
&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-07-02 17:48:55 +00:00
|
|
|
expected := ` traitFoo {publicfunctionfoo(){$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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.TryToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenCurlyBracesToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseCurlyBracesToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
Stmts: []node.Node{
|
2018-07-02 17:48:55 +00:00
|
|
|
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
Catches: []node.Node{
|
|
|
|
&stmt.Catch{
|
|
|
|
Types: []node.Node{
|
|
|
|
&name.Name{Parts: []node.Node{&name.NamePart{Value: "Exception"}}},
|
|
|
|
&name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "RuntimeException"}}},
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&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-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.UnsetToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
Vars: []node.Node{
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.UseToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.SemiColonToken,
|
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +00:00
|
|
|
expected := ` usefunctionFooasBar,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-07-02 17:48:55 +00:00
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.AsToken,
|
|
|
|
},
|
|
|
|
},
|
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-07-02 17:48:55 +00:00
|
|
|
expected := `functionFoo asBar`
|
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{
|
|
|
|
Meta: []meta.Meta{
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.WhileToken,
|
2018-04-02 09:41:47 +00:00
|
|
|
},
|
2018-07-02 17:48:55 +00:00
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.OpenParenthesisToken,
|
|
|
|
},
|
|
|
|
&meta.WhiteSpace{
|
|
|
|
Value: " ",
|
|
|
|
TokenName: meta.CloseParenthesisToken,
|
2018-04-03 16:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-02 09:41:47 +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{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
|
|
|
|
},
|
|
|
|
},
|
2018-04-02 09:41:47 +00:00
|
|
|
})
|
|
|
|
|
2018-07-02 17:48:55 +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)
|
|
|
|
}
|
|
|
|
}
|