Skip to content

assert: include map key in InDeltaMapValues error message - #1898

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

gaurav0107 wants to merge 1 commit into
stretchr:masterfrom
gaurav0107:fix/1254-assert-indeltamapvalues-prepend-key-name

Conversation

@gaurav0107

@gaurav0107 gaurav0107 commented May 24, 2026 •

Copy link
Copy Markdown

Summary

Closes #1254. When assert.InDeltaMapValues fails for a map entry, the failure output now names the offending key.

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.1, but difference was -1

After this change:

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

A user-supplied message is kept after the key (Messages: key[a]: my message).

Changes

  • assert/assertions.go: InDeltaMapValues still delegates to InDelta, but passes msgAndArgs through a small helper, msgAndArgsWithKey, which prepends key[<k>] to the user message. Nothing is formatted up front, so the message is only rendered on failure and a single string message is still printed verbatim.
  • assert/assertions_test.go: add TestInDeltaMapValues_ErrorIncludesKey covering string and int keys, a verbatim single-string message, and a formatted message.

Testing

  • go test -race ./assert/... and go test ./... pass
  • ./.ci.gofmt.sh, ./.ci.govet.sh, ./.ci.gogenerate.sh pass

Backward compatibility

Only the failure output changes: a key[<k>] prefix is added to the Messages line. Pass/fail behaviour is unchanged.

@gaurav0107
gaurav0107 marked this pull request as ready for review May 24, 2026 18:26
Comment thread assert/assertions.go Outdated
}
dt := af - bf
if dt < -delta || dt > delta {
return Fail(t, fmt.Sprintf("key[%v]: Max difference between %v and %v allowed is %v, but difference was %v", k, ev.Interface(), av.Interface(), delta, dt), msgAndArgs...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems like a quite a big change for a minor copy fix, is it possible to fix it inside of InDelta without re-writing everything, maybe pass the key[%v]: prefix with msgAndArgs?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point, done. InDeltaMapValues calls InDelta again, same as before, and the key goes in through msgAndArgs. It's added by a small helper, msgAndArgsWithKey, which puts key[<k>] in front of the user's message without formatting it, so a message like "100%" is still printed as-is and nothing gets formatted unless the assertion fails. I also reverted the generated doc-comment changes. The diff is now one changed call plus the helper and a test.

The key now shows up on the Messages: line instead of Error::

Error:      Max difference between 3 and 4 allowed is 0.1, but difference was -1
Messages:   key[a]: my message

Also rebased on master and squashed into one commit.

fredbi added a commit to go-openapi/testify that referenced this pull request Aug 23, 2026
A missing key was already reported by name, but a key present in both maps
whose values are further apart than delta was not: the failure came from
InDelta, which sees the two values and not the key they were read under. On a
map of any size that leaves the reader to work out which entry failed.

The per-key call now carries "at key <k>" through withContext, the same way
InEpsilonSlice carries its index, so the caller's own message survives too.

reference: github.com/stretchr#1898

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to go-openapi/testify that referenced this pull request Aug 23, 2026
The August sweep of stretchr/testify turned up four reports describing bugs we
had inherited. Each now has a row in the implemented table saying what we
adopted and what we left alone, and stretchr#1942 joins stretchr#1940 as the PR behind
ErrorNotContains.

stretchr#1776 goes in as informational: upstream's generated require documentation
lists a bool return that the functions do not have, the same defect our own API
pages carried until the signature tables were corrected. Nothing to adopt, but
worth recording that both projects found it independently.

Two more bugs surfaced while checking those four, neither of them reported
upstream, so neither gets a row: InDeltaSlice named expected and actual the
wrong way round in its failures, and EqualValues compared a negative signed
integer equal to a large unsigned one.

Also in this file: the review-frequency line was three months stale, [stretchr#1937]
was referenced with no link definition and rendered as literal text, three
definitions were duplicated, and the summary counts are recounted from the
tables.

reference: github.com/stretchr#1776
reference: github.com/stretchr#1874
reference: github.com/stretchr#1875
reference: github.com/stretchr#1898
reference: github.com/stretchr#1899
reference: github.com/stretchr#1908
reference: github.com/stretchr#1931
reference: github.com/stretchr#1940
reference: github.com/stretchr#1942

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
When InDeltaMapValues fails for a map entry, the output did not say
which key produced the mismatch, leaving the user to bisect the map by
hand.

Prepend "key[<k>]" to the user message passed to InDelta, so the
failure now reads:

    Error:      Max difference between 3 and 4 allowed is 0.1, but difference was -1
    Messages:   key[a]: my message

The user message is still only formatted on failure, and a single
string message is still printed verbatim.

Fixes stretchr#1254

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gaurav0107
gaurav0107 force-pushed the fix/1254-assert-indeltamapvalues-prepend-key-name branch from 4faad33 to 51ea4d9 Compare November 25, 2026 20:16
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

2 participants