Debian Codegraph @

ast/ast_test.go

raw symbols SCIP
1 package ast
2
3 import (
4 "reflect"
5 "testing"
6 )
7
8 func TestWalk(t *testing.T) {
9 tests := []struct {
10 name string
11 node Node
12 want []NodeKind
13 action map[NodeKind]WalkStatus
14 }{
15 {
16 "visits all in depth first order",
17 node(NewDocument(), node(NewHeading(1), NewText()), NewLink()),
18 []NodeKind{KindDocument, KindHeading, KindText, KindLink},
19 map[NodeKind]WalkStatus{},
20 },
21 {
22 "stops after heading",
23 node(NewDocument(), node(NewHeading(1), NewText()), NewLink()),
24 []NodeKind{KindDocument, KindHeading},
25 map[NodeKind]WalkStatus{KindHeading: WalkStop},
26 },
27 {
28 "skip children",
29 node(NewDocument(), node(NewHeading(1), NewText()), NewLink()),
30 []NodeKind{KindDocument, KindHeading, KindLink},
31 map[NodeKind]WalkStatus{KindHeading: WalkSkipChildren},
32 },
33 }
34 for _, tt := range tests {
35 var kinds []NodeKind
36 collectKinds := func(n Node, entering bool) (WalkStatus, error) {
37 if entering {
38 kinds = append(kinds, n.Kind())
39 }
40 if status, ok := tt.action[n.Kind()]; ok {
41 return status, nil
42 }
43 return WalkContinue, nil
44 }
45 t.Run(tt.name, func(t *testing.T) {
46 if err := Walk(tt.node, collectKinds); err != nil {
47 t.Errorf("Walk() error = %v", err)
48 } else if !reflect.DeepEqual(kinds, tt.want) {
49 t.Errorf("Walk() expected = %v, got = %v", tt.want, kinds)
50 }
51 })
52 }
53 }
54
55 func node(n Node, children ...Node) Node {
56 for _, c := range children {
57 n.AppendChild(n, c)
58 }
59 return n
60 }