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

testing: examples with trailing whitespace in multiple lines of output can be confusing #26460

Open
joesis opened this issue Jul 19, 2018 · 11 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@joesis
Copy link

joesis commented Jul 19, 2018

Below example fails. Even if I copy the output of go test to the output section, it still fails.

func ExampleTrailingSpace() {
	fmt.Println("abc ")
	fmt.Println("cde")
	// Output:
	//abc
	//cde
}

However, if I remove the second print, it succeeds.

func ExampleTrailingSpace() {
	fmt.Println("abc ")
	// Output:
	//abc
}

Why? Because testing/example.go trims leading and trailing spaces of the whole output, but not every line. Code formatting tools, including gofmt, trims the space of each comment line.

I suggest trimming each line before comparing the output.

@joesis
Copy link
Author

joesis commented Jul 19, 2018

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.

fmt.Println("Using Func1:")
for i := 0; i < 10; i++ {
	fmt.Printf("%.3f\t", Func1(i))
}
fmt.Println("")
fmt.Println("Using Func2:")
...

@mvdan
Copy link
Member

mvdan commented Jul 19, 2018

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?

@mvdan mvdan changed the title testing/example multiline output can be confusing testing: examples with trailing whitespace in multiple lines of output can be confusing Jul 19, 2018
@joesis
Copy link
Author

joesis commented Jul 19, 2018

I guess trimming trailing whitespace should be ok, as leading ones can be easily spotted.

@josharian
Copy link
Contributor

See also #6416 and some of the issues linked there

@bcmills bcmills added this to the Go1.12 milestone Jul 19, 2018
@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 19, 2018
@joesis
Copy link
Author

joesis commented Jul 19, 2018

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.

@andybons andybons modified the milestones: Go1.12, Go1.13 Feb 12, 2019
@iwdgo
Copy link
Contributor

iwdgo commented Apr 13, 2019

This behavior prevents to provide a testing example of a func which tabulates data using f.i. fmt.Printf("%s\t", s). Although the tab is useless before EOL, it introduces white spaces which StdOut keeps. The trailing spaces cannot be handled by the // Output: of an Example. Removing trailing spaces of every line would help like:
lines[i] = strings.TrimRightFunc(s, func(r rune) bool { return unicode.IsSpace(r) })

(edited to add proposal)

@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@mig4
Copy link

mig4 commented Jul 21, 2019

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:

func ExampleExplicitTrimming() {
	fmt.Printf("this\n\tor that  \n")

	// some way to indicate a character to use as margin
	// so expected output is between the characters

	// Output(margin='|'):
	// |this|
	// |	or that  |
}

@mig4
Copy link

mig4 commented Jul 21, 2019

Also #23542

@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@larschri
Copy link

larschri commented Mar 5, 2021

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.

@pcj
Copy link

pcj commented Oct 28, 2022

I would really appreciate if example tests displayed failures using something like go-cmp cmp.Diff output. The way it reports now is really a time sink. I often give up on example tests and just write normal ones as a result of the frustration I encounter with them.

@mvdan
Copy link
Member

mvdan commented Oct 28, 2022

@pcj that's #45200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

10 participants