-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpr_test.go
439 lines (369 loc) · 9.49 KB
/
expr_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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package exprel
import (
"bytes"
"errors"
"regexp"
"testing"
)
func TestEmpty(t *testing.T) {
expr := ``
testString(t, expr, "", nil)
}
func TestSimple(t *testing.T) {
expr := `Hello World!`
testString(t, expr, "Hello World!", nil)
}
func TestEvaluate(t *testing.T) {
data := map[string]interface{}{
"name": "Tim",
}
result, err := Evaluate(`=LOWER(name) & ".jpg"`, SourceMap(data))
if err != nil {
t.Fatal(err)
}
filename := result.(string)
const expecting = "tim.jpg"
if filename != expecting {
t.Fatalf("got %s, expecting %s\n", filename, expecting)
}
}
func TestOverflow(t *testing.T) {
var b bytes.Buffer
{
b.WriteByte('=')
const max = 1000000
for i := 0; i < max; i++ {
b.WriteByte('(')
}
b.WriteString("1 + 1")
for i := 0; i < max; i++ {
b.WriteByte(')')
}
}
testSyntaxError(t, b.String(), "maximum depth", nil)
}
func TestErrSyntax(t *testing.T) {
expr := `=5 + $`
testSyntaxError(t, expr, "expected character", nil)
}
func TestErrRuntime(t *testing.T) {
expr := `=5 + "hello"`
testRuntimeError(t, expr, "invalid.*operand", nil)
}
func TestConcat(t *testing.T) {
expr := `="Hello" & "\n" & "World"`
testString(t, expr, "Hello\nWorld", nil)
}
func TestSingleNumber(t *testing.T) {
expr := `=12345`
testNumber(t, expr, 12345, nil)
}
func TestNumberExpression(t *testing.T) {
expr := `=5+5*2/0.5`
testNumber(t, expr, 5+5*2/0.5, nil)
}
func TestNumberExpression_issue3(t *testing.T) {
expr := `=6*32+1`
testNumber(t, expr, 6*32+1, nil)
}
func TestSimpleIf(t *testing.T) {
expr := `=IF(TRUE();FALSE();2)`
testBool(t, expr, false, nil)
}
func TestIf(t *testing.T) {
expr := `=IF( 5 >= 3; 3; 5)`
testNumber(t, expr, 3, nil)
}
func TestAndShort(t *testing.T) {
fns := SourceMap{
"FUNC": func(...interface{}) (interface{}, error) {
return nil, errors.New("FUNC should not be called")
},
}
expr := `=AND(TRUE(); 5 = 2; FUNC())`
testBool(t, expr, false, fns)
}
func TestAnd(t *testing.T) {
expr := `=AND(TRUE(); 5 > 2; "hey" = "hey")`
testBool(t, expr, true, nil)
}
func TestOrShort(t *testing.T) {
fns := SourceMap{
"FUNC": func(*Call) (interface{}, error) {
return nil, errors.New("FUNC should not be called")
},
}
expr := `=OR(FALSE(); 5 >= 2; FUNC())`
testBool(t, expr, true, fns)
}
func TestOr(t *testing.T) {
fns := SourceMap{
"FUNC": func(*Call) (interface{}, error) {
return false, nil
},
}
expr := `=OR(FALSE(); 5 = 2; FUNC())`
testBool(t, expr, false, fns)
}
func TestUnaryMinus(t *testing.T) {
expr := `=1 - -4`
testNumber(t, expr, 1 - -4, nil)
}
func TestModulo(t *testing.T) {
expr := `=0 % 5`
testNumber(t, expr, 0, nil)
expr = `=5 % 2`
testNumber(t, expr, 1, nil)
expr = `=1 % 1`
testNumber(t, expr, 0, nil)
}
func TestNoArgFunction(t *testing.T) {
fns := SourceMap{
"SAYHELLO": func(*Call) (interface{}, error) {
return "Hello World!", nil
},
}
expr := `=SAYHELLO()`
testString(t, expr, "Hello World!", fns)
}
func TestMapSource(t *testing.T) {
m := map[string]interface{}{
"A": float64(123),
"B": float64(456),
}
expr := `=A + B`
testNumber(t, expr, 123+456, SourceMap(m))
}
func TestBuiltinNOT(t *testing.T) {
expr := `=NOT(TRUE())`
testBool(t, expr, false, nil)
}
func TestBaseCHOOSE(t *testing.T) {
expr := `=CHOOSE(1; 10; 20; 30)`
testNumber(t, expr, 20, Base)
}
func TestBaseTYPE(t *testing.T) {
expr := `=TYPE(123)`
testNumber(t, expr, 1, Base)
expr = `=TYPE("hello")`
testNumber(t, expr, 2, Base)
expr = `=TYPE(TRUE())`
testNumber(t, expr, 4, Base)
}
func TestBaseABS(t *testing.T) {
expr := `=ABS(-342)`
testNumber(t, expr, 342, Base)
}
func TestBaseSIGN(t *testing.T) {
expr := `=SIGN(-34)`
testNumber(t, expr, -1, Base)
expr = `=SIGN(0)`
testNumber(t, expr, 0, Base)
expr = `=SIGN(242342)`
testNumber(t, expr, 1, Base)
}
func TestBaseLN(t *testing.T) {
expr := `=LN(1)`
testNumber(t, expr, 0, Base)
expr = `=LN(EXP(1))`
testNumber(t, expr, 1, Base)
}
func TestBaseLOG10(t *testing.T) {
expr := `=LOG10(10)`
testNumber(t, expr, 1, Base)
expr = `=LOG10(100)`
testNumber(t, expr, 2, Base)
}
func TestBaseCHAR(t *testing.T) {
expr := `=CHAR(72; 101; 108; 108; 111; 33)`
testString(t, expr, "Hello!", Base)
expr = `=CHAR()`
testString(t, expr, "", Base)
}
func TestBaseJOIN(t *testing.T) {
expr := `=JOIN(", "; "a"; "b"; "c")`
testString(t, expr, "a, b, c", Base)
expr = `=JOIN("!!!")`
testString(t, expr, "", Base)
}
func TestBaseLEFT(t *testing.T) {
expr := `=LEFT("hello")`
testString(t, expr, "h", Base)
expr = `=LEFT("hello";10)`
testString(t, expr, "hello", Base)
}
func TestBaseLEN(t *testing.T) {
expr := `=LEN("hélloworld")`
testNumber(t, expr, float64(len("hélloworld")), Base)
}
func TestBaseLOWER(t *testing.T) {
expr := `=LOWER("hey" & "THERE")`
testString(t, expr, "heythere", Base)
}
func TestBaseMID(t *testing.T) {
expr := `=MID("hello world";1;5)`
testString(t, expr, "hello", Base)
expr = `=MID("hello world";-5;3)`
testString(t, expr, "", Base)
expr = `=MID("hello world";20)`
testString(t, expr, "", Base)
expr = `=MID("hello world";7;1)`
testString(t, expr, "w", Base)
expr = `=MID("hello world";7;100)`
testString(t, expr, "world", Base)
}
func TestBaseREPT(t *testing.T) {
expr := `=REPT("1"; 5)`
testString(t, expr, "11111", Base)
}
func TestBaseRIGHT(t *testing.T) {
expr := `=RIGHT("hello")`
testString(t, expr, "o", Base)
expr = `=RIGHT("hello";3)`
testString(t, expr, "llo", Base)
expr = `=RIGHT("hello";10)`
testString(t, expr, "hello", Base)
}
func TestBaseSEARCH(t *testing.T) {
expr := `=SEARCH("e"; "hello world")`
testNumber(t, expr, 2, Base)
expr = `=SEARCH("z"; "hello world")`
testNumber(t, expr, -1, Base)
expr = `=SEARCH("e"; "hello world"; 2)`
testNumber(t, expr, 2, Base)
expr = `=SEARCH("e"; "hello world"; 3)`
testNumber(t, expr, -1, Base)
expr = `=SEARCH("e"; "hello world"; 100)`
testNumber(t, expr, -1, Base)
}
func TestBaseTRIM(t *testing.T) {
expr := `=TRIM(" hello world ")`
testString(t, expr, "hello world", Base)
}
func TestBaseUPPER(t *testing.T) {
expr := `=UPPER("hey" & "THERE")`
testString(t, expr, "HEYTHERE", Base)
}
// testing helpers
func testSyntaxError(t *testing.T, expr, messageRegex string, source Source) {
_, err := Parse(expr)
if err == nil {
t.Fatalf("expecting parsing error\n")
}
syntaxErr, ok := err.(*SyntaxError)
if !ok {
t.Fatalf("expecting syntax error\n")
}
matched, err := regexp.MatchString(messageRegex, syntaxErr.Message)
if err != nil {
panic(err)
}
if !matched {
t.Fatalf("error message does not match (regex `%s`, got `%s`)", messageRegex, syntaxErr.Message)
}
}
func testRuntimeError(t *testing.T, expr, messageRegex string, source Source) {
e, err := Parse(expr)
if err != nil {
t.Fatalf("could not parse expression: %s\n", err)
}
_, err = e.Evaluate(source)
if err == nil {
t.Fatalf("expecting runtime error\n")
}
runtimeErr, ok := err.(*RuntimeError)
if !ok {
t.Fatalf("expecting runtime error\n")
}
matched, err := regexp.MatchString(messageRegex, runtimeErr.Message)
if err != nil {
panic(err)
}
if !matched {
t.Fatalf("error message does not match (regex `%s`, got `%s`)", messageRegex, runtimeErr.Message)
}
}
func testString(t *testing.T, expr, expected string, source Source) {
e, err := Parse(expr)
if err != nil {
t.Fatalf("could not parse expression: %s\n", err)
}
val, err := String(e.Evaluate(source))
if err != nil {
t.Fatalf("could not evaluate expression: %s\n", err)
}
if val != expected {
t.Fatalf("incorrect value (expecting `%s`, got `%s`)\n", expected, val)
}
raw, err := e.MarshalText()
if err != nil {
t.Fatalf("MarshalText error: %s\n", err)
}
var e2 Expression
if err := e2.UnmarshalText(raw); err != nil {
t.Fatalf("UnmarshalText error: %s\n", err)
}
val2, err := String(e2.Evaluate(source))
if err != nil {
t.Fatalf("could not evaluate unmarshaled expression: %s\n", err)
}
if val2 != expected {
t.Fatalf("incorrect unmarshaled value (expecting `%s`, got `%s`)\n", expected, val)
}
}
func testNumber(t *testing.T, expr string, expected float64, source Source) {
e, err := Parse(expr)
if err != nil {
t.Fatalf("could not parse expression: %s\n", err)
}
val, err := Number(e.Evaluate(source))
if err != nil {
t.Fatalf("could not evaluate expression: %s\n", err)
}
if val != expected {
t.Fatalf("incorrect value (expecting %f, got %f)\n", expected, val)
}
raw, err := e.MarshalText()
if err != nil {
t.Fatalf("MarshalText error: %s\n", err)
}
var e2 Expression
if err := e2.UnmarshalText(raw); err != nil {
t.Fatalf("UnmarshalText error: %s\n", err)
}
val2, err := Number(e2.Evaluate(source))
if err != nil {
t.Fatalf("could not evaluate unmarshaled expression: %s\n", err)
}
if val2 != expected {
t.Fatalf("incorrect unmarshaled value (expecting %f, got %f)\n", expected, val)
}
}
func testBool(t *testing.T, expr string, expected bool, source Source) {
e, err := Parse(expr)
if err != nil {
t.Fatalf("could not parse expression: %s\n", err)
}
val, err := Boolean(e.Evaluate(source))
if err != nil {
t.Fatalf("could not evaluate expression: %s\n", err)
}
if val != expected {
t.Fatalf("incorrect value (expecting %v, got %v)\n", expected, val)
}
raw, err := e.MarshalText()
if err != nil {
t.Fatalf("MarshalText error: %s\n", err)
}
var e2 Expression
if err := e2.UnmarshalText(raw); err != nil {
t.Fatalf("UnmarshalText error: %s\n", err)
}
val2, err := Boolean(e2.Evaluate(source))
if err != nil {
t.Fatalf("could not evaluate unmarshaled expression: %s\n", err)
}
if val2 != expected {
t.Fatalf("incorrect unmarshaled value (expecting %v, got %v)\n", expected, val)
}
}