-
Notifications
You must be signed in to change notification settings - Fork 7
/
link_test.go
122 lines (114 loc) · 2.65 KB
/
link_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"go/doc/comment"
"testing"
"text/template"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDocLinker(t *testing.T) {
t.Parallel()
var linker docLinker
linker.LocalPackage("example.com/foo")
linker.LocalPackage("example.com/bar")
linker.Template("foo.whatever/baz/qux",
requireParseTemplate(t, "https://mygodoc.example.com/{{.ImportPath}}"))
linker.Template("foo.whatever/baz/qux/quux",
requireParseTemplate(t, "https://godocs.io/{{.ImportPath}}"))
tests := []struct {
desc string
from string
link comment.DocLink
want string
}{
{
desc: "default/another package",
link: comment.DocLink{
ImportPath: "golang.org/x/net",
},
want: "https://pkg.go.dev/golang.org/x/net",
},
{
desc: "entity in another package",
link: comment.DocLink{
ImportPath: "golang.org/x/net/context",
Name: "Context",
},
want: "https://pkg.go.dev/golang.org/x/net/context#Context",
},
{
desc: "method in another package",
link: comment.DocLink{
ImportPath: "golang.org/x/tools/go/packages",
Recv: "Package",
Name: "String",
},
want: "https://pkg.go.dev/golang.org/x/tools/go/packages#Package.String",
},
{
desc: "entity in the same package",
link: comment.DocLink{
Name: "Foo",
},
want: "#Foo",
},
{
desc: "method in the same package",
link: comment.DocLink{
Recv: "Foo",
Name: "Bar",
},
want: "#Foo.Bar",
},
{
desc: "local package",
from: "example.com/foo",
link: comment.DocLink{
ImportPath: "example.com/bar",
},
want: "../bar",
},
{
desc: "local package/unknown submodule",
from: "example.com/foo",
link: comment.DocLink{
ImportPath: "example.com/bar/baz",
},
want: "https://pkg.go.dev/example.com/bar/baz",
},
{
desc: "template",
link: comment.DocLink{
ImportPath: "foo.whatever/baz/qux",
},
want: "https://mygodoc.example.com/foo.whatever/baz/qux",
},
{
desc: "template/subpackage",
link: comment.DocLink{
ImportPath: "foo.whatever/baz/qux/foo",
},
want: "https://mygodoc.example.com/foo.whatever/baz/qux/foo",
},
{
desc: "template/override",
link: comment.DocLink{
ImportPath: "foo.whatever/baz/qux/quux",
},
want: "https://godocs.io/foo.whatever/baz/qux/quux",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
got := linker.DocLinkURL(tt.from, &tt.link)
assert.Equal(t, tt.want, got)
})
}
}
func requireParseTemplate(t *testing.T, s string) *template.Template {
tmpl, err := template.New("").Parse(s)
require.NoError(t, err)
return tmpl
}