Skip to content

Commit

Permalink
Apply string-specific options to both, the subject and expected value.
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Nov 4, 2023
1 parent 9b87ce6 commit b9e138c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
Expand Up @@ -744,36 +744,6 @@ internal IEqualityComparer<string> GetStringComparerOrDefault()
return stringComparer ?? (ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
}

/// <summary>
/// Applies the string-specific options to the <paramref name="value"/>.
/// </summary>
/// <remarks>
/// When <see cref="IgnoringLeadingWhitespace()"/> whitespace is removed from the start of the <paramref name="value"/>.<br />
/// When <see cref="IgnoringTrailingWhitespace()"/> whitespace is removed from the end of the <paramref name="value"/>.<br />
/// When <see cref="IgnoringNewlines()"/> all newlines ("\r" and "\n") are removed from the <paramref name="value"/>.
/// </remarks>
internal string ApplyStringSettings(string value)
{
if (ignoreLeadingWhitespace)
{
value = value.TrimStart();
}

if (ignoreTrailingWhitespace)
{
value = value.TrimEnd();
}

if (ignoreNewlines)
{
value = value
.Replace("\r", string.Empty, StringComparison.Ordinal)
.Replace("\n", string.Empty, StringComparison.Ordinal);
}

return value;
}

/// <summary>
/// Returns a string that represents the current object.
/// </summary>
Expand Down
61 changes: 49 additions & 12 deletions Src/FluentAssertions/Primitives/StringAssertions.cs
Expand Up @@ -89,7 +89,8 @@ public AndConstraint<TAssertions> Be(string expected, string because = "", param
new StringEqualityStrategy(options.GetStringComparerOrDefault()),
because, becauseArgs);

var subject = options.ApplyStringSettings(Subject);
var subject = ApplyStringSettings(Subject, options);
expected = ApplyStringSettings(expected, options);

expectation.Validate(subject, expected);
return new AndConstraint<TAssertions>((TAssertions)this);
Expand Down Expand Up @@ -183,7 +184,8 @@ public AndConstraint<TAssertions> BeOneOf(IEnumerable<string> validValues, strin
new StringEqualityStrategy(options.GetStringComparerOrDefault()),
because, becauseArgs);

var subject = options.ApplyStringSettings(Subject);
var subject = ApplyStringSettings(Subject, options);
expected = ApplyStringSettings(expected, options);

expectation.Validate(subject, expected);
return new AndConstraint<TAssertions>((TAssertions)this);
Expand Down Expand Up @@ -542,9 +544,10 @@ public AndConstraint<TAssertions> NotMatch(string wildcardPattern, string becaus
},
because, becauseArgs);

var subject = options.ApplyStringSettings(Subject);
stringWildcardMatchingValidator.Validate(subject, wildcardPattern);
var subject = ApplyStringSettings(Subject, options);
wildcardPattern = ApplyStringSettings(wildcardPattern, options);

stringWildcardMatchingValidator.Validate(subject, wildcardPattern);
return new AndConstraint<TAssertions>((TAssertions)this);
}

Expand Down Expand Up @@ -667,9 +670,10 @@ public AndConstraint<TAssertions> NotMatch(string wildcardPattern, string becaus
},
because, becauseArgs);

var subject = options.ApplyStringSettings(Subject);
stringWildcardMatchingValidator.Validate(subject, wildcardPattern);
var subject = ApplyStringSettings(Subject, options);
wildcardPattern = ApplyStringSettings(wildcardPattern, options);

stringWildcardMatchingValidator.Validate(subject, wildcardPattern);
return new AndConstraint<TAssertions>((TAssertions)this);
}

Expand Down Expand Up @@ -1046,9 +1050,10 @@ public AndConstraint<TAssertions> NotStartWith(string unexpected, string because
new StringStartStrategy(options.GetStringComparerOrDefault()),
because, becauseArgs);

var subject = options.ApplyStringSettings(Subject);
stringStartValidator.Validate(subject, expected);
var subject = ApplyStringSettings(Subject, options);
expected = ApplyStringSettings(expected, options);

stringStartValidator.Validate(subject, expected);
return new AndConstraint<TAssertions>((TAssertions)this);
}

Expand Down Expand Up @@ -1238,9 +1243,10 @@ public AndConstraint<TAssertions> NotEndWith(string unexpected, string because =
new StringEndStrategy(options.GetStringComparerOrDefault()),
because, becauseArgs);

var subject = options.ApplyStringSettings(Subject);
stringEndValidator.Validate(subject, expected);
var subject = ApplyStringSettings(Subject, options);
expected = ApplyStringSettings(expected, options);

stringEndValidator.Validate(subject, expected);
return new AndConstraint<TAssertions>((TAssertions)this);
}

Expand Down Expand Up @@ -1473,9 +1479,10 @@ public AndConstraint<TAssertions> Contain(string expected, string because = "",
new StringContainsStrategy(options.GetStringComparerOrDefault(), occurrenceConstraint),
because, becauseArgs);

var subject = options.ApplyStringSettings(Subject);
stringContainValidator.Validate(subject, expected);
var subject = ApplyStringSettings(Subject, options);
expected = ApplyStringSettings(expected, options);

stringContainValidator.Validate(subject, expected);
return new AndConstraint<TAssertions>((TAssertions)this);
}

Expand Down Expand Up @@ -2082,6 +2089,36 @@ private static void ThrowIfValuesNullOrEmpty(IEnumerable<string> values)
}
}

/// <summary>
/// Applies the string-specific <paramref name="options"/> to the <paramref name="value"/>.
/// </summary>
/// <remarks>
/// When <see cref="IEquivalencyAssertionOptions.IgnoreLeadingWhitespace"/> is set, whitespace is removed from the start of the <paramref name="value"/>.<br />
/// When <see cref="IEquivalencyAssertionOptions.IgnoreTrailingWhitespace"/> is set, whitespace is removed from the end of the <paramref name="value"/>.<br />
/// When <see cref="IEquivalencyAssertionOptions.IgnoreNewlines"/> is set, all newlines ("\r" and "\n") are removed from the <paramref name="value"/>.
/// </remarks>
private static string ApplyStringSettings(string value, IEquivalencyAssertionOptions options)
{
if (options.IgnoreLeadingWhitespace)
{
value = value.TrimStart();
}

if (options.IgnoreTrailingWhitespace)
{
value = value.TrimEnd();
}

if (options.IgnoreNewlines)
{
value = value
.Replace("\r", string.Empty, StringComparison.Ordinal)
.Replace("\n", string.Empty, StringComparison.Ordinal);
}

return value;
}

/// <summary>
/// Returns the type of the subject the assertion applies on.
/// </summary>
Expand Down

0 comments on commit b9e138c

Please sign in to comment.