forked from PaesslerAG/gval
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gval_test.go
102 lines (87 loc) · 2.04 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
package gval
import (
"fmt"
"reflect"
"strings"
"testing"
)
type evaluationTest struct {
name string
expression string
extension Language
parameter interface{}
want interface{}
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 !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
}
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},
},
}
var fooFailureParameters = map[string]interface{}{
"foo": foo,
"fooptr": &foo,
}