OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [go/] [doc/] [doc_test.go] - Blame information for rev 775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package doc
6
 
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
        "strings"
18
        "testing"
19
        "text/template"
20
)
21
 
22
var update = flag.Bool("update", false, "update golden (.out) files")
23
 
24
const dataDir = "testdata"
25
 
26
var templateTxt = readTemplate("template.txt")
27
 
28
func readTemplate(filename string) *template.Template {
29
        t := template.New(filename)
30
        t.Funcs(template.FuncMap{
31
                "node":     nodeFmt,
32
                "synopsis": synopsisFmt,
33
        })
34
        return template.Must(t.ParseFiles(filepath.Join(dataDir, filename)))
35
}
36
 
37
func nodeFmt(node interface{}, fset *token.FileSet) string {
38
        var buf bytes.Buffer
39
        printer.Fprint(&buf, fset, node)
40
        return strings.Replace(strings.TrimSpace(buf.String()), "\n", "\n\t", -1)
41
}
42
 
43
func synopsisFmt(s string) string {
44
        const n = 64
45
        if len(s) > n {
46
                // cut off excess text and go back to a word boundary
47
                s = s[0:n]
48
                if i := strings.LastIndexAny(s, "\t\n "); i >= 0 {
49
                        s = s[0:i]
50
                }
51
                s = strings.TrimSpace(s) + " ..."
52
        }
53
        return "// " + strings.Replace(s, "\n", " ", -1)
54
}
55
 
56
func isGoFile(fi os.FileInfo) bool {
57
        name := fi.Name()
58
        return !fi.IsDir() &&
59
                len(name) > 0 && name[0] != '.' && // ignore .files
60
                filepath.Ext(name) == ".go"
61
}
62
 
63
type bundle struct {
64
        *Package
65
        FSet *token.FileSet
66
}
67
 
68
func test(t *testing.T, mode Mode) {
69
        // get all packages
70
        fset := token.NewFileSet()
71
        pkgs, err := parser.ParseDir(fset, dataDir, isGoFile, parser.ParseComments)
72
        if err != nil {
73
                t.Fatal(err)
74
        }
75
 
76
        // test all packages
77
        for _, pkg := range pkgs {
78
                importpath := dataDir + "/" + pkg.Name
79
                doc := New(pkg, importpath, mode)
80
 
81
                // golden files always use / in filenames - canonicalize them
82
                for i, filename := range doc.Filenames {
83
                        doc.Filenames[i] = filepath.ToSlash(filename)
84
                }
85
 
86
                // print documentation
87
                var buf bytes.Buffer
88
                if err := templateTxt.Execute(&buf, bundle{doc, fset}); err != nil {
89
                        t.Error(err)
90
                        continue
91
                }
92
                got := buf.Bytes()
93
 
94
                // update golden file if necessary
95
                golden := filepath.Join(dataDir, fmt.Sprintf("%s.%d.golden", pkg.Name, mode))
96
                if *update {
97
                        err := ioutil.WriteFile(golden, got, 0644)
98
                        if err != nil {
99
                                t.Error(err)
100
                        }
101
                        continue
102
                }
103
 
104
                // get golden file
105
                want, err := ioutil.ReadFile(golden)
106
                if err != nil {
107
                        t.Error(err)
108
                        continue
109
                }
110
 
111
                // compare
112
                if bytes.Compare(got, want) != 0 {
113
                        t.Errorf("package %s\n\tgot:\n%s\n\twant:\n%s", pkg.Name, got, want)
114
                }
115
        }
116
}
117
 
118
func Test(t *testing.T) {
119
        test(t, 0)
120
        test(t, AllDecls)
121
        test(t, AllMethods)
122
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.