forked from OneOfOne/genh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters_test.go
61 lines (58 loc) · 1016 Bytes
/
filters_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 genh
import (
"reflect"
"sort"
"testing"
)
func TestGroupBy(t *testing.T) {
type args struct {
in map[string]int
fn func(k string, v int) string
}
tests := []struct {
name string
args args
want map[string][]int
}{
{
name: "odds and evens",
args: args{
in: map[string]int{
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
"10": 10,
},
fn: func(k string, v int) string {
if v != 0 && v%2 == 0 {
return "even"
}
return "odd"
},
},
want: map[string][]int{
"odd": {1, 3, 5, 7, 9},
"even": {2, 4, 6, 8, 10},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sortMap(GroupBy(tt.args.in, tt.args.fn)); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GroupBy() = %v, want %v", got, tt.want)
}
})
}
}
func sortMap(m map[string][]int) map[string][]int {
for _, v := range m {
sort.Ints(v)
}
return m
}