2019-02-12 11:13:49 -07:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package span_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestURI tests the conversion between URIs and filenames. The test cases
|
|
|
|
// include Windows-style URIs and filepaths, but we avoid having OS-specific
|
|
|
|
// tests by using only forward slashes, assuming that the standard library
|
|
|
|
// functions filepath.ToSlash and filepath.FromSlash do not need testing.
|
|
|
|
func TestURI(t *testing.T) {
|
|
|
|
for _, test := range []string{
|
2019-06-11 09:17:26 -06:00
|
|
|
``,
|
2019-02-12 11:13:49 -07:00
|
|
|
`C:/Windows/System32`,
|
|
|
|
`C:/Go/src/bob.go`,
|
|
|
|
`c:/Go/src/bob.go`,
|
|
|
|
`/path/to/dir`,
|
|
|
|
`/a/b/c/src/bob.go`,
|
2019-09-16 08:49:42 -06:00
|
|
|
`c:/Go/src/bob george/george/george.go`,
|
2019-02-12 11:13:49 -07:00
|
|
|
} {
|
|
|
|
testPath := filepath.FromSlash(test)
|
|
|
|
expectPath := testPath
|
2019-06-11 09:17:26 -06:00
|
|
|
if len(test) > 0 && test[0] == '/' {
|
2019-02-12 11:13:49 -07:00
|
|
|
if abs, err := filepath.Abs(expectPath); err == nil {
|
|
|
|
expectPath = abs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expectURI := filepath.ToSlash(expectPath)
|
2019-06-11 09:17:26 -06:00
|
|
|
if len(expectURI) > 0 {
|
|
|
|
if expectURI[0] != '/' {
|
|
|
|
expectURI = "/" + expectURI
|
|
|
|
}
|
|
|
|
expectURI = "file://" + expectURI
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
uri := span.FileURI(testPath)
|
|
|
|
if expectURI != string(uri) {
|
|
|
|
t.Errorf("ToURI: expected %s, got %s", expectURI, uri)
|
|
|
|
}
|
2019-06-06 11:51:00 -06:00
|
|
|
filename := uri.Filename()
|
2019-02-12 11:13:49 -07:00
|
|
|
if expectPath != filename {
|
|
|
|
t.Errorf("Filename: expected %s, got %s", expectPath, filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|