From 063e38533390613140ab9464f82ca7f098616514 Mon Sep 17 00:00:00 2001 From: Abel Braaksma Date: Thu, 24 Nov 2022 22:28:27 +0100 Subject: [PATCH] Add tests for `TaskSeq.singleton` --- .../FSharp.Control.TaskSeq.Test.fsproj | 1 + .../TaskSeq.Singleton.Tests.fs | 80 +++++++++++++++++++ src/FSharp.Control.TaskSeq.Test/TestUtils.fs | 5 ++ 3 files changed, 86 insertions(+) create mode 100644 src/FSharp.Control.TaskSeq.Test/TaskSeq.Singleton.Tests.fs diff --git a/src/FSharp.Control.TaskSeq.Test/FSharp.Control.TaskSeq.Test.fsproj b/src/FSharp.Control.TaskSeq.Test/FSharp.Control.TaskSeq.Test.fsproj index 0d0b6019..ee7c16c8 100644 --- a/src/FSharp.Control.TaskSeq.Test/FSharp.Control.TaskSeq.Test.fsproj +++ b/src/FSharp.Control.TaskSeq.Test/FSharp.Control.TaskSeq.Test.fsproj @@ -39,6 +39,7 @@ + diff --git a/src/FSharp.Control.TaskSeq.Test/TaskSeq.Singleton.Tests.fs b/src/FSharp.Control.TaskSeq.Test/TaskSeq.Singleton.Tests.fs new file mode 100644 index 00000000..ea2d2368 --- /dev/null +++ b/src/FSharp.Control.TaskSeq.Test/TaskSeq.Singleton.Tests.fs @@ -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 = + + [)>] + 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 = + [] + let ``TaskSeq-singleton creates a sequence of one`` () = + TaskSeq.singleton 42 + |> TaskSeq.exactlyOne + |> Task.map (should equal 42) + + [] + 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 ] + + [] + let ``TaskSeq-singleton with isEmpty`` () = + TaskSeq.singleton 42 + |> TaskSeq.isEmpty + |> Task.map (should be False) + + [] + let ``TaskSeq-singleton with append`` () = + TaskSeq.singleton 42 + |> TaskSeq.append (TaskSeq.singleton 42) + |> TaskSeq.toList + |> should equal [ 42; 42 ] + + [)>] + let ``TaskSeq-singleton with collect`` variant = + Gen.getSeqImmutable variant + |> TaskSeq.collect TaskSeq.singleton + |> verify1To10 + + [] + 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 + } + + [] + 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 + } diff --git a/src/FSharp.Control.TaskSeq.Test/TestUtils.fs b/src/FSharp.Control.TaskSeq.Test/TestUtils.fs index 91baf383..dd4873b3 100644 --- a/src/FSharp.Control.TaskSeq.Test/TestUtils.fs +++ b/src/FSharp.Control.TaskSeq.Test/TestUtils.fs @@ -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)) }