-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex_test.go
100 lines (92 loc) · 2.18 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
package parser
import (
"log"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
)
type lexTest struct {
name string
input string
toks []token
}
func mkItem(typ tokType, pos int, text string) token {
return token{
typ: typ,
pos: pos,
val: []rune(text),
}
}
var lexTests = []lexTest{
{"at without username in spoiler", "||`||||@||", []token{
mkItem(tokSpoiler, 0, "||"),
mkItem(tokBacktick, 2, "`"),
mkItem(tokSpoiler, 3, "||"),
mkItem(tokSpoiler, 5, "||"),
mkItem(tokAt, 7, "@"),
mkItem(tokSpoiler, 8, "||"),
mkItem(tokEOF, 10, ""),
}},
{"emote with trailing", "PEPE0", []token{
mkItem(tokWord, 0, "PEPE0"),
mkItem(tokEOF, 5, ""),
}},
{"text with code", "text `with code`", []token{
mkItem(tokWord, 0, "text"),
mkItem(tokWhitespace, 4, " "),
mkItem(tokBacktick, 5, "`"),
mkItem(tokWord, 6, "with"),
mkItem(tokWhitespace, 10, " "),
mkItem(tokWord, 11, "code"),
mkItem(tokBacktick, 15, "`"),
mkItem(tokEOF, 16, ""),
}},
{"underscores", "words_with_underscores", []token{
mkItem(tokWord, 0, "words_with_underscores"),
mkItem(tokEOF, 22, ""),
}},
{"emoji", "🙈🙉🙊", []token{
mkItem(tokWord, 0, "🙈🙉🙊"),
mkItem(tokEOF, 3, ""),
}},
{"non ascii words", "日本語のテキスト", []token{
mkItem(tokWord, 0, "日本語のテキスト"),
mkItem(tokEOF, 8, ""),
}},
{"more unicode", "DŽ‱ஹ௸௵꧄.ဪ꧅⸻𒈙𒐫﷽", []token{
mkItem(tokWord, 0, "DŽ"),
mkItem(tokPunct, 2, "‱"),
mkItem(tokWord, 3, "ஹ௸௵꧄"),
mkItem(tokPunct, 7, "."),
mkItem(tokWord, 8, "ဪ꧅"),
mkItem(tokPunct, 10, "⸻"),
mkItem(tokWord, 11, "𒈙𒐫﷽"),
mkItem(tokEOF, 14, ""),
}},
{"at", "@", []token{
mkItem(tokAt, 0, "@"),
mkItem(tokEOF, 1, ""),
}},
}
func TestLex(t *testing.T) {
for _, test := range lexTests {
toks := lex(test.input)
if len(test.toks) != 0 {
if !reflect.DeepEqual(test.toks, toks) {
t.Errorf("%s: got\n%s\nexpected\n%s", test.name, spew.Sdump(toks), spew.Sdump(test.toks))
}
} else {
log.Println(spew.Sdump(toks))
}
}
}
func lex(input string) (tokens []token) {
l := NewLexer(input)
for {
t := l.Next()
tokens = append(tokens, t)
if t.typ == tokEOF {
return
}
}
}