Browse Source

make Guard.NotNull() and DebugGuard.NotNull() generic

af/merge-core
Anton Firszov 8 years ago
parent
commit
6e0c827f48
  1. 9
      src/ImageSharp/Common/Helpers/DebugGuard.cs
  2. 3
      src/ImageSharp/Common/Helpers/Guard.cs
  3. 8
      tests/ImageSharp.Tests/Helpers/GuardTests.cs

9
src/ImageSharp/Common/Helpers/DebugGuard.cs

@ -17,13 +17,14 @@ namespace SixLabors.ImageSharp
/// Verifies, that the method parameter with specified object value is not null /// Verifies, that the method parameter with specified object value is not null
/// and throws an exception if it is found to be so. /// and throws an exception if it is found to be so.
/// </summary> /// </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> /// <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")] [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); throw new ArgumentNullException(parameterName);
} }

3
src/ImageSharp/Common/Helpers/Guard.cs

@ -19,7 +19,8 @@ namespace SixLabors.ImageSharp
/// <param name="value">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> /// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception> /// <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) if (value == null)
{ {

8
tests/ImageSharp.Tests/Helpers/GuardTests.cs

@ -12,13 +12,17 @@ namespace SixLabors.ImageSharp.Tests.Helpers
/// </summary> /// </summary>
public class GuardTests public class GuardTests
{ {
class Test
{
}
/// <summary> /// <summary>
/// Tests that the <see cref="M:Guard.NotNull"/> method throws when the argument is null. /// Tests that the <see cref="M:Guard.NotNull"/> method throws when the argument is null.
/// </summary> /// </summary>
[Fact] [Fact]
public void NotNullThrowsWhenArgIsNull() public void NotNullThrowsWhenArgIsNull()
{ {
Assert.Throws<ArgumentNullException>(() => Guard.NotNull(null, "foo")); Assert.Throws<ArgumentNullException>(() => Guard.NotNull((Test)null, "foo"));
} }
/// <summary> /// <summary>
@ -27,7 +31,7 @@ namespace SixLabors.ImageSharp.Tests.Helpers
[Fact] [Fact]
public void NotNullThrowsWhenArgNameEmpty() public void NotNullThrowsWhenArgNameEmpty()
{ {
Assert.Throws<ArgumentNullException>(() => Guard.NotNull(null, string.Empty)); Assert.Throws<ArgumentNullException>(() => Guard.NotNull((Test)null, string.Empty));
} }
/// <summary> /// <summary>

Loading…
Cancel
Save