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

Add IsAllLettersOrDigits method for strings #144

Merged
merged 5 commits into from
Jun 7, 2020
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
17 changes: 17 additions & 0 deletions src/projects/EnsureThat/Enforcers/StringArg.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using EnsureThat.Annotations;
using JetBrains.Annotations;
Expand Down Expand Up @@ -270,6 +271,22 @@ 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)
pBouillon marked this conversation as resolved.
Show resolved Hide resolved
{
Ensure.Any.IsNotNull(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;
}

private static bool StringEquals(string x, string y)
=> string.Equals(x, y);

Expand Down
5 changes: 5 additions & 0 deletions src/projects/EnsureThat/EnsureArg.Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,10 @@ 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]
pBouillon marked this conversation as resolved.
Show resolved Hide resolved
[ContractAnnotation("value:null => halt")]
public static string IsAllLettersOrDigits([ValidatedNotNull] string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
=> Ensure.String.IsAllLettersOrDigits(value, paramName, optsFn);
}
}
3 changes: 3 additions & 0 deletions src/projects/EnsureThat/EnsureThatStringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/projects/EnsureThat/ExceptionMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.";
Expand Down
45 changes: 45 additions & 0 deletions src/tests/UnitTests/EnsureStringParamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>(
string.Format(ExceptionMessages.Strings_IsAllLettersOrDigits_Failed, value),
() => Ensure.String.IsAllLettersOrDigits(value, ParamName),
() => EnsureArg.IsAllLettersOrDigits(value, ParamName),
() => Ensure.That(value, ParamName).IsAllLettersOrDigits());
}
}
}