mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
35 changed files with 1462 additions and 340 deletions
@ -1,2 +1,11 @@ |
|||||
# Core |
# SixLabers.Core |
||||
Point, Rectangle, Size Primitives for use across SixLabors libraries. |
|
||||
|
**SixLabors.Core** provides classes for use across SixLabors libraries. |
||||
|
|
||||
|
[](https://ci.appveyor.com/project/six-labors/core/branch/develop) |
||||
|
[](https://codecov.io/gh/SixLabors/Core) |
||||
|
[](https://raw.githubusercontent.com/SixLabors/Core/master/LICENSE) |
||||
|
|
||||
|
[](https://github.com/SixLabors/Core/issues) |
||||
|
[](https://github.com/SixLabors/Core/stargazers) |
||||
|
[](https://github.com/SixLabors/Core/network) |
||||
@ -1,9 +0,0 @@ |
|||||
{ |
|
||||
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", |
|
||||
"settings": { |
|
||||
"documentationRules": { |
|
||||
"companyName": "Scott Williams", |
|
||||
"copyrightText": "Copyright (c) Scott Williams and contributors.\nLicensed under the Apache License, Version 2.0." |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,23 +0,0 @@ |
|||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Text; |
|
||||
|
|
||||
namespace SixLabors |
|
||||
{ |
|
||||
// lifted from coreFX repo
|
|
||||
internal static class HashHelpers |
|
||||
{ |
|
||||
public static readonly int RandomSeed = Guid.NewGuid().GetHashCode(); |
|
||||
|
|
||||
public static int Combine(int h1, int h2) |
|
||||
{ |
|
||||
unchecked |
|
||||
{ |
|
||||
// RyuJIT optimizes this to use the ROL instruction
|
|
||||
// Related GitHub pull request: dotnet/coreclr#1830
|
|
||||
uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27); |
|
||||
return ((int)rol5 + h1) ^ h2; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,261 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Diagnostics; |
||||
|
|
||||
|
namespace SixLabors |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Provides methods to protect against invalid parameters for a DEBUG build.
|
||||
|
/// </summary>
|
||||
|
[DebuggerStepThrough] |
||||
|
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.
|
||||
|
/// </summary>
|
||||
|
/// <param name="target">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>
|
||||
|
[Conditional("DEBUG")] |
||||
|
public static void NotNull(object target, string parameterName) |
||||
|
{ |
||||
|
if (target == null) |
||||
|
{ |
||||
|
throw new ArgumentNullException(parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies that the specified value is less than a maximum value
|
||||
|
/// and throws an exception if it is not.
|
||||
|
/// </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="ArgumentException">
|
||||
|
/// <paramref name="value"/> is greater than the maximum value.
|
||||
|
/// </exception>
|
||||
|
[Conditional("DEBUG")] |
||||
|
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}."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies that the specified value is less than or equal to a maximum value
|
||||
|
/// and throws an exception if it is not.
|
||||
|
/// </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="ArgumentException">
|
||||
|
/// <paramref name="value"/> is greater than the maximum value.
|
||||
|
/// </exception>
|
||||
|
[Conditional("DEBUG")] |
||||
|
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}."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies that the specified value is greater than a minimum value
|
||||
|
/// and throws an exception if it is not.
|
||||
|
/// </summary>
|
||||
|
/// <param name="value">The target value, which should be validated.</param>
|
||||
|
/// <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">
|
||||
|
/// <paramref name="value"/> is less than the minimum value.
|
||||
|
/// </exception>
|
||||
|
[Conditional("DEBUG")] |
||||
|
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}."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies that the specified value is greater than or equal to a minimum value
|
||||
|
/// and throws an exception if it is not.
|
||||
|
/// </summary>
|
||||
|
/// <param name="value">The target value, which should be validated.</param>
|
||||
|
/// <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">
|
||||
|
/// <paramref name="value"/> is less than the minimum value.
|
||||
|
/// </exception>
|
||||
|
[Conditional("DEBUG")] |
||||
|
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}."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <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")] |
||||
|
public static void IsTrue(bool target, string parameterName, string message) |
||||
|
{ |
||||
|
if (!target) |
||||
|
{ |
||||
|
throw new ArgumentException(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 IsFalse(bool target, string parameterName, string message) |
||||
|
{ |
||||
|
if (target) |
||||
|
{ |
||||
|
throw new ArgumentException(message, parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies, that the target span is of same size than the 'other' span.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="T">The element type of the spans</typeparam>
|
||||
|
/// <param name="target">The target span.</param>
|
||||
|
/// <param name="other">The 'other' span to compare 'target' to.</param>
|
||||
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
||||
|
/// <exception cref="ArgumentException">
|
||||
|
/// <paramref name="target"/> is true
|
||||
|
/// </exception>
|
||||
|
[Conditional("DEBUG")] |
||||
|
public static void MustBeSameSized<T>(ReadOnlySpan<T> target, ReadOnlySpan<T> other, string parameterName) |
||||
|
where T : struct |
||||
|
{ |
||||
|
if (target.Length != other.Length) |
||||
|
{ |
||||
|
throw new ArgumentException("Span-s must be the same size.", parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies, that the target span is of same size than the 'other' span.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="T">The element type of the spans</typeparam>
|
||||
|
/// <param name="target">The target span.</param>
|
||||
|
/// <param name="other">The 'other' span to compare 'target' to.</param>
|
||||
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
||||
|
/// <exception cref="ArgumentException">
|
||||
|
/// <paramref name="target"/> is true
|
||||
|
/// </exception>
|
||||
|
[Conditional("DEBUG")] |
||||
|
public static void MustBeSameSized<T>(Span<T> target, Span<T> other, string parameterName) |
||||
|
where T : struct |
||||
|
{ |
||||
|
if (target.Length != other.Length) |
||||
|
{ |
||||
|
throw new ArgumentException("Span-s must be the same size.", parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies, that the `target` span has the length of 'minSpan', or longer.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="T">The element type of the spans</typeparam>
|
||||
|
/// <param name="target">The target span.</param>
|
||||
|
/// <param name="minSpan">The 'minSpan' span to compare 'target' to.</param>
|
||||
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
||||
|
/// <exception cref="ArgumentException">
|
||||
|
/// <paramref name="target"/> is true
|
||||
|
/// </exception>
|
||||
|
[Conditional("DEBUG")] |
||||
|
public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> target, ReadOnlySpan<T> minSpan, string parameterName) |
||||
|
where T : struct |
||||
|
{ |
||||
|
if (target.Length < minSpan.Length) |
||||
|
{ |
||||
|
throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}.", parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies, that the `target` span has the length of 'minSpan', or longer.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="T">The element type of the spans</typeparam>
|
||||
|
/// <param name="target">The target span.</param>
|
||||
|
/// <param name="minSpan">The 'minSpan' span to compare 'target' to.</param>
|
||||
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
||||
|
/// <exception cref="ArgumentException">
|
||||
|
/// <paramref name="target"/> is true
|
||||
|
/// </exception>
|
||||
|
[Conditional("DEBUG")] |
||||
|
public static void MustBeSizedAtLeast<T>(Span<T> target, Span<T> minSpan, string parameterName) |
||||
|
where T : struct |
||||
|
{ |
||||
|
if (target.Length < minSpan.Length) |
||||
|
{ |
||||
|
throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}.", parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies, that the `target` array has declared the length or longer.
|
||||
|
/// </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="parameterName">The name of the parameter that is to be checked.</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 |
||||
|
{ |
||||
|
if (target.Length < minLength) |
||||
|
{ |
||||
|
throw new ArgumentException($"The size must be at least {minLength}.", parameterName); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,266 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Diagnostics; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace SixLabors |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Provides methods to protect against invalid parameters.
|
||||
|
/// </summary>
|
||||
|
[DebuggerStepThrough] |
||||
|
internal static class Guard |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Verifies, that the method parameter with specified object value is not null
|
||||
|
/// and throws an exception if it is found to be so.
|
||||
|
/// </summary>
|
||||
|
/// <param name="target">The target object, which cannot be null.</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="ArgumentNullException"><paramref name="target"/> is null</exception>
|
||||
|
public static void NotNull(object target, string parameterName, string message = "") |
||||
|
{ |
||||
|
if (target == null) |
||||
|
{ |
||||
|
if (!string.IsNullOrWhiteSpace(message)) |
||||
|
{ |
||||
|
throw new ArgumentNullException(parameterName, message); |
||||
|
} |
||||
|
|
||||
|
throw new ArgumentNullException(parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies, that the string method parameter with specified object value and message
|
||||
|
/// is not null, not empty and does not contain only blanks and throws an exception
|
||||
|
/// if the object is null.
|
||||
|
/// </summary>
|
||||
|
/// <param name="target">The target string, which should be checked against being null or empty.</param>
|
||||
|
/// <param name="parameterName">Name of the parameter.</param>
|
||||
|
/// <param name="message">The error message, if any to add to the exception.</param>
|
||||
|
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null.</exception>
|
||||
|
/// <exception cref="ArgumentException"><paramref name="target"/> is empty or contains only blanks.</exception>
|
||||
|
public static void NotNullOrEmpty(string target, string parameterName, string message = "") |
||||
|
{ |
||||
|
NotNull(target, parameterName, message); |
||||
|
|
||||
|
if (string.IsNullOrWhiteSpace(target)) |
||||
|
{ |
||||
|
if (!string.IsNullOrWhiteSpace(message)) |
||||
|
{ |
||||
|
throw new ArgumentException(message, parameterName); |
||||
|
} |
||||
|
|
||||
|
throw new ArgumentException("Value cannot be null, empty, or cannot contain only whitespace.", parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies, that the enumeration is not null and not empty.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="T">The type of objects in the <paramref name="target"/></typeparam>
|
||||
|
/// <param name="target">The target enumeration, which should be checked against being null or empty.</param>
|
||||
|
/// <param name="parameterName">Name of the parameter.</param>
|
||||
|
/// <param name="message">The error message, if any to add to the exception.</param>
|
||||
|
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null.</exception>
|
||||
|
/// <exception cref="ArgumentException"><paramref name="target"/> is empty.</exception>
|
||||
|
public static void NotNullOrEmpty<T>(IEnumerable<T> target, string parameterName, string message = "") |
||||
|
{ |
||||
|
NotNull(target, parameterName, message); |
||||
|
|
||||
|
if (!target.Any()) |
||||
|
{ |
||||
|
if (!string.IsNullOrWhiteSpace(message)) |
||||
|
{ |
||||
|
throw new ArgumentException(message, parameterName); |
||||
|
} |
||||
|
|
||||
|
throw new ArgumentException("Value cannot be empty.", parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies that the specified value is less than a maximum value
|
||||
|
/// and throws an exception if it is not.
|
||||
|
/// </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">
|
||||
|
/// <paramref name="value"/> is greater than the maximum value.
|
||||
|
/// </exception>
|
||||
|
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}."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies that the specified value is less than or equal to a maximum value
|
||||
|
/// and throws an exception if it is not.
|
||||
|
/// </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">
|
||||
|
/// <paramref name="value"/> is greater than the maximum value.
|
||||
|
/// </exception>
|
||||
|
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}."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies that the specified value is greater than a minimum value
|
||||
|
/// and throws an exception if it is not.
|
||||
|
/// </summary>
|
||||
|
/// <param name="value">The target value, which should be validated.</param>
|
||||
|
/// <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">
|
||||
|
/// <paramref name="value"/> is less than the minimum value.
|
||||
|
/// </exception>
|
||||
|
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}."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Verifies that the specified value is greater than or equal to a minimum value
|
||||
|
/// and throws an exception if it is not.
|
||||
|
/// </summary>
|
||||
|
/// <param name="value">The target value, which should be validated.</param>
|
||||
|
/// <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">
|
||||
|
/// <paramref name="value"/> is less than the minimum value.
|
||||
|
/// </exception>
|
||||
|
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}."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 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>
|
||||
|
/// <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="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) |
||||
|
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}."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <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="value">
|
||||
|
/// 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="value"/> is false
|
||||
|
/// </exception>
|
||||
|
public static void IsTrue(bool value, string parameterName, string message) |
||||
|
{ |
||||
|
if (!value) |
||||
|
{ |
||||
|
throw new ArgumentException(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="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="value"/> is true
|
||||
|
/// </exception>
|
||||
|
public static void IsFalse(bool value, string parameterName, string message) |
||||
|
{ |
||||
|
if (value) |
||||
|
{ |
||||
|
throw new ArgumentException(message, parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 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="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">
|
||||
|
/// The length of <paramref name="value"/> is less than <paramref name="minLength"/>.
|
||||
|
/// </exception>
|
||||
|
public static void MustBeSizedAtLeast<T>(T[] value, int minLength, string parameterName) |
||||
|
{ |
||||
|
if (value.Length < minLength) |
||||
|
{ |
||||
|
throw new ArgumentException($"The size must be at least {minLength}.", parameterName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 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="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">
|
||||
|
/// The length of <paramref name="value"/> is less than <paramref name="minLength"/>.
|
||||
|
/// </exception>
|
||||
|
public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> value, int minLength, string parameterName) |
||||
|
{ |
||||
|
if (value.Length < minLength) |
||||
|
{ |
||||
|
throw new ArgumentException($"The size must be at least {minLength}.", parameterName); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
namespace SixLabors |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Lifted from coreFX repo
|
||||
|
/// </summary>
|
||||
|
internal static class HashHelpers |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Combines the two specified hash codes.
|
||||
|
/// </summary>
|
||||
|
/// <param name="h1">Hash code one</param>
|
||||
|
/// <param name="h2">Hash code two</param>
|
||||
|
/// <returns>Returns a hash code for the two specified has codes.</returns>
|
||||
|
public static int Combine(int h1, int h2) |
||||
|
{ |
||||
|
unchecked |
||||
|
{ |
||||
|
// RyuJIT optimizes this to use the ROL instruction
|
||||
|
// Related GitHub pull request: dotnet/coreclr#1830
|
||||
|
uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27); |
||||
|
return ((int)rol5 + h1) ^ h2; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,9 +0,0 @@ |
|||||
{ |
|
||||
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", |
|
||||
"settings": { |
|
||||
"documentationRules": { |
|
||||
"companyName": "Six Labors", |
|
||||
"copyrightText": "Copyright (c) Six Labors and contributors.\nLicensed under the Apache License, Version 2.0." |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,15 @@ |
|||||
|
{ |
||||
|
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", |
||||
|
"settings": |
||||
|
{ |
||||
|
"orderingRules": |
||||
|
{ |
||||
|
"usingDirectivesPlacement": "outsideNamespace" |
||||
|
}, |
||||
|
"documentationRules": |
||||
|
{ |
||||
|
"xmlHeader": false, |
||||
|
"copyrightText": "Copyright (c) Six Labors and contributors.\nLicensed under the Apache License, Version 2.0." |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,258 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
// tell this file to enable debug conditional method calls, i.e. all the debug guard calls
|
||||
|
#define DEBUG
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Diagnostics; |
||||
|
using System.Linq; |
||||
|
using System.Reflection; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace SixLabors.Helpers.Tests |
||||
|
{ |
||||
|
public class DebugGuardTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void AllStaticMethodsOnOnDebugGuardHaveDEBUGConditional() |
||||
|
{ |
||||
|
var methods = typeof(DebugGuard).GetTypeInfo().GetMethods() |
||||
|
.Where(x => x.IsStatic); |
||||
|
|
||||
|
foreach (var m in methods) |
||||
|
{ |
||||
|
var attribs = m.GetCustomAttributes<ConditionalAttribute>(); |
||||
|
Assert.True(attribs.Select(x => x.ConditionString).Contains("DEBUG"), $"Method '{m.Name}' does not have [Conditional(\"DEBUG\")] set."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void NotNull_TargetNotNull_ThrowsNoException() |
||||
|
{ |
||||
|
DebugGuard.NotNull("test", "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void NotNull_TargetNull_ThrowsException() |
||||
|
{ |
||||
|
Assert.Throws<ArgumentNullException>(() => |
||||
|
{ |
||||
|
DebugGuard.NotNull(null, "myParamName"); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeLessThan_IsLess_ThrowsNoException() |
||||
|
{ |
||||
|
DebugGuard.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>(() => |
||||
|
{ |
||||
|
DebugGuard.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) |
||||
|
{ |
||||
|
DebugGuard.MustBeLessThanOrEqualTo(value, max, "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeLessThanOrEqualTo_IsGreater_ThrowsNoException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => |
||||
|
{ |
||||
|
DebugGuard.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() |
||||
|
{ |
||||
|
DebugGuard.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>(() => |
||||
|
{ |
||||
|
DebugGuard.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) |
||||
|
{ |
||||
|
DebugGuard.MustBeGreaterThanOrEqualTo(value, min, "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeGreaterThanOrEqualTo_IsLess_ThrowsNoException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => |
||||
|
{ |
||||
|
DebugGuard.MustBeGreaterThanOrEqualTo(1, 2, "myParamName"); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal("myParamName", exception.ParamName); |
||||
|
Assert.True(exception.Message.Contains($"Value must be greater than or equal to 2.")); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void IsTrue_IsTrue_ThrowsNoException() |
||||
|
{ |
||||
|
DebugGuard.IsTrue(true, "myParamName", "myTestMessage"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void IsTrue_IsFalse_ThrowsException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentException>(() => |
||||
|
{ |
||||
|
DebugGuard.IsTrue(false, "myParamName", "myTestMessage"); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal("myParamName", exception.ParamName); |
||||
|
Assert.True(exception.Message.Contains("myTestMessage")); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void IsFalse_IsFalse_ThrowsNoException() |
||||
|
{ |
||||
|
DebugGuard.IsFalse(false, "myParamName", "myTestMessage"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void IsFalse_IsTrue_ThrowsException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentException>(() => |
||||
|
{ |
||||
|
DebugGuard.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_Array_LengthIsGreaterOrEqual_ThrowsNoException(int[] value, int minLength) |
||||
|
{ |
||||
|
DebugGuard.MustBeSizedAtLeast<int>(value, minLength, "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSizedAtLeast_Array_LengthIsLess_ThrowsException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentException>(() => |
||||
|
{ |
||||
|
DebugGuard.MustBeSizedAtLeast<int>(new int[] { 1, 2 }, 3, "myParamName"); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal("myParamName", exception.ParamName); |
||||
|
Assert.Contains($"The size must be at least 3.", exception.Message); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSizedAtLeast_ReadOnlySpan_LengthIsLess_ThrowsException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentException>(() => |
||||
|
{ |
||||
|
DebugGuard.MustBeSizedAtLeast(new ReadOnlySpan<int>(new int[2]), new ReadOnlySpan<int>(new int[3]), "myParamName"); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal("myParamName", exception.ParamName); |
||||
|
Assert.Contains($"Span-s must be at least of length 3.", exception.Message); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSizedAtLeast_Span_LengthIsLess_ThrowsException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentException>(() => |
||||
|
{ |
||||
|
DebugGuard.MustBeSizedAtLeast(new Span<int>(new int[2]), new Span<int>(new int[3]), "myParamName"); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal("myParamName", exception.ParamName); |
||||
|
Assert.Contains($"Span-s must be at least of length 3.", exception.Message); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSameSized_ReadOnlySpan_LengthIsLess_ThrowsException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentException>(() => |
||||
|
{ |
||||
|
DebugGuard.MustBeSameSized(new ReadOnlySpan<int>(new int[2]), new ReadOnlySpan<int>(new int[3]), "myParamName"); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal("myParamName", exception.ParamName); |
||||
|
Assert.Contains($"Span-s must be the same size.", exception.Message); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSameSized_Span_LengthIsLess_ThrowsException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentException>(() => |
||||
|
{ |
||||
|
DebugGuard.MustBeSameSized(new Span<int>(new int[2]), new Span<int>(new int[3]), "myParamName"); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal("myParamName", exception.ParamName); |
||||
|
Assert.Contains($"Span-s must be the same size.", exception.Message); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(2, 2)] |
||||
|
[InlineData(4, 3)] |
||||
|
public void MustBeSizedAtLeast_ReadOnlySpan_LengthIsEqualOrGreater_DoesNotThowException(int leftSize, int rightSize) |
||||
|
{ |
||||
|
DebugGuard.MustBeSizedAtLeast(new ReadOnlySpan<int>(new int[leftSize]), new ReadOnlySpan<int>(new int[rightSize]), "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(2, 2)] |
||||
|
[InlineData(4, 3)] |
||||
|
public void MustBeSizedAtLeast_Span_LengthIsEqualOrGreater_DoesNotThowException(int leftSize, int rightSize) |
||||
|
{ |
||||
|
DebugGuard.MustBeSizedAtLeast(new Span<int>(new int[leftSize]), new Span<int>(new int[rightSize]), "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSameSized_ReadOnlySpan_LengthIsEqual_DoesNotThrowException() |
||||
|
{ |
||||
|
DebugGuard.MustBeSameSized(new ReadOnlySpan<int>(new int[2]), new ReadOnlySpan<int>(new int[2]), "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSameSized_Span_LengthIsEqual_DoesNotThrowException() |
||||
|
{ |
||||
|
DebugGuard.MustBeSameSized(new Span<int>(new int[2]), new Span<int>(new int[2]), "myParamName"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,326 @@ |
|||||
|
// 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(2, 1)] |
||||
|
[InlineData(2, 2)] |
||||
|
public void MustBeSizedAtLeast_Array_LengthIsGreaterOrEqual_ThrowsNoException(int valueLength, int minLength) |
||||
|
{ |
||||
|
Guard.MustBeSizedAtLeast<int>(new int[valueLength], minLength, "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(2, 1)] |
||||
|
[InlineData(2, 2)] |
||||
|
public void MustBeSizedAtLeast_Span_LengthIsGreaterOrEqual_ThrowsNoException(int valueLength, int minLength) |
||||
|
{ |
||||
|
Guard.MustBeSizedAtLeast<int>(new Span<int>(new int[valueLength]), minLength, "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(2, 1)] |
||||
|
[InlineData(2, 2)] |
||||
|
public void MustBeSizedAtLeast_ReadOnlySpan_LengthIsGreaterOrEqual_ThrowsNoException(int valueLength, int minLength) |
||||
|
{ |
||||
|
Guard.MustBeSizedAtLeast<int>(new ReadOnlySpan<int>(new int[valueLength]), minLength, "myParamName"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSizedAtLeast_Array_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.")); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSizedAtLeast_Span_LengthIsLess_ThrowsException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentException>(() => |
||||
|
{ |
||||
|
Guard.MustBeSizedAtLeast<int>(new Span<int>(new int[2]), 3, "myParamName"); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal("myParamName", exception.ParamName); |
||||
|
Assert.True(exception.Message.Contains($"The size must be at least 3.")); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void MustBeSizedAtLeast_ReadOnlySpan_LengthIsLess_ThrowsException() |
||||
|
{ |
||||
|
var exception = Assert.Throws<ArgumentException>(() => |
||||
|
{ |
||||
|
Guard.MustBeSizedAtLeast<int>(new ReadOnlySpan<int>(new int[2]), 3, "myParamName"); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal("myParamName", exception.ParamName); |
||||
|
Assert.True(exception.Message.Contains($"The size must be at least 3.")); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<RuleSet Name="Shaper2D" ToolsVersion="14.0"> |
||||
|
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers"> |
||||
|
<Rule Id="SA1413" Action="None" /> |
||||
|
<!-- Test only ignores --> |
||||
|
<Rule Id="SA0001" Action="None" /> |
||||
|
<Rule Id="SA1117" Action="None" /> |
||||
|
<Rule Id="SA1600" Action="None" /> |
||||
|
</Rules> |
||||
|
</RuleSet> |
||||
Loading…
Reference in new issue