-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser_test.go
432 lines (413 loc) · 9.12 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package codf // import "go.spiff.io/codf"
import (
"reflect"
"strings"
"testing"
"time"
)
func parse(in string) (*Document, error) {
r := strings.NewReader(in)
l := NewLexer(r)
p := NewParser()
if err := p.Parse(l); err != nil {
return nil, err
}
return p.Document(), nil
}
func mustParse(t *testing.T, in string) *Document {
doc, err := parse(in)
if err != nil {
t.Fatalf("Parse(..) error = %v; want nil", err)
}
t.Logf("-------- DOCUMENT --------\n%s\n------ END DOCUMENT ------", doc)
return doc
}
func mustParseNamed(t *testing.T, name string, in string) *Document {
doc := mustParse(t, in)
doc.Name = name
return doc
}
func mustNotParse(t *testing.T, in string) *Document {
doc, err := parse(in)
if err == nil {
t.Fatalf("Parse(..) error = %v; want error", err)
}
t.Logf("Parse(..) error = %v", err)
return doc
}
// parseTestCase is used to describe and run a parser test, optionally as a subtest.
type parseTestCase struct {
Name string
Src string
Doc *Document
Fun func(*testing.T, string) *Document
}
func (p parseTestCase) RunSubtest(t *testing.T) {
t.Run(p.Name, p.Run)
}
func (p parseTestCase) Run(t *testing.T) {
fn := p.Fun
if fn == nil {
fn = mustParse
}
doc := fn(t, p.Src)
objectsEqual(t, "", doc, p.Doc)
}
func TestParseAST(t *testing.T) {
cases := []parseTestCase{
{
Name: "Empty",
Src: "",
Doc: doc(),
},
{
Name: "EmptyArrayMap",
Src: `foo 1 { bar [] #{}; } map #{} [];// eof`,
Doc: doc().section("foo", 1).
statement("bar", []ExprNode{}, mkmap()).
up().statement("map", mkmap(), []ExprNode{}).
Doc(),
},
{
Name: "NestedArrayMaps",
Src: `foo [// Nested map inside array
#{ k [ 1 // integer
"2" // string
three // bareword (string)
true // bool (bareword->bool)
` + "`true`" + ` // raw quote bool
]
` + "`raw`" + ` bare
}
];`,
Doc: doc().statement("foo", mkexpr([]ExprNode{
mkmap(
"k", mkexpr(mkexprs(1, "2", "three", true, "true")),
"raw", "bare",
),
})).Doc(),
},
{
Name: "MinimalSpace",
Src: `sect []#{}{stmt #{k [2]"p"#{}}true [false];}`,
Doc: doc().section("sect", mkexprs(), mkmap()).
statement("stmt", mkmap("k", mkexprs(2), "p", mkmap()), true, mkexprs(false)).
Doc(),
},
{
Name: "AllLiterals",
Src: `
stmt
yes no
true false
1234 -1.234
0600 0b101
16#ffff 0xFFFF
"\u1234` + "\n" + `\x00"
"foo" bar
0.5h30s0.5s500us0.5us1ns
#/foobar/ #//
0/1 120/4
#{} #{k v}
[] [v1 v2]
{
inside Yes YES yes yeS // Last is always a bareword here
No NO no nO
True TRUE true truE
False FALSE false falsE;
}
`,
Doc: doc().section("stmt",
true, false,
true, false,
1234, mkdec("-1.234"),
0600, 5,
0xffff, 0xffff,
"\u1234\n\x00",
"foo", "bar",
time.Hour/2+time.Minute/2+time.Second/2+time.Millisecond/2+time.Microsecond/2+time.Nanosecond,
mkregex("foobar"), mkregex(""),
mkrat(0, 1), mkrat(30, 1),
mkmap(), mkmap("k", "v"),
mkexprs(), mkexprs("v1", "v2"),
).statement("inside",
true, true, true, "yeS",
false, false, false, "nO",
true, true, true, "truE",
false, false, false, "falsE",
).Doc(),
},
{Fun: mustNotParse, Name: "BadMapClose", Src: `src #{;};`},
{Fun: mustNotParse, Name: "BadMapClose", Src: `src #{ k };`},
{Fun: mustNotParse, Name: "BadMapClose", Src: `src #{ 1234 five };`},
{Fun: mustNotParse, Name: "BadMapClose", Src: `src #{ k ];`},
{Fun: mustNotParse, Name: "BadMapClose", Src: `src #{];`},
{Fun: mustNotParse, Name: "BadArrayClose", Src: `src [;];`},
{Fun: mustNotParse, Name: "BadArrayClose", Src: `src [};`},
{Fun: mustNotParse, Name: "BadStatementClose", Src: `src };`},
{Fun: mustNotParse, Name: "BadStatementClose", Src: `src ];`},
{Fun: mustNotParse, Name: "BadStatementClose", Src: `src`},
{Fun: mustNotParse, Name: "BadSectionClose", Src: `src {`},
{Fun: mustNotParse, Name: "BadSectionClose", Src: `src {]`},
{Fun: mustNotParse, Name: "BadSectionClose", Src: `src { // comment`},
{Fun: mustNotParse, Name: "BadSectionClose", Src: `}`},
{Fun: mustNotParse, Name: "BadSectionClose", Src: `]`},
}
for _, c := range cases {
c.RunSubtest(t)
}
}
func TestParseExample(t *testing.T) {
const exampleSource = `server go.spiff.io {
listen 0.0.0.0:80;
control unix:///var/run/httpd.sock;
proxy unix:///var/run/go-redirect.sock {
strip-x-headers yes;
log-access no;
}
// keep caches in 64mb of memory
cache memory 64mb {
expire 10m 404;
expire 1h 301 302;
expire 5m 200;
}
}`
parseTestCase{
Src: exampleSource,
Doc: doc().
section("server", "go.spiff.io").
/* server */ statement("listen", "0.0.0.0:80").
/* server */ statement("control", "unix:///var/run/httpd.sock").
/* server */ section("proxy", "unix:///var/run/go-redirect.sock").
/* server */ /* proxy */ statement("strip-x-headers", true).
/* server */ /* proxy */ statement("log-access", false).
/* server */ up().
/* server */ section("cache", "memory", "64mb").
/* server */ /* cache */ statement("expire", time.Minute*10, 404).
/* server */ /* cache */ statement("expire", time.Hour, 301, 302).
/* server */ /* cache */ statement("expire", time.Minute*5, 200).
Doc(),
}.Run(t)
}
func TestParseEmpty(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
objectsEqual(t, "",
mustParse(t, ""),
new(Document),
)
})
t.Run("Whitespace", func(t *testing.T) {
objectsEqual(t, "",
mustParse(t, " "),
new(Document),
)
})
t.Run("Whitespaces", func(t *testing.T) {
objectsEqual(t, "",
mustParse(t, "\t \t\n\r\n\r\n \n "),
new(Document),
)
})
t.Run("Semicolon", func(t *testing.T) {
objectsEqual(t, "",
mustParse(t, ";"),
new(Document),
)
})
t.Run("Semicolons", func(t *testing.T) {
objectsEqual(t, "",
mustParse(t, ";;;;;;;"),
new(Document),
)
})
t.Run("Mixed", func(t *testing.T) {
objectsEqual(t, "",
mustParse(t, " ;\n\t; ;\n;"),
new(Document),
)
})
}
func TestParseExpr(t *testing.T) {
type testCase struct {
name string
in string
want ExprNode
wantErr bool
}
cases := []testCase{
{
name: "QuotedString",
in: ` "quoted string" // comment // `,
want: mkexpr("quoted string"),
},
{
name: "Raw String",
in: " `raw string` // comment //",
want: mkexpr("raw string"),
},
{
name: "Positive Digit",
in: "5",
want: mkexpr(5),
},
{
name: "Positive Int",
in: "123",
want: mkexpr(123),
},
{
name: "Negative Digit",
in: "-1",
want: mkexpr(-1),
},
{
name: "Negative Int",
in: "-123",
want: mkexpr(-123),
},
{
name: "Zero",
in: "0",
want: mkexpr(0),
},
{
name: "Float Zero",
in: "0.0",
want: mkdec("0.0"),
},
{
name: "Float Positive",
in: "+1.234",
want: mkdec("1.234"),
},
{
name: "Float Negative",
in: "-1.234",
want: mkdec("-1.234"),
},
{
name: "Float Negative Exp",
in: "-1.234e2",
want: mkdec("-123.4"),
},
{
name: "Float Bad Exp",
in: "-1.234E",
want: mkword("-1.234E"),
},
{
name: "Yes",
in: "yes",
want: mkexpr(true),
},
{
name: "No",
in: "no",
want: mkexpr(false),
},
{
name: "True",
in: "TRUE",
want: mkexpr(true),
},
{
name: "False",
in: "FALSE",
want: mkexpr(false),
},
{
name: "EmptyArray",
in: "[]",
want: mkexpr([]ExprNode{}),
},
{
name: "Array",
in: "\n[ 1 2 3 ]\n",
want: mkexpr(mkexprs(1, 2, 3)),
},
{
name: "Map",
in: "#{ foo 5 }",
want: mkmap("foo", 5),
},
{
name: "Semicolon",
in: "1;",
wantErr: true,
},
{
name: "MapDoubleClose",
in: "#{ foo 5 }}",
wantErr: true,
},
{
name: "ArrayDoubleClose",
in: "[1 2 3]]",
wantErr: true,
},
{
name: "MultipleLiterals",
in: "1\n2\n3\n",
wantErr: true,
},
{
name: "Empty",
in: "",
wantErr: true,
},
{
name: "Whitespace",
in: " \n ",
wantErr: true,
},
{
name: "SpaceComment",
in: " \n // foobar\n //bar\n\n\t\n",
wantErr: true,
},
{
name: "Comment",
in: "// comment",
wantErr: true,
},
{
name: "KeyWithoutValue",
in: "#{key}",
wantErr: true,
},
{
name: "BadMapClosing",
in: "#{key value]",
wantErr: true,
},
{
name: "BadMapKey",
in: "#{1 value}",
wantErr: true,
},
}
parser := NewParser()
init := *parser
init.doc = &Document{
Children: []Node{},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
lexer := NewLexer(strings.NewReader(c.in))
got, err := parser.ParseExpr(lexer)
if (err != nil) != c.wantErr {
t.Fatalf("error parsing %q; expected err = %t; got %v", c.in, c.wantErr, err)
} else if c.wantErr {
return
}
objectsEqual(t, "", got, c.want)
})
}
t.Run("EnsureParserUnchanged", func(t *testing.T) {
objectsEqual(t, "", init.doc, parser.doc)
if !reflect.DeepEqual(parser, &init) {
t.Fatalf("parser state changed\ngot\t%#+ v\nwant\t%#+ v", parser, &init)
}
})
}