Skip to content

Commit

Permalink
Add/update tests for where and whereAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
abelbraaksma committed Dec 20, 2023
1 parent 1692d6e commit 1129aad
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 39 deletions.
119 changes: 80 additions & 39 deletions src/FSharp.Control.TaskSeq.Test/TaskSeq.Filter.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,108 @@ open FSharp.Control
//
// TaskSeq.filter
// TaskSeq.filterAsync
// TaskSeq.where
// TaskSeq.whereAsync
//


module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
let ``TaskSeq-filter or where with null source raises`` () =
assertNullArg
<| fun () -> TaskSeq.filter (fun _ -> false) null

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

assertNullArg
<| fun () -> TaskSeq.where (fun _ -> false) null

assertNullArg
<| fun () -> TaskSeq.whereAsync (fun _ -> Task.fromResult false) null


[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-filter has no effect`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.filter ((=) 12)
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)
let ``TaskSeq-filter or where has no effect`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.filter ((=) 12)
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)

do!
Gen.getEmptyVariant variant
|> TaskSeq.where ((=) 12)
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)
}

[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-filterAsync has no effect`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.filterAsync (fun x -> task { return x = 12 })
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)
let ``TaskSeq-filterAsync or whereAsync has no effect`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.filterAsync (fun x -> task { return x = 12 })
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)

do!
Gen.getEmptyVariant variant
|> TaskSeq.whereAsync (fun x -> task { return x = 12 })
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)
}

module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-filter filters correctly`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.filter ((<=) 5) // greater than
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
|> Task.map (String >> should equal "EFGHIJ")
let ``TaskSeq-filter or where filters correctly`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.filter ((<=) 5) // greater than
|> verifyDigitsAsString "EFGHIJ"

do!
Gen.getSeqImmutable variant
|> TaskSeq.where ((>) 5) // greater than
|> verifyDigitsAsString "ABCD"
}

[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-filterAsync filters correctly`` variant =
Gen.getSeqImmutable variant
|> TaskSeq.filterAsync (fun x -> task { return x <= 5 })
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
|> Task.map (String >> should equal "ABCDE")
let ``TaskSeq-filterAsync or whereAsync filters correctly`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.filterAsync (fun x -> task { return x <= 5 })
|> verifyDigitsAsString "ABCDE"

do!
Gen.getSeqImmutable variant
|> TaskSeq.whereAsync (fun x -> task { return x > 5 })
|> verifyDigitsAsString "FGHIJ"

}

module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-filter filters correctly`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.filter ((<=) 5) // greater than
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
|> Task.map (String >> should equal "EFGHIJ")
let ``TaskSeq-filter filters correctly`` variant = task {
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.filter ((<=) 5) // greater than or equal
|> verifyDigitsAsString "EFGHIJ"

do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.where ((>) 5) // less than
|> verifyDigitsAsString "ABCD"
}

[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-filterAsync filters correctly`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.filterAsync (fun x -> task { return x <= 5 })
|> TaskSeq.map char
|> TaskSeq.map ((+) '@')
|> TaskSeq.toArrayAsync
|> Task.map (String >> should equal "ABCDE")
let ``TaskSeq-filterAsync filters correctly`` variant = task {
do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.filterAsync (fun x -> task { return x <= 5 })
|> verifyDigitsAsString "ABCDE"

do!
Gen.getSeqWithSideEffect variant
|> TaskSeq.whereAsync (fun x -> task { return x > 5 && x < 9 })
|> verifyDigitsAsString "FGH"
}
7 changes: 7 additions & 0 deletions src/FSharp.Control.TaskSeq.Test/TestUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ module TestUtils =
|> TaskSeq.toArrayAsync
|> Task.map (should equal [| 1..10 |])

/// Turns a sequence of numbers into a string, starting with A for '1'
let verifyDigitsAsString expected =
TaskSeq.map char
>> TaskSeq.map ((+) '@')
>> TaskSeq.toArrayAsync
>> Task.map (String >> should equal expected)

/// Delays (no spin-wait!) between 20 and 70ms, assuming a 15.6ms resolution clock
let longDelay () = task { do! Task.Delay(Random().Next(20, 70)) }

Expand Down

0 comments on commit 1129aad

Please sign in to comment.