Skip to content

Commit a6b70d3

Browse files
committed
Merge branch 'Add_Object_IsNull' of https://github.com/dupdob/NFluent into isNullIntegration
Conflicts: NFluent.Tests/EdgeCasesTest.cs
2 parents 997e09f + b0f113a commit a6b70d3

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

NFluent.Tests/EdgeCasesTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public void IsATest()
4141
[Test]
4242
public void NumberTypeChanges()
4343
{
44-
var test = 4L;
44+
const long Test = 4L;
4545

46-
Check.That(test).IsEqualTo(4);
46+
Check.That(Test).IsEqualTo(4);
4747
}
4848
}
4949
}

NFluent.Tests/ObjectRelatedTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ public void IsDistinctWorks()
4040
Check.That(new object()).IsDistinctFrom(new object());
4141
}
4242

43+
[Test]
44+
public void IsNullWork()
45+
{
46+
Check.That((object)null).IsNull();
47+
}
48+
49+
[Test]
50+
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked object must be null.\nThe checked object:\n\t[System.Object]")]
51+
public void IsNullFailsProperly()
52+
{
53+
Check.That(new object()).IsNull();
54+
}
55+
56+
[Test]
57+
public void IsNotNullWork()
58+
{
59+
Check.That(new object()).IsNotNull();
60+
}
61+
62+
[Test]
63+
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked object must not be null.\nThe checked object:\n\t[null]")]
64+
public void IsNotNullFailsProperly()
65+
{
66+
Check.That((object)null).IsNotNull();
67+
}
68+
4369
[Test]
4470
[ExpectedException(typeof(FluentCheckException), ExpectedMessage = "\nThe checked object must have be an instance distinct from expected one.\nThe checked object:\n\t[System.Object]\nThe expected object: distinct from\n\t[System.Object]")]
4571
public void IsDistinctFailsProperly()

NFluent/Assertions/ObjectCheckExtensions.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,58 @@ public static ICheckLink<ICheck<object>> InheritsFrom<T>(this ICheck<object> che
149149
string.Format("\nThe checked expression is part of the inheritance hierarchy or of the same type than the specified one.\nIndeed, checked expression type:\n\t[{0}]\nis a derived type of\n\t[{1}].", instanceType.ToStringProperlyFormated(), expectedBaseType.ToStringProperlyFormated()));
150150
}
151151

152+
/// <summary>
153+
/// Checks that the actual expression is null.
154+
/// </summary>
155+
/// <param name="check">The fluent check to be extended.</param>
156+
/// <returns>A check link.</returns>
157+
/// <exception cref="FluentCheckException">Is the value is not null.</exception>
158+
public static ICheckLink<ICheck<object>> IsNull(this ICheck<object> check)
159+
{
160+
var runnableCheck = check as IRunnableCheck<object>;
161+
var negated = runnableCheck.Negated;
162+
var value = runnableCheck.Value;
163+
164+
var message = IsNullImpl(value, negated);
165+
if (!string.IsNullOrEmpty(message))
166+
{
167+
throw new FluentCheckException(FluentMessage.BuildMessage(message).For("object").On(value).ToString());
168+
}
169+
170+
return new CheckLink<ICheck<object>>(check);
171+
}
172+
173+
/// <summary>
174+
/// Checks that the actual expression is not null.
175+
/// </summary>
176+
/// <param name="check">The fluent check to be extended.</param>
177+
/// <returns>A check link.</returns>
178+
/// <exception cref="FluentCheckException">Is the value is null.</exception>
179+
public static ICheckLink<ICheck<object>> IsNotNull(this ICheck<object> check)
180+
{
181+
var runnableCheck = check as IRunnableCheck<object>;
182+
var negated = runnableCheck.Negated;
183+
var value = runnableCheck.Value;
184+
185+
var message = IsNullImpl(value, !negated);
186+
if (!string.IsNullOrEmpty(message))
187+
{
188+
throw new FluentCheckException(FluentMessage.BuildMessage(message).For("object").On(value).ToString());
189+
}
190+
191+
return new CheckLink<ICheck<object>>(check);
192+
}
193+
194+
private static string IsNullImpl(object value, bool negated)
195+
{
196+
if (!negated)
197+
{
198+
return value == null ? null : "The {0} must be null.";
199+
}
200+
201+
return value == null ? "The {0} must not be null." : null;
202+
}
203+
152204
/// <summary>
153205
/// Checks that the actual value has an expected reference.
154206
/// </summary>

0 commit comments

Comments
 (0)