-
Notifications
You must be signed in to change notification settings - Fork 225
/
index_test.go
53 lines (48 loc) · 1.23 KB
/
index_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 linq
import (
"testing"
)
func TestIndexOf(t *testing.T) {
tests := []struct {
input interface{}
predicate func(interface{}) bool
expected int
}{
{
input: [9]int{1, 2, 3, 4, 5, 6, 7, 8, 9},
predicate: func(i interface{}) bool {
return i.(int) == 3
},
expected: 2,
},
{
input: "sstr",
predicate: func(i interface{}) bool {
return i.(rune) == 'r'
},
expected: 3,
},
{
input: "gadsgsadgsda",
predicate: func(i interface{}) bool {
return i.(rune) == 'z'
},
expected: -1,
},
}
for _, test := range tests {
index := From(test.input).IndexOf(test.predicate)
if index != test.expected {
t.Errorf("From(%v).IndexOf() expected %v received %v", test.input, test.expected, index)
}
index = From(test.input).IndexOfT(test.predicate)
if index != test.expected {
t.Errorf("From(%v).IndexOfT() expected %v received %v", test.input, test.expected, index)
}
}
}
func TestIndexOfT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
mustPanicWithError(t, "IndexOfT: 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}).IndexOfT(func(item int) int { return item + 2 })
})
}