-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcount_test.go
65 lines (53 loc) · 1.28 KB
/
count_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
package underscore
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Count_Can_Count_Numbers(t *testing.T) {
numbers := Range(1, 100)
count := Count(numbers, func(n int) bool {
return n%2 == 0
})
assert.Equal(t, 50, count)
}
type People struct {
Name string
Age int
Gender string
}
func Test_Count_Can_Count_People(t *testing.T) {
people := []People{
{Name: "Andy", Age: 43, Gender: "M"},
{Name: "Fred", Age: 33, Gender: "M"},
{Name: "Jack", Age: 23, Gender: "M"},
{Name: "Jill", Age: 43, Gender: "F"},
{Name: "Anna", Age: 33, Gender: "F"},
{Name: "Arya", Age: 23, Gender: "F"},
{Name: "Jane", Age: 13, Gender: "F"},
}
a := Count(people, func(p People) bool {
return strings.HasPrefix(p.Name, "A")
})
assert.Equal(t, 3, a)
females := Count(people, func(p People) bool {
return p.Gender == "F"
})
assert.Equal(t, 4, females)
males := Count(people, func(p People) bool {
return p.Gender == "M"
})
assert.Equal(t, 3, males)
over30 := Count(people, func(p People) bool {
return p.Age > 30
})
assert.Equal(t, 4, over30)
under30 := Count(people, func(p People) bool {
return p.Age < 30
})
assert.Equal(t, 3, under30)
under20 := Count(people, func(p People) bool {
return p.Age < 20
})
assert.Equal(t, 1, under20)
}