Repository navigation
assert: include map key in InDeltaMapValues error message - #1898
gaurav0107 wants to merge 1 commit into
Conversation
| } | ||
| 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...) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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>
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>
4faad33 to
51ea4d9
Compare
Summary
Closes #1254. When
assert.InDeltaMapValuesfails for a map entry, the failure output now names the offending key.Why
Without the key, a failing
InDeltaMapValuesagainst a multi-entry map gives no hint about which key broke. Previously:After this change:
A user-supplied message is kept after the key (
Messages: key[a]: my message).Changes
assert/assertions.go:InDeltaMapValuesstill delegates toInDelta, but passes msgAndArgs through a small helper,msgAndArgsWithKey, which prependskey[<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: addTestInDeltaMapValues_ErrorIncludesKeycovering string and int keys, a verbatim single-string message, and a formatted message.Testing
go test -race ./assert/...andgo test ./...pass./.ci.gofmt.sh,./.ci.govet.sh,./.ci.gogenerate.shpassBackward compatibility
Only the failure output changes: a
key[<k>]prefix is added to the Messages line. Pass/fail behaviour is unchanged.