Skip to content

Commit

Permalink
Add NullOrEmpty and NullOrWhiteSpace overloads for ReadOnlySpan<char> -
Browse files Browse the repository at this point in the history
  • Loading branch information
KonH committed Oct 9, 2022
1 parent 63cd557 commit 6d95f6e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/GuardClauses/GuardAgainstNullExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ public static partial class GuardClauseExtensions
return input;
}

#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> NullOrEmpty(this IGuardClause guardClause,
ReadOnlySpan<char> input,
string parameterName,
string? message = null)
{
if (input == string.Empty)
{
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
}
return input;
}
#endif

/// <summary>
/// Throws an <see cref="ArgumentNullException" /> if <paramref name="input" /> is null.
/// Throws an <see cref="ArgumentException" /> if <paramref name="input" /> is an empty guid.
Expand Down Expand Up @@ -175,6 +198,30 @@ public static partial class GuardClauseExtensions
return input;
}

#if NET5_0_OR_GREATER
/// <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> NullOrWhiteSpace(this IGuardClause guardClause,
ReadOnlySpan<char> input,
string parameterName,
string? message = null)
{
if (input.IsWhiteSpace())
{
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
}

return input;
}
#endif

/// <summary>
/// Throws an <see cref="ArgumentException" /> if <paramref name="input" /> is default for that type.
/// </summary>
Expand Down
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.NullOrEmpty("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.NullOrEmpty("".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.NullOrWhiteSpace(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.NullOrWhiteSpace("".AsSpan(), "emptyStringSpan"));
}

[Theory]
[InlineData(" ")]
[InlineData(" ")]
public void ThrowsGivenWhiteSpaceString(string whiteSpaceString)
{
Assert.Throws<ArgumentException>(() => Guard.Against.NullOrWhiteSpace(whiteSpaceString, "whitespacestring"));
Assert.Throws<ArgumentException>(() => Guard.Against.NullOrWhiteSpace(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.NullOrWhiteSpace(nonEmptyString.AsSpan(), "stringSpan").ToString());
Assert.Equal(expected, Guard.Against.NullOrWhiteSpace(nonEmptyString, "aNumericString"));
}

Expand Down

0 comments on commit 6d95f6e

Please sign in to comment.