URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [go/] [doc/] [doc_test.go] - Rev 747
Compare with Previous | Blame | View Log
// Copyright 2012 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package docimport ("bytes""flag""fmt""go/parser""go/printer""go/token""io/ioutil""os""path/filepath""strings""testing""text/template")var update = flag.Bool("update", false, "update golden (.out) files")const dataDir = "testdata"var templateTxt = readTemplate("template.txt")func readTemplate(filename string) *template.Template {t := template.New(filename)t.Funcs(template.FuncMap{"node": nodeFmt,"synopsis": synopsisFmt,})return template.Must(t.ParseFiles(filepath.Join(dataDir, filename)))}func nodeFmt(node interface{}, fset *token.FileSet) string {var buf bytes.Bufferprinter.Fprint(&buf, fset, node)return strings.Replace(strings.TrimSpace(buf.String()), "\n", "\n\t", -1)}func synopsisFmt(s string) string {const n = 64if len(s) > n {// cut off excess text and go back to a word boundarys = s[0:n]if i := strings.LastIndexAny(s, "\t\n "); i >= 0 {s = s[0:i]}s = strings.TrimSpace(s) + " ..."}return "// " + strings.Replace(s, "\n", " ", -1)}func isGoFile(fi os.FileInfo) bool {name := fi.Name()return !fi.IsDir() &&len(name) > 0 && name[0] != '.' && // ignore .filesfilepath.Ext(name) == ".go"}type bundle struct {*PackageFSet *token.FileSet}func test(t *testing.T, mode Mode) {// get all packagesfset := token.NewFileSet()pkgs, err := parser.ParseDir(fset, dataDir, isGoFile, parser.ParseComments)if err != nil {t.Fatal(err)}// test all packagesfor _, pkg := range pkgs {importpath := dataDir + "/" + pkg.Namedoc := New(pkg, importpath, mode)// golden files always use / in filenames - canonicalize themfor i, filename := range doc.Filenames {doc.Filenames[i] = filepath.ToSlash(filename)}// print documentationvar buf bytes.Bufferif err := templateTxt.Execute(&buf, bundle{doc, fset}); err != nil {t.Error(err)continue}got := buf.Bytes()// update golden file if necessarygolden := filepath.Join(dataDir, fmt.Sprintf("%s.%d.golden", pkg.Name, mode))if *update {err := ioutil.WriteFile(golden, got, 0644)if err != nil {t.Error(err)}continue}// get golden filewant, err := ioutil.ReadFile(golden)if err != nil {t.Error(err)continue}// compareif bytes.Compare(got, want) != 0 {t.Errorf("package %s\n\tgot:\n%s\n\twant:\n%s", pkg.Name, got, want)}}}func Test(t *testing.T) {test(t, 0)test(t, AllDecls)test(t, AllMethods)}
