Skip to content

assert: include map key in InDeltaMapValues error message#1898

Open
gaurav0107 wants to merge 2 commits into
stretchr:masterfrom
gaurav0107:fix/1254-assert-indeltamapvalues-prepend-key-name
Open

assert: include map key in InDeltaMapValues error message#1898
gaurav0107 wants to merge 2 commits into
stretchr:masterfrom
gaurav0107:fix/1254-assert-indeltamapvalues-prepend-key-name

Conversation

@gaurav0107
Copy link
Copy Markdown

Summary

Closes #1254. assert.InDeltaMapValues now prefixes per-key delta failures with key[<k>]: so users can immediately see which entry diverged.

Why

Without the key, a failing InDeltaMapValues against a multi-entry map gives no hint about which key broke. Previously:

Error: Max difference between 3 and 4 allowed is 0.01, but difference was -1

After this change:

Error: key[a]: Max difference between 3 and 4 allowed is 0.01, but difference was -1

The reporter showed the same before/after on the issue and even prototyped the fix; this PR finishes that work.

Changes

  • assert/assertions.go: inline the relevant InDelta math inside InDeltaMapValues so the failure message can include the map key. The numeric/NaN/delta branches mirror InDelta exactly, with the only addition being a key[<k>]: prefix.
  • assert/assertions_test.go: add TestInDeltaMapValues_ErrorIncludesKey covering the new message format.
  • assert/assertion_format.go, assert/assertion_forward.go, require/require.go, require/require_forward.go: regenerated by go generate ./... to propagate the updated doc comment.

Testing

  • go test -v -race ./... — all packages pass
  • ./.ci.gofmt.sh, ./.ci.govet.sh, ./.ci.gogenerate.sh — pass
  • New test TestInDeltaMapValues_ErrorIncludesKey asserts key[a]: appears in the captured failure output.

Backward compatibility

Behaviour for valid input is unchanged — only the failure-message text is enriched. True/False semantics are preserved. No tests in the repo pattern-match the previous message text.

When InDeltaMapValues fails for a particular map entry, the failure
message did not say which key produced the mismatch. The output looked
like:

    Error: Max difference between 3 and 4 allowed is 0.01, but difference was -1

leaving the user to bisect their map by hand. Inline the relevant
InDelta math so the failure message can be prefixed with key[<k>]:,
producing:

    Error: key[a]: Max difference between 3 and 4 allowed is 0.01, but difference was -1

The behaviour for valid input is unchanged.

Fixes stretchr#1254
Replace the single-case test with a table covering:
- string-keyed map with int values
- int-keyed map with float values
- non-numerical value at a key (Parameters must be numerical)
- NaN actual value

This pins the new key[<k>]: prefix across the four message branches
introduced for stretchr#1254.
@gaurav0107 gaurav0107 marked this pull request as ready for review May 24, 2026 18:26
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.

assert.InDeltaMapValues: prepend key name to error message

1 participant