Merge branch 'master' into u/vgromfeld/removeUnusedFields
This commit is contained in:
Коммит
0f1b9d3c73
|
@ -225,3 +225,6 @@ msbuild.binlog
|
|||
*.project.lock.json
|
||||
/build/tools/**
|
||||
!/build/tools/packages.config
|
||||
|
||||
# Generated file from .ttinclude
|
||||
**/Generated/TypeInfo.g.cs
|
|
@ -2,10 +2,6 @@
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.Toolkit
|
||||
{
|
||||
/// <summary>
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -0,0 +1,283 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
<#@include file="TypeInfo.ttinclude" #>
|
||||
/* ========================
|
||||
* Auto generated file
|
||||
* ===================== */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to verify conditions when running code.
|
||||
/// </summary>
|
||||
public static partial class Guard
|
||||
{
|
||||
<#
|
||||
GenerateTextForItems(EnumerableTypes, item =>
|
||||
{
|
||||
#>
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must be empty.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is != 0.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> != 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#=item.Cast#><#=item.Name#>, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must not be empty.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is == 0.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> == 0)
|
||||
{
|
||||
<#
|
||||
if (item.Type == "Span<T>")
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(name);
|
||||
<#
|
||||
}
|
||||
else if (item.Type == "ReadOnlySpan<T>")
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(name);
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty<<#=item.Type#>>(name);
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size of a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is != <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> != size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size not equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is == <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> == size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size over a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is <= <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeOver<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> <= size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size of at least or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is < <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeAtLeast<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> < size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size of less than a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is >= <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> >= size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size of less than or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is > <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> > size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the source <#=item.XmlType#> instance must have the same size of a destination <#=item.XmlType#> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="source">The source <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="destination">The destination <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="source"/> is != the one of <paramref name="destination"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
|
||||
{
|
||||
if (source.<#=item.Size#> != destination.<#=item.Size#>)
|
||||
{
|
||||
<#
|
||||
if (item.HasCountProperty)
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, <#=item.Cast#>destination, name);
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the source <#=item.XmlType#> instance must have a size of less than or equal to that of a destination <#=item.XmlType#> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="source">The source <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="destination">The destination <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="source"/> is > the one of <paramref name="destination"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThanOrEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
|
||||
{
|
||||
if (source.<#=item.Size#> > destination.<#=item.Size#>)
|
||||
{
|
||||
<#
|
||||
if (item.HasCountProperty)
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, <#=item.Cast#>destination, name);
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input index is valid for a given <#=item.XmlType#> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="index">The input index to be used to access <paramref name="<#=item.Name#>"/>.</param>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to use to validate <paramref name="index"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not valid to access <paramref name="<#=item.Name#>"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
<#
|
||||
/* Here we're leveraging the fact that signed integers are represented
|
||||
* in 2-complement to perform the bounds check with a single compare operation.
|
||||
* This is the same trick used throughout CoreCLR as well.
|
||||
* For more info and code sample, see the original conversation here:
|
||||
* https://github.com/windows-toolkit/WindowsCommunityToolkit/pull/3131#discussion_r390682835 */
|
||||
#>
|
||||
if ((uint)index >= (uint)<#=item.Name#>.<#=item.Size#>)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input index is not valid for a given <#=item.XmlType#> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="index">The input index to be used to access <paramref name="<#=item.Name#>"/>.</param>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to use to validate <paramref name="index"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is valid to access <paramref name="<#=item.Name#>"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
if ((uint)index < (uint)<#=item.Name#>.<#=item.Size#>)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
||||
}
|
||||
}
|
||||
<#
|
||||
});
|
||||
#>
|
||||
}
|
||||
}
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -0,0 +1,255 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
<#@include file="TypeInfo.ttinclude" #>
|
||||
/* ========================
|
||||
* Auto generated file
|
||||
* ===================== */
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to verify conditions when running code.
|
||||
/// </summary>
|
||||
public static partial class Guard
|
||||
{
|
||||
<#
|
||||
GenerateTextForItems(NumericTypes, type =>
|
||||
{
|
||||
#>
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="<#=type#>"/> value to test for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is != <paramref name="target"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsEqualTo(<#=type#> value, <#=type#> target, string name)
|
||||
{
|
||||
if (value != target)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be not equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="<#=type#>"/> value to test for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is == <paramref name="target"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotEqualTo(<#=type#> value, <#=type#> target, string name)
|
||||
{
|
||||
if (value == target)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be less than a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is >= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsLessThan(<#=type#> value, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value >= maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be less than or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="maximum">The inclusive maximum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is > <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsLessThanOrEqualTo(<#=type#> value, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value > maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be greater than a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is <= <paramref name="minimum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsGreaterThan(<#=type#> value, <#=type#> minimum, string name)
|
||||
{
|
||||
if (value <= minimum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be greater than or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is < <paramref name="minimum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsGreaterThanOrEqualTo(<#=type#> value, <#=type#> minimum, string name)
|
||||
{
|
||||
if (value < minimum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be in a given range.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is < <paramref name="minimum"/> or >= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> in [<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsInRange(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value < minimum || value >= maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be in a given range.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is >= <paramref name="minimum"/> or < <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> not in [<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotInRange(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value >= minimum && value < maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be in a given interval.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is <= <paramref name="minimum"/> or >= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> in (<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsBetween(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value <= minimum || value >= maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be in a given interval.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is > <paramref name="minimum"/> or < <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> not in (<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotBetween(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value > minimum && value < maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be in a given interval.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The inclusive maximum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is < <paramref name="minimum"/> or > <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> in [<paramref name="minimum"/>, <paramref name="maximum"/>]", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsBetweenOrEqualTo(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value < minimum || value > maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be in a given interval.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The inclusive maximum <see cref="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is >= <paramref name="minimum"/> or <= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> not in [<paramref name="minimum"/>, <paramref name="maximum"/>]", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotBetweenOrEqualTo(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value >= minimum && value <= maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
<#
|
||||
});
|
||||
#>
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
# T4 templates and generated APIs
|
||||
|
||||
This folder contains a number of template files (with the `.tt` or `.ttinclude` extensions) and the generated `.g.cs` files generated by those templates. The template files use the T4 format, which is natively supported by Visual Studio (more info is available [here](https://docs.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2019)).
|
||||
|
||||
## Why is this needed?
|
||||
|
||||
There are a few reasons why T4 templates are used for the `Guard` class:
|
||||
|
||||
- Especially when a large number of similar overloads are available for the same APIs, using templates makes it much easier to maintain code and spot mistakes, as the actual number of lines of code to review is much smaller: it's just the code in each template!
|
||||
- Using type-specific overloads instead of generic methods can result in faster code. For instance, T4 templates are used to generate overloads for comparison APIs (eg. `Guard.IsGreaterThan(int, int, string)`). This results in more compact and optimized code as opposed to a generic method using `where T : IComparable<T>` as type constraint.
|
||||
- In some cases, using generic methods just isn't possible. For instance, types like `Span<T>` and `ReadOnlySpan<T>` can't be used as generic type parameters, and even if that had been possible, they don't implement an interface we could use in the generic type constraint. Using T4 templates solves this issue, as we can just have specialized method for each supported type or collection type.
|
||||
|
||||
## How to make changes
|
||||
|
||||
If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and save it: Visual Studio will take care of updating the generated `.g.cs` file automatically. Don't make changes to those generated `.g.cs` files directly, as those will be overwritten as soon as their source template is updated.
|
||||
|
||||
Note that all the `.g.cs` files are checked in into the repository, so if you do make changes to a template file, make sure to also include the updated `.g.cs` file in your commits.
|
|
@ -0,0 +1,817 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
/* ========================
|
||||
* Auto generated file
|
||||
* ===================== */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")]
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(Span<T> span, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must be empty, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(Span<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must have a size equal to {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(Span<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must have a size not equal to {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver<T>(Span<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must have a size over {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(Span<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must have a size of at least {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(Span<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must have a size less than {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(Span<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(Span<T> source, Span<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(Span<T> source, Span<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, Span<T> span, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {span.Length.ToAssertString()} to be a valid index for the target collection ({typeof(Span<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, Span<T> span, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {span.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(Span<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(ReadOnlySpan<T> span, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must be empty, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(ReadOnlySpan<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must have a size equal to {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(ReadOnlySpan<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must have a size not equal to {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver<T>(ReadOnlySpan<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must have a size over {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(ReadOnlySpan<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must have a size of at least {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(ReadOnlySpan<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must have a size less than {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(ReadOnlySpan<T> span, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {span.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(ReadOnlySpan<T> source, Span<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(ReadOnlySpan<T> source, Span<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, ReadOnlySpan<T> span, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {span.Length.ToAssertString()} to be a valid index for the target collection ({typeof(ReadOnlySpan<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, ReadOnlySpan<T> span, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {span.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(ReadOnlySpan<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(Memory<T> memory, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory<T>).ToTypeString()}) must be empty, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(Memory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory<T>).ToTypeString()}) must have a size equal to {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(Memory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory<T>).ToTypeString()}) must have a size not equal to {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver<T>(Memory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory<T>).ToTypeString()}) must have a size over {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(Memory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory<T>).ToTypeString()}) must have a size of at least {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(Memory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory<T>).ToTypeString()}) must have a size less than {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(Memory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory<T>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(Memory<T> source, Memory<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Memory<T>).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(Memory<T> source, Memory<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Memory<T>).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, Memory<T> memory, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {memory.Length.ToAssertString()} to be a valid index for the target collection ({typeof(Memory<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, Memory<T> memory, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {memory.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(Memory<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(ReadOnlyMemory<T> memory, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory<T>).ToTypeString()}) must be empty, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(ReadOnlyMemory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory<T>).ToTypeString()}) must have a size equal to {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(ReadOnlyMemory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory<T>).ToTypeString()}) must have a size not equal to {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver<T>(ReadOnlyMemory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory<T>).ToTypeString()}) must have a size over {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(ReadOnlyMemory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory<T>).ToTypeString()}) must have a size of at least {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(ReadOnlyMemory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory<T>).ToTypeString()}) must have a size less than {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(ReadOnlyMemory<T> memory, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory<T>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {memory.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(ReadOnlyMemory<T> source, Memory<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlyMemory<T>).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(ReadOnlyMemory<T> source, Memory<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlyMemory<T>).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, ReadOnlyMemory<T> memory, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {memory.Length.ToAssertString()} to be a valid index for the target collection ({typeof(ReadOnlyMemory<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, ReadOnlyMemory<T> memory, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {memory.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(ReadOnlyMemory<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(T[] array, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be empty, had a size of {array.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(T[] array, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size equal to {size}, had a size of {array.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(T[] array, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size not equal to {size}, had a size of {array.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver<T>(T[] array, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size over {size}, had a size of {array.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(T[] array, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size of at least {size}, had a size of {array.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(T[] array, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size less than {size}, had a size of {array.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(T[] array, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size less than or equal to {size}, had a size of {array.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(T[] source, T[] destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(T[] source, T[] destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, T[] array, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {array.Length.ToAssertString()} to be a valid index for the target collection ({typeof(T[]).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, T[] array, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {array.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(T[]).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(List<T> list, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List<T>).ToTypeString()}) must be empty, had a size of {list.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(List<T> list, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List<T>).ToTypeString()}) must have a size equal to {size}, had a size of {list.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(List<T> list, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List<T>).ToTypeString()}) must have a size not equal to {size}, had a size of {list.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver<T>(List<T> list, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List<T>).ToTypeString()}) must have a size over {size}, had a size of {list.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(List<T> list, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List<T>).ToTypeString()}) must have a size of at least {size}, had a size of {list.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(List<T> list, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List<T>).ToTypeString()}) must have a size less than {size}, had a size of {list.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(List<T> list, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List<T>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {list.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(List<T> source, List<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(List<T>).ToTypeString()}) must have a size equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(List<T> source, List<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(List<T>).ToTypeString()}) must have a size less than or equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, List<T> list, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {list.Count.ToAssertString()} to be a valid index for the target collection ({typeof(List<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, List<T> list, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {list.Count.ToAssertString()} to be an invalid index for the target collection ({typeof(List<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(ICollection<T> collection, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection<T>).ToTypeString()}) must be empty, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(ICollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection<T>).ToTypeString()}) must have a size equal to {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(ICollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection<T>).ToTypeString()}) must have a size not equal to {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver<T>(ICollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection<T>).ToTypeString()}) must have a size over {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(ICollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection<T>).ToTypeString()}) must have a size of at least {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(ICollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection<T>).ToTypeString()}) must have a size less than {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(ICollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection<T>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(ICollection<T> source, ICollection<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ICollection<T>).ToTypeString()}) must have a size equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(ICollection<T> source, ICollection<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ICollection<T>).ToTypeString()}) must have a size less than or equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, ICollection<T> collection, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {collection.Count.ToAssertString()} to be a valid index for the target collection ({typeof(ICollection<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, ICollection<T> collection, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {collection.Count.ToAssertString()} to be an invalid index for the target collection ({typeof(ICollection<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(IReadOnlyCollection<T> collection, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection<T>).ToTypeString()}) must be empty, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(IReadOnlyCollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection<T>).ToTypeString()}) must have a size equal to {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(IReadOnlyCollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection<T>).ToTypeString()}) must have a size not equal to {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver<T>(IReadOnlyCollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection<T>).ToTypeString()}) must have a size over {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(IReadOnlyCollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection<T>).ToTypeString()}) must have a size of at least {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(IReadOnlyCollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection<T>).ToTypeString()}) must have a size less than {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(IReadOnlyCollection<T> collection, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection<T>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {collection.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(IReadOnlyCollection<T> source, ICollection<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(IReadOnlyCollection<T>).ToTypeString()}) must have a size equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(IReadOnlyCollection<T> source, ICollection<T> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(IReadOnlyCollection<T>).ToTypeString()}) must have a size less than or equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, IReadOnlyCollection<T> collection, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {collection.Count.ToAssertString()} to be a valid index for the target collection ({typeof(IReadOnlyCollection<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, IReadOnlyCollection<T> collection, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {collection.Count.ToAssertString()} to be an invalid index for the target collection ({typeof(IReadOnlyCollection<T>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
<#@include file="TypeInfo.ttinclude" #>
|
||||
/* ========================
|
||||
* Auto generated file
|
||||
* ===================== */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")]
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
<#
|
||||
GenerateTextForItems(EnumerableTypes, item =>
|
||||
{
|
||||
#>
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size not equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size over {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size of at least {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be an invalid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}");
|
||||
}
|
||||
<#
|
||||
});
|
||||
#>
|
||||
}
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
<#@ template language="C#"#>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ output extension=".g.cs"#>
|
||||
<#+
|
||||
/// <summary>
|
||||
/// A model representing the info on an enumerable type
|
||||
/// </summary>
|
||||
sealed class EnumerableTypeInfo
|
||||
{
|
||||
public EnumerableTypeInfo(
|
||||
string type,
|
||||
string xmlType,
|
||||
string name,
|
||||
string size,
|
||||
string destinationType,
|
||||
string cast)
|
||||
{
|
||||
Type = type;
|
||||
XmlType = xmlType;
|
||||
Name = name;
|
||||
Size = size;
|
||||
DestinationType = destinationType;
|
||||
Cast = cast;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the current type
|
||||
/// </summary>
|
||||
public string Type { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the XML-formatted name of the current type (eg. with {T} instead of <T>)
|
||||
/// </summary>
|
||||
public string XmlType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the variable name to use
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the property to use to retrieve the length of the current type
|
||||
/// </summary>
|
||||
public string Size { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not the current type has a "Count" property
|
||||
/// </summary>
|
||||
public bool HasCountProperty => Size == "Count";
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the destination type, when comparing counts across different collections
|
||||
/// </summary>
|
||||
public string DestinationType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the (optional) casting to resolve the diamond problem between different interfaces
|
||||
/// </summary>
|
||||
public string Cast { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of available enumerable types to generate APIs for
|
||||
/// </summary>
|
||||
static readonly IReadOnlyList<EnumerableTypeInfo> EnumerableTypes = new[]
|
||||
{
|
||||
new EnumerableTypeInfo("Span<T>", "<see cref=\"Span{T}\"/>", "span", "Length", "Span<T>", ""),
|
||||
new EnumerableTypeInfo("ReadOnlySpan<T>", "<see cref=\"ReadOnlySpan{T}\"/>", "span", "Length", "Span<T>", ""),
|
||||
new EnumerableTypeInfo("Memory<T>", "<see cref=\"Memory{T}\"/>", "memory", "Length", "Memory<T>", ""),
|
||||
new EnumerableTypeInfo("ReadOnlyMemory<T>", "<see cref=\"ReadOnlyMemory{T}\"/>", "memory", "Length", "Memory<T>", ""),
|
||||
new EnumerableTypeInfo("T[]", "<see typeparamref=\"T\"/> array", "array", "Length", "T[]", ""),
|
||||
new EnumerableTypeInfo("List<T>", "<see cref=\"List{T}\"/>", "list", "Count", "List<T>", "(ICollection<T>)"),
|
||||
new EnumerableTypeInfo("ICollection<T>", "<see cref=\"ICollection{T}\"/>", "collection", "Count", "ICollection<T>", ""),
|
||||
new EnumerableTypeInfo("IReadOnlyCollection<T>", "<see cref=\"IReadOnlyCollection{T}\"/>", "collection", "Count", "ICollection<T>", ""),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of available numeric types to generate APIs for
|
||||
/// </summary>
|
||||
static readonly IReadOnlyList<string> NumericTypes = new[] { "byte", "sbyte", "short", "ushort", "char", "int", "uint", "float", "long", "ulong", "double", "decimal" };
|
||||
|
||||
/// <summary>
|
||||
/// Generates text for a given sequence of items, automatically adding the necessary spacing
|
||||
/// </summary>
|
||||
void GenerateTextForItems<T>(IReadOnlyList<T> items, Action<T> factory)
|
||||
{
|
||||
for (int i = 0; i < items.Count; i++)
|
||||
{
|
||||
// Insert a blank line after the first item
|
||||
if (i > 0) WriteLine("");
|
||||
|
||||
// Invoke the factory with the current item
|
||||
factory(items [i]);
|
||||
}
|
||||
}
|
||||
#>
|
|
@ -0,0 +1,387 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to verify conditions when running code.
|
||||
/// </summary>
|
||||
public static partial class Guard
|
||||
{
|
||||
/// <summary>
|
||||
/// Asserts that the input value is <see langword="default"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see langword="struct"/> value type being tested.</typeparam>
|
||||
/// <param name="value">The input value to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not <see langword="default"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsDefault<T>(T value, string name)
|
||||
where T : struct, IEquatable<T>
|
||||
{
|
||||
if (!value.Equals(default))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsDefault(value, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value is not <see langword="default"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see langword="struct"/> value type being tested.</typeparam>
|
||||
/// <param name="value">The input value to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is <see langword="default"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotDefault<T>(T value, string name)
|
||||
where T : struct, IEquatable<T>
|
||||
{
|
||||
if (value.Equals(default))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotDefault<T>(name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="target">The target <typeparamref name="T"/> value to test for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is != <paramref name="target"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsEqualTo<T>(T value, T target, string name)
|
||||
where T : notnull, IEquatable<T>
|
||||
{
|
||||
if (!value.Equals(target))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be not equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="target">The target <typeparamref name="T"/> value to test for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is == <paramref name="target"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotEqualTo<T>(T value, T target, string name)
|
||||
where T : notnull, IEquatable<T>
|
||||
{
|
||||
if (value.Equals(target))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be a bitwise match with a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="target">The target <typeparamref name="T"/> value to test for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not a bitwise match for <paramref name="target"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsBitwiseEqualTo<T>(T value, T target, string name)
|
||||
where T : unmanaged
|
||||
{
|
||||
/* Include some fast paths if the input type is of size 1, 2, 4 or 8.
|
||||
* In those cases, just reinterpret the bytes as values of an integer type,
|
||||
* and compare them directly, which is much faster than having a loop over each byte.
|
||||
* The conditional branches below are known at compile time by the JIT compiler,
|
||||
* so that only the right one will actually be translated into native code. */
|
||||
if (typeof(T) == typeof(byte) ||
|
||||
typeof(T) == typeof(sbyte) ||
|
||||
typeof(T) == typeof(bool))
|
||||
{
|
||||
byte valueByte = Unsafe.As<T, byte>(ref value);
|
||||
byte targetByte = Unsafe.As<T, byte>(ref target);
|
||||
|
||||
if (valueByte != targetByte)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name);
|
||||
}
|
||||
}
|
||||
else if (typeof(T) == typeof(ushort) ||
|
||||
typeof(T) == typeof(short) ||
|
||||
typeof(T) == typeof(char))
|
||||
{
|
||||
ushort valueUShort = Unsafe.As<T, ushort>(ref value);
|
||||
ushort targetUShort = Unsafe.As<T, ushort>(ref target);
|
||||
|
||||
if (valueUShort != targetUShort)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name);
|
||||
}
|
||||
}
|
||||
else if (typeof(T) == typeof(uint) ||
|
||||
typeof(T) == typeof(int) ||
|
||||
typeof(T) == typeof(float))
|
||||
{
|
||||
uint valueUInt = Unsafe.As<T, uint>(ref value);
|
||||
uint targetUInt = Unsafe.As<T, uint>(ref target);
|
||||
|
||||
if (valueUInt != targetUInt)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name);
|
||||
}
|
||||
}
|
||||
else if (typeof(T) == typeof(ulong) ||
|
||||
typeof(T) == typeof(long) ||
|
||||
typeof(T) == typeof(double))
|
||||
{
|
||||
ulong valueULong = Unsafe.As<T, ulong>(ref value);
|
||||
ulong targetULong = Unsafe.As<T, ulong>(ref target);
|
||||
|
||||
if (valueULong != targetULong)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ref byte valueRef = ref Unsafe.As<T, byte>(ref value);
|
||||
ref byte targetRef = ref Unsafe.As<T, byte>(ref target);
|
||||
int bytesCount = Unsafe.SizeOf<T>();
|
||||
|
||||
for (int i = 0; i < bytesCount; i++)
|
||||
{
|
||||
byte valueByte = Unsafe.Add(ref valueRef, i);
|
||||
byte targetByte = Unsafe.Add(ref targetRef, i);
|
||||
|
||||
if (valueByte != targetByte)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be less than a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="maximum">The exclusive maximum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is >= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsLessThan<T>(T value, T maximum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(maximum) >= 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be less than or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="maximum">The inclusive maximum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is > <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsLessThanOrEqualTo<T>(T value, T maximum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(maximum) > 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be greater than a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is <= <paramref name="minimum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsGreaterThan<T>(T value, T minimum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(minimum) <= 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be greater than or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is < <paramref name="minimum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsGreaterThanOrEqualTo<T>(T value, T minimum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(minimum) < 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be in a given range.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is < <paramref name="minimum"/> or >= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> in [<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsInRange<T>(T value, T minimum, T maximum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) >= 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be in a given range.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is >= <paramref name="minimum"/> or < <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> not in [<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotInRange<T>(T value, T minimum, T maximum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) < 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be in a given interval.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is <= <paramref name="minimum"/> or >= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> in (<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsBetween<T>(T value, T minimum, T maximum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(minimum) <= 0 || value.CompareTo(maximum) >= 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be in a given interval.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is > <paramref name="minimum"/> or < <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> not in (<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotBetween<T>(T value, T minimum, T maximum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(minimum) > 0 && value.CompareTo(maximum) < 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be in a given interval.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The inclusive maximum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is < <paramref name="minimum"/> or > <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> in [<paramref name="minimum"/>, <paramref name="maximum"/>]", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsBetweenOrEqualTo<T>(T value, T minimum, T maximum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) > 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be in a given interval.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The inclusive maximum <typeparamref name="T"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is >= <paramref name="minimum"/> or <= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This API asserts the equivalent of "<paramref name="value"/> not in [<paramref name="minimum"/>, <paramref name="maximum"/>]", using arithmetic notation.
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotBetweenOrEqualTo<T>(T value, T minimum, T maximum, string name)
|
||||
where T : notnull, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) <= 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,166 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to verify conditions when running code.
|
||||
/// </summary>
|
||||
public static partial class Guard
|
||||
{
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be within a given distance from a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="int"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="int"/> value to test for.</param>
|
||||
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) > <paramref name="delta"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsCloseTo(int value, int target, uint delta, string name)
|
||||
{
|
||||
/* Cast to long before calculating the difference to avoid overflows
|
||||
* when the values are at the two extremes of the supported range.
|
||||
* Then cast to double to calculate the absolute value: this allows
|
||||
* the JIT compiler to use AVX instructions on X64 CPUs instead of
|
||||
* conditional jumps, which results in more efficient assembly code.
|
||||
* The IEEE 754 specs guarantees that a 32 bit integer value can
|
||||
* be stored within a double precision floating point value with
|
||||
* no loss of precision, so the result will always be correct here.
|
||||
* The difference is then cast to uint as that's the maximum possible
|
||||
* value it can have, and comparing two 32 bit integer values
|
||||
* results in shorter and slightly faster code than using doubles. */
|
||||
if ((uint)Math.Abs((double)((long)value - target)) > delta)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be within a given distance from a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="int"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="int"/> value to test for.</param>
|
||||
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) <= <paramref name="delta"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotCloseTo(int value, int target, uint delta, string name)
|
||||
{
|
||||
if ((uint)Math.Abs((double)((long)value - target)) <= delta)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be within a given distance from a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="long"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="long"/> value to test for.</param>
|
||||
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) > <paramref name="delta"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void IsCloseTo(long value, long target, ulong delta, string name)
|
||||
{
|
||||
/* This method and the one below are not inlined because
|
||||
* using the decimal type results in quite a bit of code. */
|
||||
if ((ulong)Math.Abs((decimal)value - target) > delta)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be within a given distance from a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="long"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="long"/> value to test for.</param>
|
||||
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) <= <paramref name="delta"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void IsNotCloseTo(long value, long target, ulong delta, string name)
|
||||
{
|
||||
if ((ulong)Math.Abs((decimal)value - target) <= delta)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be within a given distance from a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="float"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="float"/> value to test for.</param>
|
||||
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) > <paramref name="delta"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsCloseTo(float value, float target, float delta, string name)
|
||||
{
|
||||
if (Math.Abs(value - target) > delta)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be within a given distance from a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="float"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="float"/> value to test for.</param>
|
||||
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) <= <paramref name="delta"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotCloseTo(float value, float target, float delta, string name)
|
||||
{
|
||||
if (Math.Abs(value - target) <= delta)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be within a given distance from a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="double"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="double"/> value to test for.</param>
|
||||
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) > <paramref name="delta"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsCloseTo(double value, double target, double delta, string name)
|
||||
{
|
||||
if (Math.Abs(value - target) > delta)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be within a given distance from a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="double"/> value to test.</param>
|
||||
/// <param name="target">The target <see cref="double"/> value to test for.</param>
|
||||
/// <param name="delta">The maximum distance to allow between <paramref name="value"/> and <paramref name="target"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if (<paramref name="value"/> - <paramref name="target"/>) <= <paramref name="delta"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotCloseTo(double value, double target, double delta, string name)
|
||||
{
|
||||
if (Math.Abs(value - target) <= delta)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to verify conditions when running code.
|
||||
/// </summary>
|
||||
public static partial class Guard
|
||||
{
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Stream"/> instance must support reading.
|
||||
/// </summary>
|
||||
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> doesn't support reading.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CanRead(Stream stream, string name)
|
||||
{
|
||||
if (!stream.CanRead)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForCanRead(stream, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Stream"/> instance must support writing.
|
||||
/// </summary>
|
||||
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> doesn't support writing.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CanWrite(Stream stream, string name)
|
||||
{
|
||||
if (!stream.CanWrite)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForCanWrite(stream, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Stream"/> instance must support seeking.
|
||||
/// </summary>
|
||||
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> doesn't support seeking.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CanSeek(Stream stream, string name)
|
||||
{
|
||||
if (!stream.CanSeek)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForCanSeek(stream, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Stream"/> instance must be at the starting position.
|
||||
/// </summary>
|
||||
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> is not at the starting position.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsAtStartPosition(Stream stream, string name)
|
||||
{
|
||||
if (stream.Position != 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsAtStartPosition(stream, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,233 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to verify conditions when running code.
|
||||
/// </summary>
|
||||
public static partial class Guard
|
||||
{
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/> or empty.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor empty.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNullOrEmpty(string? text, string name)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNullOrEmpty(text, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or empty.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or empty.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotNullOrEmpty(string? text, string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrEmpty(text, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/> or whitespace.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNullOrWhitespace(string? text, string name)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNullOrWhitespace(text, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotNullOrWhitespace(string? text, string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrWhitespace(text, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must be empty.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsEmpty(string text, string name)
|
||||
{
|
||||
if (text.Length != 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEmpty(text, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must not be empty.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotEmpty(string text, string name)
|
||||
{
|
||||
if (text.Length == 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(text, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must be whitespace.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsWhitespace(string text, string name)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsWhitespace(text, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotWhitespace(string text, string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotWhitespace(text, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must have a size of a specified value.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is != <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeEqualTo(string text, int size, string name)
|
||||
{
|
||||
if (text.Length != size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(text, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must have a size not equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is == <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeNotEqualTo(string text, int size, string name)
|
||||
{
|
||||
if (text.Length == size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(text, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must have a size over a specified value.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is <= <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeOver(string text, int size, string name)
|
||||
{
|
||||
if (text.Length <= size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(text, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must have a size of at least specified value.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is < <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeAtLeast(string text, int size, string name)
|
||||
{
|
||||
if (text.Length < size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(text, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must have a size of less than a specified value.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is >= <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThan(string text, int size, string name)
|
||||
{
|
||||
if (text.Length >= size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(text, size, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="string"/> instance must have a size of less than or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is > <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThanOrEqualTo(string text, int size, string name)
|
||||
{
|
||||
if (text.Length > size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(text, size, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,170 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to verify conditions when running code.
|
||||
/// </summary>
|
||||
public static partial class Guard
|
||||
{
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance is in a completed state.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> is not in a completed state.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsCompleted(Task task, string name)
|
||||
{
|
||||
if (!task.IsCompleted)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsCompleted(task, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance is not in a completed state.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> is in a completed state.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotCompleted(Task task, string name)
|
||||
{
|
||||
if (task.IsCompleted)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotCompleted(task, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance has been completed successfully.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> has not been completed successfully.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsCompletedSuccessfully(Task task, string name)
|
||||
{
|
||||
if (task.Status != TaskStatus.RanToCompletion)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsCompletedSuccessfully(task, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance has not been completed successfully.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> has been completed successfully.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotCompletedSuccessfully(Task task, string name)
|
||||
{
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotCompletedSuccessfully(task, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance is faulted.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> is not faulted.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsFaulted(Task task, string name)
|
||||
{
|
||||
if (!task.IsFaulted)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsFaulted(task, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance is not faulted.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> is faulted.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotFaulted(Task task, string name)
|
||||
{
|
||||
if (task.IsFaulted)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotFaulted(task, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance is canceled.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> is not canceled.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsCanceled(Task task, string name)
|
||||
{
|
||||
if (!task.IsCanceled)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsCanceled(task, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance is not canceled.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> is canceled.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotCanceled(Task task, string name)
|
||||
{
|
||||
if (task.IsCanceled)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotCanceled(task, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance has a specific status.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="status">The task status that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> doesn't match <paramref name="status"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasStatusEqualTo(Task task, TaskStatus status, string name)
|
||||
{
|
||||
if (task.Status != status)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasStatusEqualTo(task, status, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <see cref="Task"/> instance has not a specific status.
|
||||
/// </summary>
|
||||
/// <param name="task">The input <see cref="Task"/> instance to test.</param>
|
||||
/// <param name="status">The task status that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="task"/> matches <paramref name="status"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasStatusNotEqualTo(Task task, TaskStatus status, string name)
|
||||
{
|
||||
if (task.Status == status)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasStatusNotEqualTo(task, status, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,288 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to verify conditions when running code.
|
||||
/// </summary>
|
||||
[DebuggerStepThrough]
|
||||
public static partial class Guard
|
||||
{
|
||||
/// <summary>
|
||||
/// Asserts that the input value is <see langword="null"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of reference value type being tested.</typeparam>
|
||||
/// <param name="value">The input value to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not <see langword="null"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1119", Justification = "Negated pattern match expression")]
|
||||
public static void IsNull<T>(T? value, string name)
|
||||
where T : class
|
||||
{
|
||||
if (!(value is null))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNull(value, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value is <see langword="null"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of nullable value type being tested.</typeparam>
|
||||
/// <param name="value">The input value to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not <see langword="null"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1119", Justification = "Negated pattern match expression")]
|
||||
public static void IsNull<T>(T? value, string name)
|
||||
where T : struct
|
||||
{
|
||||
if (!(value is null))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNull(value, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value is not <see langword="null"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of reference value type being tested.</typeparam>
|
||||
/// <param name="value">The input value to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <see langword="null"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotNull<T>(T? value, string name)
|
||||
where T : class
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentNullExceptionForIsNotNull<T>(name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value is not <see langword="null"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of nullable value type being tested.</typeparam>
|
||||
/// <param name="value">The input value to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <see langword="null"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotNull<T>(T? value, string name)
|
||||
where T : struct
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentNullExceptionForIsNotNull<T?>(name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value is of a specific type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input value.</typeparam>
|
||||
/// <param name="value">The input <see cref="object"/> to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not of type <typeparamref name="T"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsOfType<T>(object value, string name)
|
||||
{
|
||||
if (value.GetType() != typeof(T))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsOfType<T>(value, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value is not of a specific type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input value.</typeparam>
|
||||
/// <param name="value">The input <see cref="object"/> to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is of type <typeparamref name="T"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotOfType<T>(object value, string name)
|
||||
{
|
||||
if (value.GetType() == typeof(T))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotOfType<T>(value, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value is of a specific type.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="object"/> to test.</param>
|
||||
/// <param name="type">The type to look for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the type of <paramref name="value"/> is not the same as <paramref name="type"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsOfType(object value, Type type, string name)
|
||||
{
|
||||
if (value.GetType() != type)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsOfType(value, type, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value is not of a specific type.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="object"/> to test.</param>
|
||||
/// <param name="type">The type to look for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the type of <paramref name="value"/> is the same as <paramref name="type"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotOfType(object value, Type type, string name)
|
||||
{
|
||||
if (value.GetType() == type)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotOfType(value, type, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value can be assigned to a specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to check the input value against.</typeparam>
|
||||
/// <param name="value">The input <see cref="object"/> to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can't be assigned to type <typeparamref name="T"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsAssignableToType<T>(object value, string name)
|
||||
{
|
||||
if (!(value is T))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsAssignableToType<T>(value, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value can't be assigned to a specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to check the input value against.</typeparam>
|
||||
/// <param name="value">The input <see cref="object"/> to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can be assigned to type <typeparamref name="T"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotAssignableToType<T>(object value, string name)
|
||||
{
|
||||
if (value is T)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotAssignableToType<T>(value, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value can be assigned to a specified type.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="object"/> to test.</param>
|
||||
/// <param name="type">The type to look for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can't be assigned to <paramref name="type"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsAssignableToType(object value, Type type, string name)
|
||||
{
|
||||
if (!type.IsInstanceOfType(value))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsAssignableToType(value, type, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value can't be assigned to a specified type.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="object"/> to test.</param>
|
||||
/// <param name="type">The type to look for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can be assigned to <paramref name="type"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotAssignableToType(object value, Type type, string name)
|
||||
{
|
||||
if (type.IsInstanceOfType(value))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotAssignableToType(value, type, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be the same instance as the target value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="target">The target <typeparamref name="T"/> value to test for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not the same instance as <paramref name="target"/>.</exception>
|
||||
/// <remarks>The method is generic to prevent using it with value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsReferenceEqualTo<T>(T value, T target, string name)
|
||||
where T : class
|
||||
{
|
||||
if (!ReferenceEquals(value, target))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsReferenceEqualTo<T>(name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be the same instance as the target value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of input values to compare.</typeparam>
|
||||
/// <param name="value">The input <typeparamref name="T"/> value to test.</param>
|
||||
/// <param name="target">The target <typeparamref name="T"/> value to test for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is the same instance as <paramref name="target"/>.</exception>
|
||||
/// <remarks>The method is generic to prevent using it with value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsReferenceNotEqualTo<T>(T value, T target, string name)
|
||||
where T : class
|
||||
{
|
||||
if (ReferenceEquals(value, target))
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsReferenceNotEqualTo<T>(name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be <see langword="true"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="bool"/> to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is <see langword="false"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsTrue(bool value, string name)
|
||||
{
|
||||
if (!value)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsTrue(name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be <see langword="false"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see cref="bool"/> to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is <see langword="true"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsFalse(bool value, string name)
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsFalse(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")]
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEmpty{T}(Span{T},string)"/> fails.
|
||||
/// </summary>
|
||||
/// <remarks>This method is needed because <see cref="Span{T}"/> can't be used as a generic type parameter.</remarks>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span<T>).ToTypeString()}) must not be empty");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEmpty{T}(ReadOnlySpan{T},string)"/> fails.
|
||||
/// </summary>
|
||||
/// <remarks>This method is needed because <see cref="Span{T}"/> can't be used as a generic type parameter.</remarks>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan<T>).ToTypeString()}) must not be empty");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotEmpty<T>(string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be empty");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,148 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")]
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsDefault{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsDefault<T>(T value, string name)
|
||||
where T : struct
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be the default value {default(T).ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotDefault{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotDefault<T>(string name)
|
||||
where T : struct
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be the default value {default(T).ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEqualTo{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEqualTo<T>(T value, T target, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be equal to {target.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEqualTo{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotEqualTo<T>(T value, T target, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be equal to {target.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsLessThan{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsLessThan<T>(T value, T maximum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than {maximum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsLessThanOrEqualTo{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo<T>(T value, T maximum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than or equal to {maximum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsGreaterThan{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan<T>(T value, T minimum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than {minimum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsGreaterThanOrEqualTo{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo<T>(T value, T minimum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than or equal to {minimum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRange{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRange<T>(T value, T minimum, T maximum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be in the range given by {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRange{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange<T>(T value, T minimum, T maximum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be in the range given by {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsBetween{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsBetween<T>(T value, T minimum, T maximum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be between {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotBetween{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween<T>(T value, T minimum, T maximum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be between {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsBetweenOrEqualTo{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo<T>(T value, T minimum, T maximum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be between or equal to {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotBetweenOrEqualTo{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo<T>(T value, T minimum, T maximum, string name)
|
||||
{
|
||||
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be between or equal to {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")]
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsCloseTo(int,int,uint,string)"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsCloseTo(int value, int target, uint delta, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(int).ToTypeString()}) must be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs((double)((long)value - target)).ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotCloseTo(int,int,uint,string)"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotCloseTo(int value, int target, uint delta, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(int).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs((double)((long)value - target)).ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsCloseTo(long,long,ulong,string)"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsCloseTo(long value, long target, ulong delta, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(long).ToTypeString()}) must be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs((decimal)value - target).ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotCloseTo(long,long,ulong,string)"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotCloseTo(long value, long target, ulong delta, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(long).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs((decimal)value - target).ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsCloseTo(float,float,float,string)"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsCloseTo(float value, float target, float delta, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(float).ToTypeString()}) must be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotCloseTo(float,float,float,string)"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotCloseTo(float value, float target, float delta, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(float).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsCloseTo(double,double,double,string)"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsCloseTo(double value, double target, double delta, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(double).ToTypeString()}) must be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotCloseTo(double,double,double,string)"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotCloseTo(double value, double target, double delta, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(double).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")]
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.CanRead"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForCanRead(Stream stream, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) doesn't support reading");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.CanWrite"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForCanWrite(Stream stream, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) doesn't support writing");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.CanSeek"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForCanSeek(Stream stream, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) doesn't support seeking");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsAtStartPosition"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsAtStartPosition(Stream stream, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) must be at position {0.ToAssertString()}, was at {stream.Position.ToAssertString()}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,145 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")]
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNullOrEmpty"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be null or empty, was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotNullOrEmpty"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be null or empty, was {(text is null ? "null" : "empty")}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNullOrWhitespace"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNullOrWhitespace(string? text, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be null or whitespace, was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotNullOrWhitespace"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotNullOrWhitespace(string? text, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsEmpty(string text, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be empty, was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEmpty"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotEmpty(string text, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be empty");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsWhitespace"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsWhitespace(string text, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be whitespace, was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotWhitespace"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotWhitespace(string text, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be whitespace, was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size equal to {size}, had a size of {text.Length} and was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not have a size equal to {size}, was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size over {size}, had a size of {text.Length} and was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeAtLeast"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size of at least {size}, had a size of {text.Length} and was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size less than {size}, had a size of {text.Length} and was {text.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string text, int size, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size less than or equal to {size}, had a size of {text.Length} and was {text.ToAssertString()}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")]
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsCompleted"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsCompleted(Task task, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed, had status {task.Status.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotCompleted"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotCompleted(Task task, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed, had status {task.Status.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsCompletedSuccessfully"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed successfully, had status {task.Status.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotCompletedSuccessfully"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed successfully, had status {task.Status.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsFaulted"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsFaulted(Task task, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be faulted, had status {task.Status.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotFaulted"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be faulted, had status {task.Status.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsCanceled"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsCanceled(Task task, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be canceled, had status {task.Status.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotCanceled"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be canceled, had status {task.Status.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasStatusEqualTo"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasStatusEqualTo(Task task, TaskStatus status, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must have status {status}, had status {task.Status.ToAssertString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasStatusNotEqualTo"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForHasStatusNotEqualTo(Task task, TaskStatus status, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not have status {status.ToAssertString()}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a formatted representation of the input value.
|
||||
/// </summary>
|
||||
/// <param name="obj">The input <see cref="object"/> to format.</param>
|
||||
/// <returns>A formatted representation of <paramref name="obj"/> to display in error messages.</returns>
|
||||
[Pure]
|
||||
private static string ToAssertString(this object? obj)
|
||||
{
|
||||
return obj switch
|
||||
{
|
||||
string _ => $"\"{obj}\"",
|
||||
null => "null",
|
||||
_ => $"<{obj}>"
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentException"/>.
|
||||
/// </summary>
|
||||
/// <param name="name">The argument name.</param>
|
||||
/// <param name="message">The message to include in the exception.</param>
|
||||
/// <exception cref="ArgumentException">Thrown with <paramref name="message"/> and <paramref name="name"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void ThrowArgumentException(string name, string message)
|
||||
{
|
||||
throw new ArgumentException(message, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentNullException"/>.
|
||||
/// </summary>
|
||||
/// <param name="name">The argument name.</param>
|
||||
/// <param name="message">The message to include in the exception.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown with <paramref name="name"/> and <paramref name="message"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void ThrowArgumentNullException(string name, string message)
|
||||
{
|
||||
throw new ArgumentNullException(name, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentOutOfRangeException"/>.
|
||||
/// </summary>
|
||||
/// <param name="name">The argument name.</param>
|
||||
/// <param name="message">The message to include in the exception.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown with <paramref name="name"/> and <paramref name="message"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void ThrowArgumentOutOfRangeException(string name, string message)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(name, message);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,171 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to throw exceptions
|
||||
/// </summary>
|
||||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")]
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
#pragma warning disable CS0419
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNull{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNull<T>(T value, string name)
|
||||
where T : class
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be null, was {value.ToAssertString()} ({value.GetType().ToTypeString()})");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNull{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNull<T>(T? value, string name)
|
||||
where T : struct
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T?).ToTypeString()}) must be null, was {value.ToAssertString()} ({typeof(T).ToTypeString()})");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentNullException"/> when <see cref="Guard.IsNotNull{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentNullExceptionForIsNotNull<T>(string name)
|
||||
{
|
||||
ThrowArgumentNullException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be not null)");
|
||||
}
|
||||
#pragma warning restore CS0419
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsOfType{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsOfType<T>(object value, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotOfType{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotOfType<T>(object value, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must not be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsOfType"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsOfType(object value, Type type, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotOfType"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotOfType(object value, Type type, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must not be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsAssignableToType{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsAssignableToType<T>(object value, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotAssignableToType{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotAssignableToType<T>(object value, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must not be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsAssignableToType"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsAssignableToType(object value, Type type, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsAssignableToType"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsNotAssignableToType(object value, Type type, string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must not be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsBitwiseEqualTo{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForsBitwiseEqualTo<T>(T value, T target, string name)
|
||||
where T : unmanaged
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) is not a bitwise match, was <{value.ToHexString()}> instead of <{target.ToHexString()}>");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsReferenceEqualTo{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsReferenceEqualTo<T>(string name)
|
||||
where T : class
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be the same instance as the target object");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsReferenceNotEqualTo{T}"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsReferenceNotEqualTo<T>(string name)
|
||||
where T : class
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be the same instance as the target object");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsTrue"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsTrue(string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be true, was false");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsFalse"/> fails.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static void ThrowArgumentExceptionForIsFalse(string name)
|
||||
{
|
||||
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be false, was true");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,7 +5,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.Toolkit.Extensions
|
||||
{
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Microsoft.Toolkit.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Helpers for working with types.
|
||||
/// </summary>
|
||||
public static class TypeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of built-in types to their simple representation.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<Type, string> BuiltInTypesMap = new Dictionary<Type, string>
|
||||
{
|
||||
[typeof(bool)] = "bool",
|
||||
[typeof(byte)] = "byte",
|
||||
[typeof(sbyte)] = "sbyte",
|
||||
[typeof(short)] = "short",
|
||||
[typeof(ushort)] = "ushort",
|
||||
[typeof(char)] = "char",
|
||||
[typeof(int)] = "int",
|
||||
[typeof(uint)] = "uint",
|
||||
[typeof(float)] = "float",
|
||||
[typeof(long)] = "long",
|
||||
[typeof(ulong)] = "ulong",
|
||||
[typeof(double)] = "double",
|
||||
[typeof(decimal)] = "decimal",
|
||||
[typeof(object)] = "object",
|
||||
[typeof(string)] = "string"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// A thread-safe mapping of precomputed string representation of types.
|
||||
/// </summary>
|
||||
private static readonly ConditionalWeakTable<Type, string> DisplayNames = new ConditionalWeakTable<Type, string>();
|
||||
|
||||
/// <summary>
|
||||
/// Returns a simple string representation of a type.
|
||||
/// </summary>
|
||||
/// <param name="type">The input type.</param>
|
||||
/// <returns>The string representation of <paramref name="type"/>.</returns>
|
||||
[Pure]
|
||||
public static string ToTypeString(this Type type)
|
||||
{
|
||||
// Local function to create the formatted string for a given type
|
||||
static string FormatDisplayString(Type type)
|
||||
{
|
||||
// Primitive types use the keyword name
|
||||
if (BuiltInTypesMap.TryGetValue(type, out string? typeName))
|
||||
{
|
||||
return typeName!;
|
||||
}
|
||||
|
||||
// Generic types
|
||||
if (type.IsGenericType &&
|
||||
type.FullName is { } fullName &&
|
||||
fullName.Split('`') is { } tokens &&
|
||||
tokens.Length > 0 &&
|
||||
tokens[0] is { } genericName &&
|
||||
genericName.Length > 0)
|
||||
{
|
||||
var typeArguments = type.GetGenericArguments().Select(FormatDisplayString);
|
||||
|
||||
// Nullable<T> types are displayed as T?
|
||||
var genericType = type.GetGenericTypeDefinition();
|
||||
if (genericType == typeof(Nullable<>))
|
||||
{
|
||||
return $"{typeArguments.First()}?";
|
||||
}
|
||||
|
||||
// ValueTuple<T1, T2> types are displayed as (T1, T2)
|
||||
if (genericType == typeof(ValueTuple<>) ||
|
||||
genericType == typeof(ValueTuple<,>) ||
|
||||
genericType == typeof(ValueTuple<,,>) ||
|
||||
genericType == typeof(ValueTuple<,,,>) ||
|
||||
genericType == typeof(ValueTuple<,,,,>) ||
|
||||
genericType == typeof(ValueTuple<,,,,,>) ||
|
||||
genericType == typeof(ValueTuple<,,,,,,>) ||
|
||||
genericType == typeof(ValueTuple<,,,,,,,>))
|
||||
{
|
||||
return $"({string.Join(", ", typeArguments)})";
|
||||
}
|
||||
|
||||
// Standard generic types are displayed as Foo<T>
|
||||
return $"{genericName}<{string.Join(", ", typeArguments)}>";
|
||||
}
|
||||
|
||||
// Array types are displayed as Foo[]
|
||||
if (type.IsArray)
|
||||
{
|
||||
var elementType = type.GetElementType();
|
||||
var rank = type.GetArrayRank();
|
||||
|
||||
return $"{FormatDisplayString(elementType)}[{new string(',', rank - 1)}]";
|
||||
}
|
||||
|
||||
return type.ToString();
|
||||
}
|
||||
|
||||
/* Atomically get or build the display string for the current type.
|
||||
* Manually create a static lambda here to enable caching of the generated closure.
|
||||
* This is a workaround for the missing caching for method group conversions, and should
|
||||
* be removed once this issue is resolved: https://github.com/dotnet/roslyn/issues/5835. */
|
||||
return DisplayNames.GetValue(type, t => FormatDisplayString(t));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Microsoft.Toolkit.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Helpers for working with value types.
|
||||
/// </summary>
|
||||
public static class ValueTypeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the table of hex characters (doesn't allocate, maps to .text section, see <see href="https://github.com/dotnet/roslyn/pull/24621"/>)
|
||||
/// </summary>
|
||||
private static ReadOnlySpan<byte> HexCharactersTable => new[]
|
||||
{
|
||||
(byte)'0', (byte)'1', (byte)'2', (byte)'3',
|
||||
(byte)'4', (byte)'5', (byte)'6', (byte)'7',
|
||||
(byte)'8', (byte)'9', (byte)'A', (byte)'B',
|
||||
(byte)'C', (byte)'D', (byte)'E', (byte)'F'
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Returns a hexadecimal <see cref="string"/> representation of a given <typeparamref name="T"/> value, left-padded and ordered as big-endian.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The input type to format to <see cref="string"/>.</typeparam>
|
||||
/// <param name="value">The input value to format to <see cref="string"/>.</param>
|
||||
/// <returns>
|
||||
/// The hexadecimal representation of <paramref name="value"/> (with the '0x' prefix), left-padded to byte boundaries and ordered as big-endian.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// As a byte (8 bits) is represented by two hexadecimal digits (each representing a group of 4 bytes), each <see cref="string"/>
|
||||
/// representation will always contain an even number of digits. For instance:
|
||||
/// <code>
|
||||
/// Console.WriteLine(1.ToHexString()); // "0x01"
|
||||
/// Console.WriteLine(((byte)255).ToHexString()); // "0xFF"
|
||||
/// Console.WriteLine((-1).ToHexString()); // "0xFFFFFFFF"
|
||||
/// </code>
|
||||
/// </remarks>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static unsafe string ToHexString<T>(this T value)
|
||||
where T : unmanaged
|
||||
{
|
||||
int
|
||||
sizeOfT = Unsafe.SizeOf<T>(),
|
||||
bufferSize = (2 * sizeOfT) + 2;
|
||||
char* p = stackalloc char[bufferSize];
|
||||
|
||||
p[0] = '0';
|
||||
p[1] = 'x';
|
||||
|
||||
ref byte r0 = ref Unsafe.As<T, byte>(ref value);
|
||||
ref byte rh = ref MemoryMarshal.GetReference(HexCharactersTable);
|
||||
|
||||
for (int i = 0, j = bufferSize - 2; i < sizeOfT; i++, j -= 2)
|
||||
{
|
||||
byte b = Unsafe.Add(ref r0, i);
|
||||
int
|
||||
low = b & 0x0F,
|
||||
high = (b & 0xF0) >> 4;
|
||||
|
||||
p[j + 1] = (char)Unsafe.Add(ref rh, low);
|
||||
p[j] = (char)Unsafe.Add(ref rh, high);
|
||||
}
|
||||
|
||||
return new string(p, 0, bufferSize);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,7 +2,6 @@
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<Title>Windows Community Toolkit .NET Standard</Title>
|
||||
<Description>
|
||||
This package includes .NET Standard code only helpers such as:
|
||||
|
@ -14,4 +16,55 @@
|
|||
<DebugType>Full</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Memory" Version="4.5.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Diagnostics\Generated\Guard.Comparable.Numeric.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Guard.Comparable.Numeric.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Diagnostics\Generated\Guard.Collection.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Guard.Collection.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Diagnostics\Generated\ThrowHelper.Collection.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>ThrowHelper.Collection.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Diagnostics\Generated\TypeInfo.ttinclude">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>TypeInfo.g.cs</LastGenOutput>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<!-- T4 service used by the Guard APIs -->
|
||||
<ItemGroup>
|
||||
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Diagnostics\Generated\Guard.Comparable.Numeric.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Guard.Comparable.Numeric.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Diagnostics\Generated\Guard.Collection.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Guard.Collection.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Diagnostics\Generated\ThrowHelper.Collection.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>ThrowHelper.Collection.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Diagnostics\Generated\TypeInfo.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>TypeInfo.ttinclude</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,182 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace UnitTests.Diagnostics
|
||||
{
|
||||
public partial class Test_Guard
|
||||
{
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsEmpty_ArrayOk()
|
||||
{
|
||||
Guard.IsEmpty(new int[0], nameof(Test_Guard_IsEmpty_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsEmpty_ArrayFail()
|
||||
{
|
||||
Guard.IsEmpty(new int[1], nameof(Test_Guard_IsEmpty_ArrayFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsNotEmpty_ArrayOk()
|
||||
{
|
||||
Guard.IsNotEmpty(new int[1], nameof(Test_Guard_IsNotEmpty_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsNotEmpty_ArrayFail()
|
||||
{
|
||||
Guard.IsNotEmpty(new int[0], nameof(Test_Guard_IsNotEmpty_ArrayFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_HasSizeEqualTo_ArrayOk()
|
||||
{
|
||||
Guard.HasSizeEqualTo(new int[4], 4, nameof(Test_Guard_HasSizeEqualTo_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeEqualTo_ArrayFail()
|
||||
{
|
||||
Guard.HasSizeEqualTo(new int[3], 4, nameof(Test_Guard_HasSizeEqualTo_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_HasSizeNotEqualTo_ArrayOk()
|
||||
{
|
||||
Guard.HasSizeNotEqualTo(new int[3], 4, nameof(Test_Guard_HasSizeNotEqualTo_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeNotEqualTo_ArrayFail()
|
||||
{
|
||||
Guard.HasSizeNotEqualTo(new int[4], 4, nameof(Test_Guard_HasSizeNotEqualTo_ArrayFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_HasSizeOver_ArrayOk()
|
||||
{
|
||||
Guard.HasSizeOver(new int[5], 2, nameof(Test_Guard_HasSizeOver_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeOver_ArrayEqualFail()
|
||||
{
|
||||
Guard.HasSizeOver(new int[4], 4, nameof(Test_Guard_HasSizeOver_ArrayEqualFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeOver_ArraSmallerFail()
|
||||
{
|
||||
Guard.HasSizeOver(new int[1], 4, nameof(Test_Guard_HasSizeOver_ArraSmallerFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_HasSizeAtLeast_ArrayOk()
|
||||
{
|
||||
Guard.HasSizeAtLeast(new int[5], 2, nameof(Test_Guard_HasSizeAtLeast_ArrayOk));
|
||||
Guard.HasSizeAtLeast(new int[2], 2, nameof(Test_Guard_HasSizeAtLeast_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeAtLeast_ArrayFail()
|
||||
{
|
||||
Guard.HasSizeOver(new int[1], 4, nameof(Test_Guard_HasSizeAtLeast_ArrayFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_HasSizeLessThan_ArrayOk()
|
||||
{
|
||||
Guard.HasSizeLessThan(new int[1], 5, nameof(Test_Guard_HasSizeLessThan_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeLessThan_ArrayEqualFail()
|
||||
{
|
||||
Guard.HasSizeLessThan(new int[4], 4, nameof(Test_Guard_HasSizeLessThan_ArrayEqualFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeLessThan_ArrayGreaterFail()
|
||||
{
|
||||
Guard.HasSizeLessThan(new int[6], 4, nameof(Test_Guard_HasSizeLessThan_ArrayGreaterFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_HasSizeLessThanOrEqualTo_ArrayOk()
|
||||
{
|
||||
Guard.HasSizeLessThanOrEqualTo(new int[1], 5, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayOk));
|
||||
Guard.HasSizeLessThanOrEqualTo(new int[5], 5, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail()
|
||||
{
|
||||
Guard.HasSizeLessThanOrEqualTo(new int[8], 4, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_HasSizeEqualToArray_ArrayOk()
|
||||
{
|
||||
Guard.HasSizeEqualTo(new int[1], new int[1], nameof(Test_Guard_HasSizeEqualToArray_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeEqualToArray_ArrayFail()
|
||||
{
|
||||
Guard.HasSizeEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeEqualToArray_ArrayFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_HasSizeLessThanOrEqualToArray_ArrayOk()
|
||||
{
|
||||
Guard.HasSizeLessThanOrEqualTo(new int[2], new int[5], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayOk));
|
||||
Guard.HasSizeLessThanOrEqualTo(new int[4], new int[4], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayOk));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail()
|
||||
{
|
||||
Guard.HasSizeLessThanOrEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace UnitTests.Diagnostics
|
||||
{
|
||||
public partial class Test_Guard
|
||||
{
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsCloseToInt_Ok()
|
||||
{
|
||||
Guard.IsCloseTo(0, 5, 10, nameof(Test_Guard_IsCloseToInt_Ok));
|
||||
Guard.IsCloseTo(0, 5, 5, nameof(Test_Guard_IsCloseToInt_Ok));
|
||||
Guard.IsCloseTo(0, int.MaxValue, int.MaxValue, nameof(Test_Guard_IsCloseToInt_Ok));
|
||||
Guard.IsCloseTo(-500, -530, 50, nameof(Test_Guard_IsCloseToInt_Ok));
|
||||
Guard.IsCloseTo(1000, 800, 200, nameof(Test_Guard_IsCloseToInt_Ok));
|
||||
Guard.IsCloseTo(int.MaxValue, int.MaxValue - 10, 10, nameof(Test_Guard_IsCloseToInt_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")]
|
||||
public void Test_Guard_IsCloseToInt_Fail()
|
||||
{
|
||||
foreach (var item in new (int Value, int Target, uint Delta)[]
|
||||
{
|
||||
(0, 20, 10),
|
||||
(0, 6, 5),
|
||||
(0, int.MaxValue, 500),
|
||||
(-500, -530, 10),
|
||||
(1000, 800, 100),
|
||||
(int.MaxValue, int.MaxValue - 10, 7),
|
||||
(int.MinValue, int.MaxValue, int.MaxValue)
|
||||
})
|
||||
{
|
||||
bool fail = false;
|
||||
|
||||
try
|
||||
{
|
||||
Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToInt_Fail));
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
fail = true;
|
||||
}
|
||||
|
||||
Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsCloseToFloat_Ok()
|
||||
{
|
||||
Guard.IsCloseTo(0f, 5, 10, nameof(Test_Guard_IsCloseToFloat_Ok));
|
||||
Guard.IsCloseTo(0f, 5, 5, nameof(Test_Guard_IsCloseToFloat_Ok));
|
||||
Guard.IsCloseTo(0f, float.MaxValue, float.MaxValue, nameof(Test_Guard_IsCloseToFloat_Ok));
|
||||
Guard.IsCloseTo(-500f, -530, 50, nameof(Test_Guard_IsCloseToFloat_Ok));
|
||||
Guard.IsCloseTo(1000f, 800, 200, nameof(Test_Guard_IsCloseToFloat_Ok));
|
||||
Guard.IsCloseTo(float.MaxValue, float.MaxValue - 10, 10, nameof(Test_Guard_IsCloseToFloat_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")]
|
||||
public void Test_Guard_IsCloseToFloat_Fail()
|
||||
{
|
||||
foreach (var item in new (float Value, float Target, float Delta)[]
|
||||
{
|
||||
(0, 20, 10),
|
||||
(0, 6, 5),
|
||||
(0, float.MaxValue, 500),
|
||||
(-500, -530, 10),
|
||||
(1000, 800, 100),
|
||||
(float.MaxValue, float.MaxValue / 2, 7),
|
||||
(float.MinValue, float.MaxValue, float.MaxValue)
|
||||
})
|
||||
{
|
||||
bool fail = false;
|
||||
|
||||
try
|
||||
{
|
||||
Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToFloat_Fail));
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
fail = true;
|
||||
}
|
||||
|
||||
Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,561 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace UnitTests.Diagnostics
|
||||
{
|
||||
[TestClass]
|
||||
public partial class Test_Guard
|
||||
{
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsNull_Ok()
|
||||
{
|
||||
Guard.IsNull<object>(null, nameof(Test_Guard_IsNull_Ok));
|
||||
Guard.IsNull<int>(null, nameof(Test_Guard_IsNull_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsNull_ClassFail()
|
||||
{
|
||||
Guard.IsNull(new object(), nameof(Test_Guard_IsNull_ClassFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsNull_StructFail()
|
||||
{
|
||||
Guard.IsNull<int>(7, nameof(Test_Guard_IsNull_StructFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsNotNull_Ok()
|
||||
{
|
||||
Guard.IsNotNull(new object(), nameof(Test_Guard_IsNotNull_Ok));
|
||||
Guard.IsNotNull<int>(7, nameof(Test_Guard_IsNotNull_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void Test_Guard_IsNotNull_ClassFail()
|
||||
{
|
||||
Guard.IsNotNull<object>(null, nameof(Test_Guard_IsNotNull_ClassFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void Test_Guard_IsNotNull_StructFail()
|
||||
{
|
||||
Guard.IsNotNull<int>(null, nameof(Test_Guard_IsNotNull_StructFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsOfT_Ok()
|
||||
{
|
||||
Guard.IsOfType<string>("Hello", nameof(Test_Guard_IsOfT_Ok));
|
||||
Guard.IsOfType<int>(7, nameof(Test_Guard_IsOfT_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsOfT_Fail()
|
||||
{
|
||||
Guard.IsOfType<string>(7, nameof(Test_Guard_IsOfT_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsOfType_Ok()
|
||||
{
|
||||
Guard.IsOfType("Hello", typeof(string), nameof(Test_Guard_IsOfType_Ok));
|
||||
Guard.IsOfType(7, typeof(int), nameof(Test_Guard_IsOfType_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsOfType_Fail()
|
||||
{
|
||||
Guard.IsOfType(7, typeof(string), nameof(Test_Guard_IsOfType_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsAssignableToT_Ok()
|
||||
{
|
||||
Guard.IsAssignableToType<string>("Hello", nameof(Test_Guard_IsAssignableToT_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsAssignableToT_Fail()
|
||||
{
|
||||
Guard.IsAssignableToType<string>(7, nameof(Test_Guard_IsAssignableToT_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsAssignableToType_Ok()
|
||||
{
|
||||
Guard.IsAssignableToType("Hello", typeof(string), nameof(Test_Guard_IsAssignableToType_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsAssignableToType_Fail()
|
||||
{
|
||||
Guard.IsAssignableToType(7, typeof(string), nameof(Test_Guard_IsAssignableToType_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsEqualTo_Ok()
|
||||
{
|
||||
Guard.IsEqualTo("Hello", "Hello", nameof(Test_Guard_IsEqualTo_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsEqualTo_Fail()
|
||||
{
|
||||
Guard.IsEqualTo("Hello", "World", nameof(Test_Guard_IsEqualTo_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsNotEqualTo_Ok()
|
||||
{
|
||||
Guard.IsNotEqualTo("Hello", "World", nameof(Test_Guard_IsNotEqualTo_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsNotEqualTo_Fail()
|
||||
{
|
||||
Guard.IsNotEqualTo("Hello", "Hello", nameof(Test_Guard_IsNotEqualTo_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsBitwiseEqualTo_Ok()
|
||||
{
|
||||
Guard.IsBitwiseEqualTo(byte.MaxValue, byte.MaxValue, nameof(Test_Guard_IsBitwiseEqualTo_Ok));
|
||||
Guard.IsBitwiseEqualTo(DateTime.MaxValue, DateTime.MaxValue, nameof(Test_Guard_IsBitwiseEqualTo_Ok));
|
||||
Guard.IsBitwiseEqualTo(double.Epsilon, double.Epsilon, nameof(Test_Guard_IsBitwiseEqualTo_Ok));
|
||||
Guard.IsBitwiseEqualTo(MathF.PI, MathF.PI, nameof(Test_Guard_IsBitwiseEqualTo_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsBitwiseEqualTo_SingleFail()
|
||||
{
|
||||
Guard.IsBitwiseEqualTo(double.PositiveInfinity, double.Epsilon, nameof(Test_Guard_IsBitwiseEqualTo_SingleFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsBitwiseEqualTo_LoopFail()
|
||||
{
|
||||
Guard.IsBitwiseEqualTo(DateTime.Now, DateTime.Today, nameof(Test_Guard_IsBitwiseEqualTo_LoopFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsReferenceEqualTo_Ok()
|
||||
{
|
||||
var obj = new object();
|
||||
|
||||
Guard.IsReferenceEqualTo(obj, obj, nameof(Test_Guard_IsReferenceEqualTo_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsReferenceEqualTo_Fail()
|
||||
{
|
||||
Guard.IsReferenceEqualTo(new object(), new object(), nameof(Test_Guard_IsReferenceEqualTo_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsReferenceNotEqualTo_Ok()
|
||||
{
|
||||
Guard.IsReferenceNotEqualTo(new object(), new object(), nameof(Test_Guard_IsReferenceEqualTo_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsReferenceNotEqualTo_Fail()
|
||||
{
|
||||
var obj = new object();
|
||||
|
||||
Guard.IsReferenceNotEqualTo(obj, obj, nameof(Test_Guard_IsReferenceEqualTo_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsTrue_Ok()
|
||||
{
|
||||
Guard.IsTrue(true, nameof(Test_Guard_IsTrue_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsTrue_Fail()
|
||||
{
|
||||
Guard.IsTrue(false, nameof(Test_Guard_IsTrue_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsFalse_Ok()
|
||||
{
|
||||
Guard.IsFalse(false, nameof(Test_Guard_IsFalse_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void Test_Guard_IsFalse_Fail()
|
||||
{
|
||||
Guard.IsFalse(true, nameof(Test_Guard_IsFalse_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsLessThan_Ok()
|
||||
{
|
||||
Guard.IsLessThan(1, 2, nameof(Test_Guard_IsLessThan_Ok));
|
||||
Guard.IsLessThan(1.2f, 3.14f, nameof(Test_Guard_IsLessThan_Ok));
|
||||
Guard.IsLessThan(DateTime.Now, DateTime.MaxValue, nameof(Test_Guard_IsLessThan_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsLessThan_EqualsFalse()
|
||||
{
|
||||
Guard.IsLessThan(1, 1, nameof(Test_Guard_IsLessThan_EqualsFalse));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsLessThan_GreaterFalse()
|
||||
{
|
||||
Guard.IsLessThan(2, 1, nameof(Test_Guard_IsLessThan_GreaterFalse));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsLessThanOrEqualTo_Ok()
|
||||
{
|
||||
Guard.IsLessThanOrEqualTo(1, 2, nameof(Test_Guard_IsLessThanOrEqualTo_Ok));
|
||||
Guard.IsLessThanOrEqualTo(1, 1, nameof(Test_Guard_IsLessThanOrEqualTo_Ok));
|
||||
Guard.IsLessThanOrEqualTo(0.1f, MathF.PI, nameof(Test_Guard_IsLessThanOrEqualTo_Ok));
|
||||
Guard.IsLessThanOrEqualTo(MathF.PI, MathF.PI, nameof(Test_Guard_IsLessThanOrEqualTo_Ok));
|
||||
Guard.IsLessThanOrEqualTo(DateTime.Today, DateTime.MaxValue, nameof(Test_Guard_IsLessThanOrEqualTo_Ok));
|
||||
Guard.IsLessThanOrEqualTo(DateTime.MaxValue, DateTime.MaxValue, nameof(Test_Guard_IsLessThanOrEqualTo_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsLessThanOrEqualTo_False()
|
||||
{
|
||||
Guard.IsLessThanOrEqualTo(2, 1, nameof(Test_Guard_IsLessThanOrEqualTo_False));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsGreaterThan_Ok()
|
||||
{
|
||||
Guard.IsGreaterThan(2, 1, nameof(Test_Guard_IsGreaterThan_Ok));
|
||||
Guard.IsGreaterThan(3.14f, 2.1f, nameof(Test_Guard_IsGreaterThan_Ok));
|
||||
Guard.IsGreaterThan(DateTime.MaxValue, DateTime.Today, nameof(Test_Guard_IsGreaterThan_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsGreaterThan_EqualsFalse()
|
||||
{
|
||||
Guard.IsGreaterThan(1, 1, nameof(Test_Guard_IsGreaterThan_EqualsFalse));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsGreaterThan_LowerFalse()
|
||||
{
|
||||
Guard.IsGreaterThan(1, 2, nameof(Test_Guard_IsGreaterThan_LowerFalse));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsGreaterThanOrEqualTo_Ok()
|
||||
{
|
||||
Guard.IsGreaterThanOrEqualTo(2,1, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok));
|
||||
Guard.IsGreaterThanOrEqualTo(1, 1, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok));
|
||||
Guard.IsGreaterThanOrEqualTo(MathF.PI, 1, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok));
|
||||
Guard.IsGreaterThanOrEqualTo(MathF.PI, MathF.PI, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok));
|
||||
Guard.IsGreaterThanOrEqualTo(DateTime.MaxValue, DateTime.Today, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok));
|
||||
Guard.IsGreaterThanOrEqualTo(DateTime.MaxValue, DateTime.MaxValue, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsGreaterThanOrEqualTo_False()
|
||||
{
|
||||
Guard.IsGreaterThanOrEqualTo(1, 2, nameof(Test_Guard_IsGreaterThanOrEqualTo_False));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsInRange_Ok()
|
||||
{
|
||||
Guard.IsInRange(1, 0, 4, nameof(Test_Guard_IsInRange_Ok));
|
||||
Guard.IsInRange(0, 0, 2, nameof(Test_Guard_IsInRange_Ok));
|
||||
Guard.IsInRange(3.14f, 0, 10, nameof(Test_Guard_IsInRange_Ok));
|
||||
Guard.IsInRange(1, 0, 3.14f, nameof(Test_Guard_IsInRange_Ok));
|
||||
Guard.IsInRange(1, -50, 2, nameof(Test_Guard_IsInRange_Ok));
|
||||
Guard.IsInRange(-44, -44, 0, nameof(Test_Guard_IsInRange_Ok));
|
||||
Guard.IsInRange(3.14f, -float.Epsilon, 22, nameof(Test_Guard_IsInRange_Ok));
|
||||
Guard.IsInRange(1, int.MinValue, int.MaxValue, nameof(Test_Guard_IsInRange_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsInRange_LowerFail()
|
||||
{
|
||||
Guard.IsInRange(-3, 0, 4, nameof(Test_Guard_IsInRange_LowerFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsInRange_EqualFail()
|
||||
{
|
||||
Guard.IsInRange(0, 4, 4, nameof(Test_Guard_IsInRange_EqualFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsInRange_HigherFail()
|
||||
{
|
||||
Guard.IsInRange(0, 20, 4, nameof(Test_Guard_IsInRange_HigherFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsNotInRange_Ok()
|
||||
{
|
||||
Guard.IsNotInRange(0, 4, 10, nameof(Test_Guard_IsNotInRange_Ok));
|
||||
Guard.IsNotInRange(-4, 0, 2, nameof(Test_Guard_IsNotInRange_Ok));
|
||||
Guard.IsNotInRange(12f, 0, 10, nameof(Test_Guard_IsNotInRange_Ok));
|
||||
Guard.IsNotInRange(-1, 0, 3.14f, nameof(Test_Guard_IsNotInRange_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsNotInRange_LowerEqualFail()
|
||||
{
|
||||
Guard.IsNotInRange(0, 0, 4, nameof(Test_Guard_IsNotInRange_LowerEqualFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsNotInRange_InnerFail()
|
||||
{
|
||||
Guard.IsNotInRange(2, 0, 4, nameof(Test_Guard_IsNotInRange_InnerFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsInRangeFor_Ok()
|
||||
{
|
||||
Span<int> span = stackalloc int[10];
|
||||
|
||||
Guard.IsInRangeFor(0, span, nameof(Test_Guard_IsInRangeFor_Ok));
|
||||
Guard.IsInRangeFor(4, span, nameof(Test_Guard_IsInRangeFor_Ok));
|
||||
Guard.IsInRangeFor(9, span, nameof(Test_Guard_IsInRangeFor_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsInRangeFor_LowerFail()
|
||||
{
|
||||
Span<int> span = stackalloc int[10];
|
||||
|
||||
Guard.IsInRangeFor(-2, span, nameof(Test_Guard_IsInRangeFor_LowerFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsInRangeFor_EqualFail()
|
||||
{
|
||||
Span<int> span = stackalloc int[10];
|
||||
|
||||
Guard.IsInRangeFor(10, span, nameof(Test_Guard_IsInRangeFor_EqualFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsInRangeFor_HigherFail()
|
||||
{
|
||||
Span<int> span = stackalloc int[10];
|
||||
|
||||
Guard.IsInRangeFor(99, span, nameof(Test_Guard_IsInRangeFor_HigherFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsNotInRangeFor_Ok()
|
||||
{
|
||||
Span<int> span = stackalloc int[10];
|
||||
|
||||
Guard.IsNotInRangeFor(-2, span, nameof(Test_Guard_IsNotInRangeFor_Ok));
|
||||
Guard.IsNotInRangeFor(10, span, nameof(Test_Guard_IsNotInRangeFor_Ok));
|
||||
Guard.IsNotInRangeFor(2222, span, nameof(Test_Guard_IsNotInRangeFor_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsNotInRangeFor_LowerFail()
|
||||
{
|
||||
Span<int> span = stackalloc int[10];
|
||||
|
||||
Guard.IsNotInRangeFor(0, span, nameof(Test_Guard_IsNotInRangeFor_LowerFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsNotInRangeFor_MiddleFail()
|
||||
{
|
||||
Span<int> span = stackalloc int[10];
|
||||
|
||||
Guard.IsNotInRangeFor(6, span, nameof(Test_Guard_IsNotInRangeFor_MiddleFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsBetween_Ok()
|
||||
{
|
||||
Guard.IsBetween(1, 0, 4, nameof(Test_Guard_IsBetween_Ok));
|
||||
Guard.IsBetween(3.14f, 0, 10, nameof(Test_Guard_IsBetween_Ok));
|
||||
Guard.IsBetween(1, 0, 3.14, nameof(Test_Guard_IsBetween_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsBetween_LowerFail()
|
||||
{
|
||||
Guard.IsBetween(-1, 0, 4, nameof(Test_Guard_IsBetween_LowerFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsBetween_EqualFail()
|
||||
{
|
||||
Guard.IsBetween(0, 0, 4, nameof(Test_Guard_IsBetween_EqualFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsBetween_HigherFail()
|
||||
{
|
||||
Guard.IsBetween(6, 0, 4, nameof(Test_Guard_IsBetween_HigherFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsNotBetween_Ok()
|
||||
{
|
||||
Guard.IsNotBetween(0, 0, 4, nameof(Test_Guard_IsNotBetween_Ok));
|
||||
Guard.IsNotBetween(10, 0, 10, nameof(Test_Guard_IsNotBetween_Ok));
|
||||
Guard.IsNotBetween(-5, 0, 3.14, nameof(Test_Guard_IsNotBetween_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsNotBetween_Fail()
|
||||
{
|
||||
Guard.IsNotBetween(1, 0, 4, nameof(Test_Guard_IsNotBetween_Fail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsBetweenOrEqualTo_Ok()
|
||||
{
|
||||
Guard.IsBetweenOrEqualTo(1, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_Ok));
|
||||
Guard.IsBetweenOrEqualTo(10, 0, 10, nameof(Test_Guard_IsBetweenOrEqualTo_Ok));
|
||||
Guard.IsBetweenOrEqualTo(1, 0, 3.14, nameof(Test_Guard_IsBetweenOrEqualTo_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsBetweenOrEqualTo_LowerFail()
|
||||
{
|
||||
Guard.IsBetweenOrEqualTo(-1, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_LowerFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsBetweenOrEqualTo_HigherFail()
|
||||
{
|
||||
Guard.IsBetweenOrEqualTo(6, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_HigherFail));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
public void Test_Guard_IsNotBetweenOrEqualTo_Ok()
|
||||
{
|
||||
Guard.IsNotBetweenOrEqualTo(6, 0, 4, nameof(Test_Guard_IsNotBetweenOrEqualTo_Ok));
|
||||
Guard.IsNotBetweenOrEqualTo(-10, 0, 10, nameof(Test_Guard_IsNotBetweenOrEqualTo_Ok));
|
||||
}
|
||||
|
||||
[TestCategory("Guard")]
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Test_Guard_IsNotBetweenOrEqualTo_Fail()
|
||||
{
|
||||
Guard.IsNotBetweenOrEqualTo(3, 0, 4, nameof(Test_Guard_IsNotBetweenOrEqualTo_Fail));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace UnitTests.Extensions
|
||||
{
|
||||
[TestClass]
|
||||
public class Test_TypeExtensions
|
||||
{
|
||||
[TestCategory("TypeExtensions")]
|
||||
[TestMethod]
|
||||
public void Test_TypeExtensions_BuiltInTypes()
|
||||
{
|
||||
Assert.AreEqual("bool", typeof(bool).ToTypeString());
|
||||
Assert.AreEqual("int", typeof(int).ToTypeString());
|
||||
Assert.AreEqual("float", typeof(float).ToTypeString());
|
||||
Assert.AreEqual("double", typeof(double).ToTypeString());
|
||||
Assert.AreEqual("decimal", typeof(decimal).ToTypeString());
|
||||
Assert.AreEqual("object", typeof(object).ToTypeString());
|
||||
Assert.AreEqual("string", typeof(string).ToTypeString());
|
||||
}
|
||||
|
||||
[TestCategory("TypeExtensions")]
|
||||
[TestMethod]
|
||||
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1009", Justification = "Nullable value tuple type")]
|
||||
public void Test_TypeExtensions_GenericTypes()
|
||||
{
|
||||
Assert.AreEqual("int?", typeof(int?).ToTypeString());
|
||||
Assert.AreEqual("System.DateTime?", typeof(DateTime?).ToTypeString());
|
||||
Assert.AreEqual("(int, float)", typeof((int, float)).ToTypeString());
|
||||
Assert.AreEqual("(double?, string, int)?", typeof((double?, string, int)?).ToTypeString());
|
||||
Assert.AreEqual("int[]", typeof(int[]).ToTypeString());
|
||||
Assert.AreEqual(typeof(int[,]).ToTypeString(), "int[,]");
|
||||
Assert.AreEqual("System.Span<float>", typeof(Span<float>).ToTypeString());
|
||||
Assert.AreEqual("System.Memory<char>", typeof(Memory<char>).ToTypeString());
|
||||
Assert.AreEqual("System.Collections.Generic.IEnumerable<int>", typeof(IEnumerable<int>).ToTypeString());
|
||||
Assert.AreEqual(typeof(Dictionary<int, List<float>>).ToTypeString(), "System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<float>>");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace UnitTests.Extensions
|
||||
{
|
||||
[TestClass]
|
||||
public class Test_ValueTypeExtensions
|
||||
{
|
||||
[TestCategory("ValueTypeExtensions")]
|
||||
[TestMethod]
|
||||
public void Test_ValueTypeExtensions_ToHexString()
|
||||
{
|
||||
Assert.AreEqual(((byte)0).ToHexString(), "0x00");
|
||||
Assert.AreEqual(((byte)127).ToHexString(), "0x7F");
|
||||
Assert.AreEqual(((byte)255).ToHexString(), "0xFF");
|
||||
Assert.AreEqual(((ushort)6458).ToHexString(), "0x193A");
|
||||
Assert.AreEqual(6458.ToHexString(), "0x0000193A");
|
||||
Assert.AreEqual((-1).ToHexString(), "0xFFFFFFFF");
|
||||
Assert.AreEqual(true.ToHexString(), "0x01");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -132,7 +132,12 @@
|
|||
<Compile Include="Converters\Test_EmptyCollectionToObjectConverter.cs" />
|
||||
<Compile Include="Converters\Test_EmptyStringToObjectConverter.cs" />
|
||||
<Compile Include="Converters\Test_StringFormatConverter.cs" />
|
||||
<Compile Include="Diagnostics\Test_Guard.Comparable.Numeric.cs" />
|
||||
<Compile Include="Diagnostics\Test_Guard.Array.cs" />
|
||||
<Compile Include="Diagnostics\Test_Guard.cs" />
|
||||
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
|
||||
<Compile Include="Extensions\Test_TypeExtensions.cs" />
|
||||
<Compile Include="Extensions\Test_ValueTypeExtensions.cs" />
|
||||
<Compile Include="Extensions\Test_ArrayExtensions.cs" />
|
||||
<Compile Include="Extensions\Test_NullableBoolMarkupExtension.cs" />
|
||||
<Compile Include="GlobalSuppressions.cs" />
|
||||
|
|
|
@ -28,7 +28,7 @@ steps:
|
|||
- task: UseDotNet@2
|
||||
inputs:
|
||||
packageType: 'sdk'
|
||||
version: '3.0.100'
|
||||
version: '3.1.101'
|
||||
displayName: Use .NET Core sdk
|
||||
|
||||
- task: DotNetCoreCLI@2
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
{
|
||||
"sdk": {
|
||||
"version": "3.0.100"
|
||||
},
|
||||
"msbuild-sdks": {
|
||||
"MSBuild.Sdk.Extras": "2.0.54"
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче