-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer_test.go
278 lines (223 loc) · 6.42 KB
/
tokenizer_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2023, Mikhail Kirillov <[email protected]>
package tokens_test
import (
"bytes"
"compress/gzip"
"log"
"strings"
"testing"
"github.com/stretchr/testify/require"
generic "github.com/wmentor/tokens"
)
func tF(t *testing.T, src string, caseSensitive bool, wait string) {
t.Helper()
res := make([]string, 0, len(wait))
opts := make([]generic.Option, 0, 1)
if caseSensitive {
opts = append(opts, generic.WithCaseSensitive())
}
parser, err := generic.New(strings.NewReader(src), opts...)
require.NoError(t, err)
defer parser.Close()
for {
tok, err := parser.Token()
if err != nil {
break
}
res = append(res, tok)
}
if strings.Join(res, "|") != wait {
t.Fatalf("test failed src=%s ret=%v wait=%v", src, res, wait)
}
}
func tFB(t *testing.T, src []byte, caseSensitive bool, wait string) {
t.Helper()
res := make([]string, 0, len(wait))
opts := make([]generic.Option, 0, 1)
if caseSensitive {
opts = append(opts, generic.WithCaseSensitive())
}
parser, err := generic.New(bytes.NewReader(src), opts...)
require.NoError(t, err)
defer parser.Close()
for {
tok, err := parser.Token()
if err != nil {
break
}
res = append(res, tok)
}
if strings.Join(res, "|") != wait {
t.Fatalf("test failed src=%s ret=%v wait=%v", src, res, wait)
}
}
func TestParser000(t *testing.T) {
t.Parallel()
tF(t, "", false, "")
}
func TestParser001(t *testing.T) {
t.Parallel()
tF(t, " \t ", false, "")
}
func TestParser002(t *testing.T) {
t.Parallel()
tF(t, "1", false, "1")
}
func TestParser003(t *testing.T) {
t.Parallel()
tF(t, " 1 2 3", false, "1|2|3")
}
func TestParser004(t *testing.T) {
t.Parallel()
tF(t, " 12 ", false, "12")
}
func TestParser005(t *testing.T) {
t.Parallel()
tF(t, "hello world!", false, "hello|world|!")
}
func TestParser006(t *testing.T) {
t.Parallel()
tF(t, "это ёлка", false, "это|елка")
}
func TestParser007(t *testing.T) {
t.Parallel()
tF(t, "зайди на mail.ru", false, "зайди|на|mail.ru")
}
func TestParser008(t *testing.T) {
t.Parallel()
tF(t, "летчик-испытатель выполнил сложный манёвр", false, "летчик-испытатель|выполнил|сложный|маневр")
}
func TestParser009(t *testing.T) {
t.Parallel()
tF(t, "Hello, world!", false, "hello|,|world|!")
}
func TestParser010(t *testing.T) {
t.Parallel()
tF(t, "Открой ссылку https://goprog.ru", false, "открой|ссылку|https://goprog.ru")
}
func TestParser011(t *testing.T) {
t.Parallel()
tF(t, "File:main.go", false, "file|:|main.go")
}
func TestParser012(t *testing.T) {
t.Parallel()
tF(t, "File:/ test", false, "file|:|/|test")
}
func TestParser013(t *testing.T) {
t.Parallel()
tF(t, "Read file:///test.txt and bashrc", false, "read|file:///test.txt|and|bashrc")
}
func TestParser014(t *testing.T) {
t.Parallel()
tF(t, "Поставь хэштег #приветмир!", false, "поставь|хэштег|#приветмир|!")
}
func TestParser015(t *testing.T) {
t.Parallel()
tF(t, "хэштеги: #приветмир#МИР#улет", false, "хэштеги|:|#приветмир|#мир|#улет")
}
func TestParser016(t *testing.T) {
t.Parallel()
tF(t, "ставь#тест в пост", false, "ставь|#тест|в|пост")
}
func TestParser017(t *testing.T) {
t.Parallel()
tF(t, "select model X/123.12. It's super", false, "select|model|x/123.12|.|it's|super")
}
func TestParser018(t *testing.T) {
t.Parallel()
tF(t, "Router IP is 192.168.1.1", false, "router|ip|is|192.168.1.1")
}
func TestParser019(t *testing.T) {
t.Parallel()
tF(t, "There're so many people", false, "there're|so|many|people")
}
func TestParser020(t *testing.T) {
t.Parallel()
tF(t, "At 5:30 am", false, "at|5:30|am")
}
func TestParser021(t *testing.T) {
t.Parallel()
tF(t, "01:12:25 is a good time", false, "01:12:25|is|a|good|time")
}
func TestParser022(t *testing.T) {
t.Parallel()
tF(t, "Москва—мой любимый город", false, "москва|-|мой|любимый|город")
}
func TestParser023(t *testing.T) {
t.Parallel()
tF(t, "Rune 'b' goes after 'a'.", false, "rune|'|b|'|goes|after|'|a|'|.")
}
func TestParser024(t *testing.T) {
t.Parallel()
tF(t, "Конь д'Артаньяна стоял в конюшне.", false, "конь|д'артаньяна|стоял|в|конюшне|.")
}
func TestParser025(t *testing.T) {
t.Parallel()
tF(t, "Конь д'Артаньяна стоял в конюшне.", true, "Конь|д'Артаньяна|стоял|в|конюшне|.")
}
func TestParser026(t *testing.T) {
t.Parallel()
tF(t, "Ops'", false, "ops|'")
}
func TestParser027(t *testing.T) {
t.Parallel()
tF(t, "Магазин L'Etoile", false, "магазин|l'etoile")
}
func TestParser028(t *testing.T) {
t.Parallel()
tF(t, "Капитан д`Артаньян", false, "капитан|д'артаньян")
}
func TestParser029(t *testing.T) {
t.Parallel()
tF(t, "Connect to 192.168.1.1:80", false, "connect|to|192.168.1.1:80")
}
func TestParser030(t *testing.T) {
t.Parallel()
tF(t, "д'Артаньян и д'Тревиль", false, "д'артаньян|и|д'тревиль")
}
func TestParser031(t *testing.T) {
t.Parallel()
tF(t, "My email is [email protected]", false, "my|email|is|[email protected]")
}
func TestParser032(t *testing.T) {
t.Parallel()
tF(t, "@test1 and @test2 work in London", false, "@test1|and|@test2|work|in|london")
}
func TestParser033(t *testing.T) {
t.Parallel()
tF(t, "Good day, @black_dragon!", false, "good|day|,|@black_dragon|!")
}
func TestParser034(t *testing.T) {
t.Parallel()
tF(t, "It's @madman", false, "it's|@madman")
}
func TestParser035(t *testing.T) {
t.Parallel()
tF(t, "Alvin’s chief pilot and the leader of the expedition", false,
"alvin's|chief|pilot|and|the|leader|of|the|expedition")
}
func TestParser036(t *testing.T) {
t.Parallel()
tF(t, "learn C++11 now", false, "learn|c|+|+|11|now")
}
func TestParser037(t *testing.T) {
t.Parallel()
tF(t, "победа муад'диба", false, "победа|муад'диба")
}
func TestParser038(t *testing.T) {
t.Parallel()
tF(t, "о’хара", false, "о'хара")
}
func TestParser039(t *testing.T) {
t.Parallel()
txt := "Working with gzip"
b := bytes.NewBuffer(nil)
gz := gzip.NewWriter(b)
if _, err := gz.Write([]byte(txt)); err != nil {
log.Fatal(err)
}
if err := gz.Close(); err != nil {
log.Fatal(err)
}
tFB(t, b.Bytes(), false, "working|with|gzip")
}