Skip to content

testing: leading/trailing spacing in the output of examples is not handled correctly #9109

@fhs

Description

@fhs
What does 'go version' print?

go version devel +fe11c71dbee6 Tue Nov 11 16:06:47 2014 +1100 linux/amd64

What steps reproduce the problem?

In the code below, there are leading and trailing spaces in both the Println lines and
each lines of Output section.

$ cat foo_test.go
package foo
import "fmt"
func Example() {
    fmt.Println("  one  ")
    fmt.Println("  two  ")
    fmt.Println("  three  ")
    // Output:
    //   one  
    //   two  
    //   three  
}
$ go test
--- FAIL: Example (0.00s)
got:
one  
  two  
  three
want:
one
  two
  three
FAIL
exit status 1
FAIL    _/home/fhs/tmp/example  0.001s
$

What happened?

It looks like the leading spaces of first line and the trailing spaces of the last line
in the "got" output was removed. All other spaces are still there.

However, for the "want" output, only the leading spaces of the second and
third lines are there. All other spaces were removed (including all the trailing spaces
-- maybe the parser is removing all trailing spaces from comments).

What should have happened instead?

The documentation says: "The comparison ignores leading and trailing space."

It doesn't say the spaces are ignored for each line, but I think it makes more sense to
trim leading and trailing spaces for each line. At least it should be consistent about
how it's removing the spaces.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions