Include original index in extraneous item failure messages#3203
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves BeEquivalentTo failure diagnostics for collection equivalency by including the original index of extraneous (subject-only) items in the assertion failure message (Fixes #985), making it easier to locate where collections diverge.
Changes:
- Wrap subjects in
IndexedItem<object>so original indices survive the matching pipeline through to reporting. - Update ordered equivalency strategies to operate on
List<IndexedItem<object>>and compare via.Item. - Extend/adjust specs and release notes to cover and document the improved failure messages.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| docs/_pages/releases.md | Adds a release note describing the improved BeEquivalentTo extraneous-item messaging. |
| Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.BeEquivalentTo.cs | Updates an existing assertion message expectation to match the new output shape. |
| Tests/FluentAssertions.Equivalency.Specs/CollectionSpecs.cs | Updates single-extra-item expectation and adds coverage for multiple extra items including indices. |
| Tests/FluentAssertions.Equivalency.Specs/BasicSpecs.cs | Updates golden failure-message text to include the extraneous item index. |
| Src/FluentAssertions/Equivalency/Steps/StrictlyOrderedEquivalencyStrategy.cs | Changes subject inputs to IndexedItem<object> and compares using .Item. |
| Src/FluentAssertions/Equivalency/Steps/LooselyOrderedEquivalencyStrategy.cs | Changes subject inputs to IndexedItem<object> and compares using .Item throughout matching/scoring. |
| Src/FluentAssertions/Equivalency/Steps/EnumerableEquivalencyValidator.cs | Wraps subjects with original indices and updates failure reporting to include indices for extraneous-only cases. |
Test Results 37 files ± 0 37 suites ±0 2m 47s ⏱️ -1s Results for commit 6059560. ± Comparison against base commit fb42954. This pull request removes 10 and adds 10 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
Coverage Report for CI Build 25427286962Coverage decreased (-0.002%) to 97.14%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
Qodana for .NETIt seems all right 👌 No new problems were found according to the checks applied 💡 Qodana analysis was run in the pull request mode: only the changed files were checked Contact Qodana teamContact us at qodana-support@jetbrains.com
|
f29c4ec to
3fc7825
Compare
When BeEquivalentTo fails because the subject collection has more items
than expected, the error message now includes the original index of each
extraneous item to help diagnose where collections diverge.
Before:
found one extraneous item Customer { Age = 16, ... }
After:
found one extraneous item at index 1 Customer { Age = 16, ... }
For multiple extra items:
found extraneous items {Customer { Age = 16, ... } (at index 1), ...}
Fixes #985
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Escape { and } in Formatter.ToString output before embedding into
the FailWith format string, preventing string.Format from treating
object dumps like 'Customer { Age = 16 }' as format placeholders
- Pass a single item (not a 1-element list) as the {1} argument
whenever remainingSubjects.Count == 1, matching the previous
behavior for the 'both missing and extra' fallback path
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Multi-item: 'found extraneous items: A (at index 3), B (at index 96)'
instead of 'found extraneous items {A (at index 3), B (at index 96)}'
- Single-item: 'found one extraneous item at index 1: X'
instead of 'found one extraneous item at index 1 X'
- Fix TestBeEquivalent to be a proper test with a throw assertion
and a descriptive name
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…lidator.cs Co-authored-by: Jonas Nyrup <jnyrup@users.noreply.github.com>
Without escaping curly braces in the formatted items string, string.Format throws a FormatException, causing a **WARNING** fallback instead of the actual failure message. The existing test didn't catch this because the WARNING still contained the expected text from the raw format string. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jonas Nyrup <jnyrup@users.noreply.github.com>
…lidator.cs Co-authored-by: Jonas Nyrup <jnyrup@users.noreply.github.com>
510afe6 to
ad34005
Compare
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Fixes #985.
When
BeEquivalentTofails because the subject collection has more items than expected, the error message now includes the original index of each extraneous item, making it easier to diagnose where collections diverge.Before
After
Single extra item:
Multiple extra items:
Implementation
EnumerableEquivalencyValidator: subjects are now wrapped inList<IndexedItem<object>>so original indices survive through the matching pipeline to the reporting stage.StrictlyOrderedEquivalencyStrategy/LooselyOrderedEquivalencyStrategy: updated to passList<IndexedItem<object>>instead ofList<object>for subjects; raw objects are accessed via.Item.at index N:. When both missing and extra items exist, the compact fallback format is used to avoid expensive per-item formatting.Customer { Age = 16 }) are escaped before embedding into theFailWithformat string to preventstring.Formattreating them as placeholders.