From 369879b01982bdbf6a2773e947a3c3526c12880f Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Fri, 22 Dec 2023 23:12:30 -0500 Subject: [PATCH] StringTooShort with tests --- .../GuardAgainstStringLengthExtensions.cs | 39 ++++++++++++++++ .../GuardAgainstStringTooShort.cs | 45 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/GuardClauses/GuardAgainstStringLengthExtensions.cs create mode 100644 test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs diff --git a/src/GuardClauses/GuardAgainstStringLengthExtensions.cs b/src/GuardClauses/GuardAgainstStringLengthExtensions.cs new file mode 100644 index 0000000..bd8ae84 --- /dev/null +++ b/src/GuardClauses/GuardAgainstStringLengthExtensions.cs @@ -0,0 +1,39 @@ +using System; +using System.Runtime.CompilerServices; +using Ardalis.GuardClauses; + +namespace GuardClauses; +public static partial class GuardClauseExtensions +{ + /// + /// Throws an if string is too short. + /// + /// + /// + /// + /// + /// Optional. Custom error message + /// if the value is not negative. + /// +#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; + } +} diff --git a/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs b/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs new file mode 100644 index 0000000..959e274 --- /dev/null +++ b/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs @@ -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(() => Guard.Against.StringTooShort("", 1, "string")); + } + + [Fact] + public void ThrowsGivenNonPositiveMinLength() + { + Assert.Throws(() => Guard.Against.StringTooShort("", 0, "string")); + Assert.Throws(() => Guard.Against.StringTooShort("", -1, "string")); + } + + [Fact] + public void ThrowsGivenStringShorterThanMinLength() + { + Assert.Throws(() => 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); + } +}