php-parser/node/expr/t_closure_test.go
2019-03-20 21:06:56 +02:00

428 lines
9.0 KiB
Go

package expr_test
import (
"testing"
"gotest.tools/assert"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7"
)
func TestClosure(t *testing.T) {
src := `<? function(){};`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 16,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 16,
},
Expr: &expr.Closure{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 15,
},
ReturnsRef: false,
Static: false,
PhpDocComment: "",
Stmts: []node.Node{},
},
},
},
}
php7parser := php7.NewParser([]byte(src))
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
php5parser := php5.NewParser([]byte(src))
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestClosureUse(t *testing.T) {
src := `<? function($a, $b) use ($c, &$d) {};`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 37,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 37,
},
Expr: &expr.Closure{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 36,
},
ReturnsRef: false,
Static: false,
PhpDocComment: "",
Params: []node.Node{
&node.Parameter{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
Variadic: false,
ByRef: false,
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
Value: "a",
},
},
},
&node.Parameter{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
ByRef: false,
Variadic: false,
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
Value: "b",
},
},
},
},
ClosureUse: &expr.ClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 33,
},
Uses: []node.Node{
&expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
Value: "c",
},
},
&expr.Reference{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 29,
EndPos: 32,
},
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
Value: "d",
},
},
},
},
},
Stmts: []node.Node{},
},
},
},
}
php7parser := php7.NewParser([]byte(src))
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
php5parser := php5.NewParser([]byte(src))
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestClosureUse2(t *testing.T) {
src := `<? function($a, $b) use (&$c, $d) {};`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 37,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 37,
},
Expr: &expr.Closure{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 36,
},
ReturnsRef: false,
Static: false,
PhpDocComment: "",
Params: []node.Node{
&node.Parameter{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
ByRef: false,
Variadic: false,
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
Value: "a",
},
},
},
&node.Parameter{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
ByRef: false,
Variadic: false,
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
Value: "b",
},
},
},
},
ClosureUse: &expr.ClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 33,
},
Uses: []node.Node{
&expr.Reference{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 28,
},
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 26,
EndPos: 28,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 26,
EndPos: 28,
},
Value: "c",
},
},
},
&expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
Value: "d",
},
},
},
},
Stmts: []node.Node{},
},
},
},
}
php7parser := php7.NewParser([]byte(src))
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
php5parser := php5.NewParser([]byte(src))
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestClosureReturnType(t *testing.T) {
src := `<? function(): void {};`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 23,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 23,
},
Expr: &expr.Closure{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 22,
},
PhpDocComment: "",
ReturnsRef: false,
Static: false,
ReturnType: &name.Name{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 19,
},
Parts: []node.Node{
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 19,
},
Value: "void",
},
},
},
Stmts: []node.Node{},
},
},
},
}
php7parser := php7.NewParser([]byte(src))
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}