-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from sergey-tihon/typed
Added FsUnitTyped module + tests
- Loading branch information
Showing
15 changed files
with
435 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
(*** hide ***) | ||
// This block of code is omitted in the generated HTML documentation. Use | ||
// it to define helpers that you do not want to show in the documentation. | ||
#I "../../bin/FsUnit.NUnit/" | ||
#r "NUnit.Framework.dll" | ||
#r "FsUnit.NUnit.dll" | ||
|
||
(** | ||
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`). | ||
No more untyped constrains and tests like | ||
1 |> should equal "1" | ||
FsUnitTyped from NuGet | ||
----------------------- | ||
The `FsUnit.Typed` is part of `FsUnit` package for NUnit and can be [installed from NuGet](https://nuget.org/packages/FsUnit). | ||
FsUnitTyped with Paket | ||
----------------------- | ||
`FsUnitTyped` supports lightweight usage scenario with [Paket](http://fsprojects.github.io/Paket/). | ||
In the case when you do not want to add dependency on [FsUnit](https://www.nuget.org/packages/FsUnit/) | ||
package to your project, you can add reference to [FsUnitTyped.fs](https://github.com/fsprojects/FsUnit/blob/master/src/FsUnit.NUnit/FsUnit.Typed.fs) | ||
file and [NUnit](https://www.nuget.org/packages/NUnit/) package. | ||
Example of `paket.dependencies` file: | ||
[lang=paket] | ||
source https://nuget.org/api/v2 | ||
nuget FSharp.Core | ||
github fsprojects/FsUnit src/FsUnit.NUnit/FsUnitTyped.fs | ||
group Test | ||
source https://nuget.org/api/v2 | ||
nuget NUnit.Console | ||
nuget NUnit | ||
Example of `paket.reference` file for test projects: | ||
[lang=paket] | ||
File:FsUnitTyped.fs | ||
group Test | ||
NUnit | ||
Syntax | ||
------- | ||
With FsUnitTyped, you can write unit tests like this: | ||
*) | ||
|
||
open NUnit.Framework | ||
open FsUnitTyped | ||
|
||
(** | ||
One object equals or does not equal another: | ||
*) | ||
|
||
1 |> shouldEqual 1 | ||
1 |> shouldNotEqual 2 | ||
|
||
(** | ||
One comparable value greater or smaller than another: | ||
*) | ||
|
||
11 |> shouldBeGreaterThan 10 | ||
10 |> shouldBeSmallerThan 11 | ||
|
||
|
||
(** | ||
A string contains specified substring: | ||
*) | ||
"ships" |> shouldContainText "hip" | ||
|
||
(** | ||
A List, Seq, or Array instance contains, does not contain a value or empty: | ||
*) | ||
[1] |> shouldContain 1 | ||
[] |> shouldNotContain 1 | ||
[] |> shouldBeEmpty | ||
|
||
(** | ||
A List or Array instance has a certain length: | ||
*) | ||
[|1;2;3;4|] |> shouldHaveLength 4 | ||
|
||
(** | ||
A function should throw a certain type of exception: | ||
*) | ||
(fun _ -> failwith "BOOM!") |> shouldFail<System.Exception> | ||
|
||
(** | ||
A function should fail | ||
*) | ||
shouldFail (fun _ -> 5/0 |> ignore) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
namespace FsUnitTyped | ||
|
||
open System.Diagnostics | ||
open NUnit.Framework | ||
open System.Collections.Generic | ||
|
||
[<AutoOpen>] | ||
module TopLevelOperators = | ||
[<DebuggerStepThrough>] | ||
let shouldEqual (expected : 'a) (actual : 'a) = | ||
Assert.IsTrue((expected = actual), sprintf "Expected: %A\nActual: %A" expected actual) | ||
|
||
[<DebuggerStepThrough>] | ||
let shouldNotEqual (expected : 'a) (actual : 'a) = | ||
Assert.IsFalse((expected = actual), sprintf "Expected: %A\nActual: %A" expected actual) | ||
|
||
[<DebuggerStepThrough>] | ||
let shouldContain (x : 'a) (y : 'a seq) = | ||
let list = List<_>() | ||
for a in y do | ||
list.Add a | ||
Assert.Contains(x, list) | ||
|
||
[<DebuggerStepThrough>] | ||
let shouldBeEmpty (list : 'a seq) = | ||
Assert.IsEmpty(list) | ||
|
||
[<DebuggerStepThrough>] | ||
let shouldNotContain (x : 'a) (y : 'a seq) = | ||
if Seq.exists ((=) x) y then failwithf "Seq %A should not contain %A" y x | ||
|
||
[<DebuggerStepThrough>] | ||
let shouldBeSmallerThan (x : 'a) (y : 'a) = | ||
Assert.GreaterOrEqual(x, y, sprintf "Expected: %A\nActual: %A" x y) | ||
|
||
[<DebuggerStepThrough>] | ||
let shouldBeGreaterThan (x : 'a) (y : 'a) = | ||
Assert.Greater(y, x, sprintf "Expected: %A\nActual: %A" x y) | ||
|
||
[<DebuggerStepThrough>] | ||
let shouldFail<'exn when 'exn :> exn> (f : unit -> unit) = | ||
let succeeded = ref false | ||
try | ||
f() | ||
succeeded := true | ||
with | ||
| exn -> | ||
if exn :? 'exn then () else | ||
failwithf "Exception was not of type %s" <| typeof<'exn>.ToString() | ||
if !succeeded then | ||
failwith "Operation did not fail." | ||
|
||
[<DebuggerStepThrough>] | ||
let shouldContainText (x : string) (y : string) = | ||
if y.Contains(x) |> not then | ||
failwithf "\"%s\" is not a substring of \"%s\"" x y | ||
|
||
[<DebuggerStepThrough>] | ||
let shouldHaveLength expected list = | ||
let actual = Seq.length list | ||
if actual <> expected then | ||
failwithf "Invalid length in %A\r\nExpected: %i\r\nActual: %i" list expected actual |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace FsUnit.Typed.Test | ||
open NUnit.Framework | ||
open FsUnitTyped | ||
|
||
[<TestFixture>] | ||
type ``shouldBeEmpty tests`` ()= | ||
[<Test>] member test. | ||
``empty List should be Empty`` ()= | ||
[] |> shouldBeEmpty | ||
|
||
[<Test>] member test. | ||
``non-empty List should fail to be Empty`` ()= | ||
shouldFail (fun () -> [1] |> shouldBeEmpty) | ||
|
||
[<Test>] member test. | ||
``empty Array should be Empty`` ()= | ||
[||] |> shouldBeEmpty | ||
|
||
[<Test>] member test. | ||
``non-empty Array should fail to be Empty`` ()= | ||
shouldFail (fun () -> [|1|] |> shouldBeEmpty) | ||
|
||
[<Test>] member test. | ||
``empty Seq should be Empty`` ()= | ||
Seq.empty |> shouldBeEmpty | ||
|
||
[<Test>] member test. | ||
``non-empty Seq should fail to be Empty`` ()= | ||
shouldFail (fun () -> seq { yield 1 } |> shouldBeEmpty) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace FsUnit.Typed.Test | ||
open NUnit.Framework | ||
open FsUnitTyped | ||
|
||
[<TestFixture>] | ||
type ``haveLength tests`` ()= | ||
// F# List | ||
[<Test>] member test. | ||
``List with 1 item should have Length 1`` ()= | ||
[1] |> shouldHaveLength 1 | ||
|
||
[<Test>] member test. | ||
``empty List should fail to have Length 1`` ()= | ||
shouldFail (fun () -> [] |> shouldHaveLength 1) | ||
|
||
// Array | ||
[<Test>] member test. | ||
``Array with 1 item should have Length 1`` ()= | ||
[|1|] |> shouldHaveLength 1 | ||
|
||
[<Test>] member test. | ||
``empty Array should fail to have Length 1`` ()= | ||
shouldFail (fun () -> [||] |> shouldHaveLength 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace FsUnit.Typed.Test | ||
open NUnit.Framework | ||
open FsUnitTyped | ||
|
||
[<TestFixture>] | ||
type ``shouldBeGreaterThan tests`` ()= | ||
[<Test>] member test. | ||
``11 should be greater than 10`` ()= | ||
11 |> shouldBeGreaterThan 10 | ||
|
||
[<Test>] member test. | ||
``11.1 should be greater than 11.0`` ()= | ||
11.1 |> shouldBeGreaterThan 11.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace FsUnit.Typed.Test | ||
open NUnit.Framework | ||
open FsUnitTyped | ||
|
||
[<TestFixture>] | ||
type ``shouldBeSmallerThan tests`` ()= | ||
[<Test>] member test. | ||
``10 should be less than 11`` ()= | ||
10 |> shouldBeSmallerThan 11 | ||
|
||
[<Test>] member test. | ||
``10.0 should be less than 10.1`` ()= | ||
10.0 |> shouldBeSmallerThan 10.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace FsUnit.Typed.Test | ||
open NUnit.Framework | ||
open FsUnitTyped | ||
|
||
[<TestFixture>] | ||
type ``shouldContain tests`` ()= | ||
[<Test>] member test. | ||
``List with item should contain item`` ()= | ||
[1] |> shouldContain 1 | ||
|
||
[<Test>] member test. | ||
``empty List should fail to contain item`` ()= | ||
shouldFail (fun () -> [] |> shouldContain 1) | ||
|
||
[<Test>] member test. | ||
``empty List should not contain item`` ()= | ||
[] |> shouldNotContain 1 | ||
|
||
[<Test>] member test. | ||
``List with item should fail to not contain item`` ()= | ||
shouldFail (fun () -> [1] |> shouldNotContain 1) | ||
|
||
[<Test>] member test. | ||
``Array with item should contain item`` ()= | ||
[|1|] |> shouldContain 1 | ||
|
||
[<Test>] member test. | ||
``empty Array should fail to contain item`` ()= | ||
shouldFail (fun () -> [||] |> shouldContain 1) | ||
|
||
[<Test>] member test. | ||
``empty Array should not contain item`` ()= | ||
[||] |> shouldNotContain 1 | ||
|
||
[<Test>] member test. | ||
``Array with item should fail to not contain item`` ()= | ||
shouldFail (fun () -> [|1|] |> shouldNotContain 1) | ||
|
||
[<Test>] member test. | ||
``Seq with item should contain item`` ()= | ||
seq { yield 1 } |> shouldContain 1 | ||
|
||
[<Test>] member test. | ||
``empty Seq should fail to contain item`` ()= | ||
shouldFail (fun () -> Seq.empty |> shouldContain 1) | ||
|
||
[<Test>] member test. | ||
``empty Seq should not contain item`` ()= | ||
Seq.empty |> shouldNotContain 1 | ||
|
||
[<Test>] member test. | ||
``Seq with item should fail to not contain item`` ()= | ||
shouldFail (fun () -> seq { yield 1 } |> shouldNotContain 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace FsUnit.Typed.Test | ||
open NUnit.Framework | ||
open FsUnitTyped | ||
|
||
[<TestFixture>] | ||
type ``shouldContainText tests`` ()= | ||
[<Test>] member test. | ||
``empty string should contain ""`` ()= | ||
"" |> shouldContainText "" | ||
|
||
[<Test>] member test. | ||
``ships should contain hip`` ()= | ||
"ships" |> shouldContainText "hip" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace FsUnit.Typed.Test | ||
open NUnit.Framework | ||
open FsUnitTyped | ||
|
||
[<TestFixture>] | ||
type ``Typed: shouldEqual null tests`` ()= | ||
[<Test>] member test. | ||
``null should be null`` ()= | ||
null |> shouldEqual null | ||
|
||
[<Test>] member test. | ||
``null should fail to not be null`` ()= | ||
shouldFail (fun () -> null |> shouldNotEqual null) |
Oops, something went wrong.