1
0
mirror of https://github.com/golang/go synced 2024-09-30 16:18:35 -06:00
go/internal/span/uri_test.go
Ian Cottrell 25a4f13759 internal/span: handle empty paths correctly
Change-Id: I3f411c5e27bcada26a756485a3b9de3514ac7955
Reviewed-on: https://go-review.googlesource.com/c/tools/+/181677
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-06-11 15:43:01 +00:00

51 lines
1.3 KiB
Go

// 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{
``,
`C:/Windows/System32`,
`C:/Go/src/bob.go`,
`c:/Go/src/bob.go`,
`/path/to/dir`,
`/a/b/c/src/bob.go`,
} {
testPath := filepath.FromSlash(test)
expectPath := testPath
if len(test) > 0 && test[0] == '/' {
if abs, err := filepath.Abs(expectPath); err == nil {
expectPath = abs
}
}
expectURI := filepath.ToSlash(expectPath)
if len(expectURI) > 0 {
if expectURI[0] != '/' {
expectURI = "/" + expectURI
}
expectURI = "file://" + expectURI
}
uri := span.FileURI(testPath)
if expectURI != string(uri) {
t.Errorf("ToURI: expected %s, got %s", expectURI, uri)
}
filename := uri.Filename()
if expectPath != filename {
t.Errorf("Filename: expected %s, got %s", expectPath, filename)
}
}
}