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 typed aliases around Xunit #203

Merged
merged 26 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b117a79
Add typed aliases around Xunit
Smaug123 Mar 31, 2022
bd20cb8
Add typed tests
Smaug123 Mar 31, 2022
56dea9b
Add missing line
Smaug123 Mar 31, 2022
d6c22ff
Update doc
Smaug123 Mar 31, 2022
d57e8f5
Include in fsproj files
Smaug123 Apr 3, 2022
908c964
Trying to get it to compile - GitHub please tell me if this worked
Smaug123 Apr 3, 2022
d81e8c9
Fix syntax error
Smaug123 Apr 3, 2022
78ed163
Fix more errors
Smaug123 Apr 3, 2022
0b8296b
Fix contains error
Smaug123 Apr 3, 2022
12db71f
Fix more errors
Smaug123 Apr 3, 2022
e1811a8
Fix brackets
Smaug123 Apr 3, 2022
2af5ee9
Pipeline, please tell me what exception gets thrown
Smaug123 Apr 3, 2022
59cf538
Merge commit 'f0ecf02143cd22f06ce5a447d9a4fe7d4aceed7d' into xunit-typed
sergey-tihon Apr 3, 2022
d22388b
feat: fantomas format
sergey-tihon Apr 3, 2022
698644f
Merge commit 'c9963411a065fde2a49f5fe258c48a9b3ab1606e' into xunit-typed
sergey-tihon Apr 3, 2022
bffbe34
Fix all but a couple of equality tests
Smaug123 Apr 3, 2022
7845667
Format
Smaug123 Apr 3, 2022
c01985f
Fix remaining tests
Smaug123 Apr 3, 2022
70e3c4b
Push tests that demonstrate inconsistency
Smaug123 Apr 3, 2022
692fede
Document bug 207
Smaug123 Apr 4, 2022
304c2da
And document instance in XUnit typed test
Smaug123 Apr 4, 2022
a7c42c7
Lint nits
Smaug123 Apr 4, 2022
664b05f
Merge commit '4fe7a5b10821db86939ea90588ab84c0ad8b6bbb' into xunit-typed
sergey-tihon Apr 5, 2022
562accb
fix: actual/expected naming
sergey-tihon Apr 5, 2022
7def6ed
Revert tests which demonstrated the fixed bug
Smaug123 Apr 5, 2022
500573e
fix: remove outdated comment
sergey-tihon Apr 6, 2022
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
4 changes: 2 additions & 2 deletions docs/FsUnitTyped.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion src/FsUnit.Xunit/FsUnit.Xunit.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</Compile>
<Compile Include="CustomMatchers.fs" />
<Compile Include="FsUnit.fs" />
<Compile Include="FsUnitTyped.fs" />
<None Include="paket.references" />
<None Include="paket.template" />
<None Include="FsUnitSample.fs.pp">
Expand All @@ -22,4 +23,4 @@
<None Include="sample.paket.template" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
</Project>
61 changes: 61 additions & 0 deletions src/FsUnit.Xunit/FsUnitTyped.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace FsUnitTyped

open System.Diagnostics
open Xunit
open System.Collections.Generic
open FsUnit.Xunit

[<AutoOpen>]
module TopLevelOperators =

[<DebuggerStepThrough>]
let shouldEqual (expected: 'a) (actual: 'a) =
expected |> should equal actual

[<DebuggerStepThrough>]
let shouldNotEqual (expected: 'a) (actual: 'a) =
expected |> should not' (equal actual)

[<DebuggerStepThrough>]
let shouldContain (x: 'a) (y: 'a seq) =
y |> should contain x

