-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator_test.go
75 lines (68 loc) · 1.96 KB
/
evaluator_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
package ysgo
import (
"testing"
"github.com/remieven/ysgo/internal/testutils"
"github.com/remieven/ysgo/internal/tree"
"github.com/remieven/ysgo/variable"
)
func TestEvaluateExpression(t *testing.T) {
tests := map[string]struct {
expression *tree.Expression
expectedValue *variable.Value
expectedError error
}{
// TODO: complete this
"simple number": {
expression: simpleNumberExpression(12.4),
expectedValue: variable.NewNumber(12.4),
},
"negative number": {
expression: &tree.Expression{
NegativeExpression: simpleNumberExpression(56.7),
},
expectedValue: variable.NewNumber(-56.7),
},
"simple boolean": {
expression: simpleBooleanExpression(true),
expectedValue: variable.NewBoolean(true),
},
"logic not boolean": {
expression: &tree.Expression{
NotExpression: simpleBooleanExpression(false),
},
expectedValue: variable.NewBoolean(true),
},
"number LTE comparison": {
expression: &tree.Expression{
Operator: toPointer(tree.LessThanEqualsBinaryOperator),
LeftOperand: simpleNumberExpression(2),
RightOperand: simpleNumberExpression(4),
},
expectedValue: variable.NewBoolean(true),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
actualValue, err := evaluateExpression(test.expression, nil, nil) // TODO: use an in memory variable storer here when testing for variable evaluation
if !testutils.ErrorEqual(err, test.expectedError) {
t.Errorf("unexpected err: wanted [%v], got [%v]", test.expectedError, err)
}
if diff := testutils.DeepEqual(actualValue, test.expectedValue); diff != "" {
t.Errorf("unexpected value: " + diff)
}
})
}
}
func simpleNumberExpression(value float64) *tree.Expression {
return &tree.Expression{
Value: variable.NewNumber(value),
}
}
func simpleBooleanExpression(value bool) *tree.Expression {
return &tree.Expression{
Value: variable.NewBoolean(value),
}
}
func toPointer[T any](value T) *T {
return &value
}