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

Add NullOrEmpty and NullOrWhiteSpace overloads for ReadOnlySpan<char> #237

Merged
merged 6 commits into from
Feb 15, 2023
Merged
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
52 changes: 52 additions & 0 deletions src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;

namespace Ardalis.GuardClauses
{
public static partial class GuardClauseExtensions
{
#if NET5_0_OR_GREATER
/// <summary>
/// Throws an <see cref="ArgumentException" /> if <paramref name="input" /> is an empty string.
/// </summary>
/// <param name="guardClause"></param>
/// <param name="input"></param>
/// <param name="parameterName"></param>
/// <param name="message">Optional. Custom error message</param>
/// <returns><paramref name="input" /> if the value is not an empty string.</returns>
/// <exception cref="ArgumentException"></exception>
public static ReadOnlySpan<char> Empty(this IGuardClause guardClause,
ReadOnlySpan<char> input,
string parameterName,
string? message = null)
{
if (input.Length == 0 || input == string.Empty)
{
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
}
return input;
}

/// <summary>
/// Throws an <see cref="ArgumentException" /> if <paramref name="input" /> is an empty or white space string.
/// </summary>
/// <param name="guardClause"></param>
/// <param name="input"></param>
/// <param name="parameterName"></param>
/// <param name="message">Optional. Custom error message</param>
/// <returns><paramref name="input" /> if the value is not an empty or whitespace string.</returns>
/// <exception cref="ArgumentException"></exception>
public static ReadOnlySpan<char> WhiteSpace(this IGuardClause guardClause,
ReadOnlySpan<char> input,
string parameterName,
string? message = null)
{
if (MemoryExtensions.IsWhiteSpace(input))
{
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
}

return input;
}
#endif
}
}
7 changes: 7 additions & 0 deletions test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class GuardAgainstNullOrEmpty
public void DoesNothingGivenNonEmptyStringValue()
{
Guard.Against.NullOrEmpty("a", "string");
Guard.Against.Empty("a".AsSpan(), "stringSpan");
Guard.Against.NullOrEmpty("1", "aNumericString");
}

Expand Down Expand Up @@ -41,6 +42,12 @@ public void ThrowsGivenEmptyString()
Assert.Throws<ArgumentException>(() => Guard.Against.NullOrEmpty("", "emptyString"));
}

[Fact]
public void ThrowsGivenEmptyStringSpan()
{
Assert.Throws<ArgumentException>(() => Guard.Against.Empty("".AsSpan(), "emptyStringSpan"));
}

[Fact]
public void ThrowsGivenNullGuid()
{
Expand Down
9 changes: 9 additions & 0 deletions test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class GuardAgainstNullOrWhiteSpace
public void DoesNothingGivenNonEmptyStringValue(string nonEmptyString)
{
Guard.Against.NullOrWhiteSpace(nonEmptyString, "string");
Guard.Against.WhiteSpace(nonEmptyString.AsSpan(), "stringSpan");
Guard.Against.NullOrWhiteSpace(nonEmptyString, "aNumericString");
}

Expand All @@ -30,12 +31,19 @@ public void ThrowsGivenEmptyString()
Assert.Throws<ArgumentException>(() => Guard.Against.NullOrWhiteSpace("", "emptystring"));
}

[Fact]
public void ThrowsGivenEmptyStringSpan()
{
Assert.Throws<ArgumentException>(() => Guard.Against.WhiteSpace("".AsSpan(), "emptyStringSpan"));
}

[Theory]
[InlineData(" ")]
[InlineData(" ")]
public void ThrowsGivenWhiteSpaceString(string whiteSpaceString)
{
Assert.Throws<ArgumentException>(() => Guard.Against.NullOrWhiteSpace(whiteSpaceString, "whitespacestring"));
Assert.Throws<ArgumentException>(() => Guard.Against.WhiteSpace(whiteSpaceString.AsSpan(), "whiteSpaceStringSpan"));
}

[Theory]
Expand All @@ -47,6 +55,7 @@ public void ThrowsGivenWhiteSpaceString(string whiteSpaceString)
public void ReturnsExpectedValueGivenNonEmptyStringValue(string nonEmptyString, string expected)
{
Assert.Equal(expected, Guard.Against.NullOrWhiteSpace(nonEmptyString, "string"));
Assert.Equal(expected, Guard.Against.WhiteSpace(nonEmptyString.AsSpan(), "stringSpan").ToString());
Assert.Equal(expected, Guard.Against.NullOrWhiteSpace(nonEmptyString, "aNumericString"));
}

Expand Down