forked from thoas/go-funk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresence_test.go
105 lines (77 loc) · 1.85 KB
/
presence_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
96
97
98
99
100
101
102
103
104
105
package funk
import (
"testing"
"github.com/stretchr/testify/assert"
)
var f = &Foo{
ID: 1,
FirstName: "Dark",
LastName: "Vador",
Age: 30,
Bar: &Bar{
Name: "Test",
},
}
var b = &Foo{
ID: 2,
FirstName: "Florent",
LastName: "Messa",
Age: 28,
}
var c = &Foo{
ID: 3,
FirstName: "Harald",
LastName: "Nordgren",
Age: 27,
}
var results = []*Foo{f, c}
func TestContains(t *testing.T) {
is := assert.New(t)
is.True(Contains([]string{"foo", "bar"}, "bar"))
is.True(Contains(results, f))
is.False(Contains(results, nil))
is.False(Contains(results, b))
is.True(Contains("florent", "rent"))
is.False(Contains("florent", "gilles"))
mapping := ToMap(results, "ID")
is.True(Contains(mapping, 1))
is.False(Contains(mapping, 2))
}
func TestEvery(t *testing.T) {
is := assert.New(t)
is.True(Every([]string{"foo", "bar", "baz"}, "bar", "foo"))
is.True(Every(results, f, c))
is.False(Every(results, nil))
is.False(Every(results, f, b))
is.True(Every("florent", "rent", "flo"))
is.False(Every("florent", "rent", "gilles"))
mapping := ToMap(results, "ID")
is.True(Every(mapping, 1, 3))
is.False(Every(mapping, 2, 3))
}
func TestIndexOf(t *testing.T) {
is := assert.New(t)
is.Equal(IndexOf([]string{"foo", "bar"}, "bar"), 1)
is.Equal(IndexOf(results, f), 0)
is.Equal(IndexOf(results, b), -1)
}
func TestLastIndexOf(t *testing.T) {
is := assert.New(t)
is.Equal(LastIndexOf([]string{"foo", "bar", "bar"}, "bar"), 2)
is.Equal(LastIndexOf([]int{1, 2, 2, 3}, 2), 2)
is.Equal(LastIndexOf([]int{1, 2, 2, 3}, 4), -1)
}
func TestFilter(t *testing.T) {
is := assert.New(t)
r := Filter([]int{1, 2, 3, 4}, func(x int) bool {
return x%2 == 0
})
is.Equal(r, []int{2, 4})
}
func TestFind(t *testing.T) {
is := assert.New(t)
r := Find([]int{1, 2, 3, 4}, func(x int) bool {
return x%2 == 0
})
is.Equal(r, 2)
}