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.
12 "golang.org/x/tools/internal/span"
15 // TestURI 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 {
27 wantURI: span.URI(""),
30 path: `C:/Windows/System32`,
31 wantFile: `C:/Windows/System32`,
32 wantURI: span.URI("file:///C:/Windows/System32"),
35 path: `C:/Go/src/bob.go`,
36 wantFile: `C:/Go/src/bob.go`,
37 wantURI: span.URI("file:///C:/Go/src/bob.go"),
40 path: `c:/Go/src/bob.go`,
41 wantFile: `C:/Go/src/bob.go`,
42 wantURI: span.URI("file:///C:/Go/src/bob.go"),
46 wantFile: `/path/to/dir`,
47 wantURI: span.URI("file:///path/to/dir"),
50 path: `/a/b/c/src/bob.go`,
51 wantFile: `/a/b/c/src/bob.go`,
52 wantURI: span.URI("file:///a/b/c/src/bob.go"),
55 path: `c:/Go/src/bob george/george/george.go`,
56 wantFile: `C:/Go/src/bob george/george/george.go`,
57 wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
60 got := span.URIFromPath(test.path)
61 if got != test.wantURI {
62 t.Errorf("URIFromPath(%q): got %q, expected %q", test.path, got, test.wantURI)
64 gotFilename := got.Filename()
65 if gotFilename != test.wantFile {
66 t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
71 func TestURIFromURI(t *testing.T) {
72 for _, test := range []struct {
73 inputURI, wantFile string
77 inputURI: `file:///c:/Go/src/bob%20george/george/george.go`,
78 wantFile: `C:/Go/src/bob george/george/george.go`,
79 wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
82 inputURI: `file:///C%3A/Go/src/bob%20george/george/george.go`,
83 wantFile: `C:/Go/src/bob george/george/george.go`,
84 wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
87 inputURI: `file:///path/to/%25p%25ercent%25/per%25cent.go`,
88 wantFile: `/path/to/%p%ercent%/per%cent.go`,
89 wantURI: span.URI(`file:///path/to/%25p%25ercent%25/per%25cent.go`),
92 inputURI: `file:///C%3A/`,
94 wantURI: span.URI(`file:///C:/`),
99 wantURI: span.URI(`file:///`),
102 inputURI: `file://wsl%24/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
103 wantFile: `/wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
104 wantURI: span.URI(`file:///wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`),
107 got := span.URIFromURI(test.inputURI)
108 if got != test.wantURI {
109 t.Errorf("NewURI(%q): got %q, expected %q", test.inputURI, got, test.wantURI)
111 gotFilename := got.Filename()
112 if gotFilename != test.wantFile {
113 t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)