Browse Source

[SL.Core] Added unit tests for the Guard class.

af/octree-no-pixelmap
Dirk Lemstra 9 years ago
parent
commit
856fc724c8
  1. 44
      src/SixLabors.Core/Helpers/Guard.cs
  2. 286
      tests/SixLabors.Core.Tests/Helpers/GuardTests.cs

44
src/SixLabors.Core/Helpers/Guard.cs

@ -56,7 +56,7 @@ namespace SixLabors
throw new ArgumentException(message, parameterName);
}
throw new ArgumentException("Value cannot be null or empty and cannot contain only blanks.", parameterName);
throw new ArgumentException("Value cannot be null, empty, or cannot contain only whitespace.", parameterName);
}
}
@ -92,7 +92,7 @@ namespace SixLabors
/// <param name="max">The maximum value.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <exception cref="ArgumentException">
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName)
@ -112,7 +112,7 @@ namespace SixLabors
/// <param name="max">The maximum value.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <exception cref="ArgumentException">
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName)
@ -132,7 +132,7 @@ namespace SixLabors
/// <param name="min">The minimum value.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <exception cref="ArgumentException">
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName)
@ -140,9 +140,7 @@ namespace SixLabors
{
if (value.CompareTo(min) <= 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
$"Value must be greater than {min}.");
throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than {min}.");
}
}
@ -154,7 +152,7 @@ namespace SixLabors
/// <param name="min">The minimum value.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <exception cref="ArgumentException">
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName)
@ -175,7 +173,7 @@ namespace SixLabors
/// <param name="max">The maximum value.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <exception cref="ArgumentException">
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="value"/> is less than the minimum value of greater than the maximum value.
/// </exception>
public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName)
@ -191,7 +189,7 @@ namespace SixLabors
/// 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">
/// <param name="value">
/// The target value, which cannot be false.
/// </param>
/// <param name="parameterName">
@ -201,11 +199,11 @@ namespace SixLabors
/// The error message, if any to add to the exception.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="target"/> is false
/// <paramref name="value"/> is false
/// </exception>
public static void IsTrue(bool target, string parameterName, string message)
public static void IsTrue(bool value, string parameterName, string message)
{
if (!target)
if (!value)
{
throw new ArgumentException(message, parameterName);
}
@ -215,35 +213,35 @@ namespace SixLabors
/// 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="value">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 true
/// <paramref name="value"/> is true
/// </exception>
public static void IsFalse(bool target, string parameterName, string message)
public static void IsFalse(bool value, string parameterName, string message)
{
if (target)
if (value)
{
throw new ArgumentException(message, parameterName);
}
}
/// <summary>
/// Verifies, that the `target` span has the length of 'minSpan', or longer.
/// Verifies, that the `target` span has the length of 'minLength', or longer.
/// </summary>
/// <typeparam name="T">The element type of the spans</typeparam>
/// <param name="target">The target span.</param>
/// <param name="value">The target span.</param>
/// <param name="minLength">The minimum length.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <exception cref="ArgumentException">
/// <paramref name="target"/> is true
/// The length of <paramref name="value"/> is less than <paramref name="minLength"/>.
/// </exception>
public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> target, int minLength, string parameterName)
public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> value, int minLength, string parameterName)
{
if (target.Length < minLength)
if (value.Length < minLength)
{
throw new ArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
throw new ArgumentException($"The size must be at least {minLength}.", parameterName);
}
}
}

286
tests/SixLabors.Core.Tests/Helpers/GuardTests.cs

