Commit 3073a02b authored by Ryan Slade's avatar Ryan Slade Committed by Andrew Gerrand

testing: in example, empty output not distinguished from missing output

Fixes #4485.

R=golang-dev, rsc, adg
CC=golang-dev
https://golang.org/cl/7071050
parent 05bf9a45
...@@ -792,7 +792,7 @@ func (t *testFuncs) load(filename, pkg string, seen *bool) error { ...@@ -792,7 +792,7 @@ func (t *testFuncs) load(filename, pkg string, seen *bool) error {
} }
} }
for _, e := range doc.Examples(f) { for _, e := range doc.Examples(f) {
if e.Output == "" { if e.Output == "" && !e.EmptyOutput {
// Don't run examples with no output. // Don't run examples with no output.
continue continue
} }
......
...@@ -19,12 +19,13 @@ import ( ...@@ -19,12 +19,13 @@ import (
) )
type Example struct { type Example struct {
Name string // name of the item being exemplified Name string // name of the item being exemplified
Doc string // example function doc string Doc string // example function doc string
Code ast.Node Code ast.Node
Play *ast.File // a whole program version of the example Play *ast.File // a whole program version of the example
Comments []*ast.CommentGroup Comments []*ast.CommentGroup
Output string // expected output Output string // expected output
EmptyOutput bool // expect empty output
} }
func Examples(files ...*ast.File) []*Example { func Examples(files ...*ast.File) []*Example {
...@@ -55,13 +56,15 @@ func Examples(files ...*ast.File) []*Example { ...@@ -55,13 +56,15 @@ func Examples(files ...*ast.File) []*Example {
if f.Doc != nil { if f.Doc != nil {
doc = f.Doc.Text() doc = f.Doc.Text()
} }
output, hasOutput := exampleOutput(f.Body, file.Comments)
flist = append(flist, &Example{ flist = append(flist, &Example{
Name: name[len("Example"):], Name: name[len("Example"):],
Doc: doc, Doc: doc,
Code: f.Body, Code: f.Body,
Play: playExample(file, f.Body), Play: playExample(file, f.Body),
Comments: file.Comments, Comments: file.Comments,
Output: exampleOutput(f.Body, file.Comments), Output: output,
EmptyOutput: output == "" && hasOutput,
}) })
} }
if !hasTests && numDecl > 1 && len(flist) == 1 { if !hasTests && numDecl > 1 && len(flist) == 1 {
...@@ -79,7 +82,8 @@ func Examples(files ...*ast.File) []*Example { ...@@ -79,7 +82,8 @@ func Examples(files ...*ast.File) []*Example {
var outputPrefix = regexp.MustCompile(`(?i)^[[:space:]]*output:`) var outputPrefix = regexp.MustCompile(`(?i)^[[:space:]]*output:`)
func exampleOutput(b *ast.BlockStmt, comments []*ast.CommentGroup) string { // Extracts the expected output and whether there was a valid output comment
func exampleOutput(b *ast.BlockStmt, comments []*ast.CommentGroup) (output string, ok bool) {
if _, last := lastComment(b, comments); last != nil { if _, last := lastComment(b, comments); last != nil {
// test that it begins with the correct prefix // test that it begins with the correct prefix
text := last.Text() text := last.Text()
...@@ -90,10 +94,10 @@ func exampleOutput(b *ast.BlockStmt, comments []*ast.CommentGroup) string { ...@@ -90,10 +94,10 @@ func exampleOutput(b *ast.BlockStmt, comments []*ast.CommentGroup) string {
if len(text) > 0 && text[0] == '\n' { if len(text) > 0 && text[0] == '\n' {
text = text[1:] text = text[1:]
} }
return text return text, true
} }
} }
return "" // no suitable comment found return "", false // no suitable comment found
} }
// isTest tells whether name looks like a test, example, or benchmark. // isTest tells whether name looks like a test, example, or benchmark.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment