@@ -3,118 +3,120 @@ package index
33import (
44 "go/ast"
55 "go/token"
6+ "slices"
67 "testing"
78
89 "golang.org/x/tools/go/packages"
910)
1011
11- func TestIndexer_findBestPackageDefinitionPath (t * testing.T ) {
12+ func TestFindPackageDocs (t * testing.T ) {
1213 type FileInfo struct {
13- Name string
14- Docs bool
14+ Name string
15+ DocText string
1516 }
1617
17- makePackage := func (pkgName string , fileInfo []FileInfo ) ( * packages.Package , map [ * ast. File ] string ) {
18+ makePackage := func (pkgName string , fileInfo []FileInfo ) * packages.Package {
1819 fset := token .NewFileSet ()
20+ var syntax []* ast.File
1921
20- files := map [string ]* ast.File {}
21- syntax := []* ast.File {}
22- posMap := map [* ast.File ]string {}
23-
24- for idx , info := range fileInfo {
22+ for _ , info := range fileInfo {
2523 var doc * ast.CommentGroup
26- if info .Docs {
27- doc = & ast.CommentGroup {}
28- }
29-
30- f := & ast.File {
31- Doc : doc ,
32- Package : 0 ,
33- Name : & ast.Ident {
34- NamePos : token .Pos (idx ),
35- Name : pkgName ,
36- Obj : & ast.Object {},
37- },
24+ if info .DocText != "" {
25+ doc = & ast.CommentGroup {
26+ List : []* ast.Comment {{Text : "// " + info .DocText }},
27+ }
3828 }
3929
40- files [info .Name ] = f
41- syntax = append (syntax , f )
42- posMap [f ] = info .Name
30+ tf := fset .AddFile (info .Name , fset .Base (), 1 )
4331
44- fset .AddFile (info .Name , fset .Base (), 1 )
32+ syntax = append (syntax , & ast.File {
33+ Doc : doc ,
34+ Package : tf .Pos (0 ),
35+ Name : & ast.Ident {Name : pkgName },
36+ })
4537 }
4638
4739 return & packages.Package {
48- ID : "test-package" ,
4940 Name : pkgName ,
5041 PkgPath : "test-package" ,
51- Imports : map [string ]* packages.Package {},
5242 Fset : fset ,
5343 Syntax : syntax ,
54- }, posMap
44+ }
5545 }
5646
57- makeTest := func (name , pkgName , expected string , fileInfo []FileInfo ) {
58- t .Run (name , func (t * testing.T ) {
59- pkg , names := makePackage (pkgName , fileInfo )
60-
61- pkgToken , _ := findBestPackageDefinitionPath (pkg )
62- if name := names [pkgToken ]; name != expected {
63- t .Errorf ("incorrect hover text documentation. want=%s have=%s" , name , expected )
64- }
65- })
47+ // doc produces the text that ast.CommentGroup.Text() returns for a "// text" comment.
48+ doc := func (text string ) string {
49+ return text + "\n "
6650 }
6751
68- makeTest ("Should find exact name match" ,
69- "smol" ,
70- "smol.go" ,
71- []FileInfo {
72- {"smol.go" , false },
73- {"other.go" , false },
74- },
75- )
52+ t .Run ("returns nil when no file has docs" , func (t * testing.T ) {
53+ pkg := makePackage ("smol" , []FileInfo {
54+ {"smol.go" , "" },
55+ {"other.go" , "" },
56+ })
57+ if docs := findPackageDocs (pkg ); docs != nil {
58+ t .Errorf ("expected nil, got %v" , docs )
59+ }
60+ })
7661
77- makeTest ("Should return something even if nothing matches" ,
78- "smol" ,
79- "random.go" ,
80- []FileInfo {
81- {"random.go" , false },
82- },
83- )
62+ t .Run ("returns nil for empty syntax" , func (t * testing.T ) {
63+ pkg := makePackage ("mylib" , []FileInfo {})
64+ if docs := findPackageDocs (pkg ); docs != nil {
65+ t .Errorf ("expected nil, got %v" , docs )
66+ }
67+ })
8468
85- makeTest ("Should not pick _test files if package is not a test package" ,
86- "unreleated" ,
87- "smol.go" ,
88- []FileInfo {
89- {"smol.go" , false },
90- {"smol_test.go" , false },
91- },
92- )
69+ t .Run ("returns single doc" , func (t * testing.T ) {
70+ pkg := makePackage ("mylib" , []FileInfo {
71+ {"mylib.go" , "" },
72+ {"has_docs.go" , "Package docs" },
73+ })
74+ want := []string {doc ("Package docs" )}
75+ got := findPackageDocs (pkg )
76+ if ! slices .Equal (got , want ) {
77+ t .Errorf ("want %v, got %v" , want , got )
78+ }
79+ })
9380
94- makeTest ("Pick whatever has documentation" ,
95- "mylib" ,
96- "has_docs.go" ,
97- []FileInfo {
98- {"mylib.go" , false },
99- {"has_docs.go" , true },
100- },
101- )
81+ t .Run ("returns all docs sorted with doc.go first" , func (t * testing.T ) {
82+ pkg := makePackage ("mylib" , []FileInfo {
83+ {"mylib.go" , "from mylib" },
84+ {"doc.go" , "from doc.go" },
85+ {"other.go" , "from other" },
86+ })
87+ want := []string {doc ("from doc.go" ), doc ("from mylib" ), doc ("from other" )}
88+ got := findPackageDocs (pkg )
89+ if ! slices .Equal (got , want ) {
90+ t .Errorf ("want %v, got %v" , want , got )
91+ }
92+ })
10293
103- makeTest ("should pick a name that is a closer edit distance than one far away" ,
104- "http_router" ,
105- "httprouter.go" ,
106- []FileInfo {
107- {"httprouter.go" , false },
108- {"httpother.go" , false },
109- },
110- )
94+ t .Run ("returns all docs sorted with package name match first when no doc.go" , func (t * testing.T ) {
95+ pkg := makePackage ("mylib" , []FileInfo {
96+ {"other.go" , "from other" },
97+ {"mylib.go" , "from mylib" },
98+ })
99+ want := []string {doc ("from mylib" ), doc ("from other" )}
100+ got := findPackageDocs (pkg )
101+ if ! slices .Equal (got , want ) {
102+ t .Errorf ("want %v, got %v" , want , got )
103+ }
104+ })
105+ }
111106
112- makeTest ("should prefer test packages over other packages if the package name has test suffix" ,
113- "mylib_test" ,
114- "mylib_test.go" ,
115- []FileInfo {
116- {"mylib_test.go" , false },
117- {"mylib.go" , false },
118- },
119- )
107+ func TestFileRelevance (t * testing.T ) {
108+ tests := []struct {
109+ filename string
110+ want int
111+ }{
112+ {"doc.go" , 0 },
113+ {"mylib.go" , 1 },
114+ {"other.go" , 2 },
115+ {"other_test.go" , 3 },
116+ }
117+ for _ , tt := range tests {
118+ if got := fileRelevance ("mylib" , tt .filename ); got != tt .want {
119+ t .Errorf ("fileRelevance(%q) = %d, want %d" , tt .filename , got , tt .want )
120+ }
121+ }
120122}
0 commit comments