Skip to content

Commit 4753832

Browse files
authored
StringTooShort with tests (#330)
1 parent 85dd232 commit 4753832

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using Ardalis.GuardClauses;
4+
5+
namespace GuardClauses;
6+
public static partial class GuardClauseExtensions
7+
{
8+
/// <summary>
9+
/// Throws an <see cref="ArgumentException" /> if string <paramref name="input"/> is too short.
10+
/// </summary>
11+
/// <param name="guardClause"></param>
12+
/// <param name="input"></param>
13+
/// <param name="minLength"></param>
14+
/// <param name="parameterName"></param>
15+
/// <param name="message">Optional. Custom error message</param>
16+
/// <returns><paramref name="input" /> if the value is not negative.</returns>
17+
/// <exception cref="ArgumentException"></exception>
18+
#if NETFRAMEWORK || NETSTANDARD2_0
19+
public static string StringTooShort(this IGuardClause guardClause,
20+
string input,
21+
int minLength,
22+
string parameterName,
23+
string? message = null)
24+
#else
25+
public static string StringTooShort(this IGuardClause guardClause,
26+
string input,
27+
int minLength,
28+
[CallerArgumentExpression("input")] string? parameterName = null,
29+
string? message = null)
30+
#endif
31+
{
32+
Guard.Against.NegativeOrZero(minLength, nameof(minLength));
33+
if (input.Length < minLength)
34+
{
35+
throw new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too short. Minimum length is {minLength}.", parameterName);
36+
}
37+
return input;
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using Ardalis.GuardClauses;
3+
using Xunit;
4+
5+
namespace GuardClauses.UnitTests;
6+
7+
public class GuardAgainstStringTooShort
8+
{
9+
[Fact]
10+
public void DoesNothingGivenNonEmptyString()
11+
{
12+
Guard.Against.StringTooShort("a", 1, "string");
13+
Guard.Against.StringTooShort("abc", 1, "string");
14+
Guard.Against.StringTooShort("a", 1, "string");
15+
Guard.Against.StringTooShort("a", 1, "string");
16+
Guard.Against.StringTooShort("a", 1, "string");
17+
}
18+
19+
[Fact]
20+
public void ThrowsGivenEmptyString()
21+
{
22+
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooShort("", 1, "string"));
23+
}
24+
25+
[Fact]
26+
public void ThrowsGivenNonPositiveMinLength()
27+
{
28+
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooShort("", 0, "string"));
29+
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooShort("", -1, "string"));
30+
}
31+
32+
[Fact]
33+
public void ThrowsGivenStringShorterThanMinLength()
34+
{
35+
Assert.Throws<ArgumentException>(() => Guard.Against.StringTooShort("a", 2, "string"));
36+
}
37+
38+
[Fact]
39+
public void ReturnsExpectedValueWhenGivenLongerString()
40+
{
41+
var expected = "abc";
42+
var actual = Guard.Against.StringTooShort("abc", 2, "string");
43+
Assert.Equal(expected, actual);
44+
}
45+
}

0 commit comments

Comments
 (0)