This repository was archived by the owner on Mar 27, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathhelper.go
More file actions
125 lines (105 loc) · 2.9 KB
/
Copy pathhelper.go
File metadata and controls
125 lines (105 loc) · 2.9 KB
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
123
124
125
package testharness
import (
"context"
"os"
"os/exec"
"testing"
"github.com/sourcegraph/langserver/client"
"github.com/sourcegraph/go-langserver/pkg/lsp"
)
var ctx = context.Background()
func initTest(cmd string, tc *TestCase) (*client.LspConn, *exec.Cmd, error) {
var c *client.LspConn
var e *exec.Cmd
var err error
c, e, err = client.NewStdioClient(ctx, cmd)
dir, err := os.Getwd()
if err != nil {
c.Close()
e.Process.Kill()
return nil, nil, err
}
err = c.Initialize(ctx, dir+"/test_repos/"+tc.Repo)
if err != nil {
c.Close()
e.Process.Kill()
return nil, nil, err
}
return c, e, nil
}
// RunHoverTests executes a TestCase's hover tests.
func RunHoverTests(cmd string, tc *TestCase, t *testing.T) {
c, e, err := initTest(cmd, tc)
if err != nil {
t.Error(err)
}
defer c.Shutdown(ctx)
defer c.Close()
defer e.Process.Kill()
for i, hc := range tc.HoverCases {
resp, err := c.Hover(ctx, hc.ToRequestParams())
if err != nil {
t.Errorf("Error returned for hover (case #%d): %v", i, err)
}
checkHoverResponse(i, &hc.Resp, resp, t)
}
}
func checkHoverResponse(i int, expected *lsp.Hover, got *lsp.Hover, t *testing.T) {
if expected.Range != got.Range {
t.Errorf("Range (case #%d): expected %v got %v", i, expected.Range, got.Range)
}
if len(expected.Contents) != len(got.Contents) {
t.Errorf("len(Contents) (case #%d): expected %d got %d", i, len(expected.Contents), len(got.Contents))
}
for j, c := range got.Contents {
e := expected.Contents[j]
if e != c {
t.Errorf("Content[%d] (case #%d): expected %v got %v", j, i, e, c)
}
}
}
// RunDefinitionTests executes a TestCase's definition tests.
func RunDefinitionTests(cmd string, tc *TestCase, t *testing.T) {
c, e, err := initTest(cmd, tc)
if err != nil {
t.Error(err)
}
defer c.Shutdown(ctx)
defer c.Close()
defer e.Process.Kill()
for i, dc := range tc.DefinitionCases {
resp, err := c.Definition(ctx, dc.ToRequestParams())
if err != nil {
t.Errorf("Error returned for definition (case #%d): %v", i, err)
}
checkLocationResponse(i, &dc.Resp, resp, t)
}
}
// RunReferencesTests executes a TestCase's references tests.
func RunReferencesTests(cmd string, tc *TestCase, t *testing.T) {
c, e, err := initTest(cmd, tc)
if err != nil {
t.Error(err)
}
defer c.Shutdown(ctx)
defer c.Close()
defer e.Process.Kill()
for i, rc := range tc.ReferencesCases {
resp, err := c.References(ctx, rc.ToRequestParams())
if err != nil {
t.Errorf("Error returned for definition (case #%d): %v", i, err)
}
checkLocationResponse(i, &rc.Resp, resp, t)
}
}
func checkLocationResponse(i int, expected *[]lsp.Location, got *[]lsp.Location, t *testing.T) {
if len(*expected) != len(*got) {
t.Errorf("len(Location) (case #%d): expected %d got %d", i, len(*expected), len(*got))
}
for j, l := range *got {
e := (*expected)[j]
if l != e {
t.Errorf("Location[%d] (case #%d): expected %v got %v", j, i, e, l)
}
}
}