Skip to content

Commit 20c55e7

Browse files
authored
Merge pull request #305 from RJSonnenberg/xunit-equivalent-assertion
Add equivalent assertion and related tests
2 parents 47a18d0 + 16f9f76 commit 20c55e7

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

docs/operators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Operators comparison across frameworks
1414
| `should` ||||
1515
| `equal` ||||
1616
| `equalSeq` ||||
17-
| `equivalent` || ||
17+
| `equivalent` || ||
1818
| `equalWithin` ||||
1919
| `contain` ||||
2020
| `haveLength` ||||

src/FsUnit.Xunit/FsUnit.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ let equalSeq expected =
5757
let equal expected =
5858
CustomMatchers.equal expected
5959

60+
let equivalent expected =
61+
CustomMatchers.equivalent (fun e a -> Assert.Equivalent(e, a, true)) expected
62+
6063
let equalWithin (tolerance: obj) (expected: obj) =
6164
CustomMatchers.equalWithin tolerance expected
6265

src/FsUnit.Xunit/FsUnitTyped.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ module TopLevelOperators =
1111
let shouldEqual<'a> (expected: 'a) (actual: 'a) =
1212
actual |> should equal expected
1313

14+
[<DebuggerStepThrough>]
15+
let shouldEquivalent<'a when 'a: equality> (expected: 'a seq) (actual: 'a seq) =
16+
actual |> should equivalent expected
17+
1418
[<DebuggerStepThrough>]
1519
let shouldNotEqual<'a> (expected: 'a) (actual: 'a) =
1620
actual |> should not' (equal expected)

tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Compile Include="typed.shouldContainTextTests.fs" />
4646
<Compile Include="typed.shouldFailTests.fs" />
4747
<Compile Include="typed.shouldEqualTests.fs" />
48+
<Compile Include="equivalentTests.fs" />
4849
<None Include="paket.references" />
4950
</ItemGroup>
5051
<ItemGroup>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace FsUnit.Test
2+
3+
open System
4+
open Xunit
5+
open FsUnit.Xunit
6+
7+
module EquivalentTests =
8+
9+
[<Fact>]
10+
let ``two lists with same elements in different order are equivalent``() =
11+
[ 1; 2; 3 ] |> should equivalent [ 3; 2; 1 ]
12+
13+
[<Fact>]
14+
let ``two lists with different elements are not equivalent``() =
15+
[ 1; 2; 3 ] |> should not' (equivalent [ 4; 5; 6 ])
16+
17+
[<Fact>]
18+
let ``two lists with different lengths are not equivalent``() =
19+
[ 1; 2; 3 ] |> should not' (equivalent [ 1; 2 ])
20+
21+
[<Fact>]
22+
let ``two arrays with same elements in different order are equivalent``() =
23+
[| 1; 2; 3 |] |> should equivalent [| 3; 2; 1 |]
24+
25+
[<Fact>]
26+
let ``two arrays with different elements are not equivalent``() =
27+
[| 1; 2; 3 |] |> should not' (equivalent [| 4; 5; 6 |])
28+
29+
[<Fact>]
30+
let ``empty collections are equivalent``() =
31+
[] |> should equivalent []
32+
[||] |> should equivalent [||]
33+
34+
[<Fact>]
35+
let ``collections with same elements and duplicates are not equivalent if counts differ``() =
36+
[ 1; 1; 2 ] |> should not' (equivalent [ 1; 2; 2 ])
37+
38+
[<Fact>]
39+
let ``collections with same elements and same counts are equivalent``() =
40+
[ 1; 1; 2 ] |> should equivalent [ 2; 1; 1 ]

0 commit comments

Comments
 (0)