Skip to content

Commit

Permalink
Merge pull request #220 from sergey-tihon/fix/i218
Browse files Browse the repository at this point in the history
Support testFunc that return obj
  • Loading branch information
CaptnCodr authored Sep 27, 2022
2 parents e8390d5 + c85c497 commit 640fa33
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ docs/output/
# Temp folder used for publishing docs
temp/
.vs/
.idea/
.fake/
.fsdocs/
tmp/
Expand Down
3 changes: 2 additions & 1 deletion src/FsUnit.NUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ module TopLevelOperators =

let y =
match actual with
| :? (unit -> unit) -> box(TestDelegate(actual :?> unit -> unit))
| :? (unit -> unit) as testFunc -> box(TestDelegate(testFunc))
| :? (unit -> obj) as testFunc -> box(TestDelegate(testFunc >> ignore))
| _ -> actual

if isNull(box expression) then
Expand Down
7 changes: 5 additions & 2 deletions src/FsUnit.Xunit/CustomMatchers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ let throw(t: Type) =

let throwWithMessage (m: string) (t: Type) =
let matches(f: obj) =
match f with
| :? (unit -> unit) as testFunc ->
let wrap testFunc =
try
testFunc()
false
with ex ->
ex.GetType() = t && ex.Message = m

match f with
| :? (unit -> unit) as testFunc -> wrap testFunc
| :? (unit -> obj) as testFunc -> wrap(testFunc >> ignore)
| _ -> false

CustomMatcher<obj>($"{string t} \"{m}\"", Func<_, _> matches)
Expand Down
10 changes: 8 additions & 2 deletions tests/FsUnit.MsTest.Test/shouldFailTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace FsUnit.Test

open Microsoft.VisualStudio.TestTools.UnitTesting
open FsUnit.MsTest
open System

[<TestClass>]
type ``shouldFail tests``() =
Expand All @@ -18,11 +19,16 @@ type ``shouldFail tests``() =
shouldFail(fun () -> shouldFail id)

[<TestMethod>]
member _.``shouldFaild should throw an exception``() =
member _.``shouldFail should throw an exception``() =
(fun () -> shouldFail id)
|> should throw typeof<AssertFailedException>

[<TestMethod>]
member _.``shouldFaild should not throw an exception when fail``() =
member _.``shouldFail should not throw an exception when fail``() =
(fun () -> shouldFail(fun () -> [] |> should contain 1))
|> should not' (throw typeof<AssertFailedException>)

[<TestMethod>]
member _.``test raising exception``() =
fun () -> raise(ArgumentException "help")
|> should (throwWithMessage "help") typeof<ArgumentException>
10 changes: 8 additions & 2 deletions tests/FsUnit.NUnit.Test/shouldFailTests.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FsUnit.Test

open System
open NUnit.Framework
open FsUnit

Expand All @@ -18,11 +19,16 @@ type ``shouldFail tests``() =
shouldFail(fun () -> shouldFail id)

[<Test>]
member _.``shouldFaild should throw an exception``() =
member _.``shouldFail should throw an exception``() =
(fun () -> shouldFail id)
|> should throw typeof<NUnit.Framework.AssertionException>

[<Test>]
member _.``shouldFaild should not throw an exception when fail``() =
member _.``shouldFail should not throw an exception when fail``() =
(fun () -> shouldFail(fun () -> [] |> should contain 1))
|> should not' (throw typeof<NUnit.Framework.AssertionException>)

[<Test>]
member _.``test raising exception``() =
fun () -> raise(ArgumentException "help")
|> should (throwWithMessage "help") typeof<ArgumentException>
6 changes: 6 additions & 0 deletions tests/FsUnit.Xunit.Test/shouldFailTests.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FsUnit.Test

open System
open Xunit
open FsUnit.Xunit

Expand All @@ -25,3 +26,8 @@ type ``shouldFail tests``() =
member _.``shouldFail should not throw an exception when fail``() =
(fun () -> shouldFail(fun () -> [] |> should contain 1))
|> should not' (throw typeof<MatchException>)

[<Fact>]
member _.``test raising exception``() =
fun () -> raise(ArgumentException "help")
|> should (throwWithMessage "help") typeof<ArgumentException>

0 comments on commit 640fa33

Please sign in to comment.