Skip to content

Commit

Permalink
Cover termination
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Dec 11, 2022
1 parent 2f55986 commit c514766
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/FSharp.Control.TaskSeq.Test/TaskSeq.TakeWhile.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,42 @@ module EmptySeq =
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)

module Terminates =

[<Fact>]
let ``TaskSeq-takeWhile stops after predicate fails`` () =
seq { 1; 2; 3; failwith "Too far" }
|> TaskSeq.ofSeq
|> TaskSeq.takeWhile (fun x -> x <= 2)
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
|> Task.map (String >> should equal "AB")

[<Fact>]
let ``TaskSeq-takeWhileAsync stops after predicate fails`` () =
taskSeq { 1; 2; 3; failwith "Too far" }
|> TaskSeq.takeWhileAsync (fun x -> task { return x <= 2 })
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
|> Task.map (String >> should equal "AB")

// This is the base condition as one would expect in actual code
let inline cond x = x <> 6

// For each of the tests below, we add a guard that will trigger if the predicate is passed items known to be beyond the
// first failing item in the known sequence (which is 1..10)
let inline condWithGuard x =
let res = cond x
if x > 6 then failwith "Test sequence should not be enumerated beyond the first item failing the predicate"
res

module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-takeWhile filters correctly`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.takeWhile (fun x -> x <> 6)
|> TaskSeq.takeWhile condWithGuard
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
Expand All @@ -40,7 +71,7 @@ module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-takeWhileAsync filters correctly`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileAsync (fun x -> task { return x <> 6 })
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x })
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
Expand All @@ -50,7 +81,7 @@ module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-takeWhile filters correctly`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.takeWhile (fun x -> x <> 6)
|> TaskSeq.takeWhile condWithGuard
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
Expand All @@ -59,7 +90,7 @@ module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-takeWhileAsync filters correctly`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.takeWhileAsync (fun x -> task { return x <> 6 })
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x })
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
Expand Down

0 comments on commit c514766

Please sign in to comment.