@@ -27,11 +27,42 @@ module EmptySeq =
27
27
|> TaskSeq.toListAsync
28
28
|> Task.map ( List.isEmpty >> should be True)
29
29
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
+
30
61
module Immutable =
31
62
[<Theory; ClassData( typeof< TestImmTaskSeq>) >]
32
63
let ``TaskSeq - takeWhile filters correctly`` variant =
33
64
Gen.getSeqImmutable variant
34
- |> TaskSeq.takeWhile ( fun x -> x <> 6 )
65
+ |> TaskSeq.takeWhile condWithGuard
35
66
|> TaskSeq.map char
36
67
|> TaskSeq.map ((+) '@' )
37
68
|> TaskSeq.toArrayAsync
@@ -40,7 +71,7 @@ module Immutable =
40
71
[<Theory; ClassData( typeof< TestImmTaskSeq>) >]
41
72
let ``TaskSeq - takeWhileAsync filters correctly`` variant =
42
73
Gen.getSeqImmutable variant
43
- |> TaskSeq.takeWhileAsync ( fun x -> task { return x <> 6 })
74
+ |> TaskSeq.takeWhileAsync ( fun x -> task { return condWithGuard x })
44
75
|> TaskSeq.map char
45
76
|> TaskSeq.map ((+) '@' )
46
77
|> TaskSeq.toArrayAsync
@@ -50,7 +81,7 @@ module SideEffects =
50
81
[<Theory; ClassData( typeof< TestSideEffectTaskSeq>) >]
51
82
let ``TaskSeq - takeWhile filters correctly`` variant =
52
83
Gen.getSeqWithSideEffect variant
53
- |> TaskSeq.takeWhile ( fun x -> x <> 6 )
84
+ |> TaskSeq.takeWhile condWithGuard
54
85
|> TaskSeq.map char
55
86
|> TaskSeq.map ((+) '@' )
56
87
|> TaskSeq.toArrayAsync
@@ -59,7 +90,7 @@ module SideEffects =
59
90
[<Theory; ClassData( typeof< TestSideEffectTaskSeq>) >]
60
91
let ``TaskSeq - takeWhileAsync filters correctly`` variant =
61
92
Gen.getSeqWithSideEffect variant
62
- |> TaskSeq.takeWhileAsync ( fun x -> task { return x <> 6 })
93
+ |> TaskSeq.takeWhileAsync ( fun x -> task { return condWithGuard x })
63
94
|> TaskSeq.map char
64
95
|> TaskSeq.map ((+) '@' )
65
96
|> TaskSeq.toArrayAsync
0 commit comments