/
hover_test.go
114 lines (99 loc) · 4.95 KB
/
hover_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
package api
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
bundles "github.com/sourcegraph/sourcegraph/internal/codeintel/bundles/client"
bundlemocks "github.com/sourcegraph/sourcegraph/internal/codeintel/bundles/mocks"
"github.com/sourcegraph/sourcegraph/internal/codeintel/db"
dbmocks "github.com/sourcegraph/sourcegraph/internal/codeintel/db/mocks"
gitservermocks "github.com/sourcegraph/sourcegraph/internal/codeintel/gitserver/mocks"
)
func TestHover(t *testing.T) {
mockDB := dbmocks.NewMockDB()
mockBundleManagerClient := bundlemocks.NewMockBundleManagerClient()
mockBundleClient := bundlemocks.NewMockBundleClient()
mockGitserverClient := gitservermocks.NewMockClient()
setMockDBGetDumpByID(t, mockDB, map[int]db.Dump{42: testDump1})
setMockBundleManagerClientBundleClient(t, mockBundleManagerClient, map[int]bundles.BundleClient{42: mockBundleClient})
setMockBundleClientHover(t, mockBundleClient, "main.go", 10, 50, "text", testRange1, true)
api := testAPI(mockDB, mockBundleManagerClient, mockGitserverClient)
text, r, exists, err := api.Hover(context.Background(), "sub1/main.go", 10, 50, 42)
if err != nil {
t.Fatalf("expected error getting hover text: %s", err)
}
if !exists {
t.Fatalf("expected hover text to exist.")
}
if text != "text" {
t.Errorf("unexpected text. want=%s have=%s", "text", text)
}
if diff := cmp.Diff(testRange1, r); diff != "" {
t.Errorf("unexpected range (-want +got):\n%s", diff)
}
}
func TestHoverUnknownDump(t *testing.T) {
mockDB := dbmocks.NewMockDB()
mockBundleManagerClient := bundlemocks.NewMockBundleManagerClient()
mockGitserverClient := gitservermocks.NewMockClient()
setMockDBGetDumpByID(t, mockDB, nil)
api := testAPI(mockDB, mockBundleManagerClient, mockGitserverClient)
if _, _, _, err := api.Hover(context.Background(), "sub1/main.go", 10, 50, 42); err != ErrMissingDump {
t.Fatalf("unexpected error getting hover text. want=%q have=%q", ErrMissingDump, err)
}
}
func TestHoverRemoteDefinitionHoverText(t *testing.T) {
mockDB := dbmocks.NewMockDB()
mockBundleManagerClient := bundlemocks.NewMockBundleManagerClient()
mockBundleClient1 := bundlemocks.NewMockBundleClient()
mockBundleClient2 := bundlemocks.NewMockBundleClient()
mockGitserverClient := gitservermocks.NewMockClient()
setMockDBGetDumpByID(t, mockDB, map[int]db.Dump{42: testDump1, 50: testDump2})
setMockBundleManagerClientBundleClient(t, mockBundleManagerClient, map[int]bundles.BundleClient{42: mockBundleClient1, 50: mockBundleClient2})
setMockBundleClientHover(t, mockBundleClient1, "main.go", 10, 50, "", bundles.Range{}, false)
setMockBundleClientDefinitions(t, mockBundleClient1, "main.go", 10, 50, nil)
setMockBundleClientMonikersByPosition(t, mockBundleClient1, "main.go", 10, 50, [][]bundles.MonikerData{{testMoniker1}})
setMockBundleClientPackageInformation(t, mockBundleClient1, "main.go", "1234", testPackageInformation)
setMockDBGetPackage(t, mockDB, "gomod", "leftpad", "0.1.0", testDump2, true)
setMockBundleClientMonikerResults(t, mockBundleClient2, "definition", "gomod", "pad", 0, 100, []bundles.Location{
{DumpID: 50, Path: "foo.go", Range: testRange1},
{DumpID: 50, Path: "bar.go", Range: testRange2},
{DumpID: 50, Path: "baz.go", Range: testRange3},
}, 15)
setMockBundleClientHover(t, mockBundleClient2, "foo.go", 10, 50, "text", testRange4, true)
api := testAPI(mockDB, mockBundleManagerClient, mockGitserverClient)
text, r, exists, err := api.Hover(context.Background(), "sub1/main.go", 10, 50, 42)
if err != nil {
t.Fatalf("expected error getting hover text: %s", err)
}
if !exists {
t.Fatalf("expected hover text to exist.")
}
if text != "text" {
t.Errorf("unexpected text. want=%s have=%s", "text", text)
}
if diff := cmp.Diff(testRange4, r); diff != "" {
t.Errorf("unexpected range (-want +got):\n%s", diff)
}
}
func TestHoverUnknownDefinition(t *testing.T) {
mockDB := dbmocks.NewMockDB()
mockBundleManagerClient := bundlemocks.NewMockBundleManagerClient()
mockBundleClient := bundlemocks.NewMockBundleClient()
mockGitserverClient := gitservermocks.NewMockClient()
setMockDBGetDumpByID(t, mockDB, map[int]db.Dump{42: testDump1})
setMockBundleManagerClientBundleClient(t, mockBundleManagerClient, map[int]bundles.BundleClient{42: mockBundleClient})
setMockBundleClientHover(t, mockBundleClient, "main.go", 10, 50, "", bundles.Range{}, false)
setMockBundleClientDefinitions(t, mockBundleClient, "main.go", 10, 50, nil)
setMockBundleClientMonikersByPosition(t, mockBundleClient, "main.go", 10, 50, [][]bundles.MonikerData{{testMoniker1}})
setMockBundleClientPackageInformation(t, mockBundleClient, "main.go", "1234", testPackageInformation)
setMockDBGetPackage(t, mockDB, "gomod", "leftpad", "0.1.0", db.Dump{}, false)
api := testAPI(mockDB, mockBundleManagerClient, mockGitserverClient)
_, _, exists, err := api.Hover(context.Background(), "sub1/main.go", 10, 50, 42)
if err != nil {
t.Fatalf("unexpected error getting hover text: %s", err)
}
if exists {
t.Errorf("unexpected hover text")
}
}