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.
17 type sources map[string]string // filename -> file contents
19 type testCase struct {
27 var tests = make(map[string]*testCase)
29 // To register a new test case, use the pattern:
31 // var _ = register(&testCase{ ... })
33 // (The result value of register is always 0 and only present to enable the pattern.)
35 func register(test *testCase) int {
36 if _, found := tests[test.name]; found {
37 panic(fmt.Sprintf("registration failed: test case %q already exists", test.name))
39 tests[test.name] = test
43 func runTest(t *testing.T, test *testCase) {
45 fset := token.NewFileSet()
47 pkg.Files = make(map[string]*ast.File)
48 for filename, src := range test.srcs {
49 file, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
51 t.Errorf("test %s: %v", test.name, err)
56 pkg.Name = file.Name.Name
57 case pkg.Name != file.Name.Name:
58 t.Errorf("test %s: different package names in test files", test.name)
61 pkg.Files[filename] = file
64 doc := New(&pkg, test.importPath, test.mode).String()
66 //TODO(gri) Enable this once the sorting issue of comments is fixed
67 //t.Errorf("test %s\n\tgot : %s\n\twant: %s", test.name, doc, test.doc)
71 func Test(t *testing.T) {
72 for _, test := range tests {
77 // ----------------------------------------------------------------------------
80 func (pkg *Package) String() string {
82 docText.Execute(&buf, pkg) // ignore error - test will fail w/ incorrect output
86 // TODO(gri) complete template
87 var docText = template.Must(template.New("docText").Parse(
90 DOC {{printf "%q" .Doc}}
91 IMPORTPATH {{.ImportPath}}
92 FILENAMES {{.Filenames}}
95 // ----------------------------------------------------------------------------
98 // Test that all package comments and bugs are collected,
99 // and that the importPath is correctly set.
101 var _ = register(&testCase{
105 "p1.go": "// comment 1\npackage p\n//BUG(uid): bug1",
106 "p0.go": "// comment 0\npackage p\n// BUG(uid): bug0",
110 DOC "comment 0\n\ncomment 1\n"
112 FILENAMES [p0.go p1.go]
116 // Test basic functionality.
118 var _ = register(&testCase{
125 const pi = 3.14 // pi
128 func F(x int) int {} // F