Skip to content

Commit 463aaae

Browse files
committed
Cover termination
1 parent 2f55986 commit 463aaae

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,41 @@ module EmptySeq =
2727
|> TaskSeq.toListAsync
2828
|> Task.map (List.isEmpty >> should be True)
2929

30+
module Terminates =
31+
[<Fact>]
32+
let ``TaskSeq-takeWhile stops after predicate fails`` () =
33+
seq { 1; 2; 3; failwith "Too far" }
34+
|> TaskSeq.ofSeq
35+
|> TaskSeq.takeWhile (fun x -> x <= 2)
36+
|> TaskSeq.map char
37+
|> TaskSeq.map ((+) '@')
38+
|> TaskSeq.toArrayAsync
39+
|> Task.map (String >> should equal "AB")
40+
41+
[<Fact>]
42+
let ``TaskSeq-takeWhileAsync stops after predicate fails`` () =
43+
taskSeq { 1; 2; 3; failwith "Too far" }
44+
|> TaskSeq.takeWhileAsync (fun x -> task { return x <= 2 })
45+
|> TaskSeq.map char
46+
|> TaskSeq.map ((+) '@')
47+
|> TaskSeq.toArrayAsync
48+
|> Task.map (String >> should equal "AB")
49+
50+
// This is the base condition as one would expect in actual code
51+
let inline cond x = x <> 6
52+
53+
// For each of the tests below, we add a guard that will trigger if the predicate is passed items known to be beyond the
54+
// first failing item in the known sequence (which is 1..10)
55+
let inline condWithGuard x =
56+
let res = cond x
57+
if x > 6 then failwith "Test sequence should not be enumerated beyond the first item failing the predicate"
58+
res
59+
3060
module Immutable =
3161
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
3262
let ``TaskSeq-takeWhile filters correctly`` variant =
3363
Gen.getSeqImmutable variant
34-
|> TaskSeq.takeWhile (fun x -> x <> 6)
64+
|> TaskSeq.takeWhile condWithGuard
3565
|> TaskSeq.map char
3666
|> TaskSeq.map ((+) '@')
3767
|> TaskSeq.toArrayAsync
@@ -40,7 +70,7 @@ module Immutable =
4070
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
4171
let ``TaskSeq-takeWhileAsync filters correctly`` variant =
4272
Gen.getSeqImmutable variant
43-
|> TaskSeq.takeWhileAsync (fun x -> task { return x <> 6 })
73+
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x })
4474
|> TaskSeq.map char
4575
|> TaskSeq.map ((+) '@')
4676
|> TaskSeq.toArrayAsync
@@ -50,7 +80,7 @@ module SideEffects =
5080
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
5181
let ``TaskSeq-takeWhile filters correctly`` variant =
5282
Gen.getSeqWithSideEffect variant
53-
|> TaskSeq.takeWhile (fun x -> x <> 6)
83+
|> TaskSeq.takeWhile condWithGuard
5484
|> TaskSeq.map char
5585
|> TaskSeq.map ((+) '@')
5686
|> TaskSeq.toArrayAsync
@@ -59,7 +89,7 @@ module SideEffects =
5989
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
6090
let ``TaskSeq-takeWhileAsync filters correctly`` variant =
6191
Gen.getSeqWithSideEffect variant
62-
|> TaskSeq.takeWhileAsync (fun x -> task { return x <> 6 })
92+
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x })
6393
|> TaskSeq.map char
6494
|> TaskSeq.map ((+) '@')
6595
|> TaskSeq.toArrayAsync

0 commit comments

Comments
 (0)