Skip to content

Commit

Permalink
Merge pull request #218 from fsprojects/refactor-tests
Browse files Browse the repository at this point in the history
Refactor tests, clarify code
  • Loading branch information
abelbraaksma authored Dec 21, 2023
2 parents b66463c + 33a882d commit de928fb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 59 deletions.
25 changes: 8 additions & 17 deletions src/FSharp.Control.TaskSeq.Test/TaskSeq.Skip.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ open FSharp.Control

exception SideEffectPastEnd of string

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

module EmptySeq =
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-skip(0) has no effect on empty input`` variant =
Expand Down Expand Up @@ -100,17 +91,17 @@ module Immutable =
do!
Gen.getSeqImmutable variant
|> TaskSeq.skip 0
|> verifyAsString "ABCDEFGHIJ"
|> verifyDigitsAsString "ABCDEFGHIJ"

do!
Gen.getSeqImmutable variant
|> TaskSeq.skip 1
|> verifyAsString "BCDEFGHIJ"
|> verifyDigitsAsString "BCDEFGHIJ"

do!
Gen.getSeqImmutable variant
|> TaskSeq.skip 5
|> verifyAsString "FGHIJ"
|> verifyDigitsAsString "FGHIJ"

do!
Gen.getSeqImmutable variant
Expand Down Expand Up @@ -143,17 +134,17 @@ module Immutable =
do!
Gen.getSeqImmutable variant
|> TaskSeq.drop 0
|> verifyAsString "ABCDEFGHIJ"
|> verifyDigitsAsString "ABCDEFGHIJ"

do!
Gen.getSeqImmutable variant
|> TaskSeq.drop 1
|> verifyAsString "BCDEFGHIJ"
|> verifyDigitsAsString "BCDEFGHIJ"

do!
Gen.getSeqImmutable variant
|> TaskSeq.drop 5
|> verifyAsString "FGHIJ"
|> verifyDigitsAsString "FGHIJ"

do!
Gen.getSeqImmutable variant
Expand All @@ -176,13 +167,13 @@ module SideEffects =
let ``TaskSeq-skip skips over enough items`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.skip 5
|> verifyAsString "FGHIJ"
|> verifyDigitsAsString "FGHIJ"

[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-drop skips over enough items`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.drop 5
|> verifyAsString "FGHIJ"
|> verifyDigitsAsString "FGHIJ"

[<Fact>]
let ``TaskSeq-skip prove we do not skip side effects`` () = task {
Expand Down
31 changes: 11 additions & 20 deletions src/FSharp.Control.TaskSeq.Test/TaskSeq.Take.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ open FSharp.Control

exception SideEffectPastEnd of string

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

module EmptySeq =
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-take(0) has no effect on empty input`` variant =
Expand Down Expand Up @@ -105,17 +96,17 @@ module Immutable =
do!
Gen.getSeqImmutable variant
|> TaskSeq.take 1
|> verifyAsString "A"
|> verifyDigitsAsString "A"

do!
Gen.getSeqImmutable variant
|> TaskSeq.take 5
|> verifyAsString "ABCDE"
|> verifyDigitsAsString "ABCDE"

do!
Gen.getSeqImmutable variant
|> TaskSeq.take 10
|> verifyAsString "ABCDEFGHIJ"
|> verifyDigitsAsString "ABCDEFGHIJ"
}

[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
Expand Down Expand Up @@ -148,41 +139,41 @@ module Immutable =
do!
Gen.getSeqImmutable variant
|> TaskSeq.truncate 1
|> verifyAsString "A"
|> verifyDigitsAsString "A"

do!
Gen.getSeqImmutable variant
|> TaskSeq.truncate 5
|> verifyAsString "ABCDE"
|> verifyDigitsAsString "ABCDE"

do!
Gen.getSeqImmutable variant
|> TaskSeq.truncate 10
|> verifyAsString "ABCDEFGHIJ"
|> verifyDigitsAsString "ABCDEFGHIJ"

do!
Gen.getSeqImmutable variant
|> TaskSeq.truncate 11
|> verifyAsString "ABCDEFGHIJ"
|> verifyDigitsAsString "ABCDEFGHIJ"

do!
Gen.getSeqImmutable variant
|> TaskSeq.truncate 10_000_000
|> verifyAsString "ABCDEFGHIJ"
|> verifyDigitsAsString "ABCDEFGHIJ"
}

