github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/go/doc/doc_test.go (about)

     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.
     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  	"regexp"
    18  	"strings"
    19  	"testing"
    20  	"text/template"
    21  )
    22  
    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")
    25  
    26  const dataDir = "testdata"
    27  
    28  var templateTxt = readTemplate("template.txt")
    29  
    30  func readTemplate(filename string) *template.Template {
    31  	t := template.New(filename)
    32  	t.Funcs(template.FuncMap{
    33  		"node":     nodeFmt,
    34  		"synopsis": synopsisFmt,
    35  	})
    36  	return template.Must(t.ParseFiles(filepath.Join(dataDir, filename)))
    37  }
    38  
    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)
    43  }
    44  
    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]
    52  		}
    53  		s = strings.TrimSpace(s) + " ..."
    54  	}
    55  	return "// " + strings.Replace(s, "\n", " ", -1)
    56  }
    57  
    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"
    63  }
    64  
    65  type bundle struct {
    66  	*Package
    67  	FSet *token.FileSet
    68  }
    69  
    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)
    77  		}
    78  		filter = func(fi os.FileInfo) bool {
    79  			return isGoFile(fi) && rx.MatchString(fi.Name())
    80  		}
    81  	}
    82  
    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)
    88  	}
    89  
    90  	// test packages
    91  	for _, pkg := range pkgs {
    92  		importpath := dataDir + "/" + pkg.Name
    93  		doc := New(pkg, importpath, mode)
    94  
    95  		// golden files always use / in filenames - canonicalize them
    96  		for i, filename := range doc.Filenames {
    97  			doc.Filenames[i] = filepath.ToSlash(filename)
    98  		}
    99  
   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
   105  		}
   106  		got := buf.Bytes()
   107  
   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)
   114  			}
   115  			continue
   116  		}
   117  
   118  		// get golden file
   119  		want, err := ioutil.ReadFile(golden)
   120  		if err != nil {
   121  			t.Error(err)
   122  			continue
   123  		}
   124  
   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)
   128  		}
   129  	}
   130  }
   131  
   132  func Test(t *testing.T) {
   133  	test(t, 0)
   134  	test(t, AllDecls)
   135  	test(t, AllMethods)
   136  }