Skip to content

Commit

Permalink
Add tests for TaskSeq.singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
abelbraaksma committed Nov 24, 2022
1 parent 7987075 commit 063e385
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Compile Include="TaskSeq.Map.Tests.fs" />
<Compile Include="TaskSeq.OfXXX.Tests.fs" />
<Compile Include="TaskSeq.Pick.Tests.fs" />
<Compile Include="TaskSeq.Singleton.Tests.fs" />
<Compile Include="TaskSeq.Tail.Tests.fs" />
<Compile Include="TaskSeq.ToXXX.Tests.fs" />
<Compile Include="TaskSeq.Zip.Tests.fs" />
Expand Down
80 changes: 80 additions & 0 deletions src/FSharp.Control.TaskSeq.Test/TaskSeq.Singleton.Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module TaskSeq.Tests.Singleton

open System.Threading.Tasks
open Xunit
open FsUnit.Xunit
open FsToolkit.ErrorHandling

open FSharp.Control

module EmptySeq =

[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-singleton with empty has length one`` variant =
taskSeq {
yield! TaskSeq.singleton 10
yield! Gen.getEmptyVariant variant
}
|> TaskSeq.exactlyOne
|> Task.map (should equal 10)

module Other =
[<Fact>]
let ``TaskSeq-singleton creates a sequence of one`` () =
TaskSeq.singleton 42
|> TaskSeq.exactlyOne
|> Task.map (should equal 42)

[<Fact>]
let ``TaskSeq-singleton can be yielded multiple times`` () =
let singleton = TaskSeq.singleton 42

taskSeq {
yield! singleton
yield! singleton
yield! singleton
yield! singleton
}
|> TaskSeq.toList
|> should equal [ 42; 42; 42; 42 ]

[<Fact>]
let ``TaskSeq-singleton with isEmpty`` () =
TaskSeq.singleton 42
|> TaskSeq.isEmpty
|> Task.map (should be False)

[<Fact>]
let ``TaskSeq-singleton with append`` () =
TaskSeq.singleton 42
|> TaskSeq.append (TaskSeq.singleton 42)
|> TaskSeq.toList
|> should equal [ 42; 42 ]

[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-singleton with collect`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.collect TaskSeq.singleton
|> verify1To10

[<Fact>]
let ``TaskSeq-singleton does not throw when getting Current before MoveNext`` () = task {
let enumerator = (TaskSeq.singleton 42).GetAsyncEnumerator()
let defaultValue = enumerator.Current // should return the default value for int
defaultValue |> should equal 0
}

[<Fact>]
let ``TaskSeq-singleton does not throw when getting Current after last MoveNext`` () = task {
let enumerator = (TaskSeq.singleton 42).GetAsyncEnumerator()
let! isNext = enumerator.MoveNextAsync()
isNext |> should be True
let value = enumerator.Current // the first and only value
value |> should equal 42

// move past the end
let! isNext = enumerator.MoveNextAsync()
isNext |> should be False
let defaultValue = enumerator.Current // should return the default value for int
defaultValue |> should equal 0
}
5 changes: 5 additions & 0 deletions src/FSharp.Control.TaskSeq.Test/TestUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ module TestUtils =
|> TaskSeq.toArrayAsync
|> Task.map (Array.isEmpty >> should be True)

let verify1To10 ts =
ts
|> TaskSeq.toArrayAsync
|> Task.map (should equal [| 1..10 |])

/// Delays (no spin-wait!) between 20 and 70ms, assuming a 15.6ms resolution clock
let longDelay () = task { do! Task.Delay(Random().Next(20, 70)) }

Expand Down

0 comments on commit 063e385

Please sign in to comment.