Support examples as tests
go test supports running examples as tests and verifying their output, add support for running them to gotestmain. Change-Id: If51abc64467d4701195cefc6db513f4d008794b5
This commit is contained in:
parent
33cfa1c98a
commit
463eab7950
1 changed files with 18 additions and 5 deletions
|
@ -19,6 +19,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"go/doc"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -39,13 +40,14 @@ var (
|
||||||
type data struct {
|
type data struct {
|
||||||
Package string
|
Package string
|
||||||
Tests []string
|
Tests []string
|
||||||
|
Examples []*doc.Example
|
||||||
HasMain bool
|
HasMain bool
|
||||||
MainStartTakesInterface bool
|
MainStartTakesInterface bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func findTests(srcs []string) (tests []string, hasMain bool) {
|
func findTests(srcs []string) (tests []string, examples []*doc.Example, hasMain bool) {
|
||||||
for _, src := range srcs {
|
for _, src := range srcs {
|
||||||
f, err := parser.ParseFile(token.NewFileSet(), src, nil, 0)
|
f, err := parser.ParseFile(token.NewFileSet(), src, nil, parser.ParseComments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -59,6 +61,8 @@ func findTests(srcs []string) (tests []string, hasMain bool) {
|
||||||
tests = append(tests, obj.Name)
|
tests = append(tests, obj.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
examples = append(examples, doc.Examples(f)...)
|
||||||
}
|
}
|
||||||
sort.Strings(tests)
|
sort.Strings(tests)
|
||||||
return
|
return
|
||||||
|
@ -81,11 +85,12 @@ func main() {
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
tests, hasMain := findTests(flag.Args())
|
tests, examples, hasMain := findTests(flag.Args())
|
||||||
|
|
||||||
d := data{
|
d := data{
|
||||||
Package: *pkg,
|
Package: *pkg,
|
||||||
Tests: tests,
|
Tests: tests,
|
||||||
|
Examples: examples,
|
||||||
HasMain: hasMain,
|
HasMain: hasMain,
|
||||||
MainStartTakesInterface: mainStartTakesInterface(),
|
MainStartTakesInterface: mainStartTakesInterface(),
|
||||||
}
|
}
|
||||||
|
@ -121,6 +126,14 @@ var t = []testing.InternalTest{
|
||||||
{{end}}
|
{{end}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var e = []testing.InternalExample{
|
||||||
|
{{range .Examples}}
|
||||||
|
{{if or .Output .EmptyOutput}}
|
||||||
|
{"{{.Name}}", pkg.Example{{.Name}}, {{.Output | printf "%q" }}, {{.Unordered}}},
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
}
|
||||||
|
|
||||||
var matchPat string
|
var matchPat string
|
||||||
var matchRe *regexp.Regexp
|
var matchRe *regexp.Regexp
|
||||||
|
|
||||||
|
@ -170,9 +183,9 @@ func (matchString) StopTestLog() error {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
{{if .MainStartTakesInterface}}
|
{{if .MainStartTakesInterface}}
|
||||||
m := testing.MainStart(matchString{}, t, nil, nil)
|
m := testing.MainStart(matchString{}, t, nil, e)
|
||||||
{{else}}
|
{{else}}
|
||||||
m := testing.MainStart(MatchString, t, nil, nil)
|
m := testing.MainStart(MatchString, t, nil, e)
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if .HasMain}}
|
{{if .HasMain}}
|
||||||
pkg.TestMain(m)
|
pkg.TestMain(m)
|
||||||
|
|
Loading…
Reference in a new issue