php-parser/node/stmt/t_trait_use_test.go

302 lines
7.0 KiB
Go
Raw Normal View History

2018-02-09 12:48:10 +00:00
package stmt_test
import (
"bytes"
"testing"
2018-04-29 19:34:24 +00:00
"github.com/z7zmey/php-parser/node/name"
2018-02-09 12:48:10 +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 TestTraitUse(t *testing.T) {
src := `<? class Foo { use Bar; }`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
ClassName: &node.Identifier{Value: "Foo"},
2018-02-09 12:48:10 +00:00
Stmts: []node.Node{
&stmt.TraitUse{
Traits: []node.Node{
&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 12:48:10 +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 12:48:10 +00:00
assertEqual(t, expected, actual)
}
func TestTraitsUse(t *testing.T) {
src := `<? class Foo { use Bar, Baz; }`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
ClassName: &node.Identifier{Value: "Foo"},
2018-02-09 12:48:10 +00:00
Stmts: []node.Node{
&stmt.TraitUse{
Traits: []node.Node{
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
&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 12:48:10 +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 12:48:10 +00:00
assertEqual(t, expected, actual)
}
2018-02-13 10:16:53 +00:00
func TestTraitsUseEmptyAdaptations(t *testing.T) {
src := `<? class Foo { use Bar, Baz {} }`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
ClassName: &node.Identifier{Value: "Foo"},
2018-02-13 10:16:53 +00:00
Stmts: []node.Node{
&stmt.TraitUse{
Traits: []node.Node{
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
},
2018-04-29 19:34:24 +00:00
TraitAdaptationList: &stmt.TraitAdaptationList{},
2018-02-13 10:16:53 +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-13 10:16:53 +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-13 10:16:53 +00:00
assertEqual(t, expected, actual)
}
func TestTraitsUseModifier(t *testing.T) {
src := `<? class Foo { use Bar, Baz { one as public; } }`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
ClassName: &node.Identifier{Value: "Foo"},
2018-02-13 10:16:53 +00:00
Stmts: []node.Node{
&stmt.TraitUse{
Traits: []node.Node{
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
},
2018-04-29 19:34:24 +00:00
TraitAdaptationList: &stmt.TraitAdaptationList{
Adaptations: []node.Node{
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Method: &node.Identifier{Value: "one"},
},
Modifier: &node.Identifier{Value: "public"},
2018-02-13 10:16:53 +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-13 10:16:53 +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-13 10:16:53 +00:00
assertEqual(t, expected, actual)
}
func TestTraitsUseAliasModifier(t *testing.T) {
src := `<? class Foo { use Bar, Baz { one as public two; } }`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
ClassName: &node.Identifier{Value: "Foo"},
2018-02-13 10:16:53 +00:00
Stmts: []node.Node{
&stmt.TraitUse{
Traits: []node.Node{
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
},
2018-04-29 19:34:24 +00:00
TraitAdaptationList: &stmt.TraitAdaptationList{
Adaptations: []node.Node{
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Method: &node.Identifier{Value: "one"},
},
Modifier: &node.Identifier{Value: "public"},
Alias: &node.Identifier{Value: "two"},
2018-02-13 10:16:53 +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-13 10:16:53 +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-13 10:16:53 +00:00
assertEqual(t, expected, actual)
}
2018-02-09 12:48:10 +00:00
func TestTraitsUseAdaptions(t *testing.T) {
src := `<? class Foo { use Bar, Baz { Bar::one insteadof Baz, Quux; Baz::one as two; } }`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Class{
PhpDocComment: "",
ClassName: &node.Identifier{Value: "Foo"},
2018-02-09 12:48:10 +00:00
Stmts: []node.Node{
&stmt.TraitUse{
Traits: []node.Node{
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
},
2018-04-29 19:34:24 +00:00
TraitAdaptationList: &stmt.TraitAdaptationList{
Adaptations: []node.Node{
&stmt.TraitUsePrecedence{
Ref: &stmt.TraitMethodRef{
Trait: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
2018-02-09 12:48:10 +00:00
},
2018-04-29 19:34:24 +00:00
Method: &node.Identifier{Value: "one"},
2018-02-09 12:48:10 +00:00
},
2018-04-29 19:34:24 +00:00
Insteadof: []node.Node{
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
2018-02-09 12:48:10 +00:00
},
2018-04-29 19:34:24 +00:00
&name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Quux"},
},
2018-02-09 12:48:10 +00:00
},
},
},
2018-04-29 19:34:24 +00:00
&stmt.TraitUseAlias{
Ref: &stmt.TraitMethodRef{
Trait: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
2018-02-09 12:48:10 +00:00
},
2018-04-29 19:34:24 +00:00
Method: &node.Identifier{Value: "one"},
2018-02-09 12:48:10 +00:00
},
2018-04-29 19:34:24 +00:00
Alias: &node.Identifier{Value: "two"},
2018-02-09 12:48:10 +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 12:48:10 +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 12:48:10 +00:00
assertEqual(t, expected, actual)
}