* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / libgo / go / go / doc / doc_test.go
blob8043038b4aefdb37efdc8b1d10bdeb1a225e920c
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.
5 package doc
7 import (
8 "bytes"
9 "flag"
10 "fmt"
11 "go/parser"
12 "go/printer"
13 "go/token"
14 "io/ioutil"
15 "os"
16 "path/filepath"
17 "regexp"
18 "strings"
19 "testing"
20 "text/template"
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{
33 "node": nodeFmt,
34 "synopsis": synopsisFmt,
36 return template.Must(t.ParseFiles(filepath.Join(dataDir, filename)))
39 func nodeFmt(node interface{}, fset *token.FileSet) string {
40 var buf bytes.Buffer
41 printer.Fprint(&buf, fset, node)
42 return strings.Replace(strings.TrimSpace(buf.String()), "\n", "\n\t", -1)
45 func synopsisFmt(s string) string {
46 const n = 64
47 if len(s) > n {
48 // cut off excess text and go back to a word boundary
49 s = s[0:n]
50 if i := strings.LastIndexAny(s, "\t\n "); i >= 0 {
51 s = s[0:i]
53 s = strings.TrimSpace(s) + " ..."
55 return "// " + strings.Replace(s, "\n", " ", -1)
58 func isGoFile(fi os.FileInfo) bool {
59 name := fi.Name()
60 return !fi.IsDir() &&
61 len(name) > 0 && name[0] != '.' && // ignore .files
62 filepath.Ext(name) == ".go"
65 type bundle struct {
66 *Package
67 FSet *token.FileSet
70 func test(t *testing.T, mode Mode) {
71 // determine file filter
72 filter := isGoFile
73 if *files != "" {
74 rx, err := regexp.Compile(*files)
75 if err != nil {
76 t.Fatal(err)
78 filter = func(fi os.FileInfo) bool {
79 return isGoFile(fi) && rx.MatchString(fi.Name())
83 // get packages
84 fset := token.NewFileSet()
85 pkgs, err := parser.ParseDir(fset, dataDir, filter, parser.ParseComments)
86 if err != nil {
87 t.Fatal(err)
90 // test packages
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
101 var buf bytes.Buffer
102 if err := templateTxt.Execute(&buf, bundle{doc, fset}); err != nil {
103 t.Error(err)
104 continue
106 got := buf.Bytes()
108 // update golden file if necessary
109 golden := filepath.Join(dataDir, fmt.Sprintf("%s.%d.golden", pkg.Name, mode))
110 if *update {
111 err := ioutil.WriteFile(golden, got, 0644)
112 if err != nil {
113 t.Error(err)
115 continue
118 // get golden file
119 want, err := ioutil.ReadFile(golden)
120 if err != nil {
121 t.Error(err)
122 continue
125 // compare
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) {
133 test(t, 0)
134 test(t, AllDecls)
135 test(t, AllMethods)