forked from PaesslerAG/gval
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gval_test.go
153 lines (127 loc) · 3.02 KB
/
gval_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
package gval
import (
"fmt"
"reflect"
"strings"
"testing"
"github.com/shopspring/decimal"
)
type evaluationTest struct {
name string
expression string
extension Language
parameter interface{}
want interface{}
equalityFunc func(x, y interface{}) bool
wantErr string
}
func testEvaluate(tests []evaluationTest, t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Evaluate(tt.expression, tt.parameter, tt.extension)
if tt.wantErr != "" {
if err == nil {
t.Fatalf("Evaluate(%s) expected error but got %v", tt.expression, got)
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("Evaluate(%s) expected error %s but got error %v", tt.expression, tt.wantErr, err)
}
return
}
if err != nil {
t.Errorf("Evaluate() error = %v", err)
return
}
if ef := tt.equalityFunc; ef != nil {
if !ef(got, tt.want) {
t.Errorf("Evaluate(%s) = %v, want %v", tt.expression, got, tt.want)
}
} else if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Evaluate(%s) = %v, want %v", tt.expression, got, tt.want)
}
})
}
}
// dummyParameter used to test "parameter calls".
type dummyParameter struct {
String string
Int int
BoolFalse bool
Nil interface{}
Nested dummyNestedParameter
MapWithFunc dummyMapWithFunc
SliceWithFunc dummySliceWithFunc
}
func (d dummyParameter) Func() string {
return "funk"
}
func (d dummyParameter) Func2() (string, error) {
return "frink", nil
}
func (d *dummyParameter) PointerFunc() (string, error) {
return "point", nil
}
func (d dummyParameter) FuncErr() (string, error) {
return "", fmt.Errorf("fumps")
}
func (d dummyParameter) FuncArgStr(arg1 string) string {
return arg1
}
func (d dummyParameter) AlwaysFail() (interface{}, error) {
return nil, fmt.Errorf("function should always fail")
}
type dummyNestedParameter struct {
Funk string
Map map[string]int
Slice []int
}
func (d dummyNestedParameter) Dunk(arg1 string) string {
return arg1 + "dunk"
}
var foo = dummyParameter{
String: "string!",
Int: 101,
BoolFalse: false,
Nil: nil,
Nested: dummyNestedParameter{
Funk: "funkalicious",
Map: map[string]int{"a": 1, "b": 2, "c": 3},
Slice: []int{1, 2, 3},
},
MapWithFunc: dummyMapWithFunc{"a": {1, 2}, "b": {3, 4}},
SliceWithFunc: dummySliceWithFunc{"a", "b", "c", "a"},
}
var fooFailureParameters = map[string]interface{}{
"foo": foo,
"fooptr": &foo,
}
var decimalEqualityFunc = func(x, y interface{}) bool {
v1, ok1 := x.(decimal.Decimal)
v2, ok2 := y.(decimal.Decimal)
if !ok1 || !ok2 {
return false
}
return v1.Equal(v2)
}
type dummyMapWithFunc map[string][]int
func (m dummyMapWithFunc) Sum(key string) int {
values, ok := m[key]
if !ok {
return -1
}
sum := 0
for _, v := range values {
sum += v
}
return sum
}
type dummySliceWithFunc []string
func (m dummySliceWithFunc) Sum(key string) int {
sum := 0
for _, v := range m {
if v == key {
sum += 1
}
}
return sum
}