-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskSeq.FindBack.Tests.fs
More file actions
345 lines (278 loc) · 13.3 KB
/
TaskSeq.FindBack.Tests.fs
File metadata and controls
345 lines (278 loc) · 13.3 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
module TaskSeq.Tests.FindBack
open System.Collections.Generic
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.findBack
// TaskSeq.findBackAsync
// TaskSeq.tryFindBack
// TaskSeq.tryFindBackAsync
// TaskSeq.findIndexBack
// TaskSeq.findIndexBackAsync
// TaskSeq.tryFindIndexBack
// TaskSeq.tryFindIndexBackAsync
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg
<| fun () -> TaskSeq.findBack (fun _ -> false) null
assertNullArg
<| fun () -> TaskSeq.findBackAsync (fun _ -> Task.fromResult false) null
assertNullArg
<| fun () -> TaskSeq.tryFindBack (fun _ -> false) null
assertNullArg
<| fun () -> TaskSeq.tryFindBackAsync (fun _ -> Task.fromResult false) null
assertNullArg
<| fun () -> TaskSeq.findIndexBack (fun _ -> false) null
assertNullArg
<| fun () -> TaskSeq.findIndexBackAsync (fun _ -> Task.fromResult false) null
assertNullArg
<| fun () -> TaskSeq.tryFindIndexBack (fun _ -> false) null
assertNullArg
<| fun () -> TaskSeq.tryFindIndexBackAsync (fun _ -> Task.fromResult false) null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-findBack raises KeyNotFoundException`` variant =
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.findBack ((=) 12)
|> Task.ignore
|> should throwAsyncExact typeof<KeyNotFoundException>
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-findBackAsync raises KeyNotFoundException`` variant =
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.findBackAsync (fun x -> task { return x = 12 })
|> Task.ignore
|> should throwAsyncExact typeof<KeyNotFoundException>
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-tryFindBack returns None`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.tryFindBack ((=) 12)
|> Task.map (should be None')
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-tryFindBackAsync returns None`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.tryFindBackAsync (fun x -> task { return x = 12 })
|> Task.map (should be None')
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-findIndexBack raises KeyNotFoundException`` variant =
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.findIndexBack ((=) 12)
|> Task.ignore
|> should throwAsyncExact typeof<KeyNotFoundException>
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-findIndexBackAsync raises KeyNotFoundException`` variant =
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.findIndexBackAsync (fun x -> task { return x = 12 })
|> Task.ignore
|> should throwAsyncExact typeof<KeyNotFoundException>
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-tryFindIndexBack returns None`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.tryFindIndexBack ((=) 12)
|> Task.map (should be None')
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-tryFindIndexBackAsync returns None`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.tryFindIndexBackAsync (fun x -> task { return x = 12 })
|> Task.map (should be None')
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findBack sad path raises KeyNotFoundException`` variant =
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.findBack ((=) 0) // dummy tasks sequence starts at 1
|> Task.ignore
|> should throwAsyncExact typeof<KeyNotFoundException>
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findBackAsync sad path raises KeyNotFoundException`` variant =
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.findBackAsync (fun x -> task { return x = 0 }) // dummy tasks sequence starts at 1
|> Task.ignore
|> should throwAsyncExact typeof<KeyNotFoundException>
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findBack happy path returns last match`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.findBack (fun x -> x < 6 && x > 3) // matches 4, 5 — last is 5
|> Task.map (should equal 5)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findBackAsync happy path returns last match`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.findBackAsync (fun x -> task { return x < 6 && x > 3 }) // matches 4, 5 — last is 5
|> Task.map (should equal 5)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findBack happy path returns last item`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.findBack (fun x -> x <= 10) // all items qualify; last is 10
|> Task.map (should equal 10)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findBackAsync happy path returns last item`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.findBackAsync (fun x -> task { return x <= 10 }) // all items qualify; last is 10
|> Task.map (should equal 10)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findBack happy path returns only matching item`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.findBack ((=) 7) // exactly one match
|> Task.map (should equal 7)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findIndexBack sad path raises KeyNotFoundException`` variant =
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.findIndexBack ((=) 0) // dummy tasks sequence starts at 1
|> Task.ignore
|> should throwAsyncExact typeof<KeyNotFoundException>
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findIndexBackAsync sad path raises KeyNotFoundException`` variant =
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.findIndexBackAsync (fun x -> task { return x = 0 }) // dummy tasks sequence starts at 1
|> Task.ignore
|> should throwAsyncExact typeof<KeyNotFoundException>
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findIndexBack happy path returns last matching index`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.findIndexBack (fun x -> x < 6 && x > 3) // matches indices 3, 4 — last is 4
|> Task.map (should equal 4)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findIndexBackAsync happy path returns last matching index`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.findIndexBackAsync (fun x -> task { return x < 6 && x > 3 }) // matches indices 3, 4 — last is 4
|> Task.map (should equal 4)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findIndexBack happy path returns last index when all match`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.findIndexBack (fun x -> x <= 10) // all 10 items qualify; last index is 9
|> Task.map (should equal 9)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-findIndexBack happy path single match`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.findIndexBack ((=) 1) // value 1 is at index 0
|> Task.map (should equal 0)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryFindBack sad path returns None`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.tryFindBack ((=) 0) // dummy tasks sequence starts at 1
|> Task.map (should be None')
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryFindBackAsync sad path returns None`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.tryFindBackAsync (fun x -> task { return x = 0 }) // dummy tasks sequence starts at 1
|> Task.map (should be None')
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryFindBack happy path returns last match`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.tryFindBack (fun x -> x < 6 && x > 3)
|> Task.map (should equal (Some 5))
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryFindBackAsync happy path returns last match`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.tryFindBackAsync (fun x -> task { return x < 6 && x > 3 })
|> Task.map (should equal (Some 5))
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryFindIndexBack sad path returns None`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.tryFindIndexBack ((=) 0)
|> Task.map (should be None')
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryFindIndexBackAsync sad path returns None`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.tryFindIndexBackAsync (fun x -> task { return x = 0 })
|> Task.map (should be None')
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryFindIndexBack happy path returns last matching index`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.tryFindIndexBack (fun x -> x < 6 && x > 3)
|> Task.map (should equal (Some 4))
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryFindIndexBackAsync happy path returns last matching index`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.tryFindIndexBackAsync (fun x -> task { return x < 6 && x > 3 })
|> Task.map (should equal (Some 4))
module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-findBack consumes the entire sequence`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.findBack (fun x -> x < 6 && x > 3)
|> Task.map (should equal 5)
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-tryFindBack consumes the entire sequence`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.tryFindBack (fun x -> x < 6 && x > 3)
|> Task.map (should equal (Some 5))
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-findIndexBack consumes the entire sequence`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.findIndexBack (fun x -> x < 6 && x > 3)
|> Task.map (should equal 4)
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-tryFindIndexBack consumes the entire sequence`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.tryFindIndexBack (fun x -> x < 6 && x > 3)
|> Task.map (should equal (Some 4))
[<Fact>]
let ``TaskSeq-findBack _specialcase_ unlike findBack, findBack always evaluates the full sequence`` () = task {
let mutable i = 0
let ts = taskSeq {
yield 42
i <- i + 1 // side effect after the matching yield
yield 1 // an item after the match
}
// findBack must find the LAST match so it evaluates everything
let! found = ts |> TaskSeq.findBack ((=) 42)
found |> should equal 42
i |> should equal 1 // side effect WAS executed — findBack consumed all items
}
[<Fact>]
let ``TaskSeq-tryFindBack _specialcase_ always evaluates the full sequence`` () = task {
let mutable i = 0
let ts = taskSeq {
yield 42
i <- i + 1
yield 1
}
let! found = ts |> TaskSeq.tryFindBack ((=) 42)
found |> should equal (Some 42)
i |> should equal 1 // side effect WAS executed
}
[<Fact>]
let ``TaskSeq-findBack _specialcase_ returns the last of multiple matches`` () = task {
let ts = taskSeq { yield! [ 3; 5; 3; 7; 3 ] }
let! found = ts |> TaskSeq.findBack ((=) 3)
found |> should equal 3 // value is 3 (last of three matches)
}
[<Fact>]
let ``TaskSeq-findIndexBack _specialcase_ returns the last matching index among multiple matches`` () = task {
let ts = taskSeq { yield! [ 3; 5; 3; 7; 3 ] }
let! found = ts |> TaskSeq.findIndexBack ((=) 3)
found |> should equal 4 // index 4 is the last 3
}
[<Fact>]
let ``TaskSeq-tryFindBack _specialcase_ returns last match when multiple exist`` () = task {
let ts = taskSeq { yield! [ 1; 2; 3; 4; 5; 4; 3; 2; 1 ] }
let! found = ts |> TaskSeq.tryFindBack (fun x -> x > 3)
found |> should equal (Some 4) // last element > 3 is the 4 at index 5
}
[<Fact>]
let ``TaskSeq-tryFindIndexBack _specialcase_ returns last matching index when multiple exist`` () = task {
let ts = taskSeq { yield! [ 1; 2; 3; 4; 5; 4; 3; 2; 1 ] }
let! found = ts |> TaskSeq.tryFindIndexBack (fun x -> x > 3)
found |> should equal (Some 5) // last element > 3 is at index 5
}
[<Fact>]
let ``TaskSeq-findIndexBack _specialcase_ single-element sequence`` () = task {
let ts = taskSeq { yield 42 }
let! found = ts |> TaskSeq.findIndexBack ((=) 42)
found |> should equal 0
}
[<Fact>]
let ``TaskSeq-tryFindBack _specialcase_ single-element no match returns None`` () = task {
let ts = taskSeq { yield 42 }
let! found = ts |> TaskSeq.tryFindBack ((=) 99)
found |> should be None'
}