Skip to content

Commit f1411c4

Browse files
feat: add string length out of range. (#333)
* feat: add string length out of range. * feat: Add min and max check. * Refactor LengthOutOfRange.
1 parent 36051ca commit f1411c4

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,47 @@
44
using System.Linq;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Runtime.CompilerServices;
7+
using GuardClauses;
78

89
namespace Ardalis.GuardClauses;
910

1011
public static partial class GuardClauseExtensions
1112
{
13+
/// <summary>
14+
/// Throws an <see cref="ArgumentException" /> if string <paramref name="input"/> length is out of range.
15+
/// </summary>
16+
/// <param name="guardClause"></param>
17+
/// <param name="input"></param>
18+
/// <param name="minLength"></param>
19+
/// <param name="maxLength"></param>
20+
/// <param name="parameterName"></param>
21+
/// <param name="message">Optional. Custom error message</param>
22+
/// <returns><paramref name="input" /> if the value is not negative.</returns>
23+
/// <exception cref="ArgumentException"></exception>
24+
#if NETFRAMEWORK || NETSTANDARD2_0
25+
public static string LengthOutOfRange(this IGuardClause guardClause,
26+
string input,
27+
int minLength,
28+
int maxLength,
29+
string parameterName,
30+
string? message = null)
31+
#else
32+
public static string LengthOutOfRange(this IGuardClause guardClause,
33+
string input,
34+
int minLength,
35+
int maxLength,
36+
[CallerArgumentExpression("input")] string? parameterName = null,
37+
string? message = null)
38+
#endif
39+
{
40+
Guard.Against.Negative<int>(maxLength - minLength, parameterName: "min or max length",
41+
message: "Min length must be equal or less than max length.");
42+
Guard.Against.StringTooShort(input, minLength, nameof(minLength));
43+
Guard.Against.StringTooLong(input, maxLength, nameof(maxLength));
44+
45+
return input;
46+
}
47+
1248
/// <summary>
1349
/// Throws an <see cref="InvalidEnumArgumentException" /> if <paramref name="input"/> is not a valid enum value.
1450
/// </summary>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using Ardalis.GuardClauses;
3+
using Xunit;
4+
5+
namespace GuardClauses.UnitTests;
6+
7+
public class GuardAgainstStringLengthOutOfRange
8+
{
9+
[Fact]
10+
public void DoesNothingGivenNonEmptyString()
11+
{
12+
Guard.Against.LengthOutOfRange("a", 1, 4, "string");
13+
Guard.Against.LengthOutOfRange("abc", 1, 4, "string");
14+
Guard.Against.LengthOutOfRange("a", 1, 4, "string");
15+
Guard.Against.LengthOutOfRange("a", 1, 4, "string");
16+
Guard.Against.LengthOutOfRange("a", 1, 4, "string");
17+
}
18+
19+
[Fact]
20+
public void ThrowsGivenEmptyString()
21+
{
22+
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("", 1, 2, "string"));
23+
}
24+
25+
[Fact]
26+
public void ThrowsGivenNonPositiveMinLength()
27+
{
28+
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("", 0, 0, "string"));
29+
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("", -1, -1, "string"));
30+
}
31+
32+
[Fact]
33+
public void ThrowsGivenStringShorterThanMinLength()
34+
{
35+
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("a", 2, 2, "string"));
36+
}
37+
38+
[Fact]
39+
public void ThrowsGivenStringLongerThanMaxLength()
40+
{
41+
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("abcd", 2, 2, "string"));
42+
}
43+
44+
[Fact]
45+
public void ThrowsWhenMinIsBiggerThanMax()
46+
{
47+
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("asd", 4, 2, "string"));
48+
}
49+
50+
[Fact]
51+
public void ReturnsExpectedValueWhenGivenLongerString()
52+
{
53+
var expected = "abc";
54+
var actual = Guard.Against.LengthOutOfRange("abc", 2, 5, "string");
55+
Assert.Equal(expected, actual);
56+
}
57+
}

0 commit comments

Comments
 (0)