-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
testing: examples with trailing whitespace in multiple lines of output can be confusing #26460
Comments
It's not uncommon to tabular print in examples. It would be annoying to have to remove the trailing spaces, and more importantly, it's hard to spot what's wrong when the example fails.
|
Even if this is working as intended, I agree that the behavior and failures are confusing. Are you suggesting to trim only the trailing whitespace from the lines, or also the leading whitespace? |
I guess trimming trailing whitespace should be ok, as leading ones can be easily spotted. |
See also #6416 and some of the issues linked there |
Thanks for the link @josharian. Somehow I didn't find it from closed tickets before creating this one. If the behavior needs to be kept, a visual clue or message to the users would be ok too. |
This behavior prevents to provide a testing example of a (edited to add proposal) |
I'm new to Go and I find the idea of examples very useful and testable examples even better, reminds me of doctests from Python. However this underspecified behaviour with whitespace trimming is indeed limiting, as it is currently not possible to cover scenarios where a function's output is expected to contain specific trailing whitespace. It seems to me it would be much better if the trimming behaviour was instead explicit, allowing the user greater control over what is expected. Perhaps a way to indicate where to trim like:
|
Also #23542 |
This issue is a bit broader than what the title says. There are many examples of whitespace in output that can't be represented in the example output. I made a small workaround here https://github.com/larschri/testable-example-output. There is a Normalize function that attempts to implement the same processing that happens to the expected output. I don't know it is complete, but it is sufficient to make the tests pass in that repo. I hope it sheds some light into how this works. |
I would really appreciate if example tests displayed failures using something like go-cmp |
Below example fails. Even if I copy the output of
go test
to the output section, it still fails.However, if I remove the second print, it succeeds.
Why? Because
testing/example.go
trims leading and trailing spaces of the whole output, but not every line. Code formatting tools, includinggofmt
, trims the space of each comment line.I suggest trimming each line before comparing the output.
The text was updated successfully, but these errors were encountered: