php-parser/node/stmt/t_use_test.go

1387 lines
28 KiB
Go
Raw Normal View History

2018-02-09 10:36:31 +00:00
package stmt_test
import (
"bytes"
"testing"
"gotest.tools/assert"
2018-06-24 07:19:44 +00:00
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/position"
2018-02-09 10:36:31 +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 TestSimpleUse(t *testing.T) {
src := `<? use Foo;`
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: 11,
},
2018-02-09 10:36:31 +00:00
Stmts: []node.Node{
&stmt.UseList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
2018-02-09 10:36:31 +00:00
Uses: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
Value: "Foo",
},
2018-02-09 10:36:31 +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 10:36:31 +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 10:36:31 +00:00
}
2018-02-12 13:08:08 +00:00
func TestUseFullyQualified(t *testing.T) {
src := `<? use \Foo;`
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: 12,
},
2018-02-12 13:08:08 +00:00
Stmts: []node.Node{
&stmt.UseList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
2018-02-12 13:08:08 +00:00
Uses: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 11,
},
2018-02-12 13:08:08 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 11,
},
2018-02-12 13:08:08 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 11,
},
Value: "Foo",
},
2018-02-12 13:08:08 +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-12 13:08:08 +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-12 13:08:08 +00:00
}
func TestUseFullyQualifiedAlias(t *testing.T) {
src := `<? use \Foo as Bar;`
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: 19,
},
2018-02-12 13:08:08 +00:00
Stmts: []node.Node{
&stmt.UseList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 19,
},
2018-02-12 13:08:08 +00:00
Uses: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 18,
},
2018-02-12 13:08:08 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 11,
},
2018-02-12 13:08:08 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 11,
},
Value: "Foo",
},
},
},
Alias: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
2018-02-12 13:08:08 +00:00
},
2018-06-24 07:19:44 +00:00
Value: "Bar",
2018-02-12 13:08:08 +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-12 13:08:08 +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-12 13:08:08 +00:00
}
2018-02-09 10:36:31 +00:00
func TestUseList(t *testing.T) {
src := `<? use Foo, Bar;`
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-09 10:36:31 +00:00
Stmts: []node.Node{
&stmt.UseList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 16,
},
2018-02-09 10:36:31 +00:00
Uses: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
Value: "Foo",
},
2018-02-09 10:36:31 +00:00
},
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
Value: "Bar",
},
2018-02-09 10:36:31 +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 10:36:31 +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 10:36:31 +00:00
}
func TestUseListAlias(t *testing.T) {
src := `<? use Foo, Bar as Baz;`
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-09 10:36:31 +00:00
Stmts: []node.Node{
&stmt.UseList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 23,
},
2018-02-09 10:36:31 +00:00
Uses: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
Value: "Foo",
},
2018-02-09 10:36:31 +00:00
},
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 22,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
Value: "Bar",
},
},
},
Alias: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 22,
2018-02-09 10:36:31 +00:00
},
2018-06-24 07:19:44 +00:00
Value: "Baz",
2018-02-09 10:36:31 +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 10:36:31 +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 10:36:31 +00:00
}
func TestUseListFunctionType(t *testing.T) {
2018-02-12 13:08:08 +00:00
src := `<? use function Foo, \Bar;`
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: 26,
},
2018-02-12 13:08:08 +00:00
Stmts: []node.Node{
&stmt.UseList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 26,
},
UseType: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 15,
},
Value: "function",
},
2018-02-12 13:08:08 +00:00
Uses: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 19,
},
2018-02-12 13:08:08 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 19,
},
2018-02-12 13:08:08 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 19,
},
Value: "Foo",
},
2018-02-12 13:08:08 +00:00
},
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 23,
EndPos: 25,
},
2018-02-12 13:08:08 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 23,
EndPos: 25,
},
2018-02-12 13:08:08 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 23,
EndPos: 25,
},
Value: "Bar",
},
2018-02-12 13:08:08 +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-12 13:08:08 +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-12 13:08:08 +00:00
}
func TestUseListFunctionTypeAliases(t *testing.T) {
src := `<? use function Foo as foo, \Bar as bar;`
2018-02-09 10:36:31 +00:00
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 10:36:31 +00:00
Stmts: []node.Node{
&stmt.UseList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 40,
},
UseType: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 15,
},
Value: "function",
},
2018-02-09 10:36:31 +00:00
Uses: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 26,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 19,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 19,
},
Value: "Foo",
},
},
},
Alias: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 26,
2018-02-09 10:36:31 +00:00
},
2018-06-24 07:19:44 +00:00
Value: "foo",
2018-02-09 10:36:31 +00:00
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 39,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
Value: "Bar",
},
},
},
Alias: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 37,
EndPos: 39,
2018-02-09 10:36:31 +00:00
},
2018-06-24 07:19:44 +00:00
Value: "bar",
2018-02-09 10:36:31 +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 10:36:31 +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 10:36:31 +00:00
}
func TestUseListConstType(t *testing.T) {
2018-02-12 13:08:08 +00:00
src := `<? use const Foo, \Bar;`
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-12 13:08:08 +00:00
Stmts: []node.Node{
&stmt.UseList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 23,
},
UseType: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 12,
},
Value: "const",
},
2018-02-12 13:08:08 +00:00
Uses: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 16,
},
2018-02-12 13:08:08 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 16,
},
2018-02-12 13:08:08 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 16,
},
Value: "Foo",
},
2018-02-12 13:08:08 +00:00
},
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 22,
},
2018-02-12 13:08:08 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 22,
},
2018-02-12 13:08:08 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 22,
},
Value: "Bar",
},
2018-02-12 13:08:08 +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-12 13:08:08 +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-12 13:08:08 +00:00
}
func TestUseListConstTypeAliases(t *testing.T) {
src := `<? use const Foo as foo, \Bar as bar;`
2018-02-09 10:36:31 +00:00
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-09 10:36:31 +00:00
Stmts: []node.Node{
&stmt.UseList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 37,
},
UseType: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 12,
},
Value: "const",
},
2018-02-09 10:36:31 +00:00
Uses: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 23,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 16,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 16,
},
Value: "Foo",
},
},
},
Alias: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 21,
EndPos: 23,
2018-02-09 10:36:31 +00:00
},
2018-06-24 07:19:44 +00:00
Value: "foo",
2018-02-09 10:36:31 +00:00
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 27,
EndPos: 36,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 27,
EndPos: 29,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 27,
EndPos: 29,
},
Value: "Bar",
},
},
},
Alias: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 34,
EndPos: 36,
2018-02-09 10:36:31 +00:00
},
2018-06-24 07:19:44 +00:00
Value: "bar",
2018-02-09 10:36:31 +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 10:36:31 +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 10:36:31 +00:00
}
func TestGroupUse(t *testing.T) {
src := `<? use Foo\{Bar, Baz};`
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 10:36:31 +00:00
Stmts: []node.Node{
&stmt.GroupUse{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 22,
},
2018-02-09 10:36:31 +00:00
Prefix: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
Value: "Foo",
},
2018-02-09 10:36:31 +00:00
},
},
UseList: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
Value: "Bar",
},
2018-02-09 10:36:31 +00:00
},
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
Value: "Baz",
},
2018-02-09 10:36:31 +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 10:36:31 +00:00
}
func TestGroupUseAlias(t *testing.T) {
src := `<? use Foo\{Bar, Baz as quux};`
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: 30,
},
2018-02-09 10:36:31 +00:00
Stmts: []node.Node{
&stmt.GroupUse{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 30,
},
2018-02-09 10:36:31 +00:00
Prefix: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
Value: "Foo",
},
2018-02-09 10:36:31 +00:00
},
},
UseList: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 15,
},
Value: "Bar",
},
2018-02-09 10:36:31 +00:00
},
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 28,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
Value: "Baz",
},
},
},
Alias: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 28,
2018-02-09 10:36:31 +00:00
},
2018-06-24 07:19:44 +00:00
Value: "quux",
2018-02-09 10:36:31 +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 10:36:31 +00:00
}
func TestFunctionGroupUse(t *testing.T) {
src := `<? use function Foo\{Bar, Baz};`
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: 31,
},
2018-02-09 10:36:31 +00:00
Stmts: []node.Node{
&stmt.GroupUse{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 31,
},
UseType: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 15,
},
Value: "function",
},
2018-02-09 10:36:31 +00:00
Prefix: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 19,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 19,
},
Value: "Foo",
},
2018-02-09 10:36:31 +00:00
},
},
UseList: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
Value: "Bar",
},
2018-02-09 10:36:31 +00:00
},
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 27,
EndPos: 29,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 27,
EndPos: 29,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 27,
EndPos: 29,
},
Value: "Baz",
},
2018-02-09 10:36:31 +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 10:36:31 +00:00
}
func TestConstGroupUse(t *testing.T) {
src := `<? use const Foo\{Bar, Baz};`
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: 28,
},
2018-02-09 10:36:31 +00:00
Stmts: []node.Node{
&stmt.GroupUse{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 28,
},
UseType: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 12,
},
Value: "const",
},
2018-02-09 10:36:31 +00:00
Prefix: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 16,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 16,
},
Value: "Foo",
},
2018-02-09 10:36:31 +00:00
},
},
UseList: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 21,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 21,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 21,
},
Value: "Bar",
},
2018-02-09 10:36:31 +00:00
},
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 26,
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 26,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 26,
},
Value: "Baz",
},
2018-02-09 10:36:31 +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 10:36:31 +00:00
}
func TestMixedGroupUse(t *testing.T) {
src := `<? use Foo\{const Bar, function Baz};`
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-09 10:36:31 +00:00
Stmts: []node.Node{
&stmt.GroupUse{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 37,
},
2018-02-09 10:36:31 +00:00
Prefix: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
Value: "Foo",
},
2018-02-09 10:36:31 +00:00
},
},
UseList: []node.Node{
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 21,
},
UseType: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 17,
},
Value: "const",
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 21,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 21,
},
Value: "Bar",
},
2018-02-09 10:36:31 +00:00
},
},
},
&stmt.Use{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 33,
EndPos: 35,
},
UseType: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 31,
},
Value: "function",
},
2018-02-09 10:36:31 +00:00
Use: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 33,
EndPos: 35,
},
2018-02-09 10:36:31 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 33,
EndPos: 35,
},
Value: "Baz",
},
2018-02-09 10:36:31 +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)
}