Debian Codegraph @

syntax/walk_test.go

raw
1 // Copyright (c) 2016, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package syntax
5
6 import (
7 "fmt"
8 "reflect"
9 "slices"
10 "strings"
11 "testing"
12 )
13
14 func TestWalk(t *testing.T) {
15 t.Parallel()
16 seen := map[string]bool{
17 "*syntax.File": false,
18 "*syntax.Comment": false,
19 "*syntax.Stmt": false,
20 "*syntax.Assign": false,
21 "*syntax.Redirect": false,
22 "*syntax.CallExpr": false,
23 "*syntax.Subshell": false,
24 "*syntax.Block": false,
25 "*syntax.IfClause": false,
26 "*syntax.WhileClause": false,
27 "*syntax.ForClause": false,
28 "*syntax.WordIter": false,
29 "*syntax.CStyleLoop": false,
30 "*syntax.BinaryCmd": false,
31 "*syntax.FuncDecl": false,
32 "*syntax.Word": false,
33 "*syntax.Lit": false,
34 "*syntax.SglQuoted": false,
35 "*syntax.DblQuoted": false,
36 "*syntax.CmdSubst": false,
37 "*syntax.ParamExp": false,
38 "*syntax.ArithmExp": false,
39 "*syntax.ArithmCmd": false,
40 "*syntax.BinaryArithm": false,
41 "*syntax.UnaryArithm": false,
42 "*syntax.ParenArithm": false,
43 "*syntax.CaseClause": false,
44 "*syntax.CaseItem": false,
45 "*syntax.TestClause": false,
46 "*syntax.BinaryTest": false,
47 "*syntax.UnaryTest": false,
48 "*syntax.ParenTest": false,
49 "*syntax.DeclClause": false,
50 "*syntax.ArrayExpr": false,
51 "*syntax.ArrayElem": false,
52 "*syntax.ExtGlob": false,
53 "*syntax.ProcSubst": false,
54 "*syntax.TimeClause": false,
55 "*syntax.CoprocClause": false,
56 "*syntax.LetClause": false,
57 }
58 parser := NewParser(KeepComments(true))
59 var allStrs []string
60 for _, c := range fileTests {
61 allStrs = append(allStrs, c.inputs[0])
62 }
63 for _, c := range printTests {
64 allStrs = append(allStrs, c.in)
65 }
66 countRan := 0
67 for _, in := range allStrs {
68 t.Run("", func(t *testing.T) {
69 t.Logf("input: %s", in)
70 countRan++
71 prog, err := parser.Parse(strings.NewReader(in), "")
72 if err != nil {
73 // good enough for now, as the bash parser
74 // ignoring errors covers what we need.
75 return
76 }
77 lastOffs := uint(0)
78 Walk(prog, func(node Node) bool {
79 if node == nil {
80 return false
81 }
82 tstr := reflect.TypeOf(node).String()
83 if _, ok := seen[tstr]; !ok {
84 t.Errorf("unexpected type: %s", tstr)
85 } else {
86 seen[tstr] = true
87 }
88 switch node.(type) {
89 case *Lit:
90 return false
91 case *Comment:
92 default:
93 return true
94 }
95 offs := node.Pos().Offset()
96 if offs >= lastOffs {
97 lastOffs = offs
98 } else {
99 t.Errorf("comment offset goes back")
100 }
101 return true
102 })
103 })
104 }
105 // If we're running a subset of the tests,
106 // we can't expect to have seen all node types.
107 if countRan == len(allStrs) {
108 for tstr, tseen := range seen {
109 if !tseen {
110 t.Errorf("type not seen: %s", tstr)
111 }
112 }
113 }
114 }
115
116 func TestWalkIfClauseComments(t *testing.T) {
117 t.Parallel()
118 // Comments aligned with "else" or "fi" are attached to IfClause.Last.
119 in := "if a; then\n\tb\n# document the else\nelse\n\tc\n# document the fi\nfi\n"
120 prog, err := NewParser(KeepComments(true)).Parse(strings.NewReader(in), "")
121 if err != nil {
122 t.Fatal(err)
123 }
124 var comments []string
125 for node := range Preorder(prog) {
126 if c, ok := node.(*Comment); ok {
127 comments = append(comments, c.Text)
128 }
129 }
130 want := []string{" document the else", " document the fi"}
131 if !slices.Equal(comments, want) {
132 t.Fatalf("walked comments %q, want %q", comments, want)
133 }
134 }
135
136 func TestPreorder(t *testing.T) {
137 t.Parallel()
138 in := "echo ${foo:-bar}; { baz >f; } # comment"
139 prog, err := NewParser(KeepComments(true)).Parse(strings.NewReader(in), "")
140 if err != nil {
141 t.Fatal(err)
142 }
143 // Comment nodes are addressed differently on each traversal,
144 // so compare the nodes by type and position rather than by pointer.
145 describe := func(node Node) string {
146 return fmt.Sprintf("%T@%d", node, node.Pos().Offset())
147 }
148 var walked []string
149 Walk(prog, func(node Node) bool {
150 if node != nil {
151 walked = append(walked, describe(node))
152 }
153 return true
154 })
155 var collected []string
156 for node := range Preorder(prog) {
157 collected = append(collected, describe(node))
158 }
159 if !slices.Equal(collected, walked) {
160 t.Errorf("Preorder visited %q, Walk visited %q in preorder", collected, walked)
161 }
162 var stopped []string
163 for node := range Preorder(prog) {
164 stopped = append(stopped, describe(node))
165 if len(stopped) == 3 {
166 break
167 }
168 }
169 if !slices.Equal(stopped, walked[:3]) {
170 t.Errorf("stopping early visited %q, want %q", stopped, walked[:3])
171 }
172 }
173
174 type newNode struct{}
175
176 func (newNode) Pos() Pos { return Pos{} }
177 func (newNode) End() Pos { return Pos{} }
178
179 func TestWalkUnexpectedType(t *testing.T) {
180 t.Parallel()
181 defer func() {
182 if r := recover(); r == nil {
183 t.Errorf("did not panic")
184 }
185 }()
186 Walk(newNode{}, func(node Node) bool {
187 return true
188 })
189 }