From 3f75c330b1e16b63b441e1d2e6ac6e9af6efb426 Mon Sep 17 00:00:00 2001 From: dirk Date: Fri, 12 Aug 2016 14:29:50 +0200 Subject: [PATCH] Added Guard for true and false. Former-commit-id: 0036dcc3316425aa6709918eb22f08d6a95c76d0 Former-commit-id: e928511040809e280a1e638a4341403d15f1ed32 Former-commit-id: cfe1b6808c914762286fcd23a95dca444cdaa954 --- .../Common/Helpers/Guard.cs | 58 +++++++++++++++++++ .../Helpers/GuardTests.cs | 38 ++++++++++++ 2 files changed, 96 insertions(+) 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