1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
23 var update
= flag
.Bool("update", false, "update golden (.out) files")
24 var files
= flag
.String("files", "", "consider only Go test files matching this regular expression")
26 const dataDir
= "testdata"
28 var templateTxt
= readTemplate("template.txt")
30 func readTemplate(filename
string) *template
.Template
{
31 t
:= template
.New(filename
)
32 t
.Funcs(template
.FuncMap
{
34 "synopsis": synopsisFmt
,
36 return template
.Must(t
.ParseFiles(filepath
.Join(dataDir
, filename
)))
39 func nodeFmt(node
interface{}, fset
*token
.FileSet
) string {
41 printer
.Fprint(&buf
, fset
, node
)
42 return strings
.Replace(strings
.TrimSpace(buf
.String()), "\n", "\n\t", -1)
45 func synopsisFmt(s
string) string {
48 // cut off excess text and go back to a word boundary
50 if i
:= strings
.LastIndexAny(s
, "\t\n "); i
>= 0 {
53 s
= strings
.TrimSpace(s
) + " ..."
55 return "// " + strings
.Replace(s
, "\n", " ", -1)
58 func isGoFile(fi os
.FileInfo
) bool {
61 len(name
) > 0 && name
[0] != '.' && // ignore .files
62 filepath
.Ext(name
) == ".go"
70 func test(t
*testing
.T
, mode Mode
) {
71 // determine file filter
74 rx
, err
:= regexp
.Compile(*files
)
78 filter
= func(fi os
.FileInfo
) bool {
79 return isGoFile(fi
) && rx
.MatchString(fi
.Name())
84 fset
:= token
.NewFileSet()
85 pkgs
, err
:= parser
.ParseDir(fset
, dataDir
, filter
, parser
.ParseComments
)
91 for _
, pkg
:= range pkgs
{
92 importpath
:= dataDir
+ "/" + pkg
.Name
93 doc
:= New(pkg
, importpath
, mode
)
95 // golden files always use / in filenames - canonicalize them
96 for i
, filename
:= range doc
.Filenames
{
97 doc
.Filenames
[i
] = filepath
.ToSlash(filename
)
100 // print documentation
102 if err
:= templateTxt
.Execute(&buf
, bundle
{doc
, fset
}); err
!= nil {
108 // update golden file if necessary
109 golden
:= filepath
.Join(dataDir
, fmt
.Sprintf("%s.%d.golden", pkg
.Name
, mode
))
111 err
:= ioutil
.WriteFile(golden
, got
, 0644)
119 want
, err
:= ioutil
.ReadFile(golden
)
126 if !bytes
.Equal(got
, want
) {
127 t
.Errorf("package %s\n\tgot:\n%s\n\twant:\n%s", pkg
.Name
, got
, want
)
132 func Test(t
*testing
.T
) {