diff --git a/src/FsUnit.MsTestUnit/FsUnit.fs b/src/FsUnit.MsTestUnit/FsUnit.fs index 97fafb5c..fe76de49 100644 --- a/src/FsUnit.MsTestUnit/FsUnit.fs +++ b/src/FsUnit.MsTestUnit/FsUnit.fs @@ -1,119 +1,117 @@ -module FsUnit.MsTest - -open System -open Microsoft.VisualStudio.TestTools.UnitTesting -open NHamcrest -open NHamcrest.Core -open System.Collections - -let inline private assertThat(actual, matcher:IMatcher<'a>) = - if not (matcher.Matches(actual)) then - let description = new StringDescription() - matcher.DescribeTo(description) - let mismatchDescription = new StringDescription() - matcher.DescribeMismatch(actual, mismatchDescription) - raise (new AssertFailedException(sprintf "%s %s" (description.ToString()) (mismatchDescription.ToString()))) - -#if NETSTANDARD1_6 -type Assert with - static member That'<'a> (actual, matcher:IMatcher<'a>) = - assertThat(actual, matcher) -#else -type Assert with - static member That<'a> (actual, matcher:IMatcher<'a>) = - assertThat(actual, matcher) -#endif - -let inline should (f : 'a -> ^b) x (y : obj) = - let c = f x - let y = - match y with - | :? (unit -> unit) as assertFunc -> box assertFunc - | _ -> y - if box c = null then +module FsUnit.MsTest + +open System +open Microsoft.VisualStudio.TestTools.UnitTesting +open NHamcrest +open NHamcrest.Core +open System.Collections + +let inline private assertThat(actual, matcher:IMatcher<'a>) = + if not (matcher.Matches(actual)) then + let description = new StringDescription() + matcher.DescribeTo(description) + raise (new AssertFailedException(sprintf "%s %s" (description.ToString()) (sprintf "was %A" actual))) + +#if NETSTANDARD1_6 +type Assert with + static member That'<'a> (actual, matcher:IMatcher<'a>) = + assertThat(actual, matcher) +#else +type Assert with + static member That<'a> (actual, matcher:IMatcher<'a>) = + assertThat(actual, matcher) +#endif + +let inline should (f : 'a -> ^b) x (y : obj) = + let c = f x + let y = + match y with + | :? (unit -> unit) as assertFunc -> box assertFunc + | _ -> y + if isNull (box c) then assertThat(y, Is.Null()) else - assertThat(y, c) - - -let inline shouldFail (f:unit->unit) = - let failed = - try - f() - false - with - | _ -> true - if not failed then - raise (new AssertFailedException("Method should fail")) - - -let equal expected = CustomMatchers.equal expected - -let equivalent expected = - CustomMatchers.equivalent (fun e a -> CollectionAssert.AreEquivalent(e, a)) expected - -let equalWithin (tolerance:obj) (expected:obj) = CustomMatchers.equalWithin tolerance expected - -let not' (expected:obj) = CustomMatchers.not' expected - -let throw (t:Type) = CustomMatchers.throw t - -let throwWithMessage (m:string) (t:Type) = CustomMatchers.throwWithMessage m t - -let be = CustomMatchers.be - -let Null = CustomMatchers.Null - -let Empty = CustomMatchers.Empty - -let EmptyString = CustomMatchers.EmptyString - -let NullOrEmptyString = CustomMatchers.NullOrEmptyString - -let True = CustomMatchers.True - -let False = CustomMatchers.False - -let NaN = CustomMatchers.NaN - -let unique = CustomMatchers.unique - -let sameAs expected = CustomMatchers.sameAs expected - -let greaterThan (expected:obj) = CustomMatchers.greaterThan expected - -let greaterThanOrEqualTo (expected:obj) = CustomMatchers.greaterThanOrEqualTo expected - -let lessThan (expected:obj) = CustomMatchers.lessThan expected - -let lessThanOrEqualTo (expected:obj) = CustomMatchers.lessThanOrEqualTo expected - -let endWith (expected:string) = CustomMatchers.endWith expected - -let startWith (expected:string) = CustomMatchers.startWith expected - -let haveSubstring (expected:string) = CustomMatchers.haveSubstring expected - -let ofExactType<'a> = CustomMatchers.ofExactType<'a> - -let instanceOfType<'a> = CustomMatchers.instanceOfType<'a> - -let contain expected = CustomMatchers.contain expected - -let haveLength n = CustomMatchers.haveLength n - -let haveCount n = CustomMatchers.haveCount n - -let matchList = CustomMatchers.matchList - -let choice = CustomMatchers.choice - -let ascending = CustomMatchers.ascending - -let descending = CustomMatchers.descending - -let inRange min max = CustomMatchers.inRange min max - + assertThat(y, c) + + +let inline shouldFail (f:unit->unit) = + let failed = + try + f() + false + with + | _ -> true + if not failed then + raise (new AssertFailedException("Method should fail")) + + +let equal expected = CustomMatchers.equal expected + +let equivalent expected = + CustomMatchers.equivalent (fun e a -> CollectionAssert.AreEquivalent(e, a)) expected + +let equalWithin (tolerance:obj) (expected:obj) = CustomMatchers.equalWithin tolerance expected + +let not' (expected:obj) = CustomMatchers.not' expected + +let throw (t:Type) = CustomMatchers.throw t + +let throwWithMessage (m:string) (t:Type) = CustomMatchers.throwWithMessage m t + +let be = CustomMatchers.be + +let Null = CustomMatchers.Null + +let Empty = CustomMatchers.Empty + +let EmptyString = CustomMatchers.EmptyString + +let NullOrEmptyString = CustomMatchers.NullOrEmptyString + +let True = CustomMatchers.True + +let False = CustomMatchers.False + +let NaN = CustomMatchers.NaN + +let unique = CustomMatchers.unique + +let sameAs expected = CustomMatchers.sameAs expected + +let greaterThan (expected:obj) = CustomMatchers.greaterThan expected + +let greaterThanOrEqualTo (expected:obj) = CustomMatchers.greaterThanOrEqualTo expected + +let lessThan (expected:obj) = CustomMatchers.lessThan expected + +let lessThanOrEqualTo (expected:obj) = CustomMatchers.lessThanOrEqualTo expected + +let endWith (expected:string) = CustomMatchers.endWith expected + +let startWith (expected:string) = CustomMatchers.startWith expected + +let haveSubstring (expected:string) = CustomMatchers.haveSubstring expected + +let ofExactType<'a> = CustomMatchers.ofExactType<'a> + +let instanceOfType<'a> = CustomMatchers.instanceOfType<'a> + +let contain expected = CustomMatchers.contain expected + +let haveLength n = CustomMatchers.haveLength n + +let haveCount n = CustomMatchers.haveCount n + +let matchList = CustomMatchers.matchList + +let choice = CustomMatchers.choice + +let ascending = CustomMatchers.ascending + +let descending = CustomMatchers.descending + +let inRange min max = CustomMatchers.inRange min max + let ofCase case = CustomMatchers.ofCase case let supersetOf expected = CustomMatchers.supersetOf expected diff --git a/src/FsUnit.NUnit/FsUnit.fs b/src/FsUnit.NUnit/FsUnit.fs index a0229f60..f99d960c 100644 --- a/src/FsUnit.NUnit/FsUnit.fs +++ b/src/FsUnit.NUnit/FsUnit.fs @@ -1,14 +1,10 @@ -// TODO : Add license header +namespace FsUnit -namespace FsUnit - -open System open System.Diagnostics open NUnit.Framework open NUnit.Framework.Constraints open CustomConstraints -// [] module TopLevelOperators = @@ -40,7 +36,7 @@ module TopLevelOperators = match y with | :? (unit -> unit) -> box (TestDelegate(y :?> unit -> unit)) | _ -> y - if box c = null then + if isNull (box c) then Assert.That(y, Is.Null) else Assert.That(y, c) @@ -90,8 +86,8 @@ module TopLevelOperators = let descending = Is.Ordered.Descending - let not' x = - if box x = null then NotConstraint(Null) else NotConstraint(x) + let not' x = + if isNull (box x) then NotConstraint(Null) else NotConstraint(x) let inRange min max = RangeConstraint(min, max) diff --git a/src/FsUnit.Xunit/FsUnit.fs b/src/FsUnit.Xunit/FsUnit.fs index d5f97c3a..beea1c28 100644 --- a/src/FsUnit.Xunit/FsUnit.fs +++ b/src/FsUnit.Xunit/FsUnit.fs @@ -10,13 +10,11 @@ type MatchException (expected, actual, userMessage) = inherit AssertActualExpectedException(expected, actual, userMessage) type Xunit.Assert with - static member That<'a> (actual, matcher:IMatcher<'a>) = + static member That<'a> (actual, matcher: IMatcher<'a>) = if not (matcher.Matches(actual)) then let description = new StringDescription() matcher.DescribeTo(description) - let mismatchDescription = new StringDescription() - matcher.DescribeMismatch(actual, mismatchDescription) - raise (new MatchException(description.ToString(), mismatchDescription.ToString(), null)) + raise (new MatchException(description.ToString(), (sprintf "%A" actual), null)) let inline should (f : 'a -> ^b) x (y : obj) = let c = f x @@ -24,7 +22,7 @@ let inline should (f : 'a -> ^b) x (y : obj) = match y with | :? (unit -> unit) as assertFunc -> box assertFunc | _ -> y - if box c = null then + if isNull (box c) then Assert.That(y, Is.Null()) else Assert.That(y, c) diff --git a/tests/FsUnit.MsTest.Test/beGreaterThanTests.fs b/tests/FsUnit.MsTest.Test/beGreaterThanTests.fs index 7e370c41..84d9851a 100644 --- a/tests/FsUnit.MsTest.Test/beGreaterThanTests.fs +++ b/tests/FsUnit.MsTest.Test/beGreaterThanTests.fs @@ -1,16 +1,16 @@ namespace FsUnit.Test open Microsoft.VisualStudio.TestTools.UnitTesting open FsUnit.MsTest -open NHamcrest.Core [] type ``be greaterThan tests`` ()= + [] member test. ``11 should be greater than 10`` ()= 11 |> should be (greaterThan 10) [] member test. - ``11.1 should be greater than 11.0`` ()= + ``11,1 should be greater than 11,0`` ()= 11.1 |> should be (greaterThan 11.0) [] member test. @@ -18,9 +18,16 @@ type ``be greaterThan tests`` ()= 9 |> should not' (be greaterThan 10) [] member test. - ``9.1 should not be greater than 9.2`` ()= + ``9,1 should not be greater than 9,2`` ()= 9.1 |> should not' (be greaterThan 9.2) [] member test. - ``9.2 should not be greater than 9.2`` ()= + ``9,2 should not be greater than 9,2`` ()= 9.2 |> should not' (be greaterThan 9.2) + + [] member test. + ``9,2 should not be greater than 9,2 but message should be equal`` ()= + (fun () -> 9.2 |> should be (greaterThan 9.2)) + |> fun f -> Assert.ThrowsException(f) + |> fun e -> e.Message + |> should equal ("Greater than 9.2 was 9.2") diff --git a/tests/FsUnit.MsTest.Test/beSubsetOfTests.fs b/tests/FsUnit.MsTest.Test/beSubsetOfTests.fs index b257ab11..8ab6dde2 100644 --- a/tests/FsUnit.MsTest.Test/beSubsetOfTests.fs +++ b/tests/FsUnit.MsTest.Test/beSubsetOfTests.fs @@ -18,7 +18,7 @@ type ``be subsetOf tests`` ()= {4..8} |> should be (subsetOf {1..10}) [] member test. - ``1 to 10 should be subset of 4. 1 and 7`` ()= + ``1 to 10 should be subset of 4, 1 and 7`` ()= [|1..10|] |> should be (supersetOf [|4;1;7|]) [] member test. @@ -37,3 +37,10 @@ type ``be subsetOf tests`` ()= ``should fail on '1 to 11 should be subset of 1 to 10'`` ()= shouldFail (fun () -> [1..11] |> should be (subsetOf [1..10])) + + [] member test. + ``11 should not be subset of 1 to 10 but message should be equal`` ()= + (fun () -> [11] |> should be (subsetOf [1..10])) + |> fun f -> Assert.ThrowsException(f) + |> fun e -> e.Message + |> should equal ("Is subset of [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] was [11]") diff --git a/tests/FsUnit.MsTest.Test/equalTests.fs b/tests/FsUnit.MsTest.Test/equalTests.fs index 37266d5c..c3e9bcbb 100644 --- a/tests/FsUnit.MsTest.Test/equalTests.fs +++ b/tests/FsUnit.MsTest.Test/equalTests.fs @@ -1,7 +1,6 @@ namespace FsUnit.Test open Microsoft.VisualStudio.TestTools.UnitTesting open FsUnit.MsTest -open NHamcrest.Core type AlwaysEqual() = override this.Equals(other) = true @@ -92,4 +91,11 @@ type ``equal Tests`` ()= [] member test. ``None should equal None`` ()= None |> should equal None + + [] member test. + ``Ok "foo" should fail on equal Ok "bar" but message should be equal`` ()= + (fun () -> Ok "foo" |> should equal (Ok "bar")) + |> fun f -> Assert.ThrowsException(f) + |> fun e -> e.Message + |> should equal ("Equals Ok \"bar\" was Ok \"foo\"") diff --git a/tests/FsUnit.MsTest.Test/haveLengthTests.fs b/tests/FsUnit.MsTest.Test/haveLengthTests.fs index 77494fd7..10345b8d 100644 --- a/tests/FsUnit.MsTest.Test/haveLengthTests.fs +++ b/tests/FsUnit.MsTest.Test/haveLengthTests.fs @@ -38,6 +38,13 @@ type ``haveLength tests`` ()= ``Array with 1 item should fail to not have Length 1`` ()= shouldFail (fun () -> [|1|] |> should not' (haveLength 1)) + [] member test. + ``Array with 1 item should fail to have Length 2 but message should be equal`` ()= + (fun () -> [|1|] |> should haveLength 2) + |> fun f -> Assert.ThrowsException(f) + |> fun e -> e.Message + |> should equal ("Have Length 2 was [|1|]") + // Seq [] member test. ``Seq with 1 item should fail to have Length 1`` ()= diff --git a/tests/FsUnit.MsTest.Test/shouldStartWithTests.fs b/tests/FsUnit.MsTest.Test/shouldStartWithTests.fs index f757ecd5..db333fe7 100644 --- a/tests/FsUnit.MsTest.Test/shouldStartWithTests.fs +++ b/tests/FsUnit.MsTest.Test/shouldStartWithTests.fs @@ -1,7 +1,6 @@ namespace FsUnit.Test open Microsoft.VisualStudio.TestTools.UnitTesting open FsUnit.MsTest -open NHamcrest.Core [] type ``should startWith tests`` ()= @@ -17,3 +16,10 @@ type ``should startWith tests`` ()= ``ships should not start with ss`` ()= "ships" |> should not' (startWith "ss") + [] member test. + ``ships should not start with ss but message should be equal`` ()= + (fun () -> "ships" |> should startWith "ss") + |> fun f -> Assert.ThrowsException(f) + |> fun e -> e.Message + |> should equal ("ss was \"ships\"") + diff --git a/tests/FsUnit.Xunit.Test/beGreaterThanTests.fs b/tests/FsUnit.Xunit.Test/beGreaterThanTests.fs index b4cf051a..1f812947 100644 --- a/tests/FsUnit.Xunit.Test/beGreaterThanTests.fs +++ b/tests/FsUnit.Xunit.Test/beGreaterThanTests.fs @@ -1,7 +1,6 @@ namespace FsUnit.Test open Xunit open FsUnit.Xunit -open NHamcrest.Core type ``be greaterThan tests`` ()= [] member test. @@ -9,7 +8,7 @@ type ``be greaterThan tests`` ()= 11 |> should be (greaterThan 10) [] member test. - ``11.1 should be greater than 11.0`` ()= + ``11,1 should be greater than 11,0`` ()= 11.1 |> should be (greaterThan 11.0) [] member test. @@ -17,9 +16,16 @@ type ``be greaterThan tests`` ()= 9 |> should not' (be greaterThan 10) [] member test. - ``9.1 should not be greater than 9.2`` ()= + ``9,1 should not be greater than 9,2`` ()= 9.1 |> should not' (be greaterThan 9.2) [] member test. - ``9.2 should not be greater than 9.2`` ()= + ``9,2 should not be greater than 9,2`` ()= 9.2 |> should not' (be greaterThan 9.2) + + [] member test. + ``10 should not be greater than 10 but messages should equal`` ()= + (fun () -> 10 |> should be (greaterThan 10)) + |> fun f -> Assert.Throws(f) + |> fun e -> (e.Expected, e.Actual) + |> should equal ("Greater than 10", "10") diff --git a/tests/FsUnit.Xunit.Test/beSubsetOfTests.fs b/tests/FsUnit.Xunit.Test/beSubsetOfTests.fs index 4ab6a7e2..eeef20c7 100644 --- a/tests/FsUnit.Xunit.Test/beSubsetOfTests.fs +++ b/tests/FsUnit.Xunit.Test/beSubsetOfTests.fs @@ -36,3 +36,10 @@ type ``be subsetOf tests`` ()= ``should fail on '1 to 11 should be subset of 1 to 10'`` ()= shouldFail (fun () -> [1..11] |> should be (subsetOf [1..10])) + + [] member test. + ``11 should not be subset of 1 to 10 but messages should equal`` ()= + (fun _ -> [11] |> should be (subsetOf [1..10])) + |> fun f -> Assert.Throws(f) + |> fun e -> (e.Expected, e.Actual) + |> should equal ("Is subset of [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]", "[11]") diff --git a/tests/FsUnit.Xunit.Test/equalTests.fs b/tests/FsUnit.Xunit.Test/equalTests.fs index 340346e9..cb4f3e60 100644 --- a/tests/FsUnit.Xunit.Test/equalTests.fs +++ b/tests/FsUnit.Xunit.Test/equalTests.fs @@ -82,3 +82,10 @@ type ``equal Tests`` ()= [] member test. ``None should equal None`` ()= None |> should equal None + + [] member test. + ``Ok "foo" should fail on equal Ok "bar" but message should be equal`` ()= + (fun () -> Ok "foo" |> should equal (Ok "bar")) + |> fun f -> Assert.Throws(f) + |> fun e -> (e.Expected, e.Actual) + |> should equal ("Equals Ok \"bar\"", "Ok \"foo\"") \ No newline at end of file diff --git a/tests/FsUnit.Xunit.Test/haveLengthTests.fs b/tests/FsUnit.Xunit.Test/haveLengthTests.fs index f9094f47..0090ff90 100644 --- a/tests/FsUnit.Xunit.Test/haveLengthTests.fs +++ b/tests/FsUnit.Xunit.Test/haveLengthTests.fs @@ -37,6 +37,13 @@ type ``haveLength tests`` ()= ``Array with 1 item should fail to not have Length 1`` ()= shouldFail (fun () -> [|1|] |> should not' (haveLength 1)) + [] member test. + ``Array with 1 item should fail to have Length 2 but messages should equal`` ()= + (fun () -> [|1|] |> should haveLength 2) + |> fun f -> Assert.Throws(f) + |> fun e -> (e.Expected, e.Actual) + |> should equal ("Have Length 2", "[|1|]") + // Seq [] member test. ``Seq with 1 item should fail to have Length 1`` ()= diff --git a/tests/FsUnit.Xunit.Test/shouldStartWithTests.fs b/tests/FsUnit.Xunit.Test/shouldStartWithTests.fs index c53ce3cb..0bdbd26c 100644 --- a/tests/FsUnit.Xunit.Test/shouldStartWithTests.fs +++ b/tests/FsUnit.Xunit.Test/shouldStartWithTests.fs @@ -16,3 +16,9 @@ type ``should startWith tests`` ()= ``ships should not start with ss`` ()= "ships" |> should not' (startWith "ss") + [] member test. + ``ships should not start with ss but messages should equal`` ()= + (fun _ -> "ships" |> should startWith "ss") + |> fun f -> Assert.Throws(f) + |> fun e -> (e.Expected, e.Actual) + |> should equal ("ss", "\"ships\"")