1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2025-04-10 11:15:45 +02:00

Guard APIs for .NET Standard 2.1 ()

* Initial guard APIs added

* Added EqualTo and NotEqualTo APIs

* Added more Guard APIs

* New interval Guard APIs added

* Added Guard APIs for IEnumerable<T> values

* Renamed APIs

* Added Guard APIs for Stream values

* Added Guard APIs for ReadOnlySpan<T> values

* Added Guard APIs for ReadOnlyMemory<T> values

* Code refactoring

* Minor code refactoring

* Added Guard.IsBitwiseEqualTo<T> API

* Added Guard.IsNull API

* Code refactoring and optimizations

* Code refactoring

* Added Guard.HasSizeNotEqualTo<T> API

* Added Guard size API overloads for the string type

* Added Guard size API overloads for T[] arrays

* Code refactoring

* Removed unnecessary using directives

* Added Guard reference check APIs

* Added new Guaard APIs for string values

* Improved Guard.IsBitwiseEqualTo<T> API

* Improved Guard.IsNull APIs

* Added new size APIs for copy operations

* Added new type test APIs, minor code tweaks

* Minor bug fixes

* Added Guard APIs for Task values

* Minor code tweak

* Added new Guard.IsEmpty APIs

* Refactored code to remove unsafe requirement

* Added Guard.IsInRange<T> APIs

* Added missing type checks in Guard.IsBitwiseEqualTo API

* Added ValueTypeExtensions.ToHexString API

* Code refactoring

* Added ValueTypeExtensions tests

* Added general Guard tests

* Bug fixes in the Guard class

* Minor speed improvements

* Code refactoring

* Removed unnecessary using directives

* More speed improvements and API refactoring

* Refactored/fixed some Guard APIs

* More bug fixes

* Added Guard tests for array APIs

* Fixed the Guard.IsNotEmpty<T> array API

* Moved exception throwers to separate class

* Moved general Guard throwers to separate class

* Disabled warning for XML overload resolution

* Moved array Guard throwers to separate class

* Moved stream Guard throwers to separate class

* Fixed some XML docs

* Moved enumerable Guard throwers to separate class

* Moved task Guard throwers to separate class

* Added new Guard APIs for tasks

* Moved string Guard throwers to separate class

* Moved ReadOnlySpan<T> Guard throwers to separate class

* Added Span<T> APIs to the Guard class

* Moved ReadOnlyMemory<T> Guard throwers to separate class

* Added Memory<T> APIs to the Guard class

* Minor code refactoring

* Update file headers

* Removed unnecessary methods

* Added TypeExtensions.ToTypeString extension

* Improved error messages for general Guard APIs

* Improved error messages for Enumerable Guard APIs

* Improved error messages for stream Guard APIs

* Improved error messages for array Guard APIs

* Improved error messages for string Guard APIs

* Improved error messages for task Guard APIs

* Improved error messages for span Guard APIs

* Improved error messages for memory Guard APIs

* Improved type string for nullable types

* Improved type string for value tuple types

* Minor performance improvement

* Added numeric comparison T4 file

* Updated .gitignore to skip generated files

* Moved comparison Guard APIs to separate file

* Renamed template file, fixed incorrect XML doc

* Fixed missing blank line

* Removed unnecessary type parameters

* Moved enumerable Guard APIs to separate file

* Code refactoring in the enumerable T4 template

* Renamed a file

* Added empty template for ThrowHelper.Collection

* Code refactoring in the T4 templates

* Switched collection throw helpers to T4 generation

* Code refactoring

* Removed incorrect file header

* Added new Guard.IsInRangeFor API

* Updated .gitignore, added generated files to source control

* Added tests for Guard.IsInRangeFor

* Improved formatting of values in error messages

* Improved error messages

* More improvements to the error messages

* Minor code tweaks

* Added Guard.IsNotOfType APIs

* Added Guard.IsNotAssignableToType APIs

* Added Guard.IsDefault<T> APIs

* Fixed some XML docs

* Added missing [Pure] attribute

* Fixed typo in a comment

* Removed unnecessary comment after code changes

* Added XML comment for the T4 service

* Added more comments to ToHexString

* Suppressed an incorrect code style warning

* Added more tests for Guard.IsInRange

* Added Guard.IsNotInRangeFor APIs

* Added readme file for the T4 templates

* Changed generated files extension to .g.cs to avoid conflicts

* Added Guard.IsCloseTo and Guard.IsNotCloseTo APIs

* Added tests for IsCloseTo

* Updated ".g.cs" extension for generated files

Co-Authored-By: Michael Hawker MSFT (XAML Llama) <michael.hawker@outlook.com>

* Removed extra space (typo)

Co-Authored-By: Michael Hawker MSFT (XAML Llama) <michael.hawker@outlook.com>

* More tweakes to the Guard.md file

* Excluded TypeInfo.g.cs file from .ttinclude from checkout

* Added more info about target ranges for some APIs

* Added IsCloseTo overloads for long type

* Minor code tweaks

* Added comment to describe (uint) cast range check trick

* Added a nore about the .g.cs files being checked in

* Added .NET Standard 2.1 target, adjusted imported packages

* Added [NotNull] attributes when needed

* Added flow attributes to bool Guard APIs

* Added missing Guard APIs for the string type

* Added shared main unit test project

* Removed duplicate files from UWP unit test project

* Refactored UWP unit tests project (can't be in same folder as .shproj)

* Moved markdown tests back into UWP project

* Added UnitTests.NetCore project

* Fixed some styling issues in test projects

* Added .editorconfig file to UniTests.NetCore project too

* Moved .editorconfig file to Shared project folder

* Removed NetCore Tests compilation for Native Configuration.

* Update Windows Community Toolkit.sln

* Updated multiline comments style

* Renamed some Guard APIs

* Fixed collections tests, moved to shared project

* Removed unnecessary [SuppressMessage] attributes

* Added internal [NotNull] and [DoesNotReturnIf] attributes

* Removed build warnings

* Removed leftover compiler directives

* Added Guard.IsTrue/False overloads with custom message

* Fixed test file location after merge

* Fixed Span<T> and Unsafe package references

Co-authored-by: Michael Hawker MSFT (XAML Llama) <michael.hawker@outlook.com>
Co-authored-by: Alexandre Zollinger Chohfi <alzollin@microsoft.com>
Co-authored-by: Alexandre Zollinger Chohfi <chohfi@outlook.com>
Co-authored-by: Michael Hawker MSFT (XAML Llama) <24302614+michael-hawker@users.noreply.github.com>
This commit is contained in:
Sergio Pedri 2020-05-15 23:14:14 +02:00 committed by GitHub
parent 21547f3712
commit 9d7ac2ee94
40 changed files with 2671 additions and 427 deletions

View File

@ -0,0 +1,36 @@
// 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.
#if !NETSTANDARD2_1
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
/// Specifies that a given <see cref="ParameterValue"/> also indicates
/// whether the method will not return (eg. throw an exception).
/// </summary>
/// <remarks>Internal copy of the .NET Standard 2.1 attribute.</remarks>
[AttributeUsage(AttributeTargets.Parameter)]
internal sealed class DoesNotReturnIfAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="DoesNotReturnIfAttribute"/> class.
/// </summary>
/// <param name="parameterValue">
/// The condition parameter value. Code after the method will be considered unreachable
/// by diagnostics if the argument to the associated parameter matches this value.
/// </param>
public DoesNotReturnIfAttribute(bool parameterValue)
{
ParameterValue = parameterValue;
}
/// <summary>
/// Gets a value indicating whether the parameter value should be <see langword="true"/>.
/// </summary>
public bool ParameterValue { get; }
}
}
#endif

View File

@ -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.
#if !NETSTANDARD2_1
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
/// Specifies that an output will not be <see langword="null"/> even if the corresponding type allows it.
/// Specifies that an input argument was not <see langword="null"/> when the call returns.
/// </summary>
/// <remarks>Internal copy of the .NET Standard 2.1 attribute.</remarks>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue)]
internal sealed class NotNullAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="NotNullAttribute"/> class.
/// </summary>
public NotNullAttribute()
{
}
}
}
#endif

View File

