-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathto_map_test.go
61 lines (59 loc) · 1.21 KB
/
to_map_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
package slices
import (
"encoding/json"
"fmt"
"testing"
)
func TestToMap(t *testing.T) {
type args struct {
slice []int
keyFunc func(int, int, []int) int
valueFunc func(int, int, []int) any
}
tests := []struct {
name string
args args
want map[int]any
}{
{
name: "number1",
args: args{
slice: []int{2, 3, 4, 13},
keyFunc: func(item int, index int, slice []int) int {
return item
},
valueFunc: func(item int, index int, slice []int) any {
return item
},
},
want: map[int]any{
2: 2, 3: 3, 4: 4, 13: 13,
},
},
{
name: "number2",
args: args{
slice: []int{2, 3, 4, 13},
keyFunc: func(item int, index int, slice []int) int {
return item
},
valueFunc: func(item int, index int, slice []int) any {
return fmt.Sprint(item)
},
},
want: map[int]any{
2: "2", 3: "3", 4: "4", 13: "13",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ToMap(tt.args.slice, tt.args.keyFunc, tt.args.valueFunc)
gotJson, _ := json.Marshal(&got)
wantJson, _ := json.Marshal(&tt.want)
if string(gotJson) != string(wantJson) {
t.Errorf("ToMap() = %v, want %v", got, tt.want)
}
})
}
}