-
Notifications
You must be signed in to change notification settings - Fork 12
/
collector_test.go
329 lines (290 loc) · 5.58 KB
/
collector_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package fuego
import (
"hash/crc32"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCollector_GroupingBy_Mapping_Filtering_ToEntrySlice(t *testing.T) {
stringLength := func(el string) int {
return len(el)
}
stringToUpper := func(el string) string {
return strings.ToUpper(el)
}
stringLengthGreaterThan := func(length int) Predicate[string] {
return func(el string) bool {
return len(el) > length
}
}
strs := []string{
"a",
"bb",
"cc",
"ddd",
}
got :=
Collect(
NewStreamFromSlice(strs, 0),
GroupingBy(
stringLength,
Mapping(
stringToUpper,
Filtering(
stringLengthGreaterThan(1),
ToSlice[string](),
),
),
),
)
expected := map[int][]string{
1: {},
2: {
"BB",
"CC"},
3: {
"DDD"},
}
assert.EqualValues(t, expected, got)
}
func TestCollector_Collect_ToEntryMap(t *testing.T) {
tt := map[string]struct {
inputData []employee
expected map[string]int
expectedPanic string
}{
"panics when key exists": {
inputData: []employee{
{
id: 1,
name: "One",
},
{
id: 1000,
name: "One",
},
},
expectedPanic: PanicDuplicateKey + ": 'One'",
},
"returns a map of employee (name, id)": {
inputData: getEmployeesSample(),
expected: map[string]int{
"One": 1,
"Two": 2,
"Three": 3,
"Four": 4,
"Five": 5,
},
},
}
for name, tc := range tt {
tc := tc
t.Run(name, func(t *testing.T) {
employeeNameByID := func() map[string]int {
return Collect(
NewStreamFromSlice(tc.inputData, 0),
ToMap(employee.Name, employee.ID),
)
}
if tc.expectedPanic != "" {
assert.PanicsWithValue(t, tc.expectedPanic, func() { _ = employeeNameByID() })
return
}
assert.EqualValues(t, tc.expected, employeeNameByID())
})
}
}
func TestCollector_Collect_ToEntryMapWithKeyMerge(t *testing.T) {
employees := getEmployeesSample()
overwriteKeyMergeFn := func(v1, v2 int) int {
return v2
}
employeeNameByID :=
Collect(
NewStreamFromSlice(employees, 0),
ToMapWithMerge(employee.Department, employee.ID, overwriteKeyMergeFn))
expected := map[string]int{
"HR": 5,
"IT": 3,
"Marketing": 1,
}
assert.EqualValues(t, expected, employeeNameByID)
}
func TestIdentityFinisher(t *testing.T) {
tests := []struct {
name string
element any
want any
}{
{
name: "Should return identity for nil",
element: nil,
want: nil,
},
{
name: "Should return identity for a given simple Entry",
element: "~å∫√çß∆",
want: "~å∫√çß∆",
},
{
name: "Should return identity for a given complex Entry",
element: map[int][]any{
1: {true},
2: {true, "abc"},
},
want: map[int][]any{
1: {true},
2: {true, "abc"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IdentityFinisher(tt.element)
assert.Equal(t, tt.want, got)
})
}
}
func TestCollector_Filtering(t *testing.T) {
employees := getEmployeesSample()
highestPaidEmployeesByDepartment :=
Collect(
NewStreamFromSlice(employees, 0),
GroupingBy(employee.Department,
Filtering(func(e employee) bool {
return e.Salary() > 2000
},
ToSlice[employee]())))
expected := map[string][]employee{
"HR": {
{
id: 5,
name: "Five",
department: "HR",
salary: 2300,
}},
"IT": {
{
id: 2,
name: "Two",
department: "IT",
salary: 2500,
},
{
id: 3,
name: "Three",
department: "IT",
salary: 2200,
}},
"Marketing": {},
}
assert.EqualValues(t, expected, highestPaidEmployeesByDepartment)
}
func TestCollector_GroupingBy_Mapping_FlatMapping_Filtering_Mapping_Reducing(t *testing.T) {
stringLength :=
func(e string) int {
return len(e)
}
toStringList :=
func(e string) []string {
r := []string{}
for _, c := range e {
r = append(r, string(c))
}
return r
}
flattenStringListToDistinct :=
func(e []string) Stream[string] {
return NewStreamFromSlice(e, 0).
Distinct(func(s string) uint32 { return crc32.ChecksumIEEE([]byte(s)) })
}
stringToUpper :=
func(e string) string {
return strings.ToUpper(e)
}
concatenateStringsBiFunc := func(i, j string) string {
iStr := i
jStr := j
return iStr + jStr
}
strs := []string{
"a",
"bb",
"cc",
"ee",
"ddd",
}
got :=
Collect(
NewStreamFromSlice(strs, 0),
GroupingBy(
stringLength,
Mapping(
toStringList,
FlatMapping(flattenStringListToDistinct,
Mapping(stringToUpper,
Reducing(concatenateStringsBiFunc),
),
),
),
),
)
expected := map[int]string{
1: "A",
2: "BCE",
3: "D",
}
assert.EqualValues(t, expected, got)
}
type employee struct {
id int
name string
department string
salary float32
}
func (e employee) ID() int {
return e.id
}
func (e employee) Name() string {
return e.name
}
func (e employee) Department() string {
return e.department
}
func (e employee) Salary() float32 {
return e.salary
}
func getEmployeesSample() []employee {
return []employee{
{
id: 1,
name: "One",
department: "Marketing",
salary: 1500,
},
{
id: 2,
name: "Two",
department: "IT",
salary: 2500,
},
{
id: 3,
name: "Three",
department: "IT",
salary: 2200,
},
{
id: 4,
name: "Four",
department: "HR",
salary: 1800,
},
{
id: 5,
name: "Five",
department: "HR",
salary: 2300,
},
}
}