test name nodes

This commit is contained in:
z7zmey 2018-01-12 09:14:11 +02:00
parent 7332ab1700
commit 54ecfa2caf
3 changed files with 143 additions and 8 deletions

View File

@ -6,14 +6,36 @@ import (
// FullyQualified node
type FullyQualified struct {
Name
Parts []node.Node
}
// NewFullyQualified node constuctor
func NewFullyQualified(Parts []node.Node) *FullyQualified {
return &FullyQualified{
Name{
Parts,
},
Parts,
}
}
// Attributes returns node attributes as map
func (n *FullyQualified) Attributes() map[string]interface{} {
return nil
}
// Walk traverses nodes
// Walk is invoked recursively until v.EnterNode returns true
func (n *FullyQualified) Walk(v node.Visitor) {
if v.EnterNode(n) == false {
return
}
if n.Parts != nil {
vv := v.GetChildrenVisitor("Parts")
for _, nn := range n.Parts {
if nn != nil {
nn.Walk(vv)
}
}
}
v.LeaveNode(n)
}

View File

@ -6,14 +6,36 @@ import (
// Relative node
type Relative struct {
Name
Parts []node.Node
}
// NewRelative node constuctor
func NewRelative(Parts []node.Node) *Relative {
return &Relative{
Name{
Parts,
},
Parts,
}
}
// Attributes returns node attributes as map
func (n *Relative) Attributes() map[string]interface{} {
return nil
}
// Walk traverses nodes
// Walk is invoked recursively until v.EnterNode returns true
func (n *Relative) Walk(v node.Visitor) {
if v.EnterNode(n) == false {
return
}
if n.Parts != nil {
vv := v.GetChildrenVisitor("Parts")
for _, nn := range n.Parts {
if nn != nil {
nn.Walk(vv)
}
}
}
v.LeaveNode(n)
}

View File

@ -0,0 +1,91 @@
package test
import (
"bytes"
"reflect"
"testing"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/name"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/parser"
)
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
diff := pretty.Compare(expected, actual)
if diff != "" {
t.Errorf("diff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("expected and actual are not equal\n")
}
}
}
func TestName(t *testing.T) {
src := `<? foo();`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
Function: &name.Name{
Parts: []node.Node{&name.NamePart{Value: "foo"}},
},
Arguments: []node.Node{},
},
},
},
}
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestFullyQualified(t *testing.T) {
src := `<? \foo();`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
Function: &name.FullyQualified{
Parts: []node.Node{&name.NamePart{Value: "foo"}},
},
Arguments: []node.Node{},
},
},
},
}
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestRelative(t *testing.T) {
src := `<? namespace\foo();`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
Function: &name.Relative{
Parts: []node.Node{&name.NamePart{Value: "foo"}},
},
Arguments: []node.Node{},
},
},
},
}
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}