-
Notifications
You must be signed in to change notification settings - Fork 31
/
parser_test.go
57 lines (49 loc) · 1.02 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
package uci
import (
"fmt"
"testing"
)
func TestParser(t *testing.T) {
for i := range parserTests {
tc := parserTests[i]
t.Run(tc.name, func(t *testing.T) {
testParser(t, tc.name, tc.input, tc.expected)
})
}
}
func testParser(t *testing.T, name, input string, expected []token) {
t.Helper()
var i int
ok := scan(name, input).each(func(tok token) bool {
if dump["token"] {
fmt.Println(tok) //nolint:forbidigo
}
if i >= len(expected) {
t.Errorf("token %d, unexpected item: %s", i, tok)
return false
}
if ex := expected[i]; tok.typ != ex.typ || !equalItemList(tok.items, ex.items) {
t.Errorf("token %d\nexpected %s\ngot %s", i, ex, tok)
return false
}
i++
return true
})
if !ok {
return
}
if l := len(expected); i != l {
t.Errorf("expected to scan %d token, actually scanned %d", l, i)
}
}
func equalItemList(a, b []item) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i].typ != b[i].typ || a[i].val != b[i].val {
return false
}
}
return true
}