Skip to content

Commit 21cb45a

Browse files
authored
Updated TUnit.Assertions.FSharp.Operations check function to support assertions with Throw Type (#2431)
* Updated Check to support assertions with Throw Type * removed unused open declarations in Extensions.fs * updated fsharp assertions readme
1 parent b782b8a commit 21cb45a

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
namespace TUnit.Assertions.FSharp
22

33
open TUnit.Assertions.AssertionBuilders
4+
open TUnit.Assertions.AssertConditions.Throws
45

56
module Operations =
67
[<CustomOperation(MaintainsVariableSpaceUsingBind = true)>]
7-
let check (assertion: IInvokableAssertionBuilder) =
8+
let check (assertion: 'T) =
89
Async.FromContinuations(fun (cont, econt, ccont) ->
9-
let awaiter = assertion.GetAwaiter()
10-
11-
awaiter.OnCompleted(fun () ->
12-
try
13-
if awaiter.IsCompleted then
14-
cont(awaiter.GetResult())
15-
else
16-
ccont (System.OperationCanceledException())
17-
with ex ->
18-
econt ex))
10+
match box assertion with
11+
| :? IInvokableAssertionBuilder as invokable ->
12+
let awaiter = invokable.GetAwaiter()
13+
awaiter.OnCompleted(fun () ->
14+
try
15+
if awaiter.IsCompleted then
16+
cont(awaiter.GetResult())
17+
else
18+
ccont (System.OperationCanceledException())
19+
with ex ->
20+
econt ex)
21+
| :? ThrowsException<obj, exn> as throwsExn ->
22+
let awaiter = throwsExn.GetAwaiter()
23+
awaiter.OnCompleted(fun () ->
24+
try
25+
if awaiter.IsCompleted then
26+
let _ = awaiter.GetResult() // ignore the exn result
27+
cont ()
28+
else
29+
ccont (System.OperationCanceledException())
30+
with ex ->
31+
econt ex)
32+
| _ ->
33+
invalidOp $"Unsupported assertion type: We currently don't support Assertion Type {assertion.GetType()}"
34+
)

docs/docs/tutorial-assertions/fsharp.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,30 @@ member this.CheckPositive() = async {
1717
let result = 1 + 1
1818
do! check (Assert.That(result).IsPositive())
1919
}
20-
```
20+
```
21+
22+
F# is a lot more strict with type resolution when it comes to extension methods and method overloads. Because of that you may need to annotate the type for the assertions.
23+
24+
For example,
25+
26+
```fsharp
27+
[<Test>]
28+
[<Category("Pass")>]
29+
member _.Test3() = async {
30+
let value = "1"
31+
do! check (Assert.That<string>(value).IsEqualTo("1"))
32+
}
33+
34+
[<Test>]
35+
[<Category("Fail")>]
36+
member _.Throws1() = async {
37+
do! check (Assert.That<string>(fun () -> task { return new string([||]) }).ThrowsException())
38+
}
39+
40+
[<Test>]
41+
[<Category("Pass")>]
42+
member _.Throws4() = async {
43+
do! check (Assert.That<bool>(fun () -> Task.FromResult(true)).ThrowsNothing())
44+
}
45+
46+
```

0 commit comments

Comments
 (0)