Debian Codegraph @

gopls/internal/protocol/uri_test.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build !windows
6
7 package protocol_test
8
9 import (
10 "testing"
11
12 "golang.org/x/tools/gopls/internal/protocol"
13 )
14
15 // TestURIFromPath tests the conversion between URIs and filenames. The test cases
16 // include Windows-style URIs and filepaths, but we avoid having OS-specific
17 // tests by using only forward slashes, assuming that the standard library
18 // functions filepath.ToSlash and filepath.FromSlash do not need testing.
19 func TestURIFromPath(t *testing.T) {
20 for _, test := range []struct {
21 path, wantFile string
22 wantURI protocol.DocumentURI
23 }{
24 {
25 path: ``,
26 wantFile: ``,
27 wantURI: protocol.DocumentURI(""),
28 },
29 {
30 path: `C:/Windows/System32`,
31 wantFile: `C:/Windows/System32`,
32 wantURI: protocol.DocumentURI("file:///C:/Windows/System32"),
33 },
34 {
35 path: `C:/Go/src/bob.go`,
36 wantFile: `C:/Go/src/bob.go`,
37 wantURI: protocol.DocumentURI("file:///C:/Go/src/bob.go"),
38 },
39 {
40 path: `c:/Go/src/bob.go`,
41 wantFile: `C:/Go/src/bob.go`,
42 wantURI: protocol.DocumentURI("file:///C:/Go/src/bob.go"),
43 },
44 {
45 path: `/path/to/dir`,
46 wantFile: `/path/to/dir`,
47 wantURI: protocol.DocumentURI("file:///path/to/dir"),
48 },
49 {
50 path: `/a/b/c/src/bob.go`,
51 wantFile: `/a/b/c/src/bob.go`,
52 wantURI: protocol.DocumentURI("file:///a/b/c/src/bob.go"),
53 },
54 {
55 path: `c:/Go/src/bob george/george/george.go`,
56 wantFile: `C:/Go/src/bob george/george/george.go`,
57 wantURI: protocol.DocumentURI("file:///C:/Go/src/bob%20george/george/george.go"),
58 },
59 } {
60 got := protocol.URIFromPath(test.path)
61 if got != test.wantURI {
62 t.Errorf("URIFromPath(%q): got %q, expected %q", test.path, got, test.wantURI)
63 }
64 gotFilename := got.Path()
65 if gotFilename != test.wantFile {
66 t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
67 }
68 }
69 }
70
71 func TestParseDocumentURI(t *testing.T) {
72 for _, test := range []struct {
73 input string
74 want string // string(DocumentURI) on success or error.Error() on failure
75 wantPath string // expected DocumentURI.Path on success
76 }{
77 {
78 input: `file:///c:/Go/src/bob%20george/george/george.go`,
79 want: "file:///C:/Go/src/bob%20george/george/george.go",
80 wantPath: `C:/Go/src/bob george/george/george.go`,
81 },
82 {
83 input: `file:///C%3A/Go/src/bob%20george/george/george.go`,
84 want: "file:///C:/Go/src/bob%20george/george/george.go",
85 wantPath: `C:/Go/src/bob george/george/george.go`,
86 },
87 {
88 input: `file:///path/to/%25p%25ercent%25/per%25cent.go`,
89 want: `file:///path/to/%25p%25ercent%25/per%25cent.go`,
90 wantPath: `/path/to/%p%ercent%/per%cent.go`,
91 },
92 {
93 input: `file:///C%3A/`,
94 want: `file:///C:/`,
95 wantPath: `C:/`,
96 },
97 {
98 input: `file:///`,
99 want: `file:///`,
100 wantPath: `/`,
101 },
102 {
103 input: `file://wsl%24/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
104 want: `file:///wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
105 wantPath: `/wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
106 },
107 {
108 input: "",
109 want: "",
110 wantPath: "",
111 },
112 // Errors:
113 {
114 input: "https://go.dev/",
115 want: "DocumentURI scheme is not 'file': https://go.dev/",
116 },
117 } {
118 uri, err := protocol.ParseDocumentURI(test.input)
119 var got string
120 if err != nil {
121 got = err.Error()
122 } else {
123 got = string(uri)
124 }
125 if got != test.want {
126 t.Errorf("ParseDocumentURI(%q): got %q, want %q", test.input, got, test.want)
127 }
128 if err == nil && uri.Path() != test.wantPath {
129 t.Errorf("DocumentURI(%s).Path = %q, want %q", uri,
130 uri.Path(), test.wantPath)
131 }
132 }
133 }