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