@@ -27,11 +27,42 @@ module EmptySeq =
2727 |> TaskSeq.toListAsync
2828 |> Task.map ( List.isEmpty >> should be True)
2929
30+ module Terminates =
31+
32+ [<Fact>]
33+ let ``TaskSeq - takeWhile stops after predicate fails`` () =
34+ seq { 1 ; 2 ; 3 ; failwith " Too far" }
35+ |> TaskSeq.ofSeq
36+ |> TaskSeq.takeWhile ( fun x -> x <= 2 )
37+ |> TaskSeq.map char
38+ |> TaskSeq.map ((+) '@' )
39+ |> TaskSeq.toArrayAsync
40+ |> Task.map ( String >> should equal " AB" )
41+
42+ [<Fact>]
43+ let ``TaskSeq - takeWhileAsync stops after predicate fails`` () =
44+ taskSeq { 1 ; 2 ; 3 ; failwith " Too far" }
45+ |> TaskSeq.takeWhileAsync ( fun x -> task { return x <= 2 })
46+ |> TaskSeq.map char
47+ |> TaskSeq.map ((+) '@' )
48+ |> TaskSeq.toArrayAsync
49+ |> Task.map ( String >> should equal " AB" )
50+
51+ // This is the base condition as one would expect in actual code
52+ let inline cond x = x <> 6
53+
54+ // For each of the tests below, we add a guard that will trigger if the predicate is passed items known to be beyond the
55+ // first failing item in the known sequence (which is 1..10)
56+ let inline condWithGuard x =
57+ let res = cond x
58+ if x > 6 then failwith " Test sequence should not be enumerated beyond the first item failing the predicate"
59+ res
60+
3061module Immutable =
3162 [<Theory; ClassData( typeof< TestImmTaskSeq>) >]
3263 let ``TaskSeq - takeWhile filters correctly`` variant =
3364 Gen.getSeqImmutable variant
34- |> TaskSeq.takeWhile ( fun x -> x <> 6 )
65+ |> TaskSeq.takeWhile condWithGuard
3566 |> TaskSeq.map char
3667 |> TaskSeq.map ((+) '@' )
3768 |> TaskSeq.toArrayAsync
@@ -40,7 +71,7 @@ module Immutable =
4071 [<Theory; ClassData( typeof< TestImmTaskSeq>) >]
4172 let ``TaskSeq - takeWhileAsync filters correctly`` variant =
4273 Gen.getSeqImmutable variant
43- |> TaskSeq.takeWhileAsync ( fun x -> task { return x <> 6 })
74+ |> TaskSeq.takeWhileAsync ( fun x -> task { return condWithGuard x })
4475 |> TaskSeq.map char
4576 |> TaskSeq.map ((+) '@' )
4677 |> TaskSeq.toArrayAsync
@@ -50,7 +81,7 @@ module SideEffects =
5081 [<Theory; ClassData( typeof< TestSideEffectTaskSeq>) >]
5182 let ``TaskSeq - takeWhile filters correctly`` variant =
5283 Gen.getSeqWithSideEffect variant
53- |> TaskSeq.takeWhile ( fun x -> x <> 6 )
84+ |> TaskSeq.takeWhile condWithGuard
5485 |> TaskSeq.map char
5586 |> TaskSeq.map ((+) '@' )
5687 |> TaskSeq.toArrayAsync
@@ -59,7 +90,7 @@ module SideEffects =
5990 [<Theory; ClassData( typeof< TestSideEffectTaskSeq>) >]
6091 let ``TaskSeq - takeWhileAsync filters correctly`` variant =
6192 Gen.getSeqWithSideEffect variant
62- |> TaskSeq.takeWhileAsync ( fun x -> task { return x <> 6 })
93+ |> TaskSeq.takeWhileAsync ( fun x -> task { return condWithGuard x })
6394 |> TaskSeq.map char
6495 |> TaskSeq.map ((+) '@' )
6596 |> TaskSeq.toArrayAsync
0 commit comments