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

Added FsUnitTyped module + tests #85

Merged
merged 7 commits into from
Mar 7, 2016
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: 2 additions & 1 deletion FsUnit.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FsUnit.NUnit", "src\FsUnit.NUnit\FsUnit.NUnit.fsproj", "{3890DC0F-5225-4ADF-9B9E-F6A482046A9E}"
EndProject
Expand Down Expand Up @@ -32,6 +32,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "content", "content", "{0BEF7A55-F158-4691-886C-1AF2FE12DD7C}"
ProjectSection(SolutionItems) = preProject
docs\content\FsCheck.fsx = docs\content\FsCheck.fsx
docs\content\FsUnitTyped.fsx = docs\content\FsUnitTyped.fsx
docs\content\index.fsx = docs\content\index.fsx
docs\content\MbUnit.fsx = docs\content\MbUnit.fsx
docs\content\MsTest.fsx = docs\content\MsTest.fsx
Expand Down
102 changes: 102 additions & 0 deletions docs/content/FsUnitTyped.fsx
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)
11 changes: 5 additions & 6 deletions docs/tools/templates/template.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@
<li><a href="@Root/release-notes.html">Release Notes</a></li>

<li class="nav-header">Getting started</li>
<li><a href="@Root/NUnit.html">NUnit</a></li>
<li><a href="@Root/NUnit.html">NUnit Classic</a></li>
<li><a href="@Root/Paket.html">Lightweight FsUnit with Paket</a></li>
<li><a href="@Root/FsUnitTyped.html">FsUnitTyped</a></li>
<li class="divider"></li>
<li><a href="@Root/xUnit.html">xUnit</a></li>
<li><a href="@Root/MbUnit.html">MbUnit</a></li>
<li><a href="@Root/MsTest.html">MsTest</a></li>
<li class="divider"></li>
<li><a href="@Root/Paket.html">Paket</a></li>
<li><a href="@Root/FsCheck.html">FsCheck</a></li>

<li><a href="@Root/MbUnit.html">MbUnit</a></li>

<li class="nav-header">Documentation</li>
<li><a href="@Root/operators.html">Operators</a></li>
Expand Down
1 change: 1 addition & 0 deletions src/FsUnit.NUnit/FsUnit.NUnit.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="FsUnit.fs" />
<Compile Include="FsUnitTyped.fs" />
<Compile Include="GenericAssert.fs" />
<Compile Include="..\FsUnit.Xunit\CustomMatchers.fs">
<Link>CustomMatchers.fs</Link>
Expand Down
62 changes: 62 additions & 0 deletions src/FsUnit.NUnit/FsUnitTyped.fs
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
9 changes: 9 additions & 0 deletions tests/FsUnit.NUnit.Test/FsUnit.NUnit.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@
<Compile Include="instanceOfTests.fs" />
<Compile Include="NaNTests.fs" />
<Compile Include="beUniqueTests.fs" />
<Compile Include="typed.shouldEqualNullTests.fs" />
<Compile Include="typed.beEmptyTests.fs" />
<Compile Include="typed.shouldContainTests.fs" />
<Compile Include="typed.shouldEqualTests.fs" />
<Compile Include="typed.shouldBeGreaterThanTests.fs" />
<Compile Include="typed.shouldBeSmallerThanTests.fs" />
<Compile Include="typed.shouldFailTests.fs" />
<Compile Include="typed.haveLengthTests.fs" />
<Compile Include="typed.shouldContainText.fs" />
<None Include="paket.references" />
</ItemGroup>
<ItemGroup>
Expand Down
29 changes: 29 additions & 0 deletions tests/FsUnit.NUnit.Test/typed.beEmptyTests.fs
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)
23 changes: 23 additions & 0 deletions tests/FsUnit.NUnit.Test/typed.haveLengthTests.fs
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)
13 changes: 13 additions & 0 deletions tests/FsUnit.NUnit.Test/typed.shouldBeGreaterThanTests.fs
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
13 changes: 13 additions & 0 deletions tests/FsUnit.NUnit.Test/typed.shouldBeSmallerThanTests.fs
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
53 changes: 53 additions & 0 deletions tests/FsUnit.NUnit.Test/typed.shouldContainTests.fs
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)
14 changes: 14 additions & 0 deletions tests/FsUnit.NUnit.Test/typed.shouldContainText.fs
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"

13 changes: 13 additions & 0 deletions tests/FsUnit.NUnit.Test/typed.shouldEqualNullTests.fs
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)
Loading