forked from thoas/go-funk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_test.go
86 lines (62 loc) · 1.42 KB
/
scan_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
package funk
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestForEach(t *testing.T) {
is := assert.New(t)
results := []int{}
ForEach([]int{1, 2, 3, 4}, func(x int) {
if x%2 == 0 {
results = append(results, x)
}
})
is.Equal(results, []int{2, 4})
mapping := map[int]string{
1: "Florent",
2: "Gilles",
}
ForEach(mapping, func(k int, v string) {
is.Equal(v, mapping[k])
})
}
func TestForEachRight(t *testing.T) {
is := assert.New(t)
results := []int{}
ForEachRight([]int{1, 2, 3, 4}, func(x int) {
results = append(results, x*2)
})
is.Equal(results, []int{8, 6, 4, 2})
mapping := map[int]string{
1: "Florent",
2: "Gilles",
}
mapKeys := []int{}
ForEachRight(mapping, func(k int, v string) {
is.Equal(v, mapping[k])
mapKeys = append(mapKeys, k)
})
is.Equal(len(mapKeys), 2)
is.Contains(mapKeys, 1)
is.Contains(mapKeys, 2)
}
func TestHead(t *testing.T) {
is := assert.New(t)
is.Equal(Head([]int{1, 2, 3, 4}), 1)
}
func TestLast(t *testing.T) {
is := assert.New(t)
is.Equal(Last([]int{1, 2, 3, 4}), 4)
}
func TestTail(t *testing.T) {
is := assert.New(t)
is.Equal(Tail([]int{}), []int{})
is.Equal(Tail([]int{1}), []int{1})
is.Equal(Tail([]int{1, 2, 3, 4}), []int{2, 3, 4})
}
func TestInitial(t *testing.T) {
is := assert.New(t)
is.Equal(Initial([]int{}), []int{})
is.Equal(Initial([]int{1}), []int{1})
is.Equal(Initial([]int{1, 2, 3, 4}), []int{1, 2, 3})
}