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

StringTooShort with tests #330

Merged
merged 1 commit into from
Dec 23, 2023
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
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'
{
/// <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);
}
}
Loading