-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreject_test.go
76 lines (63 loc) · 1.83 KB
/
reject_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
package enumerable_test
import (
"fmt"
"strings"
"testing"
E "github.com/darkhelmet/enumerable"
"github.com/stretchr/testify/assert"
)
func ExampleReject() {
words := strings.Fields("To be or not to be, that is the question!")
values := E.Reject(words, func(s string) bool {
return len(s) > 2
})
fmt.Println(values)
// Output: [To be or to is]
}
func TestRejectWorks(t *testing.T) {
ints := []int{2, 4, 6, 8}
values := E.Reject(ints, func(i int) bool {
return i > 5
})
assert.Equal(t, []int{2, 4}, values)
values = E.Reject(ints, func(i int) bool {
return i < 0
})
assert.Equal(t, values, values)
}
func TestRejectRequiresSlice(t *testing.T) {
assert.Panics(t, func() {
E.Reject(1, "doesn't matter")
}, "requires a slice as the first arg")
}
func TestRejectRequiresAFunc(t *testing.T) {
assert.Panics(t, func() {
E.Reject([]int{1}, "not a func")
}, "requires a func as the second arg")
}
func TestRejectRequiresASingleArgFunc(t *testing.T) {
assert.Panics(t, func() {
E.Reject([]int{1}, func() {})
}, "requires a single arg function")
assert.Panics(t, func() {
E.Reject([]int{1}, func(i, j int) {})
}, "requires a single arg function")
}
func TestRejectRequiresASingleReturnFunc(t *testing.T) {
assert.Panics(t, func() {
E.Reject([]int{1}, func(i int) {})
}, "requires a single arg function")
assert.Panics(t, func() {
E.Reject([]int{1}, func(i int) (int, int) { return 1, 0 })
}, "requires a single arg function")
}
func TestRejectRequiresFuncToTakeSliceArg(t *testing.T) {
assert.Panics(t, func() {
E.Reject([]int{1}, func(s string) int { return 0 })
}, "requires the func to take the same type as the slice")
}
func TestRejectRequiresFuncToReturnABool(t *testing.T) {
assert.Panics(t, func() {
E.Reject([]int{1}, func(i int) int { return i })
}, "requires the func to take the same type as the slice")
}