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

#52 - add support for null in should clause #80

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we could move implementation of not``and fix toequaltoCustomMatchers.fs` file that should be linked from all projects and do not copy parse implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, will try


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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we save prev behavior for backward compatibility as well?

null |> should be Null
null |> should be null  

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first 2 test of the test-class have exact same body, but different naming:

  • null should be Null
  • null should fail to not be Null

So they really test same thing.
There is the same situation in be Null tests for XUnit and MsTest.
If you take a loot at NUnit tests you'll see that first 2 tests are testing what is stated in their names:

[<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))

So I guess there should be no backward-compatibility issues and now test body corresponds to the test name, just like similar test for NUnit


[<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)