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 · 5 comments

Comments

Projects
None yet
4 participants
@joesis

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

This comment has been minimized.

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

This comment has been minimized.

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 from testing/example multiline output can be confusing to testing: examples with trailing whitespace in multiple lines of output can be confusing Jul 19, 2018

@joesis

This comment has been minimized.

joesis commented Jul 19, 2018

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

@josharian

This comment has been minimized.

Contributor

josharian commented Jul 19, 2018

See also #6416 and some of the issues linked there

@bcmills bcmills added this to the Go1.12 milestone Jul 19, 2018

@joesis

This comment has been minimized.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment