-
Notifications
You must be signed in to change notification settings - Fork 32
/
where_test.go
53 lines (48 loc) · 938 Bytes
/
where_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
package underscore
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Benchmark_Where(b *testing.B) {
for n := 0; n < b.N; n++ {
Range(0, benchmarkSize, 1).Where(func(r, _ int) bool {
return r > 100
}).Any(func(r, _ int) bool {
return true
})
}
}
func Test_Where(t *testing.T) {
src := []testModel{
{ID: 1, Name: "one"},
{ID: 2, Name: "one"},
{ID: 3, Name: "three"},
{ID: 4, Name: "three"},
}
var res []testModel
Chain(src).Where(func(r testModel, _ int) bool {
return r.ID%2 == 0
}).Value(&res)
assert.EqualValues(
t,
res,
[]testModel{src[1], src[3]},
)
}
func Test_WhereBy(t *testing.T) {
src := []testModel{
{ID: 1, Name: "one"},
{ID: 2, Name: "one"},
{ID: 3, Name: "three"},
{ID: 4, Name: "three"},
}
var res []testModel
Chain(src).WhereBy(map[string]interface{}{
"Name": "one",
}).Value(&res)
assert.EqualValues(
t,
res,
[]testModel{src[0], src[1]},
)
}