func newCorpus(t *testing.T) *Corpus { c := NewCorpus(mapfs.New(map[string]string{ "src/pkg/foo/foo.go": `// Package foo is an example. package foo import "bar" const Pi = 3.1415 var Foos []Foo // Foo is stuff. type Foo struct{} func New() *Foo { return new(Foo) } `, "src/pkg/bar/bar.go": `// Package bar is another example to test races. package bar `, "src/pkg/other/bar/bar.go": `// Package bar is another bar package. package bar func X() {} `, "src/pkg/skip/skip.go": `// Package skip should be skipped. package skip func Skip() {} `, "src/pkg/bar/readme.txt": `Whitelisted text file. `, "src/pkg/bar/baz.zzz": `Text file not whitelisted. `, })) c.IndexEnabled = true c.IndexDirectory = func(dir string) bool { return !strings.Contains(dir, "skip") } if err := c.Init(); err != nil { t.Fatal(err) } return c }
func TestCommandLine(t *testing.T) { cleanup := setupGoroot(t) defer cleanup() mfs := mapfs.New(map[string]string{ "src/pkg/bar/bar.go": `// Package bar is an example. package bar `, "src/pkg/foo/foo.go": `// Package foo. package foo // First function is first. func First() { } // Second function is second. func Second() { } `, "src/pkg/gen/gen.go": `// Package gen package gen //line notgen.go:3 // F doc //line 1 should appear // line 2 should appear func F() //line foo.go:100`, // no newline on end to check corner cases! "src/pkg/vet/vet.go": `// Package vet package vet `, "src/cmd/go/doc.go": `// The go command package main `, "src/cmd/gofmt/doc.go": `// The gofmt command package main `, "src/cmd/vet/vet.go": `// The vet command package main `, }) fs := make(vfs.NameSpace) fs.Bind("/", mfs, "/", vfs.BindReplace) c := NewCorpus(fs) p := &Presentation{Corpus: c} p.cmdHandler = handlerServer{p, c, "/cmd/", "/src/cmd"} p.pkgHandler = handlerServer{p, c, "/pkg/", "/src/pkg"} p.initFuncMap() p.PackageText = template.Must(template.New("PackageText").Funcs(p.FuncMap()).Parse(`{{$info := .}}{{$filtered := .IsFiltered}}{{if $filtered}}{{range .PAst}}{{range .Decls}}{{node $info .}}{{end}}{{end}}{{else}}{{with .PAst}}{{range $filename, $ast := .}}{{$filename}}: {{node $ $ast}}{{end}}{{end}}{{end}}{{with .PDoc}}{{if $.IsMain}}COMMAND {{.Doc}}{{else}}PACKAGE {{.Doc}}{{end}}{{with .Funcs}} {{range .}}{{node $ .Decl}} {{comment_text .Doc " " "\t"}}{{end}}{{end}}{{end}}`)) for _, tc := range []struct { desc string args []string exp string err bool }{ { desc: "standard package", args: []string{"fmt"}, exp: "PACKAGE Package fmt implements formatted I/O.\n", }, { desc: "package", args: []string{"bar"}, exp: "PACKAGE Package bar is an example.\n", }, { desc: "package w. filter", args: []string{"foo", "First"}, exp: "PACKAGE \nfunc First()\n First function is first.\n", }, { desc: "package w. bad filter", args: []string{"foo", "DNE"}, exp: "PACKAGE ", }, { desc: "source mode", args: []string{"src/bar"}, exp: "bar/bar.go:\n// Package bar is an example.\npackage bar\n", }, { desc: "source mode w. filter", args: []string{"src/foo", "Second"}, exp: "// Second function is second.\nfunc Second() {\n}", }, { desc: "package w. //line comments", args: []string{"gen", "F"}, exp: "PACKAGE \nfunc F()\n F doc //line 1 should appear line 2 should appear\n", }, { desc: "command", args: []string{"go"}, exp: "COMMAND The go command\n", }, { desc: "forced command", args: []string{"cmd/gofmt"}, exp: "COMMAND The gofmt command\n", }, { desc: "bad arg", args: []string{"doesnotexist"}, err: true, }, { desc: "both command and package", args: []string{"vet"}, exp: "use 'godoc cmd/vet' for documentation on the vet command \n\nPACKAGE Package vet\n", }, { desc: "root directory", args: []string{"/"}, exp: "", }, } { w := new(bytes.Buffer) err := CommandLine(w, fs, p, tc.args) if got, want := w.String(), tc.exp; got != want || tc.err == (err == nil) { t.Errorf("%s: CommandLine(%v) = %q (%v); want %q (%v)", tc.desc, tc.args, got, err, want, tc.err) } } }