@ -0,0 +1,286 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace SixLabors.Helpers.Tests
{
public class GuardTests
{
[Fact]
public void NotNull_TargetNotNull_ThrowsNoException()
{
Guard.NotNull("test", "myParamName");
}
[Fact]
public void NotNull_TargetNull_ThrowsException()
{
Assert.Throws<ArgumentNullException>(() =>
{
Guard.NotNull(null, "myParamName");
});
}
[Fact]
public void NotNull_TargetNullWithMessage_ThrowsException()
{
var exception = Assert.Throws<ArgumentNullException>(() =>
{
Guard.NotNull(null, "myParamName", "myTestMessage");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains("myTestMessage"));
}
[Fact]
public void NotNullOrEmpty_TargetNotNullOrEmpty_ThrowsNoException()
{
Guard.NotNullOrEmpty("test", "myParamName");
}
[Fact]
public void NotNullOrEmpty_TargetNull_ThrowsException()
{
Assert.Throws<ArgumentNullException>(() =>
{
Guard.NotNullOrEmpty(null, "myParamName");
});
}
[Fact]
public void NotNullOrEmpty_TargetWhitespace_ThrowsException()
{
Assert.Throws<ArgumentException>(() =>
{
Guard.NotNullOrEmpty("\n\n", "myParamName");
});
}
[Fact]
public void NotNullOrEmpty_TargetEmpty_ThrowsException()
{
var exception = Assert.Throws<ArgumentException>(() =>
{
Guard.NotNullOrEmpty(string.Empty, "myParamName");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains("Value cannot be null, empty, or cannot contain only whitespace."));
}
[Fact]
public void NotNullOrEmpty_TargetEmptyWithMessage_ThrowsException()
{
var exception = Assert.Throws<ArgumentException>(() =>
{
Guard.NotNullOrEmpty(string.Empty, "myParamName", "myTestMessage");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains("myTestMessage"));
}
[Fact]
public void NotNullOrEmptyIEnumerable_TargetNotNullOrEmpty_ThrowsNoException()
{
Guard.NotNullOrEmpty(new string[] { "test" }, "myParamName");
}
[Fact]
public void NotNullOrEmptyIEnumerable_TargetNull_ThrowsException()
{
Assert.Throws<ArgumentNullException>(() =>
{
Guard.NotNullOrEmpty((IEnumerable<string>)null, "myParamName");
});
}
[Fact]
public void NotNullOrEmptyIEnumerable_TargetEmpty_ThrowsException()
{
var exception = Assert.Throws<ArgumentException>(() =>
{
Guard.NotNullOrEmpty(new string[] { }, "myParamName");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains("Value cannot be empty."));
}
[Fact]
public void NotNullOrEmptyIEnumerable_TargetEmptyWithMessage_ThrowsException()
{
var exception = Assert.Throws<ArgumentException>(() =>
{
Guard.NotNullOrEmpty(new string[] { }, "myParamName", "myTestMessage");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains("myTestMessage"));
}
[Fact]
public void MustBeLessThan_IsLess_ThrowsNoException()
{
Guard.MustBeLessThan(0, 1, "myParamName");
}
[Theory]
[InlineData(2, 1)]
[InlineData(1, 1)]
public void MustBeLessThan_IsGreaterOrEqual_ThrowsNoException(int value, int max)
{
var exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
{
Guard.MustBeLessThan(value, max, "myParamName");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains($"Value must be less than {max}."));
}
[Theory]
[InlineData(0, 1)]
[InlineData(1, 1)]
public void MustBeLessThanOrEqualTo_IsLessOrEqual_ThrowsNoException(int value, int max)
{
Guard.MustBeLessThanOrEqualTo(value, max, "myParamName");
}
[Fact]
public void MustBeLessThanOrEqualTo_IsGreater_ThrowsNoException()
{
var exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
{
Guard.MustBeLessThanOrEqualTo(2, 1, "myParamName");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains($"Value must be less than or equal to 1."));
}
[Fact]
public void MustBeGreaterThan_IsGreater_ThrowsNoException()
{
Guard.MustBeGreaterThan(2, 1, "myParamName");
}
[Theory]
[InlineData(1, 2)]
[InlineData(1, 1)]
public void MustBeGreaterThan_IsLessOrEqual_ThrowsNoException(int value, int min)
{
var exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
{
Guard.MustBeGreaterThan(value, min, "myParamName");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains($"Value must be greater than {min}."));
}
[Theory]
[InlineData(2, 1)]
[InlineData(1, 1)]
public void MustBeGreaterThanOrEqualTo_IsGreaterOrEqual_ThrowsNoException(int value, int min)
{
Guard.MustBeGreaterThanOrEqualTo(value, min, "myParamName");
}
[Fact]
public void MustBeGreaterThanOrEqualTo_IsLess_ThrowsNoException()
{
var exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
{
Guard.MustBeGreaterThanOrEqualTo(1, 2, "myParamName");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains($"Value must be greater than or equal to 2."));
}
[Theory]
[InlineData(1, 1, 3)]
[InlineData(2, 1, 3)]
[InlineData(3, 1, 3)]
public void MustBeBetweenOrEqualTo_IsBetweenOrEqual_ThrowsNoException(int value, int min, int max)
{
Guard.MustBeBetweenOrEqualTo(value, min, max, "myParamName");
}
[Theory]
[InlineData(0, 1, 3)]
[InlineData(4, 1, 3)]
public void MustBeBetweenOrEqualTo_IsLessOrGreater_ThrowsNoException(int value, int min, int max)
{
var exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
{
Guard.MustBeBetweenOrEqualTo(value, min, max, "myParamName");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains($"Value must be greater than or equal to {min} and less than or equal to {max}."));
}
[Fact]
public void IsTrue_IsTrue_ThrowsNoException()
{
Guard.IsTrue(true, "myParamName", "myTestMessage");
}
[Fact]
public void IsTrue_IsFalse_ThrowsException()
{
var exception = Assert.Throws<ArgumentException>(() =>
{
Guard.IsTrue(false, "myParamName", "myTestMessage");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains("myTestMessage"));
}
[Fact]
public void IsFalse_IsFalse_ThrowsNoException()
{
Guard.IsFalse(false, "myParamName", "myTestMessage");
}
[Fact]
public void IsFalse_IsTrue_ThrowsException()
{
var exception = Assert.Throws<ArgumentException>(() =>
{
Guard.IsFalse(true, "myParamName", "myTestMessage");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains("myTestMessage"));
}
[Theory]
[InlineData(new int[] { 1, 2 }, 1)]
[InlineData(new int[] { 1, 2 }, 2)]
public void MustBeSizedAtLeast_LengthIsGreaterOrEqual_ThrowsNoException(int[] value, int minLength)
{
Guard.MustBeSizedAtLeast<int>(value, minLength, "myParamName");
}
[Fact]
public void MustBeSizedAtLeast_LengthIsLess_ThrowsException()
{
var exception = Assert.Throws<ArgumentException>(() =>
{
Guard.MustBeSizedAtLeast<int>(new int[] { 1, 2 }, 3, "myParamName");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains($"The size must be at least 3."));
}
}
}
Loading…
Cancel
Save