-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslices_transform_test.go
275 lines (238 loc) · 7.07 KB
/
slices_transform_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package generics_test
import (
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zeroflucs-given/generics"
)
// TestGroup checks that we can group items
func TestGroup(t *testing.T) {
// Arrange
mapper := func(index int, v int) string {
if v%2 == 0 {
return "Evens"
} else {
return "Odds"
}
}
input := []int{1, 2, 3, 4, 5}
// Act
result := generics.Group(input, mapper)
// Assert
require.Len(t, result, 2, "Should have 2 groups")
require.Equal(t, []int{2, 4}, result["Evens"], "Should have the right Evens")
require.Equal(t, []int{1, 3, 5}, result["Odds"], "Should have the right Odds")
}
// TestGroupWithContext checks that we can group items
func TestGroupWithContext(t *testing.T) {
// Arrange
testCtx := context.Background()
mapper := func(ctx context.Context, index int, v int) (string, error) {
require.Equal(t, testCtx, ctx, "Should have correct context")
if v%2 == 0 {
return "Evens", nil
}
return "Odds", nil
}
input := []int{1, 2, 3, 4, 5}
// Act
result, err := generics.GroupWithContext(testCtx, input, mapper)
// Assert
require.NoError(t, err, "Should not error")
require.Len(t, result, 2, "Should have 2 groups")
require.Equal(t, []int{2, 4}, result["Evens"], "Should have the right Evens")
require.Equal(t, []int{1, 3, 5}, result["Odds"], "Should have the right Odds")
}
// TestGroupWithContextFail checks that we can group items but stop on failure
func TestGroupWithContextFail(t *testing.T) {
// Arrange
testCtx := context.Background()
mapper := func(ctx context.Context, index int, v int) (string, error) {
require.Equal(t, testCtx, ctx, "Should have correct context")
if v == 4 {
return "", errors.New("boom")
}
if v%2 == 0 {
return "Evens", nil
}
return "Odds", nil
}
input := []int{1, 2, 3, 4, 5}
// Act
result, err := generics.GroupWithContext(testCtx, input, mapper)
// Assert
require.Error(t, err, "Should error")
require.Nil(t, result, "No result should be given")
}
// TestMap tests that we can map values from one type to another
func TestMap(t *testing.T) {
// Arrange
mapper := func(index int, v int) string {
return fmt.Sprintf("%v", v)
}
input := []int{1, 2, 3, 4, 5, 6}
// Act
result := generics.Map(input, mapper)
// Assert
require.Len(t, result, 6, "Should have 6 items")
require.Equal(t, []string{"1", "2", "3", "4", "5", "6"}, result, "Should have the right outputs")
}
// TestMapContext applies a mapper to a set with a context
func TestMapContext(t *testing.T) {
// Arrange
testCtx := context.TODO()
mapper := func(ctx context.Context, index int, v int) (string, error) {
require.Equal(t, testCtx, ctx, "Should have right context")
return fmt.Sprintf("%v", v), nil
}
input := []int{1, 2, 3, 4, 5, 6}
// Act
result, err := generics.MapWithContext(testCtx, input, mapper)
// Assert
require.NoError(t, err, "Should not error")
require.Len(t, result, 6, "Should have 6 items")
require.Equal(t, []string{"1", "2", "3", "4", "5", "6"}, result, "Should have the right outputs")
}
// TestMapContextError make sure we abort when errors occur
func TestMapContextError(t *testing.T) {
// Arrange
testCtx := context.TODO()
mapper := func(ctx context.Context, index int, v int) (string, error) {
if v == 5 {
return "", errors.New("boom")
}
require.Equal(t, testCtx, ctx, "Should have right context")
return fmt.Sprintf("%v", v), nil
}
input := []int{1, 2, 3, 4, 5, 6}
// Act
result, err := generics.MapWithContext(testCtx, input, mapper)
// Assert
require.Error(t, err, "Should error")
require.Nil(t, result, "Should have no result")
}
func TestToMap(t *testing.T) {
// Arrange
input := []int{1, 2, 3, 4, 5}
keyMapper := func(index int, k int) string {
return fmt.Sprintf("Key%v", k%4)
}
valueMapper := func(index int, v int) int {
return v * 2
}
// Act
result := generics.ToMap(input, keyMapper, valueMapper)
// Assert
assert.Len(t, result, 4, "Should have 4 items in the dictionary")
assert.Equal(t, result["Key1"], 10, "Should have overwritten the first value")
assert.Equal(t, result["Key2"], 4, "Should have the right value for second item")
}
func TestToMapContext(t *testing.T) {
// Arrange
testCtx := context.TODO()
input := []int{1, 2, 3, 4, 5}
keyMapper := func(ctx context.Context, index int, k int) (string, error) {
require.Equal(t, testCtx, ctx, "Should have right context")
return fmt.Sprintf("Key%v", k%4), nil
}
valueMapper := func(ctx context.Context, index int, v int) (int, error) {
require.Equal(t, testCtx, ctx, "Should have right context")
return v * 2, nil
}
// Act
result, err := generics.ToMapWithContext(testCtx, input, keyMapper, valueMapper)
// Assert
require.NoError(t, err, "Should not error")
assert.Len(t, result, 4, "Should have 4 items in the dictionary")
assert.Equal(t, result["Key1"], 10, "Should have overwritten the first value")
assert.Equal(t, result["Key2"], 4, "Should have the right value for second item")
}
func TestToMapContextErrorKey(t *testing.T) {
// Arrange
testCtx := context.TODO()
input := []int{1, 2, 3, 4, 5}
keyMapper := func(ctx context.Context, index int, k int) (string, error) {
require.Equal(t, testCtx, ctx, "Should have right context")
if index == 3 {
return "", fmt.Errorf("key failed")
}
return fmt.Sprintf("Key%v", k%4), nil
}
valueMapper := func(ctx context.Context, index int, v int) (int, error) {
require.Equal(t, testCtx, ctx, "Should have right context")
return v * 2, nil
}
// Act
result, err := generics.ToMapWithContext(testCtx, input, keyMapper, valueMapper)
// Assert
require.Error(t, err, "Should error")
require.Nil(t, result, "Should have no result")
}
func TestToMapContextErrorValue(t *testing.T) {
// Arrange
testCtx := context.TODO()
input := []int{1, 2, 3, 4, 5}
keyMapper := func(ctx context.Context, index int, k int) (string, error) {
require.Equal(t, testCtx, ctx, "Should have right context")
return fmt.Sprintf("Key%v", k%4), nil
}
valueMapper := func(ctx context.Context, index int, v int) (int, error) {
require.Equal(t, testCtx, ctx, "Should have right context")
if index == 3 {
return 0, fmt.Errorf("value failed")
}
return v * 2, nil
}
// Act
result, err := generics.ToMapWithContext(testCtx, input, keyMapper, valueMapper)
// Assert
require.Error(t, err, "Should error")
require.Nil(t, result, "Should have no result")
}
// TestCompactSlice makes sure we compact a slice so we keep only the last value per
// key in the slice
func TestCompactSlice(t *testing.T) {
type input struct {
K string
V string
}
// Arrange
inputData := []input{
{
K: "foo",
V: "first-foo",
},
{
K: "bar",
V: "first-bar",
},
{
K: "foo",
V: "second-foo",
},
{
K: "foo",
V: "third-foo",
},
{
K: "fizz",
V: "first-fizz",
},
{
K: "bar",
V: "second-bar",
},
}
// Act
result := generics.Compact(inputData, func(i int, v input) string {
return v.K
})
// Assert
require.Len(t, result, 3, "Should have 3 items")
require.Equal(t, "third-foo", result[0].V)
require.Equal(t, "first-fizz", result[1].V)
require.Equal(t, "second-bar", result[2].V)
}