Skip to content

Commit 565f3f5

Browse files
committed
Apply review comments: redesign two tests to better test edge cases and for clarity (tx @bartelink)
1 parent 0aa0742 commit 565f3f5

File tree

1 file changed

+34
-51
lines changed

1 file changed

+34
-51
lines changed

src/FSharp.Control.TaskSeq.Test/TaskSeq.SkipWhile.Tests.fs

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ open FSharp.Control
1616

1717
exception SideEffectPastEnd of string
1818

19-
[<AutoOpen>]
20-
module With =
21-
/// The only real difference in semantics between the base and the *Inclusive variant lies in whether the final item is skipped.
22-
let getFunction inclusive isAsync =
23-
match inclusive, isAsync with
24-
| false, false -> TaskSeq.skipWhile
25-
| false, true -> fun pred -> TaskSeq.skipWhileAsync (pred >> Task.fromResult)
26-
| true, false -> TaskSeq.skipWhileInclusive
27-
| true, true -> fun pred -> TaskSeq.skipWhileInclusiveAsync (pred >> Task.fromResult)
2819

2920
module EmptySeq =
3021

@@ -179,41 +170,37 @@ module SideEffects =
179170
|> verifyDigitsAsString "GHIJ"
180171
}
181172

182-
[<Theory>]
183-
[<InlineData(false, false)>]
184-
[<InlineData(false, true)>]
185-
[<InlineData(true, false)>]
186-
[<InlineData(true, true)>]
187-
let ``TaskSeq-skipWhileXXX prove it reads the entire input stream`` (inclusive, isAsync) = task {
188-
let mutable x = 42 // for this test, the potential mutation should not actually occur
189-
let functionToTest = getFunction inclusive isAsync ((=) 42)
173+
[<Fact>]
174+
let ``TaskSeq-skipWhile and variants prove it reads the entire input stream`` () = task {
175+
let mutable x = 42
190176

191177
let items = taskSeq {
192-
yield x // Always passes the test; always skipped
193-
yield x * 2 // Fails the test, skipped depending on "inclusive"
178+
yield x
179+
yield x * 2
194180
x <- x + 1 // we are proving we ALWAYS get here
195181
}
196182

197-
x |> should equal 42
198-
let! first = items |> functionToTest |> TaskSeq.toArrayAsync
183+
let testSkipper skipper expected = task {
184+
let! first = items |> skipper |> TaskSeq.toArrayAsync
185+
return first |> should equal expected
186+
}
187+
188+
do! testSkipper (TaskSeq.skipWhile ((=) 42)) [| 84 |]
199189
x |> should equal 43
200-
first |> should equal (if inclusive then [||] else [| 84 |])
201190

202-
let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync
191+
do! testSkipper (TaskSeq.skipWhileInclusive ((=) 43)) [||]
203192
x |> should equal 44
204193

205-
repeat
206-
|> should equal (if inclusive then [| 86 |] else [| 43; 86 |])
194+
do! testSkipper (TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x = 44))) [| 88 |]
195+
x |> should equal 45
196+
197+
do! testSkipper (TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x = 45))) [||]
198+
x |> should equal 46
207199
}
208200

209-
[<Theory>]
210-
[<InlineData(false, false)>]
211-
[<InlineData(false, true)>]
212-
[<InlineData(true, false)>]
213-
[<InlineData(true, true)>]
214-
let ``TaskSeq-skipWhileXXX prove side effects are properly executed`` (inclusive, isAsync) = task {
201+
[<Fact>]
202+
let ``TaskSeq-skipWhile and variants prove side effects are properly executed`` () = task {
215203
let mutable x = 41
216-
let functionToTest = getFunction inclusive isAsync (fun x -> x < 50)
217204

218205
let items = taskSeq {
219206
x <- x + 1
@@ -223,18 +210,22 @@ module SideEffects =
223210
x <- x + 200 // as previously proven, we should ALWAYS trigger this
224211
}
225212

226-
let expectedFirst = if inclusive then [||] else [| 88 |]
227-
let expectedRepeat = if inclusive then [| 494 |] else [| 245; 494 |]
213+
let testSkipper skipper expected = task {
214+
let! first = items |> skipper |> TaskSeq.toArrayAsync
215+
return first |> should equal expected
216+
}
228217

229-
x |> should equal 41
230-
let! first = items |> functionToTest |> TaskSeq.toArrayAsync
218+
do! testSkipper (TaskSeq.skipWhile ((=) 42)) [| 88 |]
231219
x |> should equal 244
232220

233-
let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync
221+
do! testSkipper (TaskSeq.skipWhileInclusive ((=) 245)) [||]
234222
x |> should equal 447
235223

236-
first |> should equal expectedFirst
237-
repeat |> should equal expectedRepeat
224+
do! testSkipper (TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x = 448))) [| 900 |]
225+
x |> should equal 650
226+
227+
do! testSkipper (TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x = 651))) [||]
228+
x |> should equal 853
238229
}
239230

240231
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
@@ -278,12 +269,8 @@ module SideEffects =
278269
}
279270

280271
module Other =
281-
[<Theory>]
282-
[<InlineData(false, false)>]
283-
[<InlineData(false, true)>]
284-
[<InlineData(true, false)>]
285-
[<InlineData(true, true)>]
286-
let ``TaskSeq-skipWhileXXX should include all items after predicate fails`` (inclusive, isAsync) = task {
272+
[<Fact>]
273+
let ``TaskSeq-skipWhileXXX should include all items after predicate fails`` () = task {
287274
do!
288275
[ 1; 2; 2; 3; 3; 2; 1 ]
289276
|> TaskSeq.ofSeq
@@ -309,12 +296,8 @@ module Other =
309296
|> verifyDigitsAsString "CBA"
310297
}
311298

312-
[<Theory>]
313-
[<InlineData(false, false)>]
314-
[<InlineData(false, true)>]
315-
[<InlineData(true, false)>]
316-
[<InlineData(true, true)>]
317-
let ``TaskSeq-skipWhileXXX stops consuming after predicate fails`` (inclusive, isAsync) =
299+
[<Fact>]
300+
let ``TaskSeq-skipWhileXXX stops consuming after predicate fails`` () =
318301
let testSkipper skipper =
319302
fun () ->
320303
seq {

0 commit comments

Comments
 (0)