-
Notifications
You must be signed in to change notification settings - Fork 11
Implement TaskSeq.skipWhile, skipWhileAsync, skipWhileInclusive and skipWhileInclusiveAsync
#219
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 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0b25ccd
Implement `TaskSeq.skipWhile`, `skipWhileAsync`, `skipWhileInclusive`…
abelbraaksma 9a48e58
A slightly modified implementation based on test experiences
abelbraaksma 27be66e
Add tests for `skipWhile` and `skipWhileInclusive` and async variants
abelbraaksma 8eb270c
Formatting
abelbraaksma 033731d
Improve existing `takeWhile` tests a bit
abelbraaksma 8567f34
Add/fix more tests, some assume we do not read to end of stream, this…
abelbraaksma 17aa445
Fix tests that assumed we did not "read till end", which we always do
abelbraaksma 0aa0742
Clarify test use-cases by using clearer syntax
abelbraaksma 565f3f5
Apply review comments: redesign two tests to better test edge cases a…
abelbraaksma ef920c1
Rename function names
abelbraaksma 708a4b6
Apply review comments, small clarifications/code improvements, tx @ba…
abelbraaksma faf1841
Update `readme.md`, package info and release notes
abelbraaksma a213a1f
Fix FS3511 "This state machine is not statically compilable" in tests
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
332 changes: 332 additions & 0 deletions
332
src/FSharp.Control.TaskSeq.Test/TaskSeq.SkipWhile.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,332 @@ | ||
| module TaskSeq.Tests.skipWhile | ||
|
|
||
| open System | ||
|
|
||
| open Xunit | ||
| open FsUnit.Xunit | ||
|
|
||
| open FSharp.Control | ||
|
|
||
| // | ||
| // TaskSeq.skipWhile | ||
| // TaskSeq.skipWhileAsync | ||
| // TaskSeq.skipWhileInclusive | ||
| // TaskSeq.skipWhileInclusiveAsync | ||
| // | ||
|
|
||
| exception SideEffectPastEnd of string | ||
|
|
||
| [<AutoOpen>] | ||
| module With = | ||
| /// The only real difference in semantics between the base and the *Inclusive variant lies in whether the final item is skipped. | ||
| let getFunction inclusive isAsync = | ||
| match inclusive, isAsync with | ||
| | false, false -> TaskSeq.skipWhile | ||
| | false, true -> fun pred -> TaskSeq.skipWhileAsync (pred >> Task.fromResult) | ||
| | true, false -> TaskSeq.skipWhileInclusive | ||
| | true, true -> fun pred -> TaskSeq.skipWhileInclusiveAsync (pred >> Task.fromResult) | ||
|
|
||
| module EmptySeq = | ||
|
|
||
| // TaskSeq-skipWhile+A stands for: | ||
| // skipWhile + skipWhileAsync etc. | ||
|
|
||
| [<Theory; ClassData(typeof<TestEmptyVariants>)>] | ||
| let ``TaskSeq-skipWhile+A has no effect`` variant = task { | ||
| do! | ||
| Gen.getEmptyVariant variant | ||
| |> TaskSeq.skipWhile ((=) 12) | ||
| |> verifyEmpty | ||
|
|
||
| do! | ||
| Gen.getEmptyVariant variant | ||
| |> TaskSeq.skipWhileAsync ((=) 12 >> Task.fromResult) | ||
| |> verifyEmpty | ||
| } | ||
|
|
||
| [<Theory; ClassData(typeof<TestEmptyVariants>)>] | ||
| let ``TaskSeq-skipWhileInclusive+A has no effect`` variant = task { | ||
| do! | ||
| Gen.getEmptyVariant variant | ||
| |> TaskSeq.skipWhileInclusive ((=) 12) | ||
| |> verifyEmpty | ||
|
|
||
| do! | ||
| Gen.getEmptyVariant variant | ||
| |> TaskSeq.skipWhileInclusiveAsync ((=) 12 >> Task.fromResult) | ||
| |> verifyEmpty | ||
| } | ||
|
|
||
| module Immutable = | ||
|
|
||
| // TaskSeq-skipWhile+A stands for: | ||
| // skipWhile + skipWhileAsync etc. | ||
|
|
||
| [<Theory; ClassData(typeof<TestImmTaskSeq>)>] | ||
| let ``TaskSeq-skipWhile+A filters correctly`` variant = task { | ||
| // truth table for f(x) = x < 5 | ||
| // 1 2 3 4 5 6 7 8 9 10 | ||
| // T T T T F F F F F F (stops at first F) | ||
| // x x x x _ _ _ _ _ _ (skips exclusive) | ||
| // A B C D E F G H I J | ||
|
|
||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhile ((>) 5) // skip while less than 5 | ||
| |> verifyDigitsAsString "EFGHIJ" | ||
|
|
||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhileAsync (fun x -> task { return x < 5 }) | ||
| |> verifyDigitsAsString "EFGHIJ" | ||
| } | ||
|
|
||
| [<Theory; ClassData(typeof<TestImmTaskSeq>)>] | ||
| let ``TaskSeq-skipWhile+A does not skip first item when false`` variant = task { | ||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhile ((=) 0) | ||
| |> verifyDigitsAsString "ABCDEFGHIJ" // all 10 remain! | ||
|
|
||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhileAsync ((=) 0 >> Task.fromResult) | ||
| |> verifyDigitsAsString "ABCDEFGHIJ" // all 10 remain! | ||
| } | ||
|
|
||
| [<Theory; ClassData(typeof<TestImmTaskSeq>)>] | ||
| let ``TaskSeq-skipWhileInclusive+A filters correctly`` variant = task { | ||
| // truth table for f(x) = x < 5 | ||
| // 1 2 3 4 5 6 7 8 9 10 | ||
| // T T T T F F F F F F (stops at first F) | ||
| // x x x x x _ _ _ _ _ (skips inclusively) | ||
| // A B C D E F G H I J | ||
|
|
||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhileInclusive ((>) 5) | ||
| |> verifyDigitsAsString "FGHIJ" // last 4 | ||
|
|
||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 5 }) | ||
abelbraaksma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| |> verifyDigitsAsString "FGHIJ" | ||
| } | ||
|
|
||
|
|
||
| [<Theory; ClassData(typeof<TestImmTaskSeq>)>] | ||
| let ``TaskSeq-skipWhileInclusive+A returns the empty sequence if always true`` variant = task { | ||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhileInclusive ((<) -1) | ||
abelbraaksma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| |> verifyEmpty | ||
|
|
||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhileInclusiveAsync (fun x -> task { return true }) | ||
abelbraaksma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| |> verifyEmpty | ||
| } | ||
|
|
||
| [<Theory; ClassData(typeof<TestImmTaskSeq>)>] | ||
| let ``TaskSeq-skipWhileInclusive+A always skips at least the first item`` variant = task { | ||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhileInclusive ((=) 0) | ||
| |> verifyDigitsAsString "BCDEFGHIJ" | ||
|
|
||
| do! | ||
| Gen.getSeqImmutable variant | ||
| |> TaskSeq.skipWhileInclusiveAsync ((=) 0 >> Task.fromResult) | ||
| |> verifyDigitsAsString "BCDEFGHIJ" | ||
| } | ||
|
|
||
| module SideEffects = | ||
| [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] | ||
| let ``TaskSeq-skipWhile+A filters correctly`` variant = task { | ||
| // truth table for f(x) = x < 6 | ||
| // 1 2 3 4 5 6 7 8 9 10 | ||
| // T T T T T F F F F F (stops at first F) | ||
| // x x x x x _ _ _ _ _ (skips exclusively) | ||
| // A B C D E F G H I J | ||
|
|
||
| do! | ||
| Gen.getSeqWithSideEffect variant | ||
| |> TaskSeq.skipWhile ((>) 6) | ||
| |> verifyDigitsAsString "FGHIJ" | ||
|
|
||
| do! | ||
| Gen.getSeqWithSideEffect variant | ||
| |> TaskSeq.skipWhileAsync (fun x -> task { return x < 6 }) | ||
| |> verifyDigitsAsString "FGHIJ" | ||
| } | ||
|
|
||
| [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] | ||
| let ``TaskSeq-skipWhileInclusive+A filters correctly`` variant = task { | ||
| // truth table for f(x) = x < 6 | ||
| // 1 2 3 4 5 6 7 8 9 10 | ||
| // T T T T T F F F F F (stops at first F) | ||
| // x x x x x x _ _ _ _ (skips inclusively) | ||
| // A B C D E F G H I J | ||
|
|
||
| do! | ||
| Gen.getSeqWithSideEffect variant | ||
| |> TaskSeq.skipWhileInclusive ((>) 6) | ||
| |> verifyDigitsAsString "GHIJ" | ||
|
|
||
| do! | ||
| Gen.getSeqWithSideEffect variant | ||
| |> TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 6 }) | ||
| |> verifyDigitsAsString "GHIJ" | ||
| } | ||
|
|
||
| [<Theory>] | ||
| [<InlineData(false, false)>] | ||
| [<InlineData(false, true)>] | ||
| [<InlineData(true, false)>] | ||
| [<InlineData(true, true)>] | ||
| let ``TaskSeq-skipWhileXXX prove it reads the entire input stream`` (inclusive, isAsync) = task { | ||
| let mutable x = 42 // for this test, the potential mutation should not actually occur | ||
| let functionToTest = getFunction inclusive isAsync ((=) 42) | ||
abelbraaksma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let items = taskSeq { | ||
| yield x // Always passes the test; always skipped | ||
| yield x * 2 // Fails the test, skipped depending on "inclusive" | ||
| x <- x + 1 // we are proving we ALWAYS get here | ||
| } | ||
|
|
||
| x |> should equal 42 | ||
| let! first = items |> functionToTest |> TaskSeq.toArrayAsync | ||
| x |> should equal 43 | ||
| first |> should equal (if inclusive then [||] else [| 84 |]) | ||
|
|
||
| let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync | ||
| x |> should equal 44 | ||
|
|
||
| repeat | ||
| |> should equal (if inclusive then [| 86 |] else [| 43; 86 |]) | ||
| } | ||
|
|
||
| [<Theory>] | ||
| [<InlineData(false, false)>] | ||
| [<InlineData(false, true)>] | ||
| [<InlineData(true, false)>] | ||
| [<InlineData(true, true)>] | ||
| let ``TaskSeq-skipWhileXXX prove side effects are properly 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 ALWAYS trigger this | ||
| } | ||
|
|
||
| let expectedFirst = if inclusive then [||] else [| 88 |] | ||
| let expectedRepeat = if inclusive then [| 494 |] else [| 245; 494 |] | ||
|
|
||
| x |> should equal 41 | ||
| let! first = items |> functionToTest |> TaskSeq.toArrayAsync | ||
| x |> should equal 244 | ||
|
|
||
| let! repeat = items |> functionToTest |> TaskSeq.toArrayAsync | ||
| x |> should equal 447 | ||
|
|
||
| first |> should equal expectedFirst | ||
| repeat |> should equal expectedRepeat | ||
| } | ||
|
|
||
| [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] | ||
| let ``TaskSeq-skipWhile consumes the prefix of a longer sequence, with mutation`` variant = task { | ||
| let ts = Gen.getSeqWithSideEffect variant | ||
|
|
||
| let! first = | ||
| TaskSeq.skipWhile (fun x -> x < 5) ts | ||
| |> TaskSeq.toArrayAsync | ||
|
|
||
| let expected = [| 5..10 |] | ||
| first |> should equal expected | ||
|
|
||
| // side effect, reiterating causes it to resume from where we left it (minus the failing item) | ||
| // which means the original sequence has now changed due to the side effect | ||
| let! repeat = | ||
| TaskSeq.skipWhile (fun x -> x < 5) ts | ||
| |> TaskSeq.toArrayAsync | ||
|
|
||
| repeat |> should not' (equal expected) | ||
| } | ||
|
|
||
| [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] | ||
| let ``TaskSeq-skipWhileInclusiveAsync consumes the prefix for a longer sequence, with mutation`` variant = task { | ||
| let ts = Gen.getSeqWithSideEffect variant | ||
|
|
||
| let! first = | ||
| TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 5 }) ts | ||
| |> TaskSeq.toArrayAsync | ||
|
|
||
| let expected = [| 6..10 |] | ||
| first |> should equal expected | ||
|
|
||
| // side effect, reiterating causes it to resume from where we left it (minus the failing item) | ||
| // which means the original sequence has now changed due to the side effect | ||
| let! repeat = | ||
| TaskSeq.skipWhileInclusiveAsync (fun x -> task { return x < 5 }) ts | ||
| |> TaskSeq.toArrayAsync | ||
|
|
||
| repeat |> should not' (equal expected) | ||
| } | ||
|
|
||
| module Other = | ||
| [<Theory>] | ||
| [<InlineData(false, false)>] | ||
| [<InlineData(false, true)>] | ||
| [<InlineData(true, false)>] | ||
| [<InlineData(true, true)>] | ||
| let ``TaskSeq-skipWhileXXX should include all items after predicate fails`` (inclusive, isAsync) = task { | ||
| do! | ||
| [ 1; 2; 2; 3; 3; 2; 1 ] | ||
| |> TaskSeq.ofSeq | ||
| |> TaskSeq.skipWhile (fun x -> x <= 2) | ||
| |> verifyDigitsAsString "CCBA" | ||
|
|
||
| do! | ||
| [ 1; 2; 2; 3; 3; 2; 1 ] | ||
| |> TaskSeq.ofSeq | ||
| |> TaskSeq.skipWhileInclusive (fun x -> x <= 2) | ||
| |> verifyDigitsAsString "CBA" | ||
|
|
||
| do! | ||
| [ 1; 2; 2; 3; 3; 2; 1 ] | ||
| |> TaskSeq.ofSeq | ||
| |> TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x <= 2)) | ||
| |> verifyDigitsAsString "CCBA" | ||
|
|
||
| do! | ||
| [ 1; 2; 2; 3; 3; 2; 1 ] | ||
| |> TaskSeq.ofSeq | ||
| |> TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x <= 2)) | ||
| |> verifyDigitsAsString "CBA" | ||
| } | ||
|
|
||
| [<Theory>] | ||
| [<InlineData(false, false)>] | ||
| [<InlineData(false, true)>] | ||
| [<InlineData(true, false)>] | ||
| [<InlineData(true, true)>] | ||
| let ``TaskSeq-skipWhileXXX stops consuming after predicate fails`` (inclusive, isAsync) = | ||
| let testSkipper skipper = | ||
| fun () -> | ||
| seq { | ||
| yield! [ 1; 2; 2; 3; 3 ] | ||
| yield SideEffectPastEnd "Too far" |> raise | ||
| } | ||
| |> TaskSeq.ofSeq | ||
| |> skipper | ||
| |> consumeTaskSeq | ||
| |> should throwAsyncExact typeof<SideEffectPastEnd> | ||
|
|
||
| do testSkipper (TaskSeq.skipWhile (fun x -> x <= 2)) | ||
abelbraaksma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| do testSkipper (TaskSeq.skipWhileInclusive (fun x -> x <= 2)) | ||
| do testSkipper (TaskSeq.skipWhileAsync (fun x -> Task.fromResult (x <= 2))) | ||
| do testSkipper (TaskSeq.skipWhileInclusiveAsync (fun x -> Task.fromResult (x <= 2))) | ||
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.