Skip to content

assert: preserve zero-valued elements in collection comparisons - #1998

Open
gdpu11 wants to merge 1 commit into
stretchr:masterfrom
gdpu11:fix/elements-match-empty-collections
Open

gdpu11 wants to merge 1 commit into
stretchr:masterfrom
gdpu11:fix/elements-match-empty-collections

Conversation

@gdpu11

@gdpu11 gdpu11 commented Oct 25, 2026

Copy link
Copy Markdown

ElementsMatch(t, [2]int{}, []int{}) currently succeeds even though one collection contains two elements and the other contains none. NotElementsMatch gives the inverse wrong result, and comparing nil with a nonempty collection panics. These incorrect results can hide bugs in tests using zero-filled arrays.

The general Empty assertion intentionally treats a zero-valued array as empty, but collection equality must count its elements. Remove that shortcut from the two collection assertions and let their existing list-diff owner handle nil as a zero-length list. Empty itself and the public API remain unchanged.

Validation with Go 1.26.0 on macOS arm64:

  • New public assertion table cases fail on base 87a7b9d57689f6579db2da795ab8deeab29cb724, with wrong zero-array results and a nil-reflection panic.
  • Regressions cover zero-filled array/slice equality, differing array lengths, empty collections, and nil versus nonempty collections in both positions.
  • go test ./... and go vet ./... pass.
  • gofmt and git diff --check are clean.
  • Independent autoreview found no actionable regressions.

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.

1 participant