Skip to content
Merged
Show file tree
Hide file tree
Changes from 151 commits
Commits
Show all changes
178 commits
Select commit Hold shift + click to select a range
1c79157
Initial guard APIs added
Sergio0694 Feb 13, 2020
0554d2f
Added EqualTo and NotEqualTo APIs
Sergio0694 Feb 13, 2020
6dc701f
Merge branch 'master' into feature/guard-apis
Sergio0694 Feb 13, 2020
018e5c4
Added more Guard APIs
Sergio0694 Feb 13, 2020
8d78ba3
New interval Guard APIs added
Sergio0694 Feb 13, 2020
0b76fb9
Added Guard APIs for IEnumerable<T> values
Sergio0694 Feb 13, 2020
8028741
Renamed APIs
Sergio0694 Feb 13, 2020
cd23275
Added Guard APIs for Stream values
Sergio0694 Feb 13, 2020
a54441c
Added Guard APIs for ReadOnlySpan<T> values
Sergio0694 Feb 13, 2020
8dee59a
Added Guard APIs for ReadOnlyMemory<T> values
Sergio0694 Feb 13, 2020
2d32333
Code refactoring
Sergio0694 Feb 13, 2020
19e3bcc
Minor code refactoring
Sergio0694 Feb 13, 2020
0ff1f00
Added Guard.IsBitwiseEqualTo<T> API
Sergio0694 Feb 13, 2020
9dd8a80
Added Guard.IsNull API
Sergio0694 Feb 13, 2020
f2a4500
Code refactoring and optimizations
Sergio0694 Feb 13, 2020
b79daa7
Code refactoring
Sergio0694 Feb 13, 2020
d44e633
Added Guard.HasSizeNotEqualTo<T> API
Sergio0694 Feb 13, 2020
0292f9d
Added Guard size API overloads for the string type
Sergio0694 Feb 13, 2020
073a205
Added Guard size API overloads for T[] arrays
Sergio0694 Feb 13, 2020
5edef16
Code refactoring
Sergio0694 Feb 13, 2020
1a19fea
Removed unnecessary using directives
Sergio0694 Feb 13, 2020
d38ad13
Added Guard reference check APIs
Sergio0694 Feb 13, 2020
e9108fc
Added new Guaard APIs for string values
Sergio0694 Feb 13, 2020
18bb2d4
Improved Guard.IsBitwiseEqualTo<T> API
Sergio0694 Feb 13, 2020
2d9536f
Improved Guard.IsNull APIs
Sergio0694 Feb 13, 2020
a927dad
Added new size APIs for copy operations
Sergio0694 Feb 13, 2020
56a3a18
Added new type test APIs, minor code tweaks
Sergio0694 Feb 13, 2020
bbd10a8
Minor bug fixes
Sergio0694 Feb 13, 2020
fbb5801
Added Guard APIs for Task values
Sergio0694 Feb 14, 2020
eba232e
Minor code tweak
Sergio0694 Feb 14, 2020
9071e12
Added new Guard.IsEmpty APIs
Sergio0694 Feb 14, 2020
edb1d41
Refactored code to remove unsafe requirement
Sergio0694 Feb 14, 2020
d9c83f4
Added Guard.IsInRange<T> APIs
Sergio0694 Feb 14, 2020
e63402b
Added missing type checks in Guard.IsBitwiseEqualTo API
Sergio0694 Feb 17, 2020
87056c9
Added ValueTypeExtensions.ToHexString API
Sergio0694 Feb 18, 2020
0d34c0a
Code refactoring
Sergio0694 Feb 18, 2020
497d149
Added ValueTypeExtensions tests
Sergio0694 Feb 18, 2020
bc0fc1b
Added general Guard tests
Sergio0694 Feb 18, 2020
606cbde
Bug fixes in the Guard class
Sergio0694 Feb 18, 2020
f29cd93
Minor speed improvements
Sergio0694 Feb 18, 2020
79d2dd4
Code refactoring
Sergio0694 Feb 18, 2020
793c769
Removed unnecessary using directives
Sergio0694 Feb 18, 2020
8e2f2d8
More speed improvements and API refactoring
Sergio0694 Feb 18, 2020
5b58e5e
Refactored/fixed some Guard APIs
Sergio0694 Feb 18, 2020
b664c90
More bug fixes
Sergio0694 Feb 18, 2020
7f65184
Added Guard tests for array APIs
Sergio0694 Feb 18, 2020
4304edd
Fixed the Guard.IsNotEmpty<T> array API
Sergio0694 Feb 18, 2020
4802936
Moved exception throwers to separate class
Sergio0694 Feb 23, 2020
ed5315a
Moved general Guard throwers to separate class
Sergio0694 Feb 23, 2020
1772a0f
Disabled warning for XML overload resolution
Sergio0694 Feb 23, 2020
9814d91
Moved array Guard throwers to separate class
Sergio0694 Feb 23, 2020
1b78344
Moved stream Guard throwers to separate class
Sergio0694 Feb 23, 2020
bc0dc18
Fixed some XML docs
Sergio0694 Feb 23, 2020
e000fe0
Moved enumerable Guard throwers to separate class
Sergio0694 Feb 23, 2020
21cf16a
Moved task Guard throwers to separate class
Sergio0694 Feb 23, 2020
c1b9c8b
Added new Guard APIs for tasks
Sergio0694 Feb 23, 2020
c784d4e
Moved string Guard throwers to separate class
Sergio0694 Feb 23, 2020
8cf87c3
Moved ReadOnlySpan<T> Guard throwers to separate class
Sergio0694 Feb 23, 2020
24e03f5
Added Span<T> APIs to the Guard class
Sergio0694 Feb 23, 2020
6f1679b
Moved ReadOnlyMemory<T> Guard throwers to separate class
Sergio0694 Feb 23, 2020
7e80e43
Added Memory<T> APIs to the Guard class
Sergio0694 Feb 23, 2020
662bb83
Minor code refactoring
Sergio0694 Feb 23, 2020
1c334df
Update file headers
Sergio0694 Feb 23, 2020
eccc2a6
Removed unnecessary methods
Sergio0694 Feb 23, 2020
15ef6a3
Added TypeExtensions.ToTypeString extension
Sergio0694 Feb 24, 2020
317fc99
Improved error messages for general Guard APIs
Sergio0694 Feb 24, 2020
cb1f3b0
Improved error messages for Enumerable Guard APIs
Sergio0694 Feb 24, 2020
23f1504
Improved error messages for stream Guard APIs
Sergio0694 Feb 24, 2020
920e69f
Improved error messages for array Guard APIs
Sergio0694 Feb 24, 2020
18e661f
Improved error messages for string Guard APIs
Sergio0694 Feb 24, 2020
d00e0e3
Improved error messages for task Guard APIs
Sergio0694 Feb 24, 2020
6aeedaa
Improved error messages for span Guard APIs
Sergio0694 Feb 24, 2020
acafd8a
Improved error messages for memory Guard APIs
Sergio0694 Feb 24, 2020
8c7757a
Improved type string for nullable types
Sergio0694 Feb 24, 2020
c2fd97c
Improved type string for value tuple types
Sergio0694 Feb 24, 2020
8facb7e
Minor performance improvement
Sergio0694 Feb 24, 2020
2952e23
Added numeric comparison T4 file
Sergio0694 Feb 29, 2020
4991a5c
Updated .gitignore to skip generated files
Sergio0694 Feb 29, 2020
714cf31
Moved comparison Guard APIs to separate file
Sergio0694 Feb 29, 2020
fa8cc56
Renamed template file, fixed incorrect XML doc
Sergio0694 Feb 29, 2020
effb2ed
Fixed missing blank line
Sergio0694 Mar 1, 2020
e2e5171
Removed unnecessary type parameters
Sergio0694 Mar 1, 2020
4482217
Moved enumerable Guard APIs to separate file
Sergio0694 Mar 1, 2020
94872b9
Code refactoring in the enumerable T4 template
Sergio0694 Mar 1, 2020
756c842
Renamed a file
Sergio0694 Mar 1, 2020
0ecef12
Added empty template for ThrowHelper.Collection
Sergio0694 Mar 1, 2020
e093427
Code refactoring in the T4 templates
Sergio0694 Mar 1, 2020
9db9cd0
Switched collection throw helpers to T4 generation
Sergio0694 Mar 1, 2020
f6e3248
Code refactoring
Sergio0694 Mar 1, 2020
deb302b
Merge pull request #7 from Sergio0694/feature/guard-apis-T4
Sergio0694 Mar 1, 2020
fcc3c05
Removed incorrect file header
Sergio0694 Mar 1, 2020
760972f
Added new Guard.IsInRangeFor API
Sergio0694 Mar 1, 2020
8d6cc8a
Updated .gitignore, added generated files to source control
Sergio0694 Mar 3, 2020
e3d245f
Added tests for Guard.IsInRangeFor
Sergio0694 Mar 3, 2020
d367add
Improved formatting of values in error messages
Sergio0694 Mar 4, 2020
6d97f33
Improved error messages
Sergio0694 Mar 4, 2020
ec67152
More improvements to the error messages
Sergio0694 Mar 4, 2020
b2c3acb
Minor code tweaks
Sergio0694 Mar 5, 2020
43b22f0
Added Guard.IsNotOfType APIs
Sergio0694 Mar 5, 2020
c8af095
Added Guard.IsNotAssignableToType APIs
Sergio0694 Mar 5, 2020
7adfa79
Added Guard.IsDefault<T> APIs
Sergio0694 Mar 8, 2020
95b9fd1
Merge branch 'master' into feature/guard-apis
Sergio0694 Mar 9, 2020
d4c2d05
Fixed some XML docs
Sergio0694 Mar 11, 2020
afd973e
Added missing [Pure] attribute
Sergio0694 Mar 11, 2020
96ce238
Fixed typo in a comment
Sergio0694 Mar 11, 2020
cb98a02
Removed unnecessary comment after code changes
Sergio0694 Mar 11, 2020
5f25e18
Added XML comment for the T4 service
Sergio0694 Mar 11, 2020
cf5785c
Added more comments to ToHexString
Sergio0694 Mar 11, 2020
801dc93
Suppressed an incorrect code style warning
Sergio0694 Mar 11, 2020
b840201
Added more tests for Guard.IsInRange
Sergio0694 Mar 11, 2020
818c1eb
Added Guard.IsNotInRangeFor APIs
Sergio0694 Mar 11, 2020
caad2f7
Added readme file for the T4 templates
Sergio0694 Mar 11, 2020
7134f98
Changed generated files extension to .g.cs to avoid conflicts
Sergio0694 Mar 11, 2020
01a7257
Added Guard.IsCloseTo and Guard.IsNotCloseTo APIs
Sergio0694 Mar 11, 2020
f33c984
Added tests for IsCloseTo
Sergio0694 Mar 11, 2020
1b0f2c9
Updated ".g.cs" extension for generated files
Sergio0694 Mar 11, 2020
ef189ba
Removed extra space (typo)
Sergio0694 Mar 11, 2020
ac44dad
More tweakes to the Guard.md file
Sergio0694 Mar 11, 2020
21115f6
Excluded TypeInfo.g.cs file from .ttinclude from checkout
Sergio0694 Mar 11, 2020
67fcd47
Added more info about target ranges for some APIs
Sergio0694 Mar 11, 2020
41fea84
Added IsCloseTo overloads for long type
Sergio0694 Mar 11, 2020
132d240
Minor code tweaks
Sergio0694 Mar 11, 2020
f17aacd
Added comment to describe (uint) cast range check trick
Sergio0694 Mar 11, 2020
949134b
Added a nore about the .g.cs files being checked in
Sergio0694 Mar 13, 2020
c9283d1
Merge branch 'master' into feature/guard-apis
michael-hawker Mar 13, 2020
b599bc1
Added .NET Standard 2.1 target, adjusted imported packages
Sergio0694 Mar 13, 2020
68ffb8e
Added [NotNull] attributes when needed
Sergio0694 Mar 13, 2020
2dd7e4a
Merge branch 'master' of https://github.com/windows-toolkit/WindowsCo…
Sergio0694 Mar 15, 2020
7dd004e
Merge branch 'windows-toolkit-master' into feature/guard-apis-netstan…
Sergio0694 Mar 15, 2020
c40e17d
Added flow attributes to bool Guard APIs
Sergio0694 Mar 15, 2020
afa5047
Merge pull request #10 from windows-toolkit/master
Sergio0694 Mar 15, 2020
a0d8f14
Added missing Guard APIs for the string type
Sergio0694 Mar 15, 2020
5d33d35
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Mar 18, 2020
63e558e
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Mar 19, 2020
a6d46b1
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Mar 24, 2020
b2d5a13
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Mar 26, 2020
e8cd4f7
Merge pull request #18 from windows-toolkit/master
Sergio0694 Mar 27, 2020
61719e0
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Mar 31, 2020
b1084c6
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Apr 1, 2020
fa8adc1
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Apr 1, 2020
824f2af
Merge branch 'master' into feature/guard-apis-netstandard2.1
azchohfi Apr 2, 2020
454a550
Added shared main unit test project
Sergio0694 Apr 2, 2020
7df62ab
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Apr 2, 2020
c260234
Removed duplicate files from UWP unit test project
Sergio0694 Apr 2, 2020
8564398
Merge branch 'master' into feature/guard-apis-netstandard2.1
azchohfi Apr 2, 2020
8c62dc3
Merge branch 'feature/guard-apis-netstandard2.1' of https://github.co…
Sergio0694 Apr 2, 2020
e806a95
Refactored UWP unit tests project (can't be in same folder as .shproj)
Sergio0694 Apr 2, 2020
42030bd
Moved markdown tests back into UWP project
Sergio0694 Apr 2, 2020
0754dfb
Added UnitTests.NetCore project
Sergio0694 Apr 2, 2020
08467bb
Fixed some styling issues in test projects
Sergio0694 Apr 2, 2020
eeabce0
Added .editorconfig file to UniTests.NetCore project too
Sergio0694 Apr 2, 2020
5e713fa
Moved .editorconfig file to Shared project folder
Sergio0694 Apr 2, 2020
cbc80ef
Merge pull request #19 from windows-toolkit/master
Sergio0694 Apr 3, 2020
31a4938
Removed NetCore Tests compilation for Native Configuration.
azchohfi Apr 4, 2020
72861f4
Update Windows Community Toolkit.sln
azchohfi Apr 4, 2020
4079a75
Merge remote-tracking branch 'upstream/master' into master2
Sergio0694 Apr 9, 2020
d8b3c08
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Apr 10, 2020
6f0e258
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 Apr 19, 2020
f0b5bf0
Merge remote-tracking branch 'upstream/master' into master2
Sergio0694 Apr 23, 2020
6129c69
Merge remote-tracking branch 'upstream/master' into master2
Sergio0694 Apr 30, 2020
fdaa307
Merge remote-tracking branch 'upstream/master' into master2
Sergio0694 May 6, 2020
73e277e
Updated multiline comments style
Sergio0694 May 8, 2020
0be5134
Renamed some Guard APIs
Sergio0694 May 9, 2020
29b4f58
Merge branch 'master2' into feature/guard-apis-netstandard2.1
Sergio0694 May 9, 2020
80e67d3
Fixed collections tests, moved to shared project
Sergio0694 May 9, 2020
2f2aafe
Removed unnecessary [SuppressMessage] attributes
Sergio0694 May 12, 2020
c30560a
Added internal [NotNull] and [DoesNotReturnIf] attributes
Sergio0694 May 12, 2020
a2dcde9
Removed build warnings
Sergio0694 May 12, 2020
1523b58
Removed leftover compiler directives
Sergio0694 May 12, 2020
b67e5b8
Added Guard.IsTrue/False overloads with custom message
Sergio0694 May 12, 2020
dc4f747
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 May 12, 2020
16e35b2
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 May 13, 2020
1a62ad8
Merge remote-tracking branch 'upstream/master' into feature/guard-api…
Sergio0694 May 15, 2020
e59c7ab
Fixed test file location after merge
Sergio0694 May 15, 2020
0c51da8
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 May 15, 2020
73b4c74
Fixed Span<T> and Unsafe package references
Sergio0694 May 15, 2020
f4ac2a0
Merge branch 'master' into feature/guard-apis-netstandard2.1
michael-hawker May 15, 2020
e8f0e7f
Merge branch 'master' into feature/guard-apis-netstandard2.1
Sergio0694 May 15, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Microsoft.Toolkit.Parsers/Markdown/Blocks/ListBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Microsoft.Toolkit.Parsers.Core;
using Microsoft.Toolkit.Parsers.Markdown.Helpers;

[assembly: InternalsVisibleTo("UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010041753af735ae6140c9508567666c51c6ab929806adb0d210694b30ab142a060237bc741f9682e7d8d4310364b4bba4ee89cc9d3d5ce7e5583587e8ea44dca09977996582875e71fb54fa7b170798d853d5d8010b07219633bdb761d01ac924da44576d6180cdceae537973982bb461c541541d58417a3794e34f45e6f2d129e2")]
[assembly: InternalsVisibleTo("UnitTests.UWP, PublicKey=002400000480000094000000060200000024000052534131000400000100010041753af735ae6140c9508567666c51c6ab929806adb0d210694b30ab142a060237bc741f9682e7d8d4310364b4bba4ee89cc9d3d5ce7e5583587e8ea44dca09977996582875e71fb54fa7b170798d853d5d8010b07219633bdb761d01ac924da44576d6180cdceae537973982bb461c541541d58417a3794e34f45e6f2d129e2")]

namespace Microsoft.Toolkit.Parsers.Markdown.Blocks
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: InternalsVisibleTo("UnitTests")]
[assembly: InternalsVisibleTo("UnitTests.UWP")]
[assembly: NeutralResourcesLanguage("en-US")]
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: InternalsVisibleTo("UnitTests")]
[assembly: InternalsVisibleTo("UnitTests.UWP")]
[assembly: NeutralResourcesLanguage("en-US")]
81 changes: 79 additions & 2 deletions Microsoft.Toolkit/Diagnostics/Guard.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
#if NETSTANDARD2_1
using System.Diagnostics.CodeAnalysis;
#endif
using System.Runtime.CompilerServices;

