< Summary

Information
Class: Allyaria.Abstractions.Validation.AryGuard
Assembly: Allyaria.Abstractions
File(s): /home/runner/work/allyaria/allyaria/src/Allyaria.Abstractions/Validation/AryGuard.cs
Line coverage
100%
Covered lines: 85
Uncovered lines: 0
Coverable lines: 85
Total lines: 309
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Between(...)100%11100%
Check(...)100%11100%
CheckError(...)100%22100%
EnumDefined(...)100%11100%
EqualTo(...)100%11100%
False(...)100%11100%
For(...)100%11100%
GreaterThan(...)100%11100%
GreaterThanOrEqualTo(...)100%11100%
InRange(...)100%11100%
IsAssignableTo(...)100%11100%
LessThan(...)100%11100%
LessThanOrEqualTo(...)100%11100%
NotDefault(...)100%11100%
NotEqualTo(...)100%11100%
NotNull(...)100%11100%
NotNullOrEmpty(...)100%11100%
NotNullOrEmpty(...)100%11100%
NotNullOrWhiteSpace(...)100%11100%
SameType(...)100%11100%
True(...)100%11100%

File(s)

/home/runner/work/allyaria/allyaria/src/Allyaria.Abstractions/Validation/AryGuard.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2
 3namespace Allyaria.Abstractions.Validation;
 4
 5/// <summary>
 6/// Provides guard clauses for argument validation by invoking <see cref="AryChecks" /> and throwing
 7/// <see cref="AryArgumentException" /> when validation fails.
 8/// </summary>
 9public static class AryGuard
 10{
 11    /// <summary>Ensures that the specified value lies within the given exclusive range.</summary>
 12    /// <typeparam name="T">A comparable type.</typeparam>
 13    /// <param name="value">The value to validate.</param>
 14    /// <param name="min">The exclusive lower bound.</param>
 15    /// <param name="max">The exclusive upper bound.</param>
 16    /// <param name="argName">The argument name, automatically captured.</param>
 17    /// <exception cref="AryArgumentException">Thrown if the value is outside the range.</exception>
 18    public static void Between<T>(T value,
 19        T min,
 20        T max,
 21        [CallerArgumentExpression(parameterName: nameof(value))]
 22        string? argName = null)
 23        where T : IComparable<T>
 424        => CheckError(
 425            ex: AryChecks.Between(
 426                value: value, min: min, max: max, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 427            )
 428        );
 29
 30    /// <summary>Ensures that a condition is true; throws otherwise.</summary>
 31    /// <param name="condition">The condition to evaluate.</param>
 32    /// <param name="argName">The argument name.</param>
 33    /// <param name="message">The message describing the violation.</param>
 34    /// <exception cref="AryArgumentException">Thrown if the condition is false.</exception>
 35    public static void Check(bool condition, string argName, string message)
 2147736        => CheckError(ex: AryChecks.Check(condition: condition, argName: argName, message: message));
 37
 38    /// <summary>Throws the specified <see cref="AryArgumentException" /> if it is not null.</summary>
 39    /// <param name="ex">The exception to check and potentially throw.</param>
 40    private static void CheckError(AryArgumentException? ex)
 41    {
 138203642        if (ex is not null)
 43        {
 11644            throw ex;
 45        }
 138192046    }
 47
 48    /// <summary>Ensures that the specified enumeration value is defined within the enumeration type.</summary>
 49    /// <typeparam name="TEnum">The enumeration type.</typeparam>
 50    /// <param name="value">The enumeration value to validate.</param>
 51    /// <param name="argName">The argument name, automatically captured.</param>
 52    /// <exception cref="AryArgumentException">Thrown if the value is not defined in the enumeration.</exception>
 53    public static void EnumDefined<TEnum>(TEnum value,
 54        [CallerArgumentExpression(parameterName: nameof(value))]
 55        string? argName = null)
 56        where TEnum : struct, Enum
 457        => CheckError(
 458            ex: AryChecks.EnumDefined(value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)))
 459        );
 60
 61    /// <summary>Ensures that the specified value is equal to another value.</summary>
 62    /// <typeparam name="T">The value type.</typeparam>
 63    /// <param name="value">The first value to compare.</param>
 64    /// <param name="compare">The second value to compare.</param>
 65    /// <param name="argName">The argument name, automatically captured.</param>
 66    /// <exception cref="AryArgumentException">Thrown if the values are not equal.</exception>
 67    public static void EqualTo<T>(T value,
 68        T compare,
 69        [CallerArgumentExpression(parameterName: nameof(value))]
 70        string? argName = null)
 71        where T : IEquatable<T>
 472        => CheckError(
 473            ex: AryChecks.EqualTo(
 474                value: value, compare: compare, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 475            )
 476        );
 77
 78    /// <summary>Ensures that the specified condition is false.</summary>
 79    /// <param name="condition">The condition to check.</param>
 80    /// <param name="argName">The argument name, automatically captured.</param>
 81    /// <exception cref="AryArgumentException">Thrown if the condition is true.</exception>
 82    public static void False(bool condition,
 83        [CallerArgumentExpression(parameterName: nameof(condition))]
 84        string? argName = null)
 485        => CheckError(
 486            ex: AryChecks.False(
 487                condition: condition, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(condition))
 488            )
 489        );
 90
 91    /// <summary>Creates a new <see cref="AryValidation{T}" /> context for the specified value.</summary>
 92    /// <typeparam name="T">The type of the value to validate.</typeparam>
 93    /// <param name="value">The value to validate.</param>
 94    /// <param name="argName">The argument name, automatically captured.</param>
 95    /// <returns>A new validation context.</returns>
 96    public static AryValidation<T> For<T>(T value,
 97        [CallerArgumentExpression(parameterName: nameof(value))]
 98        string? argName = null)
 299        => new(argValue: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)));
 100
 101    /// <summary>Ensures that the specified value is greater than the given minimum (exclusive).</summary>
 102    /// <typeparam name="T">A comparable type.</typeparam>
 103    /// <param name="value">The value to validate.</param>
 104    /// <param name="minExclusive">The exclusive minimum bound.</param>
 105    /// <param name="argName">The argument name, automatically captured.</param>
 106    /// <exception cref="AryArgumentException">Thrown if the value is less than or equal to the minimum.</exception>
 107    public static void GreaterThan<T>(T value,
 108        T minExclusive,
 109        [CallerArgumentExpression(parameterName: nameof(value))]
 110        string? argName = null)
 111        where T : IComparable<T>
 4112        => CheckError(
 4113            ex: AryChecks.GreaterThan(
 4114                value: value, minExclusive: minExclusive, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 4115            )
 4116        );
 117
 118    /// <summary>Ensures that the specified value is greater than or equal to the given minimum (inclusive).</summary>
 119    /// <typeparam name="T">A comparable type.</typeparam>
 120    /// <param name="value">The value to validate.</param>
 121    /// <param name="minInclusive">The inclusive minimum bound.</param>
 122    /// <param name="argName">The argument name, automatically captured.</param>
 123    /// <exception cref="AryArgumentException">Thrown if the value is less than the minimum.</exception>
 124    public static void GreaterThanOrEqualTo<T>(T value,
 125        T minInclusive,
 126        [CallerArgumentExpression(parameterName: nameof(value))]
 127        string? argName = null)
 128        where T : IComparable<T>
 4129        => CheckError(
 4130            ex: AryChecks.GreaterThanOrEqualTo(
 4131                value: value, minInclusive: minInclusive, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 4132            )
 4133        );
 134
 135    /// <summary>Ensures that the specified value lies within the given inclusive range.</summary>
 136    /// <typeparam name="T">A comparable type.</typeparam>
 137    /// <param name="value">The value to validate.</param>
 138    /// <param name="min">The inclusive lower bound.</param>
 139    /// <param name="max">The inclusive upper bound.</param>
 140    /// <param name="argName">The argument name, automatically captured.</param>
 141    /// <exception cref="AryArgumentException">Thrown if the value is outside the range.</exception>
 142    public static void InRange<T>(T value,
 143        T min,
 144        T max,
 145        [CallerArgumentExpression(parameterName: nameof(value))]
 146        string? argName = null)
 147        where T : IComparable<T>
 1345949148        => CheckError(
 1345949149            ex: AryChecks.InRange(
 1345949150                value: value, min: min, max: max, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 1345949151            )
 1345949152        );
 153
 154    /// <summary>Ensures that the given value is assignable to the specified target type.</summary>
 155    /// <typeparam name="TTarget">The target type.</typeparam>
 156    /// <param name="value">The value to check.</param>
 157    /// <param name="argName">The argument name, automatically captured.</param>
 158    /// <exception cref="AryArgumentException">Thrown if the value cannot be assigned to the target type.</exception>
 159    public static void IsAssignableTo<TTarget>(object? value,
 160        [CallerArgumentExpression(parameterName: nameof(value))]
 161        string? argName = null)
 4162        => CheckError(
 4163            ex: AryChecks.IsAssignableTo<TTarget>(
 4164                value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 4165            )
 4166        );
 167
 168    /// <summary>Ensures that the specified value is less than the given maximum (exclusive).</summary>
 169    /// <typeparam name="T">A comparable type.</typeparam>
 170    /// <param name="value">The value to validate.</param>
 171    /// <param name="maxExclusive">The exclusive upper bound.</param>
 172    /// <param name="argName">The argument name, automatically captured.</param>
 173    /// <exception cref="AryArgumentException">Thrown if the value is greater than or equal to the maximum.</exception>
 174    public static void LessThan<T>(T value,
 175        T maxExclusive,
 176        [CallerArgumentExpression(parameterName: nameof(value))]
 177        string? argName = null)
 178        where T : IComparable<T>
 4179        => CheckError(
 4180            ex: AryChecks.LessThan(
 4181                value: value, maxExclusive: maxExclusive, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 4182            )
 4183        );
 184
 185    /// <summary>Ensures that the specified value is less than or equal to the given maximum (inclusive).</summary>
 186    /// <typeparam name="T">A comparable type.</typeparam>
 187    /// <param name="value">The value to validate.</param>
 188    /// <param name="maxInclusive">The inclusive upper bound.</param>
 189    /// <param name="argName">The argument name, automatically captured.</param>
 190    /// <exception cref="AryArgumentException">Thrown if the value is greater than the maximum.</exception>
 191    public static void LessThanOrEqualTo<T>(T value,
 192        T maxInclusive,
 193        [CallerArgumentExpression(parameterName: nameof(value))]
 194        string? argName = null)
 195        where T : IComparable<T>
 4196        => CheckError(
 4197            ex: AryChecks.LessThanOrEqualTo(
 4198                value: value, maxInclusive: maxInclusive, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 4199            )
 4200        );
 201
 202    /// <summary>Ensures that the specified value is not equal to its default value.</summary>
 203    /// <typeparam name="T">The value type.</typeparam>
 204    /// <param name="value">The value to validate.</param>
 205    /// <param name="argName">The argument name, automatically captured.</param>
 206    /// <exception cref="AryArgumentException">Thrown if the value equals its default.</exception>
 207    public static void NotDefault<T>(T value,
 208        [CallerArgumentExpression(parameterName: nameof(value))]
 209        string? argName = null)
 210        where T : struct, IEquatable<T>
 4211        => CheckError(
 4212            ex: AryChecks.NotDefault(value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)))
 4213        );
 214
 215    /// <summary>Ensures that the specified value is not equal to another value.</summary>
 216    /// <typeparam name="T">The value type.</typeparam>
 217    /// <param name="value">The first value to compare.</param>
 218    /// <param name="compare">The value to compare against.</param>
 219    /// <param name="argName">The argument name, automatically captured.</param>
 220    /// <exception cref="AryArgumentException">Thrown if the values are equal.</exception>
 221    public static void NotEqualTo<T>(T value,
 222        T compare,
 223        [CallerArgumentExpression(parameterName: nameof(value))]
 224        string? argName = null)
 225        where T : IEquatable<T>
 4226        => CheckError(
 4227            ex: AryChecks.NotEqualTo(
 4228                value: value, compare: compare, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 4229            )
 4230        );
 231
 232    /// <summary>Ensures that the specified value is not null.</summary>
 233    /// <typeparam name="T">The reference type.</typeparam>
 234    /// <param name="value">The value to validate.</param>
 235    /// <param name="argName">The argument name, automatically captured.</param>
 236    /// <exception cref="AryArgumentException">Thrown if the value is null.</exception>
 237    public static void NotNull<T>(T? value,
 238        [CallerArgumentExpression(parameterName: nameof(value))]
 239        string? argName = null)
 1480240        => CheckError(
 1480241            ex: AryChecks.NotNull(value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)))
 1480242        );
 243
 244    /// <summary>Ensures that the specified string is not null or empty.</summary>
 245    /// <param name="value">The string to validate.</param>
 246    /// <param name="argName">The argument name, automatically captured.</param>
 247    /// <exception cref="AryArgumentException">Thrown if the string is null or empty.</exception>
 248    public static void NotNullOrEmpty(string? value,
 249        [CallerArgumentExpression(parameterName: nameof(value))]
 250        string? argName = null)
 4251        => CheckError(
 4252            ex: AryChecks.NotNullOrEmpty(value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)))
 4253        );
 254
 255    /// <summary>Ensures that the specified collection is not null or empty.</summary>
 256    /// <typeparam name="T">The element type of the collection.</typeparam>
 257    /// <param name="collection">The collection to validate.</param>
 258    /// <param name="argName">The argument name, automatically captured.</param>
 259    /// <exception cref="AryArgumentException">Thrown if the collection is null or empty.</exception>
 260    public static void NotNullOrEmpty<T>(IReadOnlyCollection<T>? collection,
 261        [CallerArgumentExpression(parameterName: nameof(collection))]
 262        string? argName = null)
 4263        => CheckError(
 4264            ex: AryChecks.NotNullOrEmpty(
 4265                collection: collection, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(collection))
 4266            )
 4267        );
 268
 269    /// <summary>Ensures that the specified string is not null or whitespace.</summary>
 270    /// <param name="value">The string to validate.</param>
 271    /// <param name="argName">The argument name, automatically captured.</param>
 272    /// <exception cref="AryArgumentException">Thrown if the string is null, empty, or whitespace only.</exception>
 273    public static void NotNullOrWhiteSpace(string? value,
 274        [CallerArgumentExpression(parameterName: nameof(value))]
 275        string? argName = null)
 13066276        => CheckError(
 13066277            ex: AryChecks.NotNullOrWhiteSpace(
 13066278                value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))
 13066279            )
 13066280        );
 281
 282    /// <summary>Ensures that the two provided values are of the same type.</summary>
 283    /// <typeparam name="T1">The type of the first value.</typeparam>
 284    /// <typeparam name="T2">The type of the second value.</typeparam>
 285    /// <param name="value1">The first value to compare.</param>
 286    /// <param name="value2">The second value to compare.</param>
 287    /// <param name="argName">The argument name, automatically captured.</param>
 288    /// <exception cref="AryArgumentException">Thrown if the types differ.</exception>
 289    public static void SameType<T1, T2>(T1 value1,
 290        T2 value2,
 291        [CallerArgumentExpression(parameterName: nameof(value1))]
 292        string? argName = null)
 8293        => CheckError(
 8294            ex: AryChecks.SameType(
 8295                value1: value1, value2: value2, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value1))
 8296            )
 8297        );
 298
 299    /// <summary>Ensures that the specified condition is true.</summary>
 300    /// <param name="condition">The condition to check.</param>
 301    /// <param name="argName">The argument name, automatically captured.</param>
 302    /// <exception cref="AryArgumentException">Thrown if the condition is false.</exception>
 303    public static void True(bool condition,
 304        [CallerArgumentExpression(parameterName: nameof(condition))]
 305        string? argName = null)
 4306        => CheckError(
 4307            ex: AryChecks.True(condition: condition, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(condition)))
 4308        );
 309}