Skip to content

Commit

Permalink
Add ThrowIfArgumentIsEmpty for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Oct 27, 2022
1 parent 691fba6 commit b36399a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 73 deletions.
12 changes: 2 additions & 10 deletions Src/FluentAssertions/Collections/StringCollectionAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,7 @@ public AndConstraint<TAssertions> BeEquivalentTo(IEnumerable<string> expectation
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match strings in collection against <null>. Provide a wildcard pattern or use the Contain method.");

if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match strings in collection against an empty string. Provide a wildcard pattern or use the Contain method.", nameof(wildcardPattern));
}
Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match strings in collection against an empty string. Provide a wildcard pattern or use the Contain method.");

bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down Expand Up @@ -322,11 +318,7 @@ private IEnumerable<string> AllThatMatch(string wildcardPattern)
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match strings in collection against <null>. Provide a wildcard pattern or use the NotContain method.");

if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match strings in collection against an empty string. Provide a wildcard pattern or use the NotContain method.", nameof(wildcardPattern));
}
Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match strings in collection against an empty string. Provide a wildcard pattern or use the NotContain method.");

bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down
8 changes: 8 additions & 0 deletions Src/FluentAssertions/Common/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public static void ThrowIfArgumentIsEmpty<T>(IEnumerable<T> values, string param
}
}

public static void ThrowIfArgumentIsEmpty(string str, string paramName, string message)
{
if (str.Length == 0)
{
throw new ArgumentException(message, paramName);
}
}

