generated from dailydevops/dotnet-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: Added methods `ThrowIfEqual` and `ThrowIfNotEqual` * docs: Updated README * chore(tests): Tests for newly introduced methods added * chore(tests): Tests for newly introduced method `ThrowIfNull(void*, string?)` added * docs: Updated README
- Loading branch information
Showing
15 changed files
with
259 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace NetEvolve.Arguments; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
public static partial class Argument | ||
{ | ||
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is equal to <paramref name="other"/>.</summary> | ||
/// <param name="value">The argument to validate as not equal to <paramref name="other"/>.</param> | ||
/// <param name="other">The value to compare with <paramref name="value"/>.</param> | ||
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param> | ||
[DebuggerStepThrough] | ||
[StackTraceHidden] | ||
#if NET8_0_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#else | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
#endif | ||
public static void ThrowIfEqual<T>( | ||
T value, | ||
T other, | ||
[CallerArgumentExpression(nameof(value))] string? paramName = null | ||
) | ||
where T : IEquatable<T> | ||
{ | ||
#if NET8_0_OR_GREATER | ||
ArgumentOutOfRangeException.ThrowIfEqual(value, other, paramName); | ||
#else | ||
if (EqualityComparer<T>.Default.Equals(value, other)) | ||
{ | ||
ThrowArgumentOutOfRangeException( | ||
paramName, | ||
value, | ||
$"{paramName} ('{value}') must not be equal to '{other}'." | ||
); | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace NetEvolve.Arguments; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
public static partial class Argument | ||
{ | ||
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is not equal to <paramref name="other"/>.</summary> | ||
/// <param name="value">The argument to validate as equal to <paramref name="other"/>.</param> | ||
/// <param name="other">The value to compare with <paramref name="value"/>.</param> | ||
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param> | ||
[DebuggerStepThrough] | ||
[StackTraceHidden] | ||
#if NET8_0_OR_GREATER | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
#else | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
#endif | ||
public static void ThrowIfNotEqual<T>( | ||
T value, | ||
T other, | ||
[CallerArgumentExpression(nameof(value))] string? paramName = null | ||
) | ||
where T : IEquatable<T> | ||
{ | ||
#if NET8_0_OR_GREATER | ||
ArgumentOutOfRangeException.ThrowIfNotEqual(value, other, paramName); | ||
#else | ||
if (!EqualityComparer<T>.Default.Equals(value, other)) | ||
{ | ||
ThrowArgumentOutOfRangeException( | ||
paramName, | ||
value, | ||
$"{paramName} ('{value}') must be equal to '{other}'." | ||
); | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/NetEvolve.Arguments.Tests.Unit/ArgumentTests_ThrowIfEqual.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace NetEvolve.Arguments.Tests.Unit; | ||
|
||
using System; | ||
using Xunit; | ||
|
||
public sealed partial class ArgumentTests | ||
{ | ||
[Fact] | ||
public void ThrowIfEqual_WhenArgumentIsEqualToMaximum_ThrowsArgumentOutOfRangeException() | ||
{ | ||
// Arrange | ||
var argument = 1; | ||
var maximum = 1; | ||
|
||
// Act | ||
void Act() => Argument.ThrowIfEqual(argument, maximum); | ||
|
||
// Assert | ||
_ = Assert.Throws<ArgumentOutOfRangeException>("argument", Act); | ||
} | ||
|
||
[Fact] | ||
public void ThrowIfEqual_WhenArgumentIsNotEqualToMaximum_ReturnsArgument() | ||
{ | ||
// Arrange | ||
var argument = 2; | ||
var maximum = 1; | ||
|
||
// Act | ||
Argument.ThrowIfEqual(argument, maximum); | ||
|
||
// Assert | ||
Assert.True(true); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/NetEvolve.Arguments.Tests.Unit/ArgumentTests_ThrowIfNotEqual.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace NetEvolve.Arguments.Tests.Unit; | ||
|
||
using System; | ||
using Xunit; | ||
|
||
public sealed partial class ArgumentTests | ||
{ | ||
[Fact] | ||
public void ThrowIfNotEqual_WhenArgumentIsNotEqualToMaximum_ThrowsArgumentOutOfRangeException() | ||
{ | ||
// Arrange | ||
var argument = 2; | ||
var maximum = 1; | ||
|
||
// Act | ||
void Act() => Argument.ThrowIfNotEqual(argument, maximum); | ||
|
||
// Assert | ||
_ = Assert.Throws<ArgumentOutOfRangeException>("argument", Act); | ||
} | ||
|
||
[Fact] | ||
public void ThrowIfNotEqual_WhenArgumentIsEqualToMaximum_ReturnsArgument() | ||
{ | ||
// Arrange | ||
var argument = 1; | ||
var maximum = 1; | ||
|
||
// Act | ||
Argument.ThrowIfNotEqual(argument, maximum); | ||
|
||
// Assert | ||
Assert.True(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters