-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuri_test.go
More file actions
85 lines (78 loc) · 1.84 KB
/
uri_test.go
File metadata and controls
85 lines (78 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUriToPath(t *testing.T) {
tests := []struct {
name string
uri string
wantUnix string
wantWin string
}{
{
name: "empty",
uri: "",
wantUnix: "",
wantWin: "",
},
{
name: "unix path",
uri: "file:///home/user/project/file.go",
wantUnix: "/home/user/project/file.go",
wantWin: "/home/user/project/file.go", // Not a valid Windows path anyway
},
{
name: "windows path",
uri: "file:///C:/Users/user/project/file.go",
wantUnix: "/C:/Users/user/project/file.go",
wantWin: "C:/Users/user/project/file.go",
},
{
name: "spaces in path",
uri: "file:///home/user/my%20project/file.go",
wantUnix: "/home/user/my project/file.go",
wantWin: "/home/user/my project/file.go",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := uriToPath(tt.uri)
if runtime.GOOS == "windows" {
// Normalize for Windows backslashes
assert.Contains(t, []string{tt.wantWin, toBackslash(tt.wantWin)}, result)
} else {
assert.Equal(t, tt.wantUnix, result)
}
})
}
}
func TestPathToUri(t *testing.T) {
if runtime.GOOS == "windows" {
t.Run("windows absolute path", func(t *testing.T) {
result := pathToURI("C:\\Users\\user\\file.go")
assert.Equal(t, "file:///C:/Users/user/file.go", result)
})
} else {
t.Run("unix absolute path", func(t *testing.T) {
result := pathToURI("/home/user/file.go")
assert.Equal(t, "file:///home/user/file.go", result)
})
}
t.Run("empty path", func(t *testing.T) {
result := pathToURI("")
assert.Equal(t, "", result)
})
}
func toBackslash(s string) string {
result := ""
for _, c := range s {
if c == '/' {
result += "\\"
} else {
result += string(c)
}
}
return result
}