File tree Expand file tree Collapse file tree 6 files changed +51
-0
lines changed
FsToolkit.ErrorHandling.JobResult
FsToolkit.ErrorHandling.TaskResult
FsToolkit.ErrorHandling.JobResult.Tests
FsToolkit.ErrorHandling.TaskResult.Tests
FsToolkit.ErrorHandling.Tests Expand file tree Collapse file tree 6 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -109,6 +109,11 @@ module JobResult =
109109 let defaultValue ifError jobResult =
110110 jobResult |> Job.map ( Result.defaultValue ifError)
111111
112+ /// Extracts the contained value of an job-wrapped result if Error, otherwise
113+ /// uses ifOk.
114+ let defaultError ifOk jobResult =
115+ jobResult |> Job.map ( Result.defaultError ifOk)
116+
112117 /// Extracts the contained value of an job-wrapped result if Ok, otherwise
113118 /// evaluates ifErrorThunk and uses the result.
114119 let defaultWith ifErrorThunk jobResult =
Original file line number Diff line number Diff line change @@ -103,6 +103,11 @@ module TaskResult =
103103 let defaultValue ifError taskResult =
104104 taskResult |> Task.map ( Result.defaultValue ifError)
105105
106+ /// Extracts the contained value of an task-wrapped result if Error, otherwise
107+ /// uses ifOk.
108+ let defaultError ifOk taskResult =
109+ taskResult |> Task.map ( Result.defaultError ifOk)
110+
106111 /// Extracts the contained value of an task-wrapped result if Ok, otherwise
107112 /// evaluates ifErrorThunk and uses the result.
108113 let defaultWith ifErrorThunk taskResult =
Original file line number Diff line number Diff line change @@ -112,6 +112,11 @@ module AsyncResult =
112112 let defaultValue ifError asyncResult =
113113 asyncResult |> Async.map ( Result.defaultValue ifError)
114114
115+ /// Extracts the contained value of an async-wrapped result if Error, otherwise
116+ /// uses ifOk.
117+ let defaultError ifOk asyncResult =
118+ asyncResult |> Async.map ( Result.defaultError ifOk)
119+
115120 /// Extracts the contained value of an async-wrapped result if Ok, otherwise
116121 /// evaluates ifErrorThunk and uses the result.
117122 let defaultWith ifErrorThunk asyncResult =
Original file line number Diff line number Diff line change @@ -306,6 +306,18 @@ let defaultValueTests =
306306 Expect.hasJobValue 43 v
307307 ]
308308
309+ [<Tests>]
310+ let defaultErrorTests =
311+ testList " JobResult.defaultError Tests" [
312+ testCase " defaultError returns the error value" <| fun _ ->
313+ let v = JobResult.defaultError 43 ( toJob ( Error 42 ))
314+ Expect.hasJobValue 42 v
315+
316+ testCase " defaultError returns the given value for Ok" <| fun _ ->
317+ let v = JobResult.defaultError 43 ( toJob ( Ok 42 ))
318+ Expect.hasJobValue 43 v
319+ ]
320+
309321[<Tests>]
310322let defaultWithTests =
311323 testList " JobResult.defaultWith Tests" [
Original file line number Diff line number Diff line change @@ -306,6 +306,18 @@ let defaultValueTests =
306306 Expect.hasTaskValue 43 v
307307 ]
308308
309+ [<Tests>]
310+ let defaultErrorTests =
311+ testList " TaskResult.defaultError Tests" [
312+ testCase " defaultError returns the error value" <| fun _ ->
313+ let v = TaskResult.defaultValue 43 ( toTask ( Error 42 ))
314+ Expect.hasTaskValue 42 v
315+
316+ testCase " defaultError returns the given value for Ok" <| fun _ ->
317+ let v = TaskResult.defaultValue 43 ( toTask ( Ok 42 ))
318+ Expect.hasTaskValue 43 v
319+ ]
320+
309321[<Tests>]
310322let defaultWithTests =
311323 testList " TaskResult.defaultWith Tests" [
Original file line number Diff line number Diff line change @@ -317,6 +317,18 @@ let defaultValueTests =
317317 ]
318318
319319
320+ let defaultErrorTests =
321+ testList " AsyncResult.defaultError Tests" [
322+ testCaseAsync " defaultError returns the error value" <|
323+ ( let v = AsyncResult.defaultError 43 ( toAsync ( Error 42 ))
324+ Expect.hasAsyncValue 42 v)
325+
326+ testCaseAsync " defaultError returns the given value for Ok" <|
327+ ( let v = AsyncResult.defaultError 43 ( toAsync ( Ok 42 ))
328+ Expect.hasAsyncValue 43 v)
329+ ]
330+
331+
320332let defaultWithTests =
321333 testList " AsyncResult.defaultWith Tests" [
322334 testCaseAsync " defaultWith returns the ok value" <|
You can’t perform that action at this time.
0 commit comments