Skip to content

Commit

Permalink
StringTooShort with tests (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis authored Dec 23, 2023
1 parent 85dd232 commit 4753832
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/GuardClauses/GuardAgainstStringLengthExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Runtime.CompilerServices;
using Ardalis.GuardClauses;

namespace GuardClauses;
public static partial class GuardClauseExtensions

Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'GuardClauseExtensions'

Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'GuardClauseExtensions'

Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'GuardClauseExtensions'

Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs

View workflow job for this annotation

GitHub Actions / list Ardalis.GuardClauses on nuget.org

Missing XML comment for publicly visible type or member 'GuardClauseExtensions'

Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs

View workflow job for this annotation

GitHub Actions / list Ardalis.GuardClauses on nuget.org

Missing XML comment for publicly visible type or member 'GuardClauseExtensions'

Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs

View workflow job for this annotation

GitHub Actions / list Ardalis.GuardClauses on nuget.org

Missing XML comment for publicly visible type or member 'GuardClauseExtensions'

Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs

View workflow job for this annotation

GitHub Actions / list Ardalis.GuardClauses on nuget.org

Missing XML comment for publicly visible type or member 'GuardClauseExtensions'

Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs

View workflow job for this annotation

GitHub Actions / list Ardalis.GuardClauses on nuget.org

Missing XML comment for publicly visible type or member 'GuardClauseExtensions'

Check warning on line 6 in src/GuardClauses/GuardAgainstStringLengthExtensions.cs

View workflow job for this annotation

GitHub Actions / list Ardalis.GuardClauses on nuget.org

Missing XML comment for publicly visible type or member 'GuardClauseExtensions'
{
/// <summary>
/// Throws an <see cref="ArgumentException" /> if string <paramref name="input"/> is too short.
/// </summary>
/// <param name="guardClause"></param>
/// <param name="input"></param>
/// <param name="minLength"></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 StringTooShort(this IGuardClause guardClause,
string input,
int minLength,
string parameterName,
string? message = null)
#else
public static string StringTooShort(this IGuardClause guardClause,
string input,
int minLength,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null)
#endif
{
Guard.Against.NegativeOrZero(minLength, nameof(minLength));
if (input.Length < minLength)
{
throw new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too short. Minimum length is {minLength}.", parameterName);
}
return input;
}
}
45 changes: 45 additions & 0 deletions test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Ardalis.GuardClauses;
using Xunit;

namespace GuardClauses.UnitTests;

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

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

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

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

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

0 comments on commit 4753832

Please sign in to comment.