Skip to content

Commit

Permalink
Add typed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Smaug123 committed Mar 31, 2022
1 parent b117a79 commit bd20cb8
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 0 deletions.
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<AssertionException>

[<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<AssertionException>
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)
133 changes: 133 additions & 0 deletions tests/FsUnit.Xunit.Test/typed.shouldEqualTests.fs
Original file line number Diff line number Diff line change
@@ -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)

[<Fact>]
member __.``value type should equal equivalent value``() =
1 |> shouldEqual 1

[<Fact>]
member __.``value type should fail to equal nonequivalent value``() =
shouldFail(fun () -> 1 |> shouldEqual 2)

[<Fact>]
member __.``value type should not equal nonequivalent value``() =
1 |> shouldNotEqual 2

[<Fact>]
member __.``value type should fail to not equal equivalent value``() =
shouldFail(fun () -> 1 |> shouldNotEqual 1)

[<Fact>]
member __.``reference type should equal itself``() =
anObj |> shouldEqual anObj

[<Fact>]
member __.``reference type should fail to equal other``() =
shouldFail(fun () -> anObj |> shouldEqual otherObj)

[<Fact>]
member __.``reference type should not equal other``() =
anObj |> shouldNotEqual otherObj

[<Fact>]
member __.``reference type should fail to not equal itself``() =
shouldFail(fun () -> anObj |> shouldNotEqual anObj)

[<Fact>]
member __.``should pass when Equals returns true``() =
anObj |> shouldEqual(box(new AlwaysEqual()))

[<Fact>]
member __.``should fail when Equals returns false``() =
shouldFail(fun () -> anObj |> shouldEqual(box(new NeverEqual())))

[<Fact>]
member __.``should pass when negated and Equals returns false``() =
anObj |> shouldNotEqual(box(new NeverEqual()))

[<Fact>]
member __.``should fail when negated and Equals returns true``() =
shouldFail(fun () -> anObj |> shouldNotEqual(box(new AlwaysEqual())))

[<Fact>]
member __.``None should equal None``() =
None |> shouldEqual None

[<Fact>]
member __.``Error "Foo" should equal Error "Foo"``() =
Error "Foo" |> shouldEqual(Error "Foo")

[<Fact>]
member __.``Error "Foo" should equal fails and have same message``() =
(fun () -> Error "Foo" |> shouldEqual(Error "Bar"))
|> Assert.Throws<AssertionException>
|> fun e ->
e.Message
|> should
equal
(sprintf " Expected: Error \"Bar\" or Error \"Bar\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine)

[<Fact>]
member __.``Error "Foo" should not equal Error "Bar"``() =
Error "Foo" |> shouldNotEqual(Error "Bar")

[<Fact>]
member __.``Error "Foo" should not equal Error "Bar" fails and have same message``() =
(fun () -> Error "Foo" |> shouldNotEqual(Error "Foo"))
|> Assert.Throws<AssertionException>
|> fun e ->
e.Message
|> should
equal
(sprintf " Expected: not Error \"Foo\" or Error \"Foo\"%s But was: Error \"Foo\"%s" Environment.NewLine Environment.NewLine)

[<Fact>]
member this.``structural equality``() =
let actualList: char list = []
[ (actualList, "") ] |> shouldEqual [ ([], "") ]

[<Fact>]
member __.``Empty obj list should match itself``() =
[] |> shouldEqual []

[<Fact>]
member __.``List with elements should not match empty list``() =
[ 1 ] |> shouldNotEqual []

[<Fact>]
member __.``structural value type should equal equivalent value``() =
anImmutableArray |> shouldEqual equivalentImmutableArray

[<Fact>]
member __.``structural value type should not equal non-equivalent value``() =
anImmutableArray |> shouldNotEqual otherImmutableArray

[<Fact>]
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"))

21 changes: 21 additions & 0 deletions tests/FsUnit.Xunit.Test/typed.shouldFailTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace FsUnit.Typed.Test

open Xunit
open FsUnitTyped

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

[<Fact>]
member __.``non-null should fail to be Null``() =
shouldFail(fun () -> "something" |> shouldEqual null)

[<Fact>]
member __.``shouldFail should fail when everything is OK``() =
shouldFail(fun () -> shouldFail id)

[<Fact>]
member __.``Simplify "should throw"``() =
(fun () -> failwith "BOOM!") |> shouldFail<System.Exception>

0 comments on commit bd20cb8

Please sign in to comment.