-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoleri_test.go
354 lines (312 loc) · 15.1 KB
/
goleri_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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package goleri
import (
"fmt"
"reflect"
"regexp"
"testing"
)
func assertEquals(t *testing.T, a interface{}, b interface{}) {
t.Helper()
if reflect.TypeOf(a).Kind() == reflect.Slice {
va := reflect.ValueOf(a)
vb := reflect.ValueOf(b)
if va.Len() == 0 && vb.Len() == 0 {
return
}
}
if !reflect.DeepEqual(a, b) {
t.Fatalf("%s != %s", a, b)
}
}
func parse(t *testing.T, g *Grammar, s string) *Result {
t.Helper()
res, err := g.Parse(s)
if err != nil {
t.Fatal(err)
}
return res
}
func TestKeyword(t *testing.T) {
hi := NewKeyword(0, "hi", false)
grammar := NewGrammar(hi, nil)
// assert statements
assertEquals(t, 0, hi.Gid())
assertEquals(t, "hi", hi.GetKeyword())
assertEquals(t, false, hi.IsIgnCase())
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, true, parse(t, grammar, " hi ").IsValid())
assertEquals(t, false, parse(t, grammar, "Hi").IsValid())
assertEquals(t, false, parse(t, grammar, "hello").IsValid())
assertEquals(t, "<Keyword gid:0 keyword:hi>", hi.String())
assertEquals(t, []Element{}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 2, parse(t, grammar, "hi").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "hi").Tree().String())
}
func TestKeywordIgnCase(t *testing.T) {
hi := NewKeyword(0, "hi", true)
grammar := NewGrammar(hi, nil)
// assert statements
assertEquals(t, 0, hi.Gid())
assertEquals(t, true, hi.IsIgnCase())
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, true, parse(t, grammar, " hi ").IsValid())
assertEquals(t, true, parse(t, grammar, "Hi").IsValid())
assertEquals(t, false, parse(t, grammar, "hello").IsValid())
assertEquals(t, "<Keyword gid:0 keyword:hi>", hi.String())
assertEquals(t, []Element{}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
}
func TestSequence(t *testing.T) {
hi := NewKeyword(0, "hi", false)
iris := NewKeyword(0, "iris", false)
sequence := NewSequence(0, hi, iris)
grammar := NewGrammar(sequence, nil)
// assert statements
assertEquals(t, 0, sequence.Gid())
assertEquals(t, true, parse(t, grammar, "hi iris").IsValid())
assertEquals(t, true, parse(t, grammar, " hi iris ").IsValid())
assertEquals(t, false, parse(t, grammar, "hi siri").IsValid())
assertEquals(t, false, parse(t, grammar, "hello iris").IsValid())
assertEquals(t, "<Sequence gid:0 elements:[<Keyword gid:0 keyword:hi> <Keyword gid:0 keyword:iris>]>", sequence.String())
assertEquals(t, []Element{}, parse(t, grammar, "hi iris").GetExpecting())
assertEquals(t, []Element{iris}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 7, parse(t, grammar, "hi iris").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "hi iris").Tree().String())
}
func TestChoiceMostGreedy(t *testing.T) {
hi := NewKeyword(0, "hi", false)
iris := NewKeyword(0, "iris", false)
sequence := NewSequence(0, hi, iris)
choice := NewChoice(0, true, hi, sequence)
grammar := NewGrammar(choice, nil)
// assert statements
assertEquals(t, true, choice.IsMostGreedy())
assertEquals(t, 0, choice.Gid())
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, true, parse(t, grammar, "hi iris").IsValid())
assertEquals(t, false, parse(t, grammar, "hi siri").IsValid())
assertEquals(t, "<Choice gid:0 elements:[<Keyword gid:0 keyword:hi> <Sequence gid:0 elements:[<Keyword gid:0 keyword:hi> <Keyword gid:0 keyword:iris>]>]>", choice.String())
assertEquals(t, []Element{}, parse(t, grammar, "hi iris").GetExpecting())
assertEquals(t, []Element{iris}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 2, parse(t, grammar, "hi").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "hi").Tree().String())
}
func TestChoiceFirstMatch(t *testing.T) {
hi := NewKeyword(0, "hi", false)
iris := NewKeyword(0, "iris", false)
sequence := NewSequence(0, hi, iris)
choice := NewChoice(0, false, hi, sequence)
grammar := NewGrammar(choice, nil)
// assert statements
assertEquals(t, 0, choice.Gid())
assertEquals(t, false, choice.IsMostGreedy())
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, false, parse(t, grammar, "hi iris").IsValid())
assertEquals(t, false, parse(t, grammar, "hi siri").IsValid())
assertEquals(t, "<Choice gid:0 elements:[<Keyword gid:0 keyword:hi> <Sequence gid:0 elements:[<Keyword gid:0 keyword:hi> <Keyword gid:0 keyword:iris>]>]>", choice.String())
assertEquals(t, []Element{EOS}, parse(t, grammar, "hi iris").GetExpecting())
assertEquals(t, []Element{}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
}
func TestOptional(t *testing.T) {
hi := NewKeyword(0, "hi", false)
optional := NewOptional(0, hi)
grammar := NewGrammar(optional, nil)
// assert statements
assertEquals(t, 0, optional.Gid())
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, true, parse(t, grammar, "").IsValid())
assertEquals(t, false, parse(t, grammar, "hello").IsValid())
assertEquals(t, "<Optional gid:0 elem:<Keyword gid:0 keyword:hi>>", optional.String())
assertEquals(t, []Element{}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 2, parse(t, grammar, "hi").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "hi").Tree().String())
}
func TestToken(t *testing.T) {
token := NewToken(0, "+")
grammar := NewGrammar(token, nil)
// assert statements
assertEquals(t, 0, token.Gid())
assertEquals(t, "+", token.GetToken())
assertEquals(t, true, parse(t, grammar, "+").IsValid())
assertEquals(t, true, parse(t, grammar, " + ").IsValid())
assertEquals(t, false, parse(t, grammar, "++").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, "<Token gid:0 token:+>", token.String())
assertEquals(t, "+", token.Text())
assertEquals(t, []Element{}, parse(t, grammar, "+").GetExpecting())
assertEquals(t, []Element{token}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 1, parse(t, grammar, "+").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "+").Tree().String())
}
func TestTokenMultiChars(t *testing.T) {
token := NewToken(0, "+=")
grammar := NewGrammar(token, nil)
// assert statements
assertEquals(t, 0, token.Gid())
assertEquals(t, true, parse(t, grammar, "+=").IsValid())
assertEquals(t, true, parse(t, grammar, " += ").IsValid())
assertEquals(t, false, parse(t, grammar, "+").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, "<Token gid:0 token:+=>", token.String())
assertEquals(t, "+=", token.Text())
assertEquals(t, []Element{}, parse(t, grammar, "+=").GetExpecting())
assertEquals(t, []Element{token}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 2, parse(t, grammar, "+=").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "+=").Tree().String())
}
func TestTokens(t *testing.T) {
tokens := NewTokens(0, "== != >= > < <=")
grammar := NewGrammar(tokens, nil)
// assert statements
assertEquals(t, 0, tokens.Gid())
assertEquals(t, []string{"==", "!=", ">=", "<=", ">", "<"}, tokens.GetTokens()) // ??
assertEquals(t, true, parse(t, grammar, "==").IsValid())
assertEquals(t, true, parse(t, grammar, "<=").IsValid())
assertEquals(t, true, parse(t, grammar, ">").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, "<Tokens gid:0 tokens:[== != >= <= > <]>", tokens.String())
assertEquals(t, []Element{}, parse(t, grammar, "==").GetExpecting())
assertEquals(t, []Element{tokens}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 2, parse(t, grammar, "==").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "==").Tree().String())
}
func TestList(t *testing.T) {
hi := NewKeyword(0, "hi", false)
token := NewToken(0, ",")
list := NewList(0, hi, token, 1, 3, false)
grammar := NewGrammar(list, nil)
// assert statements
assertEquals(t, 0, list.Gid())
assertEquals(t, false, list.IsOptClose())
assertEquals(t, 1, list.GetMin())
assertEquals(t, 3, list.GetMax())
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, true, parse(t, grammar, "hi, hi, hi").IsValid())
assertEquals(t, true, parse(t, grammar, "hi , hi , hi").IsValid())
assertEquals(t, false, parse(t, grammar, "hi , hi , hi,").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, false, parse(t, grammar, "hi, hi, hi, hi").IsValid())
assertEquals(t, false, parse(t, grammar, "hi hi").IsValid())
assertEquals(t, false, parse(t, grammar, ", ").IsValid())
assertEquals(t, "<List gid:0 elem:<Keyword gid:0 keyword:hi> delimiter:<Token gid:0 token:,>>", list.String())
assertEquals(t, []Element{token}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 10, parse(t, grammar, "hi, hi, hi").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "hi, hi, hi").Tree().String())
}
func TestListEndDelimiter(t *testing.T) {
hi := NewKeyword(0, "hi", true)
token := NewToken(0, ",")
list := NewList(0, hi, token, 1, 3, true)
grammar := NewGrammar(list, nil)
// assert statements
assertEquals(t, 0, list.Gid())
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, true, parse(t, grammar, "hi, hi, hi").IsValid())
assertEquals(t, true, parse(t, grammar, "hi , hi , hi").IsValid())
assertEquals(t, true, parse(t, grammar, "hi , hi , hi,").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, false, parse(t, grammar, "hi, hi, hi, hi").IsValid())
assertEquals(t, false, parse(t, grammar, "hi hi").IsValid())
assertEquals(t, false, parse(t, grammar, ", ").IsValid())
assertEquals(t, "<List gid:0 elem:<Keyword gid:0 keyword:hi> delimiter:<Token gid:0 token:,>>", list.String())
assertEquals(t, []Element{token}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
}
func TestRepeat(t *testing.T) {
hi := NewKeyword(0, "hi", true)
repeat := NewRepeat(0, hi, 1, 3)
grammar := NewGrammar(repeat, nil)
// assert statements
assertEquals(t, 0, repeat.Gid())
assertEquals(t, 1, repeat.GetMin())
assertEquals(t, 3, repeat.GetMax())
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, true, parse(t, grammar, "hi hi hi").IsValid())
assertEquals(t, true, parse(t, grammar, "hi hi hi").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, false, parse(t, grammar, "hi hi hi hi").IsValid())
assertEquals(t, "<Repeat gid:0 elem:<Keyword gid:0 keyword:hi>>", repeat.String())
assertEquals(t, []Element{hi}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 8, parse(t, grammar, "hi hi hi").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "hi hi hi").Tree().String())
}
func TestRegex(t *testing.T) {
regex := NewRegex(0, regexp.MustCompile(`^(/[^/\\]*(?:\\.[^/\\]*)*/i?)`))
grammar := NewGrammar(regex, nil)
// assert statements
assertEquals(t, 0, regex.Gid())
assertEquals(t, regexp.MustCompile(`^(/[^/\\]*(?:\\.[^/\\]*)*/i?)`), regex.GetRegex()) // ??
assertEquals(t, true, parse(t, grammar, "/hi/").IsValid())
assertEquals(t, true, parse(t, grammar, "/hi/i").IsValid())
assertEquals(t, true, parse(t, grammar, " //i").IsValid())
assertEquals(t, false, parse(t, grammar, "x//i").IsValid())
assertEquals(t, false, parse(t, grammar, "/hi//hi/i").IsValid())
assertEquals(t, false, parse(t, grammar, "//x").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, `<Regex gid:0 regex:^(/[^/\\]*(?:\\.[^/\\]*)*/i?)>`, regex.String())
assertEquals(t, []Element{}, parse(t, grammar, "/hi/i").GetExpecting())
assertEquals(t, []Element{regex}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 5, parse(t, grammar, "/hi/i").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "/hi/i").Tree().String())
}
func TestRef(t *testing.T) {
ref := NewRef()
hi := NewKeyword(0, "hi", false)
grammar := NewGrammar(ref, nil)
// assert statements (before set)
assertEquals(t, false, ref.IsSet())
assertEquals(t, "<Ref isSet:false>", ref.String())
ref.Set(hi)
// assert statements
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, true, ref.IsSet())
assertEquals(t, "<Ref isSet:true>", ref.String())
assertEquals(t, []Element{}, parse(t, grammar, "hi").GetExpecting())
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 2, parse(t, grammar, "hi").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "hi").Tree().String())
}
func TestPrio(t *testing.T) {
prio := NewPrio(1,
NewKeyword(0, "hi", false),
NewKeyword(0, "bye", false),
NewSequence(0, NewToken(0, "("), THIS, NewToken(0, ")")),
NewSequence(0, THIS, NewKeyword(0, "or", false), THIS),
NewSequence(0, THIS, NewKeyword(0, "and", false), THIS))
grammar := NewGrammar(prio, nil)
// assert statements
assertEquals(t, true, parse(t, grammar, "hi").IsValid())
assertEquals(t, true, parse(t, grammar, "(bye)").IsValid())
assertEquals(t, true, parse(t, grammar, "(hi and bye)").IsValid())
assertEquals(t, true, parse(t, grammar, "(hi or hi) and (hi or hi)").IsValid())
assertEquals(t, true, parse(t, grammar, "(hi or (hi and bye))").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, false, parse(t, grammar, "(hi").IsValid())
assertEquals(t, false, parse(t, grammar, "()").IsValid())
assertEquals(t, false, parse(t, grammar, "(hi or hi) and").IsValid())
_, err := grammar.Parse("(((((((((((((((((((((((((((((((((((((((((((((((((((hi)))))))))))))))))))))))))))))))))))))))))))))))))))")
assertEquals(t, fmt.Errorf("max recursion depth (50) is reached"), err)
assertEquals(t, "<Prio gid:1 elements:[<Keyword gid:0 keyword:hi> <Keyword gid:0 keyword:bye> <Sequence gid:0 elements:[<Token gid:0 token:(> <This> <Token gid:0 token:)>]> <Sequence gid:0 elements:[<This> <Keyword gid:0 keyword:or> <This>]> <Sequence gid:0 elements:[<This> <Keyword gid:0 keyword:and> <This>]>]>", prio.String())
assertEquals(t, 0, parse(t, grammar, "").Pos())
assertEquals(t, 25, parse(t, grammar, "(hi or hi) and (hi or hi)").Pos())
assertEquals(t, "<Node elem:<nil> children:1>", parse(t, grammar, "(hi or hi) and (hi or hi)").Tree().String())
}