Skip to content

Show the differing index when Should -Be compares arrays#2748

Merged
nohwnd merged 2 commits into
mainfrom
fix/1990-array-diff-index
Jun 27, 2026
Merged

Show the differing index when Should -Be compares arrays#2748
nohwnd merged 2 commits into
mainfrom
fix/1990-array-diff-index

Conversation

@nohwnd

@nohwnd nohwnd commented Jun 25, 2026

Copy link
Copy Markdown
Member

Fix #1990

When two arrays have the same length but a different value, the failure now points at the first differing index. It prints the expected and actual element at that index, and keeps the existing length mismatch and scalar messages unchanged. Added regression tests for Should -Be and Should -BeExactly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread tst/functions/assertions/Be.Tests.ps1 Outdated
It "Keeps the plain message for collections of equal length" {
ShouldBeFailureMessage (1, 2, 3) (1, 2, 4) | Verify-Equal "Expected @(1, 2, 4), but got @(1, 2, 3)."
It "Outputs the differing index for collections of equal length" {
ShouldBeFailureMessage (1, 2, 3) (1, 2, 4) | Verify-Equal "Expected collections to be the same, but they were different.`nCollection lengths are both 3.`nCollections differ at index 2.`nExpected: 4`nBut was: 3"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

  • format with newlines so we can read it (don't forget the normalize the output so it compares on linux as well, plenty examples around)

  • we no longer get to see the content of the array, not good.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 5a1d6fe - the message now shows the full collections (like the string diff does) plus the index, so the array contents are back. Joined with
so it compares the same on Linux.

Showing only the differing element dropped the array contents. Show the
whole collection like the string diff does and keep the index callout.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd marked this pull request as ready for review June 27, 2026 07:45
@nohwnd nohwnd merged commit f87472f into main Jun 27, 2026
13 checks passed
@nohwnd nohwnd deleted the fix/1990-array-diff-index branch June 27, 2026 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show difference index / arrow for array differences

1 participant