|
| 1 | +module TaskSeq.Tests.Tail |
| 2 | + |
| 3 | +open System |
| 4 | +open Xunit |
| 5 | +open FsUnit.Xunit |
| 6 | +open FsToolkit.ErrorHandling |
| 7 | + |
| 8 | +open FSharp.Control |
| 9 | + |
| 10 | +// |
| 11 | +// TaskSeq.tail |
| 12 | +// TaskSeq.tryTail |
| 13 | +// |
| 14 | + |
| 15 | +module EmptySeq = |
| 16 | + |
| 17 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 18 | + let ``TaskSeq-tail throws`` variant = task { |
| 19 | + fun () -> Gen.getEmptyVariant variant |> TaskSeq.tail |> Task.ignore |
| 20 | + |> should throwAsyncExact typeof<ArgumentException> |
| 21 | + } |
| 22 | + |
| 23 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 24 | + let ``TaskSeq-tryTail returns None`` variant = task { |
| 25 | + let! nothing = Gen.getEmptyVariant variant |> TaskSeq.tryTail |
| 26 | + nothing |> should be None' |
| 27 | + } |
| 28 | + |
| 29 | + [<Fact>] |
| 30 | + let ``TaskSeq-tail executes side effect`` () = task { |
| 31 | + let mutable x = 0 |
| 32 | + |
| 33 | + fun () -> taskSeq { do x <- x + 1 } |> TaskSeq.tail |> Task.ignore |
| 34 | + |> should throwAsyncExact typeof<ArgumentException> |
| 35 | + |
| 36 | + // side effect must have run! |
| 37 | + x |> should equal 1 |
| 38 | + } |
| 39 | + |
| 40 | + [<Fact>] |
| 41 | + let ``TaskSeq-tryTail executes side effect`` () = task { |
| 42 | + let mutable x = 0 |
| 43 | + |
| 44 | + let! nothing = taskSeq { do x <- x + 1 } |> TaskSeq.tryTail |
| 45 | + nothing |> should be None' |
| 46 | + |
| 47 | + // side effect must have run! |
| 48 | + x |> should equal 1 |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +module Immutable = |
| 53 | + let verifyTail tail = |
| 54 | + tail |
| 55 | + |> TaskSeq.toArrayAsync |
| 56 | + |> Task.map (should equal [| 2..10 |]) |
| 57 | + |
| 58 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 59 | + let ``TaskSeq-tail gets the tail items`` variant = task { |
| 60 | + let ts = Gen.getSeqImmutable variant |
| 61 | + |
| 62 | + let! tail = TaskSeq.tail ts |
| 63 | + do! verifyTail tail |
| 64 | + |
| 65 | + let! tail = TaskSeq.tail ts //immutable, so re-iteration does not change outcome |
| 66 | + do! verifyTail tail |
| 67 | + } |
| 68 | + |
| 69 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 70 | + let ``TaskSeq-tryTail gets the tail item`` variant = task { |
| 71 | + let ts = Gen.getSeqImmutable variant |
| 72 | + |
| 73 | + match! TaskSeq.tryTail ts with |
| 74 | + | Some tail -> do! verifyTail tail |
| 75 | + | x -> do x |> should not' (be None') |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + [<Fact>] |
| 80 | + let ``TaskSeq-tail return empty from a singleton sequence`` () = task { |
| 81 | + let ts = taskSeq { yield 42 } |
| 82 | + |
| 83 | + let! tail = TaskSeq.tail ts |
| 84 | + do! verifyEmpty tail |
| 85 | + } |
| 86 | + |
| 87 | + [<Fact>] |
| 88 | + let ``TaskSeq-tryTail gets the only item in a singleton sequence`` () = task { |
| 89 | + let ts = taskSeq { yield 42 } |
| 90 | + |
| 91 | + match! TaskSeq.tryTail ts with |
| 92 | + | Some tail -> do! verifyEmpty tail |
| 93 | + | x -> do x |> should not' (be None') |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +module SideEffects = |
| 98 | + [<Fact>] |
| 99 | + let ``TaskSeq-tail does not execute side effect after the first item in singleton`` () = task { |
| 100 | + let mutable x = 42 |
| 101 | + |
| 102 | + let one = taskSeq { |
| 103 | + yield x |
| 104 | + x <- x + 1 // <--- we should never get here |
| 105 | + } |
| 106 | + |
| 107 | + let! _ = one |> TaskSeq.tail |
| 108 | + let! _ = one |> TaskSeq.tail // side effect, re-iterating! |
| 109 | + |
| 110 | + x |> should equal 42 |
| 111 | + } |
| 112 | + |
| 113 | + [<Fact>] |
| 114 | + let ``TaskSeq-tryTail does not execute execute side effect after first item in singleton`` () = task { |
| 115 | + let mutable x = 42 |
| 116 | + |
| 117 | + let one = taskSeq { |
| 118 | + yield x |
| 119 | + x <- x + 1 // <--- we should never get here |
| 120 | + } |
| 121 | + |
| 122 | + let! _ = one |> TaskSeq.tryTail |
| 123 | + let! _ = one |> TaskSeq.tryTail |
| 124 | + |
| 125 | + // side effect, reiterating causes it to execute again! |
| 126 | + x |> should equal 42 |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + [<Fact>] |
| 131 | + let ``TaskSeq-tail executes side effect partially`` () = task { |
| 132 | + let mutable x = 42 |
| 133 | + |
| 134 | + let ts = taskSeq { |
| 135 | + x <- x + 1 // <--- executed on tail, but not materializing rest |
| 136 | + yield 1 |
| 137 | + x <- x + 1 // <--- not executed on tail, but on materializing rest |
| 138 | + yield 2 |
| 139 | + x <- x + 1 // <--- id |
| 140 | + } |
| 141 | + |
| 142 | + let! tail1 = ts |> TaskSeq.tail |
| 143 | + x |> should equal 43 // test side effect runs 1x |
| 144 | + |
| 145 | + let! tail2 = ts |> TaskSeq.tail |
| 146 | + x |> should equal 44 // test side effect ran again only 1x |
| 147 | + |
| 148 | + let! len = TaskSeq.length tail1 |
| 149 | + x |> should equal 46 // now 2nd & 3rd side effect runs, but not the first |
| 150 | + len |> should equal 1 |
| 151 | + |
| 152 | + let! len = TaskSeq.length tail2 |
| 153 | + x |> should equal 48 // now again 2nd & 3rd side effect runs, but not the first |
| 154 | + len |> should equal 1 |
| 155 | + } |
| 156 | + |
| 157 | + [<Fact>] |
| 158 | + let ``TaskSeq-tryTail executes side effect partially`` () = task { |
| 159 | + let mutable x = 42 |
| 160 | + |
| 161 | + let ts = taskSeq { |
| 162 | + x <- x + 1 // <--- executed on tail, but not materializing rest |
| 163 | + yield 1 |
| 164 | + x <- x + 1 // <--- not executed on tail, but on materializing rest |
| 165 | + yield 2 |
| 166 | + x <- x + 1 // <--- id |
| 167 | + } |
| 168 | + |
| 169 | + let! tail1 = ts |> TaskSeq.tryTail |
| 170 | + x |> should equal 43 // test side effect runs 1x |
| 171 | + |
| 172 | + let! tail2 = ts |> TaskSeq.tryTail |
| 173 | + x |> should equal 44 // test side effect ran again only 1x |
| 174 | + |
| 175 | + let! len = TaskSeq.length tail1.Value |
| 176 | + x |> should equal 46 // now 2nd side effect runs, but not the first |
| 177 | + len |> should equal 1 |
| 178 | + |
| 179 | + let! len = TaskSeq.length tail2.Value |
| 180 | + x |> should equal 48 // now again 2nd side effect runs, but not the first |
| 181 | + len |> should equal 1 |
| 182 | + } |
0 commit comments