-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexl_test.go
executable file
·319 lines (304 loc) · 5.82 KB
/
regexl_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
package regexl
import (
"testing"
)
// @TODO: Add 1+ matching strings for each positive case to be tested with Regexp.MatchString
func TestMain(t *testing.T) {
testCases := []struct {
desc string
rl Regexl
expectedRegex string
shouldError bool
}{
{
desc: "Simplest",
rl: Regexl{
Query: `
set_options({
find_all_matches: false,
})
select 'friend'
`,
},
expectedRegex: "(?i)friend",
},
{
desc: "One func",
rl: Regexl{
Query: `
set_options({
find_all_matches: true,
})
// We can accept any number of inputs here!
select any_strings_of('is', 'Omar')
`,
},
expectedRegex: "(?i)is|Omar",
},
{
desc: "Multiple object params",
rl: Regexl{
Query: `
set_options({
find_all_matches: true,
case_sensitive: false,
})
select any_chars_of('is', 'omar') // Comments work here too
`,
},
expectedRegex: "(?i)[isomar]",
},
{
desc: "Func: starts_with",
rl: Regexl{
Query: `
// /^friend/i
// Strings that can match:
// 'Friend, how are you?'
set_options({
case_sensitive: false,
})
select starts_with('friend')
`,
},
expectedRegex: "(?i)^friend",
},
{
desc: "Func: ends_with",
rl: Regexl{
Query: `
// /omar$/i
// Strings that can match:
// 'Hello there, friend! This is Omar'
set_options({
case_sensitive: false,
})
select ends_with('omar')
`,
},
expectedRegex: "(?i)omar$",
},
{
desc: "Func: zero_plus_of",
rl: Regexl{
Query: `
// /Hello*/g
// Equivalent to: /Hello*/g
// Strings that can match:
// 'Hello there, friend!'
// 'Hell there, friend!'
// 'Hellooooo there, friend!'
set_options({
find_all_matches: true,
})
select 'Hell' + zero_plus_of('o')
`,
},
expectedRegex: "(?i)Hell(?:o)*",
},
{
desc: "Func: one_plus_of",
rl: Regexl{
Query: `
// /Hell(o)+/g
// Equivalent to: /Hello+/g
// Strings that can match:
// 'Hello there, friend!'
// 'Helloooo' will match but not 'Hell'
set_options({
find_all_matches: true,
})
select 'Hell' + one_plus_of('o')
`,
},
expectedRegex: "(?i)Hell(?:o)+",
},
{
desc: "Nested funcs",
rl: Regexl{
Query: `
set_options({
case_sensitive: true,
})
// Alternative: select starts_and_ends_with('Golang')
select ends_with(starts_with('Golang'))
`,
},
expectedRegex: "(?)^Golang$",
},
{
desc: "Combined funcs 1",
rl: Regexl{
Query: `
set_options({
find_all_matches: true,
})
select starts_with('Hello') + any_chars() + 'Omar'
`,
},
expectedRegex: "(?i)^Hello.*Omar",
},
{
desc: "Combined funcs 2",
rl: Regexl{
Query: `
set_options({
find_all_matches: true,
case_sensitive: false,
})
select starts_with('Hello there, ') + one_plus_of(any_chars_of(from_to('A', 'Z'), '.!-'))
`,
},
expectedRegex: "(?i)^Hello there, (?:[A-Z\\.!-])+",
},
{
desc: "Email query",
rl: Regexl{
Query: `
set_options({
case_sensitive: false,
})
select
// Converts to: [A-Z0-9._%+-]+
one_plus_of(
any_chars_of(from_to('A', 'Z'), from_to(0, 9), '._%+-')
) +
// Converts to: @
'@' +
// Converts to: [A-Z0-9.-]+
one_plus_of(
any_chars_of(from_to('A','Z'), from_to(0, 9), '.-')
) +
// Converts to: \.
'.' +
// Converts to: [A-Z]{2,10}
count_between(
any_chars_of(from_to('A', 'Z')),
2,
10
)
`,
},
expectedRegex: "(?i)(?:[A-Z0-9\\._%+-])+@(?:[A-Z0-9\\.-])+\\.[A-Z]{2,10}",
},
{
desc: "Crazy formatting 1",
rl: Regexl{
Query: `
set_options ( {
find_all_matches : true ,
} )
select starts_with( 'Hello' ) +any_chars ( )+ 'Omar'
`,
},
expectedRegex: "(?i)^Hello.*Omar",
},
{
desc: "Crazy formatting 2",
rl: Regexl{
Query: `
set_options (
{
find_all_matches : true}
)
select starts_with( 'Hello' ) +any_chars ( )+ 'Omar'
`,
},
expectedRegex: "(?i)^Hello.*Omar",
},
{
desc: "Crazy formatting 3 - one line",
rl: Regexl{
Query: `
set_options({find_all_matches: true}) select starts_with('Hello') + any_chars() + 'Omar'
`,
},
expectedRegex: "(?i)^Hello.*Omar",
},
//
// Negative test cases
//
{
desc: "Invalid 1",
rl: Regexl{
Query: `
set_options({
find_all_matches: false,
})
select 'friend
`,
},
shouldError: true,
},
{
desc: "Invalid 2",
rl: Regexl{
Query: `
set_options({
find_all_matches:,
})
select 'friend'
`,
},
shouldError: true,
},
{
desc: "Invalid 3",
rl: Regexl{
Query: `
set_options({
find_all_matches: galse,
})
select 'friend'
`,
},
shouldError: true,
},
{
desc: "Invalid 4",
rl: Regexl{
Query: `
set_options({
find_all_matches: false,
})
'friend'
`,
},
shouldError: true,
},
{
desc: "Invalid 5",
rl: Regexl{
Query: `
set_options({
find_all_matches: false,
})
select
`,
},
shouldError: true,
},
}
for _, tc := range testCases {
success := t.Run(tc.desc, func(t *testing.T) {
err := tc.rl.Compile()
if err != nil {
if tc.shouldError {
return
}
t.Errorf("Compilation failed. Err=%v; Query=%s\n", err, tc.rl.Query)
return
}
if tc.shouldError {
t.Errorf("Compilation should have thrown an error but didn't. Query=%s\n", tc.rl.Query)
return
}
if tc.expectedRegex != tc.rl.CompiledRegexp.String() {
t.Errorf("Compiled regex does not equal expected regex. Expected=%s; Compiled=%s\n", tc.expectedRegex, tc.rl.CompiledRegexp.String())
}
})
if !success {
break
}
}
}