Skip to content

Commit 935257d

Browse files
sep2TheAngryByrd
authored andcommitted
Add tests for zip/zipError in AsyncResult/TaskResult/JobResult
1 parent 284ca93 commit 935257d

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,48 @@ let catchTests =
482482
testCase "catch returns unmapped error without exception" <| fun _ ->
483483
Expect.hasJobErrorValue "unmapped" (JobResult.catch f (toJob (Error "unmapped")))
484484
]
485+
486+
487+
[<Tests>]
488+
let zipTests =
489+
testList "JobResult.zip tests" [
490+
testCase "Ok, Ok" <| fun _ ->
491+
let v = JobResult.zip (toJob (Ok 1)) (toJob (Ok 2))
492+
Expect.hasJobValue (Ok(1, 2)) v
493+
494+
testCase "Ok, Error" <| fun _ ->
495+
let v = JobResult.zip (toJob (Ok 1)) (toJob (Error "Bad"))
496+
Expect.hasJobValue (Error("Bad")) v
497+
498+
testCase "Error, Ok" <| fun _ ->
499+
let v = JobResult.zip (toJob (Error "Bad")) (toJob (Ok 1))
500+
Expect.hasJobValue (Error("Bad")) v
501+
502+
testCase "Error, Error" <| fun _ ->
503+
let v = JobResult.zip (toJob (Error "Bad1")) (toJob (Error "Bad2"))
504+
Expect.hasJobValue (Error("Bad1")) v
505+
]
506+
507+
[<Tests>]
508+
let zipErrorTests =
509+
testList "JobResult.zipError tests" [
510+
testCase "Ok, Ok" <| fun _ ->
511+
let v = JobResult.zipError (toJob (Ok 1)) (toJob (Ok 2))
512+
Expect.hasJobValue (Ok(1)) v
513+
514+
testCase "Ok, Error" <| fun _ ->
515+
let v = JobResult.zipError (toJob (Ok 1)) (toJob (Error "Bad"))
516+
Expect.hasJobValue (Ok 1) v
517+
518+
testCase "Error, Ok" <| fun _ ->
519+
let v = JobResult.zipError (toJob (Error "Bad")) (toJob (Ok 1))
520+
Expect.hasJobValue (Ok 1) v
521+
522+
testCase "Error, Error" <| fun _ ->
523+
let v = JobResult.zipError (toJob (Error "Bad1")) (toJob (Error "Bad2"))
524+
Expect.hasJobValue (Error("Bad1", "Bad2")) v
525+
]
526+
485527

486528
type CreatePostResult =
487529
| PostSuccess of NotifyNewPostRequest

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,46 @@ let catchTests =
483483
Expect.hasTaskErrorValue "unmapped" (TaskResult.catch f (toTask (Error "unmapped")))
484484
]
485485

486+
[<Tests>]
487+
let zipTests =
488+
testList "TaskResult.zip tests" [
489+
testCase "Ok, Ok" <| fun _ ->
490+
let v = TaskResult.zip (toTask (Ok 1)) (toTask (Ok 2))
491+
Expect.hasTaskValue (Ok(1, 2)) v
492+
493+
testCase "Ok, Error" <| fun _ ->
494+
let v = TaskResult.zip (toTask (Ok 1)) (toTask (Error "Bad"))
495+
Expect.hasTaskValue (Error("Bad")) v
496+
497+
testCase "Error, Ok" <| fun _ ->
498+
let v = TaskResult.zip (toTask (Error "Bad")) (toTask (Ok 1))
499+
Expect.hasTaskValue (Error("Bad")) v
500+
501+
testCase "Error, Error" <| fun _ ->
502+
let v = TaskResult.zip (toTask (Error "Bad1")) (toTask (Error "Bad2"))
503+
Expect.hasTaskValue (Error("Bad1")) v
504+
]
505+
506+
[<Tests>]
507+
let zipErrorTests =
508+
testList "TaskResult.zipError tests" [
509+
testCase "Ok, Ok" <| fun _ ->
510+
let v = TaskResult.zipError (toTask (Ok 1)) (toTask (Ok 2))
511+
Expect.hasTaskValue (Ok(1)) v
512+
513+
testCase "Ok, Error" <| fun _ ->
514+
let v = TaskResult.zipError (toTask (Ok 1)) (toTask (Error "Bad"))
515+
Expect.hasTaskValue (Ok 1) v
516+
517+
testCase "Error, Ok" <| fun _ ->
518+
let v = TaskResult.zipError (toTask (Error "Bad")) (toTask (Ok 1))
519+
Expect.hasTaskValue (Ok 1) v
520+
521+
testCase "Error, Error" <| fun _ ->
522+
let v = TaskResult.zipError (toTask (Error "Bad1")) (toTask (Error "Bad2"))
523+
Expect.hasTaskValue (Error("Bad1", "Bad2")) v
524+
]
525+
486526
type CreatePostResult =
487527
| PostSuccess of NotifyNewPostRequest
488528
| NotAllowedToPost

tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,49 @@ let asyncResultOperatorTests =
547547
|> Expect.hasAsyncOkValue (PostId newPostId)
548548
}
549549
]
550+
551+
552+
let zipTests =
553+
testList "AsyncResult.zip Tests" [
554+
testCaseAsync "Ok, Ok" <| async {
555+
let! v = AsyncResult.zip (toAsync (Ok 1)) (toAsync (Ok 2))
556+
Expect.equal v (Ok(1, 2)) "Should be ok"
557+
}
558+
testCaseAsync "Ok, Error" <| async {
559+
let! v = AsyncResult.zip (toAsync (Ok 1)) (toAsync (Error "Bad"))
560+
Expect.equal v (Error("Bad")) "Should be Error"
561+
}
562+
testCaseAsync "Error, Ok" <| async {
563+
let! v = AsyncResult.zip (toAsync (Error "Bad")) (toAsync (Ok 1))
564+
Expect.equal v (Error("Bad")) "Should be Error"
565+
}
566+
testCaseAsync "Error, Error" <| async {
567+
let! v = AsyncResult.zip (toAsync (Error "Bad1")) (toAsync (Error "Bad2"))
568+
Expect.equal v (Error("Bad1")) "Should be Error"
569+
}
570+
]
571+
572+
573+
let zipErrorTests =
574+
testList "AsyncResult.zipError Tests" [
575+
testCaseAsync "Ok, Ok" <| async {
576+
let! v = AsyncResult.zipError (toAsync (Ok 1)) (toAsync (Ok 2))
577+
Expect.equal v (Ok(1)) "Should be ok"
578+
}
579+
testCaseAsync "Ok, Error" <| async {
580+
let! v = AsyncResult.zipError (toAsync (Ok 1)) (toAsync (Error "Bad"))
581+
Expect.equal v (Ok 1) "Should be ok"
582+
}
583+
testCaseAsync "Error, Ok" <| async {
584+
let! v = AsyncResult.zipError (toAsync (Error "Bad")) (toAsync (Ok 1))
585+
Expect.equal v (Ok 1) "Should be ok"
586+
}
587+
testCaseAsync "Error, Error" <| async {
588+
let! v = AsyncResult.zipError (toAsync (Error "Bad1")) (toAsync (Error "Bad2"))
589+
Expect.equal v (Error("Bad1", "Bad2")) "Should be Error"
590+
}
591+
]
592+
550593

551594
let allTests = testList "Async Result tests" [
552595
mapTests
@@ -576,4 +619,6 @@ let allTests = testList "Async Result tests" [
576619
catchTests
577620
asyncResultCETests
578621
asyncResultOperatorTests
622+
zipTests
623+
zipErrorTests
579624
]

0 commit comments

Comments
 (0)