Skip to content

Commit

Permalink
Adds IsNull() and IsNotNull() for all nullable types.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpierrain committed Feb 13, 2014
1 parent 737d407 commit 660894e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
52 changes: 51 additions & 1 deletion NFluent.35.Tests/ObjectRelatedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace NFluent.Tests
{
using NFluent.Tests.Extensions;

using NUnit.Framework;

[TestFixture]
Expand All @@ -41,11 +43,59 @@ public void IsDistinctWorks()
}

[Test]
public void IsNullWork()
public void IsNullWorks()
{
Check.That((object)null).IsNull();
}

[Test]
public void IsNullWorksWithNullable()
{
Check.That((int?)null).IsNull();
}

[Test]
public void IsNotNullWorksWithNullable()
{
Mood? goodMood = new Mood();
Check.That(goodMood).IsNotNull();
}

[Test]
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked nullable value is null whereas it must not.")]
public void IsNotNullThrowsExceptionWithNullNullable()
{
Check.That((Mood?)null).IsNotNull();
}

[Test]
public void NotIsNullWorksWithNullable()
{
Mood? goodMood = new Mood();
Check.That(goodMood).Not.IsNull();
}

[Test]
public void NotIsNotNullWorksWithNullable()
{
Check.That((Mood?)null).Not.IsNotNull();
}

[Test]
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked nullable value must be null.\nThe checked value:\n\t[NFluent.Tests.Extensions.Mood]")]
public void NotIsNotNullThrowsWithNonNullNullable()
{
Mood? goodMood = new Mood();
Check.That(goodMood).Not.IsNotNull();
}

[Test]
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked nullable value is null whereas it must not.")]
public void NotIsNullThrowsExceptionWithNullNullable()
{
Check.That((Mood?)null).Not.IsNull();
}

[Test]
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked object must be null.\nThe checked object:\n\t[System.Object]")]
public void IsNullFailsProperly()
Expand Down
44 changes: 43 additions & 1 deletion NFluent.35/Assertions/ObjectCheckExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static ICheckLink<ICheck<object>> InheritsFrom<T>(this ICheck<object> che
/// </summary>
/// <param name="check">The fluent check to be extended.</param>
/// <returns>A check link.</returns>
/// <exception cref="FluentCheckException">Is the value is not null.</exception>
/// <exception cref="FluentCheckException">The checked value is not null.</exception>
public static ICheckLink<ICheck<object>> IsNull(this ICheck<object> check)
{
var checker = ExtensibilityHelper.ExtractChecker(check);
Expand All @@ -199,6 +199,48 @@ public static ICheckLink<ICheck<object>> IsNull(this ICheck<object> check)
return new CheckLink<ICheck<object>>(check);
}

/// <summary>
/// Checks that the actual Nullable value is null.
/// </summary>
/// <param name="check">The fluent check to be extended.</param>
/// <returns>A check link.</returns>
/// <exception cref="FluentCheckException">The checked value is not null.</exception>
public static ICheckLink<ICheck<T?>> IsNull<T>(this ICheck<T?> check) where T : struct
{
var checker = ExtensibilityHelper.ExtractChecker(check);
return checker.ExecuteCheck(
() =>
{
if (checker.Value != null)
{
var message = FluentMessage.BuildMessage("The checked nullable value must be null.").On(checker.Value).ToString();
throw new FluentCheckException(message);
}
}
, FluentMessage.BuildMessage("The checked nullable value is null whereas it must not.").ToString());
}

/// <summary>
/// Checks that the actual Nullable value is not null.
/// </summary>
/// <param name="check">The fluent check to be extended.</param>
/// <returns>A check link.</returns>
/// <exception cref="FluentCheckException">The checked value is null.</exception>
public static ICheckLink<ICheck<T?>> IsNotNull<T>(this ICheck<T?> check) where T : struct
{
var checker = ExtensibilityHelper.ExtractChecker(check);
return checker.ExecuteCheck(
() =>
{
if (checker.Value == null)
{
var message = FluentMessage.BuildMessage("The checked nullable value is null whereas it must not.").ToString();
throw new FluentCheckException(message);
}
}
, FluentMessage.BuildMessage("The checked nullable value must be null.").On(checker.Value).ToString());
}

/// <summary>
/// Checks that the actual expression is not null.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion ReleaseNoteContentToBeInsertedWithinNuspecFile.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
New feature(s):
* Now supports IsNull() and IsNotNull() checks for nullables (thanks to Mendel Monteiro Beckerman for that).
* Check.ThatCode(...): New entry point for checks on code (Action and Func&lt;T&gt;). It supersedes the equivalent Check.That signature.

--------------
Expand All @@ -8,7 +9,7 @@ Change(s):
* The HasFieldsEqualToThose() check is now obsolete and should be replaced by HasFieldsWithSameValues() which now support anonymous classes as expected parameter.
* The HasFieldsNotEqualToThose() check is now obsolete and should be replaced by HasNotFieldsWithSameValues()
* Simplification of extensibility
* NFluent package is now signed
* We are so proud of this 1.1 version of NFuent; we decided to sign it.

--------------
Bug Fixe(s):
Expand Down

0 comments on commit 660894e

Please sign in to comment.