-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgroup_test.go
71 lines (67 loc) · 1.65 KB
/
group_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
package goterators
import (
"fmt"
"testing"
)
func TestGroup(t *testing.T) {
type Product struct {
productType string
productName string
}
testSource := []Product{{
productType: "food",
productName: "Coca-laco",
}, {
productType: "sport",
productName: "Old Balance",
}, {
productType: "sport",
productName: "Noke",
}, {
productType: "food",
productName: "Chip chip",
}}
expectedItems := [][]Product{
{
{
productType: "food",
productName: "Coca-laco",
}, {
productType: "food",
productName: "Chip chip",
},
},
{
{
productType: "sport",
productName: "Old Balance",
}, {
productType: "sport",
productName: "Noke",
},
},
}
actualItems := Group(testSource, func(item Product) string {
return item.productType
})
if len(expectedItems) != len(actualItems) {
t.Fatalf("Expected length = %v, actual length = %v", len(expectedItems), len(actualItems))
}
for groupIndex := range expectedItems {
if len(expectedItems[groupIndex]) != len(actualItems[groupIndex]) {
t.Fatalf("Group index %v, expected = %v, actual = %v", groupIndex, expectedItems[groupIndex], actualItems[groupIndex])
}
for index := range expectedItems[groupIndex] {
if expectedItems[groupIndex][index] != actualItems[groupIndex][index] {
t.Errorf("Group index %v, Index %v, expected = %v, actual = %v", groupIndex, index, expectedItems[groupIndex][index], actualItems[groupIndex][index])
}
}
}
}
func ExampleGroup() {
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
groups := Group(testSource, func(item int) int {
return item % 2
})
fmt.Println("Group: ", groups)
}