11module AsyncExtraTests
22
3+ open System.Threading
4+ open System.Threading .Tasks
35open Insurello.AsyncExtra
46open Expecto
57
68[<Tests>]
79let tests =
8- testList " Sequence tests"
10+ testList
11+ " Sequence tests"
912 [ testAsync " should return values in same order as given tasks" {
1013 let dummyAsync : int -> AsyncResult < int , string > = Ok >> AsyncResult.fromResult
1114
@@ -15,6 +18,7 @@ let tests =
1518 [ ( dummyAsync 1 )
1619 ( dummyAsync 2 )
1720 ( dummyAsync 3 ) ]
21+
1822 let! actual = AsyncResult.sequence input
1923 Expect.equal actual expected " should equal"
2024 }
@@ -37,3 +41,57 @@ let tests =
3741 let! _actual = AsyncResult.sequence input
3842 Expect.equal orderRun expectedOkValue " Should be run in same order"
3943 } ]
44+
45+ [<Tests>]
46+ let taskTests =
47+ testList
48+ " Task tests"
49+ [ testList
50+ " fromUnitTask"
51+ [ testAsync " should convert from Task to AsyncResult" {
52+ let source = new CancellationTokenSource()
53+ let input : Task = Task.Delay( 0 , source.Token)
54+
55+ let expectedValue = Ok()
56+
57+ let! actual = AsyncResult.fromUnitTask input
58+
59+ Expect.equal actual expectedValue " Should be equal"
60+ }
61+ testAsync " failing Task should result in Error" {
62+ let source = new CancellationTokenSource()
63+ let input : Task = Task.Delay( 1000 , source.Token)
64+ let expectedValue = Error " A task was canceled."
65+
66+ source.Cancel()
67+
68+ let! actual = AsyncResult.fromUnitTask input
69+
70+ Expect.equal actual expectedValue " Should be equal"
71+ } ]
72+ testList
73+ " fromTask"
74+ [ testAsync " should convert from Task<string> to AsyncResult" {
75+ let input =
76+ Async.singleton " Hello" |> Async.StartAsTask
77+
78+ let expectedValue = Ok " Hello"
79+
80+ let! actual = AsyncResult.fromTask input
81+
82+ Expect.equal actual expectedValue " Should be equal"
83+ }
84+ testAsync " fromTask failing Task should result in Error" {
85+
86+ let input =
87+ Async.singleton " Hello"
88+ |> Async.map ( fun _ -> failwith " boom" )
89+ |> Async.StartAsTask
90+
91+ let expectedValue =
92+ Error " One or more errors occurred. (boom)"
93+
94+ let! actual = AsyncResult.fromTask input
95+
96+ Expect.equal actual expectedValue " Should be equal"
97+ } ] ]
0 commit comments