diff --git a/src/ImageProcessorCore/Common/Helpers/Guard.cs b/src/ImageProcessorCore/Common/Helpers/Guard.cs index 96c7023d43..d11addf2a0 100644 --- a/src/ImageProcessorCore/Common/Helpers/Guard.cs +++ b/src/ImageProcessorCore/Common/Helpers/Guard.cs @@ -188,5 +188,63 @@ namespace ImageProcessorCore $"Value must be greater than or equal to {min} and less than or equal to {max}."); } } + + /// + /// Verifies, that the method parameter with specified target value is true + /// and throws an exception if it is found to be so. + /// + /// + /// The target value, which cannot be false. + /// + /// + /// The name of the parameter that is to be checked. + /// + /// + /// The error message, if any to add to the exception. + /// + /// + /// is null + /// + public static void IsTrue(bool target, string parameterName, string message = "") + { + if (!target) + { + if (string.IsNullOrWhiteSpace(message)) + { + throw new ArgumentException(parameterName, message); + } + + throw new ArgumentException(parameterName); + } + } + + /// + /// Verifies, that the method parameter with specified target value is false + /// and throws an exception if it is found to be so. + /// + /// + /// The target value, which cannot be true. + /// + /// + /// The name of the parameter that is to be checked. + /// + /// + /// The error message, if any to add to the exception. + /// + /// + /// is null + /// + public static void IsFalse(bool target, string parameterName, string message = "") + { + if (target) + { + if (string.IsNullOrWhiteSpace(message)) + { + throw new ArgumentException(parameterName, message); + } + + throw new ArgumentException(parameterName); + } + } } } diff --git a/tests/ImageProcessorCore.Tests/Helpers/GuardTests.cs b/tests/ImageProcessorCore.Tests/Helpers/GuardTests.cs index 14b81708ff..7c33470c30 100644 --- a/tests/ImageProcessorCore.Tests/Helpers/GuardTests.cs +++ b/tests/ImageProcessorCore.Tests/Helpers/GuardTests.cs @@ -200,5 +200,43 @@ namespace ImageProcessorCore.Tests.Helpers Exception ex = Record.Exception(() => Guard.MustBeBetweenOrEqualTo(0, -1, 1, "foo")); Assert.Null(ex); } + + /// + /// Tests that the method throws when the argument is false. + /// + [Fact] + public void IsTrueThrowsWhenArgIsFalse() + { + Assert.Throws(() => Guard.IsTrue(false, "foo")); + } + + /// + /// Tests that the method does not throw when the argument is true. + /// + [Fact] + public void IsTrueDoesThrowsWhenArgIsTrue() + { + Exception ex = Record.Exception(() => Guard.IsTrue(true, "foo")); + Assert.Null(ex); + } + + /// + /// Tests that the method throws when the argument is true. + /// + [Fact] + public void IsFalseThrowsWhenArgIsFalse() + { + Assert.Throws(() => Guard.IsFalse(true, "foo")); + } + + /// + /// Tests that the method does not throw when the argument is false. + /// + [Fact] + public void IsFalseDoesThrowsWhenArgIsTrue() + { + Exception ex = Record.Exception(() => Guard.IsFalse(false, "foo")); + Assert.Null(ex); + } } } \ No newline at end of file