From 2cee7a490d3f97fb3a33cd0c447016fb19cd2970 Mon Sep 17 00:00:00 2001 From: "Reuben J. Sonnenberg" Date: Mon, 23 Jun 2025 22:24:23 -0800 Subject: [PATCH 1/3] Add equivalent assertion and related tests --- src/FsUnit.Xunit/FsUnit.fs | 3 ++ src/FsUnit.Xunit/FsUnitTyped.fs | 4 ++ .../FsUnit.Xunit.Test.fsproj | 1 + tests/FsUnit.Xunit.Test/equivalentTests.fs | 40 +++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 tests/FsUnit.Xunit.Test/equivalentTests.fs diff --git a/src/FsUnit.Xunit/FsUnit.fs b/src/FsUnit.Xunit/FsUnit.fs index 4165fa87..3afb2030 100644 --- a/src/FsUnit.Xunit/FsUnit.fs +++ b/src/FsUnit.Xunit/FsUnit.fs @@ -57,6 +57,9 @@ let equalSeq expected = let equal expected = CustomMatchers.equal expected +let equivalent expected = + CustomMatchers.equivalent (fun e a -> Assert.Equivalent(e, a, true)) expected + let equalWithin (tolerance: obj) (expected: obj) = CustomMatchers.equalWithin tolerance expected diff --git a/src/FsUnit.Xunit/FsUnitTyped.fs b/src/FsUnit.Xunit/FsUnitTyped.fs index bd16638a..cb973bde 100644 --- a/src/FsUnit.Xunit/FsUnitTyped.fs +++ b/src/FsUnit.Xunit/FsUnitTyped.fs @@ -11,6 +11,10 @@ module TopLevelOperators = let shouldEqual<'a> (expected: 'a) (actual: 'a) = actual |> should equal expected + [] + let shouldEquivalent<'a when 'a: equality> (expected: 'a seq) (actual: 'a seq) = + actual |> should equivalent expected + [] let shouldNotEqual<'a> (expected: 'a) (actual: 'a) = actual |> should not' (equal expected) diff --git a/tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj b/tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj index 98dd7621..9e431281 100644 --- a/tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj +++ b/tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj @@ -45,6 +45,7 @@ + diff --git a/tests/FsUnit.Xunit.Test/equivalentTests.fs b/tests/FsUnit.Xunit.Test/equivalentTests.fs new file mode 100644 index 00000000..6ef75313 --- /dev/null +++ b/tests/FsUnit.Xunit.Test/equivalentTests.fs @@ -0,0 +1,40 @@ +namespace FsUnit.Test + +open System +open Xunit +open FsUnit.Xunit + +module EquivalentTests = + + [] + let ``two lists with same elements in different order are equivalent`` () = + [1;2;3] |> should equivalent [3;2;1] + + [] + let ``two lists with different elements are not equivalent`` () = + [1;2;3] |> should not' (equivalent [4;5;6]) + + [] + let ``two lists with different lengths are not equivalent`` () = + [1;2;3] |> should not' (equivalent [1;2]) + + [] + let ``two arrays with same elements in different order are equivalent`` () = + [|1;2;3|] |> should equivalent [|3;2;1|] + + [] + let ``two arrays with different elements are not equivalent`` () = + [|1;2;3|] |> should not' (equivalent [|4;5;6|]) + + [] + let ``empty collections are equivalent`` () = + [] |> should equivalent [] + [||] |> should equivalent [||] + + [] + let ``collections with same elements and duplicates are not equivalent if counts differ`` () = + [1;1;2] |> should not' (equivalent [1;2;2]) + + [] + let ``collections with same elements and same counts are equivalent`` () = + [1;1;2] |> should equivalent [2;1;1] From 653499166a1cae2bfccf501cf516fbd173f6cff9 Mon Sep 17 00:00:00 2001 From: "Reuben J. Sonnenberg" Date: Tue, 24 Jun 2025 10:18:36 -0800 Subject: [PATCH 2/3] Fix equivalent operator support in xUnit framework --- docs/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/operators.md b/docs/operators.md index 27ee0652..fbb78df1 100644 --- a/docs/operators.md +++ b/docs/operators.md @@ -14,7 +14,7 @@ Operators comparison across frameworks | `should` | ✔ | ✔ | ✔ | | `equal` | ✔ | ✔ | ✔ | | `equalSeq` | ✔ | ✔ | ✔ | -| `equivalent` | ✔ | ❌ | ✔ | +| `equivalent` | ✔ | ✔ | ✔ | | `equalWithin` | ✔ | ✔ | ✔ | | `contain` | ✔ | ✔ | ✔ | | `haveLength` | ✔ | ✔ | ✔ | From 16f9f76a1e098efe0813546af8bac33a35c1abd5 Mon Sep 17 00:00:00 2001 From: Constantin Tews Date: Thu, 26 Jun 2025 08:03:39 +0200 Subject: [PATCH 3/3] Format code. --- tests/FsUnit.Xunit.Test/equivalentTests.fs | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/FsUnit.Xunit.Test/equivalentTests.fs b/tests/FsUnit.Xunit.Test/equivalentTests.fs index 6ef75313..4be91da8 100644 --- a/tests/FsUnit.Xunit.Test/equivalentTests.fs +++ b/tests/FsUnit.Xunit.Test/equivalentTests.fs @@ -7,34 +7,34 @@ open FsUnit.Xunit module EquivalentTests = [] - let ``two lists with same elements in different order are equivalent`` () = - [1;2;3] |> should equivalent [3;2;1] + let ``two lists with same elements in different order are equivalent``() = + [ 1; 2; 3 ] |> should equivalent [ 3; 2; 1 ] [] - let ``two lists with different elements are not equivalent`` () = - [1;2;3] |> should not' (equivalent [4;5;6]) + let ``two lists with different elements are not equivalent``() = + [ 1; 2; 3 ] |> should not' (equivalent [ 4; 5; 6 ]) [] - let ``two lists with different lengths are not equivalent`` () = - [1;2;3] |> should not' (equivalent [1;2]) + let ``two lists with different lengths are not equivalent``() = + [ 1; 2; 3 ] |> should not' (equivalent [ 1; 2 ]) [] - let ``two arrays with same elements in different order are equivalent`` () = - [|1;2;3|] |> should equivalent [|3;2;1|] + let ``two arrays with same elements in different order are equivalent``() = + [| 1; 2; 3 |] |> should equivalent [| 3; 2; 1 |] [] - let ``two arrays with different elements are not equivalent`` () = - [|1;2;3|] |> should not' (equivalent [|4;5;6|]) + let ``two arrays with different elements are not equivalent``() = + [| 1; 2; 3 |] |> should not' (equivalent [| 4; 5; 6 |]) [] - let ``empty collections are equivalent`` () = + let ``empty collections are equivalent``() = [] |> should equivalent [] [||] |> should equivalent [||] [] - let ``collections with same elements and duplicates are not equivalent if counts differ`` () = - [1;1;2] |> should not' (equivalent [1;2;2]) + let ``collections with same elements and duplicates are not equivalent if counts differ``() = + [ 1; 1; 2 ] |> should not' (equivalent [ 1; 2; 2 ]) [] - let ``collections with same elements and same counts are equivalent`` () = - [1;1;2] |> should equivalent [2;1;1] + let ``collections with same elements and same counts are equivalent``() = + [ 1; 1; 2 ] |> should equivalent [ 2; 1; 1 ]