1// Copyright 2018 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
5package godoc
6
7import (
8	"testing"
9
10	"golang.org/x/tools/godoc/vfs/mapfs"
11)
12
13// TestIgnoredGoFiles tests the scenario where a folder has no .go or .c files,
14// but has an ignored go file.
15func TestIgnoredGoFiles(t *testing.T) {
16	packagePath := "github.com/package"
17	packageComment := "main is documented in an ignored .go file"
18
19	c := NewCorpus(mapfs.New(map[string]string{
20		"src/" + packagePath + "/ignored.go": `// +build ignore
21
22// ` + packageComment + `
23package main`}))
24	srv := &handlerServer{
25		p: &Presentation{
26			Corpus: c,
27		},
28		c: c,
29	}
30	pInfo := srv.GetPageInfo("/src/"+packagePath, packagePath, NoFiltering, "linux", "amd64")
31
32	if pInfo.PDoc == nil {
33		t.Error("pInfo.PDoc = nil; want non-nil.")
34	} else {
35		if got, want := pInfo.PDoc.Doc, packageComment+"\n"; got != want {
36			t.Errorf("pInfo.PDoc.Doc = %q; want %q.", got, want)
37		}
38		if got, want := pInfo.PDoc.Name, "main"; got != want {
39			t.Errorf("pInfo.PDoc.Name = %q; want %q.", got, want)
40		}
41		if got, want := pInfo.PDoc.ImportPath, packagePath; got != want {
42			t.Errorf("pInfo.PDoc.ImportPath = %q; want %q.", got, want)
43		}
44	}
45	if pInfo.FSet == nil {
46		t.Error("pInfo.FSet = nil; want non-nil.")
47	}
48}
49
50func TestIssue5247(t *testing.T) {
51	const packagePath = "example.com/p"
52	c := NewCorpus(mapfs.New(map[string]string{
53		"src/" + packagePath + "/p.go": `package p
54
55//line notgen.go:3
56// F doc //line 1 should appear
57// line 2 should appear
58func F()
59//line foo.go:100`})) // No newline at end to check corner cases.
60
61	srv := &handlerServer{
62		p: &Presentation{Corpus: c},
63		c: c,
64	}
65	pInfo := srv.GetPageInfo("/src/"+packagePath, packagePath, 0, "linux", "amd64")
66	if got, want := pInfo.PDoc.Funcs[0].Doc, "F doc //line 1 should appear\nline 2 should appear\n"; got != want {
67		t.Errorf("pInfo.PDoc.Funcs[0].Doc = %q; want %q", got, want)
68	}
69}
70