Skip to content

Commit

Permalink
feat: add string length out of range. (#333)
Browse files Browse the repository at this point in the history
* feat: add string length out of range.

* feat: Add min and max check.

* Refactor LengthOutOfRange.
  • Loading branch information
HamidRezaAshkiyan authored Jan 6, 2024
1 parent 36051ca commit f1411c4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,47 @@
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using GuardClauses;

namespace Ardalis.GuardClauses;

public static partial class GuardClauseExtensions
{
/// <summary>
/// Throws an <see cref="ArgumentException" /> if string <paramref name="input"/> length is out of range.
/// </summary>
/// <param name="guardClause"></param>
/// <param name="input"></param>
/// <param name="minLength"></param>
/// <param name="maxLength"></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 LengthOutOfRange(this IGuardClause guardClause,
string input,
int minLength,
int maxLength,
string parameterName,
string? message = null)
#else
public static string LengthOutOfRange(this IGuardClause guardClause,
string input,
int minLength,
int maxLength,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null)
#endif
{
Guard.Against.Negative<int>(maxLength - minLength, parameterName: "min or max length",
message: "Min length must be equal or less than max length.");
Guard.Against.StringTooShort(input, minLength, nameof(minLength));
Guard.Against.StringTooLong(input, maxLength, nameof(maxLength));

return input;
}

/// <summary>
/// Throws an <see cref="InvalidEnumArgumentException" /> if <paramref name="input"/> is not a valid enum value.
/// </summary>
Expand Down
57 changes: 57 additions & 0 deletions test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using Ardalis.GuardClauses;
using Xunit;

namespace GuardClauses.UnitTests;

public class GuardAgainstStringLengthOutOfRange
{
[Fact]
public void DoesNothingGivenNonEmptyString()
{
Guard.Against.LengthOutOfRange("a", 1, 4, "string");
Guard.Against.LengthOutOfRange("abc", 1, 4, "string");
Guard.Against.LengthOutOfRange("a", 1, 4, "string");
Guard.Against.LengthOutOfRange("a", 1, 4, "string");
Guard.Against.LengthOutOfRange("a", 1, 4, "string");
}

[Fact]
public void ThrowsGivenEmptyString()
{
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("", 1, 2, "string"));
}

[Fact]
public void ThrowsGivenNonPositiveMinLength()
{
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("", 0, 0, "string"));
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("", -1, -1, "string"));
}

[Fact]
public void ThrowsGivenStringShorterThanMinLength()
{
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("a", 2, 2, "string"));
}

[Fact]
public void ThrowsGivenStringLongerThanMaxLength()
{
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("abcd", 2, 2, "string"));
}

[Fact]
public void ThrowsWhenMinIsBiggerThanMax()
{
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("asd", 4, 2, "string"));
}

[Fact]
public void ReturnsExpectedValueWhenGivenLongerString()
{
var expected = "abc";
var actual = Guard.Against.LengthOutOfRange("abc", 2, 5, "string");
Assert.Equal(expected, actual);
}
}

0 comments on commit f1411c4

Please sign in to comment.