TraitUse tests

This commit is contained in:
z7zmey 2018-02-09 14:48:10 +02:00
parent 2f0480f8d6
commit 31b81540f3
7 changed files with 726 additions and 591 deletions

View File

@ -8,11 +8,11 @@ import (
// TraitUsePrecedence node // TraitUsePrecedence node
type TraitUsePrecedence struct { type TraitUsePrecedence struct {
Ref node.Node Ref node.Node
Insteadof node.Node Insteadof []node.Node
} }
// NewTraitUsePrecedence node constuctor // NewTraitUsePrecedence node constuctor
func NewTraitUsePrecedence(Ref node.Node, Insteadof node.Node) *TraitUsePrecedence { func NewTraitUsePrecedence(Ref node.Node, Insteadof []node.Node) *TraitUsePrecedence {
return &TraitUsePrecedence{ return &TraitUsePrecedence{
Ref, Ref,
Insteadof, Insteadof,
@ -38,7 +38,11 @@ func (n *TraitUsePrecedence) Walk(v walker.Visitor) {
if n.Insteadof != nil { if n.Insteadof != nil {
vv := v.GetChildrenVisitor("Insteadof") vv := v.GetChildrenVisitor("Insteadof")
n.Insteadof.Walk(vv) for _, nn := range n.Insteadof {
if nn != nil {
nn.Walk(vv)
}
}
} }
v.LeaveNode(n) v.LeaveNode(n)

View 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)
}

View File

@ -372,7 +372,7 @@ var nodesToTest = []struct {
{ {
&stmt.TraitUsePrecedence{ &stmt.TraitUsePrecedence{
Ref: &node.Identifier{}, Ref: &node.Identifier{},
Insteadof: &node.Identifier{}, Insteadof: []node.Node{&node.Identifier{}},
}, },
[]string{"Ref", "Insteadof"}, []string{"Ref", "Insteadof"},
map[string]interface{}{}, map[string]interface{}{},

File diff suppressed because it is too large Load Diff

View File

@ -1499,12 +1499,8 @@ trait_adaptation_statement:
trait_precedence: trait_precedence:
trait_method_reference_fully_qualified T_INSTEADOF trait_reference_list trait_method_reference_fully_qualified T_INSTEADOF trait_reference_list
{ {
name := name.NewName($3) $$ = stmt.NewTraitUsePrecedence($1, $3)
positions.AddPosition(name, positionBuilder.NewNodeListPosition($3))
$$ = stmt.NewTraitUsePrecedence($1, name)
positions.AddPosition($$, positionBuilder.NewNodeNodeListPosition($1, $3)) positions.AddPosition($$, positionBuilder.NewNodeNodeListPosition($1, $3))
comments.AddComments(name, ListGetFirstNodeComments($3))
comments.AddComments($$, comments[$1]) comments.AddComments($$, comments[$1])
} }
; ;

File diff suppressed because it is too large Load Diff

View File

@ -1207,12 +1207,8 @@ trait_adaptation:
trait_precedence: trait_precedence:
absolute_trait_method_reference T_INSTEADOF name_list absolute_trait_method_reference T_INSTEADOF name_list
{ {
name := name.NewName($3) $$ = stmt.NewTraitUsePrecedence($1, $3)
positions.AddPosition(name, positionBuilder.NewNodeListPosition($3))
$$ = stmt.NewTraitUsePrecedence($1, name)
positions.AddPosition($$, positionBuilder.NewNodeNodeListPosition($1, $3)) positions.AddPosition($$, positionBuilder.NewNodeNodeListPosition($1, $3))
comments.AddComments(name, ListGetFirstNodeComments($3))
comments.AddComments($$, comments[$1]) comments.AddComments($$, comments[$1])
} }
; ;