-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTaskSeq.Forall.Tests.fs
200 lines (159 loc) · 6.66 KB
/
TaskSeq.Forall.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
module TaskSeq.Tests.Forall
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.forall
// TaskSeq.forallAsyncc
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg
<| fun () -> TaskSeq.forall (fun _ -> false) null
assertNullArg
<| fun () -> TaskSeq.forallAsync (fun _ -> Task.fromResult false) null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-forall always returns true`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.forall ((=) 12)
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-forallAsync always returns true`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.forallAsync (fun x -> task { return x = 12 })
|> Task.map (should be True)
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-forall sad path returns false`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.forall ((=) 0)
|> Task.map (should be False)
do!
Gen.getSeqImmutable variant
|> TaskSeq.forall ((>) 9) // lt
|> Task.map (should be False)
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-forallAsync sad path returns false`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.forallAsync (fun x -> task { return x = 0 })
|> Task.map (should be False)
do!
Gen.getSeqImmutable variant
|> TaskSeq.forallAsync (fun x -> task { return x < 9 })
|> Task.map (should be False)
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-forall happy path whole seq true`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.forall (fun x -> x < 6 || x > 5)
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-forallAsync happy path whole seq true`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.forallAsync (fun x -> task { return x <= 10 && x >= 0 })
|> Task.map (should be True)
module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-forall mutated state can change result`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let predicate x = x > 10
// first: false
let! found = TaskSeq.forall predicate ts
found |> should be False // fails on first item, not many side effects yet
// ensure side effects executes
do! consumeTaskSeq ts
// find again: found now, because of side effects
let! found = TaskSeq.forall predicate ts
found |> should be True
// find once more, still true, as numbers increase
do! consumeTaskSeq ts // ensure side effects executes
let! found = TaskSeq.forall predicate ts
found |> should be True
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-forallAsync mutated state can change result`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let predicate x = Task.fromResult (x > 10)
// first: false
let! found = TaskSeq.forallAsync predicate ts
found |> should be False // fails on first item, not many side effects yet
// ensure side effects executes
do! consumeTaskSeq ts
// find again: found now, because of side effects
let! found = TaskSeq.forallAsync predicate ts
found |> should be True
// find once more, still true, as numbers increase
do! consumeTaskSeq ts // ensure side effects executes
let! found = TaskSeq.forallAsync predicate ts
found |> should be True
}
[<Fact>]
let ``TaskSeq-forall _specialcase_ prove we don't read past the first failing item`` () = task {
let mutable i = 0
let ts = taskSeq {
for _ in 0..9 do
i <- i + 1
yield i
}
let! found = ts |> TaskSeq.forall ((>) 3)
found |> should be False
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.forall ((<=) 4)
found |> should be True
i |> should equal 13 // we evaluated to the end
}
[<Fact>]
let ``TaskSeq-forallAsync _specialcase_ prove we don't read past the first failing item`` () = task {
let mutable i = 0
let ts = taskSeq {
for _ in 0..9 do
i <- i + 1
yield i
}
let! found = ts |> TaskSeq.forallAsync (fun x -> Task.fromResult (x < 3))
found |> should be False
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.forallAsync (fun x -> Task.fromResult (x >= 4))
found |> should be True
i |> should equal 13 // we evaluated to the end
}
[<Fact>]
let ``TaskSeq-forall _specialcase_ prove statement after first false result 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.forall ((>) 0)
found |> should be False
i |> should equal 0 // notice that it should be one higher if the statement after 'yield' was evaluated
// find some next item. We do get a new iterator, but mutable state is still starting at '0'
let! found = ts |> TaskSeq.forall ((>) 4)
found |> should be False
i |> should equal 4 // only partial evaluation!
}
[<Fact>]
let ``TaskSeq-forallAsync _specialcase_ prove statement after first false result 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.forallAsync (fun x -> Task.fromResult (x < 0))
found |> should be False
i |> should equal 0 // notice that it should be one higher if the statement after 'yield' was evaluated
// find some next item. We do get a new iterator, but mutable state is still starting at '0'
let! found = ts |> TaskSeq.forallAsync (fun x -> Task.fromResult (x < 4))
found |> should be False
i |> should equal 4 // only partial evaluation!
}