|
| 1 | +module FSharpy.Tests.Cast |
| 2 | + |
| 3 | +open System |
| 4 | + |
| 5 | +open Xunit |
| 6 | +open FsUnit.Xunit |
| 7 | +open FsToolkit.ErrorHandling |
| 8 | + |
| 9 | +open FSharpy |
| 10 | + |
| 11 | +// |
| 12 | +// TaskSeq.box |
| 13 | +// TaskSeq.unbox |
| 14 | +// TaskSeq.cast |
| 15 | +// |
| 16 | + |
| 17 | +/// Asserts that a sequence contains the char values 'A'..'J'. |
| 18 | +let validateSequence ts = |
| 19 | + ts |
| 20 | + |> TaskSeq.toSeqCachedAsync |
| 21 | + |> Task.map (Seq.map string) |
| 22 | + |> Task.map (String.concat "") |
| 23 | + |> Task.map (should equal "12345678910") |
| 24 | + |
| 25 | +module EmptySeq = |
| 26 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 27 | + let ``TaskSeq-box empty`` variant = Gen.getEmptyVariant variant |> TaskSeq.box |> verifyEmpty |
| 28 | + |
| 29 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 30 | + let ``TaskSeq-unbox empty`` variant = |
| 31 | + Gen.getEmptyVariant variant |
| 32 | + |> TaskSeq.box |
| 33 | + |> TaskSeq.unbox<int> |
| 34 | + |> verifyEmpty |
| 35 | + |
| 36 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 37 | + let ``TaskSeq-cast empty`` variant = |
| 38 | + Gen.getEmptyVariant variant |
| 39 | + |> TaskSeq.box |
| 40 | + |> TaskSeq.cast<int> |
| 41 | + |> verifyEmpty |
| 42 | + |
| 43 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 44 | + let ``TaskSeq-unbox empty to invalid type should not fail`` variant = |
| 45 | + Gen.getEmptyVariant variant |
| 46 | + |> TaskSeq.box |
| 47 | + |> TaskSeq.unbox<Guid> // cannot cast to int, but for empty sequences, the exception won't be thrown |
| 48 | + |> verifyEmpty |
| 49 | + |
| 50 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 51 | + let ``TaskSeq-cast empty to invalid type should not fail`` variant = |
| 52 | + Gen.getEmptyVariant variant |
| 53 | + |> TaskSeq.box |
| 54 | + |> TaskSeq.cast<string> // cannot cast to int, but for empty sequences, the exception won't be thrown |
| 55 | + |> verifyEmpty |
| 56 | + |
| 57 | +module Immutable = |
| 58 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 59 | + let ``TaskSeq-box`` variant = |
| 60 | + Gen.getSeqImmutable variant |
| 61 | + |> TaskSeq.box |
| 62 | + |> validateSequence |
| 63 | + |
| 64 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 65 | + let ``TaskSeq-unbox`` variant = |
| 66 | + Gen.getSeqImmutable variant |
| 67 | + |> TaskSeq.box |
| 68 | + |> TaskSeq.unbox<int> |
| 69 | + |> validateSequence |
| 70 | + |
| 71 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 72 | + let ``TaskSeq-cast`` variant = |
| 73 | + Gen.getSeqImmutable variant |
| 74 | + |> TaskSeq.box |
| 75 | + |> TaskSeq.cast<int> |
| 76 | + |> validateSequence |
| 77 | + |
| 78 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 79 | + let ``TaskSeq-unbox invalid type should throw`` variant = |
| 80 | + fun () -> |
| 81 | + Gen.getSeqImmutable variant |
| 82 | + |> TaskSeq.box |
| 83 | + |> TaskSeq.unbox<uint> // cannot unbox from int to uint, even though types have the same size |
| 84 | + |> TaskSeq.toArrayAsync |
| 85 | + |> Task.ignore |
| 86 | + |
| 87 | + |> should throwAsyncExact typeof<InvalidCastException> |
| 88 | + |
| 89 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 90 | + let ``TaskSeq-cast invalid type should throw`` variant = |
| 91 | + fun () -> |
| 92 | + Gen.getSeqImmutable variant |
| 93 | + |> TaskSeq.box |
| 94 | + |> TaskSeq.cast<string> |
| 95 | + |> TaskSeq.toArrayAsync |
| 96 | + |> Task.ignore |
| 97 | + |
| 98 | + |> should throwAsyncExact typeof<InvalidCastException> |
| 99 | + |
| 100 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 101 | + let ``TaskSeq-unbox invalid type should NOT throw before sequence is iterated`` variant = |
| 102 | + fun () -> |
| 103 | + Gen.getSeqImmutable variant |
| 104 | + |> TaskSeq.box |
| 105 | + |> TaskSeq.unbox<uint> // no iteration done |
| 106 | + |> ignore |
| 107 | + |
| 108 | + |> should not' (throw typeof<Exception>) |
| 109 | + |
| 110 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 111 | + let ``TaskSeq-cast invalid type should NOT throw before sequence is iterated`` variant = |
| 112 | + fun () -> |
| 113 | + Gen.getSeqImmutable variant |
| 114 | + |> TaskSeq.box |
| 115 | + |> TaskSeq.cast<string> // no iteration done |
| 116 | + |> ignore |
| 117 | + |
| 118 | + |> should not' (throw typeof<Exception>) |
| 119 | + |
| 120 | +module SideEffects = |
| 121 | + [<Fact>] |
| 122 | + let ``TaskSeq-box prove that it has no effect until executed`` () = |
| 123 | + let mutable i = 0 |
| 124 | + |
| 125 | + let ts = taskSeq { |
| 126 | + i <- i + 1 // we should not get here |
| 127 | + i <- i + 1 |
| 128 | + yield 42 |
| 129 | + i <- i + 1 |
| 130 | + } |
| 131 | + |
| 132 | + // point of this test: just calling 'box' won't execute anything of the sequence! |
| 133 | + let boxed = ts |> TaskSeq.box |> TaskSeq.box |> TaskSeq.box |
| 134 | + |
| 135 | + // no side effect until iterated |
| 136 | + i |> should equal 0 |
| 137 | + |
| 138 | + boxed |
| 139 | + |> TaskSeq.last |
| 140 | + |> Task.map (should equal 42) |
| 141 | + |> Task.map (fun () -> i = 9) |
| 142 | + |
| 143 | + [<Fact>] |
| 144 | + let ``TaskSeq-unbox prove that it has no effect until executed`` () = |
| 145 | + let mutable i = 0 |
| 146 | + |
| 147 | + let ts = taskSeq { |
| 148 | + i <- i + 1 // we should not get here |
| 149 | + i <- i + 1 |
| 150 | + yield box 42 |
| 151 | + i <- i + 1 |
| 152 | + } |
| 153 | + |
| 154 | + // point of this test: just calling 'unbox' won't execute anything of the sequence! |
| 155 | + let unboxed = ts |> TaskSeq.unbox |
| 156 | + |
| 157 | + // no side effect until iterated |
| 158 | + i |> should equal 0 |
| 159 | + |
| 160 | + unboxed |
| 161 | + |> TaskSeq.last |
| 162 | + |> Task.map (should equal 42) |
| 163 | + |> Task.map (fun () -> i = 3) |
| 164 | + |
| 165 | + [<Fact>] |
| 166 | + let ``TaskSeq-cast prove that it has no effect until executed`` () = |
| 167 | + let mutable i = 0 |
| 168 | + |
| 169 | + let ts = taskSeq { |
| 170 | + i <- i + 1 // we should not get here |
| 171 | + i <- i + 1 |
| 172 | + yield box 42 |
| 173 | + i <- i + 1 |
| 174 | + } |
| 175 | + |
| 176 | + // point of this test: just calling 'cast' won't execute anything of the sequence! |
| 177 | + let cast = ts |> TaskSeq.cast<int> |
| 178 | + i |> should equal 0 // no side effect until iterated |
| 179 | + |
| 180 | + cast |
| 181 | + |> TaskSeq.last |
| 182 | + |> Task.map (should equal 42) |
| 183 | + |> Task.map (fun () -> i = 3) |
0 commit comments