Skip to content

Commit 9ff60b0

Browse files
committed
test: Add tests for utils
1 parent d279389 commit 9ff60b0

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

internal/utils/utils_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestUriToPath(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
uri string
11+
want string
12+
wantErr bool
13+
}{
14+
{
15+
name: "valid unix path",
16+
uri: "file:///tmp/test.txt",
17+
want: "/tmp/test.txt",
18+
wantErr: false,
19+
},
20+
{
21+
name: "valid path with spaces",
22+
uri: "file:///home/user/My%20File.txt",
23+
want: "/home/user/My File.txt",
24+
wantErr: false,
25+
},
26+
{
27+
name: "unsupported scheme",
28+
uri: "https://example.com/file.txt",
29+
wantErr: true,
30+
},
31+
{
32+
name: "invalid uri",
33+
uri: "file://%zz",
34+
wantErr: true,
35+
},
36+
{
37+
name: "empty uri",
38+
uri: "",
39+
wantErr: true,
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
got, err := UriToPath(tt.uri)
46+
47+
if (err != nil) != tt.wantErr {
48+
t.Fatalf("UriToPath() error = %v, wantErr %v", err, tt.wantErr)
49+
}
50+
51+
if got != tt.want {
52+
t.Errorf("UriToPath() = %q, want %q", got, tt.want)
53+
}
54+
})
55+
}
56+
}
57+
58+
func TestPathToURI(t *testing.T) {
59+
tests := []struct {
60+
name string
61+
path string
62+
want string
63+
}{
64+
{
65+
name: "absolute unix path",
66+
path: "/tmp/test.txt",
67+
want: "file:///tmp/test.txt",
68+
},
69+
{
70+
name: "relative path",
71+
path: "tmp/test.txt",
72+
want: "file:///tmp/test.txt",
73+
},
74+
{
75+
name: "empty path",
76+
path: "",
77+
want: "file:///",
78+
},
79+
}
80+
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
got := PathToURI(tt.path)
84+
85+
if got != tt.want {
86+
t.Errorf("PathToURI() = %q, want %q", got, tt.want)
87+
}
88+
})
89+
}
90+
}
91+
92+
func TestGetIndentation(t *testing.T) {
93+
tests := []struct {
94+
name string
95+
line string
96+
want string
97+
}{
98+
{
99+
name: "spaces",
100+
line: " hello",
101+
want: " ",
102+
},
103+
{
104+
name: "tabs",
105+
line: "\t\thello",
106+
want: "\t\t",
107+
},
108+
{
109+
name: "tabs and spaces",
110+
line: " \t hello",
111+
want: " \t ",
112+
},
113+
{
114+
name: "no indentation",
115+
line: "hello",
116+
want: "",
117+
},
118+
{
119+
name: "only spaces",
120+
line: " ",
121+
want: " ",
122+
},
123+
{
124+
name: "only tabs",
125+
line: "\t\t",
126+
want: "\t\t",
127+
},
128+
{
129+
name: "empty string",
130+
line: "",
131+
want: "",
132+
},
133+
{
134+
name: "newline and tab",
135+
line: "\n\tfoo",
136+
want: "\n\t",
137+
},
138+
}
139+
140+
for _, tt := range tests {
141+
t.Run(tt.name, func(t *testing.T) {
142+
got := GetIndentation(tt.line)
143+
144+
if got != tt.want {
145+
t.Errorf("GetIndentation() = %q, want %q", got, tt.want)
146+
}
147+
})
148+
}
149+
}

0 commit comments

Comments
 (0)