-
Notifications
You must be signed in to change notification settings - Fork 0
/
associate_test.go
57 lines (52 loc) · 1.02 KB
/
associate_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
package slices_test
import (
"testing"
"github.com/dosadczuk/go-slices"
"github.com/google/go-cmp/cmp"
)
func TestAssociate(t *testing.T) {
tt := map[string]struct {
// input
values []int
transform func(int) (int, int)
// assert
want map[int]int
}{
"empty slice": {
values: nil, // zero value
transform: func(val int) (int, int) { return val, 0 },
want: nil, // zero value
},
"transform returns unique keys": {
values: []int{1, 2, 3, 4, 5},
transform: func(val int) (int, int) {
return val, val % 2
},
want: map[int]int{
1: 1,
2: 0,
3: 1,
4: 0,
5: 1,
},
},
"transform returns duplicated keys": {
values: []int{1, 2, 3, 4, 5},
transform: func(val int) (int, int) {
return val % 2, val
},
want: map[int]int{
0: 4,
1: 5,
},
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
have := slices.Associate(tc.values, tc.transform)
if !cmp.Equal(tc.want, have) {
t.Error(cmp.Diff(tc.want, have))
}
})
}
}