forked from zmb3/gogetdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ident_test.go
117 lines (102 loc) · 3.01 KB
/
ident_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
package main
import (
"go/token"
"path/filepath"
"runtime"
"strings"
"testing"
"golang.org/x/tools/go/packages/packagestest"
)
func TestIdent(t *testing.T) {
dir := filepath.Join(".", "testdata", "package")
mods := []packagestest.Module{
{Name: "somepkg", Files: packagestest.MustCopyFileTree(dir)},
}
packagestest.TestAll(t, func(t *testing.T, exporter packagestest.Exporter) {
if exporter == packagestest.Modules && !modulesSupported() {
t.Skip("Skipping modules test on", runtime.Version())
}
exported := packagestest.Export(t, exporter, mods)
defer exported.Cleanup()
teardown := setup(exported.Config)
defer teardown()
getDoc := func(p token.Position) *Doc {
t.Helper()
doc, docErr := Run(p.Filename, p.Offset, nil)
if docErr != nil {
t.Fatal(docErr)
}
return doc
}
pcmp := func(want, got string) {
t.Helper()
if !strings.HasPrefix(got, want) {
if len(got) > 64 {
got = got[:64]
}
t.Errorf("expected prefix %q in %q", want, got)
}
}
cmp := func(want, got string) {
t.Helper()
if got != want {
t.Errorf("want %q, got %q", want, got)
}
}
if expectErr := exported.Expect(map[string]interface{}{
"doc": func(p token.Position, doc string) { pcmp(doc, getDoc(p).Doc) },
"pkg": func(p token.Position, pkg string) { cmp(pkg, getDoc(p).Pkg) },
"decl": func(p token.Position, decl string) { cmp(decl, getDoc(p).Decl) },
"const": func(p token.Position, val string) {
d := getDoc(p)
needle := "Constant Value: " + val
if !strings.Contains(d.Doc, needle) {
t.Errorf("Expected %q in %q", needle, d.Doc)
}
},
"exported": func(p token.Position) {
for _, showUnexported := range []bool{true, false} {
*showUnexportedFields = showUnexported
d := getDoc(p)
hasUnexportedField := strings.Contains(d.Decl, "notVisible")
if hasUnexportedField != *showUnexportedFields {
t.Errorf("show unexported fields is %v, but got %q", showUnexported, d.Decl)
}
}
},
}); expectErr != nil {
t.Fatal(expectErr)
}
})
}
func TestVendoredCode(t *testing.T) {
dir := filepath.Join(".", "testdata", "withvendor")
mods := []packagestest.Module{
{Name: "main", Files: packagestest.MustCopyFileTree(dir)},
}
exported := packagestest.Export(t, packagestest.GOPATH, mods)
defer exported.Cleanup()
teardown := setup(exported.Config)
defer teardown()
filename := exported.File("main", "main.go")
getDoc := func(p token.Position) *Doc {
t.Helper()
doc, docErr := Run(filename, p.Offset, nil)
if docErr != nil {
t.Fatal(docErr)
}
return doc
}
compare := func(want, got string) {
if want != got {
t.Errorf("want %q, got %q", want, got)
}
}
if expectErr := exported.Expect(map[string]interface{}{
"import": func(p token.Position, path string) { compare(path, getDoc(p).Import) },
"decl": func(p token.Position, decl string) { compare(decl, getDoc(p).Decl) },
"doc": func(p token.Position, doc string) { compare(doc, getDoc(p).Doc) },
}); expectErr != nil {
t.Fatal(expectErr)
}
}