php-parser/node/name/t_visitor_test.go

90 lines
2.0 KiB
Go
Raw Normal View History

2018-02-08 10:48:38 +00:00
package name_test
import (
"testing"
"gotest.tools/assert"
2018-02-08 10:48:38 +00:00
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/walker"
2018-02-08 10:48:38 +00:00
)
var nameNodesTests = []struct {
node node.Node // node
expectedVisitedKeys []string // visited keys
expectedAttributes map[string]interface{}
}{
{
&name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}},
[]string{"Parts"},
nil,
},
{
&name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "foo"}}},
[]string{"Parts"},
nil,
},
{
&name.Relative{Parts: []node.Node{&name.NamePart{Value: "foo"}}},
[]string{"Parts"},
nil,
},
{
&name.NamePart{Value: "foo"},
[]string{},
map[string]interface{}{"Value": "foo"},
},
}
type visitorMock struct {
visitChildren bool
visitedKeys []string
}
2018-02-20 17:52:07 +00:00
func (v *visitorMock) EnterNode(n walker.Walkable) bool { return v.visitChildren }
2018-06-18 20:29:52 +00:00
func (v *visitorMock) LeaveNode(n walker.Walkable) {}
func (v *visitorMock) EnterChildNode(key string, w walker.Walkable) {
2018-02-08 10:48:38 +00:00
v.visitedKeys = append(v.visitedKeys, key)
}
2018-06-18 20:29:52 +00:00
func (v *visitorMock) LeaveChildNode(key string, w walker.Walkable) {}
func (v *visitorMock) EnterChildList(key string, w walker.Walkable) {
v.visitedKeys = append(v.visitedKeys, key)
}
func (v *visitorMock) LeaveChildList(key string, w walker.Walkable) {}
2018-02-08 10:48:38 +00:00
func TestNameVisitorDisableChildren(t *testing.T) {
for _, tt := range nameNodesTests {
v := &visitorMock{false, []string{}}
2018-02-08 10:48:38 +00:00
tt.node.Walk(v)
expected := []string{}
actual := v.visitedKeys
assert.DeepEqual(t, expected, actual)
2018-02-08 10:48:38 +00:00
}
}
func TestNameVisitor(t *testing.T) {
for _, tt := range nameNodesTests {
v := &visitorMock{true, []string{}}
2018-02-08 10:48:38 +00:00
tt.node.Walk(v)
expected := tt.expectedVisitedKeys
actual := v.visitedKeys
assert.DeepEqual(t, expected, actual)
2018-02-08 10:48:38 +00:00
}
}
// test Attributes()
func TestNameAttributes(t *testing.T) {
for _, tt := range nameNodesTests {
expected := tt.expectedAttributes
actual := tt.node.Attributes()
assert.DeepEqual(t, expected, actual)
2018-02-08 10:48:38 +00:00
}
}