Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collection subsetOf assertion for NUnit, xUnit and MsTest. #144

Merged
merged 4 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/content/index.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ anObj |> should not' (be sameAs otherObj)
[1..10] |> should be (supersetOf [3;6;9])
[1..10] |> should not' (be supersetOf [5;11;21])

[3;6;9] |> should be (subsetOf [1..10])
[5;11;21] |> should not' (be subsetOf [1..10])

(**
The ofCase operator allows you to check the case of a union.
Supplying an expression that will result in a non-union type as well as supplying a non-union type as value argument will result in an exception detailing which parameter is wrong. Note that the actual value of the case is NOT checked, e.g. using `<@ MyCase 5 @>` as expression and `(MyCase 10)` as parameter will succeed. It is possible to check for more than one case by using a tuple of union cases.
Expand Down
1 change: 1 addition & 0 deletions docs/content/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ Operators comparison across frameworks
| `inRange` | + | + | + |
| `ofCase` | + | + | + |
| `supersetOf` | + | + | + |
| `subsetOf` | + | + | + |
4 changes: 3 additions & 1 deletion src/FsUnit.MsTestUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,6 @@ let inRange min max = CustomMatchers.inRange min max

let ofCase case = CustomMatchers.ofCase case

let supersetOf expected = CustomMatchers.supersetOf expected
let supersetOf expected = CustomMatchers.supersetOf expected

let subsetOf expected = CustomMatchers.subsetOf expected
4 changes: 3 additions & 1 deletion src/FsUnit.NUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@ module TopLevelOperators =

let ofCase case = OfSameCaseConstraint(case)

let supersetOf x = CollectionSupersetConstraint(x)
let supersetOf x = CollectionSupersetConstraint(x)

let subsetOf x = CollectionSubsetConstraint(x)
3 changes: 3 additions & 0 deletions src/FsUnit.Xunit/CustomMatchers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ let containf f = CustomMatcher<obj>(sprintf "Contains %s" (f.ToString()),
let supersetOf x = CustomMatcher<obj>(sprintf "Is superset of %A" x,
fun c -> Set.isSuperset (Set (unbox c)) (Set x))

let subsetOf x = CustomMatcher<obj>(sprintf "Is subset of %A" x,
fun c -> Set.isSubset (Set (unbox c)) (Set x))

let matchList xs = CustomMatcher<obj>(sprintf "All elements from list %s" (xs.ToString()),
fun ys -> match ys with
| :? list<_> as ys' -> List.sort xs = List.sort ys'
Expand Down
4 changes: 3 additions & 1 deletion src/FsUnit.Xunit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,6 @@ let descending = CustomMatchers.descending

let inRange min max = CustomMatchers.inRange min max

let supersetOf expected = CustomMatchers.supersetOf expected
let supersetOf expected = CustomMatchers.supersetOf expected

let subsetOf expected = CustomMatchers.subsetOf expected
3 changes: 2 additions & 1 deletion tests/FsUnit.MsTest.Test/Fs30Unit.MsTest.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
<Compile Include="beNullTests.fs" />
<Compile Include="beOfExactTypeTests.fs" />
<Compile Include="beSameAsTests.fs" />
<Compile Include="beSupersetOf.fs" />
<Compile Include="beSupersetOfTests.fs" />
<Compile Include="beSubsetOfTests.fs" />
<Compile Include="beTrueTests.fs" />
<Compile Include="containTests.fs" />
<Compile Include="equalTests.fs" />
Expand Down
39 changes: 39 additions & 0 deletions tests/FsUnit.MsTest.Test/beSubsetOfTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace FsUnit.Test
open Microsoft.VisualStudio.TestTools.UnitTesting
open FsUnit.MsTest

[<TestClass>]
type ``be subsetOf tests`` ()=

[<TestMethod>] member test.
``5, 3 and 8 should be subset of 1 to 10`` ()=
[5;3;8] |> should be (subsetOf [1..10])

[<TestMethod>] member test.
``5 should be subset of 1 to 10`` ()=
[5] |> should be (subsetOf [1..10])

[<TestMethod>] member test.
``4 to 8 should be subset of 1 to 10`` ()=
{4..8} |> should be (subsetOf {1..10})

[<TestMethod>] member test.
``1 to 10 should be subset of 4. 1 and 7`` ()=
[|1..10|] |> should be (supersetOf [|4;1;7|])

[<TestMethod>] member test.
``5, 1 and 11 should not be subset of 1 to 10`` ()=
[5;1;11] |> should not' (be subsetOf [|1..10|])

[<TestMethod>] member test.
``1 to 10 should not be subset of 5`` ()=
[1..10] |> should not' (be subsetOf [5])

[<TestMethod>] member test.
``1 to 10 should be subset of 1 to 10`` ()=
[1..10] |> should be (subsetOf [1..10])

[<TestMethod>] member test.
``should fail on '1 to 11 should be subset of 1 to 10'`` ()=
shouldFail (fun () ->
[1..11] |> should be (subsetOf [1..10]))
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ type ``be supersetOf tests`` ()=
[1..10] |> should be (supersetOf [1..10])

[<TestMethod>] member test.
``should fail on '1 to 10 should not be superset of 1 to 10'`` ()=
``should fail on '1 to 10 should be superset of 1 to 11'`` ()=
shouldFail (fun () ->
[1..10] |> should not' (be supersetOf [1..10]))
[1..10] |> should be (supersetOf [1..11]))
3 changes: 2 additions & 1 deletion tests/FsUnit.NUnit.Test/FsUnit.NUnit.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<Compile Include="beEmptyStringTests.fs" />
<Compile Include="beInRangeTests.fs" />
<Compile Include="beNullOrEmptyStringTests.fs" />
<Compile Include="beSupersetOf.fs" />
<Compile Include="beSupersetOfTests.fs" />
<Compile Include="beSubsetOfTests.fs" />
<Compile Include="beTrueTests.fs" />
<Compile Include="beFalseTests.fs" />
<Compile Include="beSameAsTests.fs" />
Expand Down
39 changes: 39 additions & 0 deletions tests/FsUnit.NUnit.Test/beSubsetOfTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace FsUnit.Test
open NUnit.Framework
open FsUnit

[<TestFixture>]
type ``be subsetOf tests`` ()=

[<Test>] member test.
``5, 3 and 8 should be subset of 1 to 10`` ()=
[5;3;8] |> should be (subsetOf [1..10])

[<Test>] member test.
``5 should be subset of 1 to 10`` ()=
[5] |> should be (subsetOf [1..10])

[<Test>] member test.
``4 to 8 should be subset of 1 to 10`` ()=
{4..8} |> should be (subsetOf {1..10})

[<Test>] member test.
``1 to 10 should be subset of 4. 1 and 7`` ()=
[|1..10|] |> should be (supersetOf [|4;1;7|])

[<Test>] member test.
``5, 1 and 11 should not be subset of 1 to 10`` ()=
[5;1;11] |> should not' (be subsetOf [|1..10|])

[<Test>] member test.
``1 to 10 should not be subset of 5`` ()=
[1..10] |> should not' (be subsetOf [5])

[<Test>] member test.
``1 to 10 should be subset of 1 to 10`` ()=
[1..10] |> should be (subsetOf [1..10])

[<Test>] member test.
``should fail on '1 to 11 should be subset of 1 to 10'`` ()=
shouldFail (fun () ->
[1..11] |> should be (subsetOf [1..10]))
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ type ``be supersetOf tests`` ()=
[1..10] |> should be (supersetOf [1..10])

[<Test>] member test.
``should fail on '1 to 10 should not be superset of 1 to 10'`` ()=
``should fail on '1 to 10 should be superset of 1 to 11'`` ()=
shouldFail (fun () ->
[1..10] |> should not' (be supersetOf [1..10]))
[1..10] |> should be (supersetOf [1..11]))
3 changes: 2 additions & 1 deletion tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<Compile Include="beNullTests.fs" />
<Compile Include="beOfExactTypeTests.fs" />
<Compile Include="beSameAsTests.fs" />
<Compile Include="beSupersetOf.fs" />
<Compile Include="beSupersetOfTests.fs" />
<Compile Include="beSubsetOfTests.fs" />
<Compile Include="beTrueTests.fs" />
<Compile Include="haveCountTests.fs" />
<Compile Include="haveLengthTests.fs" />
Expand Down
38 changes: 38 additions & 0 deletions tests/FsUnit.Xunit.Test/beSubsetOfTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace FsUnit.Test
open Xunit
open FsUnit.Xunit

type ``be subsetOf tests`` ()=

[<Fact>] member test.
``5, 3 and 8 should be subset of 1 to 10`` ()=
[5;3;8] |> should be (subsetOf [1..10])

[<Fact>] member test.
``5 should be subset of 1 to 10`` ()=
[5] |> should be (subsetOf [1..10])

[<Fact>] member test.
``4 to 8 should be subset of 1 to 10`` ()=
{4..8} |> should be (subsetOf {1..10})

[<Fact>] member test.
``1 to 10 should be subset of 4. 1 and 7`` ()=
[|1..10|] |> should be (supersetOf [|4;1;7|])

[<Fact>] member test.
``5, 1 and 11 should not be subset of 1 to 10`` ()=
[5;1;11] |> should not' (be subsetOf [|1..10|])

[<Fact>] member test.
``1 to 10 should not be subset of 5`` ()=
[1..10] |> should not' (be subsetOf [5])

[<Fact>] member test.
``1 to 10 should be subset of 1 to 10`` ()=
[1..10] |> should be (subsetOf [1..10])

[<Fact>] member test.
``should fail on '1 to 11 should be subset of 1 to 10'`` ()=
shouldFail (fun () ->
[1..11] |> should be (subsetOf [1..10]))
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ type ``be supersetOf tests`` ()=
[1..10] |> should be (supersetOf [1..10])

[<Fact>] member test.
``should fail on '1 to 10 should not be superset of 1 to 10'`` ()=
``should fail on '1 to 10 should be superset of 1 to 11'`` ()=
shouldFail (fun () ->
[1..10] |> should not' (be supersetOf [1..10]))
[1..10] |> should be (supersetOf [1..11]))