module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-take gets enough items`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.take 5
|> verifyAsString "ABCDE"
|> verifyDigitsAsString "ABCDE"

[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-truncate gets enough items`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.truncate 5
|> verifyAsString "ABCDE"
|> verifyDigitsAsString "ABCDE"

[<Fact>]
let ``TaskSeq-take prove it does not read beyond the last yield`` () = task {
Expand Down Expand Up @@ -212,7 +203,7 @@ module SideEffects =
do SideEffectPastEnd "at the end" |> raise // we SHOULD NOT get here
}

items |> TaskSeq.take 3 |> verifyAsString "ABC"
items |> TaskSeq.take 3 |> verifyDigitsAsString "ABC"


[<Fact>]
Expand Down
31 changes: 12 additions & 19 deletions src/FSharp.Control.TaskSeq.Test/TaskSeq.TakeWhile.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ module With =
| true, false -> TaskSeq.takeWhileInclusive
| true, true -> fun pred -> TaskSeq.takeWhileInclusiveAsync (pred >> Task.fromResult)

/// Turns a sequence of numbers into a string, starting with A for '1'
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

Expand Down Expand Up @@ -82,65 +75,65 @@ module Immutable =
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhile condWithGuard
|> verifyAsString "ABCDE"
|> verifyDigitsAsString "ABCDE"

do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x })
|> verifyAsString "ABCDE"
|> verifyDigitsAsString "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 ""
|> verifyDigitsAsString ""

do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileAsync ((=) 0 >> Task.fromResult)
|> verifyAsString ""
|> verifyDigitsAsString ""
}

[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-takeWhileInclusive+A filters correctly`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileInclusive condWithGuard
|> verifyAsString "ABCDEF"
|> verifyDigitsAsString "ABCDEF"

do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileInclusiveAsync (fun x -> task { return condWithGuard x })
|> verifyAsString "ABCDEF"
|> verifyDigitsAsString "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"
|> verifyDigitsAsString "A"

do!
Gen.getSeqImmutable variant
|> TaskSeq.takeWhileInclusiveAsync ((=) 0 >> Task.fromResult)
|> verifyAsString "A"
|> verifyDigitsAsString "A"
}

module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-takeWhile filters correctly`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.takeWhile condWithGuard
|> verifyAsString "ABCDE"
|> verifyDigitsAsString "ABCDE"

[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-takeWhileAsync filters correctly`` variant =
Gen.getSeqWithSideEffect variant
|> TaskSeq.takeWhileAsync (fun x -> task { return condWithGuard x })
|> verifyAsString "ABCDE"
|> verifyDigitsAsString "ABCDE"

[<Theory>]
[<InlineData(false, false)>]
Expand Down Expand Up @@ -246,7 +239,7 @@ module Other =
[ 1; 2; 2; 3; 3; 2; 1 ]
|> TaskSeq.ofSeq
|> functionToTest (fun x -> x <= 2)
|> verifyAsString (if inclusive then "ABBC" else "ABB")
|> verifyDigitsAsString (if inclusive then "ABBC" else "ABB")

[<Theory>]
[<InlineData(false, false)>]
Expand All @@ -262,4 +255,4 @@ module Other =
}
|> TaskSeq.ofSeq
|> functionToTest (fun x -> x <= 2)
|> verifyAsString (if inclusive then "ABBC" else "ABB")
|> verifyDigitsAsString (if inclusive then "ABBC" else "ABB")
6 changes: 3 additions & 3 deletions src/FSharp.Control.TaskSeq.Test/TestUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ module TestUtils =
|> TaskSeq.toArrayAsync
|> Task.map (should equal [| 1..10 |])

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

Expand Down

0 comments on commit de928fb

Please sign in to comment.