-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.go
81 lines (75 loc) · 1.65 KB
/
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
package scan
import (
"strings"
"testing"
)
type Test struct {
src string
toks []Token
errs [][]string
}
func NewTest(src string, val string, line int, col int, type_ string) Test {
return Test{
src: src,
toks: []Token{{
Val: val,
Type: type_,
Pos: Pos{
Line: line,
Col: col,
}},
},
errs: [][]string{nil},
}
}
func (t Test) WithError(message string) Test {
last := len(t.toks) - 1
t.errs[last] = append(t.errs[last], message)
return t
}
func (t Test) And(val string, line int, col int, type_ string) Test {
t.toks = append(t.toks, Token{
Val: val,
Type: type_,
Pos: Pos{
Line: line,
Col: col,
},
})
t.errs = append(t.errs, nil)
return t
}
func RunTests(t *testing.T, rules RuleSet, tests []Test) {
for _, test := range tests {
t.Run(test.src, func(t *testing.T) {
scan := NewScannerFromString("", test.src)
r := NewRunner(scan, rules)
toks := r.All()
t.Log("\n" + FormatTokenTable(toks))
for i, tok := range toks {
if i >= len(test.toks) {
break
}
testTok := test.toks[i]
if tok.Val != testTok.Val || tok.Type != testTok.Type || tok.Pos != testTok.Pos {
t.Fatalf("\n have: %v \n want: %v", tok, testTok)
}
if len(test.errs) > 0 {
have := tok.Errs.Error()
want := strings.Join(test.errs[i], "\n")
if have != want {
t.Fatalf("\n have errors: %v\n want errors: %v", have, want)
}
}
}
if len(toks) < len(test.toks) {
idx := len(toks)
t.Errorf("not enough tokens, missing: %v", test.toks[idx:])
}
if len(toks) > len(test.toks) {
idx := len(test.toks)
t.Errorf("too many tokens, extra: %v", toks[idx:])
}
})
}
}