-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskSeq.Length.Tests.fs
More file actions
245 lines (198 loc) · 8.39 KB
/
TaskSeq.Length.Tests.fs
File metadata and controls
245 lines (198 loc) · 8.39 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
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
242
243
244
245
module TaskSeq.Tests.Length
open System
open Xunit
open FsUnit.Xunit
open FsToolkit.ErrorHandling
open FSharp.Control
//
// TaskSeq.length
// TaskSeq.lengthBy
// TaskSeq.lengthByAsync
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg <| fun () -> TaskSeq.length null
assertNullArg
<| fun () -> TaskSeq.lengthBy (fun _ -> false) null
assertNullArg
<| fun () -> TaskSeq.lengthByAsync (fun _ -> Task.fromResult false) null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-length returns zero on empty sequences`` variant = task {
let! len = Gen.getEmptyVariant variant |> TaskSeq.length
len |> should equal 0
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-lengthBy returns zero on empty sequences`` variant = task {
let! len =
Gen.getEmptyVariant variant
|> TaskSeq.lengthBy (fun _ -> true)
len |> should equal 0
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-lengthByAsync returns zero on empty sequences`` variant = task {
let! len =
Gen.getEmptyVariant variant
|> TaskSeq.lengthByAsync (Task.apply (fun _ -> true))
len |> should equal 0
}
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-length returns proper length`` variant = task {
let ts = Gen.getSeqImmutable variant
do! TaskSeq.length ts |> Task.map (should equal 10)
do! TaskSeq.length ts |> Task.map (should equal 10) // twice is fine
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-lengthBy returns proper length`` variant = task {
let ts = Gen.getSeqImmutable variant
let run () = TaskSeq.lengthBy (fun _ -> true) ts
do! run () |> Task.map (should equal 10)
do! run () |> Task.map (should equal 10) // twice is fine
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-lengthByAsync returns proper length`` variant = task {
let ts = Gen.getSeqImmutable variant
let run () = TaskSeq.lengthByAsync (Task.apply (fun _ -> true)) ts
do! run () |> Task.map (should equal 10)
do! run () |> Task.map (should equal 10) // twice is fine
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-lengthBy returns proper length when filtering`` variant = task {
let run f = Gen.getSeqImmutable variant |> TaskSeq.lengthBy f
do! run (fun x -> x % 3 = 0) |> Task.map (should equal 3) // [3; 6; 9]
do! run (fun x -> x % 3 = 1) |> Task.map (should equal 4) // [1; 4; 7; 10]
do! run (fun x -> x % 3 = 2) |> Task.map (should equal 3) // [2; 5; 8]
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-lengthByAsync returns proper length when filtering`` variant = task {
let run f =
Gen.getSeqImmutable variant
|> TaskSeq.lengthByAsync (Task.apply f)
do! run (fun x -> x % 3 = 0) |> Task.map (should equal 3) // [3; 6; 9]
do! run (fun x -> x % 3 = 1) |> Task.map (should equal 4) // [1; 4; 7; 10]
do! run (fun x -> x % 3 = 2) |> Task.map (should equal 3) // [2; 5; 8]
}
module SideSeffects =
[<Fact>]
let ``TaskSeq-length prove we execute after-effects`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
i <- i + 1
yield 42
i <- i + 1 // we should get here
}
do! ts |> TaskSeq.length |> Task.ignore
do! ts |> TaskSeq.length |> Task.ignore
do! ts |> TaskSeq.length |> Task.ignore
i |> should equal 9
}
[<Fact>]
let ``TaskSeq-lengthBy prove we execute after-effects`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
i <- i + 1
yield 42
i <- i + 1 // we should get here
}
do! ts |> TaskSeq.lengthBy (fun _ -> true) |> Task.ignore
do! ts |> TaskSeq.lengthBy (fun _ -> true) |> Task.ignore
do! ts |> TaskSeq.lengthBy (fun _ -> true) |> Task.ignore
i |> should equal 9
}
[<Fact>]
let ``TaskSeq-lengthByAsync prove we execute after-effects`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
i <- i + 1
yield 42
i <- i + 1 // we should get here
}
let lenBy =
TaskSeq.lengthByAsync (fun _ -> task { return true })
>> Task.ignore
do! lenBy ts
do! lenBy ts
do! lenBy ts
i |> should equal 9
}
[<Fact>]
let ``TaskSeq-length with sequence that changes length`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 10
yield! [ 1..i ]
}
do! TaskSeq.length ts |> Task.map (should equal 10)
do! TaskSeq.length ts |> Task.map (should equal 20) // mutable state dangers!!
do! TaskSeq.length ts |> Task.map (should equal 30) // id
do! TaskSeq.length ts |> Task.map (should equal 40) // id
do! TaskSeq.length ts |> Task.map (should equal 50) // id
}
[<Fact>]
let ``TaskSeq-lengthBy with sequence that changes length`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 10
yield! [ 1..i ]
}
do! TaskSeq.lengthBy ((<) 10) ts |> Task.map (should equal 0)
do! TaskSeq.lengthBy ((<) 20) ts |> Task.map (should equal 0) // mutable state dangers!!
do! TaskSeq.lengthBy ((<) 30) ts |> Task.map (should equal 0) // id
do! TaskSeq.lengthBy ((<) 10) ts |> Task.map (should equal 30) // id
do! TaskSeq.lengthBy ((<) 10) ts |> Task.map (should equal 40) // id
}
[<Fact>]
let ``TaskSeq-lengthByAsync with sequence that changes length`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 10
yield! [ 1..i ]
}
let notBefore x = TaskSeq.lengthByAsync (Task.apply ((<) x)) ts
do! notBefore 10 |> Task.map (should equal 0)
do! notBefore 20 |> Task.map (should equal 0) // mutable state dangers!!
do! notBefore 30 |> Task.map (should equal 0) // id
do! notBefore 10 |> Task.map (should equal 30) // id
do! notBefore 10 |> Task.map (should equal 40) // id
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-length returns proper length`` variant = task {
let! len = Gen.getSeqWithSideEffect variant |> TaskSeq.length
len |> should equal 10
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-lengthBy returns proper length`` variant = task {
let! len =
Gen.getSeqWithSideEffect variant
|> TaskSeq.lengthBy (fun _ -> true)
len |> should equal 10
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-lengthByAsync returns proper length`` variant = task {
let! len =
Gen.getSeqWithSideEffect variant
|> TaskSeq.lengthByAsync (Task.apply (fun _ -> true))
len |> should equal 10
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-lengthBy returns proper length when filtering`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let run f = ts |> TaskSeq.lengthBy f
do! run (fun x -> x % 3 = 0) |> Task.map (should equal 3) // [3; 6; 9]
do! run (fun x -> x % 3 = 1) |> Task.map (should equal 3) // [13; 16; 19] // because of side-effect run again!
do! run (fun x -> x % 3 = 2) |> Task.map (should equal 3) // [23; 26; 29] // id
do! run (fun x -> x % 3 = 1) |> Task.map (should equal 4) // [31; 34; 37; 40] // id
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-lengthByAsync returns proper length when filtering - side-effect`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let run f = ts |> TaskSeq.lengthByAsync (Task.apply f)
do! run (fun x -> x % 3 = 0) |> Task.map (should equal 3) // [3; 6; 9]
do! run (fun x -> x % 3 = 1) |> Task.map (should equal 3) // [13; 16; 19] // because of side-effect run again!
do! run (fun x -> x % 3 = 2) |> Task.map (should equal 3) // [23; 26; 29] // id
do! run (fun x -> x % 3 = 1) |> Task.map (should equal 4) // [31; 34; 37; 40] // id
}