From 1a5350e123e9ef8ce4fb27470e58a4bb58a445f0 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Fri, 20 Jul 2018 11:31:39 +0100 Subject: [PATCH] go/doc: do not treat methods as test functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The example code was treating a method starting with Test as a test function when considering whether to produce a whole-file example or not. As a method can never be a test function, this isn't correct. Change-Id: Idd8ec9eaf0904af076e941d7fe7d967f6b7eef78 Reviewed-on: https://go-review.googlesource.com/125257 Reviewed-by: Daniel Martí Reviewed-by: Brad Fitzpatrick Reviewed-by: Andrew Bonventre Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot --- src/go/doc/example.go | 2 +- src/go/doc/example_test.go | 82 +++++++++++++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/src/go/doc/example.go b/src/go/doc/example.go index 7fc6dedf7f..70d2ecb8d9 100644 --- a/src/go/doc/example.go +++ b/src/go/doc/example.go @@ -56,7 +56,7 @@ func Examples(files ...*ast.File) []*Example { continue } f, ok := decl.(*ast.FuncDecl) - if !ok { + if !ok || f.Recv != nil { continue } numDecl++ diff --git a/src/go/doc/example_test.go b/src/go/doc/example_test.go index f0c3000504..552a51bf74 100644 --- a/src/go/doc/example_test.go +++ b/src/go/doc/example_test.go @@ -6,6 +6,7 @@ package doc_test import ( "bytes" + "go/ast" "go/doc" "go/format" "go/parser" @@ -280,16 +281,7 @@ func TestExamples(t *testing.T) { t.Errorf("got Name == %q, want %q", e.Name, c.Name) } if w := c.Play; w != "" { - var g string // hah - if e.Play == nil { - g = "" - } else { - var buf bytes.Buffer - if err := format.Node(&buf, fset, e.Play); err != nil { - t.Fatal(err) - } - g = buf.String() - } + g := formatFile(t, fset, e.Play) if g != w { t.Errorf("%s: got Play == %q, want %q", c.Name, g, w) } @@ -299,3 +291,73 @@ func TestExamples(t *testing.T) { } } } + +const exampleWholeFile = `package foo_test + +type X int + +func (X) Foo() { +} + +func (X) TestBlah() { +} + +func (X) BenchmarkFoo() { +} + +func Example() { + fmt.Println("Hello, world!") + // Output: Hello, world! +} +` + +const exampleWholeFileOutput = `package main + +type X int + +func (X) Foo() { +} + +func (X) TestBlah() { +} + +func (X) BenchmarkFoo() { +} + +func main() { + fmt.Println("Hello, world!") +} +` + +func TestExamplesWholeFile(t *testing.T) { + fset := token.NewFileSet() + file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleWholeFile), 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), exampleWholeFileOutput; g != w { + t.Errorf("got Play == %q, want %q", g, w) + } + if g, w := e.Output, "Hello, world!\n"; 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 "" + } + var buf bytes.Buffer + if err := format.Node(&buf, fset, n); err != nil { + t.Fatal(err) + } + return buf.String() +}