2018-02-10 09:27:20 +00:00
|
|
|
package expr_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
"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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
EndPos: 15,
|
|
|
|
},
|
2018-02-10 09:27:20 +00:00
|
|
|
ReturnsRef: false,
|
|
|
|
Static: false,
|
|
|
|
PhpDocComment: "",
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-10 09:27:20 +00:00
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php5parser.Parse()
|
|
|
|
actual = php5parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 13,
|
|
|
|
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,
|
|
|
|
StartPos: 13,
|
|
|
|
EndPos: 14,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 13,
|
|
|
|
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,
|
|
|
|
StartPos: 17,
|
|
|
|
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,
|
|
|
|
StartPos: 17,
|
|
|
|
EndPos: 18,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 17,
|
|
|
|
EndPos: 18,
|
|
|
|
},
|
|
|
|
Value: "b",
|
|
|
|
},
|
|
|
|
},
|
2018-02-10 09:27:20 +00:00
|
|
|
},
|
|
|
|
},
|
2018-05-25 06:38:44 +00:00
|
|
|
ClosureUse: &expr.ClosureUse{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 21,
|
|
|
|
EndPos: 33,
|
|
|
|
},
|
2018-05-25 06:38:44 +00:00
|
|
|
Uses: []node.Node{
|
2018-06-24 07:19:44 +00:00
|
|
|
&expr.Variable{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 26,
|
|
|
|
EndPos: 27,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 26,
|
|
|
|
EndPos: 27,
|
|
|
|
},
|
|
|
|
Value: "c",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&expr.Reference{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 30,
|
|
|
|
EndPos: 32,
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 31,
|
|
|
|
EndPos: 32,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 31,
|
|
|
|
EndPos: 32,
|
|
|
|
},
|
|
|
|
Value: "d",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-02-10 09:27:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-10 09:27:20 +00:00
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php5parser.Parse()
|
|
|
|
actual = php5parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 13,
|
|
|
|
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,
|
|
|
|
StartPos: 13,
|
|
|
|
EndPos: 14,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 13,
|
|
|
|
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,
|
|
|
|
StartPos: 17,
|
|
|
|
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,
|
|
|
|
StartPos: 17,
|
|
|
|
EndPos: 18,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 17,
|
|
|
|
EndPos: 18,
|
|
|
|
},
|
|
|
|
Value: "b",
|
|
|
|
},
|
|
|
|
},
|
2018-02-13 15:42:00 +00:00
|
|
|
},
|
|
|
|
},
|
2018-05-25 06:38:44 +00:00
|
|
|
ClosureUse: &expr.ClosureUse{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 21,
|
|
|
|
EndPos: 33,
|
|
|
|
},
|
2018-05-25 06:38:44 +00:00
|
|
|
Uses: []node.Node{
|
2018-06-24 07:19:44 +00:00
|
|
|
&expr.Reference{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 26,
|
|
|
|
EndPos: 28,
|
|
|
|
},
|
|
|
|
Variable: &expr.Variable{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 27,
|
|
|
|
EndPos: 28,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 27,
|
|
|
|
EndPos: 28,
|
|
|
|
},
|
|
|
|
Value: "c",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 31,
|
|
|
|
EndPos: 32,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 31,
|
|
|
|
EndPos: 32,
|
|
|
|
},
|
|
|
|
Value: "d",
|
|
|
|
},
|
|
|
|
},
|
2018-02-13 15:42:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-13 15:42:00 +00:00
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php5parser.Parse()
|
|
|
|
actual = php5parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 4,
|
|
|
|
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,
|
|
|
|
StartPos: 16,
|
|
|
|
EndPos: 19,
|
|
|
|
},
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 16,
|
|
|
|
EndPos: 19,
|
|
|
|
},
|
|
|
|
Value: "void",
|
|
|
|
},
|
|
|
|
},
|
2018-02-10 09:31:57 +00:00
|
|
|
},
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-10 09:31:57 +00:00
|
|
|
}
|