-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTaskSeq.Contains.Tests.fs
118 lines (94 loc) · 3.62 KB
/
TaskSeq.Contains.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
module TaskSeq.Tests.Contains
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.contains
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () = assertNullArg <| fun () -> TaskSeq.contains 42 null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-contains returns false`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.contains 12
|> Task.map (should be False)
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-contains sad path returns false`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.contains 0
|> Task.map (should be False)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-contains happy path middle of seq`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.contains 5
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-contains happy path first item of seq`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.contains 1
|> Task.map (should be True)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-contains happy path last item of seq`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.contains 10
|> Task.map (should be True)
module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-contains KeyNotFoundException only sometimes for mutated state`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
// first: false
let! found = TaskSeq.contains 11 ts
found |> should be False
// find again: found now, because of side effects
let! found = TaskSeq.contains 11 ts
found |> should be True
// find once more: false
let! found = TaskSeq.contains 11 ts
found |> should be False
}
[<Fact>]
let ``TaskSeq-contains _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.contains 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.contains 4
found |> should be True
i |> should equal 4 // only partial evaluation!
}
[<Fact>]
let ``TaskSeq-contains _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.contains 42
found |> should be True
i |> should equal 0 // because no MoveNext after found item, the last statements are not executed
}
[<Fact>]
let ``TaskSeq-contains _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.contains 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 starting at '1'
let! found = ts |> TaskSeq.contains 4
found |> should be True
i |> should equal 4 // only partial evaluation!
}