File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed
FsToolkit.ErrorHandling.JobResult.Tests
FsToolkit.ErrorHandling.TaskResult.Tests
FsToolkit.ErrorHandling.Tests Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -452,6 +452,25 @@ let teeErrorIfTests =
452
452
Expect.equal ! foo " foo" " "
453
453
]
454
454
455
+ [<Tests>]
456
+ let catchTests =
457
+ let f ( e : exn ) = e.Message
458
+ let jobThrow () = job {
459
+ failwith err
460
+ return Error " "
461
+ }
462
+
463
+ testList " TaskResult.catch test" [
464
+ testCase " catch returns success for Ok" <| fun _ ->
465
+ Expect.hasJobOkValue 42 ( JobResult.catch f ( toJob ( Ok 42 )))
466
+
467
+ testCase " catch returns mapped Error for exception" <| fun _ ->
468
+ Expect.hasJobErrorValue err ( JobResult.catch f ( jobThrow ()))
469
+
470
+ testCase " catch returns unmapped error without exception" <| fun _ ->
471
+ Expect.hasJobErrorValue " unmapped" ( JobResult.catch f ( toJob ( Error " unmapped" )))
472
+ ]
473
+
455
474
type CreatePostResult =
456
475
| PostSuccess of NotifyNewPostRequest
457
476
| NotAllowedToPost
Original file line number Diff line number Diff line change @@ -452,6 +452,25 @@ let teeErrorIfTests =
452
452
Expect.equal ! foo " foo" " "
453
453
]
454
454
455
+ [<Tests>]
456
+ let catchTests =
457
+ let f ( e : exn ) = e.Message
458
+ let taskThrow () = task {
459
+ failwith err
460
+ return Error " "
461
+ }
462
+
463
+ testList " TaskResult.catch test" [
464
+ testCase " catch returns success for Ok" <| fun _ ->
465
+ Expect.hasTaskOkValue 42 ( TaskResult.catch f ( toTask ( Ok 42 )))
466
+
467
+ testCase " catch returns mapped Error for exception" <| fun _ ->
468
+ Expect.hasTaskErrorValue err ( TaskResult.catch f ( taskThrow ()))
469
+
470
+ testCase " catch returns unmapped error without exception" <| fun _ ->
471
+ Expect.hasTaskErrorValue " unmapped" ( TaskResult.catch f ( toTask ( Error " unmapped" )))
472
+ ]
473
+
455
474
type CreatePostResult =
456
475
| PostSuccess of NotifyNewPostRequest
457
476
| NotAllowedToPost
Original file line number Diff line number Diff line change @@ -470,6 +470,24 @@ let teeErrorIfTests =
470
470
}
471
471
]
472
472
473
+ let catchTests =
474
+ let f ( e : exn ) = e.Message
475
+ let asyncThrow () = async {
476
+ failwith err
477
+ return Error " "
478
+ }
479
+
480
+ testList " AsyncResult.catch test" [
481
+ testCaseAsync " catch returns success for Ok" <|
482
+ Expect.hasAsyncOkValue 42 ( AsyncResult.catch f ( toAsync ( Ok 42 )))
483
+
484
+ testCaseAsync " catch returns mapped Error for exception" <|
485
+ Expect.hasAsyncErrorValue err ( AsyncResult.catch f ( asyncThrow ()))
486
+
487
+ testCaseAsync " catch returns unmapped error without exception" <|
488
+ Expect.hasAsyncErrorValue " unmapped" ( AsyncResult.catch f ( toAsync ( Error " unmapped" )))
489
+ ]
490
+
473
491
type CreatePostResult =
474
492
| PostSuccess of NotifyNewPostRequest
475
493
| NotAllowedToPost
@@ -542,6 +560,7 @@ let allTests = testList "Async Result tests" [
542
560
teeIfTests
543
561
teeErrorTests
544
562
teeErrorIfTests
563
+ catchTests
545
564
asyncResultCETests
546
565
asyncResultOperatorTests
547
566
]
You can’t perform that action at this time.
0 commit comments