Skip to content

assert: avoid false changes to map entries in equality diffs - #2016

Open
businessdeveloper69 wants to merge 1 commit into
stretchr:masterfrom
businessdeveloper69:stable-map-entries-in-assertion-diffs
Open

businessdeveloper69 wants to merge 1 commit into
stretchr:masterfrom
businessdeveloper69:stable-map-entries-in-assertion-diffs

Conversation

@businessdeveloper69

Copy link
Copy Markdown

Summary

An unchanged map entry can appear on both sides of an assert.Equal diff when a new final key moves the comma. The existing TestDiff expectation showed this for the unchanged "three": 3 entry.

Changes

Use a trailing comma for every map entry when building a unified assertion diff. Enable this in the local dump configuration used by diff. The existing regression now shows "three" as a context line.

Motivation and verification

I replayed the timestamp map from #288 through public assert.Equal on v1.12.1, master and this commit. Appending and removing keys produced two false edit lines on the release and master, and zero with this change. Genuine value edits still appear.

I also ran Viper v1.21.0’s unchanged test, then repeated it with one extra YAML key. The original fixture passed in all runs. With the extra key, the test still failed correctly. Before the fix, the diff also removed and added the unchanged trousers: denim entry. With the fix, the only changed map entry was the added waistcoat: wool. Viper’s map assertion

The full Linux race suite passed on every Go minor from 1.17 through 1.27. The project’s generation, formatting and vet checks passed on Go 1.26.11 and 1.27.4.

Closes #288. The earlier go-spew proposal and the later decision to vendor the formatter provide the background for this change. Original formatter proposal · Vendoring discussion

AI assisted.

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Compared diff() for maps on master and here. When an entry is added or changed at the end, master also marked the entry before it as changed (only the comma differed). Here only the real change shows, for nested maps too.

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.

Diffs are slightly off for assert.Equal on maps

2 participants