Skip to content

Incorrect line breaks handling with custom string comparer #2566

@bart-vmware

Description

@bart-vmware

Description

Follow-up for #2413 (comment). #2413 was supposed to fix this, but it still doesn't work correctly.

Reproduction Steps

Checkout the latest develop branch (600d360) and run the below tests. Both tests should succeed, but they fail with:

Xunit.Sdk.XunitException
Expected logLines to be equivalent to "one\r\ntwo\r\nthree", but it misses some extra whitespace at the end.
   at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Execution\XUnit2TestFramework.cs:line 38
   at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Execution\TestFrameworkProvider.cs:line 39
   at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Execution\DefaultAssertionStrategy.cs:line 18
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Execution\AssertionScope.cs:line 274
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Execution\AssertionScope.cs:line 242
   at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Execution\AssertionScope.cs:line 296
   at FluentAssertions.Primitives.StringEqualityStrategy.ValidateAgainstSuperfluousWhitespace(IAssertionScope assertion, String subject, String expected) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Primitives\StringEqualityStrategy.cs:line 69
   at FluentAssertions.Primitives.StringEqualityStrategy.ValidateAgainstMismatch(IAssertionScope assertion, String subject, String expected) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Primitives\StringEqualityStrategy.cs:line 23
   at FluentAssertions.Primitives.StringValidator.Validate(String subject, String expected) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Primitives\StringValidator.cs:line 34
   at FluentAssertions.Primitives.StringAssertions`1.BeEquivalentTo(String expected, Func`2 config, String because, Object[] becauseArgs) in C:\Source\Repos\fluentassertions\Src\FluentAssertions\Primitives\StringAssertions.cs:line 157
   at FluentAssertions.Specs.AndWhichConstraintSpecs.BeEquivalentTo_LineEndingsComparer() in C:\Source\Repos\fluentassertions\Tests\FluentAssertions.Specs\AndWhichConstraintSpecs.cs:line 35
[Fact]
public void BeEquivalentTo_LineEndingsComparer()
{
    string logLines = @"one\ntwo\nthree";
    string expected = @"one\r\ntwo\r\nthree";

    logLines.Should().BeEquivalentTo(expected, options => options.Using(IgnoreLineEndingsComparer.Instance));
}

[Fact]
public void MatchEquivalentOf_LineEndingsComparer()
{
    string logLines = @"one\ntwo\nthree";
    string expected = @"one\r\ntwo\r\nthree";

    logLines.Should().MatchEquivalentOf(expected);
}

See #2339 (comment) for the contents of the IgnoreLineEndingsComparer class.

Expected behavior

Tests succeed.

Actual behavior

Tests fail.

Regression?

No.

Known Workarounds

Cast subject (typed as string) to object before executing the assertion, see #2339.

Configuration

Latest develop branch (600d360)

Other information

Possibly unrelated, but while investigating I spotted another bug at:

public static string RemoveNewLines(this string @this)
{
return @this.Replace("\n", string.Empty, StringComparison.Ordinal)
.Replace("\r", string.Empty, StringComparison.Ordinal)
.Replace(@"\r\n", string.Empty, StringComparison.Ordinal);

Note the @ verbatim string modifier on the last line, which is equivalent to matching "\\r\\n" instead of "\r\n".
While this may be effectively dead code, it looks wrong.

Are you willing to help with a pull-request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions