Skip to content

Commit 660894e

Browse files
committed
Adds IsNull() and IsNotNull() for all nullable types.
1 parent 737d407 commit 660894e

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

NFluent.35.Tests/ObjectRelatedTest.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
namespace NFluent.Tests
1717
{
18+
using NFluent.Tests.Extensions;
19+
1820
using NUnit.Framework;
1921

2022
[TestFixture]
@@ -41,11 +43,59 @@ public void IsDistinctWorks()
4143
}
4244

4345
[Test]
44-
public void IsNullWork()
46+
public void IsNullWorks()
4547
{
4648
Check.That((object)null).IsNull();
4749
}
4850

51+
[Test]
52+
public void IsNullWorksWithNullable()
53+
{
54+
Check.That((int?)null).IsNull();
55+
}
56+
57+
[Test]
58+
public void IsNotNullWorksWithNullable()
59+
{
60+
Mood? goodMood = new Mood();
61+
Check.That(goodMood).IsNotNull();
62+
}
63+
64+
[Test]
65+
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked nullable value is null whereas it must not.")]
66+
public void IsNotNullThrowsExceptionWithNullNullable()
67+
{
68+
Check.That((Mood?)null).IsNotNull();
69+
}
70+
71+
[Test]
72+
public void NotIsNullWorksWithNullable()
73+
{
74+
Mood? goodMood = new Mood();
75+
Check.That(goodMood).Not.IsNull();
76+
}
77+
78+
[Test]
79+
public void NotIsNotNullWorksWithNullable()
80+
{
81+
Check.That((Mood?)null).Not.IsNotNull();
82+
}
83+
84+
[Test]
85+
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked nullable value must be null.\nThe checked value:\n\t[NFluent.Tests.Extensions.Mood]")]
86+
public void NotIsNotNullThrowsWithNonNullNullable()
87+
{
88+
Mood? goodMood = new Mood();
89+
Check.That(goodMood).Not.IsNotNull();
90+
}
91+
92+
[Test]
93+
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked nullable value is null whereas it must not.")]
94+
public void NotIsNullThrowsExceptionWithNullNullable()
95+
{
96+
Check.That((Mood?)null).Not.IsNull();
97+
}
98+
4999
[Test]
50100
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked object must be null.\nThe checked object:\n\t[System.Object]")]
51101
public void IsNullFailsProperly()

NFluent.35/Assertions/ObjectCheckExtensions.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static ICheckLink<ICheck<object>> InheritsFrom<T>(this ICheck<object> che
183183
/// </summary>
184184
/// <param name="check">The fluent check to be extended.</param>
185185
/// <returns>A check link.</returns>
186-
/// <exception cref="FluentCheckException">Is the value is not null.</exception>
186+
/// <exception cref="FluentCheckException">The checked value is not null.</exception>
187187
public static ICheckLink<ICheck<object>> IsNull(this ICheck<object> check)
188188
{
189189
var checker = ExtensibilityHelper.ExtractChecker(check);
@@ -199,6 +199,48 @@ public static ICheckLink<ICheck<object>> IsNull(this ICheck<object> check)
199199
return new CheckLink<ICheck<object>>(check);
200200
}
201201

202+
/// <summary>
203+
/// Checks that the actual Nullable value is null.
204+
/// </summary>
205+
/// <param name="check">The fluent check to be extended.</param>
206+
/// <returns>A check link.</returns>
207+
/// <exception cref="FluentCheckException">The checked value is not null.</exception>
208+
public static ICheckLink<ICheck<T?>> IsNull<T>(this ICheck<T?> check) where T : struct
209+
{
210+
var checker = ExtensibilityHelper.ExtractChecker(check);
211+
return checker.ExecuteCheck(
212+
() =>
213+
{
214+
if (checker.Value != null)
215+
{
216+
var message = FluentMessage.BuildMessage("The checked nullable value must be null.").On(checker.Value).ToString();
217+
throw new FluentCheckException(message);
218+
}
219+
}
220+
, FluentMessage.BuildMessage("The checked nullable value is null whereas it must not.").ToString());
221+
}
222+
223+
/// <summary>
224+
/// Checks that the actual Nullable value is not null.
225+
/// </summary>
226+
/// <param name="check">The fluent check to be extended.</param>
227+
/// <returns>A check link.</returns>
228+
/// <exception cref="FluentCheckException">The checked value is null.</exception>
229+
public static ICheckLink<ICheck<T?>> IsNotNull<T>(this ICheck<T?> check) where T : struct
230+
{
231+
var checker = ExtensibilityHelper.ExtractChecker(check);
232+
return checker.ExecuteCheck(
233+
() =>
234+
{
235+
if (checker.Value == null)
236+
{
237+
var message = FluentMessage.BuildMessage("The checked nullable value is null whereas it must not.").ToString();
238+
throw new FluentCheckException(message);
239+
}
240+
}
241+
, FluentMessage.BuildMessage("The checked nullable value must be null.").On(checker.Value).ToString());
242+
}
243+
202244
/// <summary>
203245
/// Checks that the actual expression is not null.
204246
/// </summary>

ReleaseNoteContentToBeInsertedWithinNuspecFile.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
New feature(s):
2+
* Now supports IsNull() and IsNotNull() checks for nullables (thanks to Mendel Monteiro Beckerman for that).
23
* Check.ThatCode(...): New entry point for checks on code (Action and Func&lt;T&gt;). It supersedes the equivalent Check.That signature.
34

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

1314
--------------
1415
Bug Fixe(s):

0 commit comments

Comments
 (0)