Skip to content

Commit 36051ca

Browse files
authored
StringTooLong and tests (#332)
1 parent 4753832 commit 36051ca

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/GuardClauses/GuardAgainstStringLengthExtensions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,36 @@ public static string StringTooShort(this IGuardClause guardClause,
3636
}
3737
return input;
3838
}
39+
40+
/// <summary>
41+
/// Throws an <see cref="ArgumentException" /> if string <paramref name="input"/> is too long.
42+
/// </summary>
43+
/// <param name="guardClause"></param>
44+
/// <param name="input"></param>
45+
/// <param name="maxLength"></param>
46+
/// <param name="parameterName"></param>
47+
/// <param name="message">Optional. Custom error message</param>
48+
/// <returns><paramref name="input" /> if the value is not negative.</returns>
49+
/// <exception cref="ArgumentException"></exception>
50+
#if NETFRAMEWORK || NETSTANDARD2_0
51+
public static string StringTooLong(this IGuardClause guardClause,
52+
string input,
53+
int maxLength,
54+
string parameterName,
55+
string? message = null)
56+
#else
57+
public static string StringTooLong(this IGuardClause guardClause,
58+
string input,
59+
int maxLength,
60+
[CallerArgumentExpression("input")] string? parameterName = null,
61+
string? message = null)
62+
#endif
63+
{
64+
Guard.Against.NegativeOrZero(maxLength, nameof(maxLength));
65+
if (input.Length > maxLength)
66+
{
67+
throw new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too long. Maxmimum length is {maxLength}.", parameterName);
68+
}
69+
return input;
70+
}
3971
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using Ardalis.GuardClauses;
3+
using Xunit;
4+
5+
namespace GuardClauses.UnitTests;
6+
7+
public class GuardAgainstStringTooLong
8+
{
9+
[Fact]
10+
public void DoesNothingGivenNonEmptyString()
11+
{
12+
Guard.Against.StringTooLong("a", 3, "string");
13+
Guard.Against.StringTooLong("abc", 3, "string");
14+
Guard.Against.StringTooLong("a", 3, "string");
15+
Guard.Against.StringTooLong("a", 3, "string");
16+
Guard.Against.StringTooLong("a", 3, "string");
17+
}
18+
19+
[Fact]
20+
public void ThrowsGivenNonPositiveMaxLength()
21+
{
22+
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooLong("a", 0, "string"));
23+
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooLong("a", -1, "string"));
24+
}
25+
26+
[Fact]
27+
public void ThrowsGivenStringLongerThanMaxLength()
28+
{
29+
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooLong("abc", 2, "string"));
30+
}
31+
32+
[Fact]
33+
public void ReturnsExpectedValueWhenGivenValidString()
34+
{
35+
var expected = "abc";
36+
var actual = Guard.Against.StringTooLong("abc", 3, "string");
37+
Assert.Equal(expected, actual);
38+
}
39+
}

0 commit comments

Comments
 (0)