-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_test.go
230 lines (224 loc) · 4.88 KB
/
utils_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package nject
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSaveToInvalid(t *testing.T) {
f := func() {}
cases := []struct {
want string
thing interface{}
}{
{
want: "not a valid pointer",
},
{
want: "is nil",
thing: (*string)(nil),
},
{
want: "is not a pointer",
thing: 7,
},
{
want: "may not be a pointer to a function",
thing: &f,
},
}
for _, tc := range cases {
t.Log(tc.want)
_, err := SaveTo(tc.thing)
if assert.Error(t, err, tc.want) {
assert.Contains(t, err.Error(), tc.want)
}
}
}
func TestSaveToValid(t *testing.T) {
type foo struct {
i int
}
var fooDst foo
var fooPtrDst *foo
cases := []struct {
inject interface{}
ptr interface{}
check func()
}{
{
inject: foo{i: 7},
ptr: &fooDst,
check: func() {
assert.Equal(t, foo{i: 7}, fooDst)
},
},
{
inject: &foo{i: 7},
ptr: &fooPtrDst,
check: func() {
assert.Equal(t, foo{i: 7}, *fooPtrDst)
},
},
}
for i, tc := range cases {
err := Run("x",
tc.inject,
MustSaveTo(tc.ptr),
)
if assert.NoErrorf(t, err, "%d", i) {
assert.NotPanics(t, tc.check, "check")
}
}
}
func TestCurry(t *testing.T) {
t.Parallel()
seq := Sequence("available",
func() string { return "foo" },
func() int { return 3 },
func() uint { return 7 },
)
var c1 func(string) string
var c2 func(bool, bool, string, string) string
cases := []struct {
name string
fail string
curry interface{}
check func(t *testing.T)
original interface{}
}{
{
curry: &c1,
original: func(x int, s string) string {
return fmt.Sprintf("%s-%d", s, x)
},
check: func(t *testing.T) {
assert.Equal(t, "bar-3", c1("bar"))
},
},
{
curry: &c1,
original: func(x int, s string, u uint) string {
return fmt.Sprintf("%s-%d/%d", s, x, u)
},
check: func(t *testing.T) {
assert.Equal(t, "bar-3/7", c1("bar"))
},
},
{
curry: &c2,
original: func(b1 bool, x int, b2 bool, s1 string, s2 string, u uint) string {
return fmt.Sprintf("%v-%d-%v %s %s-%d", b1, x, b2, s1, s2, u)
},
check: func(t *testing.T) {
assert.Equal(t, "true-3-false bee boot-7", c2(true, false, "bee", "boot"))
},
},
{
curry: &c2,
original: func(b1 bool, s1 string, s2 string) string { return "" },
fail: "curried function must take fewer arguments",
},
{
curry: &c2,
original: func(b1 bool, b2 bool, b3 bool, s1 string, s2 string) string { return "" },
fail: "original function takes more arguments of type bool",
},
{
name: "no original",
curry: &c2,
fail: "original function is not a valid value",
},
{
name: "no curry",
original: func(b1 bool, b2 bool, b3 bool, s1 string, s2 string) string { return "" },
fail: "curried function is not a valid value",
},
{
name: "non-pointer",
curry: 7,
original: func(b1 bool, b2 bool, b3 bool, s1 string, s2 string) string { return "" },
fail: "pointer (to a function)",
},
{
name: "non-func",
curry: seq,
original: func(b1 bool, b2 bool, b3 bool, s1 string, s2 string) string { return "" },
fail: "pointer to a function",
},
{
curry: &c2,
original: "original non-func",
fail: "first argument to Curry must be a function",
},
{
name: "nil",
curry: (*func())(nil),
original: func(string) {},
fail: "pointer to curried function cannot be nil",
},
{
curry: &c1,
original: func(string) {},
fail: "same number of outputs",
},
{
curry: &c2,
original: func(b1 bool, x int, b2 bool, s1 string, s2 string, u uint) int {
return 22
},
fail: "return value #1 has a different type",
},
{
curry: &c1,
original: func(i1 int, i2 int, s string) string {
return "foo"
},
fail: "cannot curry the same type (int) more than once",
},
{
curry: &c1,
original: func(uint, int) string {
return "foo"
},
fail: "not all of the string inputs to the curried function were used",
},
{
curry: &c1,
original: func(s string, inner func(), i int) string {
return fmt.Sprintf("%s-%d", s, i)
},
fail: "may not be a function",
},
}
for _, tc := range cases {
name := tc.name
if name == "" {
name = fmt.Sprintf("%T", tc.original)
}
t.Run(name, func(t *testing.T) {
var called bool
p, err := Curry(tc.original, tc.curry)
if tc.fail != "" && err != nil {
assert.Contains(t, err.Error(), tc.fail, "curry")
assert.Panics(t, func() {
_ = MustCurry(tc.original, tc.curry)
}, "curry")
return
} else {
//nolint:testifylint
if !assert.NoError(t, err, "curry") {
return
}
}
err = Run(name, seq, p, func() { called = true })
if tc.fail != "" {
if assert.Error(t, err, "run") {
assert.Contains(t, err.Error(), tc.fail, "run")
}
return
}
assert.True(t, called, "called")
tc.check(t)
})
}
}