From 5af1840e0e0509ae329c1d1931b2c86370000a9f Mon Sep 17 00:00:00 2001 From: Pierre Bouillon Date: Sat, 6 Jun 2020 12:59:56 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9C=A8=20Add=20IsAllLettersOrDigits=20st?= =?UTF-8?q?ring=20check=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/projects/EnsureThat/Enforcers/StringArg.cs | 14 ++++++++++++++ src/projects/EnsureThat/ExceptionMessages.cs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/projects/EnsureThat/Enforcers/StringArg.cs b/src/projects/EnsureThat/Enforcers/StringArg.cs index bab34ceb..b8af6fc7 100644 --- a/src/projects/EnsureThat/Enforcers/StringArg.cs +++ b/src/projects/EnsureThat/Enforcers/StringArg.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Text.RegularExpressions; using EnsureThat.Annotations; using JetBrains.Annotations; @@ -270,6 +271,19 @@ public string IsInRange(string value, string min, string max, StringComparison c return value; } + public string IsAllLettersOrDigits([ValidatedNotNull] string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null) + { + Ensure.Any.IsNotNull(value, paramName, optsFn); + + if (!value.ToCharArray().All(char.IsLetterOrDigit)) + throw Ensure.ExceptionFactory.ArgumentException( + string.Format(ExceptionMessages.Strings_IsAllLettersOrDigits_Failed, value), + paramName, + optsFn); + + return value; + } + private static bool StringEquals(string x, string y) => string.Equals(x, y); diff --git a/src/projects/EnsureThat/ExceptionMessages.cs b/src/projects/EnsureThat/ExceptionMessages.cs index 254034b3..7caf80cd 100644 --- a/src/projects/EnsureThat/ExceptionMessages.cs +++ b/src/projects/EnsureThat/ExceptionMessages.cs @@ -37,7 +37,7 @@ public static class ExceptionMessages public static string Strings_IsNotEmpty_Failed { get; } = "Empty String is not allowed."; public static string Strings_IsGuid_Failed { get; } = "Value '{0}' is not a valid GUID."; public static string Strings_StartsWith_Failed { get; } = "Value '{0}' is expected to start with '{1}' but does not."; - + public static string Strings_IsAllLettersOrDigits_Failed { get; } = "Expected '{0} to contain only letters or digits but does not."; public static string Types_IsOfType_Failed { get; } = "The param is not of expected type. Expected: '{0}'. Got: '{1}'."; public static string Types_IsNotOfType_Failed { get; } = "The param was expected to not be of the type: '{0}'. But it was."; public static string Types_IsAssignableToType_Failed { get; } = "The param is not assignable to the expected type. Expected: '{0}'. Got: '{1}'."; From 7973a4e86ac5d38c39fefe818e9a167560c472ae Mon Sep 17 00:00:00 2001 From: Pierre Bouillon Date: Sat, 6 Jun 2020 13:00:23 +0200 Subject: [PATCH 2/5] =?UTF-8?q?=E2=9C=A8=20Add=20IsAllLettersOrDigits=20st?= =?UTF-8?q?ring=20extensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/projects/EnsureThat/EnsureArg.Strings.cs | 4 ++++ src/projects/EnsureThat/EnsureThatStringExtensions.cs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/projects/EnsureThat/EnsureArg.Strings.cs b/src/projects/EnsureThat/EnsureArg.Strings.cs index 2e7202e9..3f16fa1f 100644 --- a/src/projects/EnsureThat/EnsureArg.Strings.cs +++ b/src/projects/EnsureThat/EnsureArg.Strings.cs @@ -84,5 +84,9 @@ public static string StartsWith([ValidatedNotNull] string value, [NotNull] strin [ContractAnnotation("value:null => halt")] public static string StartsWith([ValidatedNotNull] string value, [NotNull] string expectedStartsWith, StringComparison comparisonType, [InvokerParameterName] string paramName = null, OptsFn optsFn = null) => Ensure.String.StartsWith(value, expectedStartsWith, comparisonType, paramName, optsFn); + + [NotNull] + public static string IsAllLettersOrDigits([ValidatedNotNull] string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null) + => Ensure.String.IsAllLettersOrDigits(value, paramName, optsFn); } } \ No newline at end of file diff --git a/src/projects/EnsureThat/EnsureThatStringExtensions.cs b/src/projects/EnsureThat/EnsureThatStringExtensions.cs index 16491c84..2debfdd4 100644 --- a/src/projects/EnsureThat/EnsureThatStringExtensions.cs +++ b/src/projects/EnsureThat/EnsureThatStringExtensions.cs @@ -80,5 +80,8 @@ public static void IsGte(this in StringParam param, string limit, StringComparis public static void IsInRange(this in StringParam param, string min, string max, StringComparison comparison) => Ensure.String.IsInRange(param.Value, min, max, comparison, param.Name, param.OptsFn); + + public static void IsAllLettersOrDigits(this in StringParam param) + => Ensure.String.IsAllLettersOrDigits(param.Value, param.Name, param.OptsFn); } } \ No newline at end of file From 16b7aa100428b2aaa13e3ee1643c6a231b3dd41d Mon Sep 17 00:00:00 2001 From: Pierre Bouillon Date: Sat, 6 Jun 2020 13:00:55 +0200 Subject: [PATCH 3/5] =?UTF-8?q?=E2=9C=85=20Add=20IsAlLettersOrDigits=20uni?= =?UTF-8?q?t=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tests/UnitTests/EnsureStringParamTests.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/tests/UnitTests/EnsureStringParamTests.cs b/src/tests/UnitTests/EnsureStringParamTests.cs index 0800f8b0..ce0fa219 100644 --- a/src/tests/UnitTests/EnsureStringParamTests.cs +++ b/src/tests/UnitTests/EnsureStringParamTests.cs @@ -499,5 +499,50 @@ public void StartsWith_When_DoesNotStartWith_It_should_throw() () => EnsureArg.StartsWith(value, startPart, ParamName), () => Ensure.That(value, ParamName).StartsWith(startPart)); } + + [Fact] + public void IsAllLettersOrDigits_WhenStringIsAllLettersAndDigits_It_should_not_throw() + { + const string value = "aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789"; + + ShouldNotThrow( + () => Ensure.String.IsAllLettersOrDigits(value, ParamName), + () => EnsureArg.IsAllLettersOrDigits(value, ParamName), + () => Ensure.That(value, ParamName).IsAllLettersOrDigits()); + } + + [Fact] + public void IsAllLettersOrDigits_WhenStringIsAllDigits_It_should_not_throw() + { + const string value = "0123456789"; + + ShouldNotThrow( + () => Ensure.String.IsAllLettersOrDigits(value, ParamName), + () => EnsureArg.IsAllLettersOrDigits(value, ParamName), + () => Ensure.That(value, ParamName).IsAllLettersOrDigits()); + } + + [Fact] + public void IsAllLettersOrDigits_WhenStringIsAllLetters_It_should_not_throw() + { + const string value = "aBcDeFgHiJkLmNoPqRsTuVwXyZ"; + + ShouldNotThrow( + () => Ensure.String.IsAllLettersOrDigits(value, ParamName), + () => EnsureArg.IsAllLettersOrDigits(value, ParamName), + () => Ensure.That(value, ParamName).IsAllLettersOrDigits()); + } + + [Fact] + public void IsAllLettersOrDigits_WhenStringDoesNotHaveLettersOrDigits_It_should_throw() + { + const string value = "<:)-+-<"; + + ShouldThrow( + string.Format(ExceptionMessages.Strings_IsAllLettersOrDigits_Failed, value), + () => Ensure.String.IsAllLettersOrDigits(value, ParamName), + () => EnsureArg.IsAllLettersOrDigits(value, ParamName), + () => Ensure.That(value, ParamName).IsAllLettersOrDigits()); + } } } \ No newline at end of file From 6d6284a6b54470be62c62df9f7aab2f9d8cb8235 Mon Sep 17 00:00:00 2001 From: Pierre Bouillon Date: Sat, 6 Jun 2020 21:52:15 +0200 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=8E=AF=20Add=20missing=20annotations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/projects/EnsureThat/Enforcers/StringArg.cs | 2 ++ src/projects/EnsureThat/EnsureArg.Strings.cs | 1 + 2 files changed, 3 insertions(+) diff --git a/src/projects/EnsureThat/Enforcers/StringArg.cs b/src/projects/EnsureThat/Enforcers/StringArg.cs index b8af6fc7..4e51a02c 100644 --- a/src/projects/EnsureThat/Enforcers/StringArg.cs +++ b/src/projects/EnsureThat/Enforcers/StringArg.cs @@ -271,6 +271,8 @@ public string IsInRange(string value, string min, string max, StringComparison c return value; } + [NotNull] + [ContractAnnotation("value:null => halt")] public string IsAllLettersOrDigits([ValidatedNotNull] string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null) { Ensure.Any.IsNotNull(value, paramName, optsFn); diff --git a/src/projects/EnsureThat/EnsureArg.Strings.cs b/src/projects/EnsureThat/EnsureArg.Strings.cs index 3f16fa1f..9cfcc91b 100644 --- a/src/projects/EnsureThat/EnsureArg.Strings.cs +++ b/src/projects/EnsureThat/EnsureArg.Strings.cs @@ -86,6 +86,7 @@ public static string StartsWith([ValidatedNotNull] string value, [NotNull] strin => Ensure.String.StartsWith(value, expectedStartsWith, comparisonType, paramName, optsFn); [NotNull] + [ContractAnnotation("value:null => halt")] public static string IsAllLettersOrDigits([ValidatedNotNull] string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null) => Ensure.String.IsAllLettersOrDigits(value, paramName, optsFn); } From cc60afd706a30ab0bb4f04191f4a2be42c05d9b9 Mon Sep 17 00:00:00 2001 From: Pierre Bouillon Date: Sun, 7 Jun 2020 10:46:55 +0200 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=8E=AF=20Replace=20string=20parsing?= =?UTF-8?q?=20against=20predicate=20with=20the=20iterative=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/projects/EnsureThat/Enforcers/StringArg.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/projects/EnsureThat/Enforcers/StringArg.cs b/src/projects/EnsureThat/Enforcers/StringArg.cs index 4e51a02c..e507c6c3 100644 --- a/src/projects/EnsureThat/Enforcers/StringArg.cs +++ b/src/projects/EnsureThat/Enforcers/StringArg.cs @@ -277,11 +277,12 @@ public string IsAllLettersOrDigits([ValidatedNotNull] string value, [InvokerPara { Ensure.Any.IsNotNull(value, paramName, optsFn); - if (!value.ToCharArray().All(char.IsLetterOrDigit)) - throw Ensure.ExceptionFactory.ArgumentException( - string.Format(ExceptionMessages.Strings_IsAllLettersOrDigits_Failed, value), - paramName, - optsFn); + for (var i = 0; i < value.Length; i++) + if (!char.IsLetterOrDigit(value[i])) + throw Ensure.ExceptionFactory.ArgumentException( + string.Format(ExceptionMessages.Strings_IsAllLettersOrDigits_Failed, value), + paramName, + optsFn); return value; }