-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice_custom_test.go
More file actions
238 lines (229 loc) · 5.58 KB
/
Copy pathslice_custom_test.go
File metadata and controls
238 lines (229 loc) · 5.58 KB
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
package lo
import (
"errors"
"slices"
"testing"
)
func TestHasDuplicates(t *testing.T) {
tests := map[string]struct {
collection []int
want bool
}{
"has duplicates": {
collection: []int{1, 1, 2},
want: true,
},
"no duplicates": {
collection: []int{1, 2, 3},
want: false,
},
"empty": {
collection: []int{},
want: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := HasDuplicates(tt.collection); got != tt.want {
t.Errorf("HasDuplicates() = %v, want %v", got, tt.want)
}
})
}
}
func TestHasDuplicatesBy(t *testing.T) {
type S struct {
A int
B int
}
tests := map[string]struct {
collection []S
iteratee func(S) int
want bool
}{
"has duplicates by A": {
collection: []S{
{A: 0, B: 1},
{A: 0, B: 2},
},
iteratee: func(item S) int { return item.A },
want: true,
},
"has duplicates by B": {
collection: []S{
{A: 1, B: 0},
{A: 2, B: 0},
},
iteratee: func(item S) int { return item.B },
want: true,
},
"no duplicates by A": {
collection: []S{
{A: 1, B: 0},
{A: 2, B: 0},
},
iteratee: func(item S) int { return item.A },
want: false,
},
"no duplicates by B": {
collection: []S{
{A: 0, B: 1},
{A: 0, B: 2},
},
iteratee: func(item S) int { return item.B },
want: false,
},
"empty": {
collection: []S{},
iteratee: func(item S) int { return item.A },
want: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := HasDuplicatesBy(tt.collection, tt.iteratee); got != tt.want {
t.Errorf("HasDuplicatesBy() = %v, want %v", got, tt.want)
}
})
}
}
func TestReduce(t *testing.T) {
tests := map[string]struct {
collection []int
accumulator func(agg int, item int) int
initial int
want int
}{
"sum": {
collection: []int{1, 2, 3, 4, 5},
accumulator: func(agg int, item int) int { return agg + item },
initial: 0,
want: 15,
},
"product": {
collection: []int{1, 2, 3, 4},
accumulator: func(agg int, item int) int { return agg * item },
initial: 1,
want: 24,
},
"empty": {
collection: []int{},
accumulator: func(agg int, item int) int { return agg + item },
initial: 10,
want: 10,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := Reduce(tt.collection, tt.accumulator, tt.initial); got != tt.want {
t.Errorf("Reduce() = %v, want %v", got, tt.want)
}
})
}
}
func TestReduceWithIndex(t *testing.T) {
collection := []int{1, 2, 3}
accumulator := func(agg int, item int, index int) int { return agg + item + index }
initial := 0
result := ReduceWithIndex(collection, accumulator, initial)
expected := 9 // (0+1+0)=1, (1+2+1)=4, (4+3+2)=9
if result != expected {
t.Errorf("ReduceWithIndex() = %v, want %v", result, expected)
}
}
func TestFilterWithError(t *testing.T) {
tests := map[string]struct {
collection []int
predicate func(int) (bool, error)
want []int
wantErr bool
}{
"filter even numbers": {
collection: []int{1, 2, 3, 4, 5, 6},
predicate: func(item int) (bool, error) { return item%2 == 0, nil },
want: []int{2, 4, 6},
wantErr: false,
},
"filter with error on specific value": {
collection: []int{1, 2, 3, 4, 5},
predicate: func(item int) (bool, error) {
if item == 3 {
return false, errors.New("error on value 3")
}
return item%2 == 0, nil
},
want: nil,
wantErr: true,
},
// other cases are tested in TestFilterWithIndexError
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := FilterWithError(tt.collection, tt.predicate)
if (err != nil) != tt.wantErr {
t.Errorf("FilterWithError() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !slices.Equal(got, tt.want) {
t.Errorf("FilterWithError() = %v, want %v", got, tt.want)
}
})
}
}
func TestFilterWithIndexError(t *testing.T) {
tests := map[string]struct {
collection []int
predicate func(int, int) (bool, error)
want []int
wantErr bool
}{
"filter by even index": {
collection: []int{10, 20, 30, 40, 50},
predicate: func(item int, index int) (bool, error) { return index%2 == 0, nil },
want: []int{10, 30, 50},
wantErr: false,
},
"filter with error at specific index": {
collection: []int{1, 2, 3, 4, 5},
predicate: func(item int, index int) (bool, error) {
if index == 2 {
return false, errors.New("error at index 2")
}
return item%2 == 0, nil
},
want: nil,
wantErr: true,
},
"empty collection": {
collection: []int{},
predicate: func(item int, index int) (bool, error) { return index%2 == 0, nil },
want: []int{},
wantErr: false,
},
"filter by value and index": {
collection: []int{1, 2, 3, 4, 5, 6},
predicate: func(item int, index int) (bool, error) {
return item%2 == 0 && index < 4, nil
},
want: []int{2, 4},
wantErr: false,
},
"all elements filtered out": {
collection: []int{1, 3, 5},
predicate: func(item int, index int) (bool, error) { return index > 10, nil },
want: []int{},
wantErr: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := FilterWithIndexError(tt.collection, tt.predicate)
if (err != nil) != tt.wantErr {
t.Errorf("FilterWithIndexError() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !slices.Equal(got, tt.want) {
t.Errorf("FilterWithIndexError() = %v, want %v", got, tt.want)
}
})
}
}