test name nodes
This commit is contained in:
parent
7332ab1700
commit
54ecfa2caf
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
91
test/node/name/name_test.go
Normal file
91
test/node/name/name_test.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user