Skip to content

Exception when using ContainInOrder and nested list-like data #3148

@wtjerry

Description

@wtjerry

Description

ContainInOrder throws an unexpected error during formatting in the following case:

  • actual is a nested list
  • the first element of actual is empty

The error happens in FluentAssertions.Formatting.FormattedObjectGraph.PossibleMultilineFragment.InsertLineOrFragment.
Possibly because Formatting the empty first element, resulted in an empty line.

Reproduction Steps

public record Foo(int Bar);

[Scenario]
public void Repro()
{
    IReadOnlyCollection<IReadOnlyCollection<Foo>> x =
    [
        [],
        [new Foo(42)]
    ];

    x.Should().ContainInOrder([new Foo(42)]);

    // or even more reduced:
    FluentAssertions.Formatting.Formatter.ToString(x, new FormattingOptions() { MaxDepth = 5, MaxLines = 100, UseLineBreaks = false });
}

Expected behavior

ContainInOrder succeeds or, if actual and expected differ, fails with a proper error.

Actual behavior

System.ArgumentOutOfRangeException: startIndex ('7') must be less than or equal to '0'. (Parameter 'startIndex')

System.ArgumentOutOfRangeException
startIndex ('7') must be less than or equal to '0'. (Parameter 'startIndex')
Actual value was 7.
   at System.ArgumentOutOfRangeException.ThrowGreater[T](T value, T other, String paramName)
   at System.String.Insert(Int32 startIndex, String value)
   at FluentAssertions.Formatting.FormattedObjectGraph.PossibleMultilineFragment.InsertLineOrFragment(String fragment)
   at FluentAssertions.Formatting.EnumerableValueFormatter.Format(Object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
   at FluentAssertions.Formatting.Formatter.Format(Object value, FormattedObjectGraph output, FormattingContext context, FormatChild formatChild)
   at FluentAssertions.Formatting.Formatter.ToString(Object value, FormattingOptions options)
   at FluentAssertions.Execution.MessageBuilder.<FormatArgumentPlaceholders>b__6_0(Object a)
   at System.Linq.Enumerable.ArraySelectIterator`2.Fill(ReadOnlySpan`1 source, Span`1 destination, Func`2 func)
   at System.Linq.Enumerable.ArraySelectIterator`2.ToArray()
   at FluentAssertions.Execution.MessageBuilder.FormatArgumentPlaceholders(String failureMessage, Object[] failureArgs)
   at FluentAssertions.Execution.MessageBuilder.Build(String message, Object[] messageArgs, String reason, ContextDataItems contextData, String identifier, String fallbackIdentifier)
   at FluentAssertions.Execution.AssertionScope.<>c__DisplayClass39_0.<FailWith>b__0()
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
   at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
   at FluentAssertions.Collections.GenericCollectionAssertions`3.ContainInOrder(IEnumerable`1 expected, String because, Object[] becauseArgs)
   at FluentAssertions.Collections.GenericCollectionAssertions`3.ContainInOrder(T[] expected)

Regression?

No response

Known Workarounds

No response

Configuration

nuget: 7.2.0

Other information

No response

Are you willing to help with a pull-request?

No

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions