diff options
author | Robert Griesemer <gri@golang.org> | 2012-01-12 17:20:51 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2012-01-12 17:20:51 -0800 |
commit | 4f63cdc81ff3f401a04457036e2c08f71bab7ccf (patch) | |
tree | 0a4ef1852bbab352770279c0bc6b428f7c932572 | |
parent | 9535b86a27bb6ef585e6bafe89ba19dd1bff2cb7 (diff) | |
download | go-4f63cdc81ff3f401a04457036e2c08f71bab7ccf.tar.gz go-4f63cdc81ff3f401a04457036e2c08f71bab7ccf.zip |
go/doc: initial testing support
R=rsc, adg
CC=golang-dev
https://golang.org/cl/5533082
-rw-r--r-- | src/pkg/go/doc/doc_test.go | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/src/pkg/go/doc/doc_test.go b/src/pkg/go/doc/doc_test.go new file mode 100644 index 0000000000..39266d96e7 --- /dev/null +++ b/src/pkg/go/doc/doc_test.go @@ -0,0 +1,136 @@ +// 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 doc + +import ( + "bytes" + "fmt" + "go/ast" + "go/parser" + "go/token" + "testing" + "text/template" +) + +type sources map[string]string // filename -> file contents + +type testCase struct { + name string + importPath string + exportsOnly bool + srcs sources + doc string +} + +var tests = make(map[string]*testCase) + +// To register a new test case, use the pattern: +// +// var _ = register(&testCase{ ... }) +// +// (The result value of register is always 0 and only present to enable the pattern.) +// +func register(test *testCase) int { + if _, found := tests[test.name]; found { + panic(fmt.Sprintf("registration failed: test case %q already exists", test.name)) + } + tests[test.name] = test + return 0 +} + +func runTest(t *testing.T, test *testCase) { + // create AST + fset := token.NewFileSet() + var pkg ast.Package + pkg.Files = make(map[string]*ast.File) + for filename, src := range test.srcs { + file, err := parser.ParseFile(fset, filename, src, parser.ParseComments) + if err != nil { + t.Errorf("test %s: %v", test.name, err) + return + } + switch { + case pkg.Name == "": + pkg.Name = file.Name.Name + case pkg.Name != file.Name.Name: + t.Errorf("test %s: different package names in test files", test.name) + return + } + pkg.Files[filename] = file + } + + doc := NewPackageDoc(&pkg, test.importPath, test.exportsOnly).String() + if doc != test.doc { + t.Errorf("test %s\n\tgot : %s\n\twant: %s", test.name, doc, test.doc) + } +} + +func Test(t *testing.T) { + for _, test := range tests { + runTest(t, test) + } +} + +// ---------------------------------------------------------------------------- +// Printing support + +func (pkg *PackageDoc) String() string { + var buf bytes.Buffer + docText.Execute(&buf, pkg) // ignore error - test will fail w/ incorrect output + return buf.String() +} + +// TODO(gri) complete template +var docText = template.Must(template.New("docText").Parse( + ` +PACKAGE {{.PackageName}} +DOC {{printf "%q" .Doc}} +IMPORTPATH {{.ImportPath}} +FILENAMES {{.Filenames}} +`)) + +// ---------------------------------------------------------------------------- +// Test cases + +// Test that all package comments and bugs are collected, +// and that the importPath is correctly set. +// +var _ = register(&testCase{ + name: "p", + importPath: "p", + srcs: sources{ + "p1.go": "// comment 1\npackage p\n//BUG(uid): bug1", + "p0.go": "// comment 0\npackage p\n// BUG(uid): bug0", + }, + doc: ` +PACKAGE p +DOC "comment 1\n\ncomment 0\n" +IMPORTPATH p +FILENAMES [p0.go p1.go] +`, +}) + +// Test basic functionality. +// +var _ = register(&testCase{ + name: "p1", + importPath: "p", + srcs: sources{ + "p.go": ` +package p +import "a" +const pi = 3.14 // pi +type T struct{} // T +var V T // v +func F(x int) int {} // F +`, + }, + doc: ` +PACKAGE p +DOC "" +IMPORTPATH p +FILENAMES [p.go] +`, +}) |