#nullable enable
Expand Down Expand Up @@ -36,7 +39,11 @@ 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(
#if NETSTANDARD2_1
[NotNull]
#endif
string? text, string name)
{
if (string.IsNullOrEmpty(text))
{
Expand Down Expand Up @@ -66,7 +73,11 @@ 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(
#if NETSTANDARD2_1
[NotNull]
#endif
string? text, string name)
{
if (string.IsNullOrWhiteSpace(text))
{
Expand Down Expand Up @@ -229,5 +240,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);
}
}
}
}
35 changes: 31 additions & 4 deletions Microsoft.Toolkit/Diagnostics/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,22 @@ 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)
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1114", Justification = "Comment for [NotNull] attribute")]
public static void IsNotNull<T>(
#if NETSTANDARD2_1
/* On .NET Standard 2.1, we can add the [NotNull] attribute for
* methods that throw if an argument of a nullable type is null.
* This will let the compiler know that after a call to such
* methods, the value of the variable passed as parameter will
* never be null (since if that's the case, the code would have
* thrown an exception instead of continuing the execution).
* This makes it easy for developers to keep track of the
* nullable state of variables, and to avoid having to use
* the '!' operator to explicitly ignore compiler warnings
* about variables possibly being null. */
[NotNull]
#endif
T? value, string name)
where T : class
{
if (value is null)
Expand All @@ -80,7 +95,11 @@ 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>(
#if NETSTANDARD2_1
[NotNull]
#endif
T? value, string name)
where T : struct
{
if (value is null)
Expand Down Expand Up @@ -262,7 +281,11 @@ 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(
#if NETSTANDARD2_1
[DoesNotReturnIf(false)]
#endif
bool value, string name)
{
if (!value)
{
Expand All @@ -277,7 +300,11 @@ 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(
#if NETSTANDARD2_1
[DoesNotReturnIf(true)]
#endif
bool value, string name)
{
if (value)
{
Expand Down
40 changes: 38 additions & 2 deletions Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,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)
Expand Down Expand Up @@ -134,12 +134,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()}");
}
}
}
10 changes: 8 additions & 2 deletions Microsoft.Toolkit/Microsoft.Toolkit.csproj
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -16,10 +16,16 @@
<DebugType>Full</DebugType>
</PropertyGroup>

<ItemGroup>
<!-- .NET Standard 2.0 doesn't have the Span<T> type -->
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Memory" Version="4.5.3" />
</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.0" />
</ItemGroup>

<ItemGroup>
<None Update="Diagnostics\Generated\Guard.Comparable.Numeric.tt">
<Generator>TextTemplatingFileGenerator</Generator>
Expand Down
20 changes: 20 additions & 0 deletions UnitTests/UnitTests.NetCore/UnitTests.NetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<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>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public void Test_Guard_IsGreaterThan_LowerFalse()
[TestMethod]
public void Test_Guard_IsGreaterThanOrEqualTo_Ok()
{
Guard.IsGreaterThanOrEqualTo(2,1, nameof(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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand All @@ -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 },
Expand Down
19 changes: 19 additions & 0 deletions UnitTests/UnitTests.Shared/UnitTests.Shared.projitems
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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)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>
Loading