Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Guard IsWhiteSpace and IsNotWhiteSpace Methods #650

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 10 additions & 10 deletions src/CommunityToolkit.Diagnostics/Guard.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public static void IsNotNullOrEmpty([NotNull] string? text, [CallerArgumentExpre
}

/// <summary>
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/> or whitespace.
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/>, empty or consists only of white-space characters.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/>, nor empty, nor whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNullOrWhiteSpace(string? text, [CallerArgumentExpression(nameof(text))] string name = "")
{
Expand All @@ -66,12 +66,12 @@ public static void IsNullOrWhiteSpace(string? text, [CallerArgumentExpression(na
}

/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/>, empty or consists only of white-space characters.
/// </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="ArgumentNullException">Thrown if <paramref name="text"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is whitespace.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty or whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrWhiteSpace([NotNull] string? text, [CallerArgumentExpression(nameof(text))] string name = "")
{
Expand Down Expand Up @@ -118,15 +118,15 @@ public static void IsNotEmpty(string text, [CallerArgumentExpression(nameof(text
}

/// <summary>
/// Asserts that the input <see cref="string"/> instance must be whitespace.
/// Asserts that the input <see cref="string"/> instance must consists only of white-space characters.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> has some not white-space characters.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsWhiteSpace(string text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (string.IsNullOrWhiteSpace(text))
if (!string.IsNullOrEmpty(text) && string.IsNullOrWhiteSpace(text))
{
return;
}
Expand All @@ -135,15 +135,15 @@ public static void IsWhiteSpace(string text, [CallerArgumentExpression(nameof(te
}

/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
/// Asserts that the input <see cref="string"/> instance must not consists only of white-space characters.
/// </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="ArgumentException">Thrown if <paramref name="text"/> has only white-space characters.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotWhiteSpace(string text, [CallerArgumentExpression(nameof(text))] string name = "")
{
if (!string.IsNullOrWhiteSpace(text))
if (string.IsNullOrEmpty(text) || !string.IsNullOrWhiteSpace(text))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(string name)
/// Throws an <see cref="ArgumentException"/> when <see cref="IsWhiteSpace"/> fails.
/// </summary>
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsWhiteSpace(string text, string name)
public static void ThrowArgumentExceptionForIsWhiteSpace(string? text, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} (string) must be whitespace, was {AssertString(text)}.", name);
}
Expand All @@ -103,7 +103,7 @@ public static void ThrowArgumentExceptionForIsWhiteSpace(string text, string nam
/// Throws an <see cref="ArgumentException"/> when <see cref="IsNotWhiteSpace"/> fails.
/// </summary>
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsNotWhiteSpace(string text, string name)
public static void ThrowArgumentExceptionForIsNotWhiteSpace(string? text, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be whitespace, was {AssertString(text)}.", name);
}
Expand Down
44 changes: 44 additions & 0 deletions tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,50 @@ public void Test_Guard_IsNotNullOrWhiteSpace_Empty()
{
Guard.IsNotNullOrWhiteSpace(" ", nameof(Test_Guard_IsNotNullOrWhiteSpace_Empty));
}

[TestMethod]
public void Test_Guard_IsWhiteSpace_Ok()
{
Guard.IsWhiteSpace(" ", nameof(Test_Guard_IsWhiteSpace_Ok));
Guard.IsWhiteSpace("\t", nameof(Test_Guard_IsWhiteSpace_Ok));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_IsWhiteSpace_Null()
{
Guard.IsWhiteSpace(null, nameof(Test_Guard_IsWhiteSpace_Null));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_IsWhiteSpace_Empty()
{
Guard.IsWhiteSpace(string.Empty, nameof(Test_Guard_IsWhiteSpace_Empty));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_IsWhiteSpace_NotWhiteSpace()
{
Guard.IsWhiteSpace("foo", nameof(Test_Guard_IsWhiteSpace_NotWhiteSpace));
}

[TestMethod]
public void Test_Guard_IsNotWhiteSpace_Ok()
{
Guard.IsNotWhiteSpace(null, nameof(Test_Guard_IsNotWhiteSpace_Ok));
Guard.IsNotWhiteSpace("foo", nameof(Test_Guard_IsNotWhiteSpace_Ok));
Guard.IsNotWhiteSpace(string.Empty, nameof(Test_Guard_IsNotWhiteSpace_Ok));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_Guard_IsNotWhiteSpace_WhiteSpace()
{
Guard.IsNotWhiteSpace(" ", nameof(Test_Guard_IsNotWhiteSpace_WhiteSpace));
Guard.IsNotWhiteSpace("\t", nameof(Test_Guard_IsNotWhiteSpace_WhiteSpace));
}

[TestMethod]
public void Test_Guard_IsEqualTo_Ok()
Expand Down