Skip to content

Commit

Permalink
fsprojects#52 - add support for null in should clause
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-bogomaz committed Dec 1, 2015
1 parent a764385 commit 0e4f3b2
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/FsUnit.MbUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ let inline should (f : 'a -> ^b) x (y : obj) =
match y with
| :? (unit -> unit) as assertFunc -> box assertFunc
| _ -> y
Assert.That(y, c)
if box c = null then
Assert.That(y, IsNull())
else
Assert.That(y, c)

let inline shouldFail (f:unit->unit) =
let failed =
Expand All @@ -27,7 +30,8 @@ let equal expected = CustomMatchers.equal expected

let equalWithin (tolerance:obj) (expected:obj) = CustomMatchers.equalWithin tolerance expected

let not' (expected:obj) = CustomMatchers.not' expected
let not' (expected:obj) =
if box expected = null then CustomMatchers.not' (IsNull()) else (CustomMatchers.not' expected)

let throw (t:Type) = CustomMatchers.throw t

Expand Down
8 changes: 6 additions & 2 deletions src/FsUnit.MsTestUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ let inline should (f : 'a -> ^b) x (y : obj) =
match y with
| :? (unit -> unit) as assertFunc -> box assertFunc
| _ -> y
Assert.That(y, c)
if box c = null then
Assert.That(y, IsNull())
else
Assert.That(y, c)

let inline shouldFail (f:unit->unit) =
let failed =
Expand All @@ -37,7 +40,8 @@ let equal expected = CustomMatchers.equal expected

let equalWithin (tolerance:obj) (expected:obj) = CustomMatchers.equalWithin tolerance expected

let not' (expected:obj) = CustomMatchers.not' expected
let not' (expected:obj) =
if box expected = null then CustomMatchers.not' (IsNull()) else (CustomMatchers.not' expected)

let throw (t:Type) = CustomMatchers.throw t

Expand Down
8 changes: 6 additions & 2 deletions src/FsUnit.NUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ module TopLevelOperators =
match y with
| :? (unit -> unit) -> box (TestDelegate(y :?> unit -> unit))
| _ -> y
Assert.That(y, c)
if box c = null then
Assert.That(y, Is.Null)
else
Assert.That(y, c)

let equal x = EqualsConstraint(x)

Expand Down Expand Up @@ -87,7 +90,8 @@ module TopLevelOperators =

let descending = Is.Ordered.Descending

let not' x = NotConstraint(x)
let not' x =
if box x = null then NotConstraint(Null) else NotConstraint(x)

/// Deprecated operators. These will be removed in a future version of FsUnit.
module FsUnitDeprecated =
Expand Down
8 changes: 6 additions & 2 deletions src/FsUnit.Xunit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ let inline should (f : 'a -> ^b) x (y : obj) =
match y with
| :? (unit -> unit) as assertFunc -> box assertFunc
| _ -> y
Assert.That(y, c)
if box c = null then
Assert.That(y, IsNull())
else
Assert.That(y, c)

let inline shouldFail (f:unit->unit) =
let failed =
Expand All @@ -41,7 +44,8 @@ let equal expected = CustomMatchers.equal expected

let equalWithin (tolerance:obj) (expected:obj) = CustomMatchers.equalWithin tolerance expected

let not' (expected:obj) = CustomMatchers.not' expected
let not' (expected:obj) =
if box expected = null then CustomMatchers.not' (IsNull()) else (CustomMatchers.not' expected)

let throw (t:Type) = CustomMatchers.throw t

Expand Down
23 changes: 20 additions & 3 deletions tests/FsUnit.MbUnit.Test/beNullTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,29 @@ type ``be Null tests`` ()=

[<Test>] member test.
``null should fail to not be Null`` ()=
null |> should be Null
shouldFail (fun () -> null |> should not' (be Null))

[<Test>] member test.
``non-null should fail to be Null`` ()=
"something" |> should not' (be Null)
``non-null should fail to be Null`` ()=
shouldFail (fun () -> "something" |> should be Null)

[<Test>] member test.
``non-null should not be Null`` ()=
"something" |> should not' (be Null)

[<Test>] member test.
``null should be null`` ()=
null |> should be null

[<Test>] member test.
``null should fail to not be null`` ()=
shouldFail (fun () -> null |> should not' (be null))

[<Test>] member test.
``non-null should fail to be null`` ()=
shouldFail (fun () -> "something" |> should be null)

[<Test>] member test.
``non-null should not be null`` ()=
"something" |> should not' (be null)

22 changes: 19 additions & 3 deletions tests/FsUnit.MsTest.Test/beNullTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,28 @@ type ``be Null tests`` ()=

[<TestMethod>] member test.
``null should fail to not be Null`` ()=
null |> should be Null
shouldFail (fun () -> null |> should not' (be Null))

[<TestMethod>] member test.
``non-null should fail to be Null`` ()=
"something" |> should not' (be Null)
``non-null should fail to be Null`` ()=
shouldFail (fun () -> "something" |> should be Null)

[<TestMethod>] member test.
``non-null should not be Null`` ()=
"something" |> should not' (be Null)

[<TestMethod>] member test.
``null should be null`` ()=
null |> should be null

[<TestMethod>] member test.
``null should fail to not be null`` ()=
shouldFail (fun () -> null |> should not' (be null))

[<TestMethod>] member test.
``non-null should fail to be null`` ()=
shouldFail (fun () -> "something" |> should be null)

[<TestMethod>] member test.
``non-null should not be null`` ()=
"something" |> should not' (be null)
18 changes: 17 additions & 1 deletion tests/FsUnit.NUnit.Test/beNullTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,25 @@ type ``be Null tests`` ()=
shouldFail (fun () -> null |> should not' (be Null))

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

[<Test>] member test.
``non-null should not be Null`` ()=
"something" |> should not' (be Null)

[<Test>] member test.
``null should be null`` ()=
null |> should be null

[<Test>] member test.
``null should fail to not be null`` ()=
shouldFail (fun () -> null |> should not' (be null))

[<Test>] member test.
``non-null should fail to be null`` ()=
shouldFail (fun () -> "something" |> should be null)

[<Test>] member test.
``non-null should not be null`` ()=
"something" |> should not' (be null)
22 changes: 19 additions & 3 deletions tests/FsUnit.Xunit.Test/beNullTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,28 @@ type ``be Null tests`` ()=

[<Fact>] member test.
``null should fail to not be Null`` ()=
null |> should be Null
shouldFail (fun () -> null |> should not' (be Null))

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

[<Fact>] member test.
``non-null should not be Null`` ()=
"something" |> should not' (be Null)

[<Fact>] member test.
``null should be null`` ()=
null |> should be null

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

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

[<Fact>] member test.
``non-null should not be null`` ()=
"something" |> should not' (be null)

0 comments on commit 0e4f3b2

Please sign in to comment.