Skip to content

Add TaskSeq.singleton to the surface area #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for TaskSeq.singleton
  • Loading branch information
abelbraaksma committed Nov 24, 2022
commit d5c5bbf6d573aab95c448b90fffc02d67e66bd56
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