]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc: handle Examples with no body
authorAgniva De Sarker <agnivade@yahoo.co.in>
Sun, 16 Dec 2018 13:43:25 +0000 (19:13 +0530)
committerRobert Griesemer <gri@golang.org>
Sun, 16 Dec 2018 19:03:43 +0000 (19:03 +0000)
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>
src/go/doc/example.go
src/go/doc/example_test.go

index 45350f8fd3532f5d15be8c6d4d7c38a4e9e2d1a7..81956f2fdbfe0158f8a20687da4e37dcbfa027a5 100644 (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 {
index 0d2bf72e319b6e5ecbeb4b250b60b9abdf7f4a4b..74fd10626d2771cb26b2216e1e7cf09204d21fdb 100644 (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>"