Skip to content

Commit 3bfedca

Browse files
committed
Add/update tests for where and whereAsync
1 parent 53640c5 commit 3bfedca

File tree

2 files changed

+87
-39
lines changed

2 files changed

+87
-39
lines changed

src/FSharp.Control.TaskSeq.Test/TaskSeq.Filter.Tests.fs

Lines changed: 80 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,108 @@ open FSharp.Control
1010
//
1111
// TaskSeq.filter
1212
// TaskSeq.filterAsync
13+
// TaskSeq.where
14+
// TaskSeq.whereAsync
1315
//
1416

1517

1618
module EmptySeq =
1719
[<Fact>]
18-
let ``Null source is invalid`` () =
20+
let ``TaskSeq-filter or where with null source raises`` () =
1921
assertNullArg
2022
<| fun () -> TaskSeq.filter (fun _ -> false) null
2123

2224
assertNullArg
2325
<| fun () -> TaskSeq.filterAsync (fun _ -> Task.fromResult false) null
2426

27+
assertNullArg
28+
<| fun () -> TaskSeq.where (fun _ -> false) null
29+
30+
assertNullArg
31+
<| fun () -> TaskSeq.whereAsync (fun _ -> Task.fromResult false) null
32+
2533

2634
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
27-
let ``TaskSeq-filter has no effect`` variant =
28-
Gen.getEmptyVariant variant
29-
|> TaskSeq.filter ((=) 12)
30-
|> TaskSeq.toListAsync
31-
|> Task.map (List.isEmpty >> should be True)
35+
let ``TaskSeq-filter or where has no effect`` variant = task {
36+
do!
37+
Gen.getEmptyVariant variant
38+
|> TaskSeq.filter ((=) 12)
39+
|> TaskSeq.toListAsync
40+
|> Task.map (List.isEmpty >> should be True)
41+
42+
do!
43+
Gen.getEmptyVariant variant
44+
|> TaskSeq.where ((=) 12)
45+
|> TaskSeq.toListAsync
46+
|> Task.map (List.isEmpty >> should be True)
47+
}
3248

3349
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
34-
let ``TaskSeq-filterAsync has no effect`` variant =
35-
Gen.getEmptyVariant variant
36-
|> TaskSeq.filterAsync (fun x -> task { return x = 12 })
37-
|> TaskSeq.toListAsync
38-
|> Task.map (List.isEmpty >> should be True)
50+
let ``TaskSeq-filterAsync or whereAsync has no effect`` variant = task {
51+
do!
52+
Gen.getEmptyVariant variant
53+
|> TaskSeq.filterAsync (fun x -> task { return x = 12 })
54+
|> TaskSeq.toListAsync
55+
|> Task.map (List.isEmpty >> should be True)
56+
57+
do!
58+
Gen.getEmptyVariant variant
59+
|> TaskSeq.whereAsync (fun x -> task { return x = 12 })
60+
|> TaskSeq.toListAsync
61+
|> Task.map (List.isEmpty >> should be True)
62+
}
3963

4064
module Immutable =
4165
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
42-
let ``TaskSeq-filter filters correctly`` variant =
43-
Gen.getSeqImmutable variant
44-
|> TaskSeq.filter ((<=) 5) // greater than
45-
|> TaskSeq.map char
46-
|> TaskSeq.map ((+) '@')
47-
|> TaskSeq.toArrayAsync
48-
|> Task.map (String >> should equal "EFGHIJ")
66+
let ``TaskSeq-filter or where filters correctly`` variant = task {
67+
do!
68+
Gen.getSeqImmutable variant
69+
|> TaskSeq.filter ((<=) 5) // greater than
70+
|> verifyDigitsAsString "EFGHIJ"
71+
72+
do!
73+
Gen.getSeqImmutable variant
74+
|> TaskSeq.where ((>) 5) // greater than
75+
|> verifyDigitsAsString "ABCD"
76+
}
4977

5078
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
51-
let ``TaskSeq-filterAsync filters correctly`` variant =
52-
Gen.getSeqImmutable variant
53-
|> TaskSeq.filterAsync (fun x -> task { return x <= 5 })
54-
|> TaskSeq.map char
55-
|> TaskSeq.map ((+) '@')
56-
|> TaskSeq.toArrayAsync
57-
|> Task.map (String >> should equal "ABCDE")
79+
let ``TaskSeq-filterAsync or whereAsync filters correctly`` variant = task {
80+
do!
81+
Gen.getSeqImmutable variant
82+
|> TaskSeq.filterAsync (fun x -> task { return x <= 5 })
83+
|> verifyDigitsAsString "ABCDE"
84+
85+
do!
86+
Gen.getSeqImmutable variant
87+
|> TaskSeq.whereAsync (fun x -> task { return x > 5 })
88+
|> verifyDigitsAsString "FGHIJ"
89+
90+
}
5891

5992
module SideEffects =
6093
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
61-
let ``TaskSeq-filter filters correctly`` variant =
62-
Gen.getSeqWithSideEffect variant
63-
|> TaskSeq.filter ((<=) 5) // greater than
64-
|> TaskSeq.map char
65-
|> TaskSeq.map ((+) '@')
66-
|> TaskSeq.toArrayAsync
67-
|> Task.map (String >> should equal "EFGHIJ")
94+
let ``TaskSeq-filter filters correctly`` variant = task {
95+
do!
96+
Gen.getSeqWithSideEffect variant
97+
|> TaskSeq.filter ((<=) 5) // greater than or equal
98+
|> verifyDigitsAsString "EFGHIJ"
99+
100+
do!
101+
Gen.getSeqWithSideEffect variant
102+
|> TaskSeq.where ((>) 5) // less than
103+
|> verifyDigitsAsString "ABCD"
104+
}
68105

69106
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
70-
let ``TaskSeq-filterAsync filters correctly`` variant =
71-
Gen.getSeqWithSideEffect variant
72-
|> TaskSeq.filterAsync (fun x -> task { return x <= 5 })
73-
|> TaskSeq.map char
74-
|> TaskSeq.map ((+) '@')
75-
|> TaskSeq.toArrayAsync
76-
|> Task.map (String >> should equal "ABCDE")
107+
let ``TaskSeq-filterAsync filters correctly`` variant = task {
108+
do!
109+
Gen.getSeqWithSideEffect variant
110+
|> TaskSeq.filterAsync (fun x -> task { return x <= 5 })
111+
|> verifyDigitsAsString "ABCDE"
112+
113+
do!
114+
Gen.getSeqWithSideEffect variant
115+
|> TaskSeq.whereAsync (fun x -> task { return x > 5 && x < 9 })
116+
|> verifyDigitsAsString "FGH"
117+
}

src/FSharp.Control.TaskSeq.Test/TestUtils.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ module TestUtils =
141141
|> TaskSeq.toArrayAsync
142142
|> Task.map (should equal [| 1..10 |])
143143

144+
/// Turns a sequence of numbers into a string, starting with A for '1'
145+
let verifyDigitsAsString expected =
146+
TaskSeq.map char
147+
>> TaskSeq.map ((+) '@')
148+
>> TaskSeq.toArrayAsync
149+
>> Task.map (String >> should equal expected)
150+
144151
/// Delays (no spin-wait!) between 20 and 70ms, assuming a 15.6ms resolution clock
145152
let longDelay () = task { do! Task.Delay(Random().Next(20, 70)) }
146153

0 commit comments

Comments
 (0)