php-parser/node/stmt/t_property_list_test.go

488 lines
10 KiB
Go
Raw Normal View History

2018-02-09 11:55:02 +00:00
package stmt_test
import (
2018-06-24 07:19:44 +00:00
"testing"
"gotest.tools/assert"
"github.com/z7zmey/php-parser/node/expr"
2019-12-29 14:36:56 +00:00
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/scalar"
2018-06-24 07:19:44 +00:00
"github.com/z7zmey/php-parser/position"
2018-02-09 11:55:02 +00:00
"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 TestProperty(t *testing.T) {
src := `<? class foo {var $a;}`
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: 22,
},
2018-02-09 11:55:02 +00:00
Stmts: []node.Node{
&stmt.Class{
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: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 9,
2018-06-24 07:19:44 +00:00
EndPos: 12,
},
Value: "foo",
},
2018-02-09 11:55:02 +00:00
Stmts: []node.Node{
&stmt.PropertyList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 21,
},
2018-02-09 11:55:02 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 17,
},
Value: "var",
},
2018-02-09 11:55:02 +00:00
},
Properties: []node.Node{
&stmt.Property{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 18,
2018-06-24 07:19:44 +00:00
EndPos: 20,
},
2018-02-09 11:55:02 +00:00
PhpDocComment: "",
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: 18,
2018-06-24 07:19:44 +00:00
EndPos: 20,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 18,
2018-06-24 07:19:44 +00:00
EndPos: 20,
},
Value: "a",
},
},
2018-02-09 11:55:02 +00:00
},
},
},
},
},
},
}
2019-12-26 15:57:56 +00:00
php7parser := php7.NewParser([]byte(src), "7.4")
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-09 11:55:02 +00:00
2019-12-26 15:57:56 +00:00
php5parser := php5.NewParser([]byte(src), "5.6")
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-09 11:55:02 +00:00
}
func TestProperties(t *testing.T) {
src := `<? class foo {public static $a, $b = 1;}`
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: 40,
},
2018-02-09 11:55:02 +00:00
Stmts: []node.Node{
&stmt.Class{
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: 40,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 9,
2018-06-24 07:19:44 +00:00
EndPos: 12,
},
Value: "foo",
},
2018-02-09 11:55:02 +00:00
Stmts: []node.Node{
&stmt.PropertyList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 39,
},
2018-02-09 11:55:02 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 20,
},
Value: "public",
},
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 21,
2018-06-24 07:19:44 +00:00
EndPos: 27,
},
Value: "static",
},
2018-02-09 11:55:02 +00:00
},
Properties: []node.Node{
&stmt.Property{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 28,
2018-06-24 07:19:44 +00:00
EndPos: 30,
},
2018-02-09 11:55:02 +00:00
PhpDocComment: "",
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: 28,
2018-06-24 07:19:44 +00:00
EndPos: 30,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 28,
2018-06-24 07:19:44 +00:00
EndPos: 30,
},
Value: "a",
},
},
2018-02-09 11:55:02 +00:00
},
&stmt.Property{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 32,
2018-06-24 07:19:44 +00:00
EndPos: 38,
},
2018-02-09 11:55:02 +00:00
PhpDocComment: "",
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: 32,
2018-06-24 07:19:44 +00:00
EndPos: 34,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 32,
2018-06-24 07:19:44 +00:00
EndPos: 34,
},
Value: "b",
},
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 37,
2018-06-24 07:19:44 +00:00
EndPos: 38,
},
Value: "1",
},
2018-02-09 11:55:02 +00:00
},
},
},
},
},
},
}
2019-12-26 15:57:56 +00:00
php7parser := php7.NewParser([]byte(src), "7.4")
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-09 11:55:02 +00:00
2019-12-26 15:57:56 +00:00
php5parser := php5.NewParser([]byte(src), "5.6")
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-09 11:55:02 +00:00
}
2018-02-13 10:33:21 +00:00
func TestProperties2(t *testing.T) {
src := `<? class foo {public static $a = 1, $b;}`
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: 40,
},
2018-02-13 10:33:21 +00:00
Stmts: []node.Node{
&stmt.Class{
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: 40,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 9,
2018-06-24 07:19:44 +00:00
EndPos: 12,
},
Value: "foo",
},
2018-02-13 10:33:21 +00:00
Stmts: []node.Node{
&stmt.PropertyList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 39,
},
2018-02-13 10:33:21 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 20,
},
Value: "public",
},
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 21,
2018-06-24 07:19:44 +00:00
EndPos: 27,
},
Value: "static",
},
2018-02-13 10:33:21 +00:00
},
Properties: []node.Node{
&stmt.Property{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 28,
2018-06-24 07:19:44 +00:00
EndPos: 34,
},
2018-02-13 10:33:21 +00:00
PhpDocComment: "",
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: 28,
2018-06-24 07:19:44 +00:00
EndPos: 30,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 28,
2018-06-24 07:19:44 +00:00
EndPos: 30,
},
Value: "a",
},
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 33,
2018-06-24 07:19:44 +00:00
EndPos: 34,
},
Value: "1",
},
2018-02-13 10:33:21 +00:00
},
&stmt.Property{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 36,
2018-06-24 07:19:44 +00:00
EndPos: 38,
},
2018-02-13 10:33:21 +00:00
PhpDocComment: "",
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: 36,
2018-06-24 07:19:44 +00:00
EndPos: 38,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 36,
2018-06-24 07:19:44 +00:00
EndPos: 38,
},
Value: "b",
},
},
2018-02-13 10:33:21 +00:00
},
},
},
},
},
},
}
2019-12-26 15:57:56 +00:00
php7parser := php7.NewParser([]byte(src), "7.4")
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-13 10:33:21 +00:00
2019-12-26 15:57:56 +00:00
php5parser := php5.NewParser([]byte(src), "5.6")
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-13 10:33:21 +00:00
}
2019-12-29 14:36:56 +00:00
func TestPropertyType(t *testing.T) {
src := `<? class foo {var bar $a;}`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 26,
},
Stmts: []node.Node{
&stmt.Class{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 26,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 12,
},
Value: "foo",
},
Stmts: []node.Node{
&stmt.PropertyList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 25,
},
Modifiers: []node.Node{
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 17,
},
Value: "var",
},
},
Type: &name.Name{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 21,
},
Parts: []node.Node{
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 21,
},
Value: "bar",
},
},
},
Properties: []node.Node{
&stmt.Property{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
PhpDocComment: "",
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
Value: "a",
},
},
},
},
},
},
},
},
}
php7parser := php7.NewParser([]byte(src), "7.4")
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}