-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaskSeq.Indexed.Tests.fs
More file actions
101 lines (80 loc) · 3.15 KB
/
TaskSeq.Indexed.Tests.fs
File metadata and controls
101 lines (80 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
module TaskSeq.Tests.Indexed
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.indexed
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () = assertNullArg <| fun () -> TaskSeq.indexed null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-indexed on empty`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.indexed
|> verifyEmpty
module Immutable =
[<Fact>]
let ``TaskSeq-indexed starts at zero`` () =
taskSeq { yield 99 }
|> TaskSeq.indexed
|> TaskSeq.head
|> Task.map (should equal (0, 99))
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-indexed`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.indexed
|> TaskSeq.toArrayAsync
|> Task.map (Array.forall (fun (x, y) -> x + 1 = y))
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-indexed returns all 10 pairs with correct zero-based indices`` variant = task {
let! pairs =
Gen.getSeqImmutable variant
|> TaskSeq.indexed
|> TaskSeq.toArrayAsync
pairs |> should be (haveLength 10)
pairs
|> Array.iteri (fun pos (idx, _) -> idx |> should equal pos)
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-indexed returns values 1 to 10 unchanged`` variant = task {
let! pairs =
Gen.getSeqImmutable variant
|> TaskSeq.indexed
|> TaskSeq.toArrayAsync
pairs |> Array.map snd |> should equal [| 1..10 |]
}
module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-indexed on side-effect sequence returns correct pairs`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! pairs = ts |> TaskSeq.indexed |> TaskSeq.toArrayAsync
pairs |> should be (haveLength 10)
pairs
|> Array.iteri (fun pos (idx, _) -> idx |> should equal pos)
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-indexed on side-effect sequence is re-evaluated on second iteration`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! firstPairs = ts |> TaskSeq.indexed |> TaskSeq.toArrayAsync
let! secondPairs = ts |> TaskSeq.indexed |> TaskSeq.toArrayAsync
// indices always start at 0
firstPairs |> Array.map fst |> should equal [| 0..9 |]
secondPairs |> Array.map fst |> should equal [| 0..9 |]
// values advance due to side effects
firstPairs |> Array.map snd |> should equal [| 1..10 |]
secondPairs |> Array.map snd |> should equal [| 11..20 |]
}
[<Fact>]
let ``TaskSeq-indexed prove index starts at zero even after side effects`` () = task {
let mutable counter = 0
let ts = taskSeq {
for _ in 1..5 do
counter <- counter + 1
yield counter
}
let! pairs = ts |> TaskSeq.indexed |> TaskSeq.toArrayAsync
pairs |> Array.map fst |> should equal [| 0..4 |]
pairs |> Array.map snd |> should equal [| 1..5 |]
}