forked from ahl5esoft/golang-underscore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-index_test.go
61 lines (56 loc) · 1.17 KB
/
find-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
54
55
56
57
58
59
60
61
package underscore
import (
"testing"
)
func TestFindIndex(t *testing.T) {
arr := []TestModel{
TestModel{ID: 1, Name: "one"},
TestModel{ID: 1, Name: "two"},
TestModel{ID: 1, Name: "three"},
}
i := FindIndex(arr, func(r TestModel, _ int) bool {
return r.Name == arr[1].Name
})
if i != 1 {
t.Error("wrong")
}
}
func TestFindIndexBy(t *testing.T) {
arr := []TestModel{
TestModel{ID: 1, Name: "one"},
TestModel{ID: 2, Name: "two"},
TestModel{ID: 3, Name: "three"},
}
i := FindIndexBy(arr, map[string]interface{}{
"id": 1,
})
if i != 0 {
t.Error("wrong")
}
}
func TestChain_FindIndex(t *testing.T) {
arr := []TestModel{
TestModel{ID: 1, Name: "one"},
TestModel{ID: 2, Name: "two"},
TestModel{ID: 3, Name: "three"},
}
index := Chain(arr).FindIndex(func(r TestModel, _ int) bool {
return r.Name == arr[1].Name
})
if index != 1 {
t.Error("wrong")
}
}
func TestChain_FindIndexBy(t *testing.T) {
arr := []TestModel{
TestModel{ID: 1, Name: "one"},
TestModel{ID: 2, Name: "two"},
TestModel{ID: 3, Name: "three"},
}
index := Chain(arr).FindIndexBy(map[string]interface{}{
"id": 1,
})
if index == -1 || arr[index].ID != 1 {
t.Error("wrong")
}
}