Debian Codegraph @

expand/braces_test.go

raw
1 // Copyright (c) 2018, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package expand
5
6 import (
7 "bytes"
8 "testing"
9
10 "mvdan.cc/sh/v3/syntax"
11 )
12
13 func lit(s string) *syntax.Lit { return &syntax.Lit{Value: s} }
14 func word(ps ...syntax.WordPart) *syntax.Word { return &syntax.Word{Parts: ps} }
15 func litWord(s string) *syntax.Word { return word(lit(s)) }
16 func litWords(strs ...string) []*syntax.Word {
17 l := make([]*syntax.Word, 0, len(strs))
18 for _, s := range strs {
19 l = append(l, litWord(s))
20 }
21 return l
22 }
23
24 var braceTests = []struct {
25 in *syntax.Word
26 want []*syntax.Word
27 }{
28 {
29 litWord("a{b"),
30 litWords("a{b"),
31 },
32 {
33 litWord("a}b"),
34 litWords("a}b"),
35 },
36 {
37 litWord("{a,b{c,d}"),
38 litWords("{a,bc", "{a,bd"),
39 },
40 {
41 litWord("{a{b"),
42 litWords("{a{b"),
43 },
44 {
45 litWord("a{}"),
46 litWords("a{}"),
47 },
48 {
49 litWord("a{b}"),
50 litWords("a{b}"),
51 },
52 {
53 litWord("a{b,c}"),
54 litWords("ab", "ac"),
55 },
56 {
57 litWord("a{à,世界}"),
58 litWords("aà", "a世界"),
59 },
60 {
61 litWord("a{b,c}d{e,f}g"),
62 litWords("abdeg", "abdfg", "acdeg", "acdfg"),
63 },
64 {
65 litWord("a{b{x,y},c}d"),
66 litWords("abxd", "abyd", "acd"),
67 },
68 {
69 litWord("a{1,2,3,4,5}"),
70 litWords("a1", "a2", "a3", "a4", "a5"),
71 },
72 {
73 litWord("a{1.."),
74 litWords("a{1.."),
75 },
76 {
77 litWord("a{1..4"),
78 litWords("a{1..4"),
79 },
80 {
81 litWord("a{1.4}"),
82 litWords("a{1.4}"),
83 },
84 {
85 litWord("{a,b}{1..4"),
86 litWords("a{1..4", "b{1..4"),
87 },
88 {
89 litWord("a{1..4}"),
90 litWords("a1", "a2", "a3", "a4"),
91 },
92 {
93 litWord("a{1..2}b{4..5}c"),
94 litWords("a1b4c", "a1b5c", "a2b4c", "a2b5c"),
95 },
96 {
97 litWord("a{1..f}"),
98 litWords("a{1..f}"),
99 },
100 {
101 litWord("a{c..f}"),
102 litWords("ac", "ad", "ae", "af"),
103 },
104 {
105 litWord("a{H..K}"),
106 litWords("aH", "aI", "aJ", "aK"),
107 },
108 {
109 litWord("a{-..f}"),
110 litWords("a{-..f}"),
111 },
112 {
113 litWord("a{3..-}"),
114 litWords("a{3..-}"),
115 },
116 {
117 litWord("a{1..10..3}"),
118 litWords("a1", "a4", "a7", "a10"),
119 },
120 {
121 litWord("a{1..4..0}"),
122 litWords("a1", "a2", "a3", "a4"),
123 },
124 {
125 litWord("a{4..1}"),
126 litWords("a4", "a3", "a2", "a1"),
127 },
128 {
129 litWord("a{4..1..-2}"),
130 litWords("a4", "a2"),
131 },
132 {
133 litWord("a{4..1..1}"),
134 litWords("a4", "a3", "a2", "a1"),
135 },
136 {
137 litWord("{1..005}"),
138 litWords("001", "002", "003", "004", "005"),
139 },
140 {
141 litWord("{0001..05..2}"),
142 litWords("0001", "0003", "0005"),
143 },
144 {
145 litWord("{0..1}"),
146 litWords("0", "1"),
147 },
148 {
149 litWord("{00..2}"),
150 litWords("00", "01", "02"),
151 },
152 {
153 litWord("{01..10}"),
154 litWords("01", "02", "03", "04", "05", "06", "07", "08", "09", "10"),
155 },
156 {
157 litWord("{-03..3..3}"),
158 litWords("-03", "000", "003"),
159 },
160 {
161 litWord("a{1..10..-3}"),
162 litWords("a1", "a4", "a7", "a10"),
163 },
164 {
165 litWord("a{10..1..3}"),
166 litWords("a10", "a7", "a4", "a1"),
167 },
168 {
169 litWord("a{d..k..3}"),
170 litWords("ad", "ag", "aj"),
171 },
172 {
173 litWord("a{d..k..n}"),
174 litWords("a{d..k..n}"),
175 },
176 {
177 litWord("a{k..d..-2}"),
178 litWords("ak", "ai", "ag", "ae"),
179 },
180 {
181 litWord("{1..1}"),
182 litWords("1"),
183 },
184 {
185 litWord("{1,2..3}"),
186 litWords("1", "2..3"),
187 },
188 {
189 litWord("{1..2,3}"),
190 litWords("1..2", "3"),
191 },
192 {
193 litWord("{1..2..3,4}"),
194 litWords("1..2..3", "4"),
195 },
196 {
197 litWord("a{1..2..3..4}"),
198 litWords("a{1..2..3..4}"),
199 },
200 }
201
202 func TestBraces(t *testing.T) {
203 t.Parallel()
204 for _, tc := range braceTests {
205 t.Run("", func(t *testing.T) {
206 inStr := printWords(tc.in)
207 wantStr := printWords(tc.want...)
208 wantBraceExpParts(t, tc.in, false)
209
210 inBraces := *tc.in
211 syntax.SplitBraces(&inBraces)
212 wantBraceExpParts(t, &inBraces, inStr != wantStr)
213
214 got := Braces(&inBraces)
215 gotStr := printWords(got...)
216 if gotStr != wantStr {
217 t.Fatalf("mismatch in %q\nwant:\n%s\ngot: %s",
218 inStr, wantStr, gotStr)
219 }
220 })
221 }
222 }
223
224 func TestBracesSeq(t *testing.T) {
225 t.Parallel()
226 for _, tc := range braceTests {
227 t.Run("", func(t *testing.T) {
228 inStr := printWords(tc.in)
229 wantStr := printWords(tc.want...)
230
231 inBraces := *tc.in
232 syntax.SplitBraces(&inBraces)
233
234 var got []*syntax.Word
235 for w, err := range BracesSeq(nil, &inBraces) {
236 if err != nil {
237 t.Fatalf("unexpected error on %q: %v", inStr, err)
238 }
239 got = append(got, w)
240 }
241 gotStr := printWords(got...)
242 if gotStr != wantStr {
243 t.Fatalf("mismatch in %q\nwant:\n%s\ngot: %s",
244 inStr, wantStr, gotStr)
245 }
246 })
247 }
248 }
249
250 func TestBracesSeqError(t *testing.T) {
251 t.Parallel()
252 tests := []string{
253 "{1..100000}",
254 "a{0..9223372036854775807}b",
255 "{-9223372036854775808..9223372036854775807}",
256 "{1..1000000000..1}",
257 "{1..100}{1..100}{1..100}",
258 "{a,b,c,d}{1..100}{1..100}{1..50}",
259 }
260 for _, in := range tests {
261 t.Run(in, func(t *testing.T) {
262 word := &syntax.Word{Parts: []syntax.WordPart{lit(in)}}
263 syntax.SplitBraces(word)
264 var gotErr error
265 for _, err := range BracesSeq(nil, word) {
266 if err != nil {
267 gotErr = err
268 break
269 }
270 }
271 if gotErr == nil {
272 t.Fatalf("expected error for %q", in)
273 }
274 })
275 }
276 }
277
278 func wantBraceExpParts(t *testing.T, word *syntax.Word, want bool) {
279 t.Helper()
280 anyBrace := false
281 for _, part := range word.Parts {
282 if _, anyBrace = part.(*syntax.BraceExp); anyBrace {
283 break
284 }
285 }
286 if anyBrace && !want {
287 t.Fatalf("didn't want any BraceExp node, but found one")
288 } else if !anyBrace && want {
289 t.Fatalf("wanted a BraceExp node, but found none")
290 }
291 }
292
293 func printWords(words ...*syntax.Word) string {
294 p := syntax.NewPrinter()
295 var buf bytes.Buffer
296 call := &syntax.CallExpr{Args: words}
297 p.Print(&buf, call)
298 return buf.String()
299 }