forked from samber/lo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_test.go
95 lines (72 loc) · 1.92 KB
/
find_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package lo
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIndexOf(t *testing.T) {
is := assert.New(t)
result1 := IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2)
result2 := IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6)
is.Equal(result1, 2)
is.Equal(result2, -1)
}
func TestLastIndexOf(t *testing.T) {
is := assert.New(t)
result1 := LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2)
result2 := LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6)
is.Equal(result1, 4)
is.Equal(result2, -1)
}
func TestFind(t *testing.T) {
is := assert.New(t)
result1, ok1 := Find[string]([]string{"a", "b", "c", "d"}, func(i string) bool {
return i == "b"
})
result2, ok2 := Find[string]([]string{"foobar"}, func(i string) bool {
return i == "b"
})
is.Equal(ok1, true)
is.Equal(result1, "b")
is.Equal(ok2, false)
is.Equal(result2, "")
}
func TestMin(t *testing.T) {
is := assert.New(t)
result1 := Min[int]([]int{1, 2, 3})
result2 := Min[int]([]int{3, 2, 1})
result3 := Min[int]([]int{})
is.Equal(result1, 1)
is.Equal(result2, 1)
is.Equal(result3, 0)
}
func TestMax(t *testing.T) {
is := assert.New(t)
result1 := Max[int]([]int{1, 2, 3})
result2 := Max[int]([]int{3, 2, 1})
result3 := Max[int]([]int{})
is.Equal(result1, 3)
is.Equal(result2, 3)
is.Equal(result3, 0)
}
func TestLast(t *testing.T) {
is := assert.New(t)
result1, err1 := Last[int]([]int{1, 2, 3})
result2, err2 := Last[int]([]int{})
is.Equal(result1, 3)
is.Equal(err1, nil)
is.Equal(result2, 0)
is.Equal(err2, fmt.Errorf("last: cannot extract the last element of an empty slice"))
}
func TestNth(t *testing.T) {
is := assert.New(t)
result1, err1 := Nth[int]([]int{0, 1, 2, 3}, 2)
result2, err2 := Nth[int]([]int{0, 1, 2, 3}, -2)
result3, err3 := Nth[int]([]int{0, 1, 2, 3}, 42)
is.Equal(result1, 2)
is.Equal(err1, nil)
is.Equal(result2, 2)
is.Equal(err2, nil)
is.Equal(result3, 0)
is.Equal(err3, fmt.Errorf("nth: 42 out of slice bounds"))
}