-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTaskSeq.Exists.Tests.fs
228 lines (183 loc) · 7.58 KB
/
TaskSeq.Exists.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
module TaskSeq.Tests.Exists
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.exists
// TaskSeq.existsAsyncc
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg
<| fun () -> TaskSeq.exists (fun _ -> false) null
assertNullArg
<| fun () -> TaskSeq.existsAsync (fun _ -> Task.fromResult false) null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-exists returns false`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.exists ((=) 12)
|> Task.map (should be False)
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-existsAsync returns false`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.existsAsync (fun x -> task { return x = 12 })
|> Task.map (should be False)
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-exists sad path returns false`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.exists ((=) 0)
|> Task.map (should be False)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-existsAsync sad path return false`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.existsAsync (fun x -> task { return x = 0 })
|> Task.map (should be False)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-exists happy path middle of seq`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.exists (fun x -> x < 6 && x > 4)
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-existsAsync happy path middle of seq`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.existsAsync (fun x -> task { return x < 6 && x > 4 })
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-exists happy path first item of seq`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.exists ((=) 1)
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-existsAsync happy path first item of seq`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.existsAsync (fun x -> task { return x = 1 })
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-exists happy path last item of seq`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.exists ((=) 10)
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-existsAsync happy path last item of seq`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.existsAsync (fun x -> task { return x = 10 })
|> Task.map (should be True)
module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-exists success only sometimes for mutated state`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let finder = (=) 11
// first: false
let! found = TaskSeq.exists finder ts
found |> should be False
// find again: found now, because of side effects
let! found = TaskSeq.exists finder ts
found |> should be True
// find once more: false
let! found = TaskSeq.exists finder ts
found |> should be False
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-existsAsync success only sometimes for mutated state`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let finder x = task { return x = 11 }
// first: false
let! found = TaskSeq.existsAsync finder ts
found |> should be False
// find again: found now, because of side effects
let! found = TaskSeq.existsAsync finder ts
found |> should be True
// find once more: false
let! found = TaskSeq.existsAsync finder ts
found |> should be False
}
[<Fact>]
let ``TaskSeq-exists _specialcase_ prove we don't read past the found item`` () = task {
let mutable i = 0
let ts = taskSeq {
for _ in 0..9 do
i <- i + 1
yield i
}
let! found = ts |> TaskSeq.exists ((=) 3)
found |> should be True
i |> should equal 3 // only partial evaluation!
// find next item. We do get a new iterator, but mutable state is now starting at '3', so first item now returned is '4'.
let! found = ts |> TaskSeq.exists ((=) 4)
found |> should be True
i |> should equal 4 // only partial evaluation!
}
[<Fact>]
let ``TaskSeq-existsAsync _specialcase_ prove we don't read past the found item`` () = task {
let mutable i = 0
let ts = taskSeq {
for _ in 0..9 do
i <- i + 1
yield i
}
let! found = ts |> TaskSeq.existsAsync (fun x -> task { return x = 3 })
found |> should be True
i |> should equal 3 // only partial evaluation!
// find next item. We do get a new iterator, but mutable state is now starting at '3', so first item now returned is '4'.
let! found = ts |> TaskSeq.existsAsync (fun x -> task { return x = 4 })
found |> should be True
i |> should equal 4
}
[<Fact>]
let ``TaskSeq-exists _specialcase_ prove we don't read past the found item v2`` () = task {
let mutable i = 0
let ts = taskSeq {
yield 42
i <- i + 1
i <- i + 1
}
let! found = ts |> TaskSeq.exists ((=) 42)
found |> should be True
i |> should equal 0 // because no MoveNext after found item, the last statements are not executed
}
[<Fact>]
let ``TaskSeq-existsAsync _specialcase_ prove we don't read past the found item v2`` () = task {
let mutable i = 0
let ts = taskSeq {
yield 42
i <- i + 1
i <- i + 1
}
let! found = ts |> TaskSeq.existsAsync (fun x -> task { return x = 42 })
found |> should be True
i |> should equal 0 // because no MoveNext after found item, the last statements are not executed
}
[<Fact>]
let ``TaskSeq-exists _specialcase_ prove statement after yield is not evaluated`` () = task {
let mutable i = 0
let ts = taskSeq {
for _ in 0..9 do
yield i
i <- i + 1
}
let! found = ts |> TaskSeq.exists ((=) 0)
found |> should be True
i |> should equal 0 // notice that it should be one higher if the statement after 'yield' is evaluated
// find some next item. We do get a new iterator, but mutable state is now still starting at '0'
let! found = ts |> TaskSeq.exists ((=) 4)
found |> should be True
i |> should equal 4 // only partial evaluation!
}
[<Fact>]
let ``TaskSeq-existsAsync _specialcase_ prove statement after yield is not evaluated`` () = task {
let mutable i = 0
let ts = taskSeq {
for _ in 0..9 do
yield i
i <- i + 1
}
let! found = ts |> TaskSeq.existsAsync (fun x -> task { return x = 0 })
found |> should be True
i |> should equal 0 // notice that it should be one higher if the statement after 'yield' is evaluated
// find some next item. We do get a new iterator, but mutable state is now still starting at '0'
let! found = ts |> TaskSeq.existsAsync (fun x -> task { return x = 4 })
found |> should be True
i |> should equal 4 // only partial evaluation!
}