-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
groupby_test.go
45 lines (39 loc) · 1014 Bytes
/
groupby_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
package linq_test
import (
"reflect"
"testing"
"github.com/makiuchi-d/linq/v2"
)
func TestGroupBy(t *testing.T) {
src := linq.FromSlice([]string{
"blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese", "elephant", "umbrella", "anteater",
})
e := linq.GroupBy(src, func(s string) (byte, error) { return s[0], nil })
mexp := map[byte][]string{
'a': {"abacus", "anteater", "apple"},
'b': {"banana", "blueberry"},
'c': {"cheese", "chimpanzee"},
'e': {"elephant"},
'u': {"umbrella"},
}
linq.ForEach(e, func(grp linq.Grouping[string, byte]) error {
s, err := linq.ToSlice(
linq.OrderBy(grp.Enumerable, func(s string) (string, error) { return s, nil }))
if err != nil {
t.Fatalf("%c: %v", grp.Key, err)
}
k := grp.Key
exp, ok := mexp[k]
if !ok {
t.Fatalf("invalid key: %c", k)
}
if !reflect.DeepEqual(s, exp) {
t.Fatalf("%c: %v, wants %v", k, s, exp)
}
delete(mexp, k)
return nil
})
if len(mexp) > 0 {
t.Fatalf("unreturned items: %v", mexp)
}
}