Skip to content
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
6 changes: 4 additions & 2 deletions FluentAssertions.Core/Formatting/XElementValueFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ private static string FormatElementWithChildren(XElement element)
{
string [] lines = SplitIntoSeparateLines(element);

string firstLine = lines.First().Replace(Environment.NewLine, "");
string lastLine = lines.Last().Replace(Environment.NewLine, "");
// Can't use env.newline because the input doc may have unix or windows style
// line-breaks
string firstLine = lines.First().Replace("\r", "").Replace("\n", "");
string lastLine = lines.Last().Replace("\r", "").Replace("\n", "");

string formattedElement = firstLine + "..." + lastLine;
return formattedElement.Escape();
Expand Down
4 changes: 3 additions & 1 deletion FluentAssertions.Core/Primitives/StringAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ public AndConstraint<StringAssertions> MatchEquivalentOf(string wildcardPattern,
{
var validator = new StringWildcardMatchingValidator(Subject, wildcardPattern, reason, reasonArgs)
{
IgnoreCase = true
IgnoreCase = true,
IgnoreNewLineDifferences = true
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not ignore those new line differences in all places?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but I was aiming for backwards-compatibility, ensuring that it was only enabled for specific tests like the wildcard one.

};

validator.Validate();
Expand All @@ -209,6 +210,7 @@ public AndConstraint<StringAssertions> NotMatchEquivalentOf(string wildcardPatte
var validator = new StringWildcardMatchingValidator(Subject, wildcardPattern, reason, reasonArgs)
{
IgnoreCase = true,
IgnoreNewLineDifferences = true,
Negate = true
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,26 @@ private bool IsMatch
{
var options = IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;

return Regex.IsMatch(subject, ConvertWildcardToRegEx(expected), options | RegexOptions.Singleline);
return Regex.IsMatch(CleanNewLines(subject), ConvertWildcardToRegEx(CleanNewLines(expected)), options | RegexOptions.Singleline);
}
}

private static string ConvertWildcardToRegEx(string wildcardExpression)
private string ConvertWildcardToRegEx(string wildcardExpression)
{
return "^" + Regex.Escape(wildcardExpression).Replace("\\*", ".*").Replace("\\?", ".") + "$";
}

private string CleanNewLines(string input)
{
if (input == null)
return null;

if (IgnoreNewLineDifferences)
return input.Replace("\n", "").Replace("\r", "").Replace("\\r\\n", "");

return input;
}

protected override string ExpectationDescription
{
get
Expand All @@ -61,5 +72,10 @@ protected override string ExpectationDescription
/// Gets or sets a value indicating whether the matching process should ignore any casing difference.
/// </summary>
public bool IgnoreCase { get; set; }

/// <summary>
/// Ignores the difference betweeen environment newline differences
/// </summary>
public bool IgnoreNewLineDifferences { get; set; }
}
}