mirror of
https://github.com/golang/go
synced 2024-09-04 23:44:16 +00:00
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:
parent
d50390ce72
commit
e167587dd6
|
@ -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 {
|
||||
|
|
|
@ -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>"
|
||||
|
|
Loading…
Reference in a new issue