@ -1,10 +1,9 @@
// 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
* ===================== */
// =====================
// Auto generated file
// =====================
using System;
using System.Collections.Generic;
@ -94,11 +93,11 @@ public static void HasSizeNotEqualTo<T>(Span<T> span, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="span"/> is &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver<T>(Span<T> span, int size, string name)
public static void HasSizeGreaterThan<T>(Span<T> span, int size, string name)
{
if (span.Length <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(span, size, name);
}
}
@ -111,11 +110,11 @@ public static void HasSizeOver<T>(Span<T> span, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="span"/> is &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast<T>(Span<T> span, int size, string name)
public static void HasSizeGreaterThanOrEqualTo<T>(Span<T> span, int size, string name)
{
if (span.Length < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(span, size, name);
}
}
@ -296,11 +295,11 @@ public static void HasSizeNotEqualTo<T>(ReadOnlySpan<T> span, int size, string n
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="span"/> is &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver<T>(ReadOnlySpan<T> span, int size, string name)
public static void HasSizeGreaterThan<T>(ReadOnlySpan<T> span, int size, string name)
{
if (span.Length <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(span, size, name);
}
}
@ -313,11 +312,11 @@ public static void HasSizeOver<T>(ReadOnlySpan<T> span, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="span"/> is &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast<T>(ReadOnlySpan<T> span, int size, string name)
public static void HasSizeGreaterThanOrEqualTo<T>(ReadOnlySpan<T> span, int size, string name)
{
if (span.Length < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(span, size, name);
}
}
@ -498,11 +497,11 @@ public static void HasSizeNotEqualTo<T>(Memory<T> memory, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="memory"/> is &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver<T>(Memory<T> memory, int size, string name)
public static void HasSizeGreaterThan<T>(Memory<T> memory, int size, string name)
{
if (memory.Length <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(memory, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(memory, size, name);
}
}
@ -515,11 +514,11 @@ public static void HasSizeOver<T>(Memory<T> memory, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="memory"/> is &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast<T>(Memory<T> memory, int size, string name)
public static void HasSizeGreaterThanOrEqualTo<T>(Memory<T> memory, int size, string name)
{
if (memory.Length < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(memory, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(memory, size, name);
}
}
@ -700,11 +699,11 @@ public static void HasSizeNotEqualTo<T>(ReadOnlyMemory<T> memory, int size, stri
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="memory"/> is &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver<T>(ReadOnlyMemory<T> memory, int size, string name)
public static void HasSizeGreaterThan<T>(ReadOnlyMemory<T> memory, int size, string name)
{
if (memory.Length <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(memory, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(memory, size, name);
}
}
@ -717,11 +716,11 @@ public static void HasSizeOver<T>(ReadOnlyMemory<T> memory, int size, string nam
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="memory"/> is &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast<T>(ReadOnlyMemory<T> memory, int size, string name)
public static void HasSizeGreaterThanOrEqualTo<T>(ReadOnlyMemory<T> memory, int size, string name)
{
if (memory.Length < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(memory, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(memory, size, name);
}
}
@ -902,11 +901,11 @@ public static void HasSizeNotEqualTo<T>(T[] array, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="array"/> is &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver<T>(T[] array, int size, string name)
public static void HasSizeGreaterThan<T>(T[] array, int size, string name)
{
if (array.Length <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(array, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(array, size, name);
}
}
@ -919,11 +918,11 @@ public static void HasSizeOver<T>(T[] array, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="array"/> is &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast<T>(T[] array, int size, string name)
public static void HasSizeGreaterThanOrEqualTo<T>(T[] array, int size, string name)
{
if (array.Length < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(array, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(array, size, name);
}
}
@ -1104,11 +1103,11 @@ public static void HasSizeNotEqualTo<T>(List<T> list, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="list"/> is &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver<T>(List<T> list, int size, string name)
public static void HasSizeGreaterThan<T>(List<T> list, int size, string name)
{
if (list.Count <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver((ICollection<T>)list, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan((ICollection<T>)list, size, name);
}
}
@ -1121,11 +1120,11 @@ public static void HasSizeOver<T>(List<T> list, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="list"/> is &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast<T>(List<T> list, int size, string name)
public static void HasSizeGreaterThanOrEqualTo<T>(List<T> list, int size, string name)
{
if (list.Count < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast((ICollection<T>)list, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo((ICollection<T>)list, size, name);
}
}
@ -1306,11 +1305,11 @@ public static void HasSizeNotEqualTo<T>(ICollection<T> collection, int size, str
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="collection"/> is &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver<T>(ICollection<T> collection, int size, string name)
public static void HasSizeGreaterThan<T>(ICollection<T> collection, int size, string name)
{
if (collection.Count <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(collection, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(collection, size, name);
}
}
@ -1323,11 +1322,11 @@ public static void HasSizeOver<T>(ICollection<T> collection, int size, string na
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="collection"/> is &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast<T>(ICollection<T> collection, int size, string name)
public static void HasSizeGreaterThanOrEqualTo<T>(ICollection<T> collection, int size, string name)
{
if (collection.Count < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(collection, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(collection, size, name);
}
}
@ -1508,11 +1507,11 @@ public static void HasSizeNotEqualTo<T>(IReadOnlyCollection<T> collection, int s
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="collection"/> is &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver<T>(IReadOnlyCollection<T> collection, int size, string name)
public static void HasSizeGreaterThan<T>(IReadOnlyCollection<T> collection, int size, string name)
{
if (collection.Count <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(collection, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(collection, size, name);
}
}
@ -1525,11 +1524,11 @@ public static void HasSizeOver<T>(IReadOnlyCollection<T> collection, int size, s
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="collection"/> is &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast<T>(IReadOnlyCollection<T> collection, int size, string name)
public static void HasSizeGreaterThanOrEqualTo<T>(IReadOnlyCollection<T> collection, int size, string name)
{
if (collection.Count < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(collection, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(collection, size, name);
}
}

View File

@ -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.
<#@include file="TypeInfo.ttinclude" #>
/* ========================
* Auto generated file
* ===================== */
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
@ -117,11 +113,11 @@ GenerateTextForItems(EnumerableTypes, item =>
/// <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 &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
public static void HasSizeGreaterThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
{
if (<#=item.Name#>.<#=item.Size#> <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(<#=item.Cast#><#=item.Name#>, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(<#=item.Cast#><#=item.Name#>, size, name);
}
}
@ -134,11 +130,11 @@ GenerateTextForItems(EnumerableTypes, item =>
/// <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 &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
public static void HasSizeGreaterThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
{
if (<#=item.Name#>.<#=item.Size#> < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(<#=item.Cast#><#=item.Name#>, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
}
}
@ -248,11 +244,11 @@ GenerateTextForItems(EnumerableTypes, item =>
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 */
// 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#>)
{

View File

@ -1,10 +1,9 @@
// 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
* ===================== */
// =====================
// Auto generated file
// =====================
using System;
using System.Runtime.CompilerServices;

View File

@ -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.
<#@include file="TypeInfo.ttinclude" #>
/* ========================
* Auto generated file
* ===================== */
using System;
using System.Runtime.CompilerServices;

View File

@ -1,14 +1,12 @@
// 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
* ===================== */
// =====================
// Auto generated file
// =====================
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.Extensions;
@ -19,7 +17,6 @@ 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>
@ -50,19 +47,19 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(Span<T> span, i
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeOver<T>(Span<T> span, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThan<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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(Span<T> span, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<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()}");
}
@ -149,19 +146,19 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(ReadOnlySpan<T>
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeOver<T>(ReadOnlySpan<T> span, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThan<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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(ReadOnlySpan<T> span, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<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()}");
}
@ -248,19 +245,19 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(Memory<T> memor
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeOver<T>(Memory<T> memory, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThan<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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(Memory<T> memory, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<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()}");
}
@ -347,19 +344,19 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(ReadOnlyMemory<
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeOver<T>(ReadOnlyMemory<T> memory, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThan<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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(ReadOnlyMemory<T> memory, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<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()}");
}
@ -446,19 +443,19 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(T[] array, int
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeOver<T>(T[] array, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThan<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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(T[] array, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<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()}");
}
@ -545,19 +542,19 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(List<T> list, i
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeOver<T>(List<T> list, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThan<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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(List<T> list, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<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()}");
}
@ -644,19 +641,19 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(ICollection<T>
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeOver<T>(ICollection<T> collection, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThan<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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(ICollection<T> collection, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<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()}");
}
@ -743,19 +740,19 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(IReadOnlyCollec
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeOver<T>(IReadOnlyCollection<T> collection, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThan<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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeAtLeast<T>(IReadOnlyCollection<T> collection, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<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()}");
}

View File

@ -2,13 +2,8 @@
// 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;
@ -19,7 +14,6 @@ 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
{
<#
@ -54,19 +48,19 @@ GenerateTextForItems(EnumerableTypes, item =>
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver{T}(T[],int,string)"/> (or an overload) fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{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)
public static void ThrowArgumentExceptionForHasSizeGreaterThan<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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{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)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<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()}");
}

View File

@ -2,6 +2,9 @@
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".g.cs"#>
// =====================
// Auto generated file
// =====================
<#+
/// <summary>
/// A model representing the info on an enumerable type

View File

@ -98,11 +98,11 @@ public static void IsNotEqualTo<T>(T value, T target, string name)
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. */
// 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))

View File

@ -25,17 +25,17 @@ public static partial class Guard
[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. */
// 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);
@ -70,8 +70,8 @@ public static void IsNotCloseTo(int value, int target, uint delta, string name)
[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. */
// 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);

View File

@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
#nullable enable
@ -36,7 +37,7 @@ public static void IsNullOrEmpty(string? text, string name)
/// <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)
public static void IsNotNullOrEmpty([NotNull] string? text, string name)
{
if (string.IsNullOrEmpty(text))
{
@ -66,7 +67,7 @@ public static void IsNullOrWhitespace(string? text, string name)
/// <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)
public static void IsNotNullOrWhitespace([NotNull] string? text, string name)
{
if (string.IsNullOrWhiteSpace(text))
{
@ -174,11 +175,11 @@ public static void HasSizeNotEqualTo(string text, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is &lt;= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver(string text, int size, string name)
public static void HasSizeGreaterThan(string text, int size, string name)
{
if (text.Length <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(text, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(text, size, name);
}
}
@ -190,11 +191,11 @@ public static void HasSizeOver(string text, int size, string name)
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is &lt; <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast(string text, int size, string name)
public static void HasSizeGreaterThanOrEqualTo(string text, int size, string name)
{
if (text.Length < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(text, size, name);
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(text, size, name);
}
}
@ -229,5 +230,71 @@ public static void HasSizeLessThanOrEqualTo(string text, int size, string name)
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(text, size, name);
}
}
/// <summary>
/// Asserts that the source <see cref="string"/> instance must have the same size of a destination <see cref="string"/> instance.
/// </summary>
/// <param name="source">The source <see cref="string"/> instance to check the size for.</param>
/// <param name="destination">The destination <see cref="string"/> 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>
/// <remarks>The <see cref="string"/> type is immutable, but the name of this API is kept for consistency with the other overloads.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeEqualTo(string source, string destination, string name)
{
if (source.Length != destination.Length)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name);
}
}
/// <summary>
/// Asserts that the source <see cref="string"/> instance must have a size of less than or equal to that of a destination <see cref="string"/> instance.
/// </summary>
/// <param name="source">The source <see cref="string"/> instance to check the size for.</param>
/// <param name="destination">The destination <see cref="string"/> 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>
/// <remarks>The <see cref="string"/> type is immutable, but the name of this API is kept for consistency with the other overloads.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThanOrEqualTo(string source, string destination, string name)
{
if (source.Length > destination.Length)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name);
}
}
/// <summary>
/// Asserts that the input index is valid for a given <see cref="string"/> instance.
/// </summary>
/// <param name="index">The input index to be used to access <paramref name="text"/>.</param>
/// <param name="text">The input <see cref="string"/> 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="text"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsInRangeFor(int index, string text, string name)
{
if ((uint)index >= (uint)text.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, text, name);
}
}
/// <summary>
/// Asserts that the input index is not valid for a given <see cref="string"/> instance.
/// </summary>
/// <param name="index">The input index to be used to access <paramref name="text"/>.</param>
/// <param name="text">The input <see cref="string"/> 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="text"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotInRangeFor(int index, string text, string name)
{
if ((uint)index < (uint)text.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, text, name);
}
}
}
}

View File

@ -25,7 +25,6 @@ public static partial class Guard
/// <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
{
@ -44,7 +43,6 @@ public static void IsNull<T>(T? value, string name)
/// <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
{
@ -62,7 +60,7 @@ public static void IsNull<T>(T? value, string name)
/// <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)
public static void IsNotNull<T>([NotNull] T? value, string name)
where T : class
{
if (value is null)
@ -80,7 +78,7 @@ public static void IsNotNull<T>(T? value, string name)
/// <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)
public static void IsNotNull<T>([NotNull] T? value, string name)
where T : struct
{
if (value is null)
@ -262,7 +260,7 @@ public static void IsReferenceNotEqualTo<T>(T value, T target, string name)
/// <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)
public static void IsTrue([DoesNotReturnIf(false)] bool value, string name)
{
if (!value)
{
@ -270,6 +268,22 @@ public static void IsTrue(bool value, string 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>
/// <param name="message">A message to display if <paramref name="value"/> is <see langword="false"/>.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is <see langword="false"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsTrue([DoesNotReturnIf(false)] bool value, string name, string message)
{
if (!value)
{
ThrowHelper.ThrowArgumentExceptionForIsTrue(name, message);
}
}
/// <summary>
/// Asserts that the input value must be <see langword="false"/>.
/// </summary>
@ -277,12 +291,28 @@ public static void IsTrue(bool value, string name)
/// <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)
public static void IsFalse([DoesNotReturnIf(true)] bool value, string name)
{
if (value)
{
ThrowHelper.ThrowArgumentExceptionForIsFalse(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>
/// <param name="message">A message to display if <paramref name="value"/> is <see langword="true"/>.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is <see langword="true"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsFalse([DoesNotReturnIf(true)] bool value, string name, string message)
{
if (value)
{
ThrowHelper.ThrowArgumentExceptionForIsFalse(name, message);
}
}
}
}

View File

@ -3,7 +3,6 @@
// 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;
@ -14,12 +13,12 @@ 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>
/// <typeparam name="T">The item of items in the input <see cref="Span{T}"/> instance.</typeparam>
/// <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)
@ -30,7 +29,8 @@ public static void ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(string name)
/// <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>
/// <typeparam name="T">The item of items in the input <see cref="ReadOnlySpan{T}"/> instance.</typeparam>
/// <remarks>This method is needed because <see cref="ReadOnlySpan{T}"/> can't be used as a generic type parameter.</remarks>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(string name)
{
@ -40,6 +40,7 @@ public static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(string
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEmpty{T}(T[],string)"/> (or an overload) fails.
/// </summary>
/// <typeparam name="T">The item of items in the input collection.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsNotEmpty<T>(string name)
{

View File

@ -3,7 +3,6 @@
// 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;
@ -14,12 +13,12 @@ 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>
/// <typeparam name="T">The type of <see langword="struct"/> value type being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsDefault<T>(T value, string name)
where T : struct
@ -30,6 +29,7 @@ public static void ThrowArgumentExceptionForIsDefault<T>(T value, string name)
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotDefault{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of <see langword="struct"/> value type being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsNotDefault<T>(string name)
where T : struct
@ -40,6 +40,7 @@ public static void ThrowArgumentExceptionForIsNotDefault<T>(string name)
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEqualTo{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsEqualTo<T>(T value, T target, string name)
{
@ -49,6 +50,7 @@ public static void ThrowArgumentExceptionForIsEqualTo<T>(T value, T target, stri
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotEqualTo{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsNotEqualTo<T>(T value, T target, string name)
{
@ -58,6 +60,7 @@ public static void ThrowArgumentExceptionForIsNotEqualTo<T>(T value, T target, s
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsLessThan{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsLessThan<T>(T value, T maximum, string name)
{
@ -67,6 +70,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThan<T>(T value, T m
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsLessThanOrEqualTo{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo<T>(T value, T maximum, string name)
{
@ -76,6 +80,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo<T>(T v
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsGreaterThan{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan<T>(T value, T minimum, string name)
{
@ -85,6 +90,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan<T>(T value,
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsGreaterThanOrEqualTo{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo<T>(T value, T minimum, string name)
{
@ -94,6 +100,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo<T>(
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRange{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsInRange<T>(T value, T minimum, T maximum, string name)
{
@ -103,6 +110,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRange<T>(T value, T mi
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRange{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange<T>(T value, T minimum, T maximum, string name)
{
@ -112,6 +120,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange<T>(T value, T
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsBetween{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsBetween<T>(T value, T minimum, T maximum, string name)
{
@ -121,6 +130,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetween<T>(T value, T mi
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotBetween{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween<T>(T value, T minimum, T maximum, string name)
{
@ -130,6 +140,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween<T>(T value, T
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsBetweenOrEqualTo{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo<T>(T value, T minimum, T maximum, string name)
{
@ -139,6 +150,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo<T>(T va
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotBetweenOrEqualTo{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of values being tested.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo<T>(T value, T minimum, T maximum, string name)
{

View File

@ -3,7 +3,6 @@
// 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;
@ -14,7 +13,6 @@ 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>

View File

@ -3,7 +3,6 @@
// 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;
@ -15,7 +14,6 @@ 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>

View File

@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
#nullable enable
@ -13,7 +12,6 @@ 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>
@ -89,7 +87,7 @@ public static void ThrowArgumentExceptionForIsNotWhitespace(string text, string
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo"/> fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo(string,int,string)"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size, string name)
@ -107,19 +105,19 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int s
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeOver"/> fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThan(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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size, string name)
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(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()}");
}
@ -134,12 +132,48 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int siz
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo"/> fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo(string,int,string)"/> 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()}");
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo(string,string,string)"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeEqualTo(string source, string destination, string name)
{
ThrowArgumentException(name, $"The source {name.ToAssertString()} (string) 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(string,string,string)"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string source, string destination, string name)
{
ThrowArgumentException(name, $"The source {name.ToAssertString()} (string) 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(int,string,string)"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, string text, string name)
{
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {text.Length.ToAssertString()} to be a valid index for the target string, was {index.ToAssertString()}");
}
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor(int,string,string)"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, string text, string name)
{
ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {text.Length.ToAssertString()} to be an invalid index for the target string, was {index.ToAssertString()}");
}
}
}

View File

@ -3,7 +3,6 @@
// 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;
@ -15,7 +14,6 @@ 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>

View File

@ -3,7 +3,6 @@
// 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;
@ -14,13 +13,12 @@ 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.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNull{T}(T,string)"/> (where <typeparamref name="T"/> is <see langword="class"/>) fails.
/// </summary>
/// <typeparam name="T">The type of the input value.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsNull<T>(T value, string name)
where T : class
@ -29,8 +27,9 @@ public static void ThrowArgumentExceptionForIsNull<T>(T value, string name)
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNull{T}"/> fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNull{T}(T,string)"/> (where <typeparamref name="T"/> is <see langword="struct"/>) fails.
/// </summary>
/// <typeparam name="T">The type of the input value.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsNull<T>(T? value, string name)
where T : struct
@ -39,18 +38,19 @@ public static void ThrowArgumentExceptionForIsNull<T>(T? value, string name)
}
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> when <see cref="Guard.IsNotNull{T}"/> fails.
/// Throws an <see cref="ArgumentNullException"/> when <see cref="Guard.IsNotNull{T}(T,string)"/> fails.
/// </summary>
/// <typeparam name="T">The type of the input value.</typeparam>
[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>
/// <typeparam name="T">The type of the input value.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsOfType<T>(object value, string name)
{
@ -60,6 +60,7 @@ public static void ThrowArgumentExceptionForIsOfType<T>(object value, string nam
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotOfType{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of the input value.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsNotOfType<T>(object value, string name)
{
@ -87,6 +88,7 @@ public static void ThrowArgumentExceptionForIsNotOfType(object value, Type type,
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsAssignableToType{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type being checked against.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsAssignableToType<T>(object value, string name)
{
@ -96,6 +98,7 @@ public static void ThrowArgumentExceptionForIsAssignableToType<T>(object value,
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotAssignableToType{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type being checked against.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsNotAssignableToType<T>(object value, string name)
{
@ -123,6 +126,7 @@ public static void ThrowArgumentExceptionForIsNotAssignableToType(object value,
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsBitwiseEqualTo{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of input values being compared.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForsBitwiseEqualTo<T>(T value, T target, string name)
where T : unmanaged
@ -133,6 +137,7 @@ public static void ThrowArgumentExceptionForsBitwiseEqualTo<T>(T value, T target
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsReferenceEqualTo{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of input value being compared.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsReferenceEqualTo<T>(string name)
where T : class
@ -143,6 +148,7 @@ public static void ThrowArgumentExceptionForIsReferenceEqualTo<T>(string name)
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsReferenceNotEqualTo{T}"/> fails.
/// </summary>
/// <typeparam name="T">The type of input value being compared.</typeparam>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsReferenceNotEqualTo<T>(string name)
where T : class
@ -151,7 +157,7 @@ public static void ThrowArgumentExceptionForIsReferenceNotEqualTo<T>(string name
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsTrue"/> fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsTrue(bool,string)"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsTrue(string name)
@ -160,12 +166,30 @@ public static void ThrowArgumentExceptionForIsTrue(string name)
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsFalse"/> fails.
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsTrue(bool,string,string)"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsTrue(string name, string message)
{
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be true, was false: {message.ToAssertString()}");
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsFalse(bool,string)"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsFalse(string name)
{
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be false, was true");
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsFalse(bool,string,string)"/> fails.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentExceptionForIsFalse(string name, string message)
{
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be false, was true: {message.ToAssertString()}");
}
}
}

View File

@ -107,10 +107,10 @@ tokens[0] is { } genericName &&
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. */
// 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));
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Title>Windows Community Toolkit .NET Standard</Title>
@ -16,8 +16,14 @@
<DebugType>Full</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.3" />
<!-- .NET Standard 2.0 doesn't have the Span<T> type -->
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>
<!-- .NET Standard 2.1 doesn't have the Unsafe type -->
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
</ItemGroup>
<ItemGroup>

View File

@ -1,23 +0,0 @@
// 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 Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
namespace UnitTests.Extensions.Helpers
{
[Bindable]
public class ObjectWithNullableBoolProperty : DependencyObject
{
public bool? NullableBool
{
get { return (bool?)GetValue(NullableBoolProperty); }
set { SetValue(NullableBoolProperty, value); }
}
// Using a DependencyProperty as the backing store for NullableBool. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NullableBoolProperty =
DependencyProperty.Register(nameof(NullableBool), typeof(bool?), typeof(ObjectWithNullableBoolProperty), new PropertyMetadata(null));
}
}

View File

@ -1,228 +0,0 @@
// 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.Uwp.UI.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using UnitTests.Extensions.Helpers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
namespace UnitTests.Extensions
{
[TestClass]
public class Test_NullableBoolMarkupExtension
{
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_MarkupExtension_ProvidesTrue()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<CheckBox x:Name=""Check"" IsChecked=""{ex:NullableBool Value=True}""/>
</Page>") as FrameworkElement;
var toggle = treeroot.FindChildByName("Check") as CheckBox;
Assert.IsNotNull(toggle, "Could not find checkbox control in tree.");
Assert.AreEqual(true, toggle.IsChecked, "Expected checkbox value to be true.");
}
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_MarkupExtension_ProvidesFalse()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<CheckBox x:Name=""Check"" IsChecked=""{ex:NullableBool Value=False}""/>
</Page>") as FrameworkElement;
var toggle = treeroot.FindChildByName("Check") as CheckBox;
Assert.IsNotNull(toggle, "Could not find checkbox control in tree.");
Assert.AreEqual(false, toggle.IsChecked, "Expected checkbox value to be false.");
}
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_MarkupExtension_ProvidesNull()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<CheckBox x:Name=""Check"" IsChecked=""{ex:NullableBool IsNull=True}""/>
</Page>") as FrameworkElement;
var toggle = treeroot.FindChildByName("Check") as CheckBox;
Assert.IsNotNull(toggle, "Could not find checkbox control in tree.");
Assert.AreEqual(null, toggle.IsChecked, "Expected checkbox value to be null.");
}
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_MarkupExtension_ProvidesNullStillWithTrueValue()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<CheckBox x:Name=""Check"" IsChecked=""{ex:NullableBool IsNull=True, Value=True}""/>
</Page>") as FrameworkElement;
var toggle = treeroot.FindChildByName("Check") as CheckBox;
Assert.IsNotNull(toggle, "Could not find checkbox control in tree.");
Assert.AreEqual(null, toggle.IsChecked, "Expected checkbox value to be null.");
}
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_MarkupExtension_ProvidesNullStillWithFalseValue()
{
// Should be no-op as Value is false by default.
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"">
<CheckBox x:Name=""Check"" IsChecked=""{ex:NullableBool IsNull=True, Value=False}""/>
</Page>") as FrameworkElement;
var toggle = treeroot.FindChildByName("Check") as CheckBox;
Assert.IsNotNull(toggle, "Could not find checkbox control in tree.");
Assert.AreEqual(null, toggle.IsChecked, "Expected checkbox value to be null.");
}
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_Test_TestObject()
{
// Just test that we can properly parse our object as-is into our test harness.
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
xmlns:helpers=""using:UnitTests.Extensions.Helpers"">
<Page.Resources>
<helpers:ObjectWithNullableBoolProperty x:Key=""OurObject""/>
</Page.Resources>
</Page>") as FrameworkElement;
var obj = treeroot.Resources["OurObject"] as ObjectWithNullableBoolProperty;
Assert.IsNotNull(obj, "Could not find object in resources.");
Assert.AreEqual(null, obj.NullableBool, "Expected obj value to be null.");
}
#pragma warning disable SA1124 // Do not use regions
#region System-based Unit Tests, See Issue #3198
#pragma warning restore SA1124 // Do not use regions
[Ignore] // This test has trouble running on CI in release mode for some reason, we should re-enable when we test WinUI 3 Issue #3106
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_DependencyProperty_SystemTrue()
{
// This is the failure case in the OS currently which causes us to need
// this markup extension.
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
xmlns:helpers=""using:UnitTests.Extensions.Helpers"">
<Page.Resources>
<helpers:ObjectWithNullableBoolProperty x:Key=""OurObject"" NullableBool=""True""/>
</Page.Resources>
</Page>") as FrameworkElement;
var obj = treeroot.Resources["OurObject"] as ObjectWithNullableBoolProperty;
Assert.IsNotNull(obj, "Could not find object in resources.");
Assert.AreEqual(true, obj.NullableBool, "Expected obj value to be true.");
}
[Ignore] // This test has trouble running on CI in release mode for some reason, we should re-enable when we test WinUI 3 Issue #3106
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_DependencyProperty_SystemFalse()
{
// This is the failure case in the OS currently which causes us to need
// this markup extension.
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
xmlns:helpers=""using:UnitTests.Extensions.Helpers"">
<Page.Resources>
<helpers:ObjectWithNullableBoolProperty x:Key=""OurObject"" NullableBool=""False""/>
</Page.Resources>
</Page>") as FrameworkElement;
var obj = treeroot.Resources["OurObject"] as ObjectWithNullableBoolProperty;
Assert.IsNotNull(obj, "Could not find object in resources.");
Assert.AreEqual(false, obj.NullableBool, "Expected obj value to be true.");
}
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_DependencyProperty_SystemNull()
{
// This is the failure case in the OS currently which causes us to need
// this markup extension.
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
xmlns:helpers=""using:UnitTests.Extensions.Helpers"">
<Page.Resources>
<helpers:ObjectWithNullableBoolProperty x:Key=""OurObject"" NullableBool=""{x:Null}""/>
</Page.Resources>
</Page>") as FrameworkElement;
var obj = treeroot.Resources["OurObject"] as ObjectWithNullableBoolProperty;
Assert.IsNotNull(obj, "Could not find object in resources.");
Assert.IsNull(obj.NullableBool, "Expected obj value to be null.");
}
#endregion
[TestCategory("NullableBoolMarkupExtension")]
[UITestMethod]
public void Test_NullableBool_DependencyProperty_TrueValue()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
xmlns:helpers=""using:UnitTests.Extensions.Helpers"">
<Page.Resources>
<helpers:ObjectWithNullableBoolProperty x:Key=""OurObject"" NullableBool=""{ex:NullableBool Value=True}""/>
</Page.Resources>
</Page>") as FrameworkElement;
var obj = treeroot.Resources["OurObject"] as ObjectWithNullableBoolProperty;
Assert.IsNotNull(obj, "Could not find object in resources.");
Assert.AreEqual(true, obj.NullableBool, "Expected obj value to be true.");
}
}
}

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Microsoft.Toolkit\Microsoft.Toolkit.csproj" />
</ItemGroup>
<Import Project="..\UnitTests.Shared\UnitTests.Shared.projitems" Label="Shared" />
</Project>

View File

@ -0,0 +1,7 @@
[*.{cs,vb}]
# SA1601: Partial elements should be documented
dotnet_diagnostic.SA1601.severity = none
dotnet_diagnostic.CS1573.severity = none
dotnet_diagnostic.CS1591.severity = none
dotnet_diagnostic.CS1712.severity = none

View File

@ -0,0 +1,20 @@
// 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.Collections.Generic;
using System.Linq;
namespace UnitTests.Collections
{
public class IntGroup : List<int>, IGrouping<string, int>
{
public IntGroup(string key, IEnumerable<int> collection)
: base(collection)
{
Key = key;
}
public string Key { get; }
}
}

View File

@ -0,0 +1,125 @@
// 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.Collections.Specialized;
using System.Linq;
using FluentAssertions;
using Microsoft.Toolkit.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.Collections
{
[TestClass]
public class ObservableGroupTests
{
[TestCategory("Collections")]
[TestMethod]
public void Ctor_ShouldHaveExpectedState()
{
var group = new ObservableGroup<string, int>("key");
group.Key.Should().Be("key");
group.Should().BeEmpty();
}
[TestCategory("Collections")]
[TestMethod]
public void Ctor_WithGrouping_ShouldHaveExpectedState()
{
var source = new IntGroup("Key", new[] { 1, 2, 3 });
var group = new ObservableGroup<string, int>(source);
group.Key.Should().Be("Key");
group.Should().BeEquivalentTo(new[] { 1, 2, 3 }, option => option.WithStrictOrdering());
}
[TestCategory("Collections")]
[TestMethod]
public void Ctor_WithCollection_ShouldHaveExpectedState()
{
var source = new[] { 1, 2, 3 };
var group = new ObservableGroup<string, int>("key", source);
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 1, 2, 3 }, option => option.WithStrictOrdering());
}
[TestCategory("Collections")]
[TestMethod]
public void Add_ShouldRaiseEvent()
{
var collectionChangedEventRaised = false;
var source = new[] { 1, 2, 3 };
var group = new ObservableGroup<string, int>("key", source);
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
group.Add(4);
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 1, 2, 3, 4 }, option => option.WithStrictOrdering());
collectionChangedEventRaised.Should().BeTrue();
}
[TestCategory("Collections")]
[TestMethod]
public void Update_ShouldRaiseEvent()
{
var collectionChangedEventRaised = false;
var source = new[] { 1, 2, 3 };
var group = new ObservableGroup<string, int>("key", source);
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
group[1] = 4;
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 1, 4, 3 }, option => option.WithStrictOrdering());
collectionChangedEventRaised.Should().BeTrue();
}
[TestCategory("Collections")]
[TestMethod]
public void Remove_ShouldRaiseEvent()
{
var collectionChangedEventRaised = false;
var source = new[] { 1, 2, 3 };
var group = new ObservableGroup<string, int>("key", source);
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
group.Remove(1);
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 2, 3 }, option => option.WithStrictOrdering());
collectionChangedEventRaised.Should().BeTrue();
}
[TestCategory("Collections")]
[TestMethod]
public void Clear_ShouldRaiseEvent()
{
var collectionChangedEventRaised = false;
var source = new[] { 1, 2, 3 };
var group = new ObservableGroup<string, int>("key", source);
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
group.Clear();
group.Key.Should().Be("key");
group.Should().BeEmpty();
collectionChangedEventRaised.Should().BeTrue();
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(0)]
[DataRow(3)]
public void IReadOnlyObservableGroup_ShouldReturnExpectedValues(int count)
{
var group = new ObservableGroup<string, int>("key", Enumerable.Range(0, count));
var iReadOnlyObservableGroup = (IReadOnlyObservableGroup)group;
iReadOnlyObservableGroup.Key.Should().Be("key");
iReadOnlyObservableGroup.Count.Should().Be(count);
}
}
}

View File

@ -0,0 +1,539 @@
// 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.Linq;
using FluentAssertions;
using Microsoft.Toolkit.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.Collections
{
[TestClass]
public class ObservableGroupedCollectionExtensionsTests
{
[TestCategory("Collections")]
[TestMethod]
public void First_WhenGroupExists_ShouldReturnFirstGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
var target = groupedCollection.AddGroup("B", 10);
groupedCollection.AddGroup("B", 42);
var result = groupedCollection.First("B");
result.Should().BeSameAs(target);
}
[TestCategory("Collections")]
[TestMethod]
public void First_WhenGroupDoesNotExist_ShouldThrow()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
Action action = () => groupedCollection.First("I do not exist");
action.Should().Throw<InvalidOperationException>();
}
[TestCategory("Collections")]
[TestMethod]
public void FirstOrDefault_WhenGroupExists_ShouldReturnFirstGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
var target = groupedCollection.AddGroup("B", 10);
groupedCollection.AddGroup("B", 42);
var result = groupedCollection.FirstOrDefault("B");
result.Should().BeSameAs(target);
}
[TestCategory("Collections")]
[TestMethod]
public void FirstOrDefault_WhenGroupDoesNotExist_ShouldReturnNull()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
var result = groupedCollection.FirstOrDefault("I do not exist");
result.Should().BeNull();
}
[TestCategory("Collections")]
[TestMethod]
public void ElementAt_WhenGroupExistsAndIndexInRange_ShouldReturnFirstGroupValue()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
groupedCollection.AddGroup("B", 10, 11, 12);
groupedCollection.AddGroup("B", 42);
var result = groupedCollection.ElementAt("B", 2);
result.Should().Be(12);
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(-1)]
[DataRow(3)]
public void ElementAt_WhenGroupExistsAndIndexOutOfRange_ShouldReturnThrow(int index)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
groupedCollection.AddGroup("B", 10, 11, 12);
groupedCollection.AddGroup("B", 42);
Action action = () => groupedCollection.ElementAt("B", index);
action.Should().Throw<ArgumentException>();
}
[TestCategory("Collections")]
[TestMethod]
public void ElementAt_WhenGroupDoesNotExist_ShouldThrow()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
Action action = () => groupedCollection.ElementAt("I do not exist", 0);
action.Should().Throw<InvalidOperationException>();
}
[TestCategory("Collections")]
[TestMethod]
public void ElementAtOrDefault_WhenGroupExistsAndIndexInRange_ShouldReturnValue()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
groupedCollection.AddGroup("B", 10, 11, 12);
groupedCollection.AddGroup("B", 42);
var result = groupedCollection.ElementAt("B", 2);
result.Should().Be(12);
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(-1)]
[DataRow(3)]
public void ElementAtOrDefault_WhenGroupExistsAndIndexOutOfRange_ShouldReturnDefaultValue(int index)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
groupedCollection.AddGroup("B", 10, 11, 12);
groupedCollection.AddGroup("B", 42);
var result = groupedCollection.ElementAtOrDefault("B", index);
result.Should().Be(0);
}
[TestCategory("Collections")]
[TestMethod]
public void ElementAtOrDefault_WhenGroupDoesNotExist_ShouldReturnDefaultValue()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 23);
var result = groupedCollection.ElementAtOrDefault("I do not exist", 0);
result.Should().Be(0);
}
[TestCategory("Collections")]
[TestMethod]
public void AddGroup_WithItem_ShouldAddGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
var addedGroup = groupedCollection.AddGroup("new key", 23);
addedGroup.Should().NotBeNull();
addedGroup.Key.Should().Be("new key");
addedGroup.Should().ContainSingle();
addedGroup.Should().ContainInOrder(23);
groupedCollection.Should().ContainSingle();
groupedCollection.Should().HaveElementAt(0, addedGroup);
}
[TestCategory("Collections")]
[TestMethod]
public void AddGroup_WithCollection_ShouldAddGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
var addedGroup = groupedCollection.AddGroup("new key", new[] { 23, 10, 42 });
addedGroup.Should().NotBeNull();
addedGroup.Key.Should().Be("new key");
addedGroup.Should().HaveCount(3);
addedGroup.Should().ContainInOrder(23, 10, 42);
groupedCollection.Should().ContainSingle();
groupedCollection.Should().HaveElementAt(0, addedGroup);
}
[TestCategory("Collections")]
[TestMethod]
public void AddGroup_WithParamsCollection_ShouldAddGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
var addedGroup = groupedCollection.AddGroup("new key", 23, 10, 42);
addedGroup.Should().NotBeNull();
addedGroup.Key.Should().Be("new key");
addedGroup.Should().HaveCount(3);
addedGroup.Should().ContainInOrder(23, 10, 42);
groupedCollection.Should().ContainSingle();
groupedCollection.Should().HaveElementAt(0, addedGroup);
}
[TestCategory("Collections")]
[TestMethod]
public void AddItem_WhenTargetGroupDoesNotExists_ShouldCreateAndAddNewGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
var addedGroup = groupedCollection.AddItem("new key", 23);
addedGroup.Should().NotBeNull();
addedGroup.Key.Should().Be("new key");
addedGroup.Should().ContainSingle();
addedGroup.Should().ContainInOrder(23);
groupedCollection.Should().ContainSingle();
groupedCollection.Should().HaveElementAt(0, addedGroup);
}
[TestCategory("Collections")]
[TestMethod]
public void AddItem_WhenSingleTargetGroupAlreadyExists_ShouldAddItemToExistingGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
var targetGroup = groupedCollection.AddGroup("B", 4, 5, 6);
groupedCollection.AddGroup("C", 7, 8);
var addedGroup = groupedCollection.AddItem("B", 23);
addedGroup.Should().BeSameAs(targetGroup);
addedGroup.Key.Should().Be("B");
addedGroup.Should().HaveCount(4);
addedGroup.Should().ContainInOrder(4, 5, 6, 23);
groupedCollection.Should().HaveCount(3);
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(3);
groupedCollection.ElementAt(0).Should().ContainInOrder(1, 2, 3);
groupedCollection.ElementAt(1).Key.Should().Be("B");
groupedCollection.ElementAt(1).Should().HaveCount(4);
groupedCollection.ElementAt(1).Should().ContainInOrder(4, 5, 6, 23);
groupedCollection.ElementAt(2).Key.Should().Be("C");
groupedCollection.ElementAt(2).Should().HaveCount(2);
groupedCollection.ElementAt(2).Should().ContainInOrder(7, 8);
}
[TestCategory("Collections")]
[TestMethod]
public void AddItem_WhenSeveralTargetGroupsAlreadyExist_ShouldAddItemToFirstExistingGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
var targetGroup = groupedCollection.AddGroup("B", 4, 5, 6);
groupedCollection.AddGroup("B", 7, 8, 9);
groupedCollection.AddGroup("C", 10, 11);
var addedGroup = groupedCollection.AddItem("B", 23);
addedGroup.Should().BeSameAs(targetGroup);
addedGroup.Key.Should().Be("B");
addedGroup.Should().HaveCount(4);
addedGroup.Should().ContainInOrder(4, 5, 6, 23);
groupedCollection.Should().HaveCount(4);
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(3);
groupedCollection.ElementAt(0).Should().ContainInOrder(1, 2, 3);
groupedCollection.ElementAt(1).Key.Should().Be("B");
groupedCollection.ElementAt(1).Should().HaveCount(4);
groupedCollection.ElementAt(1).Should().ContainInOrder(4, 5, 6, 23);
groupedCollection.ElementAt(2).Key.Should().Be("B");
groupedCollection.ElementAt(2).Should().HaveCount(3);
groupedCollection.ElementAt(2).Should().ContainInOrder(7, 8, 9);
groupedCollection.ElementAt(3).Key.Should().Be("C");
groupedCollection.ElementAt(3).Should().HaveCount(2);
groupedCollection.ElementAt(3).Should().ContainInOrder(10, 11);
}
[TestCategory("Collections")]
[TestMethod]
public void InsertItem_WhenGroupDoesNotExist_ShoudThrow()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
Action action = () => groupedCollection.InsertItem("I do not exist", 0, 23);
action.Should().Throw<InvalidOperationException>();
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(-1)]
[DataRow(4)]
public void InsertItem_WhenIndexOutOfRange_ShoudThrow(int index)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
Action action = () => groupedCollection.InsertItem("A", index, 23);
action.Should().Throw<ArgumentOutOfRangeException>();
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(0, new[] { 23, 1, 2, 3 })]
[DataRow(1, new[] { 1, 23, 2, 3 })]
[DataRow(3, new[] { 1, 2, 3, 23 })]
public void InsertItem_WithValidIndex_WithSeveralGroups_ShoudInsertItemInFirstGroup(int index, int[] expecteGroupValues)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 4, 5);
var targetGroup = groupedCollection.AddGroup("B", 1, 2, 3);
groupedCollection.AddGroup("B", 6, 7);
var group = groupedCollection.InsertItem("B", index, 23);
group.Should().BeSameAs(targetGroup);
groupedCollection.Should().HaveCount(3);
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(2);
groupedCollection.ElementAt(0).Should().ContainInOrder(4, 5);
groupedCollection.ElementAt(1).Key.Should().Be("B");
groupedCollection.ElementAt(1).Should().HaveCount(4);
groupedCollection.ElementAt(1).Should().ContainInOrder(expecteGroupValues);
groupedCollection.ElementAt(2).Key.Should().Be("B");
groupedCollection.ElementAt(2).Should().HaveCount(2);
groupedCollection.ElementAt(2).Should().ContainInOrder(6, 7);
}
[TestCategory("Collections")]
[TestMethod]
public void SetItem_WhenGroupDoesNotExist_ShoudThrow()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
Action action = () => groupedCollection.SetItem("I do not exist", 0, 23);
action.Should().Throw<InvalidOperationException>();
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(-1)]
[DataRow(3)]
public void SetItem_WhenIndexOutOfRange_ShoudThrow(int index)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
Action action = () => groupedCollection.SetItem("A", index, 23);
action.Should().Throw<ArgumentOutOfRangeException>();
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(0, new[] { 23, 2, 3 })]
[DataRow(1, new[] { 1, 23, 3 })]
[DataRow(2, new[] { 1, 2, 23 })]
public void SetItem_WithValidIndex_WithSeveralGroups_ShoudReplaceItemInFirstGroup(int index, int[] expecteGroupValues)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 4, 5);
var targetGroup = groupedCollection.AddGroup("B", 1, 2, 3);
groupedCollection.AddGroup("B", 6, 7);
var group = groupedCollection.SetItem("B", index, 23);
group.Should().BeSameAs(targetGroup);
groupedCollection.Should().HaveCount(3);
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(2);
groupedCollection.ElementAt(0).Should().ContainInOrder(4, 5);
groupedCollection.ElementAt(1).Key.Should().Be("B");
groupedCollection.ElementAt(1).Should().HaveCount(3);
groupedCollection.ElementAt(1).Should().ContainInOrder(expecteGroupValues);
groupedCollection.ElementAt(2).Key.Should().Be("B");
groupedCollection.ElementAt(2).Should().HaveCount(2);
groupedCollection.ElementAt(2).Should().ContainInOrder(6, 7);
}
[TestCategory("Collections")]
[TestMethod]
public void RemoveGroup_WhenGroupDoesNotExists_ShouldDoNothing()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
groupedCollection.RemoveGroup("I do not exist");
groupedCollection.Should().ContainSingle();
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(3);
groupedCollection.ElementAt(0).Should().ContainInOrder(1, 2, 3);
}
[TestCategory("Collections")]
[TestMethod]
public void RemoveGroup_WhenSingleGroupExists_ShouldRemoveGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
groupedCollection.AddGroup("B", 4, 5, 6);
groupedCollection.RemoveGroup("B");
groupedCollection.Should().ContainSingle();
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(3);
groupedCollection.ElementAt(0).Should().ContainInOrder(1, 2, 3);
}
[TestCategory("Collections")]
[TestMethod]
public void RemoveGroup_WhenSeveralGroupsExist_ShouldRemoveFirstGroup()
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
groupedCollection.AddGroup("B", 4, 5, 6);
groupedCollection.AddGroup("B", 7, 8);
groupedCollection.RemoveGroup("B");
groupedCollection.Should().HaveCount(2);
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(3);
groupedCollection.ElementAt(0).Should().ContainInOrder(1, 2, 3);
groupedCollection.ElementAt(1).Key.Should().Be("B");
groupedCollection.ElementAt(1).Should().HaveCount(2);
groupedCollection.ElementAt(1).Should().ContainInOrder(7, 8);
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(true)]
[DataRow(false)]
public void RemoveItem_WhenGroupDoesNotExist_ShouldDoNothing(bool removeGroupIfEmpty)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
groupedCollection.AddGroup("B", 4, 5, 6);
groupedCollection.RemoveItem("I do not exist", 8, removeGroupIfEmpty);
groupedCollection.Should().HaveCount(2);
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(3);
groupedCollection.ElementAt(0).Should().ContainInOrder(1, 2, 3);
groupedCollection.ElementAt(1).Key.Should().Be("B");
groupedCollection.ElementAt(1).Should().HaveCount(3);
groupedCollection.ElementAt(1).Should().ContainInOrder(4, 5, 6);
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(true)]
[DataRow(false)]
public void RemoveItem_WhenGroupExistsAndItemDoesNotExist_ShouldDoNothing(bool removeGroupIfEmpty)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
groupedCollection.AddGroup("B", 4, 5, 6);
groupedCollection.RemoveItem("B", 8, removeGroupIfEmpty);
groupedCollection.Should().HaveCount(2);
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(3);
groupedCollection.ElementAt(0).Should().ContainInOrder(1, 2, 3);
groupedCollection.ElementAt(1).Key.Should().Be("B");
groupedCollection.ElementAt(1).Should().HaveCount(3);
groupedCollection.ElementAt(1).Should().ContainInOrder(4, 5, 6);
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(true)]
[DataRow(false)]
public void RemoveItem_WhenGroupAndItemExist_ShouldRemoveItemFromGroup(bool removeGroupIfEmpty)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
groupedCollection.AddGroup("B", 4, 5, 6);
groupedCollection.RemoveItem("B", 5, removeGroupIfEmpty);
groupedCollection.Should().HaveCount(2);
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(3);
groupedCollection.ElementAt(0).Should().ContainInOrder(1, 2, 3);
groupedCollection.ElementAt(1).Key.Should().Be("B");
groupedCollection.ElementAt(1).Should().HaveCount(2);
groupedCollection.ElementAt(1).Should().ContainInOrder(4, 6);
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(true, true)]
[DataRow(false, false)]
public void RemoveItem_WhenRemovingLastItem_ShouldRemoveGroupIfRequired(bool removeGroupIfEmpty, bool expectGroupRemoved)
{
var groupedCollection = new ObservableGroupedCollection<string, int>();
groupedCollection.AddGroup("A", 1, 2, 3);
groupedCollection.AddGroup("B", 4);
groupedCollection.RemoveItem("B", 4, removeGroupIfEmpty);
groupedCollection.Should().HaveCount(expectGroupRemoved ? 1 : 2);
groupedCollection.ElementAt(0).Key.Should().Be("A");
groupedCollection.ElementAt(0).Should().HaveCount(3);
groupedCollection.ElementAt(0).Should().ContainInOrder(1, 2, 3);
if (!expectGroupRemoved)
{
groupedCollection.ElementAt(1).Key.Should().Be("B");
groupedCollection.ElementAt(1).Should().BeEmpty();
}
}
}
}

View File

@ -0,0 +1,43 @@
// 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.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Microsoft.Toolkit.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.Collections
{
[TestClass]
public class ObservableGroupedCollectionTests
{
[TestCategory("Collections")]
[TestMethod]
public void Ctor_ShouldHaveExpectedValues()
{
var groupCollection = new ObservableGroupedCollection<string, int>();
groupCollection.Should().BeEmpty();
}
[TestCategory("Collections")]
[TestMethod]
public void Ctor_WithGroups_ShouldHaveExpectedValues()
{
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", new[] { 1, 3, 5 }),
new IntGroup("B", new[] { 2, 4, 6 }),
};
var groupCollection = new ObservableGroupedCollection<string, int>(groups);
groupCollection.Should().HaveCount(2);
groupCollection.ElementAt(0).Key.Should().Be("A");
groupCollection.ElementAt(0).Should().BeEquivalentTo(new[] { 1, 3, 5 }, o => o.WithStrictOrdering());
groupCollection.ElementAt(1).Key.Should().Be("B");
groupCollection.ElementAt(1).Should().BeEquivalentTo(new[] { 2, 4, 6 }, o => o.WithStrictOrdering());
}
}
}

View File

@ -0,0 +1,133 @@
// 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.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using FluentAssertions;
using Microsoft.Toolkit.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.Collections
{
[TestClass]
public class ReadOnlyObservableGroupTests
{
[TestCategory("Collections")]
[TestMethod]
public void Ctor_WithKeyAndOBservableCollection_ShouldHaveExpectedInitialState()
{
var source = new ObservableCollection<int>(new[] { 1, 2, 3 });
var group = new ReadOnlyObservableGroup<string, int>("key", source);
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 1, 2, 3 }, option => option.WithStrictOrdering());
}
[TestCategory("Collections")]
[TestMethod]
public void Ctor_ObservableGroup_ShouldHaveExpectedInitialState()
{
var source = new[] { 1, 2, 3 };
var sourceGroup = new ObservableGroup<string, int>("key", source);
var group = new ReadOnlyObservableGroup<string, int>(sourceGroup);
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 1, 2, 3 }, option => option.WithStrictOrdering());
}
[TestCategory("Collections")]
[TestMethod]
public void Ctor_WithKeyAndCollection_ShouldHaveExpectedInitialState()
{
var source = new[] { 1, 2, 3 };
var group = new ReadOnlyObservableGroup<string, int>("key", source);
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 1, 2, 3 }, option => option.WithStrictOrdering());
}
[TestCategory("Collections")]
[TestMethod]
public void Add_ShouldRaiseEvent()
{
var collectionChangedEventRaised = false;
var source = new[] { 1, 2, 3 };
var sourceGroup = new ObservableGroup<string, int>("key", source);
var group = new ReadOnlyObservableGroup<string, int>(sourceGroup);
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
sourceGroup.Add(4);
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 1, 2, 3, 4 }, option => option.WithStrictOrdering());
collectionChangedEventRaised.Should().BeTrue();
}
[TestCategory("Collections")]
[TestMethod]
public void Update_ShouldRaiseEvent()
{
var collectionChangedEventRaised = false;
var source = new[] { 1, 2, 3 };
var sourceGroup = new ObservableGroup<string, int>("key", source);
var group = new ReadOnlyObservableGroup<string, int>(sourceGroup);
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
sourceGroup[1] = 4;
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 1, 4, 3 }, option => option.WithStrictOrdering());
collectionChangedEventRaised.Should().BeTrue();
}
[TestCategory("Collections")]
[TestMethod]
public void Remove_ShouldRaiseEvent()
{
var collectionChangedEventRaised = false;
var source = new[] { 1, 2, 3 };
var sourceGroup = new ObservableGroup<string, int>("key", source);
var group = new ReadOnlyObservableGroup<string, int>(sourceGroup);
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
sourceGroup.Remove(1);
group.Key.Should().Be("key");
group.Should().BeEquivalentTo(new[] { 2, 3 }, option => option.WithStrictOrdering());
collectionChangedEventRaised.Should().BeTrue();
}
[TestCategory("Collections")]
[TestMethod]
public void Clear_ShouldRaiseEvent()
{
var collectionChangedEventRaised = false;
var source = new[] { 1, 2, 3 };
var sourceGroup = new ObservableGroup<string, int>("key", source);
var group = new ReadOnlyObservableGroup<string, int>(sourceGroup);
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
sourceGroup.Clear();
group.Key.Should().Be("key");
group.Should().BeEmpty();
collectionChangedEventRaised.Should().BeTrue();
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(0)]
[DataRow(3)]
public void IReadOnlyObservableGroup_ShouldReturnExpectedValues(int count)
{
var sourceGroup = new ObservableGroup<string, int>("key", Enumerable.Range(0, count));
var group = new ReadOnlyObservableGroup<string, int>(sourceGroup);
var iReadOnlyObservableGroup = (IReadOnlyObservableGroup)group;
iReadOnlyObservableGroup.Key.Should().Be("key");
iReadOnlyObservableGroup.Count.Should().Be(count);
}
}
}

View File

@ -0,0 +1,472 @@
// 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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using FluentAssertions;
using Microsoft.Toolkit.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.Collections
{
[TestClass]
public class ReadOnlyObservableGroupedCollectionTests
{
[TestCategory("Collections")]
[TestMethod]
public void Ctor_WithEmptySource_ShoudInitializeObject()
{
var source = new ObservableGroupedCollection<string, int>();
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
readOnlyGroup.Should().BeEmpty();
readOnlyGroup.Count.Should().Be(0);
}
[TestCategory("Collections")]
[TestMethod]
public void Ctor_WithObservableGroupedCollection_ShoudInitializeObject()
{
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", new[] { 1, 3, 5 }),
new IntGroup("B", new[] { 2, 4, 6 }),
};
var source = new ObservableGroupedCollection<string, int>(groups);
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
readOnlyGroup.Should().HaveCount(2);
readOnlyGroup.Count.Should().Be(2);
readOnlyGroup.ElementAt(0).Key.Should().Be("A");
readOnlyGroup.ElementAt(0).Should().BeEquivalentTo(new[] { 1, 3, 5 }, o => o.WithoutStrictOrdering());
readOnlyGroup.ElementAt(1).Key.Should().Be("B");
readOnlyGroup.ElementAt(1).Should().BeEquivalentTo(new[] { 2, 4, 6 }, o => o.WithoutStrictOrdering());
}
[TestCategory("Collections")]
[TestMethod]
public void Ctor_WithListOfIGroupingSource_ShoudInitializeObject()
{
var source = new List<IGrouping<string, int>>
{
new IntGroup("A", new[] { 1, 3, 5 }),
new IntGroup("B", new[] { 2, 4, 6 }),
};
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
readOnlyGroup.Should().HaveCount(2);
readOnlyGroup.Count.Should().Be(2);
readOnlyGroup.ElementAt(0).Key.Should().Be("A");
readOnlyGroup.ElementAt(0).Should().BeEquivalentTo(new[] { 1, 3, 5 }, o => o.WithoutStrictOrdering());
readOnlyGroup.ElementAt(1).Key.Should().Be("B");
readOnlyGroup.ElementAt(1).Should().BeEquivalentTo(new[] { 2, 4, 6 }, o => o.WithoutStrictOrdering());
}
[TestCategory("Collections")]
[TestMethod]
public void Ctor_WithListOfReadOnlyObservableGroupSource_ShoudInitializeObject()
{
var source = new List<ReadOnlyObservableGroup<string, int>>
{
new ReadOnlyObservableGroup<string, int>("A", new[] { 1, 3, 5 }),
new ReadOnlyObservableGroup<string, int>("B", new[] { 2, 4, 6 }),
};
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
readOnlyGroup.Should().HaveCount(2);
readOnlyGroup.Count.Should().Be(2);
readOnlyGroup.ElementAt(0).Key.Should().Be("A");
readOnlyGroup.ElementAt(0).Should().BeEquivalentTo(new[] { 1, 3, 5 }, o => o.WithoutStrictOrdering());
readOnlyGroup.ElementAt(1).Key.Should().Be("B");
readOnlyGroup.ElementAt(1).Should().BeEquivalentTo(new[] { 2, 4, 6 }, o => o.WithoutStrictOrdering());
}
[TestCategory("Collections")]
[TestMethod]
public void IListImplementation_Properties_ShoudReturnExpectedValues()
{
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", new[] { 1, 3, 5 }),
new IntGroup("B", new[] { 2, 4, 6 }),
};
var source = new ObservableGroupedCollection<string, int>(groups);
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
var list = (IList)readOnlyGroup;
list.Count.Should().Be(2);
var group0 = (ReadOnlyObservableGroup<string, int>)list[0];
group0.Key.Should().Be("A");
group0.Should().BeEquivalentTo(new[] { 1, 3, 5 }, o => o.WithoutStrictOrdering());
var group1 = (ReadOnlyObservableGroup<string, int>)list[1];
group1.Key.Should().Be("B");
group1.Should().BeEquivalentTo(new[] { 2, 4, 6 }, o => o.WithoutStrictOrdering());
list.SyncRoot.Should().NotBeNull();
list.IsFixedSize.Should().BeTrue();
list.IsReadOnly.Should().BeTrue();
list.IsSynchronized.Should().BeFalse();
}
[TestCategory("Collections")]
[TestMethod]
public void IListImplementation_MutableMethods_ShoudThrow()
{
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", new[] { 1, 3, 5 }),
new IntGroup("B", new[] { 2, 4, 6 }),
};
var source = new ObservableGroupedCollection<string, int>(groups);
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
var list = (IList)readOnlyGroup;
var testGroup = new ReadOnlyObservableGroup<string, int>("test", new ObservableCollection<int>());
Action add = () => list.Add(testGroup);
add.Should().Throw<NotSupportedException>();
Action clear = () => list.Clear();
clear.Should().Throw<NotSupportedException>();
Action insert = () => list.Insert(2, testGroup);
insert.Should().Throw<NotSupportedException>();
Action remove = () => list.Remove(testGroup);
remove.Should().Throw<NotSupportedException>();
Action removeAt = () => list.RemoveAt(2);
removeAt.Should().Throw<NotSupportedException>();
Action set = () => list[2] = testGroup;
set.Should().Throw<NotSupportedException>();
var array = new object[5];
Action copyTo = () => list.CopyTo(array, 0);
copyTo.Should().NotThrow();
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(-1)]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
public void IListImplementation_IndexOf_ShoudReturnExpectedValue(int groupIndex)
{
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", new[] { 1, 3, 5 }),
new IntGroup("B", new[] { 2, 4, 6 }),
new IntGroup("C", new[] { 7, 8, 9 }),
};
var source = new ObservableGroupedCollection<string, int>(groups);
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
var list = (IList)readOnlyGroup;
var groupToSearch = groupIndex >= 0 ? list[groupIndex] : null;
var index = list.IndexOf(groupToSearch);
index.Should().Be(groupIndex);
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(-1, false)]
[DataRow(0, true)]
[DataRow(1, true)]
public void IListImplementation_Contains_ShoudReturnExpectedValue(int groupIndex, bool expectedResult)
{
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", new[] { 1, 3, 5 }),
new IntGroup("B", new[] { 2, 4, 6 }),
};
var source = new ObservableGroupedCollection<string, int>(groups);
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
var list = (IList)readOnlyGroup;
var groupToSearch = groupIndex >= 0 ? list[groupIndex] : null;
var result = list.Contains(groupToSearch);
result.Should().Be(expectedResult);
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(0, 0)]
[DataRow(3, 3)]
public void AddGroupInSource_ShouldAddGroup(int sourceInitialItemsCount, int expectedInsertionIndex)
{
NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
var collectionChangedEventsCount = 0;
var isCountPropertyChangedEventRaised = false;
var itemsList = new[] { 1, 2, 3 };
var source = new ObservableGroupedCollection<string, int>();
for (var i = 0; i < sourceInitialItemsCount; i++)
{
source.Add(new ObservableGroup<string, int>($"group {i}", Enumerable.Empty<int>()));
}
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
{
collectionChangedEventArgs = e;
collectionChangedEventsCount++;
};
((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);
source.Add(new ObservableGroup<string, int>("Add", itemsList));
var expectedReadOnlyGroupCount = sourceInitialItemsCount + 1;
readOnlyGroup.Should().HaveCount(expectedReadOnlyGroupCount);
readOnlyGroup.Count.Should().Be(expectedReadOnlyGroupCount);
readOnlyGroup.Last().Key.Should().Be("Add");
readOnlyGroup.Last().Should().BeEquivalentTo(itemsList, o => o.WithoutStrictOrdering());
isCountPropertyChangedEventRaised.Should().BeTrue();
collectionChangedEventArgs.Should().NotBeNull();
collectionChangedEventsCount.Should().Be(1);
IsAddEventValid(collectionChangedEventArgs, itemsList, expectedInsertionIndex).Should().BeTrue();
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
public void InsertGroupInSource_ShouldAddGroup(int insertionIndex)
{
NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
var collectionChangedEventsCount = 0;
var isCountPropertyChangedEventRaised = false;
var itemsList = new[] { 1, 2, 3 };
var source = new ObservableGroupedCollection<string, int>
{
new ObservableGroup<string, int>("Group0", new[] { 10, 20, 30 }),
new ObservableGroup<string, int>("Group1", new[] { 40, 50, 60 })
};
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
{
collectionChangedEventArgs = e;
collectionChangedEventsCount++;
};
((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);
source.Insert(insertionIndex, new ObservableGroup<string, int>("Add", itemsList));
readOnlyGroup.Should().HaveCount(3);
readOnlyGroup.Count.Should().Be(3);
readOnlyGroup.ElementAt(insertionIndex).Key.Should().Be("Add");
readOnlyGroup.ElementAt(insertionIndex).Should().BeEquivalentTo(itemsList, o => o.WithoutStrictOrdering());
isCountPropertyChangedEventRaised.Should().BeTrue();
collectionChangedEventArgs.Should().NotBeNull();
collectionChangedEventsCount.Should().Be(1);
IsAddEventValid(collectionChangedEventArgs, itemsList, addIndex: insertionIndex).Should().BeTrue();
}
[TestCategory("Collections")]
[TestMethod]
public void RemoveGroupInSource_ShoudRemoveGroup()
{
NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
var collectionChangedEventsCount = 0;
var isCountPropertyChangedEventRaised = false;
var aItemsList = new[] { 1, 2, 3 };
var bItemsList = new[] { 2, 4, 6 };
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", aItemsList),
new IntGroup("B", bItemsList),
};
var source = new ObservableGroupedCollection<string, int>(groups);
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
{
collectionChangedEventArgs = e;
collectionChangedEventsCount++;
};
((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);
source.RemoveAt(1);
readOnlyGroup.Should().ContainSingle();
readOnlyGroup.Count.Should().Be(1);
readOnlyGroup.ElementAt(0).Key.Should().Be("A");
readOnlyGroup.ElementAt(0).Should().BeEquivalentTo(aItemsList, o => o.WithoutStrictOrdering());
isCountPropertyChangedEventRaised.Should().BeTrue();
collectionChangedEventArgs.Should().NotBeNull();
collectionChangedEventsCount.Should().Be(1);
IsRemoveEventValid(collectionChangedEventArgs, bItemsList, 1).Should().BeTrue();
}
[TestCategory("Collections")]
[DataTestMethod]
[DataRow(1, 0)]
[DataRow(0, 1)]
public void MoveGroupInSource_ShoudMoveGroup(int oldIndex, int newIndex)
{
NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
var collectionChangedEventsCount = 0;
var isCountPropertyChangedEventRaised = false;
var aItemsList = new[] { 1, 2, 3 };
var bItemsList = new[] { 2, 4, 6 };
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", aItemsList),
new IntGroup("B", bItemsList),
};
var source = new ObservableGroupedCollection<string, int>(groups);
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
{
collectionChangedEventArgs = e;
collectionChangedEventsCount++;
};
((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);
source.Move(oldIndex, newIndex);
readOnlyGroup.Should().HaveCount(2);
readOnlyGroup.Count.Should().Be(2);
readOnlyGroup.ElementAt(0).Key.Should().Be("B");
readOnlyGroup.ElementAt(0).Should().BeEquivalentTo(bItemsList, o => o.WithoutStrictOrdering());
readOnlyGroup.ElementAt(1).Key.Should().Be("A");
readOnlyGroup.ElementAt(1).Should().BeEquivalentTo(aItemsList, o => o.WithoutStrictOrdering());
isCountPropertyChangedEventRaised.Should().BeFalse();
collectionChangedEventArgs.Should().NotBeNull();
collectionChangedEventsCount.Should().Be(1);
IsMoveEventValid(collectionChangedEventArgs, groups[oldIndex], oldIndex, newIndex).Should().BeTrue();
}
[TestCategory("Collections")]
[TestMethod]
public void ClearSource_ShoudClear()
{
NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
var collectionChangedEventsCount = 0;
var isCountPropertyChangedEventRaised = false;
var aItemsList = new[] { 1, 2, 3 };
var bItemsList = new[] { 2, 4, 6 };
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", aItemsList),
new IntGroup("B", bItemsList),
};
var source = new ObservableGroupedCollection<string, int>(groups);
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
{
collectionChangedEventArgs = e;
collectionChangedEventsCount++;
};
((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);
source.Clear();
readOnlyGroup.Should().BeEmpty();
readOnlyGroup.Count.Should().Be(0);
isCountPropertyChangedEventRaised.Should().BeTrue();
collectionChangedEventArgs.Should().NotBeNull();
collectionChangedEventsCount.Should().Be(1);
IsResetEventValid(collectionChangedEventArgs).Should().BeTrue();
}
[TestCategory("Collections")]
[TestMethod]
public void ReplaceGroupInSource_ShoudReplaceGroup()
{
NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
var collectionChangedEventsCount = 0;
var isCountPropertyChangedEventRaised = false;
var aItemsList = new[] { 1, 2, 3 };
var bItemsList = new[] { 2, 4, 6 };
var cItemsList = new[] { 7, 8, 9 };
var groups = new List<IGrouping<string, int>>
{
new IntGroup("A", aItemsList),
new IntGroup("B", bItemsList),
};
var source = new ObservableGroupedCollection<string, int>(groups);
var readOnlyGroup = new ReadOnlyObservableGroupedCollection<string, int>(source);
((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
{
collectionChangedEventArgs = e;
collectionChangedEventsCount++;
};
((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);
source[0] = new ObservableGroup<string, int>("C", cItemsList);
readOnlyGroup.Should().HaveCount(2);
readOnlyGroup.Count.Should().Be(2);
readOnlyGroup.ElementAt(0).Key.Should().Be("C");
readOnlyGroup.ElementAt(0).Should().BeEquivalentTo(cItemsList, o => o.WithoutStrictOrdering());
readOnlyGroup.ElementAt(1).Key.Should().Be("B");
readOnlyGroup.ElementAt(1).Should().BeEquivalentTo(bItemsList, o => o.WithoutStrictOrdering());
isCountPropertyChangedEventRaised.Should().BeFalse();
collectionChangedEventArgs.Should().NotBeNull();
collectionChangedEventsCount.Should().Be(1);
IsReplaceEventValid(collectionChangedEventArgs, aItemsList, cItemsList).Should().BeTrue();
}
private static bool IsAddEventValid(NotifyCollectionChangedEventArgs args, IEnumerable<int> expectedGroupItems, int addIndex)
{
var newItems = args.NewItems?.Cast<IEnumerable<int>>();
return args.Action == NotifyCollectionChangedAction.Add &&
args.NewStartingIndex == addIndex &&
args.OldItems == null &&
newItems?.Count() == 1 &&
Enumerable.SequenceEqual(newItems.ElementAt(0), expectedGroupItems);
}
private static bool IsRemoveEventValid(NotifyCollectionChangedEventArgs args, IEnumerable<int> expectedGroupItems, int oldIndex)
{
var oldItems = args.OldItems?.Cast<IEnumerable<int>>();
return args.Action == NotifyCollectionChangedAction.Remove &&
args.NewItems == null &&
args.OldStartingIndex == oldIndex &&
oldItems?.Count() == 1 &&
Enumerable.SequenceEqual(oldItems.ElementAt(0), expectedGroupItems);
}
private static bool IsMoveEventValid(NotifyCollectionChangedEventArgs args, IEnumerable<int> expectedGroupItems, int oldIndex, int newIndex)
{
var oldItems = args.OldItems?.Cast<IEnumerable<int>>();
var newItems = args.NewItems?.Cast<IEnumerable<int>>();
return args.Action == NotifyCollectionChangedAction.Move &&
args.OldStartingIndex == oldIndex &&
args.NewStartingIndex == newIndex &&
oldItems?.Count() == 1 &&
Enumerable.SequenceEqual(oldItems.ElementAt(0), expectedGroupItems) &&
newItems?.Count() == 1 &&
Enumerable.SequenceEqual(newItems.ElementAt(0), expectedGroupItems);
}
private static bool IsReplaceEventValid(NotifyCollectionChangedEventArgs args, IEnumerable<int> expectedRemovedItems, IEnumerable<int> expectedAddItems)
{
var oldItems = args.OldItems?.Cast<IEnumerable<int>>();
var newItems = args.NewItems?.Cast<IEnumerable<int>>();
return args.Action == NotifyCollectionChangedAction.Replace &&
oldItems?.Count() == 1 &&
Enumerable.SequenceEqual(oldItems.ElementAt(0), expectedRemovedItems) &&
newItems?.Count() == 1 &&
Enumerable.SequenceEqual(newItems.ElementAt(0), expectedAddItems);
}
private static bool IsResetEventValid(NotifyCollectionChangedEventArgs args) => args.Action == NotifyCollectionChangedAction.Reset && args.NewItems == null && args.OldItems == null;
}
}

View File

@ -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_HasSizeGreaterThan_ArrayOk()
{
Guard.HasSizeGreaterThan(new int[5], 2, nameof(Test_Guard_HasSizeGreaterThan_ArrayOk));
}
[TestCategory("Guard")]
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_HasSizeGreaterThan_ArrayEqualFail()
{
Guard.HasSizeGreaterThan(new int[4], 4, nameof(Test_Guard_HasSizeGreaterThan_ArrayEqualFail));
}
[TestCategory("Guard")]
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_HasSizeGreaterThan_ArraSmallerFail()
{
Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThan_ArraSmallerFail));
}
[TestCategory("Guard")]
[TestMethod]
public void Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayOk()
{
Guard.HasSizeGreaterThanOrEqualTo(new int[5], 2, nameof(Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayOk));
Guard.HasSizeGreaterThanOrEqualTo(new int[2], 2, nameof(Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayOk));
}
[TestCategory("Guard")]
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayFail()
{
Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThanOrEqualTo_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));
}
}
}

View File

@ -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}");
}
}
}
}

View File

@ -0,0 +1,601 @@
// 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));
Guard.IsTrue(true, nameof(Test_Guard_IsTrue_Ok), "Hello world");
}
[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_IsTrue_Fail_WithMessage()
{
try
{
Guard.IsTrue(false, nameof(Test_Guard_IsTrue_Fail_WithMessage), "Hello world");
}
catch (ArgumentException e)
{
Assert.IsTrue(e.Message.Contains("\"Hello world\""));
return;
}
// Compiler detects this is unreachable from attribute,
// but we leave the assertion to double check that's valid
Assert.Fail();
}
[TestCategory("Guard")]
[TestMethod]
public void Test_Guard_IsFalse_Ok()
{
Guard.IsFalse(false, nameof(Test_Guard_IsFalse_Ok));
Guard.IsFalse(false, nameof(Test_Guard_IsFalse_Ok), "Hello world");
}
[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_IsFalse_Fail_WithMessage()
{
try
{
Guard.IsFalse(true, nameof(Test_Guard_IsFalse_Fail_WithMessage), "Hello world");
}
catch (ArgumentException e)
{
Assert.IsTrue(e.Message.Contains("\"Hello world\""));
return;
}
Assert.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));
}
}
}

View File

@ -2,11 +2,11 @@
// 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;
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.Toolkit.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.Extensions
{
@ -174,9 +174,9 @@ public void Test_ArrayExtensions_Jagged_GetColumn()
{
int[][] array =
{
new int[] { 5, 2, 4 },
new int[] { 6, 3 },
new int[] { 7 }
new int[] { 5, 2, 4 },
new int[] { 6, 3 },
new int[] { 7 }
};
var col = array.GetColumn(1).ToArray();
@ -188,7 +188,7 @@ public void Test_ArrayExtensions_Jagged_GetColumn()
[TestMethod]
public void Test_ArrayExtensions_Jagged_GetColumn_Exception()
{
int[][] array =
int[][] array =
{
new int[] { 5, 2, 4 },
new int[] { 6, 3 },

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>4e9466d1-d5aa-46ac-801b-c8fdab79f0d4</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>UnitTests.Shared</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Collections\IntGroup.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Collections\ObservableGroupedCollectionExtensionsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Collections\ObservableGroupedCollectionTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Collections\ObservableGroupTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Collections\ReadOnlyObservableGroupedCollectionTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Collections\ReadOnlyObservableGroupTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Test_Guard.Array.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Test_Guard.Comparable.Numeric.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Test_Guard.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_ArrayExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_TypeExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_ValueTypeExtensions.cs" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>4e9466d1-d5aa-46ac-801b-c8fdab79f0d4</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="UnitTests.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>