Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add string length out of range. #333

Merged
merged 3 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading