Browse Source
make Guard.NotNull() and DebugGuard.NotNull() generic (#634)
af/merge-core
Anton Firsov
8 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
13 additions and
7 deletions
-
src/ImageSharp/Common/Helpers/DebugGuard.cs
-
src/ImageSharp/Common/Helpers/Guard.cs
-
tests/ImageSharp.Tests/Helpers/GuardTests.cs
|
|
|
@ -17,13 +17,14 @@ namespace SixLabors.ImageSharp |
|
|
|
/// Verifies, that the method parameter with specified object value is not null
|
|
|
|
/// and throws an exception if it is found to be so.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="target">The target object, which cannot be null.</param>
|
|
|
|
/// <param name="value">The target object, which cannot be null.</param>
|
|
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null</exception>
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
|
|
|
|
[Conditional("DEBUG")] |
|
|
|
public static void NotNull(object target, string parameterName) |
|
|
|
public static void NotNull<T>(T value, string parameterName) |
|
|
|
where T : class |
|
|
|
{ |
|
|
|
if (target == null) |
|
|
|
if (value == null) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException(parameterName); |
|
|
|
} |
|
|
|
|
|
|
|
@ -19,7 +19,8 @@ namespace SixLabors.ImageSharp |
|
|
|
/// <param name="value">The target object, which cannot be null.</param>
|
|
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
|
|
|
|
public static void NotNull(object value, string parameterName) |
|
|
|
public static void NotNull<T>(T value, string parameterName) |
|
|
|
where T : class |
|
|
|
{ |
|
|
|
if (value == null) |
|
|
|
{ |
|
|
|
|
|
|
|
@ -12,13 +12,17 @@ namespace SixLabors.ImageSharp.Tests.Helpers |
|
|
|
/// </summary>
|
|
|
|
public class GuardTests |
|
|
|
{ |
|
|
|
class Test |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests that the <see cref="M:Guard.NotNull"/> method throws when the argument is null.
|
|
|
|
/// </summary>
|
|
|
|
[Fact] |
|
|
|
public void NotNullThrowsWhenArgIsNull() |
|
|
|
{ |
|
|
|
Assert.Throws<ArgumentNullException>(() => Guard.NotNull(null, "foo")); |
|
|
|
Assert.Throws<ArgumentNullException>(() => Guard.NotNull((Test)null, "foo")); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -27,7 +31,7 @@ namespace SixLabors.ImageSharp.Tests.Helpers |
|
|
|
[Fact] |
|
|
|
public void NotNullThrowsWhenArgNameEmpty() |
|
|
|
{ |
|
|
|
Assert.Throws<ArgumentNullException>(() => Guard.NotNull(null, string.Empty)); |
|
|
|
Assert.Throws<ArgumentNullException>(() => Guard.NotNull((Test)null, string.Empty)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|