Use UseList GroupUseList tests

This commit is contained in:
z7zmey 2018-02-09 12:36:31 +02:00
parent 57061bb982
commit 591c6a4f03

355
node/stmt/t_use_test.go Normal file
View File

@ -0,0 +1,355 @@
package stmt_test
import (
"github.com/z7zmey/php-parser/node/name"
"bytes"
"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"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
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"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
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"},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestUseListFunctionType(t *testing.T) {
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"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestUseListConstType(t *testing.T) {
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"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
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"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
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"},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
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"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
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"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
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"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}