Skip to content

Commit c1cdbf2

Browse files
meridaioTheAngryByrd
authored andcommitted
add unit tests
1 parent 5a17cc7 commit c1cdbf2

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,25 @@ let teeErrorIfTests =
452452
Expect.equal !foo "foo" ""
453453
]
454454

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+
455474
type CreatePostResult =
456475
| PostSuccess of NotifyNewPostRequest
457476
| NotAllowedToPost

tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,25 @@ let teeErrorIfTests =
452452
Expect.equal !foo "foo" ""
453453
]
454454

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+
455474
type CreatePostResult =
456475
| PostSuccess of NotifyNewPostRequest
457476
| NotAllowedToPost

tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,24 @@ let teeErrorIfTests =
470470
}
471471
]
472472

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+
473491
type CreatePostResult =
474492
| PostSuccess of NotifyNewPostRequest
475493
| NotAllowedToPost
@@ -542,6 +560,7 @@ let allTests = testList "Async Result tests" [
542560
teeIfTests
543561
teeErrorTests
544562
teeErrorIfTests
563+
catchTests
545564
asyncResultCETests
546565
asyncResultOperatorTests
547566
]

0 commit comments

Comments
 (0)