Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / godoc / server_test.go
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
5 package godoc
6
7 import (
8         "net/http"
9         "net/http/httptest"
10         "net/url"
11         "strings"
12         "testing"
13         "text/template"
14
15         "golang.org/x/tools/godoc/vfs/mapfs"
16 )
17
18 // TestIgnoredGoFiles tests the scenario where a folder has no .go or .c files,
19 // but has an ignored go file.
20 func TestIgnoredGoFiles(t *testing.T) {
21         packagePath := "github.com/package"
22         packageComment := "main is documented in an ignored .go file"
23
24         c := NewCorpus(mapfs.New(map[string]string{
25                 "src/" + packagePath + "/ignored.go": `// +build ignore
26
27 // ` + packageComment + `
28 package main`}))
29         srv := &handlerServer{
30                 p: &Presentation{
31                         Corpus: c,
32                 },
33                 c: c,
34         }
35         pInfo := srv.GetPageInfo("/src/"+packagePath, packagePath, NoFiltering, "linux", "amd64")
36
37         if pInfo.PDoc == nil {
38                 t.Error("pInfo.PDoc = nil; want non-nil.")
39         } else {
40                 if got, want := pInfo.PDoc.Doc, packageComment+"\n"; got != want {
41                         t.Errorf("pInfo.PDoc.Doc = %q; want %q.", got, want)
42                 }
43                 if got, want := pInfo.PDoc.Name, "main"; got != want {
44                         t.Errorf("pInfo.PDoc.Name = %q; want %q.", got, want)
45                 }
46                 if got, want := pInfo.PDoc.ImportPath, packagePath; got != want {
47                         t.Errorf("pInfo.PDoc.ImportPath = %q; want %q.", got, want)
48                 }
49         }
50         if pInfo.FSet == nil {
51                 t.Error("pInfo.FSet = nil; want non-nil.")
52         }
53 }
54
55 func TestIssue5247(t *testing.T) {
56         const packagePath = "example.com/p"
57         c := NewCorpus(mapfs.New(map[string]string{
58                 "src/" + packagePath + "/p.go": `package p
59
60 //line notgen.go:3
61 // F doc //line 1 should appear
62 // line 2 should appear
63 func F()
64 //line foo.go:100`})) // No newline at end to check corner cases.
65
66         srv := &handlerServer{
67                 p: &Presentation{Corpus: c},
68                 c: c,
69         }
70         pInfo := srv.GetPageInfo("/src/"+packagePath, packagePath, 0, "linux", "amd64")
71         if got, want := pInfo.PDoc.Funcs[0].Doc, "F doc //line 1 should appear\nline 2 should appear\n"; got != want {
72                 t.Errorf("pInfo.PDoc.Funcs[0].Doc = %q; want %q", got, want)
73         }
74 }
75
76 func TestRedirectAndMetadata(t *testing.T) {
77         c := NewCorpus(mapfs.New(map[string]string{
78                 "doc/y/index.html": "Hello, y.",
79                 "doc/x/index.html": `<!--{
80                 "Path": "/doc/x/"
81 }-->
82
83 Hello, x.
84 `}))
85         c.updateMetadata()
86         p := &Presentation{
87                 Corpus:    c,
88                 GodocHTML: template.Must(template.New("").Parse(`{{printf "%s" .Body}}`)),
89         }
90         r := &http.Request{URL: &url.URL{}}
91
92         // Test that redirect is sent back correctly.
93         // Used to panic. See golang.org/issue/40665.
94         for _, elem := range []string{"x", "y"} {
95                 dir := "/doc/" + elem + "/"
96                 r.URL.Path = dir + "index.html"
97                 rw := httptest.NewRecorder()
98                 p.ServeFile(rw, r)
99                 loc := rw.Result().Header.Get("Location")
100                 if rw.Code != 301 || loc != dir {
101                         t.Errorf("GET %s: expected 301 -> %q, got %d -> %q", r.URL.Path, dir, rw.Code, loc)
102                 }
103
104                 r.URL.Path = dir
105                 rw = httptest.NewRecorder()
106                 p.ServeFile(rw, r)
107                 if rw.Code != 200 || !strings.Contains(rw.Body.String(), "Hello, "+elem) {
108                         t.Fatalf("GET %s: expected 200 w/ Hello, %s: got %d w/ body:\n%s",
109                                 r.URL.Path, elem, rw.Code, rw.Body)
110                 }
111         }
112 }