1
0
mirror of https://github.com/golang/go synced 2024-09-30 16:28:32 -06:00
go/internal/span/uri_test.go
Ian Cottrell b40df0fb21 internal/span: adding a source location package to unify the pos conversions
This implements a standard way of describing source locations along with
printing and parsing of those locations and conversion to and from the token.Pos
forms.

Change-Id: Ibb3df0a282bc2effb0fa8bd3a51bb0d281b2ffb1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/163778
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-03-08 14:21:31 +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 test[0] == '/' {
if abs, err := filepath.Abs(expectPath); err == nil {
expectPath = abs
}
}
expectURI := filepath.ToSlash(expectPath)
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, err := uri.Filename()
if err != nil {
t.Fatal(err)
}
if expectPath != filename {
t.Errorf("Filename: expected %s, got %s", expectPath, filename)
}
}
}