Skip to content

Commit f598331

Browse files
committed
Add multi-iteration side-effect tests for TaskSeq.isEmpty and TaskSeq.empty
1 parent f727e1c commit f598331

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,29 @@ let ``TaskSeq-empty multiple times in a taskSeq context`` () = task {
6262

6363
Array.isEmpty sq |> should be True
6464
}
65+
66+
[<Fact>]
67+
let ``TaskSeq-empty multiple times with side effects`` () = task {
68+
let mutable x = 0
69+
70+
let sq = taskSeq {
71+
yield! TaskSeq.empty<string>
72+
yield! TaskSeq.empty<string>
73+
x <- x + 1
74+
yield! TaskSeq.empty<string>
75+
x <- x + 1
76+
yield! TaskSeq.empty<string>
77+
x <- x + 1
78+
yield! TaskSeq.empty<string>
79+
x <- x + 1
80+
x <- x + 1
81+
}
82+
83+
// executing side effects once
84+
(TaskSeq.toArray >> Array.isEmpty) sq |> should be True
85+
x |> should equal 5
86+
87+
// twice
88+
(TaskSeq.toArray >> Array.isEmpty) sq |> should be True
89+
x |> should equal 10
90+
}

src/FSharp.Control.TaskSeq.Test/TaskSeq.IsEmpty.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ module SideEffects =
6060
|> Task.map (should be True)
6161
|> Task.map (fun () -> i |> should equal 2)
6262

63+
[<Fact>]
64+
let ``TaskSeq-isEmpty executes side effects each time`` () =
65+
let mutable i = 0
66+
67+
taskSeq {
68+
i <- i + 1
69+
i <- i + 1
70+
}
71+
|>> TaskSeq.isEmpty
72+
|>> TaskSeq.isEmpty
73+
|>> TaskSeq.isEmpty
74+
|> TaskSeq.isEmpty // 4th time: 8
75+
|> Task.map (should be True)
76+
|> Task.map (fun () -> i |> should equal 8)
77+
6378
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
6479
let ``TaskSeq-isEmpty returns false for non-empty`` variant =
6580
Gen.getSeqWithSideEffect variant

src/FSharp.Control.TaskSeq/TaskSeq.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ module TaskSeq =
168168
Internal.checkNonNull (nameof sources) sources
169169

170170
for ts in sources do
171+
// no null-check of inner taskseqs, similar to seq
171172
yield! (ts :> taskSeq<'T>)
172173
}
173174

0 commit comments

Comments
 (0)