php-parser/node/stmt/t_class_const_list_test.go

239 lines
5.1 KiB
Go
Raw Normal View History

2018-02-08 17:52:22 +00:00
package stmt_test
import (
"bytes"
"testing"
"github.com/z7zmey/php-parser/node/scalar"
2018-06-24 07:19:44 +00:00
"github.com/z7zmey/php-parser/position"
2018-02-08 17:52:22 +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 TestClassConstList(t *testing.T) {
src := `<? class foo{ public const FOO = 1, BAR = 2; }`
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: 46,
},
2018-02-08 17:52:22 +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: 46,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 12,
},
Value: "foo",
},
2018-02-08 17:52:22 +00:00
Stmts: []node.Node{
&stmt.ClassConstList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 44,
},
2018-02-08 17:52:22 +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",
},
2018-02-08 17:52:22 +00:00
},
Consts: []node.Node{
&stmt.Constant{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 28,
EndPos: 34,
},
2018-02-08 17:52:22 +00:00
PhpDocComment: "",
2018-06-24 07:19:44 +00:00
ConstantName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 28,
EndPos: 30,
},
Value: "FOO",
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 34,
EndPos: 34,
},
Value: "1",
},
2018-02-08 17:52:22 +00:00
},
&stmt.Constant{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 37,
EndPos: 43,
},
2018-02-08 17:52:22 +00:00
PhpDocComment: "",
2018-06-24 07:19:44 +00:00
ConstantName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 37,
EndPos: 39,
},
Value: "BAR",
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 43,
EndPos: 43,
},
Value: "2",
},
2018-02-08 17:52:22 +00:00
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-08 17:52:22 +00:00
assertEqual(t, expected, actual)
}
func TestClassConstListWithoutModifiers(t *testing.T) {
src := `<? class foo{ const FOO = 1, BAR = 2; }`
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: 39,
},
2018-02-08 17:52:22 +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: 39,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 12,
},
Value: "foo",
},
2018-02-08 17:52:22 +00:00
Stmts: []node.Node{
&stmt.ClassConstList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 37,
},
2018-02-08 17:52:22 +00:00
Consts: []node.Node{
&stmt.Constant{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 21,
EndPos: 27,
},
2018-02-08 17:52:22 +00:00
PhpDocComment: "",
2018-06-24 07:19:44 +00:00
ConstantName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 21,
EndPos: 23,
},
Value: "FOO",
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 27,
EndPos: 27,
},
Value: "1",
},
2018-02-08 17:52:22 +00:00
},
&stmt.Constant{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 36,
},
2018-02-08 17:52:22 +00:00
PhpDocComment: "",
2018-06-24 07:19:44 +00:00
ConstantName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
Value: "BAR",
},
Expr: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 36,
EndPos: 36,
},
Value: "2",
},
2018-02-08 17:52:22 +00:00
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-08 17:52:22 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-08 17:52:22 +00:00
assertEqual(t, expected, actual)
}