Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message on Len() assumes string #1482

Closed
kevinburkesegment opened this issue Oct 9, 2023 · 2 comments · Fixed by #1485
Closed

Error message on Len() assumes string #1482

kevinburkesegment opened this issue Oct 9, 2023 · 2 comments · Fixed by #1485
Labels
bug good first issue pkg-assert Change related to package testify/assert

Comments

@kevinburkesegment
Copy link
Contributor

This error message in assert.Len assumes that whatever you are asserting a length about can be formatted with the %s format string.

	if l != length {
		return Fail(t, fmt.Sprintf("\"%s\" should have %d item(s), but has %d", object, length, l), msgAndArgs...)
	}

If you look at the failure in e.g. https://drone.grafana.net/grafana/dskit/3189/1/4, I believe this is a []uint32, which is being formatted as "[%!s(uint32=2452015680) %!s(uint32=2476020736)]".

I think the %v format string might be a better fit.

@dolmen
Copy link
Collaborator

dolmen commented Oct 11, 2023

@kevinburkesegment Could you submit a patch?

If you submit an MR I will be able to merge it.
However if I do the MR myself I will not be able to merge it because, despites being a maintainer, I need another maintainer to validate it, but all other maintainers are not responsive.

@dolmen dolmen added bug good first issue pkg-assert Change related to package testify/assert labels Oct 11, 2023
kevinburkesegment added a commit to kevinburkesegment/testify that referenced this issue Oct 11, 2023
Previously, the use of %s with array objects meant you would get an
error like this:

    "[%!s(int=1) %!s(int=2) %!s(int=3)]\" should have 4 item(s), but has 3

Use %v instead, which provides a much nicer error.

    "[1 2 3]" should have 4 item(s), but has 3

Fixes stretchr#1482.
kevinburkesegment added a commit to kevinburkesegment/testify that referenced this issue Oct 12, 2023
Previously, the use of %s with array objects meant you would get an
error like this:

    "[%!s(int=1) %!s(int=2) %!s(int=3)]\" should have 4 item(s), but has 3

Use %v instead, which provides a much nicer error.

    "[1 2 3]" should have 4 item(s), but has 3

Fixes stretchr#1482.
kevinburkesegment added a commit to kevinburkesegment/testify that referenced this issue Oct 12, 2023
Previously, the use of %s with array objects meant you would get an
error like this:

    "[%!s(int=1) %!s(int=2) %!s(int=3)]\" should have 4 item(s), but has 3

Use %v instead, which provides a much nicer error.

    "[1 2 3]" should have 4 item(s), but has 3

Fixes stretchr#1482.
@dolmen
Copy link
Collaborator

dolmen commented Oct 13, 2023

Reproducer: https://go.dev/play/p/xH-RSrn8HP6

func Test(t *testing.T) {
	assert.Len(t, [...]int{1, 2, 3}, 1)
}
=== RUN   Test
    prog_test.go:10: 
        	Error Trace:	/tmp/sandbox2682030218/prog_test.go:10
        	Error:      	"[%!s(int=1) %!s(int=2) %!s(int=3)]" should have 1 item(s), but has 3
        	Test:       	Test
--- FAIL: Test (0.00s)
FAIL

kevinburkesegment added a commit to kevinburkesegment/testify that referenced this issue Oct 16, 2023
Previously, the use of %s with array objects meant you would get an
error like this:

    "[%!s(int=1) %!s(int=2) %!s(int=3)]\" should have 4 item(s), but has 3

Use %v instead, which provides a much nicer error.

    "[1 2 3]" should have 4 item(s), but has 3

Fixes stretchr#1482.
kevinburkesegment added a commit to kevinburkesegment/testify that referenced this issue Oct 24, 2023
Previously, the use of %s with array objects meant you would get an
error like this:

    "[%!s(int=1) %!s(int=2) %!s(int=3)]\" should have 4 item(s), but has 3

Use %v instead, which provides a much nicer error.

    "[1 2 3]" should have 4 item(s), but has 3

Fixes stretchr#1482.
kevinburkesegment added a commit to kevinburkesegment/testify that referenced this issue Oct 30, 2023
Previously, the use of %s with array objects meant you would get an
error like this:

    "[%!s(int=1) %!s(int=2) %!s(int=3)]\" should have 4 item(s), but has 3

Use %v instead, which provides a much nicer error.

    "[1 2 3]" should have 4 item(s), but has 3

Fixes stretchr#1482.
kevinburkesegment added a commit to kevinburkesegment/testify that referenced this issue Oct 31, 2023
Previously, the use of %s with array objects meant you would get an
error like this:

    "[%!s(int=1) %!s(int=2) %!s(int=3)]\" should have 4 item(s), but has 3

Use %v instead, which provides a much nicer error.

    "[1 2 3]" should have 4 item(s), but has 3

Fixes stretchr#1482.
kevinburkesegment added a commit to kevinburkesegment/testify that referenced this issue Feb 15, 2024
Previously, the use of %s with array objects meant you would get an
error like this:

    "[%!s(int=1) %!s(int=2) %!s(int=3)]\" should have 4 item(s), but has 3

Use %v instead, which provides a much nicer error.

    "[1 2 3]" should have 4 item(s), but has 3

Fixes stretchr#1482.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue pkg-assert Change related to package testify/assert
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants