-
Notifications
You must be signed in to change notification settings - Fork 9
Add TaskSeq.takeWhile
, takeWhileAsync
, takeWhileInclusive
, takeWhileInclusiveAsync
#126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
370679a
Add takeWhile, takeWhileInclusive
bartelink 197318b
README updates
bartelink 0d34373
Add inital tests
bartelink 4763eaa
Cover termination implicitly
bartelink ff94115
Cover termination
bartelink d1e8005
Add more complete example-based tests
bartelink 50b6a54
Cover inclusive variants
bartelink 6ddf16e
Address @abelbraaksma review comments
bartelink 86c4adc
Extend SideEffect tests
bartelink 11ce9d1
Update release-notes.txt for adding `TaskSeq.takeWhileXXX` and fix re…
abelbraaksma c8dc715
Small refactoring of new tests, each original test is still there, bu…
abelbraaksma 4f0ee2f
Use a struct DU for differentiating between exclusive/inclusive while…
abelbraaksma 4b3f62f
Expand the tests a bit for takeWhile, make sure each use-case is cove…
abelbraaksma ea22f48
Fix absent link
abelbraaksma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
258 changes: 258 additions & 0 deletions
258
src/FSharp.Control.TaskSeq.Test/TaskSeq.TakeWhile.Tests.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
module TaskSeq.Tests.TakeWhile | ||
|
||
open System | ||
open Xunit | ||
open FsUnit.Xunit | ||
open FsToolkit.ErrorHandling | ||
|
||
open FSharp.Control | ||
|
||
// | ||
// TaskSeq.takeWhile | ||
// TaskSeq.takeWhileAsync | ||
// TaskSeq.takeWhileInclusive | ||
// TaskSeq.takeWhileInclusiveAsync | ||
// | ||
|
||
[<AutoOpen>] | ||
module With = | ||
/// The only real difference in semantics between the base and the *Inclusive variant lies in whether the final item is returned. | ||
/// NOTE the semantics are very clear on only propagating a single failing item in the inclusive case. | ||
let getFunction inclusive isAsync = | ||
match inclusive, isAsync with | ||
| false, false -> TaskSeq.takeWhile | ||
| false, true -> fun pred -> TaskSeq.takeWhileAsync (pred >> Task.fromResult) | ||
| true, false -> TaskSeq.takeWhileInclusive | ||
| true, true -> fun pred -> TaskSeq.takeWhileInclusiveAsync (pred >> Task.fromResult) | ||
|
||
/// adds '@' to each number and concatenates the chars before calling 'should equal' | ||
let verifyAsString expected = | ||
TaskSeq.map char | ||
>> TaskSeq.map ((+) '@') | ||
>> TaskSeq.toArrayAsync | ||
>> Task.map (String >> should equal expected) | ||
|
||
/// This is the base condition as one would expect in actual code | ||
let inline cond x = x <> 6 | ||
|
||
/// For each of the tests below, we add a guard that will trigger if the predicate is passed items known to be beyond the | ||
/// first failing item in the known sequence (which is 1..10) | ||
let inline condWithGuard x = | ||
let res = cond x | ||
|
||
if x > 6 then | ||
failwith "Test sequence should not be enumerated beyond the first item failing the predicate" | ||
|
||
res | ||
|
||
module EmptySeq = | ||
[<Theory; ClassData(typeof<TestEmptyVariants>)>] | ||
let ``TaskSeq-takeWhile+A has no effect`` variant = task { | ||
do! Gen.getEmptyVariant variant | ||
|> TaskSeq.takeWhile ((=) 12) | ||
|> verifyEmpty | ||
|
||
do! Gen.getEmptyVariant variant | ||
|> TaskSeq.takeWhileAsync ((=) 12 >> Task.fromResult) | ||
|> verifyEmpty | ||
} | ||
|
||
[<Theory; ClassData(typeof<TestEmptyVariants>)>] | ||
let ``TaskSeq-takeWhileInclusive+A has no effect`` variant = task { | ||
do! Gen.getEmptyVariant variant | ||
|> TaskSeq.takeWhileInclusive ((=) 12) | ||
|> verifyEmpty | ||
|
||
do! Gen.getEmptyVariant variant | ||
|> TaskSeq.takeWhileInclusiveAsync ((=) 12 >> Task.fromResult) | ||
|> verifyEmpty | ||
} | ||
|
||
module Immutable = | ||
|
||
[<Theory; ClassData(typeof<TestImmTaskSeq>)>] | ||
let ``TaskSeq-takeWhile+A filters correctly`` variant = task { | ||
do! | ||
Gen.getSeqImmutable variant | ||
|> TaskSeq.takeWhile condWithGuard | ||
|> verifyAsString "ABCDE" | ||
|
||
do! | ||
Gen.getSeqImmutable variant | ||
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x }) | ||
|> verifyAsString "ABCDE" | ||
} | ||
|
||
[<Theory; ClassData(typeof<TestImmTaskSeq>)>] | ||
let ``TaskSeq-takeWhile+A does not pick first item when false`` variant = task { | ||
do! | ||
Gen.getSeqImmutable variant | ||
|> TaskSeq.takeWhile ((=) 0) | ||
|> verifyAsString "" | ||
|
||
do! | ||
Gen.getSeqImmutable variant | ||
|> TaskSeq.takeWhileAsync ((=) 0 >> Task.fromResult) | ||
|> verifyAsString "" | ||
} | ||
|
||
[<Theory; ClassData(typeof<TestImmTaskSeq>)>] | ||
let ``TaskSeq-takeWhileInclusive+A filters correctly`` variant = task { | ||
do! | ||
Gen.getSeqImmutable variant | ||
|> TaskSeq.takeWhileInclusive condWithGuard | ||
|> verifyAsString "ABCDEF" | ||
|
||
do! | ||
Gen.getSeqImmutable variant | ||
|> TaskSeq.takeWhileInclusiveAsync (fun x -> task { return condWithGuard x }) | ||
|> verifyAsString "ABCDEF" | ||
} | ||
|
||
[<Theory; ClassData(typeof<TestImmTaskSeq>)>] | ||
let ``TaskSeq-takeWhileInclusive+A always pick at least the first item`` variant = task { | ||
do! | ||
Gen.getSeqImmutable variant | ||
|> TaskSeq.takeWhileInclusive ((=) 0) | ||
|> verifyAsString "A" | ||
|
||
do! | ||
Gen.getSeqImmutable variant | ||
|> TaskSeq.takeWhileInclusiveAsync ((=) 0 >> Task.fromResult) | ||
|> verifyAsString "A" | ||
} | ||
|
||
module SideEffects = | ||
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] | ||
let ``TaskSeq-takeWhile filters correctly`` variant = | ||
Gen.getSeqWithSideEffect variant | ||
|> TaskSeq.takeWhile condWithGuard | ||
|> verifyAsString "ABCDE" | ||
|
||
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] | ||
let ``TaskSeq-takeWhileAsync filters correctly`` variant = | ||
Gen.getSeqWithSideEffect variant | ||
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x }) | ||
|> verifyAsString "ABCDE" | ||
|
||
[<Theory>] | ||
[<InlineData(false, false)>] | ||
[<InlineData(false, true)>] | ||
[<InlineData(true, false)>] | ||
[<InlineData(true, true)>] | ||
let ``TaskSeq-takeWhileXXX prove it does not read beyond the failing yield`` (inclusive, isAsync) = task { | ||
let mutable x = 42 // for this test, the potential mutation should not actually occur | ||
let functionToTest = getFunction inclusive isAsync ((=) 42) | ||
|
||
let items = taskSeq { | ||
yield x // Always passes the test; always returned | ||
yield x * 2 // the failing item (which will also be yielded in the result when using *Inclusive) | ||
x <- x + 1 // we are proving we never get here | ||
} | ||
|
||
let expected = if inclusive then [| 42; 84 |] else [| 42 |] | ||
|
||
let! first = items |> functionToTest |> TaskSeq.toArrayAsync | ||
let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync | ||
|
||
first |> should equal expected | ||
repeat |> should equal expected | ||
x |> should equal 42 | ||
} | ||
|
||
[<Theory>] | ||
[<InlineData(false, false)>] | ||
[<InlineData(false, true)>] | ||
[<InlineData(true, false)>] | ||
[<InlineData(true, true)>] | ||
let ``TaskSeq-takeWhileXXX prove side effects are executed`` (inclusive, isAsync) = task { | ||
let mutable x = 41 | ||
let functionToTest = getFunction inclusive isAsync ((>) 50) | ||
|
||
let items = taskSeq { | ||
x <- x + 1 | ||
yield x | ||
x <- x + 2 | ||
yield x * 2 | ||
x <- x + 200 // as previously proven, we should not trigger this | ||
} | ||
|
||
let expectedFirst = if inclusive then [| 42; 44 * 2 |] else [| 42 |] | ||
let expectedRepeat = if inclusive then [| 45; 47 * 2 |] else [| 45 |] | ||
|
||
let! first = items |> functionToTest |> TaskSeq.toArrayAsync | ||
x |> should equal 44 | ||
let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync | ||
x |> should equal 47 | ||
|
||
first |> should equal expectedFirst | ||
repeat |> should equal expectedRepeat | ||
} | ||
|
||
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] | ||
let ``TaskSeq-takeWhile consumes the prefix of a longer sequence, with mutation`` variant = task { | ||
let ts = Gen.getSeqWithSideEffect variant | ||
|
||
let! first = | ||
TaskSeq.takeWhile (fun x -> x < 5) ts | ||
|> TaskSeq.toArrayAsync | ||
|
||
let expected = [| 1..4 |] | ||
first |> should equal expected | ||
|
||
// side effect, reiterating causes it to resume from where we left it (minus the failing item) | ||
let! repeat = | ||
TaskSeq.takeWhile (fun x -> x < 5) ts | ||
|> TaskSeq.toArrayAsync | ||
|
||
repeat |> should not' (equal expected) | ||
} | ||
|
||
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] | ||
let ``TaskSeq-takeWhileInclusiveAsync consumes the prefix for a longer sequence, with mutation`` variant = task { | ||
let ts = Gen.getSeqWithSideEffect variant | ||
|
||
let! first = | ||
TaskSeq.takeWhileInclusiveAsync (fun x -> task { return x < 5 }) ts | ||
|> TaskSeq.toArrayAsync | ||
|
||
let expected = [| 1..5 |] | ||
first |> should equal expected | ||
|
||
// side effect, reiterating causes it to resume from where we left it (minus the failing item) | ||
let! repeat = | ||
TaskSeq.takeWhileInclusiveAsync (fun x -> task { return x < 5 }) ts | ||
|> TaskSeq.toArrayAsync | ||
|
||
repeat |> should not' (equal expected) | ||
} | ||
|
||
module Other = | ||
[<Theory>] | ||
[<InlineData(false, false)>] | ||
abelbraaksma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[<InlineData(false, true)>] | ||
[<InlineData(true, false)>] | ||
[<InlineData(true, true)>] | ||
let ``TaskSeq-takeWhileXXX exclude all items after predicate fails`` (inclusive, isAsync) = | ||
let functionToTest = With.getFunction inclusive isAsync | ||
|
||
[ 1; 2; 2; 3; 3; 2; 1 ] | ||
|> TaskSeq.ofSeq | ||
|> functionToTest (fun x -> x <= 2) | ||
|> verifyAsString (if inclusive then "ABBC" else "ABB") | ||
|
||
[<Theory>] | ||
[<InlineData(false, false)>] | ||
[<InlineData(false, true)>] | ||
[<InlineData(true, false)>] | ||
[<InlineData(true, true)>] | ||
let ``TaskSeq-takeWhileXXX stops consuming after predicate fails`` (inclusive, isAsync) = | ||
let functionToTest = With.getFunction inclusive isAsync | ||
|
||
seq { | ||
yield! [ 1; 2; 2; 3; 3 ] | ||
yield failwith "Too far" | ||
} | ||
|> TaskSeq.ofSeq | ||
|> functionToTest (fun x -> x <= 2) | ||
|> verifyAsString (if inclusive then "ABBC" else "ABB") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.