-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskSeq.SkipWhile.Tests.fs
More file actions
332 lines (272 loc) · 11.2 KB
/
TaskSeq.SkipWhile.Tests.fs
File metadata and controls
332 lines (272 loc) · 11.2 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
module TaskSeq.Tests.skipWhile
open System
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.skipWhile
// TaskSeq.skipWhileAsync
// TaskSeq.skipWhileInclusive
// TaskSeq.skipWhileInclusiveAsync
//
exception SideEffectPastEnd of string
[<AutoOpen>]
module With =
/// The only real difference in semantics between the base and the *Inclusive variant lies in whether the final item is skipped.
let getFunction inclusive isAsync =
match inclusive, isAsync with
| false, false -> TaskSeq.skipWhile
| false, true -> fun pred -> TaskSeq.skipWhileAsync (pred >> Task.fromResult)
| true, false -> TaskSeq.skipWhileInclusive
| true, true -> fun pred -> TaskSeq.skipWhileInclusiveAsync (pred >> Task.fromResult)
module EmptySeq =
// TaskSeq-skipWhile+A stands for:
// skipWhile + skipWhileAsync etc.
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-skipWhile+A has no effect`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.skipWhile ((=) 12)
|> verifyEmpty
do!
Gen.getEmptyVariant variant
|> TaskSeq.skipWhileAsync ((=) 12 >> Task.fromResult)
|> verifyEmpty
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-skipWhileInclusive+A has no effect`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.skipWhileInclusive ((=) 12)
|> verifyEmpty
do!
Gen.getEmptyVariant variant
|> TaskSeq.skipWhileInclusiveAsync ((=) 12 >> Task.fromResult)
|> verifyEmpty
}
module Immutable =
// TaskSeq-skipWhile+A stands for:
// skipWhile + skipWhileAsync etc.
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhile+A filters correctly`` variant = task {
// truth table for f(x) = x < 5
// 1 2 3 4 5 6 7 8 9 10
// T T T T F F F F F F (stops at first F)
// x x x x _ _ _ _ _ _ (skips exclusive)
// A B C D E F G H I J
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhile (fun x -> x < 5)
|> verifyDigitsAsString "EFGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileAsync (fun x -> task { return x < 5 })
|> verifyDigitsAsString "EFGHIJ"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhile+A does not skip first item when false`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhile ((=) 0)
|> verifyDigitsAsString "ABCDEFGHIJ" // all 10 remain!
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileAsync ((=) 0 >> Task.fromResult)
|> verifyDigitsAsString "ABCDEFGHIJ" // all 10 remain!
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhileInclusive+A filters correctly`` variant = task {
// truth table for f(x) = x < 5
// 1 2 3 4 5 6 7 8 9 10
// T T T T F F F F F F (stops at first F)
// x x x x x _ _ _ _ _ (skips inclusively)
// A B C D E F G H I J
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusive (fun x -> x < 5)
|> verifyDigitsAsString "FGHIJ" // last 4
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 5 })
|> verifyDigitsAsString "FGHIJ"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhileInclusive+A returns the empty sequence if always true`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusive (fun x -> x > -1) // always true
|> verifyEmpty
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x > -1))
|> verifyEmpty
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-skipWhileInclusive+A always skips at least the first item`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusive ((=) 0)
|> verifyDigitsAsString "BCDEFGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.skipWhileInclusiveAsync ((=) 0 >> Task.fromResult)
|> verifyDigitsAsString "BCDEFGHIJ"
}
module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-skipWhile+A filters correctly`` variant = task {
// truth table for f(x) = x < 6
// 1 2 3 4 5 6 7 8 9 10
// T T T T T F F F F F (stops at first F)
// x x x x x _ _ _ _ _ (skips exclusively)
// A B C D E F G H I J
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.skipWhile (fun x -> x < 6)
|> verifyDigitsAsString "FGHIJ"
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.skipWhileAsync (fun x -> task { return x < 6 })
|> verifyDigitsAsString "FGHIJ"
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-skipWhileInclusive+A filters correctly`` variant = task {
// truth table for f(x) = x < 6
// 1 2 3 4 5 6 7 8 9 10
// T T T T T F F F F F (stops at first F)
// x x x x x x _ _ _ _ (skips inclusively)
// A B C D E F G H I J
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.skipWhileInclusive (fun x -> x < 6)
|> verifyDigitsAsString "GHIJ"
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 6 })
|> verifyDigitsAsString "GHIJ"
}
[<Theory>]
[<InlineData(false, false)>]
[<InlineData(false, true)>]
[<InlineData(true, false)>]
[<InlineData(true, true)>]
let ``TaskSeq-skipWhileXXX prove it reads the entire input stream`` (inclusive, isAsync) = task {
let mutable x = 42 // for this test, the potential mutation should not actually occur
let functionToTest = getFunction inclusive isAsync ((=) 42)
let items = taskSeq {
yield x // Always passes the test; always skipped
yield x * 2 // Fails the test, skipped depending on "inclusive"
x <- x + 1 // we are proving we ALWAYS get here
}
x |> should equal 42
let! first = items |> functionToTest |> TaskSeq.toArrayAsync
x |> should equal 43
first |> should equal (if inclusive then [||] else [| 84 |])
let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync
x |> should equal 44
repeat
|> should equal (if inclusive then [| 86 |] else [| 43; 86 |])
}
[<Theory>]
[<InlineData(false, false)>]
[<InlineData(false, true)>]
[<InlineData(true, false)>]
[<InlineData(true, true)>]
let ``TaskSeq-skipWhileXXX prove side effects are properly executed`` (inclusive, isAsync) = task {
let mutable x = 41
let functionToTest = getFunction inclusive isAsync (fun x -> x < 50)
let items = taskSeq {
x <- x + 1
yield x
x <- x + 2
yield x * 2
x <- x + 200 // as previously proven, we should ALWAYS trigger this
}
let expectedFirst = if inclusive then [||] else [| 88 |]
let expectedRepeat = if inclusive then [| 494 |] else [| 245; 494 |]
x |> should equal 41
let! first = items |> functionToTest |> TaskSeq.toArrayAsync
x |> should equal 244
let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync
x |> should equal 447
first |> should equal expectedFirst
repeat |> should equal expectedRepeat
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-skipWhile consumes the prefix of a longer sequence, with mutation`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! first =
TaskSeq.skipWhile (fun x -> x < 5) ts
|> TaskSeq.toArrayAsync
let expected = [| 5..10 |]
first |> should equal expected
// side effect, reiterating causes it to resume from where we left it (minus the failing item)
// which means the original sequence has now changed due to the side effect
let! repeat =
TaskSeq.skipWhile (fun x -> x < 5) ts
|> TaskSeq.toArrayAsync
repeat |> should not' (equal expected)
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-skipWhileInclusiveAsync consumes the prefix for a longer sequence, with mutation`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! first =
TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 5 }) ts
|> TaskSeq.toArrayAsync
let expected = [| 6..10 |]
first |> should equal expected
// side effect, reiterating causes it to resume from where we left it (minus the failing item)
// which means the original sequence has now changed due to the side effect
let! repeat =
TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 5 }) ts
|> TaskSeq.toArrayAsync
repeat |> should not' (equal expected)
}
module Other =
[<Theory>]
[<InlineData(false, false)>]
[<InlineData(false, true)>]
[<InlineData(true, false)>]
[<InlineData(true, true)>]
let ``TaskSeq-skipWhileXXX should include all items after predicate fails`` (inclusive, isAsync) = task {
do!
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> TaskSeq.skipWhile (fun x -> x <= 2)
|> verifyDigitsAsString "CCBA"
do!
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> TaskSeq.skipWhileInclusive (fun x -> x <= 2)
|> verifyDigitsAsString "CBA"
do!
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x <= 2))
|> verifyDigitsAsString "CCBA"
do!
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x <= 2))
|> verifyDigitsAsString "CBA"
}
[<Theory>]
[<InlineData(false, false)>]
[<InlineData(false, true)>]
[<InlineData(true, false)>]
[<InlineData(true, true)>]
let ``TaskSeq-skipWhileXXX stops consuming after predicate fails`` (inclusive, isAsync) =
let testSkipper skipper =
fun () ->
seq {
yield! [ 1; 2; 2; 3; 3 ]
yield SideEffectPastEnd "Too far" |> raise
}
|> TaskSeq.ofSeq
|> skipper
|> consumeTaskSeq
|> should throwAsyncExact typeof<SideEffectPastEnd>
testSkipper (TaskSeq.skipWhile (fun x -> x <= 2))
testSkipper (TaskSeq.skipWhileInclusive (fun x -> x <= 2))
testSkipper (TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x <= 2)))
testSkipper (TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x <= 2)))