-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
using Ardalis.GuardClauses; | ||
|
||
namespace GuardClauses; | ||
public static partial class GuardClauseExtensions | ||
Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs GitHub Actions / build
Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs GitHub Actions / build
Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs GitHub Actions / build
Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs GitHub Actions / list Ardalis.GuardClauses on nuget.org
Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs GitHub Actions / list Ardalis.GuardClauses on nuget.org
Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs GitHub Actions / list Ardalis.GuardClauses on nuget.org
Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs GitHub Actions / list Ardalis.GuardClauses on nuget.org
Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs GitHub Actions / list Ardalis.GuardClauses on nuget.org
|
||
{ | ||
/// <summary> | ||
/// Throws an <see cref="ArgumentException" /> if string <paramref name="input"/> is too short. | ||
/// </summary> | ||
/// <param name="guardClause"></param> | ||
/// <param name="input"></param> | ||
/// <param name="minLength"></param> | ||
/// <param name="parameterName"></param> | ||
/// <param name="message">Optional. Custom error message</param> | ||
/// <returns><paramref name="input" /> if the value is not negative.</returns> | ||
/// <exception cref="ArgumentException"></exception> | ||
#if NETFRAMEWORK || NETSTANDARD2_0 | ||
public static string StringTooShort(this IGuardClause guardClause, | ||
string input, | ||
int minLength, | ||
string parameterName, | ||
string? message = null) | ||
#else | ||
public static string StringTooShort(this IGuardClause guardClause, | ||
string input, | ||
int minLength, | ||
[CallerArgumentExpression("input")] string? parameterName = null, | ||
string? message = null) | ||
#endif | ||
{ | ||
Guard.Against.NegativeOrZero(minLength, nameof(minLength)); | ||
if (input.Length < minLength) | ||
{ | ||
throw new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too short. Minimum length is {minLength}.", parameterName); | ||
} | ||
return input; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using Ardalis.GuardClauses; | ||
using Xunit; | ||
|
||
namespace GuardClauses.UnitTests; | ||
|
||
public class GuardAgainstStringTooShort | ||
{ | ||
[Fact] | ||
public void DoesNothingGivenNonEmptyString() | ||
{ | ||
Guard.Against.StringTooShort("a", 1, "string"); | ||
Guard.Against.StringTooShort("abc", 1, "string"); | ||
Guard.Against.StringTooShort("a", 1, "string"); | ||
Guard.Against.StringTooShort("a", 1, "string"); | ||
Guard.Against.StringTooShort("a", 1, "string"); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsGivenEmptyString() | ||
{ | ||
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooShort("", 1, "string")); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsGivenNonPositiveMinLength() | ||
{ | ||
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooShort("", 0, "string")); | ||
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooShort("", -1, "string")); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsGivenStringShorterThanMinLength() | ||
{ | ||
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooShort("a", 2, "string")); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsExpectedValueWhenGivenLongerString() | ||
{ | ||
var expected = "abc"; | ||
var actual = Guard.Against.StringTooShort("abc", 2, "string"); | ||
Assert.Equal(expected, actual); | ||
} | ||
} |