Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.5.2 / internal / regtest / link_test.go
1 // Copyright 2020 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 regtest
6
7 import (
8         "strings"
9         "testing"
10
11         "golang.org/x/tools/internal/testenv"
12 )
13
14 func TestHoverAndDocumentLink(t *testing.T) {
15         testenv.NeedsGo1Point(t, 13)
16         const program = `
17 -- go.mod --
18 module mod.test
19
20 go 1.12
21
22 require import.test v1.2.3
23 -- main.go --
24 package main
25
26 import "import.test/pkg"
27
28 func main() {
29         println(pkg.Hello)
30 }`
31
32         const proxy = `
33 -- import.test@v1.2.3/go.mod --
34 module import.test
35
36 go 1.12
37 -- import.test@v1.2.3/pkg/const.go --
38 package pkg
39
40 const Hello = "Hello"
41 `
42         runner.Run(t, program, func(t *testing.T, env *Env) {
43                 env.OpenFile("main.go")
44                 env.OpenFile("go.mod")
45
46                 modLink := "https://pkg.go.dev/mod/import.test@v1.2.3"
47                 pkgLink := "https://pkg.go.dev/import.test@v1.2.3/pkg"
48
49                 // First, check that we get the expected links via hover and documentLink.
50                 content, _ := env.Hover("main.go", env.RegexpSearch("main.go", "pkg.Hello"))
51                 if content == nil || !strings.Contains(content.Value, pkgLink) {
52                         t.Errorf("hover: got %v in main.go, want contains %q", content, pkgLink)
53                 }
54                 content, _ = env.Hover("go.mod", env.RegexpSearch("go.mod", "import.test"))
55                 if content == nil || !strings.Contains(content.Value, pkgLink) {
56                         t.Errorf("hover: got %v in go.mod, want contains %q", content, pkgLink)
57                 }
58                 links := env.DocumentLink("main.go")
59                 if len(links) != 1 || links[0].Target != pkgLink {
60                         t.Errorf("documentLink: got %v for main.go, want link to %q", links, pkgLink)
61                 }
62                 links = env.DocumentLink("go.mod")
63                 if len(links) != 1 || links[0].Target != modLink {
64                         t.Errorf("documentLink: got %v for go.mod, want link to %q", links, modLink)
65                 }
66
67                 // Then change the environment to make these links private.
68                 env.ChangeEnv(map[string]string{"GOPRIVATE": "import.test"})
69
70                 // Finally, verify that the links are gone.
71                 content, _ = env.Hover("main.go", env.RegexpSearch("main.go", "pkg.Hello"))
72                 if content == nil || strings.Contains(content.Value, pkgLink) {
73                         t.Errorf("hover: got %v in main.go, want non-empty hover without %q", content, pkgLink)
74                 }
75                 content, _ = env.Hover("go.mod", env.RegexpSearch("go.mod", "import.test"))
76                 if content == nil || strings.Contains(content.Value, modLink) {
77                         t.Errorf("hover: got %v in go.mod, want contains %q", content, modLink)
78                 }
79                 links = env.DocumentLink("main.go")
80                 if len(links) != 0 {
81                         t.Errorf("documentLink: got %d document links for main.go, want 0\nlinks: %v", len(links), links)
82                 }
83                 links = env.DocumentLink("go.mod")
84                 if len(links) != 0 {
85                         t.Errorf("documentLink: got %d document links for go.mod, want 0\nlinks: %v", len(links), links)
86                 }
87         }, WithProxyFiles(proxy))
88 }