-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter_test.go
63 lines (56 loc) · 1.28 KB
/
filter_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
package slice
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilter(t *testing.T) {
type testStruct struct {
ID int
Valid bool
}
type testStructSlice []testStruct
tests := []struct {
name string
elems []any
predicate func(any) bool
want []any
}{
{
name: "EvenIntegers",
elems: []interface{}{1, 2, 3, 4, 5, 6, 7},
predicate: func(i any) bool {
return i.(int)%2 == 0
},
want: []any{2, 4, 6},
},
{
name: "ValidStructs",
elems: []interface{}{
testStruct{ID: 1, Valid: true},
testStruct{ID: 2},
testStruct{ID: 3, Valid: true},
testStruct{ID: 4},
},
predicate: func(i any) bool {
return i.(testStruct).Valid
},
want: []any{testStruct{ID: 1, Valid: true}, testStruct{ID: 3, Valid: true}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.EqualValues(t, tt.want, Filter(tt.elems, tt.predicate))
})
}
t.Run("ValidTypedSlice", func(t *testing.T) {
assert.EqualValues(t, testStructSlice{
testStruct{ID: 1, Valid: true},
testStruct{ID: 3, Valid: true},
}, Filter(testStructSlice{
testStruct{ID: 1, Valid: true},
testStruct{ID: 2},
testStruct{ID: 3, Valid: true},
testStruct{ID: 4},
}, func(i testStruct) bool { return i.Valid }))
})
}