forked from digineo/go-uci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer_test.go
44 lines (38 loc) · 867 Bytes
/
lexer_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
package uci
import (
"fmt"
"testing"
)
func TestLexer(t *testing.T) {
for i := range lexerTests {
tc := lexerTests[i]
t.Run(tc.name, func(t *testing.T) {
testLexer(t, tc.name, tc.input, tc.expected)
})
}
}
func testLexer(t *testing.T, name, input string, expected []item) {
t.Helper()
if dump["lex"] {
defer fmt.Println("") //nolint:forbidigo
}
l := lex(name, input)
var i int
for it := l.nextItem(); it.typ != itemEOF; it = l.nextItem() {
if dump["lex"] {
fmt.Print(it, " ") //nolint:forbidigo
}
if i >= len(expected) {
t.Errorf("token %d, unexpected item: %s", i, it)
return
}
if ex := expected[i]; it.typ != ex.typ || it.val != ex.val {
t.Errorf("token %d, expected %s, got %s", i, ex, it)
return
}
i++
}
if l := len(expected); i != l {
t.Errorf("expected to lex %d items, actually lexed %d", l, i)
}
}