From 1d1b5d9ddf7d0d9ac6787750d42dfcb5df52f92e Mon Sep 17 00:00:00 2001 From: Anton Firsov Date: Tue, 26 Jun 2018 09:34:15 +0200 Subject: [PATCH] make Guard.NotNull() and DebugGuard.NotNull() generic (#634) --- src/ImageSharp/Common/Helpers/DebugGuard.cs | 9 +++++---- src/ImageSharp/Common/Helpers/Guard.cs | 3 ++- tests/ImageSharp.Tests/Helpers/GuardTests.cs | 8 ++++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/DebugGuard.cs b/src/ImageSharp/Common/Helpers/DebugGuard.cs index 6dcd0fd27..5a1d3a2e3 100644 --- a/src/ImageSharp/Common/Helpers/DebugGuard.cs +++ b/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 /// and throws an exception if it is found to be so. /// - /// The target object, which cannot be null. + /// The target object, which cannot be null. /// The name of the parameter that is to be checked. - /// is null + /// is null [Conditional("DEBUG")] - public static void NotNull(object target, string parameterName) + public static void NotNull(T value, string parameterName) + where T : class { - if (target == null) + if (value == null) { throw new ArgumentNullException(parameterName); } diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index 011d7fdaa..d09079062 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -19,7 +19,8 @@ namespace SixLabors.ImageSharp /// The target object, which cannot be null. /// The name of the parameter that is to be checked. /// is null - public static void NotNull(object value, string parameterName) + public static void NotNull(T value, string parameterName) + where T : class { if (value == null) { diff --git a/tests/ImageSharp.Tests/Helpers/GuardTests.cs b/tests/ImageSharp.Tests/Helpers/GuardTests.cs index 42913e02d..0d1bb5ce9 100644 --- a/tests/ImageSharp.Tests/Helpers/GuardTests.cs +++ b/tests/ImageSharp.Tests/Helpers/GuardTests.cs @@ -12,13 +12,17 @@ namespace SixLabors.ImageSharp.Tests.Helpers /// public class GuardTests { + class Test + { + } + /// /// Tests that the method throws when the argument is null. /// [Fact] public void NotNullThrowsWhenArgIsNull() { - Assert.Throws(() => Guard.NotNull(null, "foo")); + Assert.Throws(() => Guard.NotNull((Test)null, "foo")); } /// @@ -27,7 +31,7 @@ namespace SixLabors.ImageSharp.Tests.Helpers [Fact] public void NotNullThrowsWhenArgNameEmpty() { - Assert.Throws(() => Guard.NotNull(null, string.Empty)); + Assert.Throws(() => Guard.NotNull((Test)null, string.Empty)); } ///