From b8ef8314af79618111c974bc8cc8d4d607fcaa1c Mon Sep 17 00:00:00 2001 From: z7zmey Date: Fri, 12 Jan 2018 11:26:00 +0200 Subject: [PATCH] update name tests --- node/name/name_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/node/name/name_test.go b/node/name/name_test.go index be072e2..4d244d4 100644 --- a/node/name/name_test.go +++ b/node/name/name_test.go @@ -89,3 +89,88 @@ func TestRelative(t *testing.T) { assertEqual(t, expected, actual) } + +// Test Name nodes visitors + +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 +} + +func (v *visitorMock) EnterNode(n node.Node) bool { return v.visitChildren } +func (v *visitorMock) GetChildrenVisitor(key string) node.Visitor { + v.visitedKeys = append(v.visitedKeys, key) + return &visitorMock{v.visitChildren, nil} +} +func (v *visitorMock) LeaveNode(n node.Node) {} + +func TestNameVisitorDisableChildren(t *testing.T) { + for _, tt := range nameNodesTests { + v := &visitorMock{false, nil} + tt.node.Walk(v) + + expected := []string{} + actual := v.visitedKeys + + diff := pretty.Compare(expected, actual) + if diff != "" { + t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff) + } + } +} + +func TestNameVisitor(t *testing.T) { + for _, tt := range nameNodesTests { + v := &visitorMock{true, nil} + tt.node.Walk(v) + + expected := tt.expectedVisitedKeys + actual := v.visitedKeys + + diff := pretty.Compare(expected, actual) + if diff != "" { + t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff) + } + } +} + +// test Attributes() + +func TestNameAttributes(t *testing.T) { + for _, tt := range nameNodesTests { + expected := tt.expectedAttributes + actual := tt.node.Attributes() + + diff := pretty.Compare(expected, actual) + if diff != "" { + t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff) + } + } +}