File: proxy_lspext_test.go

package info (click to toggle)
golang-github-sourcegraph-go-lsp 0.0~git20200429.219e11d-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 168 kB
  • sloc: makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,626 bytes parent folder | download
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
package lspext

import (
	"encoding/json"
	"reflect"
	"strings"
	"testing"

	"github.com/sourcegraph/go-lsp"
)

func TestWalkURIFields(t *testing.T) {
	tests := map[string][]lsp.DocumentURI{
		`{"textDocument":{"uri":"u1"}}`: []lsp.DocumentURI{"u1"},
		`{"uri":"u1"}`:                  []lsp.DocumentURI{"u1"},
	}
	for objStr, wantURIs := range tests {
		var obj interface{}
		if err := json.Unmarshal([]byte(objStr), &obj); err != nil {
			t.Error(err)
			continue
		}

		var uris []lsp.DocumentURI
		collect := func(uri lsp.DocumentURI) { uris = append(uris, uri) }
		update := func(uri lsp.DocumentURI) lsp.DocumentURI { return "XXX" }
		WalkURIFields(obj, collect, update)

		if !reflect.DeepEqual(uris, wantURIs) {
			t.Errorf("%s: got URIs %q, want %q", objStr, uris, wantURIs)
		}

		wantObj := objStr
		for _, uri := range uris {
			wantObj = strings.Replace(wantObj, string(uri), "XXX", -1)
		}
		gotObj, err := json.Marshal(obj)
		if err != nil {
			t.Error(err)
			continue
		}
		if string(gotObj) != wantObj {
			t.Errorf("%s: got obj %q, want %q after updating URI pointers", objStr, gotObj, wantObj)
		}
	}
}

func TestWalkURIFields_struct(t *testing.T) {
	v := lsp.PublishDiagnosticsParams{URI: "u1"}

	var uris []lsp.DocumentURI
	collect := func(uri lsp.DocumentURI) { uris = append(uris, uri) }
	update := func(uri lsp.DocumentURI) lsp.DocumentURI { return "XXX" }
	WalkURIFields(&v, collect, update)

	if want := []lsp.DocumentURI{"u1"}; !reflect.DeepEqual(uris, want) {
		t.Errorf("got %v, want %v", uris, want)
	}

	if want := "XXX"; string(v.URI) != want {
		t.Errorf("got %q, want %q", v.URI, want)
	}
}