forked from lytics/confl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lex_test.go
707 lines (646 loc) · 14.1 KB
/
lex_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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package confl
import (
"testing"
u "github.com/araddon/gou"
)
var _ = u.EMPTY
// Test to make sure we get what we expect.
func expect(t *testing.T, lx *lexer, items []item) {
for i := 0; i < len(items); i++ {
item := lx.nextItem()
if item.typ == itemEOF {
break
} else if item.typ == itemError {
t.Fatal(item.val)
}
if item != items[i] {
//u.Debugf("\n\n%s wanted: \n%s\ngot: \n%s", label, wantStr, got)
wantStr := items[i].val
got := item.val
for pos, r := range wantStr {
if len(got)-1 < pos {
u.Warnf("len mismatch? %v vs %v", len(got), len(wantStr))
} else if r != rune(got[pos]) {
u.Warnf("mismatch at position: %v %q!=%q", pos, string(r), string(got[pos]))
break
} else {
//u.Debugf("match at position: %v %q=%q", pos, string(r), string(got[pos]))
}
}
t.Fatalf("Testing: '%s'\nExpected %q, received %q\n",
lx.input, items[i], item)
}
}
}
// Test to make sure we get what we expect.
func expectError(t *testing.T, conf string) {
lx := lex(conf)
for {
item := lx.nextItem()
if item.typ == itemEOF {
t.Fatalf("expected error got EOF for %v", conf)
break
} else if item.typ == itemError {
// Success
return
}
}
}
var errorVals = []string{
`//comment1
config : {
/ -- bad comment1
}`,
`//comment2
config : {
port: 8080
}/ -- bad comment2`,
`/comment3
port: 8080`,
`port: 8083 /badcomment4`,
`port: 8084 //goodcomment1
portx: 80/badcomment4`,
`:port: 8085`, // Can't start key with key seperator
`=port=8086`, // Can't start key with key seperator
`rate=.55`, // can't start values with .
`rate=
`, // can't start values with new line.
}
func TestLexErrors(t *testing.T) {
for _, v := range errorVals {
expectError(t, v)
}
}
func TestLexBadConfString(t *testing.T) {
lx := lex("\x0e9\xbd\xbf\xefr")
item := lx.nextItem()
if item.typ != itemError {
t.Fatal(item.val)
}
}
func TestLexSimpleKeyStringValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemString, "bar", 1},
{itemEOF, "", 1},
}
// Double quotes
lx := lex("foo = \"bar\"")
expect(t, lx, expectedItems)
// Single quotes
lx = lex("foo = 'bar'")
expect(t, lx, expectedItems)
// No spaces
lx = lex("foo='bar'")
expect(t, lx, expectedItems)
// NL
lx = lex("foo='bar'\r\n")
expect(t, lx, expectedItems)
// '''
expectedItems = []item{
{itemKey, "foo", 1},
{itemString, `json:"-"`, 1},
{itemEOF, "", 1},
}
lx = lex(`foo='''json:"-"'''`)
expect(t, lx, expectedItems)
// ''' Multi line
expectedItems = []item{
{itemKey, "foo", 1},
{itemString, `json:"-"
json:"-"
json:"-"`, 3},
{itemEOF, "", 1},
}
lx = lex(`foo='''json:"-"
json:"-"
json:"-"'''`)
expect(t, lx, expectedItems)
}
func TestLexSimpleKeyIntegerValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemInteger, "123", 1},
{itemEOF, "", 1},
}
lx := lex("foo = 123")
expect(t, lx, expectedItems)
lx = lex("foo=123")
expect(t, lx, expectedItems)
lx = lex("foo=123\r\n")
expect(t, lx, expectedItems)
}
func TestLexSimpleKeyFloatValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemFloat, "22.2", 1},
{itemEOF, "", 1},
}
lx := lex("foo = 22.2")
expect(t, lx, expectedItems)
lx = lex("foo=22.2")
expect(t, lx, expectedItems)
lx = lex("foo=22.2\r\n")
expect(t, lx, expectedItems)
}
func TestLexSimpleKeyBoolValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemBool, "true", 1},
{itemEOF, "", 1},
}
lx := lex("foo = true")
expect(t, lx, expectedItems)
lx = lex("foo=true")
expect(t, lx, expectedItems)
lx = lex("foo=true\r\n")
expect(t, lx, expectedItems)
lx = lex("foo=True")
expect(t, lx, []item{
{itemKey, "foo", 1},
{itemBool, "True", 1},
{itemEOF, "", 1},
})
}
func TestLexComments(t *testing.T) {
expectedItems := []item{
{itemCommentStart, "", 1},
{itemText, " This is a comment", 1},
{itemEOF, "", 1},
}
lx := lex("# This is a comment")
expect(t, lx, expectedItems)
lx = lex("# This is a comment\r\n")
expect(t, lx, expectedItems)
lx = lex("// This is a comment\r\n")
expect(t, lx, expectedItems)
}
func TestLexArrays(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemArrayStart, "", 1},
{itemInteger, "1", 1},
{itemInteger, "2", 1},
{itemInteger, "3", 1},
{itemString, "bar", 1},
{itemArrayEnd, "", 1},
{itemEOF, "", 1},
}
lx := lex("foo = [1, 2, 3, 'bar']")
expect(t, lx, expectedItems)
lx = lex("foo = [1,2,3,'bar']")
expect(t, lx, expectedItems)
lx = lex("foo = [1, 2,3,'bar']")
expect(t, lx, expectedItems)
}
var mlArray = `
# top level comment
foo = [
1, # One
2, // Two
3 , // Three
'bar' ,
"bar"
]
`
func TestLexMultilineArrays(t *testing.T) {
expectedItems := []item{
{itemCommentStart, "", 2},
{itemText, " top level comment", 2},
{itemKey, "foo", 3},
{itemArrayStart, "", 3},
{itemInteger, "1", 4},
{itemCommentStart, "", 4},
{itemText, " One", 4},
{itemInteger, "2", 5},
{itemCommentStart, "", 5},
{itemText, " Two", 5},
{itemInteger, "3", 6},
{itemCommentStart, "", 6},
{itemText, " Three", 6},
{itemString, "bar", 7},
{itemString, "bar", 8},
{itemArrayEnd, "", 9},
{itemEOF, "", 9},
}
lx := lex(mlArray)
expect(t, lx, expectedItems)
}
func TestLexUnterminated(t *testing.T) {
lx := lex("foo {\n name = 'bill' \n")
lx.nextItem() // foo
lx.nextItem() // map start = ""
lx.nextItem() // name
lx.nextItem() // bill
item := lx.nextItem()
if item.typ != itemError {
t.Errorf("Should be error for un terminated map: %v", item)
}
}
var mlArrayNoSep = `
# top level comment
foo = [
1
2
3
'bar'
"bar"
]
`
func TestLexMultilineArraysNoSep(t *testing.T) {
expectedItems := []item{
{itemCommentStart, "", 2},
{itemText, " top level comment", 2},
{itemKey, "foo", 3},
{itemArrayStart, "", 3},
{itemInteger, "1", 4},
{itemInteger, "2", 5},
{itemInteger, "3", 6},
{itemString, "bar", 7},
{itemString, "bar", 8},
{itemArrayEnd, "", 9},
{itemEOF, "", 9},
}
lx := lex(mlArrayNoSep)
expect(t, lx, expectedItems)
}
func TestLexSimpleMap(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemMapStart, "", 1},
{itemKey, "ip", 1},
{itemString, "127.0.0.1", 1},
{itemKey, "port", 1},
{itemInteger, "4242", 1},
{itemMapEnd, "", 1},
{itemEOF, "", 1},
}
lx := lex("foo = {ip='127.0.0.1', port = 4242}")
expect(t, lx, expectedItems)
}
var mlMap = `
foo = {
ip = '127.0.0.1'
# comment1
#comment2
port= 4242
// comment3
rate = 55
}
`
func TestLexMultilineMap(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 2},
{itemMapStart, "", 2},
{itemKey, "ip", 3},
{itemString, "127.0.0.1", 3},
{itemCommentStart, "", 4},
{itemText, " comment1", 4},
{itemCommentStart, "", 5},
{itemText, "comment2", 5},
{itemKey, "port", 6},
{itemInteger, "4242", 6},
{itemCommentStart, "", 7},
{itemText, " comment3", 7},
{itemKey, "rate", 8},
{itemInteger, "55", 8},
{itemMapEnd, "", 9},
{itemEOF, "", 9},
}
lx := lex(mlMap)
expect(t, lx, expectedItems)
}
var nestedMap = `
foo = {
host = {
ip = '127.0.0.1'
port= 4242
// rate comment
rate = 55
}
}
`
func TestLexNestedMaps(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 2},
{itemMapStart, "", 2},
{itemKey, "host", 3},
{itemMapStart, "", 3},
{itemKey, "ip", 4},
{itemString, "127.0.0.1", 4},
{itemKey, "port", 5},
{itemInteger, "4242", 5},
{itemCommentStart, "", 6},
{itemText, " rate comment", 6},
{itemKey, "rate", 7},
{itemInteger, "55", 7},
{itemMapEnd, "", 8},
{itemMapEnd, "", 9},
{itemEOF, "", 5},
}
lx := lex(nestedMap)
expect(t, lx, expectedItems)
}
func TestLexQuotedKeys(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemInteger, "123", 1},
{itemEOF, "", 1},
}
lx := lex("foo : 123")
expect(t, lx, expectedItems)
lx = lex("'foo' : 123")
expect(t, lx, expectedItems)
lx = lex("\"foo\" : 123")
expect(t, lx, expectedItems)
}
func TestLexQuotedKeysWithSpace(t *testing.T) {
expectedItems := []item{
{itemKey, " foo", 1},
{itemInteger, "123", 1},
{itemEOF, "", 1},
}
lx := lex("' foo' : 123")
expect(t, lx, expectedItems)
lx = lex("\" foo\" : 123")
expect(t, lx, expectedItems)
}
func TestLexColonKeySep(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemInteger, "123", 1},
{itemEOF, "", 1},
}
lx := lex("foo : 123")
expect(t, lx, expectedItems)
lx = lex("foo:123")
expect(t, lx, expectedItems)
lx = lex("foo: 123")
expect(t, lx, expectedItems)
lx = lex("foo: 123\r\n")
expect(t, lx, expectedItems)
}
func TestLexWhitespaceKeySep(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemInteger, "123", 1},
{itemEOF, "", 1},
}
lx := lex("foo 123")
expect(t, lx, expectedItems)
lx = lex("foo 123")
expect(t, lx, expectedItems)
lx = lex("foo\t123")
expect(t, lx, expectedItems)
lx = lex("foo\t\t123\r\n")
expect(t, lx, expectedItems)
}
var nestedWhitespaceMap = `
foo {
host {
ip = '127.0.0.1'
port= 4242
}
}
`
func TestLexNestedWhitespaceMaps(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 2},
{itemMapStart, "", 2},
{itemKey, "host", 3},
{itemMapStart, "", 3},
{itemKey, "ip", 4},
{itemString, "127.0.0.1", 4},
{itemKey, "port", 5},
{itemInteger, "4242", 5},
{itemMapEnd, "", 6},
{itemMapEnd, "", 7},
{itemEOF, "", 5},
}
lx := lex(nestedWhitespaceMap)
expect(t, lx, expectedItems)
}
// Not currently supported
var tableOfArrays = `
table [
[ 1, 2],
[ "a", "b"],
]
`
func TestLexTableOfArrays(t *testing.T) {
expectedItems := []item{
{itemKey, "table", 2},
{itemArrayStart, "", 2},
{itemArrayStart, "", 3},
{itemInteger, "1", 3},
{itemInteger, "2", 3},
{itemArrayEnd, "", 3},
{itemArrayStart, "", 4},
{itemString, "a", 4},
{itemString, "b", 4},
{itemArrayEnd, "", 4},
{itemArrayEnd, "", 5},
{itemEOF, "", 6},
}
lx := lex(tableOfArrays)
expect(t, lx, expectedItems)
}
var semicolons = `
foo = 123;
bar = 'baz';
baz = 'boo'
map {
id = 1;
}
`
func TestOptionalSemicolons(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 2},
{itemInteger, "123", 2},
{itemKey, "bar", 3},
{itemString, "baz", 3},
{itemKey, "baz", 4},
{itemString, "boo", 4},
{itemKey, "map", 5},
{itemMapStart, "", 5},
{itemKey, "id", 6},
{itemInteger, "1", 6},
{itemMapEnd, "", 7},
{itemEOF, "", 5},
}
lx := lex(semicolons)
expect(t, lx, expectedItems)
}
func TestLexSemicolonChaining(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemString, "1", 1},
{itemKey, "bar", 1},
{itemFloat, "2.2", 1},
{itemKey, "baz", 1},
{itemBool, "true", 1},
{itemEOF, "", 1},
}
lx := lex("foo='1'; bar=2.2; baz=true;")
expect(t, lx, expectedItems)
}
var noquotes = `
foo = 123
bar = baz
baz=boo
map {
id:one
id2 : onetwo
}
t true
f false
tstr "true"
tkey = two
fkey = five # This should be a string
`
func TestLexNonQuotedStrings(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 2},
{itemInteger, "123", 2},
{itemKey, "bar", 3},
{itemString, "baz", 3},
{itemKey, "baz", 4},
{itemString, "boo", 4},
{itemKey, "map", 5},
{itemMapStart, "", 5},
{itemKey, "id", 6},
{itemString, "one", 6},
{itemKey, "id2", 7},
{itemString, "onetwo", 7},
{itemMapEnd, "", 8},
{itemKey, "t", 9},
{itemBool, "true", 9},
{itemKey, "f", 10},
{itemBool, "false", 10},
{itemKey, "tstr", 11},
{itemString, "true", 11},
{itemKey, "tkey", 12},
{itemString, "two", 12},
{itemKey, "fkey", 13},
{itemString, "five", 13},
{itemCommentStart, "", 13},
{itemText, " This should be a string", 13},
{itemEOF, "", 14},
}
lx := lex(noquotes)
expect(t, lx, expectedItems)
}
func TestLexMapQuotedKeys(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemMapStart, "", 1},
{itemKey, "bar", 1},
{itemInteger, "4242", 1},
{itemMapEnd, "", 1},
{itemEOF, "", 1},
}
lx := lex("foo = {'bar' = 4242}")
expect(t, lx, expectedItems)
lx = lex("foo = {\"bar\" = 4242}")
expect(t, lx, expectedItems)
}
func TestLexSpecialCharsMapQuotedKeys(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemMapStart, "", 1},
{itemKey, "bar-1.2.3", 1},
{itemMapStart, "", 1},
{itemKey, "port", 1},
{itemInteger, "4242", 1},
{itemMapEnd, "", 1},
{itemMapEnd, "", 1},
{itemEOF, "", 1},
}
lx := lex("foo = {'bar-1.2.3' = { port:4242 }}")
expect(t, lx, expectedItems)
lx = lex("foo = {\"bar-1.2.3\" = { port:4242 }}")
expect(t, lx, expectedItems)
}
var mlnestedmap = `
systems {
allinone {
description: "This is a description."
}
}
`
func TestLexDoubleNestedMapsNewLines(t *testing.T) {
expectedItems := []item{
{itemKey, "systems", 2},
{itemMapStart, "", 2},
{itemKey, "allinone", 3},
{itemMapStart, "", 3},
{itemKey, "description", 4},
{itemString, "This is a description.", 4},
{itemMapEnd, "", 5},
{itemMapEnd, "", 6},
{itemEOF, "", 7},
}
lx := lex(mlnestedmap)
expect(t, lx, expectedItems)
}
var blockexample = `
numbers (
1234567890
)
`
func TestLexBlockString(t *testing.T) {
expectedItems := []item{
{itemKey, "numbers", 2},
{itemString, "1234567890", 3},
}
lx := lex(blockexample)
expect(t, lx, expectedItems)
}
func TestLexBlockStringEOF(t *testing.T) {
expectedItems := []item{
{itemKey, "numbers", 2},
{itemString, "1234567890", 3},
}
blockbytes := []byte(blockexample[0 : len(blockexample)-1])
blockbytes = append(blockbytes, 0)
lx := lex(string(blockbytes))
expect(t, lx, expectedItems)
}
var mlblockexample = `
numbers (
12(34)56
(
7890
)
)
`
var mlBlockTextVal = ` 12(34)56
(
7890
)`
func TestLexBlockStringMultiLine(t *testing.T) {
expectedItems := []item{
{itemKey, "numbers", 2},
{itemString, mlBlockTextVal, 6},
}
lx := lex(mlblockexample)
expect(t, lx, expectedItems)
}
var mlblockCRLFExample = "numbers (\r\n20060102\r\n)\r\n"
func TestLexBlockStringCRLF(t *testing.T) {
expectedItems := []item{
{itemKey, "numbers", 1},
{itemString, "20060102", 2},
}
lx := lex(mlblockCRLFExample)
expect(t, lx, expectedItems)
}
var mlblockLFExample = "numbers (\n20060102\n)\n"
func TestLexBlockStringLF(t *testing.T) {
expectedItems := []item{
{itemKey, "numbers", 1},
{itemString, "20060102", 2},
}
lx := lex(mlblockCRLFExample)
expect(t, lx, expectedItems)
}