From: Agniva De Sarker Date: Sun, 16 Dec 2018 13:43:25 +0000 (+0530) Subject: go/doc: handle Examples with no body X-Git-Tag: go1.12beta1~37 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e167587dd62a7e1d8683982e2b6eaa1c1cff9c67;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- diff --git a/src/go/doc/example.go b/src/go/doc/example.go index 45350f8fd3..81956f2fdb 100644 --- a/src/go/doc/example.go +++ b/src/go/doc/example.go @@ -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 { diff --git a/src/go/doc/example_test.go b/src/go/doc/example_test.go index 0d2bf72e31..74fd10626d 100644 --- a/src/go/doc/example_test.go +++ b/src/go/doc/example_test.go @@ -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 ""