-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathany_test.go
275 lines (265 loc) · 6.14 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package go2linq
import (
"errors"
"fmt"
"iter"
"slices"
"strings"
"testing"
"github.com/solsw/errorhelper"
"github.com/solsw/iterhelper"
)
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/AnyTest.cs
func TestAny_int(t *testing.T) {
type args struct {
source iter.Seq[int]
}
tests := []struct {
name string
args args
want bool
}{
{name: "EmptySequenceWithoutPredicate",
args: args{
source: Empty[int](),
},
want: false,
},
{name: "NonEmptySequenceWithoutPredicate",
args: args{
source: iterhelper.VarSeq(0),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := Any(tt.args.source)
if got != tt.want {
t.Errorf("Any() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnyPred_int(t *testing.T) {
type args struct {
source iter.Seq[int]
predicate func(int) bool
}
tests := []struct {
name string
args args
want bool
wantErr bool
expectedErr error
}{
{name: "NullPredicate",
args: args{
source: iterhelper.VarSeq(1, 3, 5),
predicate: nil,
},
wantErr: true,
expectedErr: ErrNilPredicate,
},
{name: "EmptySequenceWithPredicate",
args: args{
source: Empty[int](),
predicate: func(x int) bool { return x > 10 },
},
want: false,
},
{name: "NonEmptySequenceWithPredicateMatchingElement",
args: args{
source: iterhelper.VarSeq(1, 5, 20, 30),
predicate: func(x int) bool { return x > 10 },
},
want: true,
},
{name: "NonEmptySequenceWithPredicateNotMatchingElement",
args: args{
source: iterhelper.VarSeq(1, 5, 8, 9),
predicate: func(x int) bool { return x > 10 },
},
want: false,
},
{name: "SequenceIsNotEvaluatedAfterFirstMatch",
args: args{
source: errorhelper.Must(Select(
iterhelper.VarSeq(10, 2, 0, 3),
func(x int) int { return 10 / x },
)),
predicate: func(y int) bool { return y > 2 },
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := AnyPred(tt.args.source, tt.args.predicate)
if (err != nil) != tt.wantErr {
t.Errorf("AnyPred() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
if !errors.Is(err, tt.expectedErr) {
t.Errorf("AnyPred() error = %v, expectedErr %v", err, tt.expectedErr)
}
return
}
if got != tt.want {
t.Errorf("AnyPred() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnyPred_any(t *testing.T) {
type args struct {
source iter.Seq[any]
predicate func(any) bool
}
tests := []struct {
name string
args args
want bool
}{
{name: "1",
args: args{
source: iterhelper.VarSeq[any](1, 2, 3, 4),
predicate: func(e any) bool { return e.(int) == 4 },
},
want: true,
},
{name: "2",
args: args{
source: iterhelper.VarSeq[any]("one", "two", "three", "four"),
predicate: func(e any) bool { return len(e.(string)) == 4 },
},
want: true,
},
{name: "3",
args: args{
source: iterhelper.VarSeq[any](1, 2, "three", "four"),
predicate: func(e any) bool { _, ok := e.(int); return ok },
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := AnyPred(tt.args.source, tt.args.predicate)
if got != tt.want {
t.Errorf("AnyPred() = %v, want %v", got, tt.want)
}
})
}
}
// first example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.any
func ExampleAny_ex1() {
numbers := []int{1, 2}
hasElements, _ := Any(iterhelper.VarSeq(numbers...))
var what string
if hasElements {
what = "is not"
} else {
what = "is"
}
fmt.Printf("The list %s empty.\n", what)
// Output:
// The list is not empty.
}
// AnyEx2 example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.any
func ExampleAny_ex2() {
people := []Person{
{
LastName: "Haas",
Pets: []Pet{
{Name: "Barley", Age: 10},
{Name: "Boots", Age: 14},
{Name: "Whiskers", Age: 6},
},
},
{
LastName: "Fakhouri",
Pets: []Pet{
{Name: "Snowball", Age: 1},
},
},
{
LastName: "Antebi",
Pets: []Pet{},
},
{
LastName: "Philips",
Pets: []Pet{
{Name: "Sweetie", Age: 2},
{Name: "Rover", Age: 13},
},
},
}
// Determine which people have a non-empty Pet array.
where, _ := Where(
slices.Values(people),
func(person Person) bool { return errorhelper.Must(Any(slices.Values(person.Pets))) },
)
names, _ := Select(
where,
func(person Person) string { return person.LastName },
)
for name := range names {
fmt.Println(name)
}
// Output:
// Haas
// Fakhouri
// Philips
}
// AnyEx3 example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.any
func ExampleAnyPred_ex1() {
pets := []Pet{
{Name: "Barley", Age: 8, Vaccinated: true},
{Name: "Boots", Age: 4, Vaccinated: false},
{Name: "Whiskers", Age: 1, Vaccinated: false},
}
// Determine whether any pets over Age 1 are also unvaccinated.
unvaccinated, _ := AnyPred(
slices.Values(pets),
func(pet Pet) bool { return pet.Age > 1 && pet.Vaccinated == false },
)
var what string
if unvaccinated {
what = "are"
} else {
what = "are not any"
}
fmt.Printf("There %s unvaccinated animals over age one.\n", what)
// Output:
// There are unvaccinated animals over age one.
}
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/quantifier-operations#query-expression-syntax-examples
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/quantifier-operations#any
func ExampleAnyPred_ex2() {
markets := []Market{
{Name: "Emily's", Items: []string{"kiwi", "cheery", "banana"}},
{Name: "Kim's", Items: []string{"melon", "mango", "olive"}},
{Name: "Adam's", Items: []string{"kiwi", "apple", "orange"}},
}
where, _ := Where(
slices.Values(markets),
func(m Market) bool {
return errorhelper.Must(AnyPred(
slices.Values(m.Items),
func(item string) bool { return strings.HasPrefix(item, "o") },
))
},
)
names, _ := Select(where, func(m Market) string { return m.Name })
for name := range names {
fmt.Printf("%s market\n", name)
}
// Output:
// Kim's market
// Adam's market
}