[<DebuggerStepThrough>]
let shouldBeEmpty(list: 'a seq) =
list |> should be Empty

[<DebuggerStepThrough>]
let shouldNotContain (x: 'a) (y: 'a seq) =
if Seq.exists ((=) x) y then
failwith $"Seq %A{y} should not contain %A{x}"

[<DebuggerStepThrough>]
let shouldBeSmallerThan (x: 'a) (y: 'a) =
if not(x < y) then
Smaug123 marked this conversation as resolved.
Show resolved Hide resolved
failwith $"Expected:\n %A{x}\nto be smaller than:\n %A{y}"

[<DebuggerStepThrough>]
let shouldBeGreaterThan (x: 'a) (y: 'a) =
if not(x > y) then
Smaug123 marked this conversation as resolved.
Show resolved Hide resolved
failwith $"Expected:\n %A{x}\nto be greater than:\n %A{y}"

[<DebuggerStepThrough>]
let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) =
f |> should throw typeof<'exn>

[<DebuggerStepThrough>]
let shouldContainText (x: string) (y: string) =
if y.Contains(x) |> not then
failwith $"\"{x}\" is not a substring of \"{y}\""

[<DebuggerStepThrough>]
let shouldNotContainText (x: string) (y: string) =
if y.Contains(x) then
failwith $"\"{x}\" is a substring of \"{y}\""

[<DebuggerStepThrough>]
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}"
11 changes: 10 additions & 1 deletion tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@
<Compile Include="instanceOfTests.fs" />
<Compile Include="NaNTests.fs" />
<Compile Include="beUniqueTests.fs" />
<Compile Include="typed.beEmptyTests.fs" />
<Compile Include="typed.haveLengthTests.fs" />
<Compile Include="typed.shouldBeGreaterThanTests.fs" />
<Compile Include="typed.shouldBeSmallerThanTests.fs" />
<Compile Include="typed.shouldContainTests.fs" />
<Compile Include="typed.shouldContainText.fs" />
<Compile Include="typed.shouldEqualNullTests.fs" />
<Compile Include="typed.shouldFailTests.fs" />
<Compile Include="typed.shouldEqualTests.fs" />
<None Include="paket.references" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/FsUnit.Xunit/FsUnit.Xunit.fsproj" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
</Project>
29 changes: 29 additions & 0 deletions tests/FsUnit.Xunit.Test/typed.beEmptyTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace FsUnit.Typed.Test

open Xunit
open FsUnitTyped

type ``shouldBeEmpty tests``() =
[<Fact>]
member __.``empty List should be Empty``() =
[] |> shouldBeEmpty

[<Fact>]
member __.``non-empty List should fail to be Empty``() =
shouldFail(fun () -> [ 1 ] |> shouldBeEmpty)

[<Fact>]
member __.``empty Array should be Empty``() =
[||] |> shouldBeEmpty

[<Fact>]
member __.``non-empty Array should fail to be Empty``() =
shouldFail(fun () -> [| 1 |] |> shouldBeEmpty)

[<Fact>]
member __.``empty Seq should be Empty``() =
Seq.empty |> shouldBeEmpty

[<Fact>]
member __.``non-empty Seq should fail to be Empty``() =
shouldFail(fun () -> seq { 1 } |> shouldBeEmpty)
23 changes: 23 additions & 0 deletions tests/FsUnit.Xunit.Test/typed.haveLengthTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace FsUnit.Typed.Test

open Xunit
open FsUnitTyped

type ``haveLength tests``() =
// F# List
[<Fact>]
member __.``List with 1 item should have Length 1``() =
[ 1 ] |> shouldHaveLength 1

[<Fact>]
member __.``empty List should fail to have Length 1``() =
shouldFail(fun () -> [] |> shouldHaveLength 1)

// Array
[<Fact>]
member __.``Array with 1 item should have Length 1``() =
[| 1 |] |> shouldHaveLength 1

[<Fact>]
member __.``empty Array should fail to have Length 1``() =
shouldFail(fun () -> [||] |> shouldHaveLength 1)
13 changes: 13 additions & 0 deletions tests/FsUnit.Xunit.Test/typed.shouldBeGreaterThanTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace FsUnit.Typed.Test

open Xunit
open FsUnitTyped

type ``shouldBeGreaterThan tests``() =
[<Fact>]
member __.``11 should be greater than 10``() =
11 |> shouldBeGreaterThan 10

[<Fact>]
member __.``11[dot]1 should be greater than 11[dot]0``() =
11.1 |> shouldBeGreaterThan 11.0
23 changes: 23 additions & 0 deletions tests/FsUnit.Xunit.Test/typed.shouldBeSmallerThanTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace FsUnit.Typed.Test

open Xunit
open FsUnitTyped

type ``shouldBeSmallerThan tests``() =
[<Fact>]
member __.``10 should be less than 11``() =
10 |> shouldBeSmallerThan 11

[<Fact>]
member __.``10 should not be less than 10``() =
(fun () -> 10 |> shouldBeSmallerThan 10)
|> shouldFail<System.NullReferenceException>
Smaug123 marked this conversation as resolved.
Show resolved Hide resolved

[<Fact>]
member __.``10[dot]0 should be less than 10[dot]1``() =
10.0 |> shouldBeSmallerThan 10.1

[<Fact>]
member __.``10[dot]0 should not be less than 10[dot]0``() =
(fun () -> 10.0 |> shouldBeSmallerThan 10.0)
|> shouldFail<System.NullReferenceException>
Smaug123 marked this conversation as resolved.
Show resolved Hide resolved
53 changes: 53 additions & 0 deletions tests/FsUnit.Xunit.Test/typed.shouldContainTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace FsUnit.Typed.Test

open Xunit
open FsUnitTyped

type ``shouldContain tests``() =
[<Fact>]
member __.``List with item should contain item``() =
[ 1 ] |> shouldContain 1

[<Fact>]
member __.``empty List should fail to contain item``() =
shouldFail(fun () -> [] |> shouldContain 1)

[<Fact>]
member __.``empty List should not contain item``() =
[] |> shouldNotContain 1

[<Fact>]
member __.``List with item should fail to not contain item``() =
shouldFail(fun () -> [ 1 ] |> shouldNotContain 1)

[<Fact>]
member __.``Array with item should contain item``() =
[| 1 |] |> shouldContain 1

[<Fact>]
member __.``empty Array should fail to contain item``() =
shouldFail(fun () -> [||] |> shouldContain 1)

[<Fact>]
member __.``empty Array should not contain item``() =
[||] |> shouldNotContain 1

[<Fact>]
member __.``Array with item should fail to not contain item``() =
shouldFail(fun () -> [| 1 |] |> shouldNotContain 1)

[<Fact>]
member __.``Seq with item should contain item``() =
seq { 1 } |> shouldContain 1

[<Fact>]
member __.``empty Seq should fail to contain item``() =
shouldFail(fun () -> Seq.empty |> shouldContain 1)

[<Fact>]
member __.``empty Seq should not contain item``() =
Seq.empty |> shouldNotContain 1

[<Fact>]
member __.``Seq with item should fail to not contain item``() =
shouldFail(fun () -> seq { 1 } |> shouldNotContain 1)
13 changes: 13 additions & 0 deletions tests/FsUnit.Xunit.Test/typed.shouldContainText.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace FsUnit.Typed.Test

open Xunit
open FsUnitTyped

type ``shouldContainText tests``() =
[<Fact>]
member __.``empty string should contain ""``() =
"" |> shouldContainText ""

[<Fact>]
member __.``ships should contain hip``() =
"ships" |> shouldContainText "hip"
13 changes: 13 additions & 0 deletions tests/FsUnit.Xunit.Test/typed.shouldEqualNullTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace FsUnit.Typed.Test

open Xunit
open FsUnitTyped

type ``Typed: shouldEqual null tests``() =
[<Fact>]
member __.``null should be null``() =
null |> shouldEqual null

[<Fact>]
member __.``null should fail to not be null``() =
shouldFail(fun () -> null |> shouldNotEqual null)
Loading