-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gal_test.go
471 lines (388 loc) · 13.1 KB
/
gal_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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package gal_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/seborama/gal/v9"
)
func TestEval(t *testing.T) {
expr := `-1 + 2 * 3 / 2 + 3 ** 2 -8`
val := gal.Parse(expr).Eval()
assert.Equal(t, gal.NewNumberFromInt(3).String(), val.String())
expr = `-"123"+"100"`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.NewNumberFromInt(-23).String(), val.String())
expr = `1-2+7<<4+5`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.NewNumberFromInt((1-2+7)<<(4+5)).String(), val.String())
expr = `-1-2-7<<4+5`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.NewNumberFromInt((-1-2-7)<<(4+5)).String(), val.String())
expr = `-100*2*7+1>>2+3`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.NewNumberFromInt((-100*2*7+1)>>(2+3)).String(), val.String())
expr = `100*2*7+1>>2+3`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.NewNumberFromInt((100*2*7+1)>>(2+3)).String(), val.String())
expr = `2+Factorial(4)-5`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.NewNumberFromInt(21).String(), val.String())
}
func TestTreeBuilder_FromExpr_Variables(t *testing.T) {
vars := gal.Variables{
":var1:": gal.NewNumberFromInt(4), // TODO: remove the need to surround with `:`?
":var2:": gal.NewNumberFromInt(3),
}
expr := `2 + :var1: * :var2: - 5`
got := gal.Parse(expr).Eval(gal.WithVariables(vars))
expected := gal.NewNumberFromInt(9)
if !cmp.Equal(expected, got) {
t.Error(cmp.Diff(expected, got))
}
}
func TestTreeBuilder_FromExpr_UnknownVariable(t *testing.T) {
expr := `2 + :var1: * :var2: - 5`
got := gal.Parse(expr).Eval()
expected := gal.NewUndefinedWithReasonf("syntax error: unknown variable name: ':var1:'")
if !cmp.Equal(expected, got) {
t.Error(cmp.Diff(expected, got))
}
}
func TestEval_Boolean(t *testing.T) {
expr := `2 > 1`
val := gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `2 > 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.False.String(), val.String())
expr = `2 >= 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `2 < 1`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.False.String(), val.String())
expr = `2 < 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.False.String(), val.String())
expr = `2 <= 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `2 != 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.False.String(), val.String())
expr = `1 != 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `3 != 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `2 == 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `1 == 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.False.String(), val.String())
expr = `3 == 2`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.False.String(), val.String())
expr = `( 123 == 123 && 12 <= 45 ) Or ( "a" != "b" )`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `( 123 == 123 && 12 <= 45 ) Or ( "b" != "b" )`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `( 123 == 123 && 12 > 45 ) Or ( "b" == "b" )`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `( 123 == 123 And 12 > 45 ) Or ( "b" != "b" )`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.False.String(), val.String())
expr = `True Or False`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
expr = `True Or (False)`
val = gal.Parse(expr).Eval()
assert.Equal(t, gal.True.String(), val.String())
// in this expression, the `()` are attached to `Or` which makes `Or()` a user-defined
// function, rather than the `Or` operator.
expr = `True Or(False)`
val = gal.Parse(expr).Eval()
assert.Equal(t, `undefined: unknown function 'Or'`, val.String())
}
func TestWithVariablesAndFunctions(t *testing.T) {
expr := `double(:val1:) + triple(:val2:)`
parsedExpr := gal.Parse(expr)
// step 1: define funcs and vars and Eval the expression
funcs := gal.Functions{
"double": func(args ...gal.Value) gal.Value {
if len(args) != 1 {
return gal.NewUndefinedWithReasonf("double() requires a single argument, got %d", len(args))
}
value, ok := args[0].(gal.Numberer)
if !ok {
return gal.NewUndefinedWithReasonf("double(): syntax error - argument must be a number-like value, got '%v'", args[0])
}
return value.Number().Multiply(gal.NewNumberFromInt(2))
},
"triple": func(args ...gal.Value) gal.Value {
if len(args) != 1 {
return gal.NewUndefinedWithReasonf("triple() requires a single argument, got %d", len(args))
}
value, ok := args[0].(gal.Numberer)
if !ok {
return gal.NewUndefinedWithReasonf("triple(): syntax error - argument must be a number-like value, got '%v'", args[0])
}
return value.Number().Multiply(gal.NewNumberFromInt(3))
},
}
vars := gal.Variables{
":val1:": gal.NewNumberFromInt(4),
":val2:": gal.NewNumberFromInt(5),
}
got := parsedExpr.Eval(
gal.WithVariables(vars),
gal.WithFunctions(funcs),
)
expected := gal.NewNumberFromInt(23)
if !cmp.Equal(expected, got) {
t.Error(cmp.Diff(expected, got))
}
// step 2: re-define funcs and vars and Eval the expression again
// note that we do not need to parse the expression again, only just evaluate it
funcs = gal.Functions{
"double": func(args ...gal.Value) gal.Value {
// should first validate argument count here
value := args[0].(gal.Numberer) // should check type assertion is ok here
return value.Number().Divide(gal.NewNumberFromInt(2))
},
"triple": func(args ...gal.Value) gal.Value {
// should first validate argument count here
value := args[0].(gal.Numberer) // should check type assertion is ok here
return value.Number().Divide(gal.NewNumberFromInt(3))
},
}
vars = gal.Variables{
":val1:": gal.NewNumberFromInt(2),
":val2:": gal.NewNumberFromInt(6),
}
got = parsedExpr.Eval(
gal.WithVariables(vars),
gal.WithFunctions(funcs),
)
expected = gal.NewNumberFromInt(3)
if !cmp.Equal(expected, got) {
t.Error(cmp.Diff(expected, got))
}
}
func TestNestedFunctions(t *testing.T) {
expr := `double(triple(7))`
parsedExpr := gal.Parse(expr)
// step 1: define funcs and vars and Eval the expression
funcs := gal.Functions{
"double": func(args ...gal.Value) gal.Value {
if len(args) != 1 {
return gal.NewUndefinedWithReasonf("double() requires a single argument, got %d", len(args))
}
value, ok := args[0].(gal.Numberer)
if !ok {
return gal.NewUndefinedWithReasonf("double(): syntax error - argument must be a number-like value, got '%v'", args[0])
}
return value.Number().Multiply(gal.NewNumberFromInt(2))
},
"triple": func(args ...gal.Value) gal.Value {
if len(args) != 1 {
return gal.NewUndefinedWithReasonf("triple() requires a single argument, got %d", len(args))
}
value, ok := args[0].(gal.Numberer)
if !ok {
return gal.NewUndefinedWithReasonf("triple(): syntax error - argument must be a number-like value, got '%v'", args[0])
}
return value.Number().Multiply(gal.NewNumberFromInt(3))
},
}
got := parsedExpr.Eval(
gal.WithFunctions(funcs),
)
expected := gal.NewNumberFromInt(42)
assert.Equal(t, expected.String(), got.String())
}
// If renaming this test, also update the README.md file, where it is mentioned.
func TestMultiValueFunctions(t *testing.T) {
expr := `sum(div(triple(7) double(4)))`
// step 1: define funcs and vars and Eval the expression
funcs := gal.Functions{
"double": func(args ...gal.Value) gal.Value {
if len(args) != 1 {
return gal.NewUndefinedWithReasonf("double() requires a single argument, got %d", len(args))
}
value, ok := args[0].(gal.Numberer)
if !ok {
return gal.NewUndefinedWithReasonf("double(): syntax error - argument must be a number-like value, got '%v'", args[0])
}
return value.Number().Multiply(gal.NewNumberFromInt(2))
},
"triple": func(args ...gal.Value) gal.Value {
if len(args) != 1 {
return gal.NewUndefinedWithReasonf("triple() requires a single argument, got %d", len(args))
}
value, ok := args[0].(gal.Numberer)
if !ok {
return gal.NewUndefinedWithReasonf("triple(): syntax error - argument must be a number-like value, got '%v'", args[0])
}
return value.Number().Multiply(gal.NewNumberFromInt(3))
},
"div": func(args ...gal.Value) gal.Value {
// returns the division of value1 by value2 as the interger portion and the remainder
if len(args) != 2 {
return gal.NewUndefinedWithReasonf("div() requires two arguments, got %d", len(args))
}
dividend := args[0].(gal.Numberer).Number()
divisor := args[1].(gal.Numberer).Number()
quotient := dividend.Divide(divisor).(gal.Numberer).Number().IntPart()
remainder := dividend.Number().Sub(quotient.(gal.Number).Multiply(divisor.Number()))
return gal.NewMultiValue(quotient, remainder)
},
"sum": func(args ...gal.Value) gal.Value {
// NOTE: we convert the args to a MultiValue to make this function "bilingual".
// That way, it can receiv either two Numberer's or one single MultiValue that holds 2 Numberer's.
var margs gal.MultiValue
if len(args) == 1 {
margs = args[0].(gal.MultiValue) // not checking type satisfaction for simplicity
}
if len(args) == 2 {
margs = gal.NewMultiValue(args...)
}
if margs.Size() != 2 {
return gal.NewUndefinedWithReasonf("sum() requires either two Numberer-type Value's or one MultiValue holdings 2 Numberer's, as arguments, but got %d arguments", margs.Size())
}
value1 := args[0].(gal.MultiValue).Get(0).(gal.Numberer)
value2 := args[0].(gal.MultiValue).Get(1).(gal.Numberer)
return value1.Number().Add(value2.Number())
},
}
got := gal.Parse(expr).Eval(
gal.WithFunctions(funcs),
)
expected := gal.NewNumberFromInt(7)
assert.Equal(t, expected.String(), got.String())
}
func TestStringsWithSpaces(t *testing.T) {
expr := `"ab cd" + "ef gh"`
parsedExpr := gal.Parse(expr)
got := parsedExpr.Eval()
assert.Equal(t, `"ab cdef gh"`, got.String())
}
func TestFunctionsAndStringsWithSpaces(t *testing.T) {
expr := `f("ab cd") + f("ef gh")`
parsedExpr := gal.Parse(expr)
got := parsedExpr.Eval(
gal.WithFunctions(gal.Functions{
"f": func(args ...gal.Value) gal.Value {
if len(args) != 1 {
return gal.NewUndefinedWithReasonf("f() requires a single argument, got %d", len(args))
}
return args[0]
},
}),
)
assert.Equal(t, `"ab cdef gh"`, got.String())
}
func TestObjects_Properties(t *testing.T) {
expr := `aCar.MaxSpeed - aCar.Speed`
parsedExpr := gal.Parse(expr)
got := parsedExpr.Eval(
gal.WithObjects(map[string]gal.Object{
"aCar": Car{
Make: "Lotus Esprit",
Mileage: gal.NewNumberFromInt(2000),
Speed: 100,
MaxSpeed: 250,
},
}),
)
assert.Equal(t, "150", got.String())
}
func TestObjects_Properties_TwoObjects(t *testing.T) {
expr := `Road.Type == "Highway"
And Car.IsRunning()
And Car.Speed < 100
And Car.Speed <= Car.MaxSpeed`
parsedExpr := gal.Parse(expr)
got := parsedExpr.Eval(
gal.WithObjects(map[string]gal.Object{
"Car": &Car{
Make: "Lotus Esprit",
Mileage: gal.NewNumberFromInt(2000),
Speed: 80,
MaxSpeed: 250,
},
"Road": &Road{
Type: "Highway",
},
}),
)
assert.Equal(t, gal.True, got)
}
func TestObjects_Methods(t *testing.T) {
expr := `aCar.MaxSpeed - aCar.CurrentSpeed()`
parsedExpr := gal.Parse(expr)
got := parsedExpr.Eval(
gal.WithObjects(map[string]gal.Object{
"aCar": &Car{
Make: "Lotus Esprit",
Mileage: gal.NewNumberFromInt(2000),
Speed: 100,
MaxSpeed: 250,
},
}),
)
assert.Equal(t, "150", got.String())
}
func TestObjects_Methods_WithSubTree(t *testing.T) {
expr := `2 * (aCar.MaxSpeed - aCar.CurrentSpeed())`
parsedExpr := gal.Parse(expr)
got := parsedExpr.Eval(
gal.WithObjects(map[string]gal.Object{
"aCar": &Car{
Make: "Lotus Esprit",
Mileage: gal.NewNumberFromInt(2000),
Speed: 100,
MaxSpeed: 250,
},
}),
)
assert.Equal(t, "300", got.String())
}
func TestObjects_Methods_WithArgsSubTree(t *testing.T) {
expr := `2 * (aCar.MaxSpeed - aCar.TillMaxSpeed(aCar.CurrentSpeed()))`
parsedExpr := gal.Parse(expr)
got := parsedExpr.Eval(
gal.WithObjects(map[string]gal.Object{
"aCar": &Car{
Make: "Lotus Esprit",
Mileage: gal.NewNumberFromInt(2000),
Speed: 100,
MaxSpeed: 250,
},
}),
)
assert.Equal(t, "200", got.String())
}
func TestObjects_MethodReceiver(t *testing.T) {
expr := `aCar.MaxSpeed - aCar.CurrentSpeed()`
parsedExpr := gal.Parse(expr)
got := parsedExpr.Eval(
gal.WithObjects(map[string]gal.Object{
"aCar": Car{
Make: "Lotus Esprit",
Mileage: gal.NewNumberFromInt(2000),
Speed: 100,
MaxSpeed: 250,
},
}),
)
// Note: in this test, WithObjects is called with a `Car`, not a `*Car`.
// However, Car.CurrentSpeed has a *Car receiver, hence from a Go perspective, the method
// exists on *Car but it does NOT exist on Car!
assert.Equal(t, "undefined: type 'gal_test.Car' does not have a method 'CurrentSpeed' (check if it has a pointer receiver)", got.String())
}