Browse Source

Added Guard for true and false.

Former-commit-id: 0036dcc3316425aa6709918eb22f08d6a95c76d0
Former-commit-id: e928511040809e280a1e638a4341403d15f1ed32
Former-commit-id: cfe1b6808c914762286fcd23a95dca444cdaa954
af/merge-core
dirk 10 years ago
parent
commit
3f75c330b1
  1. 58
      src/ImageProcessorCore/Common/Helpers/Guard.cs
  2. 38
      tests/ImageProcessorCore.Tests/Helpers/GuardTests.cs

58
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}.");
}
}
/// <summary>
/// Verifies, that the method parameter with specified target value is true
/// and throws an exception if it is found to be so.
/// </summary>
/// <param name="target">
/// The target value, which cannot be false.
/// </param>
/// <param name="parameterName">
/// The name of the parameter that is to be checked.
/// </param>
/// <param name="message">
/// The error message, if any to add to the exception.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="target"/> is null
/// </exception>
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);
}
}
/// <summary>
/// Verifies, that the method parameter with specified target value is false
/// and throws an exception if it is found to be so.
/// </summary>
/// <param name="target">
/// The target value, which cannot be true.
/// </param>
/// <param name="parameterName">
/// The name of the parameter that is to be checked.
/// </param>
/// <param name="message">
/// The error message, if any to add to the exception.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="target"/> is null
/// </exception>
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);
}
}
}
}

38
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);
}
/// <summary>
/// Tests that the <see cref="M:Guard.IsTrue"/> method throws when the argument is false.
/// </summary>
[Fact]
public void IsTrueThrowsWhenArgIsFalse()
{
Assert.Throws<ArgumentException>(() => Guard.IsTrue(false, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.IsTrue"/> method does not throw when the argument is true.
/// </summary>
[Fact]
public void IsTrueDoesThrowsWhenArgIsTrue()
{
Exception ex = Record.Exception(() => Guard.IsTrue(true, "foo"));
Assert.Null(ex);
}
/// <summary>
/// Tests that the <see cref="M:Guard.IsFalse"/> method throws when the argument is true.
/// </summary>
[Fact]
public void IsFalseThrowsWhenArgIsFalse()
{
Assert.Throws<ArgumentException>(() => Guard.IsFalse(true, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.IsFalse"/> method does not throw when the argument is false.
/// </summary>
[Fact]
public void IsFalseDoesThrowsWhenArgIsTrue()
{
Exception ex = Record.Exception(() => Guard.IsFalse(false, "foo"));
Assert.Null(ex);
}
}
}
Loading…
Cancel
Save