-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaskSeq.ExactlyOne.Tests.fs
More file actions
280 lines (230 loc) · 8.37 KB
/
TaskSeq.ExactlyOne.Tests.fs
File metadata and controls
280 lines (230 loc) · 8.37 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
module TaskSeq.Tests.ExactlyOne
open System
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.exactlyOne
// TaskSeq.tryExactlyOne
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg <| fun () -> TaskSeq.exactlyOne null
assertNullArg <| fun () -> TaskSeq.tryExactlyOne null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-exactlyOne throws`` variant = task {
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.exactlyOne
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-tryExactlyOne returns None`` variant = task {
let! nothing = Gen.getEmptyVariant variant |> TaskSeq.tryExactlyOne
nothing |> should be None'
}
module Other =
[<Fact>]
let ``TaskSeq-exactlyOne throws for a sequence of length = two`` () = task {
fun () ->
taskSeq {
yield 1
yield 2
}
|> TaskSeq.exactlyOne
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
}
[<Fact>]
let ``TaskSeq-exactlyOne throws for a sequence of length = two - variant`` () = task {
fun () ->
Gen.sideEffectTaskSeqMicro 50L<µs> 1000L<µs> 2
|> TaskSeq.exactlyOne
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
}
[<Fact>]
let ``TaskSeq-tryExactlyOne returns None for sequence of length = two`` () =
taskSeq {
yield 1
yield 2
}
|> TaskSeq.tryExactlyOne
|> Task.map (should be None')
[<Fact>]
let ``TaskSeq-tryExactlyOne returns None for sequence of length = two - variant`` () =
Gen.sideEffectTaskSeqMicro 50L<µs> 1000L<µs> 2
|> TaskSeq.tryExactlyOne
|> Task.map (should be None')
[<Fact>]
let ``TaskSeq-exactlyOne throws with a larger sequence`` () = task {
fun () ->
Gen.sideEffectTaskSeqMicro 50L<µs> 300L<µs> 200
|> TaskSeq.exactlyOne
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
}
[<Fact>]
let ``TaskSeq-tryExactlyOne returns None with a larger sequence`` () = task {
let! nothing =
Gen.sideEffectTaskSeqMicro 50L<µs> 300L<µs> 20
|> TaskSeq.tryExactlyOne
nothing |> should be None'
}
[<Fact>]
let ``TaskSeq-exactlyOne gets the only item in a singleton sequence`` () = task {
let! exactlyOne = taskSeq { yield 10 } |> TaskSeq.exactlyOne
exactlyOne |> should equal 10
}
[<Fact>]
let ``TaskSeq-tryExactlyOne gets the only item in a singleton sequence`` () = task {
let! exactlyOne = taskSeq { yield 10 } |> TaskSeq.tryExactlyOne
exactlyOne |> should be Some'
exactlyOne |> should equal (Some 10)
}
[<Fact>]
let ``TaskSeq-exactlyOne gets the only item in a singleton sequence - variant`` () = task {
let! exactlyOne =
Gen.sideEffectTaskSeqMicro 1_000L<µs> 5_000L<µs> 1
|> TaskSeq.exactlyOne
exactlyOne |> should equal 1
}
[<Fact>]
let ``TaskSeq-tryExactlyOne gets the only item in a singleton sequence - variant`` () = task {
let! exactlyOne =
Gen.sideEffectTaskSeqMicro 1_000L<µs> 5_000L<µs> 1
|> TaskSeq.tryExactlyOne
exactlyOne |> should be Some'
exactlyOne |> should equal (Some 1)
}
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-exactlyOne throws`` variant =
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.exactlyOne
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryExactlyOne returns None`` variant = task {
let ts = Gen.getSeqImmutable variant
let! head1 = TaskSeq.tryExactlyOne ts
let! head2 = TaskSeq.tryExactlyOne ts
head1 |> should be None'
head2 |> should be None'
}
module SideEffects =
[<Fact>]
let ``TaskSeq-exactlyOne prove we don't iterate further than necessary`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
yield 1
i <- i + 1 // to test we're "exactly one", we need to read until 2nd item
yield 2
i <- i + 1 // we never get here
}
fun () -> ts |> TaskSeq.exactlyOne |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
i |> should equal 2 // last side effect is not executed
}
[<Fact>]
let ``TaskSeq-tryExactlyOne prove we don't iterate further than necessary`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
yield 1
i <- i + 1 // to test we're "exactly one", we need to read until 2nd item
yield 2
i <- i + 1 // we never get here
}
do! ts |> TaskSeq.tryExactlyOne |> Task.map (should be None')
i |> should equal 2 // last side effect is not executed
}
[<Fact>]
let ``TaskSeq-exactlyOne prove we execute side-effects in empty seq`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
i <- i + 1
i <- i + 1 // we should get here
}
fun () -> ts |> TaskSeq.exactlyOne |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
i |> should equal 3 // last side effect is ALSO executed
}
[<Fact>]
let ``TaskSeq-tryExactlyOne prove we execute side-effects in empty seq`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
i <- i + 1
i <- i + 1 // we should get here
}
do! ts |> TaskSeq.tryExactlyOne |> Task.map (should be None')
i |> should equal 3 // last side effect is ALSO executed
}
[<Fact>]
let ``TaskSeq-exactlyOne prove we execute side-effects in singleton seq`` () = 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.exactlyOne |> Task.map (should equal 42)
i |> should equal 3 // last side effect is ALSO executed
}
[<Fact>]
let ``TaskSeq-tryExactlyOne prove we execute side-effects in singleton seq`` () = 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.tryExactlyOne
|> Task.map (should equal (Some 42))
i |> should equal 3 // last side effect is ALSO executed
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-exactlyOne throws`` variant =
fun () ->
Gen.getSeqWithSideEffect variant
|> TaskSeq.exactlyOne
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-exactlyOne throws, but sequence remains accessible`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! nothing = task {
try
return! TaskSeq.exactlyOne ts
with ex ->
ex |> should be ofExactType<ArgumentException>
return -42
}
nothing |> should equal -42
// Test that side-effect has executed. Different sequence variants
// increase the counter differently but they're never 1
let! head1 = TaskSeq.head ts
head1 |> should not' (equal 1)
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-tryExactlyOne returns None`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! head1 = TaskSeq.tryExactlyOne ts
let! head2 = TaskSeq.tryExactlyOne ts
head1 |> should be None'
head2 |> should be None'
// Test that side-effect has executed. Different sequence variants
// increase the counter differently but they're never 1
let! head3 = TaskSeq.head ts
head3 |> should not' (equal 1)
}