From b117a797abfab089ee6a33ab3062e022c6196c6c Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Thu, 31 Mar 2022 21:53:46 +0100 Subject: [PATCH 01/23] Add typed aliases around Xunit --- src/FsUnit.Xunit/FsUnitTyped.fs | 60 +++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/FsUnit.Xunit/FsUnitTyped.fs diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs new file mode 100644 index 00000000..09f07929 --- /dev/null +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -0,0 +1,60 @@ +namespace FsUnitTyped + +open System.Diagnostics +open Xunit +open System.Collections.Generic + +[] +module TopLevelOperators = + + [] + let shouldEqual (expected: 'a) (actual: 'a) = + Assert.That(actual, FsUnit.Equality.IsEqualTo(expected)) + + [] + let shouldNotEqual (expected: 'a) (actual: 'a) = + Assert.That(actual, FsUnit.Equality.IsNotEqualTo(expected)) + + [] + let shouldContain (x: 'a) (y: 'a seq) = + let list = List<_>() + for a in y do + list.Add a + Assert.Contains(x, list) + + [] + let shouldBeEmpty(list: 'a seq) = + Assert.IsEmpty(list) + + [] + let shouldNotContain (x: 'a) (y: 'a seq) = + if Seq.exists ((=) x) y + then failwith $"Seq %A{y} should not contain %A{x}" + + [] + let shouldBeSmallerThan (x: 'a) (y: 'a) = + Assert.Less(y, x, $"Expected: %A{x}\nActual: %A{y}") + + [] + let shouldBeGreaterThan (x: 'a) (y: 'a) = + Assert.Greater(y, x, $"Expected: %A{x}\nActual: %A{y}") + + [] + let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = + Assert.Throws(Is.InstanceOf<'exn>(), TestDelegate(f)) |> ignore + + [] + let shouldContainText (x: string) (y: string) = + if y.Contains(x) |> not + then failwith $"\"{x}\" is not a substring of \"{y}\"" + + [] + let shouldNotContainText (x: string) (y: string) = + if y.Contains(x) + then failwith $"\"{x}\" is a substring of \"{y}\"" + + [] + let shouldHaveLength expected list = + let actual = Seq.length list + if actual <> expected + then failwith $"Invalid length in %A{list}\r\nExpected: {expected}\r\nActual: {actual}" From bd20cb8c996742f8ae5afebd86eb700dd306c282 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Thu, 31 Mar 2022 22:01:37 +0100 Subject: [PATCH 02/23] Add typed tests --- tests/FsUnit.Xunit.Test/typed.beEmptyTests.fs | 29 ++++ .../typed.haveLengthTests.fs | 23 +++ .../typed.shouldBeGreaterThanTests.fs | 13 ++ .../typed.shouldBeSmallerThanTests.fs | 23 +++ .../typed.shouldContainTests.fs | 53 +++++++ .../typed.shouldContainText.fs | 13 ++ .../typed.shouldEqualNullTests.fs | 13 ++ .../typed.shouldEqualTests.fs | 133 ++++++++++++++++++ .../typed.shouldFailTests.fs | 21 +++ 9 files changed, 321 insertions(+) create mode 100644 tests/FsUnit.Xunit.Test/typed.beEmptyTests.fs create mode 100644 tests/FsUnit.Xunit.Test/typed.haveLengthTests.fs create mode 100644 tests/FsUnit.Xunit.Test/typed.shouldBeGreaterThanTests.fs create mode 100644 tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs create mode 100644 tests/FsUnit.Xunit.Test/typed.shouldContainTests.fs create mode 100644 tests/FsUnit.Xunit.Test/typed.shouldContainText.fs create mode 100644 tests/FsUnit.Xunit.Test/typed.shouldEqualNullTests.fs create mode 100644 tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs create mode 100644 tests/FsUnit.Xunit.Test/typed.shouldFailTests.fs diff --git a/tests/FsUnit.Xunit.Test/typed.beEmptyTests.fs b/tests/FsUnit.Xunit.Test/typed.beEmptyTests.fs new file mode 100644 index 00000000..3ea80fa4 --- /dev/null +++ b/tests/FsUnit.Xunit.Test/typed.beEmptyTests.fs @@ -0,0 +1,29 @@ +namespace FsUnit.Typed.Test + +open Xunit +open FsUnitTyped + +type ``shouldBeEmpty tests``() = + [] + member __.``empty List should be Empty``() = + [] |> shouldBeEmpty + + [] + member __.``non-empty List should fail to be Empty``() = + shouldFail(fun () -> [ 1 ] |> shouldBeEmpty) + + [] + member __.``empty Array should be Empty``() = + [||] |> shouldBeEmpty + + [] + member __.``non-empty Array should fail to be Empty``() = + shouldFail(fun () -> [| 1 |] |> shouldBeEmpty) + + [] + member __.``empty Seq should be Empty``() = + Seq.empty |> shouldBeEmpty + + [] + member __.``non-empty Seq should fail to be Empty``() = + shouldFail(fun () -> seq { 1 } |> shouldBeEmpty) diff --git a/tests/FsUnit.Xunit.Test/typed.haveLengthTests.fs b/tests/FsUnit.Xunit.Test/typed.haveLengthTests.fs new file mode 100644 index 00000000..1e8228b2 --- /dev/null +++ b/tests/FsUnit.Xunit.Test/typed.haveLengthTests.fs @@ -0,0 +1,23 @@ +namespace FsUnit.Typed.Test + +open Xunit +open FsUnitTyped + +type ``haveLength tests``() = + // F# List + [] + member __.``List with 1 item should have Length 1``() = + [ 1 ] |> shouldHaveLength 1 + + [] + member __.``empty List should fail to have Length 1``() = + shouldFail(fun () -> [] |> shouldHaveLength 1) + + // Array + [] + member __.``Array with 1 item should have Length 1``() = + [| 1 |] |> shouldHaveLength 1 + + [] + member __.``empty Array should fail to have Length 1``() = + shouldFail(fun () -> [||] |> shouldHaveLength 1) diff --git a/tests/FsUnit.Xunit.Test/typed.shouldBeGreaterThanTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldBeGreaterThanTests.fs new file mode 100644 index 00000000..7f1a742a --- /dev/null +++ b/tests/FsUnit.Xunit.Test/typed.shouldBeGreaterThanTests.fs @@ -0,0 +1,13 @@ +namespace FsUnit.Typed.Test + +open Xunit +open FsUnitTyped + +type ``shouldBeGreaterThan tests``() = + [] + member __.``11 should be greater than 10``() = + 11 |> shouldBeGreaterThan 10 + + [] + member __.``11[dot]1 should be greater than 11[dot]0``() = + 11.1 |> shouldBeGreaterThan 11.0 diff --git a/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs new file mode 100644 index 00000000..fc11b696 --- /dev/null +++ b/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs @@ -0,0 +1,23 @@ +namespace FsUnit.Typed.Test + +open Xunit +open FsUnitTyped + +type ``shouldBeSmallerThan tests``() = + [] + member __.``10 should be less than 11``() = + 10 |> shouldBeSmallerThan 11 + + [] + member __.``10 should not be less than 10``() = + (fun () -> 10 |> shouldBeSmallerThan 10) + |> shouldFail + + [] + member __.``10[dot]0 should be less than 10[dot]1``() = + 10.0 |> shouldBeSmallerThan 10.1 + + [] + member __.``10[dot]0 should not be less than 10[dot]0``() = + (fun () -> 10.0 |> shouldBeSmallerThan 10.0) + |> shouldFail diff --git a/tests/FsUnit.Xunit.Test/typed.shouldContainTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldContainTests.fs new file mode 100644 index 00000000..baa4b442 --- /dev/null +++ b/tests/FsUnit.Xunit.Test/typed.shouldContainTests.fs @@ -0,0 +1,53 @@ +namespace FsUnit.Typed.Test + +open Xunit +open FsUnitTyped + +type ``shouldContain tests``() = + [] + member __.``List with item should contain item``() = + [ 1 ] |> shouldContain 1 + + [] + member __.``empty List should fail to contain item``() = + shouldFail(fun () -> [] |> shouldContain 1) + + [] + member __.``empty List should not contain item``() = + [] |> shouldNotContain 1 + + [] + member __.``List with item should fail to not contain item``() = + shouldFail(fun () -> [ 1 ] |> shouldNotContain 1) + + [] + member __.``Array with item should contain item``() = + [| 1 |] |> shouldContain 1 + + [] + member __.``empty Array should fail to contain item``() = + shouldFail(fun () -> [||] |> shouldContain 1) + + [] + member __.``empty Array should not contain item``() = + [||] |> shouldNotContain 1 + + [] + member __.``Array with item should fail to not contain item``() = + shouldFail(fun () -> [| 1 |] |> shouldNotContain 1) + + [] + member __.``Seq with item should contain item``() = + seq { 1 } |> shouldContain 1 + + [] + member __.``empty Seq should fail to contain item``() = + shouldFail(fun () -> Seq.empty |> shouldContain 1) + + [] + member __.``empty Seq should not contain item``() = + Seq.empty |> shouldNotContain 1 + + [] + member __.``Seq with item should fail to not contain item``() = + shouldFail(fun () -> seq { 1 } |> shouldNotContain 1) diff --git a/tests/FsUnit.Xunit.Test/typed.shouldContainText.fs b/tests/FsUnit.Xunit.Test/typed.shouldContainText.fs new file mode 100644 index 00000000..5e377602 --- /dev/null +++ b/tests/FsUnit.Xunit.Test/typed.shouldContainText.fs @@ -0,0 +1,13 @@ +namespace FsUnit.Typed.Test + +open Xunit +open FsUnitTyped + +type ``shouldContainText tests``() = + [] + member __.``empty string should contain ""``() = + "" |> shouldContainText "" + + [] + member __.``ships should contain hip``() = + "ships" |> shouldContainText "hip" diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualNullTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualNullTests.fs new file mode 100644 index 00000000..4c294380 --- /dev/null +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualNullTests.fs @@ -0,0 +1,13 @@ +namespace FsUnit.Typed.Test + +open Xunit +open FsUnitTyped + +type ``Typed: shouldEqual null tests``() = + [] + member __.``null should be null``() = + null |> shouldEqual null + + [] + member __.``null should fail to not be null``() = + shouldFail(fun () -> null |> shouldNotEqual null) diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs new file mode 100644 index 00000000..2f7e866e --- /dev/null +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs @@ -0,0 +1,133 @@ +namespace FsUnit.Typed.Test + +open System.Collections.Immutable + +open Xunit +open FsUnitTyped +open FsUnit +open System + +type AlwaysEqual() = + override __.Equals(other) = true + override __.GetHashCode() = 1 + +type NeverEqual() = + override __.Equals(other) = false + override __.GetHashCode() = 1 + +type ``shouldEqual Tests``() = + let anObj = new obj() + let otherObj = new obj() + let anImmutableArray = ImmutableArray.Create(1, 2, 3) + let equivalentImmutableArray = ImmutableArray.Create(1, 2, 3) + let otherImmutableArray = ImmutableArray.Create(1, 2, 4) + + [] + member __.``value type should equal equivalent value``() = + 1 |> shouldEqual 1 + + [] + member __.``value type should fail to equal nonequivalent value``() = + shouldFail(fun () -> 1 |> shouldEqual 2) + + [] + member __.``value type should not equal nonequivalent value``() = + 1 |> shouldNotEqual 2 + + [] + member __.``value type should fail to not equal equivalent value``() = + shouldFail(fun () -> 1 |> shouldNotEqual 1) + + [] + member __.``reference type should equal itself``() = + anObj |> shouldEqual anObj + + [] + member __.``reference type should fail to equal other``() = + shouldFail(fun () -> anObj |> shouldEqual otherObj) + + [] + member __.``reference type should not equal other``() = + anObj |> shouldNotEqual otherObj + + [] + member __.``reference type should fail to not equal itself``() = + shouldFail(fun () -> anObj |> shouldNotEqual anObj) + + [] + member __.``should pass when Equals returns true``() = + anObj |> shouldEqual(box(new AlwaysEqual())) + + [] + member __.``should fail when Equals returns false``() = + shouldFail(fun () -> anObj |> shouldEqual(box(new NeverEqual()))) + + [] + member __.``should pass when negated and Equals returns false``() = + anObj |> shouldNotEqual(box(new NeverEqual())) + + [] + member __.``should fail when negated and Equals returns true``() = + shouldFail(fun () -> anObj |> shouldNotEqual(box(new AlwaysEqual()))) + + [] + member __.``None should equal None``() = + None |> shouldEqual None + + [] + member __.``Error "Foo" should equal Error "Foo"``() = + Error "Foo" |> shouldEqual(Error "Foo") + + [] + member __.``Error "Foo" should equal fails and have same message``() = + (fun () -> Error "Foo" |> shouldEqual(Error "Bar")) + |> Assert.Throws + |> fun e -> + e.Message + |> should + equal + (sprintf " Expected: Error \"Bar\" or Error \"Bar\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) + + [] + member __.``Error "Foo" should not equal Error "Bar"``() = + Error "Foo" |> shouldNotEqual(Error "Bar") + + [] + member __.``Error "Foo" should not equal Error "Bar" fails and have same message``() = + (fun () -> Error "Foo" |> shouldNotEqual(Error "Foo")) + |> Assert.Throws + |> fun e -> + e.Message + |> should + equal + (sprintf " Expected: not Error \"Foo\" or Error \"Foo\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) + + [] + member this.``structural equality``() = + let actualList: char list = [] + [ (actualList, "") ] |> shouldEqual [ ([], "") ] + + [] + member __.``Empty obj list should match itself``() = + [] |> shouldEqual [] + + [] + member __.``List with elements should not match empty list``() = + [ 1 ] |> shouldNotEqual [] + + [] + member __.``structural value type should equal equivalent value``() = + anImmutableArray |> shouldEqual equivalentImmutableArray + + [] + member __.``structural value type should not equal non-equivalent value``() = + anImmutableArray |> shouldNotEqual otherImmutableArray + + [] + member __.``structural comparable type containing non-equivalent structural equatable type fails with correct exception``() = + let array1 = + ImmutableArray.Create(Uri("https://example.com/1")) + + let array2 = + ImmutableArray.Create(Uri("https://example.com/2")) + diff --git a/tests/FsUnit.Xunit.Test/typed.shouldFailTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldFailTests.fs new file mode 100644 index 00000000..7bc9636a --- /dev/null +++ b/tests/FsUnit.Xunit.Test/typed.shouldFailTests.fs @@ -0,0 +1,21 @@ +namespace FsUnit.Typed.Test + +open Xunit +open FsUnitTyped + +type ``shouldFail tests``() = + [] + member __.``empty List should fail to contain item``() = + shouldFail(fun () -> [] |> shouldContain 1) + + [] + member __.``non-null should fail to be Null``() = + shouldFail(fun () -> "something" |> shouldEqual null) + + [] + member __.``shouldFail should fail when everything is OK``() = + shouldFail(fun () -> shouldFail id) + + [] + member __.``Simplify "should throw"``() = + (fun () -> failwith "BOOM!") |> shouldFail From 56dea9bd41a7deb101229fcdf6dd8075c9bec5c3 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Thu, 31 Mar 2022 22:09:37 +0100 Subject: [PATCH 03/23] Add missing line --- tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs index 2f7e866e..f1854918 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs @@ -131,3 +131,4 @@ type ``shouldEqual Tests``() = let array2 = ImmutableArray.Create(Uri("https://example.com/2")) + shouldFail(fun () -> array1 |> shouldEqual array2) From d6c22ff5a188756573eddcc4b2d9ede1302d88c6 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Thu, 31 Mar 2022 22:15:25 +0100 Subject: [PATCH 04/23] Update doc --- docs/FsUnitTyped.fsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/FsUnitTyped.fsx b/docs/FsUnitTyped.fsx index fa8694fa..11f035e7 100644 --- a/docs/FsUnitTyped.fsx +++ b/docs/FsUnitTyped.fsx @@ -9,9 +9,9 @@ What is FsUnitTyped? =============== **FsUnitTyped** is a statically typed set of FsUnit operators that makes -unit-testing with `FsUnit` even more safe and enjoyable (Available only for `NUnit`). +unit-testing with `FsUnit` even more safe and enjoyable (Available only for `NUnit` and `Xunit`). -No more untyped constrains and tests like +No more untyped constraints and tests like 1 |> should equal "1" From d57e8f5b76706ed11f33f3a438392cfb5dceea94 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 20:04:12 +0100 Subject: [PATCH 05/23] Include in fsproj files --- src/FsUnit.Xunit/FsUnit.Xunit.fsproj | 3 ++- tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnit.Xunit.fsproj b/src/FsUnit.Xunit/FsUnit.Xunit.fsproj index ccc25ccf..9ad97a92 100644 --- a/src/FsUnit.Xunit/FsUnit.Xunit.fsproj +++ b/src/FsUnit.Xunit/FsUnit.Xunit.fsproj @@ -14,6 +14,7 @@ + @@ -22,4 +23,4 @@ - \ No newline at end of file + diff --git a/tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj b/tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj index 50895d0b..e4579bb1 100644 --- a/tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj +++ b/tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj @@ -36,10 +36,19 @@ + + + + + + + + + - \ No newline at end of file + From 908c96413e44370082a073c50903d0acdf00d577 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 20:12:27 +0100 Subject: [PATCH 06/23] Trying to get it to compile - GitHub please tell me if this worked --- src/FsUnit.Xunit/FsUnitTyped.fs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index 09f07929..85d5b8fc 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -3,6 +3,7 @@ namespace FsUnitTyped open System.Diagnostics open Xunit open System.Collections.Generic +open FsUnit.Xunit [] module TopLevelOperators = @@ -24,7 +25,7 @@ module TopLevelOperators = [] let shouldBeEmpty(list: 'a seq) = - Assert.IsEmpty(list) + Assert.That(list, Empty) [] let shouldNotContain (x: 'a) (y: 'a seq) = @@ -33,15 +34,17 @@ module TopLevelOperators = [] let shouldBeSmallerThan (x: 'a) (y: 'a) = - Assert.Less(y, x, $"Expected: %A{x}\nActual: %A{y}") + if not (x < y) then + failwith $"Expected:\n %A{x}\nto be smaller than:\n %A{y}") [] let shouldBeGreaterThan (x: 'a) (y: 'a) = - Assert.Greater(y, x, $"Expected: %A{x}\nActual: %A{y}") + if not (x > y) then + failwith $"Expected:\n %A{x}\nto be greater than:\n %A{y}") [] let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = - Assert.Throws(Is.InstanceOf<'exn>(), TestDelegate(f)) |> ignore + f |> should throw [] let shouldContainText (x: string) (y: string) = From d81e8c96b91fa514a72424b2e5372e02bac51523 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 20:14:39 +0100 Subject: [PATCH 07/23] Fix syntax error --- src/FsUnit.Xunit/FsUnitTyped.fs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index 85d5b8fc..cbb745bb 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -35,12 +35,12 @@ module TopLevelOperators = [] let shouldBeSmallerThan (x: 'a) (y: 'a) = if not (x < y) then - failwith $"Expected:\n %A{x}\nto be smaller than:\n %A{y}") + failwith $"Expected:\n %A{x}\nto be smaller than:\n %A{y}" [] let shouldBeGreaterThan (x: 'a) (y: 'a) = if not (x > y) then - failwith $"Expected:\n %A{x}\nto be greater than:\n %A{y}") + failwith $"Expected:\n %A{x}\nto be greater than:\n %A{y}" [] let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = From 78ed16330662fab78f5ea149348a5bb1e3f02ade Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 20:24:31 +0100 Subject: [PATCH 08/23] Fix more errors --- src/FsUnit.Xunit/FsUnitTyped.fs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index cbb745bb..e034787e 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -10,11 +10,11 @@ module TopLevelOperators = [] let shouldEqual (expected: 'a) (actual: 'a) = - Assert.That(actual, FsUnit.Equality.IsEqualTo(expected)) + expected |> should equal actual [] let shouldNotEqual (expected: 'a) (actual: 'a) = - Assert.That(actual, FsUnit.Equality.IsNotEqualTo(expected)) + expected |> should not' (equal actual) [] let shouldContain (x: 'a) (y: 'a seq) = @@ -25,7 +25,7 @@ module TopLevelOperators = [] let shouldBeEmpty(list: 'a seq) = - Assert.That(list, Empty) + list |> should be Empty [] let shouldNotContain (x: 'a) (y: 'a seq) = From 0b8296b4030858a31ac61641baed1de82233e2be Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 20:27:03 +0100 Subject: [PATCH 09/23] Fix contains error --- src/FsUnit.Xunit/FsUnitTyped.fs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index e034787e..ad646a12 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -18,10 +18,7 @@ module TopLevelOperators = [] let shouldContain (x: 'a) (y: 'a seq) = - let list = List<_>() - for a in y do - list.Add a - Assert.Contains(x, list) + y |> should contain expected [] let shouldBeEmpty(list: 'a seq) = From 12db71f14b863e9337f4b6597197957596136d74 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 20:30:03 +0100 Subject: [PATCH 10/23] Fix more errors --- src/FsUnit.Xunit/FsUnitTyped.fs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index ad646a12..6ba95b11 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -18,7 +18,7 @@ module TopLevelOperators = [] let shouldContain (x: 'a) (y: 'a seq) = - y |> should contain expected + y |> should contain x [] let shouldBeEmpty(list: 'a seq) = @@ -41,7 +41,7 @@ module TopLevelOperators = [] let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = - f |> should throw + f |> should (throw typeof<'exn>) [] let shouldContainText (x: string) (y: string) = From e1811a80803f6efaa81a85370134170948397f1f Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 20:34:03 +0100 Subject: [PATCH 11/23] Fix brackets --- src/FsUnit.Xunit/FsUnitTyped.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index 6ba95b11..c1861241 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -41,7 +41,7 @@ module TopLevelOperators = [] let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = - f |> should (throw typeof<'exn>) + f |> should throw typeof<'exn> [] let shouldContainText (x: string) (y: string) = From 2af5ee92660b5920592132bbbc9a8fe13decfb12 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 20:41:08 +0100 Subject: [PATCH 12/23] Pipeline, please tell me what exception gets thrown --- .../typed.shouldBeSmallerThanTests.fs | 4 ++-- tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs | 12 +++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs index fc11b696..a45ccf63 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs @@ -11,7 +11,7 @@ type ``shouldBeSmallerThan tests``() = [] member __.``10 should not be less than 10``() = (fun () -> 10 |> shouldBeSmallerThan 10) - |> shouldFail + |> shouldFail [] member __.``10[dot]0 should be less than 10[dot]1``() = @@ -20,4 +20,4 @@ type ``shouldBeSmallerThan tests``() = [] member __.``10[dot]0 should not be less than 10[dot]0``() = (fun () -> 10.0 |> shouldBeSmallerThan 10.0) - |> shouldFail + |> shouldFail diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs index f1854918..0ffee969 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs @@ -81,11 +81,10 @@ type ``shouldEqual Tests``() = [] member __.``Error "Foo" should equal fails and have same message``() = (fun () -> Error "Foo" |> shouldEqual(Error "Bar")) - |> Assert.Throws + |> Assert.Throws |> fun e -> e.Message - |> should - equal + |> shouldEqual (sprintf " Expected: Error \"Bar\" or Error \"Bar\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) [] @@ -95,12 +94,11 @@ type ``shouldEqual Tests``() = [] member __.``Error "Foo" should not equal Error "Bar" fails and have same message``() = (fun () -> Error "Foo" |> shouldNotEqual(Error "Foo")) - |> Assert.Throws + |> Assert.Throws |> fun e -> e.Message - |> should - equal - (sprintf " Expected: not Error \"Foo\" or Error \"Foo\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) + |> shouldEqual + (sprintf " Expected: not Error \"Foo\" or Error \"Foo\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) [] member this.``structural equality``() = From d22388b726c3e07da363790e6830ee5052a398ae Mon Sep 17 00:00:00 2001 From: Sergey Tihon Date: Sun, 3 Apr 2022 22:33:42 +0200 Subject: [PATCH 13/23] feat: fantomas format --- src/FsUnit.Xunit/FsUnitTyped.fs | 21 ++++++++++--------- .../typed.shouldEqualTests.fs | 14 ++++++------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index c1861241..56f87ab6 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -26,17 +26,17 @@ module TopLevelOperators = [] let shouldNotContain (x: 'a) (y: 'a seq) = - if Seq.exists ((=) x) y - then failwith $"Seq %A{y} should not contain %A{x}" + if Seq.exists ((=) x) y then + failwith $"Seq %A{y} should not contain %A{x}" [] let shouldBeSmallerThan (x: 'a) (y: 'a) = - if not (x < y) then + if not(x < y) then failwith $"Expected:\n %A{x}\nto be smaller than:\n %A{y}" [] let shouldBeGreaterThan (x: 'a) (y: 'a) = - if not (x > y) then + if not(x > y) then failwith $"Expected:\n %A{x}\nto be greater than:\n %A{y}" [] @@ -45,16 +45,17 @@ module TopLevelOperators = [] let shouldContainText (x: string) (y: string) = - if y.Contains(x) |> not - then failwith $"\"{x}\" is not a substring of \"{y}\"" + if y.Contains(x) |> not then + failwith $"\"{x}\" is not a substring of \"{y}\"" [] let shouldNotContainText (x: string) (y: string) = - if y.Contains(x) - then failwith $"\"{x}\" is a substring of \"{y}\"" + if y.Contains(x) then + failwith $"\"{x}\" is a substring of \"{y}\"" [] let shouldHaveLength expected list = let actual = Seq.length list - if actual <> expected - then failwith $"Invalid length in %A{list}\r\nExpected: {expected}\r\nActual: {actual}" + + if actual <> expected then + failwith $"Invalid length in %A{list}\r\nExpected: {expected}\r\nActual: {actual}" diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs index 0ffee969..6d474d42 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs @@ -84,8 +84,7 @@ type ``shouldEqual Tests``() = |> Assert.Throws |> fun e -> e.Message - |> shouldEqual - (sprintf " Expected: Error \"Bar\" or Error \"Bar\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) + |> shouldEqual(sprintf " Expected: Error \"Bar\" or Error \"Bar\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) [] member __.``Error "Foo" should not equal Error "Bar"``() = @@ -97,8 +96,9 @@ type ``shouldEqual Tests``() = |> Assert.Throws |> fun e -> e.Message - |> shouldEqual - (sprintf " Expected: not Error \"Foo\" or Error \"Foo\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) + |> shouldEqual( + sprintf " Expected: not Error \"Foo\" or Error \"Foo\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine + ) [] member this.``structural equality``() = @@ -123,10 +123,8 @@ type ``shouldEqual Tests``() = [] member __.``structural comparable type containing non-equivalent structural equatable type fails with correct exception``() = - let array1 = - ImmutableArray.Create(Uri("https://example.com/1")) + let array1 = ImmutableArray.Create(Uri("https://example.com/1")) - let array2 = - ImmutableArray.Create(Uri("https://example.com/2")) + let array2 = ImmutableArray.Create(Uri("https://example.com/2")) shouldFail(fun () -> array1 |> shouldEqual array2) From bffbe34a9ce24ee9a58a9157cd29e9c7a75bb94b Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 22:36:02 +0100 Subject: [PATCH 14/23] Fix all but a couple of equality tests --- src/FsUnit.Xunit/FsUnitTyped.fs | 28 ++++++++----------- tests/FsUnit.Xunit.Test/shouldFailTests.fs | 2 +- .../typed.shouldBeSmallerThanTests.fs | 6 ++-- .../typed.shouldEqualTests.fs | 7 +++-- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index 56f87ab6..86a21313 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -1,47 +1,43 @@ namespace FsUnitTyped open System.Diagnostics -open Xunit -open System.Collections.Generic open FsUnit.Xunit [] module TopLevelOperators = [] - let shouldEqual (expected: 'a) (actual: 'a) = - expected |> should equal actual + let shouldEqual<'a> (expected: 'a) (actual: 'a) = + should equal expected actual [] - let shouldNotEqual (expected: 'a) (actual: 'a) = + let shouldNotEqual<'a> (expected: 'a) (actual: 'a) = expected |> should not' (equal actual) [] - let shouldContain (x: 'a) (y: 'a seq) = + let shouldContain<'a when 'a : equality> (x: 'a) (y: 'a seq) = y |> should contain x [] - let shouldBeEmpty(list: 'a seq) = + let shouldBeEmpty<'a> (list: 'a seq) = list |> should be Empty [] - let shouldNotContain (x: 'a) (y: 'a seq) = + let shouldNotContain<'a when 'a : equality> (x: 'a) (y: 'a seq) = if Seq.exists ((=) x) y then failwith $"Seq %A{y} should not contain %A{x}" [] - let shouldBeSmallerThan (x: 'a) (y: 'a) = - if not(x < y) then - failwith $"Expected:\n %A{x}\nto be smaller than:\n %A{y}" + let shouldBeSmallerThan<'a when 'a : comparison> (x: 'a) (y: 'a) = + should be (lessThan x) y [] - let shouldBeGreaterThan (x: 'a) (y: 'a) = - if not(x > y) then - failwith $"Expected:\n %A{x}\nto be greater than:\n %A{y}" + let shouldBeGreaterThan<'a when 'a : comparison> (x: 'a) (y: 'a) = + should be (greaterThan x) y [] let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = - f |> should throw typeof<'exn> + should throw typeof<'exn> f [] let shouldContainText (x: string) (y: string) = @@ -54,7 +50,7 @@ module TopLevelOperators = failwith $"\"{x}\" is a substring of \"{y}\"" [] - let shouldHaveLength expected list = + let shouldHaveLength<'a> (expected: int) (list: 'a seq) = let actual = Seq.length list if actual <> expected then diff --git a/tests/FsUnit.Xunit.Test/shouldFailTests.fs b/tests/FsUnit.Xunit.Test/shouldFailTests.fs index c3d769d2..5dca8338 100644 --- a/tests/FsUnit.Xunit.Test/shouldFailTests.fs +++ b/tests/FsUnit.Xunit.Test/shouldFailTests.fs @@ -17,7 +17,7 @@ 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 [] diff --git a/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs index a45ccf63..896485ec 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs @@ -1,5 +1,6 @@ namespace FsUnit.Typed.Test +open FsUnit.Xunit open Xunit open FsUnitTyped @@ -11,7 +12,7 @@ type ``shouldBeSmallerThan tests``() = [] member __.``10 should not be less than 10``() = (fun () -> 10 |> shouldBeSmallerThan 10) - |> shouldFail + |> shouldFail [] member __.``10[dot]0 should be less than 10[dot]1``() = @@ -20,4 +21,5 @@ type ``shouldBeSmallerThan tests``() = [] member __.``10[dot]0 should not be less than 10[dot]0``() = (fun () -> 10.0 |> shouldBeSmallerThan 10.0) - |> shouldFail + |> should throw typeof + //|> shouldFail diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs index 6d474d42..0c9b3d25 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs @@ -2,6 +2,7 @@ namespace FsUnit.Typed.Test open System.Collections.Immutable +open FsUnit.Xunit open Xunit open FsUnitTyped open FsUnit @@ -81,7 +82,7 @@ type ``shouldEqual Tests``() = [] member __.``Error "Foo" should equal fails and have same message``() = (fun () -> Error "Foo" |> shouldEqual(Error "Bar")) - |> Assert.Throws + |> Assert.Throws |> fun e -> e.Message |> shouldEqual(sprintf " Expected: Error \"Bar\" or Error \"Bar\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) @@ -93,11 +94,11 @@ type ``shouldEqual Tests``() = [] member __.``Error "Foo" should not equal Error "Bar" fails and have same message``() = (fun () -> Error "Foo" |> shouldNotEqual(Error "Foo")) - |> Assert.Throws + |> Assert.Throws |> fun e -> e.Message |> shouldEqual( - sprintf " Expected: not Error \"Foo\" or Error \"Foo\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine + sprintf " Expected: not Equals Error \"Foo\"%sActual: Error \"Foo\"%s" Environment.NewLine Environment.NewLine ) [] From 78456677422765a35dcba5299a26ef1592fbbff6 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 22:36:32 +0100 Subject: [PATCH 15/23] Format --- tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs | 5 ++--- tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs index 896485ec..53c55641 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs @@ -11,8 +11,7 @@ type ``shouldBeSmallerThan tests``() = [] member __.``10 should not be less than 10``() = - (fun () -> 10 |> shouldBeSmallerThan 10) - |> shouldFail + (fun () -> 10 |> shouldBeSmallerThan 10) |> shouldFail [] member __.``10[dot]0 should be less than 10[dot]1``() = @@ -22,4 +21,4 @@ type ``shouldBeSmallerThan tests``() = member __.``10[dot]0 should not be less than 10[dot]0``() = (fun () -> 10.0 |> shouldBeSmallerThan 10.0) |> should throw typeof - //|> shouldFail +//|> shouldFail diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs index 0c9b3d25..71f699cd 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs @@ -97,9 +97,7 @@ type ``shouldEqual Tests``() = |> Assert.Throws |> fun e -> e.Message - |> shouldEqual( - sprintf " Expected: not Equals Error \"Foo\"%sActual: Error \"Foo\"%s" Environment.NewLine Environment.NewLine - ) + |> shouldEqual(sprintf " Expected: not Equals Error \"Foo\"%sActual: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) [] member this.``structural equality``() = From c01985f789f7ef8dd3e9efdaf43cfb6b3c294fa9 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 22:45:18 +0100 Subject: [PATCH 16/23] Fix remaining tests --- src/FsUnit.Xunit/FsUnitTyped.fs | 18 +++++++++++------- .../typed.shouldEqualTests.fs | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index 86a21313..8f8c29cc 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -6,33 +6,37 @@ open FsUnit.Xunit [] module TopLevelOperators = + /// Asserts that `expected` is equal to `actual`. + /// The equality instance on `expected` is used. [] let shouldEqual<'a> (expected: 'a) (actual: 'a) = - should equal expected actual + should equal actual expected + /// Asserts that `expected` is not equal to `actual`. + /// The equality instance on `expected` is used. [] let shouldNotEqual<'a> (expected: 'a) (actual: 'a) = - expected |> should not' (equal actual) + should not' (equal actual) expected [] - let shouldContain<'a when 'a : equality> (x: 'a) (y: 'a seq) = + let shouldContain<'a when 'a: equality> (x: 'a) (y: 'a seq) = y |> should contain x [] - let shouldBeEmpty<'a> (list: 'a seq) = + let shouldBeEmpty<'a>(list: 'a seq) = list |> should be Empty [] - let shouldNotContain<'a when 'a : equality> (x: 'a) (y: 'a seq) = + let shouldNotContain<'a when 'a: equality> (x: 'a) (y: 'a seq) = if Seq.exists ((=) x) y then failwith $"Seq %A{y} should not contain %A{x}" [] - let shouldBeSmallerThan<'a when 'a : comparison> (x: 'a) (y: 'a) = + let shouldBeSmallerThan<'a when 'a: comparison> (x: 'a) (y: 'a) = should be (lessThan x) y [] - let shouldBeGreaterThan<'a when 'a : comparison> (x: 'a) (y: 'a) = + let shouldBeGreaterThan<'a when 'a: comparison> (x: 'a) (y: 'a) = should be (greaterThan x) y [] diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs index 71f699cd..4466030c 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs @@ -5,7 +5,6 @@ open System.Collections.Immutable open FsUnit.Xunit open Xunit open FsUnitTyped -open FsUnit open System type AlwaysEqual() = @@ -85,7 +84,12 @@ type ``shouldEqual Tests``() = |> Assert.Throws |> fun e -> e.Message - |> shouldEqual(sprintf " Expected: Error \"Bar\" or Error \"Bar\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) + |> shouldEqual( + sprintf + "Exception of type 'FsUnit.Xunit+MatchException' was thrown.%sExpected: Equals Error \"Bar\"%sActual: Error \"Foo\"" + Environment.NewLine + Environment.NewLine + ) [] member __.``Error "Foo" should not equal Error "Bar"``() = @@ -97,7 +101,12 @@ type ``shouldEqual Tests``() = |> Assert.Throws |> fun e -> e.Message - |> shouldEqual(sprintf " Expected: not Equals Error \"Foo\"%sActual: Error \"Foo\"%s" Environment.NewLine Environment.NewLine) + |> shouldEqual( + sprintf + "Exception of type 'FsUnit.Xunit+MatchException' was thrown.%sExpected: not Equals Error \"Foo\"%sActual: Error \"Foo\"" + Environment.NewLine + Environment.NewLine + ) [] member this.``structural equality``() = From 70e3c4b5261e21724fbd7bb6b6b8f16250bbea3f Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sun, 3 Apr 2022 23:06:37 +0100 Subject: [PATCH 17/23] Push tests that demonstrate inconsistency --- src/FsUnit.Xunit/FsUnitTyped.fs | 6 +++--- tests/FsUnit.Xunit.Test/equalTests.fs | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index 8f8c29cc..65d904e2 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -7,13 +7,13 @@ open FsUnit.Xunit module TopLevelOperators = /// Asserts that `expected` is equal to `actual`. - /// The equality instance on `expected` is used. + /// The equality instance on `expected` is used, if available. [] let shouldEqual<'a> (expected: 'a) (actual: 'a) = - should equal actual expected + should equal expected actual /// Asserts that `expected` is not equal to `actual`. - /// The equality instance on `expected` is used. + /// The equality instance on `expected` is used, if available. [] let shouldNotEqual<'a> (expected: 'a) (actual: 'a) = should not' (equal actual) expected diff --git a/tests/FsUnit.Xunit.Test/equalTests.fs b/tests/FsUnit.Xunit.Test/equalTests.fs index e27684c7..9e0942b7 100644 --- a/tests/FsUnit.Xunit.Test/equalTests.fs +++ b/tests/FsUnit.Xunit.Test/equalTests.fs @@ -61,6 +61,10 @@ type ``equal Tests``() = member __.``reference type should fail to not equal itself``() = anObj |> should equal anObj + [] + member __.``should pass when Equals returns true``() = + anObj |> should equal (box(new AlwaysEqual())) + [] member __.``should fail when Equals returns false``() = anObj |> should not' (equal(NeverEqual())) @@ -69,6 +73,10 @@ type ``equal Tests``() = member __.``should pass when negated and Equals returns false``() = anObj |> should not' (equal(NeverEqual())) + [] + member __.``should fail when negated and Equals returns true``() = + shouldFail(fun () -> anObj |> should not' (equal(box(new AlwaysEqual())))) + [] member __.``should pass when comparing two lists that have the same values``() = [ 1 ] |> should equal [ 1 ] From 692fedebfe72eddfcc72efdae305d7716a7252af Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Mon, 4 Apr 2022 09:08:51 +0100 Subject: [PATCH 18/23] Document bug 207 --- src/FsUnit.NUnit/FsUnit.fs | 7 +++++-- src/FsUnit.Xunit/CustomMatchers.fs | 3 +++ src/FsUnit.Xunit/FsUnit.fs | 1 + src/FsUnit.Xunit/FsUnitTyped.fs | 6 +++--- tests/FsUnit.Xunit.Test/equalTests.fs | 8 ++++++-- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/FsUnit.NUnit/FsUnit.fs b/src/FsUnit.NUnit/FsUnit.fs index cde05ddd..548355c8 100644 --- a/src/FsUnit.NUnit/FsUnit.fs +++ b/src/FsUnit.NUnit/FsUnit.fs @@ -42,8 +42,11 @@ module TopLevelOperators = else Assert.That(y, c) - let equal x = - Equality.IsEqualTo(x) + /// Constraint that attempts to use a number of more specific equality + /// checks available to NUnit, ultimately calling `expected.Equals(actual)` + /// if no more specific checks apply. + let equal expected = + Equality.IsEqualTo(expected) let equivalent x = CollectionEquivalentConstraint(x) diff --git a/src/FsUnit.Xunit/CustomMatchers.fs b/src/FsUnit.Xunit/CustomMatchers.fs index d0398b42..514bb369 100644 --- a/src/FsUnit.Xunit/CustomMatchers.fs +++ b/src/FsUnit.Xunit/CustomMatchers.fs @@ -7,6 +7,9 @@ open NHamcrest open NHamcrest.Core open System.Reflection +/// The `equal x` matcher, when applied to an object `a`, matches if `a = x`. +/// Note that this is inconsistent with FsUnit.NUnit +/// (see https://github.com/fsprojects/FsUnit/issues/207). let equal x = CustomMatcher($"Equals %A{x}", (fun a -> a = x)) diff --git a/src/FsUnit.Xunit/FsUnit.fs b/src/FsUnit.Xunit/FsUnit.fs index a57dfa5a..35d7bbcd 100644 --- a/src/FsUnit.Xunit/FsUnit.fs +++ b/src/FsUnit.Xunit/FsUnit.fs @@ -41,6 +41,7 @@ let inline shouldFail(f: unit -> unit) = raise(MatchException("Method should fail", "No exception raised", null)) +/// Constraint requiring that `actual.Equals(expected)`. let equal expected = CustomMatchers.equal expected diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index 65d904e2..f0752ae9 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -7,16 +7,16 @@ open FsUnit.Xunit module TopLevelOperators = /// Asserts that `expected` is equal to `actual`. - /// The equality instance on `expected` is used, if available. + /// The equality instance on `actual` is used, if available. [] let shouldEqual<'a> (expected: 'a) (actual: 'a) = should equal expected actual /// Asserts that `expected` is not equal to `actual`. - /// The equality instance on `expected` is used, if available. + /// The equality instance on `actual` is used, if available. [] let shouldNotEqual<'a> (expected: 'a) (actual: 'a) = - should not' (equal actual) expected + should not' (equal expected) actual [] let shouldContain<'a when 'a: equality> (x: 'a) (y: 'a seq) = diff --git a/tests/FsUnit.Xunit.Test/equalTests.fs b/tests/FsUnit.Xunit.Test/equalTests.fs index 9e0942b7..5bbf8b2f 100644 --- a/tests/FsUnit.Xunit.Test/equalTests.fs +++ b/tests/FsUnit.Xunit.Test/equalTests.fs @@ -63,7 +63,9 @@ type ``equal Tests``() = [] member __.``should pass when Equals returns true``() = - anObj |> should equal (box(new AlwaysEqual())) + // See https://github.com/fsprojects/FsUnit/issues/207 for why + // this test differs from the corresponding NUnit one. + box(new AlwaysEqual()) |> should equal anObj [] member __.``should fail when Equals returns false``() = @@ -75,7 +77,9 @@ type ``equal Tests``() = [] member __.``should fail when negated and Equals returns true``() = - shouldFail(fun () -> anObj |> should not' (equal(box(new AlwaysEqual())))) + // See https://github.com/fsprojects/FsUnit/issues/207 for why + // this test differs from the corresponding NUnit one. + shouldFail(fun () -> box(new AlwaysEqual()) |> should not' (equal anObj)) [] member __.``should pass when comparing two lists that have the same values``() = From 304c2da57033715da6c2719ef709a623aaa869f6 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Mon, 4 Apr 2022 09:12:22 +0100 Subject: [PATCH 19/23] And document instance in XUnit typed test --- tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs index 4466030c..81e70a9a 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs @@ -56,7 +56,9 @@ type ``shouldEqual Tests``() = [] member __.``should pass when Equals returns true``() = - anObj |> shouldEqual(box(new AlwaysEqual())) + // See https://github.com/fsprojects/FsUnit/issues/207 for why + // this test differs from the corresponding NUnit one. + box(AlwaysEqual()) |> shouldEqual anObj [] member __.``should fail when Equals returns false``() = @@ -68,7 +70,9 @@ type ``shouldEqual Tests``() = [] member __.``should fail when negated and Equals returns true``() = - shouldFail(fun () -> anObj |> shouldNotEqual(box(new AlwaysEqual()))) + // See https://github.com/fsprojects/FsUnit/issues/207 for why + // this test differs from the corresponding NUnit one. + shouldFail(fun () -> box(AlwaysEqual()) |> shouldNotEqual anObj) [] member __.``None should equal None``() = From a7c42c77299551402e8b48e9702b8279dab6f688 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Mon, 4 Apr 2022 18:06:18 +0100 Subject: [PATCH 20/23] Lint nits --- src/FsUnit.Xunit/FsUnitTyped.fs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index f0752ae9..75f8fb91 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -1,6 +1,7 @@ namespace FsUnitTyped open System.Diagnostics +open Xunit open FsUnit.Xunit [] @@ -10,13 +11,13 @@ module TopLevelOperators = /// The equality instance on `actual` is used, if available. [] let shouldEqual<'a> (expected: 'a) (actual: 'a) = - should equal expected actual + actual |> should equal expected /// Asserts that `expected` is not equal to `actual`. /// The equality instance on `actual` is used, if available. [] let shouldNotEqual<'a> (expected: 'a) (actual: 'a) = - should not' (equal expected) actual + actual |> should not' (equal expected) [] let shouldContain<'a when 'a: equality> (x: 'a) (y: 'a seq) = @@ -24,7 +25,7 @@ module TopLevelOperators = [] let shouldBeEmpty<'a>(list: 'a seq) = - list |> should be Empty + Assert.Empty list [] let shouldNotContain<'a when 'a: equality> (x: 'a) (y: 'a seq) = @@ -33,15 +34,15 @@ module TopLevelOperators = [] let shouldBeSmallerThan<'a when 'a: comparison> (x: 'a) (y: 'a) = - should be (lessThan x) y + y |> should be (lessThan x) [] let shouldBeGreaterThan<'a when 'a: comparison> (x: 'a) (y: 'a) = - should be (greaterThan x) y + y |> should be (greaterThan x) [] let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = - should throw typeof<'exn> f + f |> should throw typeof<'exn> [] let shouldContainText (x: string) (y: string) = From 562accb303c1e6f9e6c784c12d00634468cf2cd7 Mon Sep 17 00:00:00 2001 From: Sergey Tihon Date: Tue, 5 Apr 2022 11:19:55 +0200 Subject: [PATCH 21/23] fix: actual/expected naming --- src/FsUnit.Xunit/FsUnitTyped.fs | 40 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index 75f8fb91..178e1e36 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -20,43 +20,43 @@ module TopLevelOperators = actual |> should not' (equal expected) [] - let shouldContain<'a when 'a: equality> (x: 'a) (y: 'a seq) = - y |> should contain x + let shouldContain<'a when 'a: equality> (expected: 'a) (actual: 'a seq) = + actual |> should contain expected [] - let shouldBeEmpty<'a>(list: 'a seq) = - Assert.Empty list + let shouldBeEmpty<'a>(actual: 'a seq) = + Assert.Empty actual [] - let shouldNotContain<'a when 'a: equality> (x: 'a) (y: 'a seq) = - if Seq.exists ((=) x) y then - failwith $"Seq %A{y} should not contain %A{x}" + let shouldNotContain<'a when 'a: equality> (expected: 'a) (actual: 'a seq) = + if Seq.exists ((=) expected) actual then + failwith $"Seq %A{actual} should not contain %A{expected}" [] - let shouldBeSmallerThan<'a when 'a: comparison> (x: 'a) (y: 'a) = - y |> should be (lessThan x) + let shouldBeSmallerThan<'a when 'a: comparison> (expected: 'a) (actual: 'a) = + actual |> should be (lessThan expected) [] - let shouldBeGreaterThan<'a when 'a: comparison> (x: 'a) (y: 'a) = - y |> should be (greaterThan x) + let shouldBeGreaterThan<'a when 'a: comparison> (expected: 'a) (actual: 'a) = + actual |> should be (greaterThan expected) [] let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = f |> should throw typeof<'exn> [] - let shouldContainText (x: string) (y: string) = - if y.Contains(x) |> not then - failwith $"\"{x}\" is not a substring of \"{y}\"" + let shouldContainText (expected: string) (actual: string) = + if actual.Contains(expected) |> not then + failwith $"\"{expected}\" is not a substring of \"{actual}\"" [] - let shouldNotContainText (x: string) (y: string) = - if y.Contains(x) then - failwith $"\"{x}\" is a substring of \"{y}\"" + let shouldNotContainText (expected: string) (actual: string) = + if actual.Contains(expected) then + failwith $"\"{expected}\" is a substring of \"{actual}\"" [] - let shouldHaveLength<'a> (expected: int) (list: 'a seq) = - let actual = Seq.length list + let shouldHaveLength<'a> (expected: int) (actual: 'a seq) = + let actual = Seq.length actual if actual <> expected then - failwith $"Invalid length in %A{list}\r\nExpected: {expected}\r\nActual: {actual}" + failwith $"Invalid length in %A{actual}\r\nExpected: {expected}\r\nActual: {actual}" From 7def6eda344ded93301abc3da27bb336e1ea7ba4 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Tue, 5 Apr 2022 18:35:03 +0100 Subject: [PATCH 22/23] Revert tests which demonstrated the fixed bug --- tests/FsUnit.Xunit.Test/equalTests.fs | 8 ++------ tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/tests/FsUnit.Xunit.Test/equalTests.fs b/tests/FsUnit.Xunit.Test/equalTests.fs index 5bbf8b2f..2343e4a6 100644 --- a/tests/FsUnit.Xunit.Test/equalTests.fs +++ b/tests/FsUnit.Xunit.Test/equalTests.fs @@ -63,9 +63,7 @@ type ``equal Tests``() = [] member __.``should pass when Equals returns true``() = - // See https://github.com/fsprojects/FsUnit/issues/207 for why - // this test differs from the corresponding NUnit one. - box(new AlwaysEqual()) |> should equal anObj + anObj |> should equal (box(new AlwaysEqual())) [] member __.``should fail when Equals returns false``() = @@ -77,9 +75,7 @@ type ``equal Tests``() = [] member __.``should fail when negated and Equals returns true``() = - // See https://github.com/fsprojects/FsUnit/issues/207 for why - // this test differs from the corresponding NUnit one. - shouldFail(fun () -> box(new AlwaysEqual()) |> should not' (equal anObj)) + shouldFail(fun () -> anObj |> should not' (equal(box(AlwaysEqual())))) [] member __.``should pass when comparing two lists that have the same values``() = diff --git a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs index 81e70a9a..6606fa25 100644 --- a/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs +++ b/tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs @@ -56,9 +56,7 @@ type ``shouldEqual Tests``() = [] member __.``should pass when Equals returns true``() = - // See https://github.com/fsprojects/FsUnit/issues/207 for why - // this test differs from the corresponding NUnit one. - box(AlwaysEqual()) |> shouldEqual anObj + anObj |> shouldEqual(box(AlwaysEqual())) [] member __.``should fail when Equals returns false``() = @@ -70,9 +68,7 @@ type ``shouldEqual Tests``() = [] member __.``should fail when negated and Equals returns true``() = - // See https://github.com/fsprojects/FsUnit/issues/207 for why - // this test differs from the corresponding NUnit one. - shouldFail(fun () -> box(AlwaysEqual()) |> shouldNotEqual anObj) + shouldFail(fun () -> anObj |> shouldNotEqual(box(AlwaysEqual()))) [] member __.``None should equal None``() = From 500573e060cd256f8b8bf5cf31053dd873db5ed8 Mon Sep 17 00:00:00 2001 From: Sergey Tihon Date: Wed, 6 Apr 2022 09:39:01 +0200 Subject: [PATCH 23/23] fix: remove outdated comment --- src/FsUnit.Xunit/FsUnit.fs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/FsUnit.Xunit/FsUnit.fs b/src/FsUnit.Xunit/FsUnit.fs index 8cf27348..23651e6f 100644 --- a/src/FsUnit.Xunit/FsUnit.fs +++ b/src/FsUnit.Xunit/FsUnit.fs @@ -41,7 +41,6 @@ let inline shouldFail(f: unit -> unit) = raise(MatchException("Method should fail", "No exception raised", null)) -/// Constraint requiring that `actual.Equals(expected)`. let equal expected = CustomMatchers.equal expected