diff --git a/uri.go b/uri.go index e6e1e04..33b4067 100644 --- a/uri.go +++ b/uri.go @@ -53,6 +53,10 @@ func (u URI) Filename() string { return filepath.FromSlash(filename) } +func IsFileURI(uri string) bool { + return strings.HasPrefix(uri, FileScheme+hierPart) +} + func filename(uri URI) (string, error) { u, err := url.ParseRequestURI(string(uri)) if err != nil { @@ -76,7 +80,7 @@ func New(s string) URI { s = u } - if strings.HasPrefix(s, FileScheme+hierPart) { + if IsFileURI(s) { return URI(s) } diff --git a/uri_test.go b/uri_test.go index 322a43c..dd7118d 100644 --- a/uri_test.go +++ b/uri_test.go @@ -140,3 +140,45 @@ func TestFrom(t *testing.T) { }) } } + +func TestIsFileURI(t *testing.T) { + tests := []struct { + name string + s string + want bool + }{ + { + name: "File Scheme", + s: "file://code.visualstudio.com/docs/extensions/overview.md", + want: true, + }, + { + name: "HTTP Scheme", + s: "http://code.visualstudio.com/docs/extensions/overview.md", + want: false, + }, + { + name: "Empty string", + s: "", + want: false, + }, + { + name: "Random string", + s: "8o3hsXTXRG", + want: false, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + got := IsFileURI(tt.s) + + if got != tt.want { + t.Errorf("want '%v' != got '%v'", tt.want, got) + } + }) + } +}