From 54ecfa2cafe806cc6142dfb980a78716228b2123 Mon Sep 17 00:00:00 2001 From: z7zmey Date: Fri, 12 Jan 2018 09:14:11 +0200 Subject: [PATCH] test name nodes --- node/name/fully_qualified.go | 30 ++++++++++-- node/name/relative.go | 30 ++++++++++-- test/node/name/name_test.go | 91 ++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 test/node/name/name_test.go diff --git a/node/name/fully_qualified.go b/node/name/fully_qualified.go index 4f95f2b..d973177 100644 --- a/node/name/fully_qualified.go +++ b/node/name/fully_qualified.go @@ -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) +} diff --git a/node/name/relative.go b/node/name/relative.go index 4c11deb..9fa1614 100644 --- a/node/name/relative.go +++ b/node/name/relative.go @@ -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) +} diff --git a/test/node/name/name_test.go b/test/node/name/name_test.go new file mode 100644 index 0000000..3097c87 --- /dev/null +++ b/test/node/name/name_test.go @@ -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 := `