|
| 1 | +module TaskSeq.Tests.Singleton |
| 2 | + |
| 3 | +open System.Threading.Tasks |
| 4 | +open Xunit |
| 5 | +open FsUnit.Xunit |
| 6 | +open FsToolkit.ErrorHandling |
| 7 | + |
| 8 | +open FSharp.Control |
| 9 | + |
| 10 | +module EmptySeq = |
| 11 | + |
| 12 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 13 | + let ``TaskSeq-singleton with empty has length one`` variant = |
| 14 | + taskSeq { |
| 15 | + yield! TaskSeq.singleton 10 |
| 16 | + yield! Gen.getEmptyVariant variant |
| 17 | + } |
| 18 | + |> TaskSeq.exactlyOne |
| 19 | + |> Task.map (should equal 10) |
| 20 | + |
| 21 | +module Other = |
| 22 | + [<Fact>] |
| 23 | + let ``TaskSeq-singleton creates a sequence of one`` () = |
| 24 | + TaskSeq.singleton 42 |
| 25 | + |> TaskSeq.exactlyOne |
| 26 | + |> Task.map (should equal 42) |
| 27 | + |
| 28 | + [<Fact>] |
| 29 | + let ``TaskSeq-singleton can be yielded multiple times`` () = |
| 30 | + let singleton = TaskSeq.singleton 42 |
| 31 | + |
| 32 | + taskSeq { |
| 33 | + yield! singleton |
| 34 | + yield! singleton |
| 35 | + yield! singleton |
| 36 | + yield! singleton |
| 37 | + } |
| 38 | + |> TaskSeq.toList |
| 39 | + |> should equal [ 42; 42; 42; 42 ] |
| 40 | + |
| 41 | + [<Fact>] |
| 42 | + let ``TaskSeq-singleton with isEmpty`` () = |
| 43 | + TaskSeq.singleton 42 |
| 44 | + |> TaskSeq.isEmpty |
| 45 | + |> Task.map (should be False) |
| 46 | + |
| 47 | + [<Fact>] |
| 48 | + let ``TaskSeq-singleton with append`` () = |
| 49 | + TaskSeq.singleton 42 |
| 50 | + |> TaskSeq.append (TaskSeq.singleton 42) |
| 51 | + |> TaskSeq.toList |
| 52 | + |> should equal [ 42; 42 ] |
| 53 | + |
| 54 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 55 | + let ``TaskSeq-singleton with collect`` variant = |
| 56 | + Gen.getSeqImmutable variant |
| 57 | + |> TaskSeq.collect TaskSeq.singleton |
| 58 | + |> verify1To10 |
| 59 | + |
| 60 | + [<Fact>] |
| 61 | + let ``TaskSeq-singleton does not throw when getting Current before MoveNext`` () = task { |
| 62 | + let enumerator = (TaskSeq.singleton 42).GetAsyncEnumerator() |
| 63 | + let defaultValue = enumerator.Current // should return the default value for int |
| 64 | + defaultValue |> should equal 0 |
| 65 | + } |
| 66 | + |
| 67 | + [<Fact>] |
| 68 | + let ``TaskSeq-singleton does not throw when getting Current after last MoveNext`` () = task { |
| 69 | + let enumerator = (TaskSeq.singleton 42).GetAsyncEnumerator() |
| 70 | + let! isNext = enumerator.MoveNextAsync() |
| 71 | + isNext |> should be True |
| 72 | + let value = enumerator.Current // the first and only value |
| 73 | + value |> should equal 42 |
| 74 | + |
| 75 | + // move past the end |
| 76 | + let! isNext = enumerator.MoveNextAsync() |
| 77 | + isNext |> should be False |
| 78 | + let defaultValue = enumerator.Current // should return the default value for int |
| 79 | + defaultValue |> should equal 0 |
| 80 | + } |
0 commit comments