go/doc: handle Examples with no body

Fixes #29271

Change-Id: Iff6a16c659ad6ec1b4d9559fcbcd40196086c60e
Reviewed-on: https://go-review.googlesource.com/c/154380
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Agniva De Sarker 2018-12-16 19:13:25 +05:30 committed by Robert Griesemer
parent d50390ce72
commit e167587dd6
2 changed files with 38 additions and 0 deletions

View file

@ -426,6 +426,9 @@ func stripOutputComment(body *ast.BlockStmt, comments []*ast.CommentGroup) (*ast
// lastComment returns the last comment inside the provided block.
func lastComment(b *ast.BlockStmt, c []*ast.CommentGroup) (i int, last *ast.CommentGroup) {
if b == nil {
return
}
pos, end := b.Pos(), b.End()
for j, cg := range c {
if cg.Pos() < pos {

View file

@ -413,6 +413,41 @@ func TestExampleInspectSignature(t *testing.T) {
}
}
const exampleEmpty = `
package p
func Example() {}
func Example_a()
`
const exampleEmptyOutput = `package main
func main() {}
func main()
`
func TestExampleEmpty(t *testing.T) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleEmpty), parser.ParseComments)
if err != nil {
t.Fatal(err)
}
es := doc.Examples(file)
if len(es) != 1 {
t.Fatalf("wrong number of examples; got %d want 1", len(es))
}
e := es[0]
if e.Name != "" {
t.Errorf("got Name == %q, want %q", e.Name, "")
}
if g, w := formatFile(t, fset, e.Play), exampleEmptyOutput; g != w {
t.Errorf("got Play == %q, want %q", g, w)
}
if g, w := e.Output, ""; g != w {
t.Errorf("got Output == %q, want %q", g, w)
}
}
func formatFile(t *testing.T, fset *token.FileSet, n *ast.File) string {
if n == nil {
return "<nil>"