-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathJobResult.fs
More file actions
829 lines (673 loc) · 23.7 KB
/
JobResult.fs
File metadata and controls
829 lines (673 loc) · 23.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
module JobResultTests
open Expecto
open Expects.JobResult
open SampleDomain
open TestData
open TestHelpers
open FsToolkit.ErrorHandling
// open FsToolkit.ErrorHandling.Operator.JobResult
open System
open Hopac
let runJobSync = run
let createPostSuccess =
createPostSuccess
>> Job.fromAsync
let createPostFailure =
createPostFailure
>> Job.fromAsync
let getFollowersSuccess =
getFollowersSuccess
>> Job.fromAsync
let getFollowersFailure =
getFollowersFailure
>> Job.fromAsync
let allowedToPost =
allowedToPost
>> Job.fromAsync
[<Tests>]
let mapTests =
testList "JobResult.map tests" [
testCase "map with Task(Ok x)"
<| fun _ ->
createPostSuccess validCreatePostRequest
|> JobResult.map (fun (PostId id) -> { Id = id })
|> Expect.hasJobOkValueSync { Id = newPostId }
testCase "map with Task(Error x)"
<| fun _ ->
createPostFailure validCreatePostRequest
|> JobResult.mapError (fun ex -> { Message = ex.Message })
|> Expect.hasJobErrorValueSync { Message = "something went wrong!" }
]
[<Tests>]
let map2Tests =
testList "JobResult.map2 tests" [
testCase "map2 with Task(Ok x) Task(Ok y)"
<| fun _ ->
let userId = Guid.NewGuid()
let getFollowersResult = getFollowersSuccess (UserId userId)
let createPostResult = createPostSuccess validCreatePostRequest
JobResult.map2 newPostRequest getFollowersResult createPostResult
|> Expect.hasJobOkValueSync {
NewPostId = PostId newPostId
UserIds = followerIds
}
testCase "map2 with Task(Error x) Task(Ok y)"
<| fun _ ->
let userId = Guid.NewGuid()
let getFollowersResult = getFollowersFailure (UserId userId)
let createPostResult = createPostSuccess validCreatePostRequest
JobResult.map2 newPostRequest getFollowersResult createPostResult
|> Expect.hasJobErrorValueSync getFollowersEx
testCase "map2 with Task(Ok x) Task(Error y)"
<| fun _ ->
let userId = Guid.NewGuid()
let getFollowersResult = getFollowersSuccess (UserId userId)
let createPostResult = createPostFailure validCreatePostRequest
JobResult.map2 newPostRequest getFollowersResult createPostResult
|> Expect.hasJobErrorValueSync commonEx
testCase "map2 with Task(Error x) Task(Error y)"
<| fun _ ->
let userId = Guid.NewGuid()
let getFollowersResult = getFollowersFailure (UserId userId)
let createPostResult = createPostFailure validCreatePostRequest
JobResult.map2 newPostRequest getFollowersResult createPostResult
|> Expect.hasJobErrorValueSync getFollowersEx
]
[<Tests>]
let mapErrorTests =
testList "JobResult.mapError tests" [
testCase "mapError with Task(Ok x)"
<| fun _ ->
createPostSuccess validCreatePostRequest
|> JobResult.mapError id
|> runJobSync
|> flip Expect.isOk "mapError should not map Ok"
testCase "mapError with Task(Error x)"
<| fun _ ->
createPostFailure validCreatePostRequest
|> JobResult.mapError (fun ex -> ex.Message)
|> Expect.hasJobErrorValueSync (commonEx.Message)
]
[<Tests>]
let bindTests =
testList "JobResult.bind tests" [
testCase "bind with Task(Ok x)"
<| fun _ ->
allowedToPost sampleUserId
|> JobResult.bind (fun isAllowed ->
job {
if isAllowed then
return! createPostSuccess validCreatePostRequest
else
return (Error(Exception "not allowed to post"))
}
)
|> Expect.hasJobOkValueSync (PostId newPostId)
testCase "bind with Task(Error x)"
<| fun _ ->
allowedToPost (UserId(Guid.NewGuid()))
|> JobResult.bind (fun isAllowed -> job { return Ok isAllowed })
|> Expect.hasJobErrorValueSync commonEx
testCase "bind with Task(Ok x) that returns Task (Error x)"
<| fun _ ->
let ex = Exception "not allowed to post"
allowedToPost sampleUserId
|> JobResult.bind (fun _ -> job { return (Error ex) })
|> Expect.hasJobErrorValueSync ex
]
[<Tests>]
let orElseTests =
testList "JobResult.orElseWith Tests" [
testCaseJob "Ok Ok takes first Ok"
<| job {
return!
JobResult.singleton "First"
|> JobResult.orElse (JobResult.singleton "Second")
|> Expect.hasJobOkValue "First"
}
testCaseJob "Ok Error takes first Ok"
<| job {
return!
JobResult.singleton "First"
|> JobResult.orElse (JobResult.error "Second")
|> Expect.hasJobOkValue "First"
}
testCaseJob "Error Ok takes second Ok"
<| job {
return!
JobResult.error "First"
|> JobResult.orElse (JobResult.singleton "Second")
|> Expect.hasJobOkValue "Second"
}
testCaseJob "Error Error takes second error"
<| job {
return!
JobResult.error "First"
|> JobResult.orElse (JobResult.error "Second")
|> Expect.hasJobErrorValue "Second"
}
]
[<Tests>]
let orElseWithTests =
testList "JobResult.orElse Tests" [
testCaseJob "Ok Ok takes first Ok"
<| job {
return!
JobResult.singleton "First"
|> JobResult.orElseWith (fun _ -> JobResult.singleton "Second")
|> Expect.hasJobOkValue "First"
}
testCaseJob "Ok Error takes first Ok"
<| job {
return!
JobResult.singleton "First"
|> JobResult.orElseWith (fun _ -> JobResult.error "Second")
|> Expect.hasJobOkValue "First"
}
testCaseJob "Error Ok takes second Ok"
<| job {
return!
JobResult.error "First"
|> JobResult.orElseWith (fun _ -> JobResult.singleton "Second")
|> Expect.hasJobOkValue "Second"
}
testCaseJob "Error Error takes second error"
<| job {
return!
JobResult.error "First"
|> JobResult.orElseWith (fun _ -> JobResult.error "Second")
|> Expect.hasJobErrorValue "Second"
}
]
[<Tests>]
let ignoreTests =
testList "JobResult.ignore tests" [
testCase "ignore with Task(Ok x)"
<| fun _ ->
createPostSuccess validCreatePostRequest
|> JobResult.ignore
|> Expect.hasJobOkValueSync ()
testCase "ignore with Task(Error x)"
<| fun _ ->
createPostFailure validCreatePostRequest
|> JobResult.ignore
|> Expect.hasJobErrorValueSync commonEx
testCase "can call ignore without type parameters"
<| fun _ -> ignore JobResult.ignore
testCase "can call ignore with type parameters"
<| fun _ ->
ignore<Job<Result<int, string>> -> Job<Result<unit, string>>>
JobResult.ignore<int, string>
]
let err = "foobar"
let toJob = Job.singleton
[<Tests>]
let requireTrueTests =
testList "JobResult.requireTrue Tests" [
testCase "requireTrue happy path"
<| fun _ ->
toJob true
|> JobResult.requireTrue err
|> Expect.hasJobOkValueSync ()
testCase "requireTrue error path"
<| fun _ ->
toJob false
|> JobResult.requireTrue err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireFalseTests =
testList "JobResult.requireFalse Tests" [
testCase "requireFalse happy path"
<| fun _ ->
toJob false
|> JobResult.requireFalse err
|> Expect.hasJobOkValueSync ()
testCase "requireFalse error path"
<| fun _ ->
toJob true
|> JobResult.requireFalse err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireSomeTests =
testList "JobResult.requireSome Tests" [
testCase "requireSome happy path"
<| fun _ ->
toJob (Some 42)
|> JobResult.requireSome err
|> Expect.hasJobOkValueSync 42
testCase "requireSome error path"
<| fun _ ->
toJob None
|> JobResult.requireSome err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireSomeWithTests =
testList "JobResult.requireSomeWith Tests" [
testCase "requireSomeWith happy path"
<| fun _ ->
toJob (Some 42)
|> JobResult.requireSomeWith (fun () -> err)
|> Expect.hasJobOkValueSync 42
testCase "requireSomeWith error path"
<| fun _ ->
toJob None
|> JobResult.requireSomeWith (fun () -> err)
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireNoneTests =
testList "JobResult.requireNone Tests" [
testCase "requireNone happy path"
<| fun _ ->
toJob None
|> JobResult.requireNone err
|> Expect.hasJobOkValueSync ()
testCase "requireNone error path"
<| fun _ ->
toJob (Some 42)
|> JobResult.requireNone err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireNoneWithTests =
testList "JobResult.requireNoneWith Tests" [
testCase "requireNoneWith happy path"
<| fun _ ->
toJob None
|> JobResult.requireNoneWith (fun () -> err)
|> Expect.hasJobOkValueSync ()
testCase "requireNoneWith error path"
<| fun _ ->
toJob (Some 42)
|> JobResult.requireNoneWith (fun () -> err)
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireValueSomeTests =
testList "JobResult.requireValueSome Tests" [
testCase "requireValueSome happy path"
<| fun _ ->
toJob (ValueSome 42)
|> JobResult.requireValueSome err
|> Expect.hasJobOkValueSync 42
testCase "requireValueSome error path"
<| fun _ ->
toJob ValueNone
|> JobResult.requireValueSome err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireValueNoneTests =
testList "JobResult.requireValueNone Tests" [
testCase "requireValueNone happy path"
<| fun _ ->
toJob ValueNone
|> JobResult.requireValueNone err
|> Expect.hasJobOkValueSync ()
testCase "requireValueNone error path"
<| fun _ ->
toJob (ValueSome 42)
|> JobResult.requireValueNone err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireEqualToTests =
testList "JobResult.requireEqualTo Tests" [
testCase "requireEqualTo happy path"
<| fun _ ->
toJob 42
|> JobResult.requireEqualTo 42 err
|> Expect.hasJobOkValueSync ()
testCase "requireEqualTo error path"
<| fun _ ->
toJob 43
|> JobResult.requireEqualTo 42 err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireEqualTests =
testList "JobResult.requireEqual Tests" [
testCase "requireEqual happy path"
<| fun _ ->
JobResult.requireEqual 42 (toJob 42) err
|> Expect.hasJobOkValueSync ()
testCase "requireEqual error path"
<| fun _ ->
JobResult.requireEqual 42 (toJob 43) err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireEmptyTests =
testList "JobResult.requireEmpty Tests" [
testCase "requireEmpty happy path"
<| fun _ ->
toJob []
|> JobResult.requireEmpty err
|> Expect.hasJobOkValueSync ()
testCase "requireEmpty error path"
<| fun _ ->
toJob [ 42 ]
|> JobResult.requireEmpty err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireNotEmptyTests =
testList "JobResult.requireNotEmpty Tests" [
testCase "requireNotEmpty happy path"
<| fun _ ->
toJob [ 42 ]
|> JobResult.requireNotEmpty err
|> Expect.hasJobOkValueSync ()
testCase "requireNotEmpty error path"
<| fun _ ->
toJob []
|> JobResult.requireNotEmpty err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let requireHeadTests =
testList "JobResult.requireHead Tests" [
testCase "requireHead happy path"
<| fun _ ->
toJob [ 42 ]
|> JobResult.requireHead err
|> Expect.hasJobOkValueSync 42
testCase "requireHead error path"
<| fun _ ->
toJob []
|> JobResult.requireHead err
|> Expect.hasJobErrorValueSync err
]
[<Tests>]
let setErrorTests =
testList "JobResult.setError Tests" [
testCase "setError replaces a any error value with a custom error value"
<| fun _ ->
toJob (Error "foo")
|> JobResult.setError err
|> Expect.hasJobErrorValueSync err
testCase "setError does not change an ok value"
<| fun _ ->
toJob (Ok 42)
|> JobResult.setError err
|> Expect.hasJobOkValueSync 42
]
[<Tests>]
let withErrorTests =
testList "JobResult.withError Tests" [
testCase "withError replaces a any error value with a custom error value"
<| fun _ ->
toJob (Error())
|> JobResult.withError err
|> Expect.hasJobErrorValueSync err
testCase "withError does not change an ok value"
<| fun _ ->
toJob (Ok 42)
|> JobResult.withError err
|> Expect.hasJobOkValueSync 42
]
[<Tests>]
let defaultValueTests =
testList "JobResult.defaultValue Tests" [
testCase "defaultValue returns the ok value"
<| fun _ ->
let v = JobResult.defaultValue 43 (toJob (Ok 42))
Expect.hasJobValue 42 v
testCase "defaultValue returns the given value for Error"
<| fun _ ->
let v = JobResult.defaultValue 43 (toJob (Error err))
Expect.hasJobValue 43 v
]
[<Tests>]
let defaultErrorTests =
testList "JobResult.defaultError Tests" [
testCase "defaultError returns the error value"
<| fun _ ->
let v = JobResult.defaultError 43 (toJob (Error 42))
Expect.hasJobValue 42 v
testCase "defaultError returns the given value for Ok"
<| fun _ ->
let v = JobResult.defaultError 43 (toJob (Ok 42))
Expect.hasJobValue 43 v
]
[<Tests>]
let defaultWithTests =
testList "JobResult.defaultWith Tests" [
testCase "defaultWith returns the ok value"
<| fun _ ->
let v = JobResult.defaultWith (fun _ -> 43) (toJob (Ok 42))
Expect.hasJobValue 42 v
testCase "defaultValue invoks the given thunk for Error"
<| fun _ ->
let v = JobResult.defaultWith (fun _ -> 42) (toJob (Error err))
Expect.hasJobValue 42 v
]
[<Tests>]
let ignoreErrorTests =
testList "JobResult.ignoreError Tests" [
testCase "ignoreError returns the unit for ok"
<| fun _ -> Expect.hasJobValue () (JobResult.ignoreError (toJob (Ok())))
testCase "ignoreError returns the unit for Error"
<| fun _ -> Expect.hasJobValue () (JobResult.ignoreError (toJob (Error err)))
testCase "Can call ignoreError without type parameter"
<| fun _ -> ignore JobResult.ignoreError
testCase "Can call ignoreError with type parameter"
<| fun _ -> ignore<Job<Result<unit, string>> -> Job<unit>> JobResult.ignoreError<string>
]
[<Tests>]
let teeTests =
testList "JobResult.tee Tests" [
testCase "tee executes the function for ok"
<| fun _ ->
let mutable foo = "foo"
let mutable input = 0
let bar x =
input <- x
foo <- "bar"
let result = JobResult.tee bar (toJob (Ok 42))
Expect.hasJobOkValueSync 42 result
Expect.equal foo "bar" ""
Expect.equal input 42 ""
testCase "tee ignores the function for Error"
<| fun _ ->
let mutable foo = "foo"
let bar _ = foo <- "bar"
let result = JobResult.tee bar (toJob (Error err))
Expect.hasJobErrorValueSync err result
Expect.equal foo "foo" ""
]
let returnTrue _ = true
let returnFalse _ = false
[<Tests>]
let teeIfTests =
testList "JobResult.teeIf Tests" [
testCase "teeIf executes the function for ok and true predicate "
<| fun _ ->
let mutable foo = "foo"
let mutable input = 0
let mutable pInput = 0
let returnTrue x =
pInput <- x
true
let bar x =
input <- x
foo <- "bar"
let result = JobResult.teeIf returnTrue bar (toJob (Ok 42))
Expect.hasJobOkValueSync 42 result
Expect.equal foo "bar" ""
Expect.equal input 42 ""
Expect.equal pInput 42 ""
testCase "teeIf ignores the function for Ok and false predicate"
<| fun _ ->
let mutable foo = "foo"
let bar _ = foo <- "bar"
let result = JobResult.teeIf returnFalse bar (toJob (Ok 42))
Expect.hasJobOkValueSync 42 result
Expect.equal foo "foo" ""
testCase "teeIf ignores the function for Error"
<| fun _ ->
let mutable foo = "foo"
let bar _ = foo <- "bar"
let result = JobResult.teeIf returnTrue bar (toJob (Error err))
Expect.hasJobErrorValueSync err result
Expect.equal foo "foo" ""
]
[<Tests>]
let teeErrorTests =
testList "JobResult.teeError Tests" [
testCase "teeError executes the function for Error"
<| fun _ ->
let mutable foo = "foo"
let mutable input = ""
let bar x =
input <- x
foo <- "bar"
let result = JobResult.teeError bar (toJob (Error err))
Expect.hasJobErrorValueSync err result
Expect.equal foo "bar" ""
Expect.equal input err ""
testCase "teeError ignores the function for Ok"
<| fun _ ->
let mutable foo = "foo"
let bar _ = foo <- "bar"
let result = JobResult.teeError bar (toJob (Ok 42))
Expect.hasJobOkValueSync 42 result
Expect.equal foo "foo" ""
]
[<Tests>]
let teeErrorIfTests =
testList "JobResult.teeErrorIf Tests" [
testCase "teeErrorIf executes the function for Error and true predicate "
<| fun _ ->
let mutable foo = "foo"
let mutable input = ""
let mutable pInput = ""
let returnTrue x =
pInput <- x
true
let bar x =
input <- x
foo <- "bar"
let result = JobResult.teeErrorIf returnTrue bar (toJob (Error err))
Expect.hasJobErrorValueSync err result
Expect.equal foo "bar" ""
Expect.equal input err ""
Expect.equal pInput err ""
testCase "teeErrorIf ignores the function for Error and false predicate"
<| fun _ ->
let mutable foo = "foo"
let bar _ = foo <- "bar"
let result = JobResult.teeErrorIf returnFalse bar (toJob (Error err))
Expect.hasJobErrorValueSync err result
Expect.equal foo "foo" ""
testCase "teeErrorIf ignores the function for Ok"
<| fun _ ->
let mutable foo = "foo"
let bar _ = foo <- "bar"
let result = JobResult.teeErrorIf returnTrue bar (toJob (Ok 42))
Expect.hasJobOkValueSync 42 result
Expect.equal foo "foo" ""
]
[<Tests>]
let catchTests =
let f (e: exn) = e.Message
let jobThrow () =
job {
failwith err
return Error ""
}
testList "JobResult.catch tests" [
testCase "catch returns success for Ok"
<| fun _ -> Expect.hasJobOkValueSync 42 (JobResult.catch f (toJob (Ok 42)))
testCase "catch returns mapped Error for exception"
<| fun _ -> Expect.hasJobErrorValueSync err (JobResult.catch f (jobThrow ()))
testCase "catch returns unmapped error without exception"
<| fun _ ->
Expect.hasJobErrorValueSync "unmapped" (JobResult.catch f (toJob (Error "unmapped")))
]
[<Tests>]
let zipTests =
testList "JobResult.zip tests" [
testCase "Ok, Ok"
<| fun _ ->
let v = JobResult.zip (toJob (Ok 1)) (toJob (Ok 2))
Expect.hasJobValue (Ok(1, 2)) v
testCase "Ok, Error"
<| fun _ ->
let v = JobResult.zip (toJob (Ok 1)) (toJob (Error "Bad"))
Expect.hasJobValue (Error("Bad")) v
testCase "Error, Ok"
<| fun _ ->
let v = JobResult.zip (toJob (Error "Bad")) (toJob (Ok 1))
Expect.hasJobValue (Error("Bad")) v
testCase "Error, Error"
<| fun _ ->
let v = JobResult.zip (toJob (Error "Bad1")) (toJob (Error "Bad2"))
Expect.hasJobValue (Error("Bad1")) v
]
[<Tests>]
let zipErrorTests =
testList "JobResult.zipError tests" [
testCase "Ok, Ok"
<| fun _ ->
let v = JobResult.zipError (toJob (Ok 1)) (toJob (Ok 2))
Expect.hasJobValue (Ok(1)) v
testCase "Ok, Error"
<| fun _ ->
let v = JobResult.zipError (toJob (Ok 1)) (toJob (Error "Bad"))
Expect.hasJobValue (Ok 1) v
testCase "Error, Ok"
<| fun _ ->
let v = JobResult.zipError (toJob (Error "Bad")) (toJob (Ok 1))
Expect.hasJobValue (Ok 1) v
testCase "Error, Error"
<| fun _ ->
let v = JobResult.zipError (toJob (Error "Bad1")) (toJob (Error "Bad2"))
Expect.hasJobValue (Error("Bad1", "Bad2")) v
]
[<Tests>]
let bindRequireTests =
testList "JobResult Bind + Require tests" [
testCaseJob "bindRequireNone"
<| job {
return!
Some "john_doe"
|> JobResult.singleton
|> JobResult.bindRequireNone "user exists"
|> Expect.hasJobErrorValue "user exists"
}
testCaseJob "bindRequireSome"
<| job {
return!
Some "john_doe"
|> JobResult.singleton
|> JobResult.bindRequireSome "user doesn't exists"
|> Expect.hasJobOkValue "john_doe"
}
]
[<Tests>]
let bindRequireValueOptionTests =
testList "JobResult Bind + RequireValueOption tests" [
testCaseJob "bindRequireValueNone"
<| job {
return!
ValueSome "john_doe"
|> JobResult.singleton
|> JobResult.bindRequireValueNone "user exists"
|> Expect.hasJobErrorValue "user exists"
}
testCaseJob "bindRequireValueSome"
<| job {
return!
ValueSome "john_doe"
|> JobResult.singleton
|> JobResult.bindRequireValueSome "user doesn't exists"
|> Expect.hasJobOkValue "john_doe"
}
]
type CreatePostResult =
| PostSuccess of NotifyNewPostRequest
| NotAllowedToPost