From: Ryan Slade Date: Sat, 12 Jan 2013 00:05:53 +0000 (+1100) Subject: testing: in example, empty output not distinguished from missing output X-Git-Tag: go1.1rc2~1407 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=3073a02b19464f189cfd7f66ac5edf48742616e7;p=gostls13.git 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 --- diff --git a/src/cmd/go/test.go b/src/cmd/go/test.go index 5d3f21e5e9..d2498cafce 100644 --- a/src/cmd/go/test.go +++ b/src/cmd/go/test.go @@ -792,7 +792,7 @@ func (t *testFuncs) load(filename, pkg string, seen *bool) error { } } for _, e := range doc.Examples(f) { - if e.Output == "" { + if e.Output == "" && !e.EmptyOutput { // Don't run examples with no output. continue } diff --git a/src/pkg/go/doc/example.go b/src/pkg/go/doc/example.go index c7a0cf8c6d..f634e16770 100644 --- a/src/pkg/go/doc/example.go +++ b/src/pkg/go/doc/example.go @@ -19,12 +19,13 @@ import ( ) type Example struct { - Name string // name of the item being exemplified - Doc string // example function doc string - Code ast.Node - Play *ast.File // a whole program version of the example - Comments []*ast.CommentGroup - Output string // expected output + Name string // name of the item being exemplified + Doc string // example function doc string + Code ast.Node + Play *ast.File // a whole program version of the example + Comments []*ast.CommentGroup + Output string // expected output + EmptyOutput bool // expect empty output } func Examples(files ...*ast.File) []*Example { @@ -55,13 +56,15 @@ func Examples(files ...*ast.File) []*Example { if f.Doc != nil { doc = f.Doc.Text() } + output, hasOutput := exampleOutput(f.Body, file.Comments) flist = append(flist, &Example{ - Name: name[len("Example"):], - Doc: doc, - Code: f.Body, - Play: playExample(file, f.Body), - Comments: file.Comments, - Output: exampleOutput(f.Body, file.Comments), + Name: name[len("Example"):], + Doc: doc, + Code: f.Body, + Play: playExample(file, f.Body), + Comments: file.Comments, + Output: output, + EmptyOutput: output == "" && hasOutput, }) } if !hasTests && numDecl > 1 && len(flist) == 1 { @@ -79,7 +82,8 @@ func Examples(files ...*ast.File) []*Example { 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 { // test that it begins with the correct prefix text := last.Text() @@ -90,10 +94,10 @@ func exampleOutput(b *ast.BlockStmt, comments []*ast.CommentGroup) string { if len(text) > 0 && text[0] == '\n' { 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.