-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathany_test.go
146 lines (105 loc) · 3.67 KB
/
any_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package godash_test
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/thecasualcoder/godash"
"testing"
)
func TestSomeAndAny(t *testing.T) {
var funcs = map[string]func(interface{}, interface{}) (bool, error){
"Any()": godash.Any,
"Some()": godash.Some,
}
for fnName, fn := range funcs {
t.Run(fmt.Sprintf("%s should return err if predicate is not a function", fnName), func(t *testing.T) {
in := []int{1, 2, 3}
_, err := fn(in, "not a func")
assert.EqualError(t, err, "predicateFn has to be a function")
})
t.Run(fmt.Sprintf("%s should return err if predicate function do not take exactly one argument", fnName), func(t *testing.T) {
in := []int{1, 2, 3}
{
_, err := fn(in, func() {})
assert.EqualError(t, err, "predicate function has to take only one argument")
}
{
_, err := fn(in, func(int, int) {})
assert.EqualError(t, err, "predicate function has to take only one argument")
}
})
t.Run(fmt.Sprintf("%s should return err if predicate function do not return exactly one value", fnName), func(t *testing.T) {
in := []int{1, 2, 3}
{
_, err := fn(in, func(int) {})
assert.EqualError(t, err, "predicate function should return only one return value")
}
{
_, err := fn(in, func(int) (bool, bool) { return true, true })
assert.EqualError(t, err, "predicate function should return only one return value")
}
})
t.Run(fmt.Sprintf("%s should return err if predicate function's return value is not a boolean", fnName), func(t *testing.T) {
in := []int{1, 2, 3}
_, err := fn(in, func(int) int { return 0 })
assert.EqualError(t, err, "predicate function should return a boolean value")
})
t.Run(fmt.Sprintf("%s should return err if input is not a slice", fnName), func(t *testing.T) {
in := 1
_, err := fn(in, func(int) bool { return true })
assert.EqualError(t, err, "not implemented for (int)")
})
t.Run(fmt.Sprintf("%s should return err if there is a type mismatch between predicate function's argument and input slice", fnName), func(t *testing.T) {
in := []string{"hello", "world"}
_, err := fn(in, func(int) bool { return true })
assert.EqualError(t, err, "predicate function's argument (int) has to be (string)")
})
t.Run(fmt.Sprintf("%s should return true if predicate passes for at least one of the element in input slice", fnName), func(t *testing.T) {
in := []int{1, 1, 2, 3, 5, 8, 13}
output, err := fn(in, func(elem int) bool { return elem%5 == 0 })
assert.NoError(t, err)
assert.True(t, output)
})
t.Run(fmt.Sprintf("%s should return false if predicate fails for all the elements in input slice", fnName), func(t *testing.T) {
in := []int{1, 1, 2, 3, 5, 8, 13}
output, err := fn(in, func(num int) bool { return num%6 == 0 })
assert.NoError(t, err)
assert.False(t, output)
})
t.Run(fmt.Sprintf("%s should support structs", fnName), func(t *testing.T) {
type person struct {
name string
age int
}
in := []person{
{name: "John", age: 12},
{name: "Doe", age: 25},
}
{
output, err := fn(in, func(person person) bool { return person.age < 18 })
assert.NoError(t, err)
assert.True(t, output)
}
{
output, err := fn(in, func(person person) bool { return person.age < 30 })
assert.NoError(t, err)
assert.True(t, output)
}
})
}
}
func ExampleAny() {
input := []int{0, 1, 2, 3, 4}
output, _ := godash.Any(input, func(num int) bool {
return num%3 == 0
})
fmt.Println(output)
// Output: true
}
func ExampleSome() {
input := []int{0, 1, 2, 3, 4}
output, _ := godash.Some(input, func(num int) bool {
return num%3 == 0
})
fmt.Println(output)
// Output: true
}