forked from ahmetb/go-linq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where_test.go
60 lines (53 loc) · 1.89 KB
/
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
54
55
56
57
58
59
60
package linq
import "testing"
func TestWhere(t *testing.T) {
tests := []struct {
input interface{}
predicate func(interface{}) bool
output []interface{}
}{
{[9]int{1, 1, 1, 2, 1, 2, 3, 4, 2}, func(i interface{}) bool {
return i.(int) >= 3
}, []interface{}{3, 4}},
{"sstr", func(i interface{}) bool {
return i.(rune) != 's'
}, []interface{}{'t', 'r'}},
}
for _, test := range tests {
if q := From(test.input).Where(test.predicate); !validateQuery(q, test.output) {
t.Errorf("From(%v).Where()=%v expected %v", test.input, toSlice(q), test.output)
}
}
}
func TestWhereT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "WhereT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).WhereT(func(item int) int { return item + 2 })
})
}
func TestWhereIndexed(t *testing.T) {
tests := []struct {
input interface{}
predicate func(int, interface{}) bool
output []interface{}
}{
{[9]int{1, 1, 1, 2, 1, 2, 3, 4, 2}, func(i int, x interface{}) bool {
return x.(int) < 4 && i > 4
}, []interface{}{2, 3, 2}},
{"sstr", func(i int, x interface{}) bool {
return x.(rune) != 's' || i == 1
}, []interface{}{'s', 't', 'r'}},
{"abcde", func(i int, _ interface{}) bool {
return i < 2
}, []interface{}{'a', 'b'}},
}
for _, test := range tests {
if q := From(test.input).WhereIndexed(test.predicate); !validateQuery(q, test.output) {
t.Errorf("From(%v).WhereIndexed()=%v expected %v", test.input, toSlice(q), test.output)
}
}
}
func TestWhereIndexedT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "WhereIndexedT: parameter [predicateFn] has a invalid function signature. Expected: 'func(int,T)bool', actual: 'func(string)'", func() {
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).WhereIndexedT(func(item string) {})
})
}