php-parser/node/stmt/t_use_test.go

525 lines
11 KiB
Go
Raw Normal View History

2018-02-09 10:36:31 +00:00
package stmt_test
import (
"bytes"
"github.com/z7zmey/php-parser/node/name"
2018-02-09 10:36:31 +00:00
"testing"
"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;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.UseList{
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 10:36:31 +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-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}
2018-02-12 13:08:08 +00:00
func TestUseFullyQualified(t *testing.T) {
src := `<? use \Foo;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.UseList{
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-12 13:08:08 +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-12 13:08:08 +00:00
assertEqual(t, expected, actual)
}
func TestUseFullyQualifiedAlias(t *testing.T) {
src := `<? use \Foo as Bar;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.UseList{
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
Alias: &node.Identifier{Value: "Bar"},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-12 13:08:08 +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-12 13:08:08 +00:00
assertEqual(t, expected, actual)
}
2018-02-09 10:36:31 +00:00
func TestUseList(t *testing.T) {
src := `<? use Foo, Bar;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.UseList{
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 10:36:31 +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-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}
func TestUseListAlias(t *testing.T) {
src := `<? use Foo, Bar as Baz;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.UseList{
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
Alias: &node.Identifier{Value: "Baz"},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 10:36:31 +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-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}
func TestUseListFunctionType(t *testing.T) {
2018-02-12 13:08:08 +00:00
src := `<? use function Foo, \Bar;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.UseList{
UseType: &node.Identifier{Value: "function"},
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-12 13:08:08 +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-12 13:08:08 +00:00
assertEqual(t, expected, actual)
}
func TestUseListFunctionTypeAliases(t *testing.T) {
src := `<? use function Foo as foo, \Bar as bar;`
2018-02-09 10:36:31 +00:00
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.UseList{
UseType: &node.Identifier{Value: "function"},
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
2018-02-12 13:08:08 +00:00
Alias: &node.Identifier{Value: "foo"},
2018-02-09 10:36:31 +00:00
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
2018-02-12 13:08:08 +00:00
Alias: &node.Identifier{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()
2018-02-09 10:36:31 +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-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}
func TestUseListConstType(t *testing.T) {
2018-02-12 13:08:08 +00:00
src := `<? use const Foo, \Bar;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.UseList{
UseType: &node.Identifier{Value: "const"},
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-12 13:08:08 +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-12 13:08:08 +00:00
assertEqual(t, expected, actual)
}
func TestUseListConstTypeAliases(t *testing.T) {
src := `<? use const Foo as foo, \Bar as bar;`
2018-02-09 10:36:31 +00:00
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.UseList{
UseType: &node.Identifier{Value: "const"},
Uses: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
2018-02-12 13:08:08 +00:00
Alias: &node.Identifier{Value: "foo"},
2018-02-09 10:36:31 +00:00
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
2018-02-12 13:08:08 +00:00
Alias: &node.Identifier{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()
2018-02-09 10:36:31 +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-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}
func TestGroupUse(t *testing.T) {
src := `<? use Foo\{Bar, Baz};`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.GroupUse{
Prefix: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
UseList: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}
func TestGroupUseAlias(t *testing.T) {
src := `<? use Foo\{Bar, Baz as quux};`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.GroupUse{
Prefix: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
UseList: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
Alias: &node.Identifier{Value: "quux"},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}
func TestFunctionGroupUse(t *testing.T) {
src := `<? use function Foo\{Bar, Baz};`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.GroupUse{
UseType: &node.Identifier{Value: "function"},
Prefix: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
UseList: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}
func TestConstGroupUse(t *testing.T) {
src := `<? use const Foo\{Bar, Baz};`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.GroupUse{
UseType: &node.Identifier{Value: "const"},
Prefix: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
UseList: []node.Node{
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
},
&stmt.Use{
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}
func TestMixedGroupUse(t *testing.T) {
src := `<? use Foo\{const Bar, function Baz};`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.GroupUse{
Prefix: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
UseList: []node.Node{
&stmt.Use{
UseType: &node.Identifier{Value: "const"},
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
},
&stmt.Use{
UseType: &node.Identifier{Value: "function"},
Use: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 10:36:31 +00:00
assertEqual(t, expected, actual)
}