-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTaskSeq.Map.Tests.fs
241 lines (196 loc) · 7.8 KB
/
TaskSeq.Map.Tests.fs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
module TaskSeq.Tests.Map
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.map
// TaskSeq.mapi
// TaskSeq.mapAsync
// TaskSeq.mapiAsync
//
/// Asserts that a sequence contains the char values 'A'..'J'.
let validateSequence ts =
ts
|> TaskSeq.toListAsync
|> Task.map (List.map string)
|> Task.map (String.concat "")
|> Task.map (should equal "ABCDEFGHIJ")
/// Validates for "ABCDEFGHIJ" char sequence, or any amount of char-value higher
let validateSequenceWithOffset offset ts =
let expected =
[ 'A' .. 'J' ]
|> List.map (int >> (+) offset >> char >> string)
|> String.concat ""
ts
|> TaskSeq.toListAsync
|> Task.map (List.map string)
|> Task.map (String.concat "")
|> Task.map (should equal expected)
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg <| fun () -> TaskSeq.map (fun _ -> ()) null
assertNullArg <| fun () -> TaskSeq.mapi (fun _ _ -> ()) null
assertNullArg
<| fun () -> TaskSeq.mapAsync (fun _ -> Task.fromResult ()) null
assertNullArg
<| fun () -> TaskSeq.mapiAsync (fun _ _ -> Task.fromResult ()) null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-map empty`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.map (fun item -> char (item + 64))
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-mapi empty`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.mapi (fun i _ -> char (i + 65))
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-mapAsync empty`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.mapAsync (fun item -> task { return char (item + 64) })
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-mapiAsync empty`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.mapiAsync (fun i _ -> task { return char (i + 65) })
|> verifyEmpty
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-map maps in correct order`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.map (fun item -> char (item + 64))
|> validateSequence
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-mapi maps in correct order`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.mapi (fun i _ -> char (i + 65))
|> validateSequence
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-mapAsync maps in correct order`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.mapAsync (fun item -> task { return char (item + 64) })
|> validateSequence
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-mapiAsync maps in correct order`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.mapiAsync (fun i _ -> task { return char (i + 65) })
|> validateSequence
module SideEffects =
[<Fact>]
let ``TaskSeq-map prove that it has no effect until executed`` () =
let mutable i = 0
let ts = taskSeq {
i <- i + 1 // we should not get here
i <- i + 1
yield 42
i <- i + 1
}
// point of this test: just calling 'map' won't execute anything of the sequence!
let _ =
ts
|> TaskSeq.map (fun x -> x + 10)
|> TaskSeq.map (fun x -> x + 10)
|> TaskSeq.map (fun x -> x + 10)
// multiple maps have no effect unless executed
i |> should equal 0
[<Fact>]
let ``TaskSeq-mapi prove that it has no effect until executed`` () =
let mutable i = 0
let ts = taskSeq {
i <- i + 1 // we should not get here
i <- i + 1
yield 42
i <- i + 1
}
// point of this test: just calling 'map' won't execute anything of the sequence!
let _ =
ts
|> TaskSeq.mapi (fun x _ -> x + 10)
|> TaskSeq.mapi (fun x _ -> x + 10)
|> TaskSeq.mapi (fun x _ -> x + 10)
// multiple maps have no effect unless executed
i |> should equal 0
[<Fact>]
let ``TaskSeq-mapAsync prove that it has no effect until executed`` () =
let mutable i = 0
let ts = taskSeq {
i <- i + 1 // we should not get here
i <- i + 1
yield 42
i <- i + 1
}
// point of this test: just calling 'map' won't execute anything of the sequence!
let _ =
ts
|> TaskSeq.mapAsync (fun x -> task { return x + 10 })
|> TaskSeq.mapAsync (fun x -> task { return x + 10 })
|> TaskSeq.mapAsync (fun x -> task { return x + 10 })
// multiple maps have no effect unless executed
i |> should equal 0
[<Fact>]
let ``TaskSeq-mapiAsync prove that it has no effect until executed`` () =
let mutable i = 0
let ts = taskSeq {
i <- i + 1 // we should not get here
i <- i + 1
yield 42
i <- i + 1
}
// point of this test: just calling 'map' won't execute anything of the sequence!
let _ =
ts
|> TaskSeq.mapiAsync (fun x _ -> task { return x + 10 })
|> TaskSeq.mapiAsync (fun x _ -> task { return x + 10 })
|> TaskSeq.mapiAsync (fun x _ -> task { return x + 10 })
// multiple maps have no effect unless executed
i |> should equal 0
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-map can access mutables that are mutated in correct order`` variant =
let mutable sum = 0
Gen.getSeqWithSideEffect variant
|> TaskSeq.map (fun _ ->
sum <- sum + 1
char (sum + 64))
|> validateSequence
|> Task.map (fun () -> sum |> should equal 10)
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-mapi can access mutables which are mutated in correct order`` variant =
let mutable sum = 0
Gen.getSeqWithSideEffect variant
|> TaskSeq.mapi (fun i _ ->
sum <- i + 1
char (sum + 64))
|> validateSequence
|> Task.map (fun () -> sum |> should equal 10)
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-mapAsync can map the same sequence multiple times`` variant = task {
let doMap = TaskSeq.mapAsync (fun item -> task { return char (item + 64) })
let ts = Gen.getSeqWithSideEffect variant
// each time we do GetAsyncEnumerator(), and go through the whole sequence,
// the whole sequence gets re-evaluated, causing our +1 side-effect to run again.
do! doMap ts |> validateSequence
do! doMap ts |> validateSequenceWithOffset 10 // the mutable is 10 higher
do! doMap ts |> validateSequenceWithOffset 20 // again
do! doMap ts |> validateSequenceWithOffset 30 // again
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-mapAsync can access mutables which are mutated in correct order`` variant =
let mutable sum = 0
Gen.getSeqWithSideEffect variant
|> TaskSeq.mapAsync (fun _ -> task {
sum <- sum + 1
return char (sum + 64)
})
|> validateSequence
|> Task.map (fun () -> sum |> should equal 10)
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-mapiAsync can access mutables which are mutated in correct order`` variant =
let mutable data = '0'
Gen.getSeqWithSideEffect variant
|> TaskSeq.mapiAsync (fun i _ -> task {
data <- char (i + 65)
return data
})
|> validateSequence
|> Task.map (fun () -> data |> should equal (char 74))