Skip to content

Commit 43a5edf

Browse files
committed
internal/fetch/internal/doc: fix detection of whole file examples
This CL uses the fix from CL 222477 (golang/go#38409), to fix detection of whole file examples. Fixes golang/go#39006 Fixes golang/go#39727 Change-Id: Iaf9240f7637a1723e681cdbb0d06bf36a43d8d2a Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/239180 Reviewed-by: Jonathan Amsterdam <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 3e55432 commit 43a5edf

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

internal/fetch/internal/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
}

internal/fetch/internal/doc/example_test.go

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -331,26 +331,65 @@ func main() {
331331
fmt.Println("Hello, world!")
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+
}
334371

335372
func TestExamplesWholeFile(t *testing.T) {
336-
fset := token.NewFileSet()
337-
file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleWholeFile), parser.ParseComments)
338-
if err != nil {
339-
t.Fatal(err)
340-
}
341-
es := doc.Examples(file)
342-
if len(es) != 1 {
343-
t.Fatalf("wrong number of examples; got %d want 1", len(es))
344-
}
345-
e := es[0]
346-
if e.Name != "" {
347-
t.Errorf("got Name == %q, want %q", e.Name, "")
348-
}
349-
if g, w := formatFile(t, fset, e.Play), exampleWholeFileOutput; g != w {
350-
t.Errorf("got Play == %q, want %q", g, w)
351-
}
352-
if g, w := e.Output, "Hello, world!\n"; g != w {
353-
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+
}
354393
}
355394
}
356395

0 commit comments

Comments
 (0)