Skip to content

Commit c4961dc

Browse files
flyingmutantgriesemer
authored andcommitted
go/doc: fix detection of whole file examples
After CL 211357 (commit 499dc1c), hasTests and numDecl were not updated properly for function declarations with parameters, which affected the whole file example detection logic. This caused examples like package foo_test func Foo(x int) { } func Example() { fmt.Println("Hello, world!") // Output: Hello, world! } to not be detected as whole file ones. Change-Id: I9ebd47e52d7ee9d91eb6f8e0257511de69b2a402 GitHub-Last-Rev: cc71c31 GitHub-Pull-Request: #37730 Reviewed-on: https://go-review.googlesource.com/c/go/+/222477 Reviewed-by: Agniva De Sarker <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Agniva De Sarker <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent aa3413c commit c4961dc

File tree

2 files changed

+61
-21
lines changed

2 files changed

+61
-21
lines changed

src/go/doc/example.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ func Examples(testFiles ...*ast.File) []*Example {
6262
if !ok || f.Recv != nil {
6363
continue
6464
}
65-
if params := f.Type.Params; len(params.List) != 0 {
66-
continue // function has params; not a valid example
67-
}
6865
numDecl++
6966
name := f.Name.Name
7067
if isTest(name, "Test") || isTest(name, "Benchmark") {
@@ -74,6 +71,9 @@ func Examples(testFiles ...*ast.File) []*Example {
7471
if !isTest(name, "Example") {
7572
continue
7673
}
74+
if params := f.Type.Params; len(params.List) != 0 {
75+
continue // function has params; not a valid example
76+
}
7777
if f.Body == nil { // ast.File.Body nil dereference (see issue 28044)
7878
continue
7979
}

src/go/doc/example_test.go

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -331,25 +331,65 @@ func main() {
331331
}
332332
`
333333

334+
const exampleWholeFileFunction = `package foo_test
335+
336+
func Foo(x int) {
337+
}
338+
339+
func Example() {
340+
fmt.Println("Hello, world!")
341+
// Output: Hello, world!
342+
}
343+
`
344+
345+
const exampleWholeFileFunctionOutput = `package main
346+
347+
func Foo(x int) {
348+
}
349+
350+
func main() {
351+
fmt.Println("Hello, world!")
352+
}
353+
`
354+
355+
var exampleWholeFileTestCases = []struct {
356+
Title, Source, Play, Output string
357+
}{
358+
{
359+
"Methods",
360+
exampleWholeFile,
361+
exampleWholeFileOutput,
362+
"Hello, world!\n",
363+
},
364+
{
365+
"Function",
366+
exampleWholeFileFunction,
367+
exampleWholeFileFunctionOutput,
368+
"Hello, world!\n",
369+
},
370+
}
371+
334372
func TestExamplesWholeFile(t *testing.T) {
335-
fset := token.NewFileSet()
336-
file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleWholeFile), parser.ParseComments)
337-
if err != nil {
338-
t.Fatal(err)
339-
}
340-
es := doc.Examples(file)
341-
if len(es) != 1 {
342-
t.Fatalf("wrong number of examples; got %d want 1", len(es))
343-
}
344-
e := es[0]
345-
if e.Name != "" {
346-
t.Errorf("got Name == %q, want %q", e.Name, "")
347-
}
348-
if g, w := formatFile(t, fset, e.Play), exampleWholeFileOutput; g != w {
349-
t.Errorf("got Play == %q, want %q", g, w)
350-
}
351-
if g, w := e.Output, "Hello, world!\n"; g != w {
352-
t.Errorf("got Output == %q, want %q", g, w)
373+
for _, c := range exampleWholeFileTestCases {
374+
fset := token.NewFileSet()
375+
file, err := parser.ParseFile(fset, "test.go", strings.NewReader(c.Source), parser.ParseComments)
376+
if err != nil {
377+
t.Fatal(err)
378+
}
379+
es := doc.Examples(file)
380+
if len(es) != 1 {
381+
t.Fatalf("%s: wrong number of examples; got %d want 1", c.Title, len(es))
382+
}
383+
e := es[0]
384+
if e.Name != "" {
385+
t.Errorf("%s: got Name == %q, want %q", c.Title, e.Name, "")
386+
}
387+
if g, w := formatFile(t, fset, e.Play), c.Play; g != w {
388+
t.Errorf("%s: got Play == %q, want %q", c.Title, g, w)
389+
}
390+
if g, w := e.Output, c.Output; g != w {
391+
t.Errorf("%s: got Output == %q, want %q", c.Title, g, w)
392+
}
353393
}
354394
}
355395

0 commit comments

Comments
 (0)