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

Added AsyncResult and TaskResult Helpers #245

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
61 changes: 58 additions & 3 deletions src/FsToolkit.ErrorHandling.TaskResult/TaskResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ module TaskResult =
Result.requireNone error
>> Task.singleton
)

/// Bind the TaskResult and requireValueSome on the inner voption value.
let inline bindRequireValueSome error x =
x
Expand All @@ -276,11 +276,66 @@ module TaskResult =
Result.requireValueNone error
>> Task.singleton
)


/// Bind the TaskResult and requireTrue on the inner value.
let inline bindRequireTrue error x =
x
|> bind (
Result.requireTrue error
>> Task.singleton
)

/// Bind the TaskResult and requireFalse on the inner value.
let inline bindRequireFalse error x =
x
|> bind (
Result.requireFalse error
>> Task.singleton
)

/// Bind the TaskResult and requireNotNull on the inner value.
let inline bindRequireNotNull error x =
x
|> bind (
Result.requireNotNull error
>> Task.singleton
)

/// Bind the TaskResult and requireEequal on the inner value.
let inline bindRequireEqual y error x =
x
|> bind (fun x ->
Result.requireEqual x y error
|> Task.singleton
)

/// Bind the TaskResult and requireEmpty on the inner value.
let inline bindRequireEmpty error x =
x
|> bind (
Result.requireEmpty error
>> Task.singleton
)

/// Bind the TaskResult and requireNotEmpty on the inner value.
let inline bindRequireNotEmpty error x =
x
|> bind (
Result.requireNotEmpty error
>> Task.singleton
)

/// Bind the TaskResult and requireHead on the inner value
let inline bindRequireHead error x =
x
|> bind (
Result.requireHead error
>> Task.singleton
)

let inline foldResult
([<InlineIfLambda>] onSuccess: 'input -> 'output)
([<InlineIfLambda>] onError: 'inputError -> 'output)
(input: Task<Result<'input, 'inputError>>)
: Task<'output> =
Task.map (Result.either onSuccess onError) input

56 changes: 56 additions & 0 deletions src/FsToolkit.ErrorHandling/AsyncResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,59 @@ module AsyncResult =
Result.requireValueNone error
>> Async.singleton
)

/// Bind the AsyncResult and requireTrue on the inner value.
let inline bindRequireTrue error x =
x
|> bind (
Result.requireTrue error
>> Async.singleton
)

/// Bind the AsyncResult and requireFalse on the inner value.
let inline bindRequireFalse error x =
x
|> bind (
Result.requireFalse error
>> Async.singleton
)

/// Bind the AsyncResult and requireNotNull on the inner value.
let inline bindRequireNotNull error x =
x
|> bind (
Result.requireNotNull error
>> Async.singleton
)

/// Bind the AsyncResult and requireEequal on the inner value.
let inline bindRequireEqual y error x =
x
|> bind (fun x ->
Result.requireEqual x y error
|> Async.singleton
)

/// Bind the AsyncResult and requireEmpty on the inner value.
let inline bindRequireEmpty error x =
x
|> bind (
Result.requireEmpty error
>> Async.singleton
)

/// Bind the AsyncResult and requireNotEmpty on the inner value.
let inline bindRequireNotEmpty error x =
x
|> bind (
Result.requireNotEmpty error
>> Async.singleton
)

/// Bind the AsyncResult and requireHead on the inner value
let inline bindRequireHead error x =
x
|> bind (
Result.requireHead error
>> Async.singleton
)
94 changes: 94 additions & 0 deletions tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,97 @@ let foldResultTests =

}
]

[<Tests>]
let taskResultBindRequireTrueTests =
testList "TaskResult Bind + RequireTrue Tests" [
testCaseTask "bindRequireTrue"
<| fun _ ->
task {
do!
true
|> TaskResult.ok
|> TaskResult.bindRequireTrue "Should be true"
|> Expect.hasTaskOkValue ()
}

testCaseTask "bindRequireFalse"
<| fun _ ->
task {
do!
false
|> TaskResult.ok
|> TaskResult.bindRequireFalse "Should be false"
|> Expect.hasTaskOkValue ()
}
]

[<Tests>]
let taskResultBindRequireNotNullTests =
testList "TaskResult Bind + RequireNotNull Tests" [
testCaseTask "bindRequireNotNull"
<| fun _ ->
task {
do!
"Test"
|> TaskResult.ok
|> TaskResult.bindRequireNotNull "Should not be null"
|> Expect.hasTaskOkValue "Test"
}
]

