-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_test.go
70 lines (59 loc) · 1.12 KB
/
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
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
package functionalstatemachineexample
import (
"io"
"strings"
"testing"
"github.com/matryer/is"
)
var (
tests = []struct {
input string
expected []Token
}{
{
`1 + 23 - 345 "56 + 7" "" 3 - 2`,
makeTokens(
Int, "1",
Plus, "+",
Int, "23",
Minus, "-",
Int, "345",
String, "56 + 7",
String, "",
Int, "3",
Minus, "-",
Int, "2",
),
},
}
)
func TestTokensTraditional(t *testing.T) {
testTokens(TokensTraditional, t)
}
func TestTokensFunctional(t *testing.T) {
testTokens(TokensFunctional, t)
}
func testTokens(lexerFunc func(r io.Reader, ch chan<- Token), t *testing.T) {
t.Helper()
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
is := is.New(t)
r := strings.NewReader(test.input)
ch := make(chan Token, 100)
lexerFunc(r, ch)
close(ch)
tokens := []Token{}
for t := range ch {
tokens = append(tokens, t)
}
is.Equal(tokens, test.expected)
})
}
}
func makeTokens(v ...string) []Token {
tokens := make([]Token, len(v)/2)
for i := 0; i < len(v)/2; i++ {
tokens[i] = makeToken(v[i*2], v[i*2+1])
}
return tokens
}