php-parser/node/stmt/t_property_list_test.go

382 lines
8.3 KiB
Go
Raw Normal View History

2018-02-09 11:55:02 +00:00
package stmt_test
import (
"bytes"
2018-06-24 07:19:44 +00:00
"testing"
"gotest.tools/assert"
"github.com/z7zmey/php-parser/node/expr"
"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,
StartPos: 4,
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,
StartPos: 4,
EndPos: 22,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
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,
StartPos: 15,
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,
StartPos: 15,
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,
StartPos: 19,
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,
StartPos: 19,
EndPos: 20,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 20,
},
Value: "a",
},
},
2018-02-09 11:55:02 +00:00
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-09 11:55:02 +00:00
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
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,
StartPos: 4,
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,
StartPos: 4,
EndPos: 40,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
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,
StartPos: 15,
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,
StartPos: 15,
EndPos: 20,
},
Value: "public",
},
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
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,
StartPos: 29,
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,
StartPos: 29,
EndPos: 30,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 29,
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,
StartPos: 33,
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,
StartPos: 33,
EndPos: 34,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 33,
EndPos: 34,
},
Value: "b",
},
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 38,
EndPos: 38,
},
Value: "1",
},
2018-02-09 11:55:02 +00:00
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-09 11:55:02 +00:00
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
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,
StartPos: 4,
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,
StartPos: 4,
EndPos: 40,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
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,
StartPos: 15,
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,
StartPos: 15,
EndPos: 20,
},
Value: "public",
},
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
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,
StartPos: 29,
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,
StartPos: 29,
EndPos: 30,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 29,
EndPos: 30,
},
Value: "a",
},
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 34,
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,
StartPos: 37,
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,
StartPos: 37,
EndPos: 38,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 37,
EndPos: 38,
},
Value: "b",
},
},
2018-02-13 10:33:21 +00:00
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-13 10:33:21 +00:00
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-13 10:33:21 +00:00
}