From c85c4977b750c98bf8e24a4ad469b6d859cc78b1 Mon Sep 17 00:00:00 2001 From: Sergey Tihon Date: Tue, 27 Sep 2022 20:33:06 +0200 Subject: [PATCH] fix for #218 --- .gitignore | 1 + src/FsUnit.NUnit/FsUnit.fs | 3 ++- src/FsUnit.Xunit/CustomMatchers.fs | 7 +++++-- tests/FsUnit.MsTest.Test/shouldFailTests.fs | 10 ++++++++-- tests/FsUnit.NUnit.Test/shouldFailTests.fs | 10 ++++++++-- tests/FsUnit.Xunit.Test/shouldFailTests.fs | 6 ++++++ 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index baa5252c..f77b8cce 100644 --- a/.gitignore +++ b/.gitignore @@ -90,6 +90,7 @@ docs/output/ # Temp folder used for publishing docs temp/ .vs/ +.idea/ .fake/ .fsdocs/ tmp/ diff --git a/src/FsUnit.NUnit/FsUnit.fs b/src/FsUnit.NUnit/FsUnit.fs index d85f0ec0..6acb0576 100644 --- a/src/FsUnit.NUnit/FsUnit.fs +++ b/src/FsUnit.NUnit/FsUnit.fs @@ -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 diff --git a/src/FsUnit.Xunit/CustomMatchers.fs b/src/FsUnit.Xunit/CustomMatchers.fs index a62faa20..f234ee1b 100644 --- a/src/FsUnit.Xunit/CustomMatchers.fs +++ b/src/FsUnit.Xunit/CustomMatchers.fs @@ -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($"{string t} \"{m}\"", Func<_, _> matches) diff --git a/tests/FsUnit.MsTest.Test/shouldFailTests.fs b/tests/FsUnit.MsTest.Test/shouldFailTests.fs index f340a587..53e8ad71 100644 --- a/tests/FsUnit.MsTest.Test/shouldFailTests.fs +++ b/tests/FsUnit.MsTest.Test/shouldFailTests.fs @@ -2,6 +2,7 @@ namespace FsUnit.Test open Microsoft.VisualStudio.TestTools.UnitTesting open FsUnit.MsTest +open System [] type ``shouldFail tests``() = @@ -18,11 +19,16 @@ type ``shouldFail tests``() = shouldFail(fun () -> shouldFail id) [] - member _.``shouldFaild should throw an exception``() = + member _.``shouldFail should throw an exception``() = (fun () -> shouldFail id) |> should throw typeof [] - 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) + + [] + member _.``test raising exception``() = + fun () -> raise(ArgumentException "help") + |> should (throwWithMessage "help") typeof diff --git a/tests/FsUnit.NUnit.Test/shouldFailTests.fs b/tests/FsUnit.NUnit.Test/shouldFailTests.fs index cae9e10e..29f12556 100644 --- a/tests/FsUnit.NUnit.Test/shouldFailTests.fs +++ b/tests/FsUnit.NUnit.Test/shouldFailTests.fs @@ -1,5 +1,6 @@ namespace FsUnit.Test +open System open NUnit.Framework open FsUnit @@ -18,11 +19,16 @@ type ``shouldFail tests``() = shouldFail(fun () -> shouldFail id) [] - member _.``shouldFaild should throw an exception``() = + member _.``shouldFail should throw an exception``() = (fun () -> shouldFail id) |> should throw typeof [] - 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) + + [] + member _.``test raising exception``() = + fun () -> raise(ArgumentException "help") + |> should (throwWithMessage "help") typeof diff --git a/tests/FsUnit.Xunit.Test/shouldFailTests.fs b/tests/FsUnit.Xunit.Test/shouldFailTests.fs index 5c3b200a..20ec4313 100644 --- a/tests/FsUnit.Xunit.Test/shouldFailTests.fs +++ b/tests/FsUnit.Xunit.Test/shouldFailTests.fs @@ -1,5 +1,6 @@ namespace FsUnit.Test +open System open Xunit open FsUnit.Xunit @@ -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) + + [] + member _.``test raising exception``() = + fun () -> raise(ArgumentException "help") + |> should (throwWithMessage "help") typeof