-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTaskSeq.TakeWhile.Tests.fs
277 lines (225 loc) · 9.55 KB
/
TaskSeq.TakeWhile.Tests.fs
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
module TaskSeq.Tests.TakeWhile
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.takeWhile
// TaskSeq.takeWhileAsync
// TaskSeq.takeWhileInclusive
// TaskSeq.takeWhileInclusiveAsync
//
[<AutoOpen>]
module With =
/// The only real difference in semantics between the base and the *Inclusive variant lies in whether the final item is returned.
/// NOTE the semantics are very clear on only propagating a single failing item in the inclusive case.
let getFunction inclusive isAsync =
match inclusive, isAsync with
| false, false -> TaskSeq.takeWhile
| false, true -> fun pred -> TaskSeq.takeWhileAsync (pred >> Task.fromResult)
| true, false -> TaskSeq.takeWhileInclusive
| true, true -> fun pred -> TaskSeq.takeWhileInclusiveAsync (pred >> Task.fromResult)
/// This is the base condition as one would expect in actual code
let inline cond x = x <> 6
/// For each of the tests below, we add a guard that will trigger if the predicate is passed items known to be beyond the
/// first failing item in the known sequence (which is 1..10)
let inline condWithGuard x =
let res = cond x
if x > 6 then
failwith "Test sequence should not be enumerated beyond the first item failing the predicate"
res
module EmptySeq =
// TaskSeq-takeWhile+A stands for:
// takeWhile + takeWhileAsync etc.
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-takeWhile+A has no effect`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.takeWhile ((=) 12)
|> verifyEmpty
do!
Gen.getEmptyVariant variant
|> TaskSeq.takeWhileAsync ((=) 12 >> Task.fromResult)
|> verifyEmpty
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-takeWhileInclusive+A has no effect`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.takeWhileInclusive ((=) 12)
|> verifyEmpty
do!
Gen.getEmptyVariant variant
|> TaskSeq.takeWhileInclusiveAsync ((=) 12 >> Task.fromResult)
|> verifyEmpty
}
module Immutable =
// TaskSeq-takeWhile+A stands for:
// takeWhile + takeWhileAsync etc.
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-takeWhile+A filters correctly`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhile condWithGuard
|> verifyDigitsAsString "ABCDE"
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x })
|> verifyDigitsAsString "ABCDE"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-takeWhile+A does not pick first item when false`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhile ((=) 0)
|> verifyDigitsAsString ""
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileAsync ((=) 0 >> Task.fromResult)
|> verifyDigitsAsString ""
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-takeWhileInclusive+A filters correctly`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileInclusive condWithGuard
|> verifyDigitsAsString "ABCDEF"
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileInclusiveAsync (fun x -> task { return condWithGuard x })
|> verifyDigitsAsString "ABCDEF"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-takeWhileInclusive+A always pick at least the first item`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileInclusive ((=) 0)
|> verifyDigitsAsString "A"
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileInclusiveAsync ((=) 0 >> Task.fromResult)
|> verifyDigitsAsString "A"
}
module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-takeWhile+A filters correctly`` variant = task {
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.takeWhile condWithGuard
|> verifyDigitsAsString "ABCDE"
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x })
|> verifyDigitsAsString "ABCDE"
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-takeWhileInclusive+A filters correctly`` variant = task {
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.takeWhileInclusive condWithGuard
|> verifyDigitsAsString "ABCDEF"
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.takeWhileInclusiveAsync (fun x -> task { return condWithGuard x })
|> verifyDigitsAsString "ABCDEF"
}
[<Theory>]
[<InlineData(false, false)>]
[<InlineData(false, true)>]
[<InlineData(true, false)>]
[<InlineData(true, true)>]
let ``TaskSeq-takeWhile and variants prove it does not read beyond the failing yield`` (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 returned
yield x * 2 // the failing item (which will also be yielded in the result when using *Inclusive)
x <- x + 1 // we are proving we never get here
}
let expected = if inclusive then [| 42; 84 |] else [| 42 |]
let! first = items |> functionToTest |> TaskSeq.toArrayAsync
let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync
first |> should equal expected
repeat |> should equal expected
x |> should equal 42
}
[<Theory>]
[<InlineData(false, false)>]
[<InlineData(false, true)>]
[<InlineData(true, false)>]
[<InlineData(true, true)>]
let ``TaskSeq-takeWhile and variants prove side effects are executed`` (inclusive, isAsync) = task {
let mutable x = 41
let functionToTest = getFunction inclusive isAsync ((>) 50)
let items = taskSeq {
x <- x + 1
yield x
x <- x + 2
yield x * 2
x <- x + 200 // as previously proven, we should not trigger this
}
let expectedFirst = if inclusive then [| 42; 44 * 2 |] else [| 42 |]
let expectedRepeat = if inclusive then [| 45; 47 * 2 |] else [| 45 |]
x |> should equal 41
let! first = items |> functionToTest |> TaskSeq.toArrayAsync
x |> should equal 44
let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync
x |> should equal 47
first |> should equal expectedFirst
repeat |> should equal expectedRepeat
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-takeWhile consumes the prefix of a longer sequence, with mutation`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! first =
TaskSeq.takeWhile (fun x -> x < 5) ts
|> TaskSeq.toArrayAsync
let expected = [| 1..4 |]
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.takeWhile (fun x -> x < 5) ts
|> TaskSeq.toArrayAsync
repeat |> should not' (equal expected)
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-takeWhileInclusiveAsync consumes the prefix for a longer sequence, with mutation`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! first =
TaskSeq.takeWhileInclusiveAsync (fun x -> task { return x < 5 }) ts
|> TaskSeq.toArrayAsync
let expected = [| 1..5 |]
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.takeWhileInclusiveAsync (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-takeWhile and variants excludes all items after predicate fails`` (inclusive, isAsync) =
let functionToTest = With.getFunction inclusive isAsync
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> functionToTest (fun x -> x <= 2)
|> verifyDigitsAsString (if inclusive then "ABBC" else "ABB")
[<Theory>]
[<InlineData(false, false)>]
[<InlineData(false, true)>]
[<InlineData(true, false)>]
[<InlineData(true, true)>]
let ``TaskSeq-takeWhile and variants stops consuming after predicate fails`` (inclusive, isAsync) =
let functionToTest = With.getFunction inclusive isAsync
seq {
yield! [ 1; 2; 2; 3; 3 ]
yield failwith "Too far"
}
|> TaskSeq.ofSeq
|> functionToTest (fun x -> x <= 2)
|> verifyDigitsAsString (if inclusive then "ABBC" else "ABB")