-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: example output whitespace trimming can be confusing #23542
Comments
Perhaps another solution would be to not leave the empty line print at either end, so that it could not be trimmed. Leaving it in the middle, for example. |
I could take this one. I prefer mvdan's suggestion unless we'd like to be more explicit with the whitespace. Output would of course look like this:
Edit: Here's another proposal with explicit treatment of whitespace: The code:
The output:
|
It seems like the simplest fix is to swap the second and third line. |
This simple change exposed a limitation in example output testing. The example works if the Adding a whitespace character in the example output doesn't help either because each line is trimmed before being joined with a new line character as best I can tell.
|
Possibly related: #6416 |
@josharian I'm making certain to preserve the trailing space on the second line in the example (i.e., omit The unique aspect about this example is the single whitespace character in the middle line as opposed to a blank line. Stdout will always display the whitespace character but I cannot see a way to preserve the whitespace character in the example output. |
Here's a proposed compromise: Code:
Output:
This passes but it may convolute the last example. |
I dunno. It'd be nice for all three to be constructed similarly. I tried this, but it's even worse: package main
import (
"fmt"
"path"
)
func main() {
for _, s := range []string{"static/myfile.css", "myfile.css", ""} {
file, dir := path.Split(s)
fmt.Printf("path.Split(%q): file=%q dir=%q\n", s, file, dir)
}
} I can't say I have any better ideas at this point. |
Here's another suggestion that similarly constructs all 3 examples and passes. out := func(args ...interface{}) {
fmt.Printf("%q\n", fmt.Sprintln(args...))
}
out(path.Split("static/myfile.css"))
out(path.Split("myfile.css"))
out(path.Split("")) Output:
|
Any opinion on my last example? I will submit it as a CL if it's received better than the current example. |
On m phone, but: it is nice for examples to be copy-paste friendly. In particular, having a line that reads: file, sir := path.Split(x) with useful mnemonic variable names seems valuable. And then we could avoid nested fmt calls and a code golf flavor. |
Hey folks, I just did a Go contributors' workshop at London GopherCon and was looking at this as potentially my first issue to try to contribute. |
@irbekrm I don't think there was a consensus so far; I'd say pick a solution you like (or your own solution), and we can always discuss it on the CL. |
Thanks @mvdan |
Change https://golang.org/cl/191397 mentions this issue: |
At the moment the last output line of ExampleSplit- two empty strings- are being trimmed from the output. I have formatted the output of the function to avoid whitespace trimming and show empty strings more clearly. Fixes golang#23542 Change-Id: Ic2a4d98cfa06db1466c6c6d98099542df9e7c88b Reviewed-on: https://go-review.googlesource.com/c/go/+/191397 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Consider the Example for filepath.Split.
The code is:
The output is:
Given that there are three
fmt.Println
calls, it seems to me that there really ought to be three lines of output.(And if we decide that we don't want to or can't change example output whitespace trimming, we should at least tweak this example so that the last line of output contains non-whitespace, e.g. by prefixing each output line with an asterisk or number.)
The text was updated successfully, but these errors were encountered: