]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc: do not treat methods as test functions
authorRoger Peppe <rogpeppe@gmail.com>
Fri, 20 Jul 2018 10:31:39 +0000 (11:31 +0100)
committerAndrew Bonventre <andybons@golang.org>
Tue, 31 Jul 2018 16:11:48 +0000 (16:11 +0000)
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í <mvdan@mvdan.cc>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/go/doc/example.go
src/go/doc/example_test.go

index 7fc6dedf7f8979c9e7b90a5750881503c7bea428..70d2ecb8d956051675a0ed6a3e0bc99cd6b1a0c3 100644 (file)
@@ -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++
index f0c30005048bb4768eae408983bc72eee65249e6..552a51bf742192a4659aeb7b2bdd236bf42297ce 100644 (file)
@@ -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 = "<nil>"
-                       } 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 "<nil>"
+       }
+       var buf bytes.Buffer
+       if err := format.Node(&buf, fset, n); err != nil {
+               t.Fatal(err)
+       }
+       return buf.String()
+}