[<Tests>]
let taskResultBindRequireEqualTests =
testList "TaskResult Bind + RequireEqual Tests" [
testCaseTask "bindRequireEqual"
<| fun _ ->
task {
do!
2
|> TaskResult.ok
|> TaskResult.bindRequireEqual 2 "Should be equal"
|> Expect.hasTaskOkValue ()
}
]

[<Tests>]
let taskResultBindRequireEmptyTests =
testList "TaskResult Bind + RequireEmpty Tests" [
testCaseTask "bindRequireEmpty"
<| fun _ ->
task {
do!
[]
|> TaskResult.ok
|> TaskResult.bindRequireEmpty "Should be empty"
|> Expect.hasTaskOkValue ()
}
]

[<Tests>]
let taskResultBindRequireNotEmptyTests =
testList "TaskResult Bind + RequireNotEmpty Tests" [
testCaseTask "bindRequireNotEmpty"
<| fun _ ->
task {
do!
[ 1 ]
|> TaskResult.ok
|> TaskResult.bindRequireNotEmpty "Should not be empty"
|> Expect.hasTaskOkValue ()
}
]

[<Tests>]
let taskResultBindRequireHeadTests =
testList "TaskResult Bind + RequireHead Tests" [
testCaseTask "bindRequireHead"
<| fun _ ->
task {
do!
[ 1 ]
|> TaskResult.ok
|> TaskResult.bindRequireHead "Should not be empty"
|> Expect.hasTaskOkValue 1
}
]
87 changes: 87 additions & 0 deletions tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,87 @@ let asyncResultBindRequireValueOptionTests =
}
]

let asyncResultBindRequireTrueTests =
testList "AsyncResult Bind + RequireTrue Tests" [
testCaseAsync "bindRequireTrue"
<| async {
do!
true
|> AsyncResult.ok
|> AsyncResult.bindRequireTrue "Should be true"
|> Expect.hasAsyncOkValue ()
}

testCaseAsync "bindRequireFalse"
<| async {
do!
false
|> AsyncResult.ok
|> AsyncResult.bindRequireFalse "Should be false"
|> Expect.hasAsyncOkValue ()
}
]

let asyncResultBindRequireNotNullTests =
testList "AsyncResult Bind + RequireNotNull Tests" [
testCaseAsync "bindRequireNotNull"
<| async {
do!
"Test"
|> AsyncResult.ok
|> AsyncResult.bindRequireNotNull "Should not be null"
|> Expect.hasAsyncOkValue "Test"
}
]

let asyncResultBindRequireEqualTests =
testList "AsyncResult Bind + RequireEqual Tests" [
testCaseAsync "bindRequireEqual"
<| async {
do!
2
|> AsyncResult.ok
|> AsyncResult.bindRequireEqual 2 "Should be equal"
|> Expect.hasAsyncOkValue ()
}
]

let asyncResultBindRequireEmptyTests =
testList "AsyncResult Bind + RequireEmpty Tests" [
testCaseAsync "bindRequireEmpty"
<| async {
do!
[]
|> AsyncResult.ok
|> AsyncResult.bindRequireEmpty "Should be empty"
|> Expect.hasAsyncOkValue ()
}
]

let asyncResultBindRequireNotEmptyTests =
testList "AsyncResult Bind + RequireNotEmpty Tests" [
testCaseAsync "bindRequireNotEmpty"
<| async {
do!
[ 1 ]
|> AsyncResult.ok
|> AsyncResult.bindRequireNotEmpty "Should not be empty"
|> Expect.hasAsyncOkValue ()
}
]

let asyncResultBindRequireHeadTests =
testList "AsyncResult Bind + RequireHead Tests" [
testCaseAsync "bindRequireHead"
<| async {
do!
[ 1 ]
|> AsyncResult.ok
|> AsyncResult.bindRequireHead "Should not be empty"
|> Expect.hasAsyncOkValue 1
}
]

let allTests =
testList "Async Result tests" [
mapTests
Expand Down Expand Up @@ -889,4 +970,10 @@ let allTests =
zipErrorTests
asyncResultBindRequireTests
asyncResultBindRequireValueOptionTests
asyncResultBindRequireTrueTests
asyncResultBindRequireNotNullTests
asyncResultBindRequireEqualTests
asyncResultBindRequireEmptyTests
asyncResultBindRequireNotEmptyTests
asyncResultBindRequireHeadTests
]