Skip to content

Commit 6d41101

Browse files
authored
Add NullOrEmpty and NullOrWhiteSpace overloads for ReadOnlySpan<char> (#237)
1 parent 63cd557 commit 6d41101

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
3+
namespace Ardalis.GuardClauses
4+
{
5+
public static partial class GuardClauseExtensions
6+
{
7+
#if NET5_0_OR_GREATER
8+
/// <summary>
9+
/// Throws an <see cref="ArgumentException" /> if <paramref name="input" /> is an empty string.
10+
/// </summary>
11+
/// <param name="guardClause"></param>
12+
/// <param name="input"></param>
13+
/// <param name="parameterName"></param>
14+
/// <param name="message">Optional. Custom error message</param>
15+
/// <returns><paramref name="input" /> if the value is not an empty string.</returns>
16+
/// <exception cref="ArgumentException"></exception>
17+
public static ReadOnlySpan<char> Empty(this IGuardClause guardClause,
18+
ReadOnlySpan<char> input,
19+
string parameterName,
20+
string? message = null)
21+
{
22+
if (input.Length == 0 || input == string.Empty)
23+
{
24+
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
25+
}
26+
return input;
27+
}
28+
29+
/// <summary>
30+
/// Throws an <see cref="ArgumentException" /> if <paramref name="input" /> is an empty or white space string.
31+
/// </summary>
32+
/// <param name="guardClause"></param>
33+
/// <param name="input"></param>
34+
/// <param name="parameterName"></param>
35+
/// <param name="message">Optional. Custom error message</param>
36+
/// <returns><paramref name="input" /> if the value is not an empty or whitespace string.</returns>
37+
/// <exception cref="ArgumentException"></exception>
38+
public static ReadOnlySpan<char> WhiteSpace(this IGuardClause guardClause,
39+
ReadOnlySpan<char> input,
40+
string parameterName,
41+
string? message = null)
42+
{
43+
if (MemoryExtensions.IsWhiteSpace(input))
44+
{
45+
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
46+
}
47+
48+
return input;
49+
}
50+
#endif
51+
}
52+
}

test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class GuardAgainstNullOrEmpty
1212
public void DoesNothingGivenNonEmptyStringValue()
1313
{
1414
Guard.Against.NullOrEmpty("a", "string");
15+
Guard.Against.Empty("a".AsSpan(), "stringSpan");
1516
Guard.Against.NullOrEmpty("1", "aNumericString");
1617
}
1718

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

45+
[Fact]
46+
public void ThrowsGivenEmptyStringSpan()
47+
{
48+
Assert.Throws<ArgumentException>(() => Guard.Against.Empty("".AsSpan(), "emptyStringSpan"));
49+
}
50+
4451
[Fact]
4552
public void ThrowsGivenNullGuid()
4653
{

test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class GuardAgainstNullOrWhiteSpace
1515
public void DoesNothingGivenNonEmptyStringValue(string nonEmptyString)
1616
{
1717
Guard.Against.NullOrWhiteSpace(nonEmptyString, "string");
18+
Guard.Against.WhiteSpace(nonEmptyString.AsSpan(), "stringSpan");
1819
Guard.Against.NullOrWhiteSpace(nonEmptyString, "aNumericString");
1920
}
2021

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

34+
[Fact]
35+
public void ThrowsGivenEmptyStringSpan()
36+
{
37+
Assert.Throws<ArgumentException>(() => Guard.Against.WhiteSpace("".AsSpan(), "emptyStringSpan"));
38+
}
39+
3340
[Theory]
3441
[InlineData(" ")]
3542
[InlineData(" ")]
3643
public void ThrowsGivenWhiteSpaceString(string whiteSpaceString)
3744
{
3845
Assert.Throws<ArgumentException>(() => Guard.Against.NullOrWhiteSpace(whiteSpaceString, "whitespacestring"));
46+
Assert.Throws<ArgumentException>(() => Guard.Against.WhiteSpace(whiteSpaceString.AsSpan(), "whiteSpaceStringSpan"));
3947
}
4048

4149
[Theory]
@@ -47,6 +55,7 @@ public void ThrowsGivenWhiteSpaceString(string whiteSpaceString)
4755
public void ReturnsExpectedValueGivenNonEmptyStringValue(string nonEmptyString, string expected)
4856
{
4957
Assert.Equal(expected, Guard.Against.NullOrWhiteSpace(nonEmptyString, "string"));
58+
Assert.Equal(expected, Guard.Against.WhiteSpace(nonEmptyString.AsSpan(), "stringSpan").ToString());
5059
Assert.Equal(expected, Guard.Against.NullOrWhiteSpace(nonEmptyString, "aNumericString"));
5160
}
5261

0 commit comments

Comments
 (0)