From f1411c4ef8354527cd5103e5643029e204b84c25 Mon Sep 17 00:00:00 2001
From: Hamid Reza Ashkiyan
<38791873+HamidRezaAshkiyan@users.noreply.github.com>
Date: Sun, 7 Jan 2024 01:06:37 +0330
Subject: [PATCH] feat: add string length out of range. (#333)
* feat: add string length out of range.
* feat: Add min and max check.
* Refactor LengthOutOfRange.
---
.../GuardAgainstOutOfRangeExtensions.cs | 36 ++++++++++++
.../GuardAgainstStringLengthOutOfRange.cs | 57 +++++++++++++++++++
2 files changed, 93 insertions(+)
create mode 100644 test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.cs
diff --git a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs
index 6621e35..06da4e2 100644
--- a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs
+++ b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs
@@ -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
{
+ ///
+ /// Throws an if string length is out of range.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ /// Optional. Custom error message
+ /// if the value is not negative.
+ ///
+#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(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;
+ }
+
///
/// Throws an if is not a valid enum value.
///
diff --git a/test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.cs b/test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.cs
new file mode 100644
index 0000000..3dceae0
--- /dev/null
+++ b/test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.cs
@@ -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(() => Guard.Against.LengthOutOfRange("", 1, 2, "string"));
+ }
+
+ [Fact]
+ public void ThrowsGivenNonPositiveMinLength()
+ {
+ Assert.Throws(() => Guard.Against.LengthOutOfRange("", 0, 0, "string"));
+ Assert.Throws(() => Guard.Against.LengthOutOfRange("", -1, -1, "string"));
+ }
+
+ [Fact]
+ public void ThrowsGivenStringShorterThanMinLength()
+ {
+ Assert.Throws(() => Guard.Against.LengthOutOfRange("a", 2, 2, "string"));
+ }
+
+ [Fact]
+ public void ThrowsGivenStringLongerThanMaxLength()
+ {
+ Assert.Throws(() => Guard.Against.LengthOutOfRange("abcd", 2, 2, "string"));
+ }
+
+ [Fact]
+ public void ThrowsWhenMinIsBiggerThanMax()
+ {
+ Assert.Throws(() => 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);
+ }
+}