Skip to content
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

Support ValueTask and Async bindings in testTask #489

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Expecto.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,22 @@ let taskTests =
testTask "inner skip" {
skiptest "skipped"
}
testTask "can let! bind ValueTask<T>" {
let expected = 5
let valueTask = ValueTask.FromResult(expected)
let! actual = valueTask
Expect.equal expected actual "should be able to let! bind ValueTask<T>"
}
testTask "can do! bind ValueTask" {
let valueTask = ValueTask.CompletedTask
do! valueTask
}
testTask "can let! bind Async<T>" {
let expected = 5
let asyncValue = async.Return(expected)
let! actual = asyncValue
Expect.equal expected actual "should be able to let! bind ValueTask<T>"
}
]
]

Expand Down
7 changes: 7 additions & 0 deletions Expecto/Expecto.fs
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,20 @@
member inline __.TryWith(p, cf) = task.TryWith(p, cf)
member __.Run f =
let a = task {
do! task.Run f

Check warning on line 239 in Expecto/Expecto.fs

View workflow job for this annotation

GitHub Actions / build

This state machine is not statically compilable. The resumable code value(s) 'f' does not have a definition. An alternative dynamic implementation will be used, which may be slower. Consider adjusting your code to ensure this state machine is statically compilable, or else suppress this warning.

Check warning on line 239 in Expecto/Expecto.fs

View workflow job for this annotation

GitHub Actions / build

This state machine is not statically compilable. The resumable code value(s) 'f' does not have a definition. An alternative dynamic implementation will be used, which may be slower. Consider adjusting your code to ensure this state machine is statically compilable, or else suppress this warning.
}
match focusState with
| Normal -> testCaseTask name a
| Focused -> ftestCaseTask name a
| Pending -> ptestCaseTask name a

[<AutoOpen>]
module TestTaskExtensions =
type TestTaskBuilder with
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding these overloads as extensions helps the compiler know to prioritize the original Task overload, which prevents breakage in existing code that may have implicitly typed an expression based on Bind originally only taking variations of Task.

member inline __.Bind(p1:ValueTask<'a>, p2:'a->_) = task.Bind(p1, p2)
member inline __.Bind(p1:ValueTask, p2:unit->_) = task.Bind(p1, p2)
member inline __.Bind(p1:Async<'a>, p2:'a->_) = task.Bind(p1, p2)

/// Builds a task test case
let inline testTask name =
TestTaskBuilder (name, Normal)
Expand Down
Loading