You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
assert/require.Len doesn't print anything if the slice is too long #1525
I haven't quite figured out why this happens, although I can imagine the output would be impractically long anyway. Worst of all, the main assertion message is not displayed which is quite confusing (especially to someone new using the library, like me 😄).
Personally, I think that it would be best not to display the slice at all for the Len function, and instead just display:
Should have X item(s), but has Y
However, I suspect my view on that may not be what some people want. The reason I think it is unwise to print the slice in any form, is that typically when many people are testing for the length of a slice, they may be comparing to a relatively large slice of items (e.g. the output of a mocked API call or similar).
An additional point that I believe has been discussed before is the inconsistency with argument ordering for the Len function which takes the actual first and expected second.
This mishandles the scanner.Scan() call. If scanner.Scan() returns false then you need to check scanner.Err() in case it isn't io.EOF. If we did that we'd have found it is bufio.Scanner: token too long.
We need to think of a thing to do about very long lines in this code. We should also probably not produce such a long line from assert.Len, maybe we should truncate after a sane number of elements?
This mishandles the scanner.Scan() call. If scanner.Scan() returns false then you need to check scanner.Err() in case it isn't io.EOF. If we did that we'd have found it is bufio.Scanner: token too long.
We need to think of a thing to do about very long lines in this code. We should also probably not produce such a long line from assert.Len, maybe we should truncate after a sane number of elements?
That sounds good to me. Personally I'd even be happy for the contents of the slice not to be printed at all, but I suppose this output can be useful for shorter slices when troubleshooting why a test is failing.
Thanks, yep I'm using this already, but I think my point was more around the inconsistency of the API and how that particular function feels odd in the context of the rest.
Of course I appreciate that this would be a breaking change but just thought I'd bring it up for consideration in case a major new version of testify was being planned.
This mishandles the scanner.Scan() call. If scanner.Scan() returns false then you need to check scanner.Err() in case it isn't io.EOF. If we did that we'd have found it is bufio.Scanner: token too long.
I've raised a PR that will make sure that the TokenTooLong never happens.
We should also probably not produce such a long line from assert.Len, maybe we should truncate after a sane number of elements?
Agreed. But regardless of the call we take about displaying long messages, I think this fix will be useful.
I think the idea of truncating the error after a certain length (1k?, 4k?) seems reasonable, and avoids spamming a huge amount of unnecessary output while making sure that the error message is useful.
Personally, I think that it would be best not to display the slice at all for the Len function, and instead just display:\n\nShould have X item(s), but has Y\nHowever, I suspect my view on that may not be what some people want. The reason I think it is unwise to print the slice in any form, is that typically when many people are testing for the length of a slice, they may be comparing to a relatively large slice of items (e.g. the output of a mocked API call or similar).
One naive solution could be to check the length of the sprinted value, and truncate it after let's say an arbitrary value of 20-30 characters. Something with a trailing … plus tge number of items.
If the slice is short and simple, such as a slice 2-3 int/string/bool, the message displayed will be the same as now. The error message will be meaningful.
In other cases, where the message is currently useless, it would be better than nothing.
One naive solution could be to check the length of the sprinted value, and truncate it after let's say an arbitrary value of 20-30 characters. Something with a trailing … plus tge number of items.
If the slice is short and simple, such as a slice 2-3 int/string/bool, the message displayed will be the same as now. The error message will be meaningful.
In other cases, where the message is currently useless, it would be better than nothing.
Any thoughts guys?
A very rough draft, but potentially something like:
funcContains(tTestingT, s, containsinterface{}, msgAndArgs...interface{}) bool {
ifh, ok:=t.(tHelper); ok {
h.Helper()
}
ok, found:=containsElement(s, contains)
if!ok {
returnFail(t, fmt.Sprintf("%#v could not be applied builtin len()", s), msgAndArgs...)
}
if!found {
returnFail(t, fmt.Sprintf("%#v does not contain %#v", s, contains), msgAndArgs...)
}
returntrue
}
Replaced with
functruncatingSprintf(formatstring, a...any) string {
if!strings.HasPrefix(format, "%#v") {
panic("TruncatingSprintf format argument must begin with '%#v'")
}
result:=fmt.Sprintf(format, a...)
iflen(result) <1024 {
returnresult
}
truncatedFormat:="object of type %T"+format[3:]
returnfmt.Sprintf(truncatedFormat, len(result))
}
funcContains(tTestingT, s, containsinterface{}, msgAndArgs...interface{}) bool {
ifh, ok:=t.(tHelper); ok {
h.Helper()
}
ok, found:=containsElement(s, contains)
if!ok {
returnFail(t, truncatingSprintf("%#v could not be applied builtin len()", s), msgAndArgs...)
}
if!found {
returnFail(t, truncatingSprintf("%#v does not contain %#v", s, contains), msgAndArgs...)
}
returntrue
}
I'm unsure there will be an agreement or consensus on inverting the message and the truncated string. But even if not inverted, I'm OK with it as long we keep the truncated string short so <= 32
If I'm asserting the length of a slice, it's precisely because I don't care about the slice items. Otherwise, I would have used assert.Equal() instead. So for me, a good solution would also be simply showing the length mismatch on failure, never the items of the slice.
There actually is already a truncatingSprintf function in testify used by Equal. It's called truncatingFormat and it comes with an existing decision on what the limit should be. I've opened a PR to use it in other relevant assertions. If we feel the limit should be smaller than the technical maximum then we can address that in another PR.
Lines should now be not too long that they can't be printed to the terminal. #1801 will choose a lower limit where they are not too big to be useful to humans.
Activity
fgimian commentedon Jan 23, 2024
The easiest workaround for now seems to be:
brackendawson commentedon Feb 21, 2024
We use a scanner to get the text which should go there line by line (it can be multiline) to indent each line until after the field name:
testify/assert/assertions.go
Lines 304 to 313 in 7caada5
This mishandles the
scanner.Scan()call. Ifscanner.Scan()returns false then you need to checkscanner.Err()in case it isn'tio.EOF. If we did that we'd have found it isbufio.Scanner: token too long.We need to think of a thing to do about very long lines in this code. We should also probably not produce such a long line from
assert.Len, maybe we should truncate after a sane number of elements?fgimian commentedon Feb 21, 2024
That sounds good to me. Personally I'd even be happy for the contents of the slice not to be printed at all, but I suppose this output can be useful for shorter slices when troubleshooting why a test is failing.
Cheers
Fotis
hendrywiranto commentedon Feb 22, 2024
Hello
For the argument ordering, you can enable https://github.com/Antonboom/testifylint via golangci
fgimian commentedon Feb 22, 2024
Thanks, yep I'm using this already, but I think my point was more around the inconsistency of the API and how that particular function feels odd in the context of the rest.
Of course I appreciate that this would be a breaking change but just thought I'd bring it up for consideration in case a major new version of testify was being planned.
Cheers
Fotis
brackendawson commentedon Feb 23, 2024
The argument order is covered by #146. Let's keep this issue about the missing message.
assert: handle TokenTooLong error scenario while formatting assertion…
arjunmahishi commentedon Mar 3, 2024
I've raised a PR that will make sure that the
TokenTooLongnever happens.Agreed. But regardless of the call we take about displaying long messages, I think this fix will be useful.
mirober commentedon Jul 6, 2024
I hit this recently and debugged it to the same root cause. For example the following test:
Produces:
I think the idea of truncating the error after a certain length (1k?, 4k?) seems reasonable, and avoids spamming a huge amount of unnecessary output while making sure that the error message is useful.
Looking at the usage code it might be a bit awkward to do this though (https://github.com/stretchr/testify/blob/master/assert/assertions.go#L938). I guess everywhere messages are formatted would need to be modified with a call to a function that would format and then truncate.
I'd be happy to put together a PR if there is agreement on the right approach.
ccoVeille commentedon Jul 11, 2024
I totally agree with you
ccoVeille commentedon Jul 11, 2024
One naive solution could be to check the length of the sprinted value, and truncate it after let's say an arbitrary value of 20-30 characters. Something with a trailing … plus tge number of items.
If the slice is short and simple, such as a slice 2-3 int/string/bool, the message displayed will be the same as now. The error message will be meaningful.
In other cases, where the message is currently useless, it would be better than nothing.
Any thoughts guys?
mirober commentedon Jul 11, 2024
A very rough draft, but potentially something like:
Replaced with
ccoVeille commentedon Jul 11, 2024
Except we were talking about "Len", and not "Contains", yes it's exactly what I'm thinking about
Should I assume there is the same issue (dumping a slice) with Contains?
mirober commentedon Jul 11, 2024
Ah yes, I guess there is the same issue for any function which operates on containers (at least Len and Contains, but perhaps others?)
ccoVeille commentedon Jul 11, 2024
1024 seems a very high value.
It's OK for the example reported here, but having 1024 characters dumped on screen, which means 13 screens of text (assuming 80x25 terminal)
maybe 1024 is fine, but then we could simply add an "\n" in the formatted string. This way the message Len expected actual would be visible
ccoVeille commentedon Jul 11, 2024
Let's wait for other people feedbacks. But, yes anything like that would be better than current messages
thockin commentedon Jul 11, 2024
In other code I have written is was presented a little differently:
Where
snippet()was:They key parts being:
snippet()on anything and get an abbreviated snippet.ccoVeille commentedon Jul 13, 2024
I'm unsure there will be an agreement or consensus on inverting the message and the truncated string. But even if not inverted, I'm OK with it as long we keep the truncated string short so <= 32
jdemeyer commentedon Sep 27, 2024
Instead of
I'm simply using
If I'm asserting the length of a slice, it's precisely because I don't care about the slice items. Otherwise, I would have used
assert.Equal()instead. So for me, a good solution would also be simply showing the length mismatch on failure, never the items of the slice.brackendawson commentedon Oct 3, 2024
There actually is already a
truncatingSprintffunction in testify used byEqual. It's calledtruncatingFormatand it comes with an existing decision on what the limit should be. I've opened a PR to use it in other relevant assertions. If we feel the limit should be smaller than the technical maximum then we can address that in another PR.brackendawson commentedon Sep 19, 2025
Closed by #1646
brackendawson commentedon Sep 19, 2025
Lines should now be not too long that they can't be printed to the terminal. #1801 will choose a lower limit where they are not too big to be useful to humans.