1
0
Fork 0
mirror of https://github.com/laravel-ls/uri synced 2026-06-16 01:54:57 +02:00

uri.go: add IsFileURI()

This commit is contained in:
Henrik Hautakoski 2025-05-22 21:30:09 +02:00
parent 6c8dc7b70e
commit 9ec02972e6
2 changed files with 47 additions and 1 deletions

6
uri.go
View file

@ -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)
}

View file

@ -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)
}
})
}
}