-
Notifications
You must be signed in to change notification settings - Fork 2
/
flows_test.go
202 lines (188 loc) · 5.62 KB
/
flows_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
package nject
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type flows interface {
DownFlows() (inputs []reflect.Type, outputs []reflect.Type)
UpFlows() (consume []reflect.Type, produce []reflect.Type)
String() string
}
func toTypes(real ...interface{}) []reflect.Type {
types := make([]reflect.Type, len(real))
for i, r := range real {
switch t := r.(type) {
case reflect.Type:
types[i] = t
case typeCode:
types[i] = t.Type()
default:
types[i] = reflect.TypeOf(r)
}
}
return types
}
func flowToStrings(types []typeCode) []string {
types = noNoType(types)
s := make([]string, len(types))
for i, t := range types {
s[i] = t.Type().String()
}
return s
}
func toStrings(types []reflect.Type) []string {
s := make([]string, len(types))
for i, t := range types {
s[i] = t.String()
}
return s
}
func TestFlows(t *testing.T) {
t.Parallel()
cases := []struct {
name string
provider interface{}
upIn []interface{}
upOut []interface{}
downIn []interface{}
downOut []interface{}
class classType
}{
{
name: "fallable injector",
provider: func(int, string) TerminalError { return nil },
upOut: []interface{}{errorType},
downIn: []interface{}{3, ""},
},
{
name: "injecting error",
provider: func(int, string) error { return nil },
downOut: []interface{}{errorType},
downIn: []interface{}{3, ""},
},
{
name: "wrapper",
provider: func(func(int, string) bool, float64) float32 { return 3.2 },
downIn: []interface{}{float64(3.3)},
downOut: []interface{}{3, ""},
upOut: []interface{}{float32(9.2)},
upIn: []interface{}{true},
},
{
name: "constant",
provider: int64(32),
downOut: []interface{}{int64(10)},
},
{
name: "collection",
provider: Sequence("x",
int64(38), // int64 down/out
func(string, float32) bool { return true }, // string, float32 down/in; bool down/out
func(string, bool) {}, // string, bool down/in
func(bool) TerminalError { return nil }, // bool down/in; error up/out
func(func(float64) string, bool) complex128 { return 7 + 2i }, // bool down/in; float64 down/out; complex128 up/out; string up/in
),
downIn: []interface{}{"", float32(3)},
downOut: []interface{}{true, int64(10), float64(10)},
upOut: []interface{}{errorType, complex128(7 + 2i)},
upIn: []interface{}{""},
},
{
name: "reflective injector",
provider: MakeReflective(
toTypes(9, ""),
toTypes(float32(8)),
func([]reflect.Value) []reflect.Value {
return nil
}),
downIn: []interface{}{9, ""},
downOut: []interface{}{float32(8)},
},
{
name: "Reflective wrapper",
provider: MakeReflective(
toTypes(func(string) bool { return true }, 9, ""),
toTypes(float32(8)),
func([]reflect.Value) []reflect.Value {
return nil
}),
downIn: []interface{}{9, ""},
upOut: []interface{}{float32(8)},
upIn: []interface{}{true},
downOut: []interface{}{""},
class: wrapperFunc,
},
{
name: "ReflectiveWrapper",
provider: MakeReflectiveWrapper(
toTypes(5, 8, ""),
toTypes(float32(7)),
toTypes(""),
toTypes(false),
func([]reflect.Value) []reflect.Value {
return nil
}),
downIn: []interface{}{7, 9, ""},
upOut: []interface{}{float32(8)},
downOut: []interface{}{""},
upIn: []interface{}{true},
class: wrapperFunc,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
f, ok := tc.provider.(flows)
if ok {
t.Log("->", f.String())
} else {
t.Log("->", reflect.TypeOf(tc.provider))
f = Provide(tc.name, tc.provider)
}
wantDownIn, wantDownOut := toTypes(tc.downIn...), toTypes(tc.downOut...)
wantUpIn, wantUpOut := toTypes(tc.upIn...), toTypes(tc.upOut...)
fCheck := func(f flows, context string) {
t.Log(context)
downIn, downOut := f.DownFlows()
upIn, upOut := f.UpFlows()
t.Log("down/in", downIn)
t.Log("down/out", downOut)
t.Log("up/in", upIn)
t.Log("up/out", upOut)
assert.ElementsMatchf(t, toStrings(wantDownIn), toStrings(downIn), "down in %s", context)
assert.ElementsMatchf(t, toStrings(wantDownOut), toStrings(downOut), "down out %s", context)
assert.ElementsMatchf(t, toStrings(wantUpIn), toStrings(upIn), "up in %s", context)
assert.ElementsMatchf(t, toStrings(wantUpOut), toStrings(upOut), "up out %s", context)
}
fCheck(f, "direct")
charCheck := func(p *provider) {
t.Log("checking against characterize flows too")
fm, err := handlerRegistry.characterizeFuncDetails(p, charContext{})
if err != nil {
var e2 error
fm, e2 = handlerRegistry.characterizeFuncDetails(p, charContext{inputsAreStatic: true})
require.NoErrorf(t, e2, "static characterize, and non-static: %s", err)
}
if tc.class != unsetClassType {
require.Equalf(t, tc.class, fm.class, "class type %s vs %s", tc.class, fm.class)
}
assert.Equal(t, toStrings(wantUpOut), flowToStrings(fm.flows[returnParams]), "char up out")
assert.Equal(t, toStrings(wantDownOut), flowToStrings(fm.flows[outputParams]), "char down out")
assert.Equal(t, toStrings(wantDownIn), flowToStrings(fm.flows[inputParams]), "char down in")
assert.Equal(t, toStrings(wantUpIn), flowToStrings(fm.flows[receivedParams]), "char up in")
fCheck(p, "characterized")
}
switch p := f.(type) {
case *provider:
charCheck(p)
case *Collection:
// skip
default:
assert.Failf(t, "unexpected type", "unexpected type %T", f)
}
})
}
}