Skip to content

assert: improve uint formatting on mismatch#1223

Open
mnotti wants to merge 3 commits intostretchr:masterfrom
mnotti:mn/uint-clean-formatting-on-mismatch
Open

assert: improve uint formatting on mismatch#1223
mnotti wants to merge 3 commits intostretchr:masterfrom
mnotti:mn/uint-clean-formatting-on-mismatch

Conversation

@mnotti
Copy link

@mnotti mnotti commented Jul 7, 2022

Summary

Cleans the formatting on uint mismatches. AND refactors unit tests as a little bonus :) .

Changes

I added uint types to switch on type in formatUnequalValues to use %v instead of %#v which was giving weird hex display.

I also refactored the accompanying unit test to use a more conventional testCase struct loop.

Motivation

All uints show weird hex value when displaying diffs which is confusing and annoying when debugging failed tests.
https://go.dev/play/p/hEUdfo-pVlP

Seems to be a known issue since 2017 and no one has done anything so I thought I'd take a quick stab at it.
(#400)

Related issues

Closes #400

@mnotti
Copy link
Author

mnotti commented Aug 17, 2022

@boyan-soubachov could i get some quick eyes on this one if you get some cycles?

@dolmen dolmen added enhancement pkg-assert Change related to package testify/assert must-rebase labels Jul 6, 2023
@dolmen
Copy link
Collaborator

dolmen commented Jul 6, 2023

@mnotti Could you rebase your branch on current master?

@dolmen
Copy link
Collaborator

dolmen commented Jul 6, 2023

Simplified test case to confirm the issue on latest release: https://go.dev/play/p/6xEleN5v45P

@mnotti mnotti force-pushed the mn/uint-clean-formatting-on-mismatch branch from 7fa3412 to ebb036f Compare July 6, 2023 23:40
@mnotti
Copy link
Author

mnotti commented Jul 6, 2023

@mnotti Could you rebase your branch on current master?

Done.

@mnotti mnotti requested a review from dolmen July 6, 2023 23:55
@vitorfalcaor
Copy link

Hey @dolmen would you be able to take a look at this PR, please? Just spent some time debugging a test case thinking that assert.Equal, for some mysterious reason, was comparing memory addresses, then I realized it was actually a formatting issue. Not sure if @mnotti is still able to rebase and update the PR, so happy to take this to the finish line.

case time.Duration:
return fmt.Sprintf("%v", expected), fmt.Sprintf("%v", actual)
case time.Duration, uint, uint8, uint16, uint32, uint64:
return fmt.Sprint(expected), fmt.Sprint(actual)
Copy link
Collaborator

@brackendawson brackendawson Oct 7, 2024

Choose a reason for hiding this comment

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

Formatting these with "%[1]T(%[1]v)" would be better so that we don't say two apparently identical numbers are different. Otherwise we regress assert.Equal(t, int(1), uint(1)).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, using this format for all numeric types would fix #912, but this is creeping somewhat, perhaps better in a follow up PR.

Copy link
Collaborator

@dolmen dolmen left a comment

Choose a reason for hiding this comment

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

Reformatting a test suite is not something we want in the same PR as a change of the feature tested.

Please, add tests if necessary, but don't change existing tests, even if that is just reformatting. Because that makes review harder.

Reformatting of a test suite should be done in a PR that does only that.

@dolmen dolmen added the enhancement: output format Enhancement related to formatting of messages label May 30, 2025
@dolmen dolmen changed the title Clean uint formatting on mismatch assert: improve uint formatting on mismatch May 30, 2025
@dolmen dolmen added the assert.EqualValues About equality label Jun 2, 2025
@brackendawson
Copy link
Collaborator

I'm happy doing the work to review that the changes in the unit test are correct in this PR. I see an argument where these additions tip the threshold of making this into a tabled test. My previous review comments are still un-addressed though.

fredbi added a commit to fredbi/testify that referenced this pull request Dec 27, 2025
Merged from stretchr#1223

The issue is described at stretchr#400

Thanks to the original contributor @mnotti

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/testify that referenced this pull request Dec 27, 2025
Merged from stretchr#1223

The issue is described at stretchr#400

Thanks to the original contributor @mnotti

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to go-openapi/testify that referenced this pull request Dec 27, 2025
Merged from stretchr#1223

The issue is described at stretchr#400

Thanks to the original contributor @mnotti

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

assert.EqualValues About equality enhancement: output format Enhancement related to formatting of messages enhancement must-rebase pkg-assert Change related to package testify/assert

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Equal diff prints uints in hex format

4 participants