Skip to content

Commit

Permalink
Merge pull request #4327 from Sergio0694/bugfix/incorrect-notnullorem…
Browse files Browse the repository at this point in the history
…pty-exception

Fixed exception type for Guard.IsNotNullOr[Empty|WhiteSpace]
  • Loading branch information
michael-hawker authored Oct 18, 2021
2 parents 7113a60 + f4c75d2 commit 44ad0cc
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
6 changes: 4 additions & 2 deletions Microsoft.Toolkit.Diagnostics/Guard.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static void IsNullOrEmpty(string? text, string name)
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="text"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrEmpty([NotNull] string? text, string name)
{
Expand Down Expand Up @@ -89,7 +90,8 @@ public static void IsNullOrWhitespace(string? text, string name)
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="text"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrWhiteSpace([NotNull] string? text, string name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Microsoft.Toolkit.Diagnostics
{
Expand All @@ -27,12 +28,23 @@ public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string n
}

/// <summary>
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotNullOrEmpty"/> fails.
/// Throws an <see cref="ArgumentNullException"/> or <see cref="ArgumentException"/> when <see cref="Guard.IsNotNullOrEmpty"/> fails.
/// </summary>
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or empty, was {(text is null ? "null" : "empty")}", name);
[MethodImpl(MethodImplOptions.NoInlining)]
static Exception GetException(string? text, string name)
{
if (text is null)
{
return new ArgumentNullException(name, $"Parameter {AssertString(name)} (string) must not be null or empty, was null");
}

return new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or empty, was empty", name);
}

throw GetException(text, name);
}

/// <summary>
Expand All @@ -50,7 +62,18 @@ public static void ThrowArgumentExceptionForIsNullOrWhiteSpace(string? text, str
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsNotNullOrWhiteSpace(string? text, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}", name);
[MethodImpl(MethodImplOptions.NoInlining)]
static Exception GetException(string? text, string name)
{
if (text is null)
{
return new ArgumentNullException(name, $"Parameter {AssertString(name)} (string) must not be null or whitespace, was null");
}

return new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or whitespace, was whitespace", name);
}

throw GetException(text, name);
}

/// <summary>
Expand Down
62 changes: 62 additions & 0 deletions UnitTests/UnitTests.Shared/Diagnostics/Test_Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,68 @@ public void Test_Guard_IsAssignableToType_Fail()
Guard.IsAssignableToType(7, typeof(string), nameof(Test_Guard_IsAssignableToType_Fail));
}

[TestCategory("Guard")]
[TestMethod]
public void Test_Guard_IsNullOrEmpty_Ok()
{
Guard.IsNullOrEmpty(null, nameof(Test_Guard_IsNullOrEmpty_Ok));
Guard.IsNullOrEmpty(string.Empty, nameof(Test_Guard_IsNullOrEmpty_Ok));
}

[TestCategory("Guard")]
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_IsNullOrEmpty_Fail()
{
Guard.IsNullOrEmpty("Hello", nameof(Test_Guard_IsNullOrEmpty_Fail));
}

[TestCategory("Guard")]
[TestMethod]
public void Test_Guard_IsNotNullOrEmpty_Ok()
{
Guard.IsNotNullOrEmpty("Hello", nameof(Test_Guard_IsNotNullOrEmpty_Ok));
}

[TestCategory("Guard")]
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Test_Guard_IsNotNullOrEmpty_Null()
{
Guard.IsNotNullOrEmpty(null, nameof(Test_Guard_IsNotNullOrEmpty_Null));
}

[TestCategory("Guard")]
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_IsNotNullOrEmpty_Empty()
{
Guard.IsNotNullOrEmpty(string.Empty, nameof(Test_Guard_IsNotNullOrEmpty_Empty));
}

[TestCategory("Guard")]
[TestMethod]
public void Test_Guard_IsNotNullOrWhiteSpace_Ok()
{
Guard.IsNotNullOrWhiteSpace("Hello", nameof(Test_Guard_IsNotNullOrWhiteSpace_Ok));
}

[TestCategory("Guard")]
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Test_Guard_IsNotNullOrWhiteSpace_Null()
{
Guard.IsNotNullOrWhiteSpace(null, nameof(Test_Guard_IsNotNullOrWhiteSpace_Null));
}

[TestCategory("Guard")]
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_IsNotNullOrWhiteSpace_Empty()
{
Guard.IsNotNullOrWhiteSpace(" ", nameof(Test_Guard_IsNotNullOrWhiteSpace_Empty));
}

[TestCategory("Guard")]
[TestMethod]
public void Test_Guard_IsEqualTo_Ok()
Expand Down

0 comments on commit 44ad0cc

Please sign in to comment.