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.
13 "golang.org/x/tools/internal/span"
16 // TestURI tests the conversion between URIs and filenames. The test cases
17 // include Windows-style URIs and filepaths, but we avoid having OS-specific
18 // tests by using only forward slashes, assuming that the standard library
19 // functions filepath.ToSlash and filepath.FromSlash do not need testing.
20 func TestURIFromPath(t *testing.T) {
21 for _, test := range []struct {
28 wantURI: span.URI(""),
31 path: `C:/Windows/System32`,
32 wantFile: `C:/Windows/System32`,
33 wantURI: span.URI("file:///C:/Windows/System32"),
36 path: `C:/Go/src/bob.go`,
37 wantFile: `C:/Go/src/bob.go`,
38 wantURI: span.URI("file:///C:/Go/src/bob.go"),
41 path: `c:/Go/src/bob.go`,
42 wantFile: `C:/Go/src/bob.go`,
43 wantURI: span.URI("file:///C:/Go/src/bob.go"),
47 wantFile: `/path/to/dir`,
48 wantURI: span.URI("file:///path/to/dir"),
51 path: `/a/b/c/src/bob.go`,
52 wantFile: `/a/b/c/src/bob.go`,
53 wantURI: span.URI("file:///a/b/c/src/bob.go"),
56 path: `c:/Go/src/bob george/george/george.go`,
57 wantFile: `C:/Go/src/bob george/george/george.go`,
58 wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
61 got := span.URIFromPath(test.path)
62 if got != test.wantURI {
63 t.Errorf("URIFromPath(%q): got %q, expected %q", test.path, got, test.wantURI)
65 gotFilename := got.Filename()
66 if gotFilename != test.wantFile {
67 t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
72 func TestURIFromURI(t *testing.T) {
73 for _, test := range []struct {
74 inputURI, wantFile string
78 inputURI: `file:///c:/Go/src/bob%20george/george/george.go`,
79 wantFile: `C:/Go/src/bob george/george/george.go`,
80 wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
83 inputURI: `file:///C%3A/Go/src/bob%20george/george/george.go`,
84 wantFile: `C:/Go/src/bob george/george/george.go`,
85 wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
88 inputURI: `file:///path/to/%25p%25ercent%25/per%25cent.go`,
89 wantFile: `/path/to/%p%ercent%/per%cent.go`,
90 wantURI: span.URI(`file:///path/to/%25p%25ercent%25/per%25cent.go`),
93 inputURI: `file:///C%3A/`,
95 wantURI: span.URI(`file:///C:/`),
100 wantURI: span.URI(`file:///`),
103 inputURI: `file://wsl%24/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
104 wantFile: `/wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
105 wantURI: span.URI(`file:///wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`),
108 got := span.URIFromURI(test.inputURI)
109 if got != test.wantURI {
110 t.Errorf("NewURI(%q): got %q, expected %q", test.inputURI, got, test.wantURI)
112 gotFilename := got.Filename()
113 if gotFilename != test.wantFile {
114 t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)