forked from PaesslerAG/gval
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions_test.go
162 lines (159 loc) · 3.23 KB
/
functions_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
package gval
import (
"context"
"fmt"
"reflect"
"testing"
"time"
)
func Test_toFunc(t *testing.T) {
myError := fmt.Errorf("my error")
tests := []struct {
name string
function interface{}
arguments []interface{}
want interface{}
wantErr error
wantAnyErr bool
}{
{
name: "empty",
function: func() {},
},
{
name: "one arg",
function: func(a interface{}) {
if a != true {
panic("fail")
}
},
arguments: []interface{}{true},
},
{
name: "three args",
function: func(a, b, c interface{}) {
if a != 1 || b != 2 || c != 3 {
panic("fail")
}
},
arguments: []interface{}{1, 2, 3},
},
{
name: "input types",
function: func(a int, b string, c bool) {
if a != 1 || b != "2" || !c {
panic("fail")
}
},
arguments: []interface{}{1, "2", true},
},
{
name: "wronge input type int",
function: func(a int, b string, c bool) {},
arguments: []interface{}{"1", "2", true},
wantAnyErr: true,
},
{
name: "wronge input type string",
function: func(a int, b string, c bool) {},
arguments: []interface{}{1, 2, true},
wantAnyErr: true,
},
{
name: "wronge input type bool",
function: func(a int, b string, c bool) {},
arguments: []interface{}{1, "2", "true"},
wantAnyErr: true,
},
{
name: "wronge input number",
function: func(a int, b string, c bool) {},
arguments: []interface{}{1, "2"},
wantAnyErr: true,
},
{
name: "one return",
function: func() bool {
return true
},
want: true,
},
{
name: "three returns",
function: func() (bool, string, int) {
return true, "2", 3
},
want: []interface{}{true, "2", 3},
},
{
name: "error",
function: func() error {
return myError
},
wantErr: myError,
},
{
name: "none error",
function: func() error {
return nil
},
},
{
name: "one return with error",
function: func() (bool, error) {
return false, myError
},
want: false,
wantErr: myError,
},
{
name: "three returns with error",
function: func() (bool, string, int, error) {
return false, "", 0, myError
},
want: []interface{}{false, "", 0},
wantErr: myError,
},
{
name: "context not expiring",
function: func(ctx context.Context) error {
return nil
},
},
{
name: "context expires",
function: func(ctx context.Context) error {
time.Sleep(20 * time.Millisecond)
return nil
},
wantErr: context.DeadlineExceeded,
},
{
name: "nil arg",
function: func(a interface{}) bool {
return a != nil
},
arguments: []interface{}{nil},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
got, err := toFunc(tt.function)(ctx, tt.arguments...)
cancel()
if tt.wantAnyErr {
if err != nil {
return
}
t.Fatalf("toFunc()(args...) = error(nil), but wantAnyErr")
}
if err != tt.wantErr {
t.Fatalf("toFunc()(args...) = error(%v), wantErr (%v)", err, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("toFunc()(args...) = %v, want %v", got, tt.want)
}
})
}
}