-
Notifications
You must be signed in to change notification settings - Fork 273
/
predicate_test.go
192 lines (187 loc) · 4.12 KB
/
predicate_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package funk
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestAllPredicates(t *testing.T) {
type args struct {
value interface{}
predicates interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Sanity string predicates",
args: args{
value: "test",
predicates: []func(string) bool{
func(v string) bool { return strings.Contains(v, "est") },
func(v string) bool { return len(v) < 10 },
func(v string) bool { return len(v) > 2 },
},
},
want: true,
},
{
name: "Sanity int predicates",
args: args{
value: 4,
predicates: []func(int) bool{
func(v int) bool { return v < 5 },
func(v int) bool { return v > 2 },
},
},
want: true,
},
{
name: "Failed predicate",
args: args{
value: "test",
predicates: []func(string) bool{
func(v string) bool { return strings.Contains(v, "est") },
func(v string) bool { return len(v) > 10 },
func(v string) bool { return len(v) > 2 },
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AllPredicates(tt.args.value, tt.args.predicates); got != tt.want {
t.Errorf("AllPredicates() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnyPredicates(t *testing.T) {
type args struct {
value interface{}
predicates interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Sanity string predicates",
args: args{
value: "test",
predicates: []func(string) bool{
func(v string) bool { return strings.Contains(v, "est") },
func(v string) bool { return len(v) > 10 },
func(v string) bool { return len(v) < 2 },
},
},
want: true,
},
{
name: "Sanity int predicates",
args: args{
value: 4,
predicates: []func(int) bool{
func(v int) bool { return v > 5 },
func(v int) bool { return v > 2 },
},
},
want: true,
},
{
name: "All failed predicate",
args: args{
value: "test",
predicates: []func(string) bool{
func(v string) bool { return !strings.Contains(v, "est") },
func(v string) bool { return len(v) > 10 },
func(v string) bool { return len(v) < 2 },
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AnyPredicates(tt.args.value, tt.args.predicates); got != tt.want {
t.Errorf("AnyPredicates() = %v, want %v", got, tt.want)
}
})
}
}
func TestPredicatesImplPanics(t *testing.T) {
type args struct {
value interface{}
wantedAnswer bool
predicates interface{}
}
tests := []struct {
name string
args args
}{
{
name: "predicates are not collection",
args: args{
value: nil,
wantedAnswer: false,
predicates: nil,
},
},
{
name: "predicates are collection of strings",
args: args{
value: nil,
wantedAnswer: false,
predicates: []string{"hey"},
},
},
{
name: "predicate has 2 out parameters",
args: args{
value: nil,
wantedAnswer: false,
predicates: []func(string) (bool, error){ func(string) (bool, error){return true, nil}},
},
},
{
name: "predicate has out parameter of type string",
args: args{
value: nil,
wantedAnswer: false,
predicates: []func(string) string{ func(string) string{return ""}},
},
},
{
name: "predicate has 2 in parameters",
args: args{
value: nil,
wantedAnswer: false,
predicates: []func(string, bool) bool{ func(string, bool) bool{return true}},
},
},
{
name: "predicate has 0 in parameters",
args: args{
value: nil,
wantedAnswer: false,
predicates: []func() bool{ func() bool{return true}},
},
},
{
name: "value is not convertible to in parameter",
args: args{
value: 1,
wantedAnswer: false,
predicates: []func(string) bool{ func(string) bool{return true}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Panics(t, func() {predicatesImpl(tt.args.value, tt.args.wantedAnswer, tt.args.predicates)})
})
}
}