TraitUse tests
This commit is contained in:
parent
2f0480f8d6
commit
31b81540f3
@ -8,11 +8,11 @@ import (
|
||||
// TraitUsePrecedence node
|
||||
type TraitUsePrecedence struct {
|
||||
Ref node.Node
|
||||
Insteadof node.Node
|
||||
Insteadof []node.Node
|
||||
}
|
||||
|
||||
// NewTraitUsePrecedence node constuctor
|
||||
func NewTraitUsePrecedence(Ref node.Node, Insteadof node.Node) *TraitUsePrecedence {
|
||||
func NewTraitUsePrecedence(Ref node.Node, Insteadof []node.Node) *TraitUsePrecedence {
|
||||
return &TraitUsePrecedence{
|
||||
Ref,
|
||||
Insteadof,
|
||||
@ -38,7 +38,11 @@ func (n *TraitUsePrecedence) Walk(v walker.Visitor) {
|
||||
|
||||
if n.Insteadof != nil {
|
||||
vv := v.GetChildrenVisitor("Insteadof")
|
||||
n.Insteadof.Walk(vv)
|
||||
for _, nn := range n.Insteadof {
|
||||
if nn != nil {
|
||||
nn.Walk(vv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
v.LeaveNode(n)
|
||||
|
147
node/stmt/t_trait_use_test.go
Normal file
147
node/stmt/t_trait_use_test.go
Normal file
@ -0,0 +1,147 @@
|
||||
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 TestTraitUse(t *testing.T) {
|
||||
src := `<? class Foo { use Bar; }`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Class{
|
||||
PhpDocComment: "",
|
||||
ClassName: &node.Identifier{Value: "Foo"},
|
||||
Stmts: []node.Node{
|
||||
&stmt.TraitUse{
|
||||
Traits: []node.Node{
|
||||
&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 TestTraitsUse(t *testing.T) {
|
||||
src := `<? class Foo { use Bar, Baz; }`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Class{
|
||||
PhpDocComment: "",
|
||||
ClassName: &node.Identifier{Value: "Foo"},
|
||||
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"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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 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"},
|
||||
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"},
|
||||
},
|
||||
},
|
||||
},
|
||||
Adaptations: []node.Node{
|
||||
&stmt.TraitUsePrecedence{
|
||||
Ref: &stmt.TraitMethodRef{
|
||||
Trait: &name.Name{
|
||||
Parts: []node.Node{
|
||||
&name.NamePart{Value: "Bar"},
|
||||
},
|
||||
},
|
||||
Method: &node.Identifier{Value: "one"},
|
||||
},
|
||||
Insteadof: []node.Node{
|
||||
&name.Name{
|
||||
Parts: []node.Node{
|
||||
&name.NamePart{Value: "Baz"},
|
||||
},
|
||||
},
|
||||
&name.Name{
|
||||
Parts: []node.Node{
|
||||
&name.NamePart{Value: "Quux"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
&stmt.TraitUseAlias{
|
||||
Ref: &stmt.TraitMethodRef{
|
||||
Trait: &name.Name{
|
||||
Parts: []node.Node{
|
||||
&name.NamePart{Value: "Baz"},
|
||||
},
|
||||
},
|
||||
Method: &node.Identifier{Value: "one"},
|
||||
},
|
||||
Alias: &node.Identifier{Value: "two"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
||||
assertEqual(t, expected, actual)
|
||||
|
||||
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
@ -372,7 +372,7 @@ var nodesToTest = []struct {
|
||||
{
|
||||
&stmt.TraitUsePrecedence{
|
||||
Ref: &node.Identifier{},
|
||||
Insteadof: &node.Identifier{},
|
||||
Insteadof: []node.Node{&node.Identifier{}},
|
||||
},
|
||||
[]string{"Ref", "Insteadof"},
|
||||
map[string]interface{}{},
|
||||
|
670
php5/php5.go
670
php5/php5.go
File diff suppressed because it is too large
Load Diff
@ -1499,12 +1499,8 @@ trait_adaptation_statement:
|
||||
trait_precedence:
|
||||
trait_method_reference_fully_qualified T_INSTEADOF trait_reference_list
|
||||
{
|
||||
name := name.NewName($3)
|
||||
positions.AddPosition(name, positionBuilder.NewNodeListPosition($3))
|
||||
$$ = stmt.NewTraitUsePrecedence($1, name)
|
||||
$$ = stmt.NewTraitUsePrecedence($1, $3)
|
||||
positions.AddPosition($$, positionBuilder.NewNodeNodeListPosition($1, $3))
|
||||
|
||||
comments.AddComments(name, ListGetFirstNodeComments($3))
|
||||
comments.AddComments($$, comments[$1])
|
||||
}
|
||||
;
|
||||
|
476
php7/php7.go
476
php7/php7.go
File diff suppressed because it is too large
Load Diff
@ -1207,12 +1207,8 @@ trait_adaptation:
|
||||
trait_precedence:
|
||||
absolute_trait_method_reference T_INSTEADOF name_list
|
||||
{
|
||||
name := name.NewName($3)
|
||||
positions.AddPosition(name, positionBuilder.NewNodeListPosition($3))
|
||||
$$ = stmt.NewTraitUsePrecedence($1, name)
|
||||
$$ = stmt.NewTraitUsePrecedence($1, $3)
|
||||
positions.AddPosition($$, positionBuilder.NewNodeNodeListPosition($1, $3))
|
||||
|
||||
comments.AddComments(name, ListGetFirstNodeComments($3))
|
||||
comments.AddComments($$, comments[$1])
|
||||
}
|
||||
;
|
||||
|
Loading…
Reference in New Issue
Block a user