php-parser/node/expr/t_closure_test.go

428 lines
9.0 KiB
Go
Raw Normal View History

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