OSDN Git Service

libgo: Update to weekly.2012-01-15.
[pf3gnuchains/gcc-fork.git] / libgo / go / go / doc / doc_test.go
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         "fmt"
10         "go/ast"
11         "go/parser"
12         "go/token"
13         "testing"
14         "text/template"
15 )
16
17 type sources map[string]string // filename -> file contents
18
19 type testCase struct {
20         name       string
21         importPath string
22         mode       Mode
23         srcs       sources
24         doc        string
25 }
26
27 var tests = make(map[string]*testCase)
28
29 // To register a new test case, use the pattern:
30 //
31 //      var _ = register(&testCase{ ... })
32 //
33 // (The result value of register is always 0 and only present to enable the pattern.)
34 //
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))
38         }
39         tests[test.name] = test
40         return 0
41 }
42
43 func runTest(t *testing.T, test *testCase) {
44         // create AST
45         fset := token.NewFileSet()
46         var pkg ast.Package
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)
50                 if err != nil {
51                         t.Errorf("test %s: %v", test.name, err)
52                         return
53                 }
54                 switch {
55                 case pkg.Name == "":
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)
59                         return
60                 }
61                 pkg.Files[filename] = file
62         }
63
64         doc := New(&pkg, test.importPath, test.mode).String()
65         if doc != test.doc {
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)
68         }
69 }
70
71 func Test(t *testing.T) {
72         for _, test := range tests {
73                 runTest(t, test)
74         }
75 }
76
77 // ----------------------------------------------------------------------------
78 // Printing support
79
80 func (pkg *Package) String() string {
81         var buf bytes.Buffer
82         docText.Execute(&buf, pkg) // ignore error - test will fail w/ incorrect output
83         return buf.String()
84 }
85
86 // TODO(gri) complete template
87 var docText = template.Must(template.New("docText").Parse(
88         `
89 PACKAGE {{.Name}}
90 DOC {{printf "%q" .Doc}}
91 IMPORTPATH {{.ImportPath}}
92 FILENAMES {{.Filenames}}
93 `))
94
95 // ----------------------------------------------------------------------------
96 // Test cases
97
98 // Test that all package comments and bugs are collected,
99 // and that the importPath is correctly set.
100 //
101 var _ = register(&testCase{
102         name:       "p",
103         importPath: "p",
104         srcs: sources{
105                 "p1.go": "// comment 1\npackage p\n//BUG(uid): bug1",
106                 "p0.go": "// comment 0\npackage p\n// BUG(uid): bug0",
107         },
108         doc: `
109 PACKAGE p
110 DOC "comment 0\n\ncomment 1\n"
111 IMPORTPATH p
112 FILENAMES [p0.go p1.go]
113 `,
114 })
115
116 // Test basic functionality.
117 //
118 var _ = register(&testCase{
119         name:       "p1",
120         importPath: "p",
121         srcs: sources{
122                 "p.go": `
123 package p
124 import "a"
125 const pi = 3.14       // pi
126 type T struct{}       // T
127 var V T               // v
128 func F(x int) int {}  // F
129 `,
130         },
131         doc: `
132 PACKAGE p
133 DOC ""
134 IMPORTPATH p
135 FILENAMES [p.go]
136 `,
137 })