/// <summary>
/// Workaround to make dotnet_code_quality.null_check_validation_methods work
/// https://github.com/dotnet/roslyn-analyzers/issues/3451#issuecomment-606690452
Expand Down
82 changes: 19 additions & 63 deletions Src/FluentAssertions/Primitives/StringAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,7 @@ public AndConstraint<TAssertions> NotBe(string unexpected, string because = "",
public AndConstraint<TAssertions> Match(string wildcardPattern, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against <null>. Provide a wildcard pattern or use the BeNull method.");

if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method.", nameof(wildcardPattern));
}
Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method.");

var stringWildcardMatchingValidator = new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs);
stringWildcardMatchingValidator.Validate();
Expand Down Expand Up @@ -260,11 +256,7 @@ public AndConstraint<TAssertions> Match(string wildcardPattern, string because =
public AndConstraint<TAssertions> NotMatch(string wildcardPattern, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against <null>. Provide a wildcard pattern or use the NotBeNull method.");

if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method.", nameof(wildcardPattern));
}
Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method.");

new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs)
{
Expand Down Expand Up @@ -313,11 +305,7 @@ public AndConstraint<TAssertions> NotMatch(string wildcardPattern, string becaus
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against <null>. Provide a wildcard pattern or use the BeNull method.");

if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method.", nameof(wildcardPattern));
}
Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method.");

var validator = new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs)
{
Expand Down Expand Up @@ -369,11 +357,7 @@ public AndConstraint<TAssertions> NotMatch(string wildcardPattern, string becaus
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against <null>. Provide a wildcard pattern or use the NotBeNull method.");

if (wildcardPattern.Length == 0)
{
throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method.", nameof(wildcardPattern));
}
Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method.");

var validator = new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs)
{
Expand Down Expand Up @@ -482,19 +466,15 @@ public AndConstraint<TAssertions> NotMatch(string wildcardPattern, string becaus
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="regularExpression"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="regularExpression"/> is empty.</exception>
public AndConstraint<TAssertions> MatchRegex(Regex regularExpression,
OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression),
"Cannot match string against <null>. Provide a regex pattern or use the BeNull method.");

var regexStr = regularExpression.ToString();
if (regexStr.Length == 0)
{
throw new ArgumentException(
"Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method.",
nameof(regularExpression));
}
Guard.ThrowIfArgumentIsEmpty(regexStr, nameof(regularExpression), "Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method.");

bool success = Execute.Assertion
.ForCondition(Subject is not null)
Expand Down Expand Up @@ -532,19 +512,15 @@ public AndConstraint<TAssertions> NotMatch(string wildcardPattern, string becaus
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="regularExpression"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="regularExpression"/> is empty.</exception>
public AndConstraint<TAssertions> MatchRegex(Regex regularExpression,
string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression),
"Cannot match string against <null>. Provide a regex pattern or use the BeNull method.");

var regexStr = regularExpression.ToString();
if (regexStr.Length == 0)
{
throw new ArgumentException(
"Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method.",
nameof(regularExpression));
}
Guard.ThrowIfArgumentIsEmpty(regexStr, nameof(regularExpression), "Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method.");

bool success = Execute.Assertion
.ForCondition(Subject is not null)
Expand Down Expand Up @@ -612,18 +588,14 @@ public AndConstraint<TAssertions> NotMatch(string wildcardPattern, string becaus
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="regularExpression"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="regularExpression"/> is empty.</exception>
public AndConstraint<TAssertions> NotMatchRegex(Regex regularExpression, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression),
"Cannot match string against <null>. Provide a regex pattern or use the NotBeNull method.");

var regexStr = regularExpression.ToString();
if (regexStr.Length == 0)
{
throw new ArgumentException(
"Cannot match string against an empty regex pattern. Provide a regex pattern or use the NotBeEmpty method.",
nameof(regularExpression));
}
Guard.ThrowIfArgumentIsEmpty(regexStr, nameof(regularExpression), "Cannot match string against an empty regex pattern. Provide a regex pattern or use the NotBeEmpty method.");

bool success = Execute.Assertion
.ForCondition(Subject is not null)
Expand Down Expand Up @@ -907,14 +879,11 @@ public AndConstraint<TAssertions> NotEndWithEquivalentOf(string unexpected, stri
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expected"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="expected"/> is empty.</exception>
public AndConstraint<TAssertions> Contain(string expected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against <null>.");

if (expected.Length == 0)
{
throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected));
}
Guard.ThrowIfArgumentIsEmpty(expected, nameof(expected), "Cannot assert string containment against an empty string.");

Execute.Assertion
.ForCondition(Contains(Subject, expected, StringComparison.Ordinal))
Expand Down Expand Up @@ -944,14 +913,11 @@ public AndConstraint<TAssertions> Contain(string expected, string because = "",
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expected"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="expected"/> is empty.</exception>
public AndConstraint<TAssertions> Contain(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against <null>.");

if (expected.Length == 0)
{
throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected));
}
Guard.ThrowIfArgumentIsEmpty(expected, nameof(expected), "Cannot assert string containment against an empty string.");

int actual = Subject.CountSubstring(expected, StringComparison.Ordinal);

Expand All @@ -978,14 +944,11 @@ public AndConstraint<TAssertions> Contain(string expected, OccurrenceConstraint
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expected"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="expected"/> is empty.</exception>
public AndConstraint<TAssertions> ContainEquivalentOf(string expected, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against <null>.");

if (expected.Length == 0)
{
throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected));
}
Guard.ThrowIfArgumentIsEmpty(expected, nameof(expected), "Cannot assert string containment against an empty string.");

Execute.Assertion
.ForCondition(Contains(Subject, expected, StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -1016,14 +979,11 @@ public AndConstraint<TAssertions> ContainEquivalentOf(string expected, string be
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expected"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="expected"/> is empty.</exception>
public AndConstraint<TAssertions> ContainEquivalentOf(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against <null>.");

if (expected.Length == 0)
{
throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected));
}
Guard.ThrowIfArgumentIsEmpty(expected, nameof(expected), "Cannot assert string containment against an empty string.");

int actual = Subject.CountSubstring(expected, StringComparison.OrdinalIgnoreCase);

Expand Down Expand Up @@ -1128,11 +1088,7 @@ public AndConstraint<TAssertions> ContainAny(params string[] values)
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot assert string containment against <null>.");

if (unexpected.Length == 0)
{
throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(unexpected));
}
Guard.ThrowIfArgumentIsEmpty(unexpected, nameof(unexpected), "Cannot assert string containment against an empty string.");

Execute.Assertion
.ForCondition(!Contains(Subject, unexpected, StringComparison.Ordinal))
Expand Down

0 comments on commit b36399a

Please sign in to comment.