Browse Source

[SL.Core] Extend Guard and DebugGuard

pull/1087/head
Anton Firszov 7 years ago
parent
commit
20aef1b6da
  1. 217
      src/SixLabors.Core/Helpers/DebugGuard.cs
  2. 217
      src/SixLabors.Core/Helpers/Guard.cs
  3. 102
      tests/SixLabors.Core.Tests/Helpers/GuardTests.cs

217
src/SixLabors.Core/Helpers/DebugGuard.cs

@ -3,6 +3,7 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace SixLabors
{
@ -13,26 +14,47 @@ namespace SixLabors
internal static class DebugGuard
{
/// <summary>
/// Verifies, that the method parameter with specified object value is not null
/// and throws an exception if it is found to be so.
/// Ensures that the value is not null.
/// </summary>
/// <param name="target">The target object, which cannot be null.</param>
/// <param name="value">The target object, which cannot be null.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null.</exception>
/// <typeparam name="T">The type of the object to verify.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
[Conditional("DEBUG")]
public static void NotNull<T>(T target, string parameterName)
where T : class
[DebuggerStepThrough]
public static void NotNull<TValue>(TValue value, string parameterName)
where TValue : class
{
if (target is null)
if (value is null)
{
throw new ArgumentNullException(parameterName);
ThrowArgumentNullException(parameterName);
}
}
/// <summary>
/// Verifies that the specified value is less than a maximum value
/// and throws an exception if it is not.
/// Ensures that the target value is not null, empty, or whitespace.
/// </summary>
/// <param name="value">The target string, which should be checked against being null or empty.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or contains only blanks.</exception>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void NotNullOrWhiteSpace(string value, string parameterName)
{
if (value is null)
{
ThrowArgumentNullException(parameterName);
}
if (string.IsNullOrWhiteSpace(value))
{
ThrowArgumentException("Must not be empty or whitespace.", parameterName);
}
}
/// <summary>
/// Ensures that the specified value is less than a maximum value.
/// </summary>
/// <param name="value">The target value, which should be validated.</param>
/// <param name="max">The maximum value.</param>
@ -42,12 +64,13 @@ namespace SixLabors
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(max) >= 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than {max}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than {max}.");
}
}
@ -63,12 +86,13 @@ namespace SixLabors
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than or equal to {max}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than or equal to {max}.");
}
}
@ -84,14 +108,15 @@ namespace SixLabors
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(min) <= 0)
{
throw new ArgumentOutOfRangeException(
ThrowArgumentOutOfRangeException(
parameterName,
$"Value must be greater than {min}.");
$"Value {value} must be greater than {min}.");
}
}
@ -107,33 +132,177 @@ namespace SixLabors
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(min) < 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min}.");
}
}
/// <summary>
/// Verifies, that the `target` array has declared the length or longer.
/// Verifies that the specified value is greater than or equal to a minimum value and less than
/// or equal to a maximum value and throws an exception if it is not.
/// </summary>
/// <typeparam name="T">The element type of the spans.</typeparam>
/// <param name="target">The target array.</param>
/// <param name="minLength">The min length the array must have.</param>
/// <param name="value">The target value, which should be validated.</param>
/// <param name="min">The minimum value.</param>
/// <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">
/// <paramref name="value"/> is less than the minimum value of greater than the maximum value.
/// </exception>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
{
ThrowArgumentOutOfRangeException(parameterName, $"Value {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 false.
/// </exception>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void IsTrue(bool target, string parameterName, string message)
{
if (!target)
{
ThrowArgumentException(message, 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 true.
/// </exception>
[Conditional("DEBUG")]
public static void MustBeSizedAtLeast<T>(T[] target, int minLength, string parameterName)
where T : struct
[DebuggerStepThrough]
public static void IsFalse(bool target, string parameterName, string message)
{
if (target.Length < minLength)
if (target)
{
throw new ArgumentException($"The size must be at least {minLength}.", parameterName);
ThrowArgumentException(message, parameterName);
}
}
/// <summary>
/// Verifies, that the `source` span has the length of 'minLength', or longer.
/// </summary>
/// <typeparam name="T">The element type of the spans.</typeparam>
/// <param name="source">The source 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="source"/> has less than <paramref name="minLength"/> items.
/// </exception>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> source, int minLength, string parameterName)
{
if (source.Length < minLength)
{
ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
}
}
/// <summary>
/// Verifies that the 'destination' span is not shorter than 'source'.
/// </summary>
/// <typeparam name="TSource">The source element type.</typeparam>
/// <typeparam name="TDest">The destination element type.</typeparam>
/// <param name="source">The source span.</param>
/// <param name="destination">The destination span.</param>
/// <param name="destinationParamName">The name of the argument for 'destination'.</param>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void DestinationShouldNotBeTooShort<TSource, TDest>(
ReadOnlySpan<TSource> source,
Span<TDest> destination,
string destinationParamName)
{
if (destination.Length < source.Length)
{
ThrowArgumentException($"Destination span is too short!", destinationParamName);
}
}
/// <summary>
/// Verifies that the 'destination' span is not shorter than 'source'.
/// </summary>
/// <typeparam name="TSource">The source element type.</typeparam>
/// <typeparam name="TDest">The destination element type.</typeparam>
/// <param name="source">The source span.</param>
/// <param name="destination">The destination span.</param>
/// <param name="destinationParamName">The name of the argument for 'destination'.</param>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void DestinationShouldNotBeTooShort<TSource, TDest>(
Span<TSource> source,
Span<TDest> destination,
string destinationParamName)
{
if (destination.Length < source.Length)
{
ThrowArgumentException($"Destination span is too short!", destinationParamName);
}
}
/// <summary>
/// Verifies, that the `source` span has the length of 'minLength', or longer.
/// </summary>
/// <typeparam name="T">The element type of the spans.</typeparam>
/// <param name="source">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="source"/> has less than <paramref name="minLength"/> items.
/// </exception>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public static void MustBeSizedAtLeast<T>(Span<T> source, int minLength, string parameterName)
{
if (source.Length < minLength)
{
ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentException(string message, string parameterName)
{
throw new ArgumentException(message, parameterName);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentOutOfRangeException(string parameterName, string message)
{
throw new ArgumentOutOfRangeException(parameterName, message);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentNullException(string parameterName)
{
throw new ArgumentNullException(parameterName);
}
}
}

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

@ -4,7 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
namespace SixLabors
{
@ -15,22 +15,63 @@ namespace SixLabors
internal static class Guard
{
/// <summary>
/// Verifies that the specified value is less than a maximum value
/// and throws an exception if it is not.
/// Ensures that the value is not null.
/// </summary>
/// <param name="value">The target object, which cannot be null.</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="ArgumentNullException"><paramref name="value"/> is null.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void NotNull<TValue>(TValue value, string parameterName)
where TValue : class
{
if (value is null)
{
ThrowArgumentNullException(parameterName);
}
}
/// <summary>
/// Ensures that the target value is not null, empty, or whitespace.
/// </summary>
/// <param name="value">The target string, which should be checked against being null or empty.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or contains only blanks.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void NotNullOrWhiteSpace(string value, string parameterName)
{
if (value is null)
{
ThrowArgumentNullException(parameterName);
}
if (string.IsNullOrWhiteSpace(value))
{
ThrowArgumentException("Must not be empty or whitespace.", parameterName);
}
}
/// <summary>
/// Ensures that the specified value is less than a maximum value.
/// </summary>
/// <param name="value">The target value, which should be validated.</param>
/// <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="ArgumentOutOfRangeException">
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName)
where TValue : IComparable<TValue>
where TValue : IComparable<TValue>
{
if (value.CompareTo(max) >= 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than {max}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than {max}.");
}
}
@ -42,15 +83,17 @@ 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="ArgumentOutOfRangeException">
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName)
where TValue : IComparable<TValue>
where TValue : IComparable<TValue>
{
if (value.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than or equal to {max}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than or equal to {max}.");
}
}
@ -62,15 +105,19 @@ 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="ArgumentOutOfRangeException">
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(min) <= 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than {min}.");
ThrowArgumentOutOfRangeException(
parameterName,
$"Value {value} must be greater than {min}.");
}
}
@ -82,15 +129,17 @@ 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="ArgumentOutOfRangeException">
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(min) < 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min}.");
}
}
@ -103,34 +152,158 @@ 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="ArgumentOutOfRangeException">
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is less than the minimum value of greater than the maximum value.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min} and less than or equal to {max}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {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 false.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void IsTrue(bool target, string parameterName, string message)
{
if (!target)
{
ThrowArgumentException(message, 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 true.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void IsFalse(bool target, string parameterName, string message)
{
if (target)
{
ThrowArgumentException(message, parameterName);
}
}
/// <summary>
/// Verifies, that the `target` span has the length of 'minLength', or longer.
/// Verifies, that the `source` span has the length of 'minLength', or longer.
/// </summary>
/// <typeparam name="T">The element type of the spans.</typeparam>
/// <param name="value">The target span.</param>
/// <param name="source">The source 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">
/// The length of <paramref name="value"/> is less than <paramref name="minLength"/>.
/// <paramref name="source"/> has less than <paramref name="minLength"/> items.
/// </exception>
public static void MustBeSizedAtLeast<T>(T[] value, int minLength, string parameterName)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> source, int minLength, string parameterName)
{
if (value.Length < minLength)
if (source.Length < minLength)
{
throw new ArgumentException($"The size must be at least {minLength}.", parameterName);
ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
}
}
/// <summary>
/// Verifies, that the `source` span has the length of 'minLength', or longer.
/// </summary>
/// <typeparam name="T">The element type of the spans.</typeparam>
/// <param name="source">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="source"/> has less than <paramref name="minLength"/> items.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void MustBeSizedAtLeast<T>(Span<T> source, int minLength, string parameterName)
{
if (source.Length < minLength)
{
ThrowArgumentException($"The size must be at least {minLength}.", parameterName);
}
}
/// <summary>
/// Verifies that the 'destination' span is not shorter than 'source'.
/// </summary>
/// <typeparam name="TSource">The source element type.</typeparam>
/// <typeparam name="TDest">The destination element type.</typeparam>
/// <param name="source">The source span.</param>
/// <param name="destination">The destination span.</param>
/// <param name="destinationParamName">The name of the argument for 'destination'.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void DestinationShouldNotBeTooShort<TSource, TDest>(
ReadOnlySpan<TSource> source,
Span<TDest> destination,
string destinationParamName)
{
if (destination.Length < source.Length)
{
ThrowArgumentException($"Destination span is too short!", destinationParamName);
}
}
/// <summary>
/// Verifies that the 'destination' span is not shorter than 'source'.
/// </summary>
/// <typeparam name="TSource">The source element type.</typeparam>
/// <typeparam name="TDest">The destination element type.</typeparam>
/// <param name="source">The source span.</param>
/// <param name="destination">The destination span.</param>
/// <param name="destinationParamName">The name of the argument for 'destination'.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static void DestinationShouldNotBeTooShort<TSource, TDest>(
Span<TSource> source,
Span<TDest> destination,
string destinationParamName)
{
if (destination.Length < source.Length)
{
ThrowArgumentException($"Destination span is too short!", destinationParamName);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentException(string message, string parameterName)
{
throw new ArgumentException(message, parameterName);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentOutOfRangeException(string parameterName, string message)
{
throw new ArgumentOutOfRangeException(parameterName, message);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentNullException(string parameterName)
{
throw new ArgumentNullException(parameterName);
}
}
}
}

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

@ -8,6 +8,96 @@ namespace SixLabors.Helpers.Tests
{
public class GuardTests
{
private class Foo
{
}
[Fact]
public void NotNull_WhenNull_Throws()
{
Foo foo = null;
Assert.Throws<ArgumentNullException>(() => Guard.NotNull(foo, nameof(foo)));
}
[Fact]
public void NotNull_NotNull()
{
Foo foo = new Foo();
Guard.NotNull(foo, nameof(foo));
}
[Theory]
[InlineData(null, true)]
[InlineData("", true)]
[InlineData(" ", true)]
[InlineData("$", false)]
[InlineData("lol", false)]
public void NotNullOrWhiteSpace(string str, bool shouldThrow)
{
if (shouldThrow)
{
Assert.ThrowsAny<ArgumentException>(() => Guard.NotNullOrWhiteSpace(str, nameof(str)));
}
else
{
Guard.NotNullOrWhiteSpace(str, nameof(str));
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsTrue(bool value)
{
if (!value)
{
Assert.Throws<ArgumentException>(() => Guard.IsTrue(value, nameof(value), "Boo!"));
}
else
{
Guard.IsTrue(value, nameof(value), "Boo.");
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsFalse(bool value)
{
if (value)
{
Assert.Throws<ArgumentException>(() => Guard.IsFalse(value, nameof(value), "Boo!"));
}
else
{
Guard.IsFalse(value, nameof(value), "Boo.");
}
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(1, 1, false)]
[InlineData(1, 0, false)]
[InlineData(13, 13, false)]
[InlineData(20, 13, false)]
[InlineData(12, 13, true)]
[InlineData(0, 1, true)]
public void MustBeSizedAtLeast(int length, int minLength, bool shouldThrow)
{
int[] data = new int[length];
if (shouldThrow)
{
Assert.Throws<ArgumentException>(() => Guard.MustBeSizedAtLeast((Span<int>)data, minLength, nameof(data)));
Assert.Throws<ArgumentException>(() => Guard.MustBeSizedAtLeast((ReadOnlySpan<int>)data, minLength, nameof(data)));
}
else
{
Guard.MustBeSizedAtLeast((Span<int>)data, minLength, nameof(data));
Guard.MustBeSizedAtLeast((ReadOnlySpan<int>)data, minLength, nameof(data));
}
}
[Fact]
public void MustBeLessThan_IsLess_ThrowsNoException()
{
@ -25,7 +115,7 @@ namespace SixLabors.Helpers.Tests
});
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"Value must be less than {max}.", exception.Message);
Assert.Contains($"Value {value} must be less than {max}.", exception.Message);
}
[Theory]
@ -45,7 +135,7 @@ namespace SixLabors.Helpers.Tests
});
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"Value must be less than or equal to 1.", exception.Message);
Assert.Contains($"Value 2 must be less than or equal to 1.", exception.Message);
}
[Fact]
@ -65,7 +155,7 @@ namespace SixLabors.Helpers.Tests
});
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"Value must be greater than {min}.", exception.Message);
Assert.Contains($"Value {value} must be greater than {min}.", exception.Message);
}
[Theory]
@ -85,7 +175,7 @@ namespace SixLabors.Helpers.Tests
});
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"Value must be greater than or equal to 2.", exception.Message);
Assert.Contains($"Value 1 must be greater than or equal to 2.", exception.Message);
}
[Theory]
@ -108,7 +198,7 @@ namespace SixLabors.Helpers.Tests
});
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"Value must be greater than or equal to {min} and less than or equal to {max}.", exception.Message);
Assert.Contains($"Value {value} must be greater than or equal to {min} and less than or equal to {max}.", exception.Message);
}
[Theory]
@ -128,7 +218,7 @@ namespace SixLabors.Helpers.Tests
});
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains("The size must be at least 3.", exception.Message);
Assert.Contains("The size must be at least 3", exception.Message);
}
}
}

Loading…
Cancel
Save