Debian Codegraph @

lspext/proxy_lspext_test.go

raw symbols SCIP
1 package lspext
2
3 import (
4 "encoding/json"
5 "reflect"
6 "strings"
7 "testing"
8
9 "github.com/sourcegraph/go-lsp"
10 )
11
12 func TestWalkURIFields(t *testing.T) {
13 tests := map[string][]lsp.DocumentURI{
14 `{"textDocument":{"uri":"u1"}}`: []lsp.DocumentURI{"u1"},
15 `{"uri":"u1"}`: []lsp.DocumentURI{"u1"},
16 }
17 for objStr, wantURIs := range tests {
18 var obj interface{}
19 if err := json.Unmarshal([]byte(objStr), &obj); err != nil {
20 t.Error(err)
21 continue
22 }
23
24 var uris []lsp.DocumentURI
25 collect := func(uri lsp.DocumentURI) { uris = append(uris, uri) }
26 update := func(uri lsp.DocumentURI) lsp.DocumentURI { return "XXX" }
27 WalkURIFields(obj, collect, update)
28
29 if !reflect.DeepEqual(uris, wantURIs) {
30 t.Errorf("%s: got URIs %q, want %q", objStr, uris, wantURIs)
31 }
32
33 wantObj := objStr
34 for _, uri := range uris {
35 wantObj = strings.Replace(wantObj, string(uri), "XXX", -1)
36 }
37 gotObj, err := json.Marshal(obj)
38 if err != nil {
39 t.Error(err)
40 continue
41 }
42 if string(gotObj) != wantObj {
43 t.Errorf("%s: got obj %q, want %q after updating URI pointers", objStr, gotObj, wantObj)
44 }
45 }
46 }
47
48 func TestWalkURIFields_struct(t *testing.T) {
49 v := lsp.PublishDiagnosticsParams{URI: "u1"}
50
51 var uris []lsp.DocumentURI
52 collect := func(uri lsp.DocumentURI) { uris = append(uris, uri) }
53 update := func(uri lsp.DocumentURI) lsp.DocumentURI { return "XXX" }
54 WalkURIFields(&v, collect, update)
55
56 if want := []lsp.DocumentURI{"u1"}; !reflect.DeepEqual(uris, want) {
57 t.Errorf("got %v, want %v", uris, want)
58 }
59
60 if want := "XXX"; string(v.URI) != want {
61 t.Errorf("got %q, want %q", v.URI, want)
62 }
63 }